-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow setting and deleting of attributes on supported XML::Node types #3902
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra newline in between would do good. See previous comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Sija there is no consistency regarding if the docs for the returned / raised should be in a new paragraph. Applying your proposed changes will left xml/node docs inconsistent. Let's merge this as it is and unify formatting later. I agree a new line looks better in the html rendering but in an .cr file I would probably forgot to include it. |
||
def delete(name : String) | ||
attributes.delete(name) | ||
end | ||
|
||
# Compares with *other*. | ||
def ==(other : Node) | ||
@node == other.@node | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the reason for removal?