redcloth icon indicating copy to clipboard operation
redcloth copied to clipboard

Stop extending into ERB the `t` and `textilize`

Open dmitry opened this issue 10 years ago • 22 comments

Prevent injection of the t and textilize methods into a ERB module through the Util module. When using the i18n gem in Rails it uses textilize method, instead of translate (aliased as t) helper. It will be still possible to inject textilize into a ERB after this commit with a manual code:

begin
  require 'erb'
  require 'redcloth/erb_extension'
  include ERB::Util
rescue LoadError
end

dmitry avatar Jan 25 '15 18:01 dmitry

removing:

    alias t textilize
    module_function :t

should be enough

timoschilling avatar Jan 31 '15 02:01 timoschilling

This should work as hotfix:

require "redcloth"
class ERB::Util
  alias t translate
  module_function :t
end

timoschilling avatar Jan 31 '15 02:01 timoschilling

@jgarber can you please look into the PR? Thank you!

dmitry avatar Apr 07 '15 12:04 dmitry

@joshuasiler please merge this one.

dmitry avatar Apr 30 '16 10:04 dmitry

I am happy to take a look, however I'm not 100% clear on what problem this is solving. Can you elaborate further? Won't this break expected functionality for some users?

joshuasiler avatar Apr 30 '16 14:04 joshuasiler

@joshuasiler any specific solution?

I think most of the users instead of t they are using textile, as normally t is reserved for the translations. We could do minor release, and tell the users in the gem after install message that they have to update their code from t to textile, in other case it will break their ERB views.

dmitry avatar May 03 '16 12:05 dmitry

Including include ERB::Util in the main thread not really good idea...

dmitry avatar May 03 '16 12:05 dmitry

This makes sense to me, however I'm reluctant to suddenly change existing behavior on users and force a refactor, just based on my bad experiences with other libraries doing so and causing extra work. What about a version of this patch that adds a config flag? The config flag would default to current behavior, but display a deprecation/recommendation message to users along with the suggested setting. We could then remove it completely in a later major release.

joshuasiler avatar May 03 '16 17:05 joshuasiler

@joshuasiler great, thanks for the suggestion! I will work on the patch very soon to deprecate it via flag.

dmitry avatar May 04 '16 08:05 dmitry

This does look like it should be opt-in instead of default. A major version change should stop anyone (familiar with semver) being surprised about the API changing, and there could be docs showing how to transition / new usage.

nruth avatar May 06 '16 14:05 nruth

@nruth any idea how it could be opt-in?

dmitry avatar Nov 10 '16 16:11 dmitry

@dmitry Allowing the user to deprecate via a flag would effectively make the feature "opt-in"

joshuasiler avatar Nov 29 '16 15:11 joshuasiler

How would you opt-in: https://github.com/jgarber/redcloth/pull/19/files#diff-69f5e2690debe0fabf6b62eb702d1852L40?

dmitry avatar Nov 29 '16 17:11 dmitry

@joshuasiler and this code as well: https://github.com/jgarber/redcloth/pull/19/files#diff-d5bc76d7c8479896071db64fe1e3e9a0L22 ?

dmitry avatar Nov 29 '16 17:11 dmitry

We have RbConfig in the stack - you could use that to define a config variable, and then check for that variable and only include your mod if it's turned on.

joshuasiler avatar Nov 29 '16 17:11 joshuasiler

@joshuasiler where it should be configured in the application? In config.ru before environment/application requirement? This changes are made when the gem loads, normally with Bundle.require method (at least in rails application.rb https://github.com/bundler/bundler/blob/master/lib/bundler.rb#L108).

dmitry avatar Nov 29 '16 22:11 dmitry

Good question, and no easy answers. You're right it has to be set fairly early. Adding an optional config file would be one option. Or you could have an optional function to call like RedCloth.disable_erb_extensions() that applies the hotfix above...

joshuasiler avatar Jan 23 '17 23:01 joshuasiler

Can we proceed on this issue? I would like to push changes to the master.

dmitry avatar Jun 15 '17 15:06 dmitry

This looks like a reasonable change, I'm open to merging. As a final step can you submit an update to the readme that explains this change? I'll release as 4.4

joshuasiler avatar Jun 15 '17 23:06 joshuasiler

Also it looks like tests are failing - can you make sure they show green before we are ready to merge?

joshuasiler avatar Jun 16 '17 19:06 joshuasiler

@dmitry Are you able to complete this?

kmcphillips avatar Jul 18 '17 20:07 kmcphillips

Attention: BREAKING CHANGE RedCloth 5.x series

GregThoma avatar Jun 01 '24 11:06 GregThoma