File: test_comments.rb

package info (click to toggle)
ruby-nokogiri 1.11.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,576 kB
  • sloc: xml: 28,086; ruby: 18,456; java: 13,067; ansic: 5,138; yacc: 265; sh: 208; makefile: 27
file content (204 lines) | stat: -rw-r--r-- 9,003 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
require "helper"

module Nokogiri
  module HTML
    # testing error edge cases of HTML comments from the living WHATWG spec
    # as of 2020-08-03
    # https://html.spec.whatwg.org/multipage/parsing.html
    class TestComment < Nokogiri::TestCase
      # https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-closing-of-empty-comment
      #
      # This error occurs if the parser encounters an empty comment
      # that is abruptly closed by a U+003E (>) code point (i.e.,
      # <!--> or <!--->). The parser behaves as if the comment is
      # closed correctly.
      describe "abrupt closing of empty comment" do
        let(:doc) { Nokogiri::HTML(html) }
        let(:subject) { doc.at_css("div#under-test") }
        let(:other_div) { doc.at_css("div#also-here") }

        describe "two dashes" do
          let(:html) { "<html><body><div id=under-test><!--></div><div id=also-here></div></body></html>" }

          if Nokogiri.uses_libxml?
            it "behaves as if the comment is unterminated and doesn't exist" do # NON-COMPLIANT
              assert_equal 0, subject.children.length
              assert_equal 1, doc.errors.length
              assert_match(/Comment not terminated/, doc.errors.first.to_s)
              assert !other_div
            end
          end

          if Nokogiri.jruby?
            it "behaves as if the comment is closed correctly" do # COMPLIANT
              assert_equal 1, subject.children.length
              assert subject.children.first.comment?
              assert_equal "", subject.children.first.content
              assert other_div
            end
          end
        end

        describe "three dashes" do
          let(:html) { "<html><body><div id=under-test><!---></div><div id=also-here></div></body></html>" }

          if Nokogiri.uses_libxml?
            it "behaves as if the comment is unterminated and doesn't exist" do # NON-COMPLIANT
              assert_equal 0, subject.children.length
              assert_equal 1, doc.errors.length
              assert_match(/Comment not terminated/, doc.errors.first.to_s)
              assert !other_div
            end
          end

          if Nokogiri.jruby?
            it "behaves as if the comment is closed correctly" do # COMPLIANT
              assert_equal 1, subject.children.length
              assert subject.children.first.comment?
              assert_equal "-", subject.children.first.content # curious
              assert other_div
            end
          end
        end

        describe "four dashes" do
          let(:html) { "<html><body><div id=under-test><!----></div><div id=also-here></div></body></html>" }

          it "behaves as if the comment is closed correctly" do # COMPLIANT
            assert_equal 1, subject.children.length
            assert subject.children.first.comment?
            assert_equal "", subject.children.first.content
            assert other_div
          end
        end
      end

      # https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-comment
      #
      # This error occurs if the parser encounters the end of the
      # input stream in a comment. The parser treats such comments as
      # if they are closed immediately before the end of the input
      # stream.
      describe "eof in comment" do
        let(:html) { "<html><body><div id=under-test><!--start of unterminated comment" }
        let(:doc) { Nokogiri::HTML(html) }
        let(:subject) { doc.at_css("div#under-test") }

        if Nokogiri.uses_libxml?
          it "behaves as if the comment is unterminated and doesn't exist" do # NON-COMPLIANT
            assert_equal 0, subject.children.length
            assert_equal 1, doc.errors.length
            assert_match(/Comment not terminated/, doc.errors.first.to_s)
          end
        end

        if Nokogiri.jruby?
          it "behaves as if the comment is closed immediately before the end of the input stream" do # COMPLIANT
            assert_equal 1, subject.children.length
            assert subject.children.first.comment?
            assert_equal "start of unterminated comment", subject.children.first.content
          end
        end
      end

      # https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-closed-comment
      #
      # This error occurs if the parser encounters a comment that is
      # closed by the "--!>" code point sequence. The parser treats
      # such comments as if they are correctly closed by the "-->"
      # code point sequence.
      describe "incorrectly closed comment" do
        let(:html) { "<html><body><div id=under-test><!--foo--!><div id=do-i-exist></div><!--bar--></div></body></html>" }
        let(:doc) { Nokogiri::HTML(html) }
        let(:subject) { doc.at_css("div#under-test") }
        let(:inner_div) { doc.at_css("div#do-i-exist") }

        if Nokogiri.uses_libxml? && Nokogiri::VersionInfo.instance.libxml2_using_packaged?
          # see patches/libxml2/0006-htmlParseComment-treat-as-if-it-closed-the-comment.patch
          it "behaves as if the comment is normally closed" do # COMPLIANT
            assert_equal 3, subject.children.length
            assert subject.children[0].comment?
            assert_equal "foo", subject.children[0].content
            assert inner_div
            assert_equal inner_div, subject.children[1]
            assert subject.children[2].comment?
            assert_equal "bar", subject.children[2].content
            assert_equal 1, doc.errors.length
            assert_match(/Comment incorrectly closed/, doc.errors.first.to_s)
          end
        end

        if Nokogiri.jruby? || Nokogiri::VersionInfo.instance.libxml2_using_system?
          # this behavior may change to the above in libxml v2.9.11 depending on whether
          # https://gitlab.gnome.org/GNOME/libxml2/-/merge_requests/82 is merged
          it "behaves as if the comment encompasses the inner div" do # NON-COMPLIANT
            assert_equal 1, subject.children.length
            assert subject.children.first.comment?
            assert !inner_div
            assert_match(/id=do-i-exist/, subject.children.first.content)
            assert_equal 0, doc.errors.length
          end
        end
      end

      # https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-opened-comment
      #
      # This error occurs if the parser encounters the "<!" code point
      # sequence that is not immidiately followed by two U+002D (-)
      # code points and that is not the start of a DOCTYPE or a CDATA
      # section. All content that follows the "<!" code point sequence
      # up to a U+003E (>) code point (if present) or to the end of
      # the input stream is treated as a comment.
      describe "incorrectly opened comment" do
        let(:html) { "<html><body><div id=under-test><! comment <div id=do-i-exist>inner content</div>-->hello</div></body></html>" }

        let(:doc) { Nokogiri::HTML(html) }
        let(:body) { doc.at_css("body") }
        let(:subject) { doc.at_css("div#under-test") }

        if Nokogiri.uses_libxml?
          it "ignores up to the next '>'" do # NON-COMPLIANT
            assert_equal 2, body.children.length
            assert_equal body.children[0], subject
            assert_equal 1, subject.children.length
            assert subject.children[0].text?
            assert_equal "inner content", subject.children[0].content
            assert body.children[1].text?
            assert_equal "-->hello", body.children[1].content
          end
        end

        if Nokogiri.jruby?
          it "ignores up to the next '-->'" do # NON-COMPLIANT
            assert_equal 1, subject.children.length
            assert subject.children[0].text?
            assert_equal "hello", subject.children[0].content
          end
        end
      end

      # https://html.spec.whatwg.org/multipage/parsing.html#parse-error-nested-comment
      #
      # This error occurs if the parser encounters a nested comment
      # (e.g., <!-- <!-- nested --> -->). Such a comment will be
      # closed by the first occuring "-->" code point sequence and
      # everything that follows will be treated as markup.
      describe "nested comment" do
        let(:html) { "<html><body><div id=under-test><!-- outer <!-- inner --><div id=do-i-exist></div>--></div></body></html>" }
        let(:doc) { Nokogiri::HTML(html) }
        let(:subject) { doc.at_css("div#under-test") }
        let(:inner_div) { doc.at_css("div#do-i-exist") }

        it "ignores to the next '-->'" do # COMPLIANT
          assert_equal 3, subject.children.length
          assert subject.children[0].comment?
          assert_equal " outer <!-- inner ", subject.children[0].content
          assert inner_div
          assert_equal inner_div, subject.children[1]
          assert subject.children[2].text?
          assert_equal "-->", subject.children[2].content
        end
      end
    end
  end
end