Skip to content

Commit

Permalink
Add a new filter unescape
Browse files Browse the repository at this point in the history
Proposed in #889.
  • Loading branch information
knu committed Jul 16, 2015
1 parent 7f4bda9 commit e5860dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/concerns/liquid_interpolatable.rb
Expand Up @@ -185,6 +185,14 @@ def uri_expand(url, limit = 5)
url
end

# Unescape (basic) HTML entities in a string
#
# This currently decodes the following entities only: "'",
# """, "<", ">", "&", "&#dd;" and "&#xhh;".
def unescape(input)
CGI.unescapeHTML(input) rescue input
end

# Escape a string for use in XPath expression
def to_xpath(string)
subs = string.to_s.scan(/\G(?:\A\z|[^"]+|[^']+)/).map { |x|
Expand Down
10 changes: 10 additions & 0 deletions spec/concerns/liquid_interpolatable_spec.rb
Expand Up @@ -38,6 +38,16 @@ def validate_options
end
end

describe 'unescape' do
let(:agent) { Agents::InterpolatableAgent.new(name: "test") }

it 'should unescape basic HTML entities' do
agent.interpolation_context['something'] = ''<foo> & bar''
agent.options['cleaned'] = '{{ something | unescape }}'
expect(agent.interpolated['cleaned']).to eq("'<foo> & bar'")
end
end

describe 'to_xpath' do
before do
def @filter.to_xpath_roundtrip(string)
Expand Down

0 comments on commit e5860dd

Please sign in to comment.