Merge pull request #919 from cantino/add_unescape_filter

Add a new filter `unescape`

Akinori MUSHA 8 years ago
parent
commit
ced14e4f85
2 changed files with 18 additions and 0 deletions
  1. 8 0
      app/concerns/liquid_interpolatable.rb
  2. 10 0
      spec/concerns/liquid_interpolatable_spec.rb

+ 8 - 0
app/concerns/liquid_interpolatable.rb

@@ -185,6 +185,14 @@ module LiquidInterpolatable
185 185
       url
186 186
     end
187 187
 
188
+    # Unescape (basic) HTML entities in a string
189
+    #
190
+    # This currently decodes the following entities only: "'",
191
+    # """, "<", ">", "&", "&#dd;" and "&#xhh;".
192
+    def unescape(input)
193
+      CGI.unescapeHTML(input) rescue input
194
+    end
195
+
188 196
     # Escape a string for use in XPath expression
189 197
     def to_xpath(string)
190 198
       subs = string.to_s.scan(/\G(?:\A\z|[^"]+|[^']+)/).map { |x|

+ 10 - 0
spec/concerns/liquid_interpolatable_spec.rb

@@ -38,6 +38,16 @@ describe LiquidInterpolatable::Filters do
38 38
     end
39 39
   end
40 40
 
41
+  describe 'unescape' do
42
+    let(:agent) { Agents::InterpolatableAgent.new(name: "test") }
43
+
44
+    it 'should unescape basic HTML entities' do
45
+      agent.interpolation_context['something'] = ''<foo> & bar''
46
+      agent.options['cleaned'] = '{{ something | unescape }}'
47
+      expect(agent.interpolated['cleaned']).to eq("'<foo> & bar'")
48
+    end
49
+  end
50
+
41 51
   describe 'to_xpath' do
42 52
     before do
43 53
       def @filter.to_xpath_roundtrip(string)