File: test_attr.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 (133 lines) | stat: -rw-r--r-- 4,410 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
# frozen_string_literal: true

require "helper"

module Nokogiri
  module XML
    class TestAttr < Nokogiri::TestCase
      def test_new_raises_argerror_on_nondocument
        document = Nokogiri::XML("<root><foo/></root>")

        assert_raises(ArgumentError) do
          Nokogiri::XML::Attr.new(document.at_css("foo"), "bar")
        end
      end

      def test_content=
        xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
        address = xml.xpath("//address")[3]
        street = address.attributes["street"]
        street.content = "Y&ent1;"

        assert_equal("Y&ent1;", street.value)
      end

      #
      #  note that the set of tests around set_value include
      #  assertions on the serialized format. this is intentional.
      #
      def test_set_value_with_entity_string_in_xml_file
        xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
        address = xml.xpath("//address")[3]
        street = address.attributes["street"]
        street.value = "Y&ent1;"

        assert_equal("Y&ent1;", street.value)
        assert_equal(%{street="Y&amp;ent1;"}, street.to_xml.strip)
      end

      def test_set_value_with_entity_string_in_html_file
        html = Nokogiri::HTML("<html><body><div foo='asdf'>")
        foo = html.at_css("div").attributes["foo"]
        foo.value = "Y&ent1;"

        assert_equal("Y&ent1;", foo.value)
        assert_equal(%{foo="Y&amp;ent1;"}, foo.to_html.strip)
      end

      def test_set_value_with_blank_string_in_html_file
        html = Nokogiri::HTML("<html><body><div foo='asdf'>")
        foo = html.at_css("div").attributes["foo"]
        foo.value = ""

        assert_equal("", foo.value)
        assert_equal(%{foo=""}, foo.to_html.strip)
      end

      def test_set_value_with_nil_in_html_file
        html = Nokogiri::HTML("<html><body><div foo='asdf'>")
        foo = html.at_css("div").attributes["foo"]
        foo.value = nil

        # this may be surprising to people, see xmlGetPropNodeValueInternal
        assert_equal("", foo.value)
        if Nokogiri.uses_libxml?
          # libxml2 still emits a boolean attribute at serialize-time
          assert_equal(%{foo}, foo.to_html.strip)
        else
          # jruby does not
          assert_equal(%{foo=""}, foo.to_html.strip)
        end
      end

      def test_set_value_of_boolean_attr_with_blank_string_in_html_file
        html = Nokogiri::HTML("<html><body><div disabled='disabled'>")
        disabled = html.at_css("div").attributes["disabled"]
        disabled.value = ""

        assert_equal("", disabled.value)
        # we still emit a boolean attribute at serialize-time!
        assert_equal(%{disabled}, disabled.to_html.strip)
      end

      def test_set_value_of_boolean_attr_with_nil_in_html_file
        html = Nokogiri::HTML("<html><body><div disabled='disabled'>")
        disabled = html.at_css("div").attributes["disabled"]
        disabled.value = nil

        # this may be surprising to people, see xmlGetPropNodeValueInternal
        assert_equal("", disabled.value)
        # but we emit a boolean attribute at serialize-time
        assert_equal(%{disabled}, disabled.to_html.strip)
      end

      def test_unlink # aliased as :remove
        xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
        address = xml.xpath("/staff/employee/address").first

        assert_equal("Yes", address["domestic"])

        attr = address.attribute_nodes.first
        return_val = attr.unlink

        assert_nil(address["domestic"])
        assert_equal(attr, return_val)
      end

      def test_parsing_attribute_namespace
        doc = Nokogiri::XML(<<~EOXML)
          <root xmlns='http://google.com/' xmlns:f='http://flavorjon.es/'>
            <div f:myattr='foo'></div>
          </root>
        EOXML
        node = doc.at_css("div")
        attr = node.attributes["myattr"]

        assert_equal("http://flavorjon.es/", attr.namespace.href)
      end

      def test_setting_attribute_namespace
        doc = Nokogiri::XML(<<~EOXML)
          <root xmlns='http://google.com/' xmlns:f='http://flavorjon.es/'>
            <div f:myattr='foo'></div>
          </root>
        EOXML
        node = doc.at_css("div")
        attr = node.attributes["myattr"]
        attr.add_namespace("fizzle", "http://fizzle.com/")

        assert_equal("http://fizzle.com/", attr.namespace.href)
      end
    end
  end
end