Skip to content

Commit

Permalink
allow setting and deleting of attributes on supported XML::Node types (
Browse files Browse the repository at this point in the history
…#3902)

* allow setting and deleting of attributes on supported XML::Node types
  • Loading branch information
Brandon McGinty-Carroll authored and bcardiff committed Mar 13, 2017
1 parent 8a437e6 commit f1a1adb
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/xml/node.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,30 @@ struct XML::Node
end

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

# Gets the attribute content for the *attribute* given by name.
#
# Returns `nil` if attribute is not found.
def []?(attribute : String) : String?
attributes[attribute]?.try &.content
end

# Sets *attribute* of this node to *value*.
# Raises `XML::Error` if this node does not support attributes.
def []=(name : String, value : String)
raise XML::Error.new("Can't set attribute of #{type}", 0) unless element?
attributes[name] = value
end

# Deletes attribute given by *name*.
# Returns attributes value, or `nil` if attribute not found.
def delete(name : String)
attributes.delete(name)
end

# Compares with *other*.
def ==(other : Node)
@node == other.@node
Expand Down

0 comments on commit f1a1adb

Please sign in to comment.