Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions spec/std/xml/xml_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,17 @@ describe XML do
root["bar"] = 1
root["bar"].should eq("1")
end

it "deletes an attribute" do
doc = XML.parse(%{<foo bar="baz"></foo>})
root = doc.root.not_nil!

res = root.delete("bar")
root["bar"]?.should be_nil
root.to_s.should eq(%{<foo/>})
res.should eq "baz"

res = root.delete("biz")
res.should be_nil
end
end
6 changes: 6 additions & 0 deletions src/xml/attributes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
2 changes: 2 additions & 0 deletions src/xml/libxml2.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down