-
-
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
allow setting and deleting of attributes on supported XML::Node types #3902
Conversation
Thank you for this! I initially wrote the libxml2 bindings and I mainly focused on parsing XML, not so much in modifying it afterwards. So that the code is not using functions that depend on Some comments:
|
src/xml/attributes.cr
Outdated
def []=(name : String, value : String) | ||
curr = self[name]? | ||
if curr | ||
curr.not_nil!.content = value |
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.
This not_nil!
is not needed because you already checked that it's not nil with if curr
above
No more raising and Node[attr]=nil now deletes. |
Hi @bmmcginty , Sorry for what I'm going to say, but we had a discussion about this with @bcardiff and @spalladino and we finally decided that:
In Nokogiri you can assign any type as the value, for example In short... could you revert your commits to your original proposal? 😊 Thank you! |
No problem. That completely makes sense. |
a12491f
to
c8a96b7
Compare
Fixed. []=(string,nil) removed. node.delete calls attributes.delete. attributes.delete deletes attribute if exists, and returns the nodes content or nil if non-existing attribute. |
c8a96b7
to
54faf04
Compare
src/xml/node.cr
Outdated
@@ -522,4 +534,4 @@ struct XML::Node | |||
ptr = @node.value._private | |||
ptr ? (ptr.as(Array(XML::Error))) : nil | |||
end | |||
end | |||
end |
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.
No newline at the end of file.
@Sija added blank line. travis builds seem to be timing out, though I don't have a Mac to test on. |
@bmmcginty the linux builds are failing due to a formatter error. I think the mac builds died due to a travis incident. |
@bmmcginty I'd suggest running |
acdae9c
to
85bef20
Compare
@Sija Format has been run and code rebased. Error appears to be just that odd mac one on Travis. Thanks for your patience. |
@bmmcginty CI on OSX has been fixed on master, if you rebase your PR onto master it should be fixed. |
85bef20
to
168621a
Compare
@RX14 rebased, pushed, and passed. thanks. |
src/xml/node.cr
Outdated
# 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. |
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.
sets
-> Sets
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.
Double newline before 2nd sentence would do good.
@@ -21,19 +21,29 @@ struct XML::Node | |||
end | |||
|
|||
# Gets the attribute content for the *attribute* given by name. | |||
# |
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?
# 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. | ||
# |
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.
ditto
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 comment
The 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 comment
The 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.
7e90aec
to
e5a9945
Compare
Thanks @bmmcginty |
Is it just me or has there never been a
|
@joenas looks like a bug, can you report it? |
There's unlink |
@RX14 absolutely, issue here or elsewhere? |
@joenas either open a new issue or, if you have time, a PR fixing it with specs. Thanks! |
I’ll see what I can do :) spending most of my free time porting |
If you haven't done this yet, let me know and I'll do it this afternoon. (I might be responsible for this myself.)
…On Wed, Oct 03, 2018 at 05:34:37PM +0000, Jon Neverland wrote:
I�ll see what I can do :) spending most of my free time porting readability
to crystal atm
—
You are receiving this because you were mentioned.
Reply to this email directly,
[view it on GitHub](http://go.bmcginty.us/274708)
, or
[mute the thread](http://go.bmcginty.us/274709)
.![]
|
@bmmcginty Sorry I just saw this, I'm i CET so... anyways I haven't so feel free! |
@bmmcginty Actually I need this to get my |
Please do. Thanks.
|
Done: #6910 |
Allow node[name]=value and node.attributes.delete(name)
I'm not sure if Attributes#delete is the correct place for the delete method, but I'm open to suggestions. Maybe Node#delete_attribute?
Also, are we avoiding all methods in libxml2 that require the LIBXML_TREE_ENABLED define? (I've continued that pattern in this PR.)
That should probably be noted somewhere if so.