File: test_parser.rb

package info (click to toggle)
ruby-nokogiri 1.18.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,076 kB
  • sloc: ansic: 38,893; xml: 27,665; ruby: 27,285; java: 15,348; cpp: 7,107; yacc: 244; sh: 208; makefile: 154; sed: 14
file content (115 lines) | stat: -rw-r--r-- 2,313 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
# frozen_string_literal: true

require "helper"

describe Nokogiri::CSS::Parser do
  let(:parser) { Nokogiri::CSS::Parser.new }

  it "#find_by_type" do
    ast = parser.parse("a:nth-child(2)").first
    matches = ast.find_by_type(
      [
        :CONDITIONAL_SELECTOR,
        [:ELEMENT_NAME],
        [
          :PSEUDO_CLASS,
          [:FUNCTION],
        ],
      ],
    )
    assert_equal(1, matches.length)
    assert_equal(ast, matches.first)
  end

  it "#to_type" do
    ast = parser.parse("a:nth-child(2)").first
    assert_equal(
      [
        :CONDITIONAL_SELECTOR,
        [:ELEMENT_NAME],
        [
          :PSEUDO_CLASS,
          [:FUNCTION],
        ],
      ],
      ast.to_type,
    )
  end

  it "#to_a_" do
    asts = parser.parse("a:nth-child(2)")
    assert_equal(
      [
        :CONDITIONAL_SELECTOR,
        [:ELEMENT_NAME, ["a"]],
        [
          :PSEUDO_CLASS,
          [:FUNCTION, ["nth-child("], ["2"]],
        ],
      ],
      asts.first.to_a,
    )
  end

  it "parses xpath attributes in conditional selectors" do
    ast = parser.parse("a[@class~=bar]").first
    assert_equal(
      [
        :CONDITIONAL_SELECTOR,
        [:ELEMENT_NAME, ["a"]],
        [
          :ATTRIBUTE_CONDITION,
          [:ATTRIB_NAME, ["class"]],
          [:includes],
          ["bar"],
        ],
      ],
      ast.to_a,
    )
  end

  it "parses xpath attributes" do
    ast = parser.parse("a/@href").first
    assert_equal(
      [:CHILD_SELECTOR, [:ELEMENT_NAME, ["a"]], [:ATTRIB_NAME, ["href"]]],
      ast.to_a,
    )
  end

  it "parses xpath attributes passed to xpath functions" do
    ast = parser.parse("a:foo(@href)").first
    assert_equal(
      [
        :CONDITIONAL_SELECTOR,
        [:ELEMENT_NAME, ["a"]],
        [
          :PSEUDO_CLASS,
          [
            :FUNCTION,
            ["foo("],
            [:ATTRIB_NAME, ["href"]],
          ],
        ],
      ],
      ast.to_a,
    )

    ast = parser.parse("a:foo(@href,@id)").first
    assert_equal(
      [
        :CONDITIONAL_SELECTOR,
        [:ELEMENT_NAME, ["a"]],
        [
          :PSEUDO_CLASS,
          [
            :FUNCTION,
            ["foo("],
            [:ATTRIB_NAME, ["href"]],
            [:ATTRIB_NAME, ["id"]],
          ],
        ],
      ],
      ast.to_a,
    )
  end
end