diff --git a/spec/std/xml/xml_spec.cr b/spec/std/xml/xml_spec.cr index fb74b5e4288d..5bc83d9051a1 100644 --- a/spec/std/xml/xml_spec.cr +++ b/spec/std/xml/xml_spec.cr @@ -359,4 +359,17 @@ describe XML do root["bar"] = 1 root["bar"].should eq("1") end + + it "deletes an attribute" do + doc = XML.parse(%{}) + root = doc.root.not_nil! + + res = root.delete("bar") + root["bar"]?.should be_nil + root.to_s.should eq(%{}) + res.should eq "baz" + + res = root.delete("biz") + res.should be_nil + end end diff --git a/src/xml/attributes.cr b/src/xml/attributes.cr index 2c1511c48222..aae965a3b2ca 100644 --- a/src/xml/attributes.cr +++ b/src/xml/attributes.cr @@ -42,6 +42,12 @@ struct XML::Attributes value end + def delete(name : String) + value = self[name]?.try &.content + res = LibXML.xmlUnsetProp(@node, name) + value if res == 0 + end + def each : Nil return unless @node.element? diff --git a/src/xml/libxml2.cr b/src/xml/libxml2.cr index a24841b19b10..8f1d70522537 100644 --- a/src/xml/libxml2.cr +++ b/src/xml/libxml2.cr @@ -308,6 +308,8 @@ lib LibXML fun xmlSetProp(node : Node*, name : UInt8*, value : UInt8*) : Attr* + fun xmlUnsetProp(node : Node*, name : UInt8*) : Int + fun xmlValidateNameValue(value : UInt8*) : Int end