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
|
# frozen_string_literal: true
require "helper"
module Nokogiri
module XML
class TestAttr < Nokogiri::TestCase
def test_new
100.times do
doc = Nokogiri::XML::Document.new
assert(doc)
assert(Nokogiri::XML::Attr.new(doc, "foo"))
end
end
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_includes(%{ street="Y&ent1;"}, street.to_xml)
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_includes(%{ foo="Y&ent1;"}, foo.to_html)
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_includes(%{ foo=""}, foo.to_html)
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
assert_equal("", foo.value) # this may be surprising to people, see xmlGetPropNodeValueInternal
if Nokogiri.uses_libxml?
assert_includes(%{ foo}, foo.to_html) # libxml2 still emits a boolean attribute at serialize-time
else
assert_includes(%{ foo=""}, foo.to_html) # jruby does not
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)
assert_includes(%{ disabled}, disabled.to_html) # we still emit a boolean attribute at serialize-time!
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
assert_equal("", disabled.value) # this may be surprising to people, see xmlGetPropNodeValueInternal
assert_includes(%{ disabled}, disabled.to_html) # but we emit a boolean attribute at serialize-time
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
|