-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
to make Nokogiri instances act like Loofah instances. These classes have a new instance method, `#acts_as_loofah`: - Nokogiri::XML::Document - Nokogiri::XML::DocumentFragment - Nokogiri::HTML4::Document - Nokogiri::HTML4::DocumentFragment - Nokogiri::HTML5::Document - Nokogiri::HTML5::DocumentFragment This method extends the Nokogiri object so that it quacks like the corresponding Loofah object. Note that this method will also extend any existing child element objects, just as if those objects were created as children of a Loofah document.
- Loading branch information
1 parent
8599060
commit a397eda
Showing
6 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
require "helper" | ||
|
||
class UnitTestActsAsLoofah < Loofah::TestCase | ||
SUBJECTS = [Nokogiri::XML, Nokogiri::HTML4, defined?(Nokogiri::HTML5) && Nokogiri::HTML5].compact | ||
|
||
SUBJECTS.each do |subject| | ||
describe subject do | ||
it "Document act like Loofah" do | ||
ndoc = subject::Document.parse("<html><body><div>hello</div><span>hello</span><script>alert(1)</script></body></html>") | ||
node = ndoc.at_css("div") | ||
|
||
# method presence | ||
refute_respond_to(ndoc, :scrub!) | ||
refute_respond_to(node, :scrub!) | ||
|
||
ndoc.acts_as_loofah | ||
|
||
assert_respond_to(ndoc, :scrub!, "#{subject}::Document should be extended") | ||
assert_respond_to(ndoc.at_css("span"), :scrub!, "New child elements should be extended") | ||
assert_respond_to(node, :scrub!, "Existing child elements should be extended") | ||
|
||
refute_respond_to(subject::Document.parse("<div>"), :scrub!, "Other instances should not be extended") | ||
|
||
# scrub behavior | ||
ndoc.scrub!(:prune) | ||
|
||
refute_includes(ndoc.to_html, "script") | ||
|
||
# other concerns | ||
if subject.name.include?("HTML") | ||
assert_includes(ndoc.singleton_class.ancestors, Loofah::TextBehavior) | ||
assert_includes(ndoc.singleton_class.ancestors, Loofah::HtmlDocumentBehavior) | ||
end | ||
end | ||
|
||
it "DocumentFragment act like Loofah" do | ||
nfrag = subject::DocumentFragment.parse("<div>hello</div><span>hello</span><script>alert(1)</script>") | ||
node = nfrag.at_css("div") | ||
|
||
# method presence | ||
refute_respond_to(nfrag, :scrub!) | ||
refute_respond_to(node, :scrub!) | ||
|
||
nfrag.acts_as_loofah | ||
|
||
assert_respond_to(nfrag, :scrub!, "#{subject}::DocumentFragment should be extended") | ||
assert_respond_to(nfrag.at_css("span"), :scrub!, "New child elements should be extended") | ||
assert_respond_to(node, :scrub!, "Existing child elements should be extended") | ||
|
||
refute_respond_to(subject::DocumentFragment.parse("<div>"), :scrub!, "Other instances should not be extended") | ||
|
||
# scrub behavior | ||
nfrag.scrub!(:prune) | ||
|
||
refute_includes(nfrag.to_html, "script") | ||
|
||
# other concerns | ||
if subject.name.include?("HTML") | ||
assert_includes(nfrag.singleton_class.ancestors, Loofah::TextBehavior) | ||
assert_includes(nfrag.singleton_class.ancestors, Loofah::HtmlFragmentBehavior) | ||
end | ||
end | ||
end | ||
end | ||
end |