File: html_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (82 lines) | stat: -rw-r--r-- 1,826 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
require "spec"
require "xml"

describe XML do
  it "parses HTML" do
    doc = XML.parse_html(%(\
      <!doctype html>
      <html>
      <head>
          <title>Samantha</title>
      </head>
      <body>
          <h1 class="large">Boat</h1>
      </body>
      </html>
    ))

    html = doc.children[1]
    html.name.should eq("html")

    head = html.children.find! { |node| node.name == "head" }
    head.name.should eq("head")

    title = head.children.find! { |node| node.name == "title" }
    title.text.should eq("Samantha")

    body = html.children.find! { |node| node.name == "body" }

    h1 = body.children.find! { |node| node.name == "h1" }

    attrs = h1.attributes
    attrs.should_not be_empty
    attrs.size.should eq(1)

    attr = attrs[0]
    attr.name.should eq("class")
    attr.content.should eq("large")
    attr.text.should eq("large")
    attr.inner_text.should eq("large")
  end

  it "parses HTML from IO" do
    io = IO::Memory.new(%(\
      <!doctype html>
      <html>
      <head>
          <title>Samantha</title>
      </head>
      <body>
          <h1 class="large">Boat</h1>
      </body>
      </html>
    ))

    doc = XML.parse_html(io)
    html = doc.children[1]
    html.name.should eq("html")
  end

  it "parses html5 (#1404)" do
    html5 = "<html><body><nav>Test</nav></body></html>"
    xml = XML.parse_html(html5)
    xml.errors.should_not be_nil
    xml.xpath_node("//html/body/nav").should_not be_nil
  end

  it "raises error when parsing empty string (#2752)" do
    expect_raises XML::Error, "Document is empty" do
      XML.parse_html("")
    end
  end

  it "gets name of HTML document node (#4040)" do
    doc = XML.parse_html(%(\
      <!doctype html>
      <html>
      </html>
    ))
    doc.document?.should be_true
    doc.name.should eq("document")
  end
end