Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :parser_options config option #194

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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