File: test_clean_doctype.rb

package info (click to toggle)
ruby-sanitize 7.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 324 kB
  • sloc: ruby: 3,153; makefile: 6
file content (72 lines) | stat: -rw-r--r-- 2,644 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
# frozen_string_literal: true

require_relative "common"

describe "Sanitize::Transformers::CleanDoctype" do
  make_my_diffs_pretty!
  parallelize_me!

  describe "when :allow_doctype is false" do
    before do
      @s = Sanitize.new(allow_doctype: false, elements: ["html"])
    end

    it "should remove doctype declarations" do
      _(@s.document("<!DOCTYPE html><html>foo</html>")).must_equal "<html>foo</html>"
      _(@s.fragment("<!DOCTYPE html>foo")).must_equal "foo"
    end

    it "should not allow doctype definitions in fragments" do
      _(@s.fragment("<!DOCTYPE html><html>foo</html>"))
        .must_equal "foo"

      _(@s.fragment('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html>foo</html>'))
        .must_equal "foo"

      _(@s.fragment("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html>foo</html>"))
        .must_equal "foo"
    end
  end

  describe "when :allow_doctype is true" do
    before do
      @s = Sanitize.new(allow_doctype: true, elements: ["html"])
    end

    it "should allow doctype declarations in documents" do
      _(@s.document("<!DOCTYPE html><html>foo</html>"))
        .must_equal "<!DOCTYPE html><html>foo</html>"

      _(@s.document('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html>foo</html>'))
        .must_equal "<!DOCTYPE html><html>foo</html>"

      _(@s.document("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html>foo</html>"))
        .must_equal "<!DOCTYPE html><html>foo</html>"
    end

    it "should not allow obviously invalid doctype declarations in documents" do
      _(@s.document("<!DOCTYPE blah blah blah><html>foo</html>"))
        .must_equal "<!DOCTYPE html><html>foo</html>"

      _(@s.document("<!DOCTYPE blah><html>foo</html>"))
        .must_equal "<!DOCTYPE html><html>foo</html>"

      _(@s.document('<!DOCTYPE html BLAH "-//W3C//DTD HTML 4.01//EN"><html>foo</html>'))
        .must_equal "<!DOCTYPE html><html>foo</html>"

      _(@s.document("<!whatever><html>foo</html>"))
        .must_equal "<html>foo</html>"
    end

    it "should not allow doctype definitions in fragments" do
      _(@s.fragment("<!DOCTYPE html><html>foo</html>"))
        .must_equal "foo"

      _(@s.fragment('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"><html>foo</html>'))
        .must_equal "foo"

      _(@s.fragment("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html>foo</html>"))
        .must_equal "foo"
    end
  end
end