Skip to content
This repository

Remove j alias for ERB::Util.json_escape #3578

Merged
merged 1 commit into from over 1 year ago

7 participants

Akira Matsuda José Valim Jeremy Kemper John Firebaugh Dave Rapin dlee Rafael Mendonça França
Akira Matsuda
Collaborator

I noticed that this newly added j method https://github.com/rails/rails/blob/6e87281b5f/actionpack/lib/action_view/helpers/javascript_helper.rb#L37
overwrites existing another j method
https://github.com/rails/rails/blob/6e87281b5f/activesupport/lib/active_support/core_ext/string/output_safety.rb#L58
in ActionView context.

FYI: It seems this global j is called in other contexts. https://github.com/flori/json/blob/b50b1bdeae/lib/json/common.rb#L395

Attached a patch that removes j alias for json_escape because this j is no longer available for app developers, and not used inside the framework (maybe those who are using output_safety.rb out of the Rails box would be affected... but who does?).

José Valim
Owner

While I agree it is confusing having two methods called j, j is also a module function for ERB::Util. And it is common for ERB::Util have aliases based on the escape method: http://www.ruby-doc.org/stdlib-1.9.2/libdoc/erb/rdoc/ERB/Util.html

It also freaks me out that some methods are called json_escape and others escape_javascript (I always get confused). It also annoys me they are in different files. Maybe the best solution would be:

1) Move escape_javascript to ERB::Util as well (or move all to AV::Helpers::EscapeHelper)
2) Alias escape_javascript to js
3) Deprecate j in all cases

But I am not sure. What do you think? /cc @jeremy

Jeremy Kemper
Owner

j mirrors h and is meant to only work in views. Global Kernel#j seems terrible: https://github.com/flori/json/blob/b50b1bdeae/lib/json/common.rb#L395

+1 to consolidating the implementations, in any case.

John Firebaugh

My preference would be to consolidate in EscapeHelper, alias escape_javascript to js, and alias json_escape to j (with the much more useful definition provided in #6094).

Dave Rapin
rapind commented

+1

dlee
dlee commented

If j is changing to js and another j is taking its place, the documentation has to be eye popping since it'll surely be confusing.

I would personally recommend leaving j aliased to escape_javascript, since it has meant escape_javascript for ages.

Rafael Mendonça França rafaelfranca merged commit d65a15d into from
Rafael Mendonça França rafaelfranca closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Nov 09, 2011
Akira Matsuda Remove j alias for ERB::Util.json_escape ed2eac8
This page is out of date. Refresh to see the latest.
7  activesupport/lib/active_support/core_ext/string/output_safety.rb
@@ -45,18 +45,11 @@ def html_escape(s)
45 45
     #   json_escape('{"name":"john","created_at":"2010-04-28T01:39:31Z","id":1}')
46 46
     #   # => {name:john,created_at:2010-04-28T01:39:31Z,id:1}
47 47
     #
48  
-    # This method is also aliased as +j+, and available as a helper
49  
-    # in Rails templates:
50  
-    #
51  
-    #   <%=j @person.to_json %>
52  
-    #
53 48
     def json_escape(s)
54 49
       result = s.to_s.gsub(/[&"><]/) { |special| JSON_ESCAPE[special] }
55 50
       s.html_safe? ? result.html_safe : result
56 51
     end
57 52
 
58  
-    alias j json_escape
59  
-    module_function :j
60 53
     module_function :json_escape
61 54
   end
62 55
 end
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.