diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa408f..bc763ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## next / unreleased + +### Bug fixes + +* Loofah::HTML::DocumentFragment#text no longer serializes top-level comment children. [[#221](https://github.com/flavorjones/loofah/issues/221)] + + ## 2.12.0 / 2021-08-11 ### Features diff --git a/lib/loofah/instance_methods.rb b/lib/loofah/instance_methods.rb index 8b49dbb..316ac9d 100644 --- a/lib/loofah/instance_methods.rb +++ b/lib/loofah/instance_methods.rb @@ -93,7 +93,11 @@ module TextBehavior # frag.text(:encode_special_chars => false) # => "" # def text(options = {}) - result = serialize_root.children.inner_text rescue "" + result = if serialize_root + serialize_root.children.reject(&:comment?).map(&:inner_text).join("") + else + "" + end if options[:encode_special_chars] == false result # possibly dangerous if rendered in a browser else diff --git a/test/integration/test_html.rb b/test/integration/test_html.rb index ab9746e..8ae60a8 100644 --- a/test/integration/test_html.rb +++ b/test/integration/test_html.rb @@ -3,18 +3,36 @@ class IntegrationTestHtml < Loofah::TestCase context "html fragment" do context "#to_s" do - it "not include head tags (like style)" do - skip "depends on nokogiri version" - html = Loofah.fragment "