Skip to content

Commit f1a1adb

Browse files
Brandon McGinty-Carrollbcardiff
Brandon McGinty-Carroll
authored andcommittedMar 13, 2017
allow setting and deleting of attributes on supported XML::Node types (#3902)
* allow setting and deleting of attributes on supported XML::Node types
1 parent 8a437e6 commit f1a1adb

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed
 

‎src/xml/node.cr

+13-2
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,30 @@ struct XML::Node
2121
end
2222

2323
# Gets the attribute content for the *attribute* given by name.
24-
#
2524
# Raises `KeyError` if attribute is not found.
2625
def [](attribute : String) : String
2726
attributes[attribute].content || raise(KeyError.new("Missing attribute: #{attribute}"))
2827
end
2928

3029
# Gets the attribute content for the *attribute* given by name.
31-
#
3230
# Returns `nil` if attribute is not found.
3331
def []?(attribute : String) : String?
3432
attributes[attribute]?.try &.content
3533
end
3634

35+
# Sets *attribute* of this node to *value*.
36+
# Raises `XML::Error` if this node does not support attributes.
37+
def []=(name : String, value : String)
38+
raise XML::Error.new("Can't set attribute of #{type}", 0) unless element?
39+
attributes[name] = value
40+
end
41+
42+
# Deletes attribute given by *name*.
43+
# Returns attributes value, or `nil` if attribute not found.
44+
def delete(name : String)
45+
attributes.delete(name)
46+
end
47+
3748
# Compares with *other*.
3849
def ==(other : Node)
3950
@node == other.@node

0 commit comments

Comments
 (0)
Please sign in to comment.