File: test_api.rb

package info (click to toggle)
ruby-loofah 2.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 488 kB
  • sloc: ruby: 4,176; makefile: 4
file content (325 lines) | stat: -rw-r--r-- 9,520 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# frozen_string_literal: true

require "helper"

class UnitTestApi < Loofah::TestCase
  let(:html) { "<div>a</div>\n<div>b</div>" }
  let(:xml_fragment) { "<div>a</div>\n<div>b</div>" }
  let(:xml) { "<root>#{xml_fragment}</root>" }
  let(:xml_scrubber) do
    Loofah::Scrubber.new do |node|
      # no-op
    end
  end

  describe Loofah do
    describe "generic class methods" do
      it "creates html4 documents" do
        doc = Loofah.document(html)

        assert_kind_of(Loofah::HTML4::Document, doc)
        assert_equal html, doc.xpath("/html/body").inner_html
      end

      it "scrubs html4 documents" do
        doc = Loofah.scrub_document(html, :strip)

        assert_kind_of(Loofah::HTML4::Document, doc)
        assert_equal html, doc.xpath("/html/body").inner_html
      end

      it "creates html4 fragments" do
        doc = Loofah.fragment(html)

        assert_kind_of(Loofah::HTML4::DocumentFragment, doc)
        assert_equal html, doc.inner_html
      end

      it "scrubs html4 fragments" do
        doc = Loofah.scrub_fragment(html, :strip)

        assert_kind_of(Loofah::HTML4::DocumentFragment, doc)
        assert_equal html, doc.inner_html
      end
    end

    describe "html4 methods" do
      it "creates html4 documents" do
        doc = Loofah.html4_document(html)

        assert_kind_of(Loofah::HTML4::Document, doc)
        assert_equal html, doc.xpath("/html/body").inner_html
      end

      it "scrubs html4 documents" do
        doc = Loofah.scrub_html4_document(html, :strip)

        assert_kind_of(Loofah::HTML4::Document, doc)
        assert_equal html, doc.xpath("/html/body").inner_html
      end

      it "creates html4 fragments" do
        doc = Loofah.html4_fragment(html)

        assert_kind_of(Loofah::HTML4::DocumentFragment, doc)
        assert_equal html, doc.inner_html
      end

      it "scrubs html4 fragments" do
        doc = Loofah.scrub_html4_fragment(html, :strip)

        assert_kind_of(Loofah::HTML4::DocumentFragment, doc)
        assert_equal html, doc.inner_html
      end
    end

    describe "html5 methods" do
      if Loofah.html5_support?
        it "creates html5 documents" do
          doc = Loofah.html5_document(html)

          assert_kind_of(Loofah::HTML5::Document, doc)
          assert_equal html, doc.xpath("/html/body").inner_html
        end

        it "scrubs html5 documents" do
          doc = Loofah.scrub_html5_document(html, :strip)

          assert_kind_of(Loofah::HTML5::Document, doc)
          assert_equal html, doc.xpath("/html/body").inner_html
        end

        it "creates html5 fragments" do
          doc = Loofah.html5_fragment(html)

          assert_kind_of(Loofah::HTML5::DocumentFragment, doc)
          assert_equal html, doc.inner_html
        end

        it "scrubs html5 fragments" do
          doc = Loofah.scrub_html5_fragment(html, :strip)

          assert_kind_of(Loofah::HTML5::DocumentFragment, doc)
          assert_equal html, doc.inner_html
        end
      else
        it "raises an error" do
          assert_raises(NotImplementedError) { Loofah.html5_document(html) }
          assert_raises(NotImplementedError) { Loofah.scrub_html5_document(html, :strip) }
          assert_raises(NotImplementedError) { Loofah.html5_fragment(html) }
          assert_raises(NotImplementedError) { Loofah.scrub_html5_fragment(html, :strip) }
        end

        it "does not implement classes" do
          assert_raises(NameError) { Loofah::HTML5::Document }
          assert_raises(NameError) { Loofah::HTML5::DocumentFragment }
        end
      end
    end

    describe "xml methods" do
      it "creates xml documents" do
        doc = Loofah.xml_document(xml)

        assert_kind_of(Loofah::XML::Document, doc)
        assert_equal xml, doc.root.to_xml
      end

      it "scrubs xml documents" do
        doc = Loofah.scrub_xml_document(xml, xml_scrubber)

        assert_kind_of(Loofah::XML::Document, doc)
        assert_equal xml, doc.root.to_xml
      end

      it "creates xml fragments" do
        doc = Loofah.xml_fragment(xml_fragment)

        assert_kind_of(Loofah::XML::DocumentFragment, doc)
        assert_equal xml_fragment, doc.children.to_xml
      end

      it "scrubs xml fragments" do
        doc = Loofah.scrub_xml_fragment(xml_fragment, :strip)

        assert_kind_of(Loofah::XML::DocumentFragment, doc)
        assert_equal xml_fragment, doc.children.to_xml
      end
    end
  end

  describe Loofah::HTML4 do
    it "subclasses Nokogiri::HTML4" do
      assert_includes(Loofah::HTML4::Document.ancestors, Nokogiri::HTML4::Document)
      assert_includes(Loofah::HTML4::DocumentFragment.ancestors, Nokogiri::HTML4::DocumentFragment)
    end

    it "parses documents" do
      doc = Loofah::HTML4::Document.parse(html)

      assert_kind_of(Loofah::HTML4::Document, doc)
      assert_equal html, doc.xpath("/html/body").inner_html
    end

    it "parses document fragment" do
      doc = Loofah::HTML4::DocumentFragment.parse(html)

      assert_kind_of(Loofah::HTML4::DocumentFragment, doc)
      assert_equal html, doc.inner_html
    end

    it "scrubs documents" do
      doc = Loofah::HTML4::Document.parse(html).scrub!(:strip)

      assert_equal html, doc.xpath("/html/body").inner_html
    end

    it "scrubs fragments" do
      doc = Loofah::HTML4::DocumentFragment.parse(html).scrub!(:strip)

      assert_equal html, doc.inner_html
    end

    it "adds instance methods to documents" do
      doc = Loofah::HTML4::Document.parse(html)
      doc.scrub!(:strip)
    end

    it "adds instance methods to document nodes" do
      doc = Loofah::HTML4::Document.parse(html)

      assert(node = doc.at_css("div"))
      node.scrub!(:strip)
    end

    it "adds instance methods to fragments" do
      doc = Loofah::HTML4::DocumentFragment.parse(html)
      doc.scrub!(:strip)
    end

    it "adds instance methods to fragment nodes" do
      doc = Loofah::HTML4::DocumentFragment.parse(html)

      assert(node = doc.at_css("div"))
      node.scrub!(:strip)
    end

    it "adds instance methods to document nodesets" do
      doc = Loofah.html4_document(html)

      assert(node_set = doc.css("div"))
      assert_instance_of Nokogiri::XML::NodeSet, node_set
      node_set.scrub!(:strip)
    end

    it "adds instance methods to fragment nodesets" do
      doc = Loofah.html4_fragment(html)

      assert(node_set = doc.css("div"))
      assert_instance_of Nokogiri::XML::NodeSet, node_set
      node_set.scrub!(:strip)
    end

    it "exposes serialize_root on Loofah::HTML4::DocumentFragment" do
      doc = Loofah.html4_fragment(html)

      assert_equal html, doc.serialize_root.to_html
    end

    it "exposes serialize_root on Loofah::HTML4::Document" do
      doc = Loofah.html4_document(html)

      assert_equal html, doc.serialize_root.children.to_html
    end
  end

  describe Loofah::XML do
    it "subclasses Nokogiri::XML" do
      assert_includes(Loofah::XML::Document.ancestors, Nokogiri::XML::Document)
      assert_includes(Loofah::XML::DocumentFragment.ancestors, Nokogiri::XML::DocumentFragment)
    end

    it "parses documents" do
      doc = Loofah::XML::Document.parse(xml)

      assert_kind_of(Loofah::XML::Document, doc)
      assert_equal xml, doc.root.to_xml
    end

    it "parses document fragments" do
      doc = Loofah::XML::DocumentFragment.parse(xml_fragment)

      assert_kind_of(Loofah::XML::DocumentFragment, doc)
      assert_equal xml_fragment, doc.children.to_xml
    end

    it "scrubs documents" do
      scrubber = Loofah::Scrubber.new { |node| }
      doc = Loofah.xml_document(xml).scrub!(scrubber)

      assert_equal xml, doc.root.to_xml
    end

    it "scrubs fragments" do
      scrubber = Loofah::Scrubber.new { |node| }
      doc = Loofah.xml_fragment(xml_fragment).scrub!(scrubber)

      assert_equal xml_fragment, doc.children.to_xml
    end

    it "adds instance methods to documents" do
      doc = Loofah.xml_document(xml)
      scrubber = Loofah::Scrubber.new { |node| }
      doc.scrub!(scrubber)
    end

    it "adds instance methods to document nodes" do
      doc = Loofah.xml_document(xml)

      assert(node = doc.at_css("div"))
      node.scrub!(:strip)
    end

    it "adds instance methods to fragments" do
      doc = Loofah.xml_fragment(xml)
      doc.scrub!(:strip)
    end

    it "adds instance methods to fragment nodes" do
      doc = Loofah.xml_fragment(xml)

      assert(node = doc.at_css("div"))
      node.scrub!(:strip)
    end

    it "adds instance methods to document nodesets" do
      doc = Loofah.xml_document(xml)

      assert(node_set = doc.css("div"))
      assert_instance_of Nokogiri::XML::NodeSet, node_set
      node_set.scrub!(:strip)
    end

    it "adds instance methods to document nodesets" do
      doc = Loofah.xml_fragment(xml)

      assert(node_set = doc.css("div"))
      assert_instance_of Nokogiri::XML::NodeSet, node_set
      node_set.scrub!(:strip)
    end
  end

  describe Loofah::HTML do
    it "is aliased to Loofah::HTML4" do
      assert_equal(Loofah::HTML4, Loofah::HTML)
      assert_equal(Loofah::HTML4::Document, Loofah::HTML::Document)
      assert_equal(Loofah::HTML4::DocumentFragment, Loofah::HTML::DocumentFragment)
    end

    it "has an HTML4 name" do
      assert_equal("Loofah::HTML4", Loofah::HTML.to_s)
      assert_equal("Loofah::HTML4::Document", Loofah::HTML::Document.to_s)
      assert_equal("Loofah::HTML4::DocumentFragment", Loofah::HTML::DocumentFragment.to_s)
    end
  end
end