File: test_api.rb

package info (click to toggle)
ruby-loofah 2.2.3-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 452 kB
  • sloc: ruby: 2,153; makefile: 2
file content (142 lines) | stat: -rw-r--r-- 3,686 bytes parent folder | download | duplicates (2)
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
require "helper"

class UnitTestApi < Loofah::TestCase

  HTML          = "<div>a</div>\n<div>b</div>"
  XML_FRAGMENT  = "<div>a</div>\n<div>b</div>"
  XML           = "<root>#{XML_FRAGMENT}</root>"

  describe "HTML" do
    it "creates documents" do
      doc = Loofah.document(HTML)
      assert_html_documentish doc
    end

    it "creates fragments" do
      doc = Loofah.fragment(HTML)
      assert_html_fragmentish doc
    end

    it "parses documents" do
      doc = Loofah::HTML::Document.parse(HTML)
      assert_html_documentish doc
    end

    it "parses document fragment" do
      doc = Loofah::HTML::DocumentFragment.parse(HTML)
      assert_html_fragmentish doc
    end

    it "scrubs documents" do
      doc = Loofah.document(HTML).scrub!(:strip)
      assert_html_documentish doc
    end

    it "scrubs fragments" do
      doc = Loofah.fragment(HTML).scrub!(:strip)
      assert_html_fragmentish doc
    end

    it "scrubs document nodes" do
      doc = Loofah.document(HTML)
      assert(node = doc.at_css("div"))
      node.scrub!(:strip)
    end

    it "scrubs fragment nodes" do
      doc = Loofah.fragment(HTML)
      assert(node = doc.at_css("div"))
      node.scrub!(:strip)
    end

    it "scrubs document nodesets" do
      doc = Loofah.document(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 HTML::DocumentFragment" do
      doc = Loofah.fragment(HTML)
      assert_equal HTML, doc.serialize_root.to_html
    end

    it "exposes serialize_root on HTML::Document" do
      doc = Loofah.document(HTML)
      assert_equal HTML, doc.serialize_root.children.to_html
    end
  end

  describe "XML" do
    it "creates documents" do
      doc = Loofah.xml_document(XML)
      assert_xml_documentish doc
    end

    it "creates fragments" do
      doc = Loofah.xml_fragment(XML_FRAGMENT)
      assert_xml_fragmentish doc
    end

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

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

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

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

    it "scrubs document nodes" do
      doc = Loofah.xml_document(XML)
      assert(node = doc.at_css("div"))
      node.scrub!(:strip)
    end

    it "scrubs fragment nodes" do
      doc = Loofah.xml_fragment(XML)
      assert(node = doc.at_css("div"))
      node.scrub!(:strip)
    end
  end

  private

  def assert_html_documentish(doc)
    assert_kind_of Nokogiri::HTML::Document, doc
    assert_kind_of Loofah::HTML::Document,   doc
    assert_equal HTML, doc.xpath("/html/body").inner_html
  end

  def assert_html_fragmentish(doc)
    assert_kind_of Nokogiri::HTML::DocumentFragment, doc
    assert_kind_of Loofah::HTML::DocumentFragment,   doc
    assert_equal HTML, doc.inner_html
  end

  def assert_xml_documentish(doc)
    assert_kind_of Nokogiri::XML::Document, doc
    assert_kind_of Loofah::XML::Document,   doc
    assert_equal XML, doc.root.to_xml
  end

  def assert_xml_fragmentish(doc)
    assert_kind_of Nokogiri::XML::DocumentFragment, doc
    assert_kind_of Loofah::XML::DocumentFragment,   doc
    assert_equal XML_FRAGMENT, doc.children.to_xml
  end

end