Skip to content

Commit

Permalink
Add :parser_options config option
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin Wang committed Sep 6, 2019
1 parent 935eb9d commit 6fd16a6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,17 @@ elements not in this array will be removed.
]
```

#### :parser_options (Hash)

[Parsing options](https://github.com/rubys/nokogumbo/tree/v2.0.1#parsing-options) supplied to `nokogumbo`.

```ruby
:parser_options => {
max_errors: -1,
max_tree_depth: -1
}
```

#### :protocols (Hash)

URL protocols to allow in specific attributes. If an attribute is listed here
Expand Down
2 changes: 1 addition & 1 deletion lib/sanitize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def fragment(html)
return '' unless html

html = preprocess(html)
frag = Nokogiri::HTML5.fragment(html)
frag = Nokogiri::HTML5.fragment(html, @config[:parser_options])
node!(frag)
to_html(frag)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/sanitize/config/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ module Config
# that all HTML will be stripped).
:elements => [],

# Parsing options supplied to nokogumbo.
# https://github.com/rubys/nokogumbo/tree/v2.0.1#parsing-options
:parser_options => {},

# URL handling protocols to allow in specific attributes. By default, no
# protocols are allowed. Use :relative in place of a protocol if you want
# to allow relative URLs sans protocol.
Expand Down
29 changes: 29 additions & 0 deletions test/test_sanitize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,29 @@
it 'should not choke on frozen fragments' do
@s.fragment('<b>foo</b>'.freeze).must_equal 'foo'
end

describe 'when html body exceeds Nokogumbo::DEFAULT_MAX_TREE_DEPTH' do
let(:content) do
content = nest_html_content('<b>foo</b>', Nokogumbo::DEFAULT_MAX_TREE_DEPTH)
"<body>#{content}</body>"
end

it 'raises an ArgumentError exception' do
assert_raises ArgumentError do
@s.fragment(content)
end
end

describe 'and :max_tree_depth of -1 is supplied in :parser_options' do
before do
@s = Sanitize.new(parser_options: { max_tree_depth: -1 })
end

it 'does not raise an ArgumentError exception' do
@s.fragment(content).must_equal 'foo'
end
end
end
end

describe '#node!' do
Expand Down Expand Up @@ -109,4 +132,10 @@
end
end
end

private

def nest_html_content(html_content, depth)
"#{'<span>' * depth}#{html_content}#{'</span>' * depth}"
end
end

0 comments on commit 6fd16a6

Please sign in to comment.