You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Kernel.Array() method fails to wrap text node #679
Moving this implementation of to_ary up to Node seems wrong. Not all nodes are text nodes, and not all nodes should be treated as such when converting to an array.
The other thing we could do is make to_ary return [self], which would result in Array() returning an array with the text node inside. Either way, meh.
In case it isn't obvious, the only reason I'm using Array() in the first place is because I don't know if the passed object is a single node or an array of nodes.
@mislav what I am saying is that either it is not a bug (which I believe) or that to_ary method should be moved to Nodesince a Text node is not that different from a simple Node.
That I have no opinion about. It's for you to discuss. The only thing I care about is that I don't end up with empty arrays when I use Array() with Nokogiri objects, since that breaks the principle of least surprise for me.
@tenderlove and it is the expected behaviour since to_a comes from Enumerable and each goes through attributes. Then to_ary would go in Node, in which Enumerable is being included.
Our implementation of to_a returns a list of all the attributes for a particular node. to_a doesn't really make sense in the context of a text node since a text node can never have attributes. I think in this case we either need to remove to_a on the text node, or return [self] from to_ary so that Array() works.
@tenderlove then I would expect any node (since any node can come from a search) to respond to to_ary as [self] not just Text. Otherwise changing the query in @mislav's code could break it.
My understanding is that, because XML::Node implements #each, and also mixes in Enumerable, then we get an implementation of #to_a. Ya?
I think we should reconsider a Node being Enumerable in Nokogiri 2.0. My strawman proposal is to remove it in favor of a better-defined Attributes class which would be Enumerable (and very Hash-y).
This would help us address some other lingering problems around, e.g., how SAX parsers handle node attributes; and the odd API we have around Node#attributes and XML::Attr.
My understanding is that, because XML::Node implements #each, and also
mixes in Enumerable, then we get an implementation of #to_a. Ya?
I think we should reconsider a Node being Enumerable in Nokogiri 2.0. My
strawman proposal is to remove it in favor of a better-defined Attributes
class which would be Enumerable (and very Hash-y).
This would help us address some other lingering problems around, e.g., how
SAX parsers handle node attributes; the odd API we have around Node#attributes and XML::Attr.
I think we should reconsider a Node being Enumerable in Nokogiri 2.0. My strawman proposal is to remove it in favor of a better-defined Attributes class which would be Enumerable (and very Hash-y).
👍
I'd like to keep the hashy syntax for nodes, but removing enumerable seems fine.
Activity
Serabe commentedon May 15, 2012
@tenderlove maybe, moving the to_ary up to Nokogiri:: XML::Node would be best since now the behaviour is inconsistent between Nokogiri classes.
Though, I would rather suggest @mislav using Array[] since it doesn't rely on objects having a method and reverting this commit.
tenderlove commentedon May 15, 2012
Moving this implementation of
to_aryup toNodeseems wrong. Not all nodes are text nodes, and not all nodes should be treated as such when converting to an array.The other thing we could do is make
to_aryreturn[self], which would result inArray()returning an array with the text node inside. Either way, meh./cc @flavorjones @ender672
tenderlove commentedon May 15, 2012
Oops, forgot to cc @yokolet
Serabe commentedon May 15, 2012
I think the problem is using a core method in a way is not intended to.
to_aryandto_adoes not make sense in a simple object just to use parenthesis instead of brackets.mislav commentedon May 15, 2012
In case it isn't obvious, the only reason I'm using
Array()in the first place is because I don't know if the passed object is a single node or an array of nodes.Serabe commentedon May 15, 2012
@mislav
atalways returnsnilor aNode.mislav commentedon May 15, 2012
@Serabe
atis only for the purpose of demonstration of the bug. My user code is different. At several points, I want to avoid the check:Serabe commentedon May 15, 2012
@mislav what I am saying is that either it is not a bug (which I believe) or that
to_arymethod should be moved toNodesince aTextnode is not that different from a simpleNode.tenderlove commentedon May 15, 2012
@Serabe The current behavior is obviously a bug. Why would you expect that
Array(node)would return an empty list?I'm reverting my patch for now, until we get more input. :-)
mislav commentedon May 15, 2012
That I have no opinion about. It's for you to discuss. The only thing I care about is that I don't end up with empty arrays when I use
Array()with Nokogiri objects, since that breaks the principle of least surprise for me.Serabe commentedon May 15, 2012
@tenderlove because if you expect to
Array(any_node)to return[any_node]you should addto_atoNode, not toText.1 remaining item
Serabe commentedon May 15, 2012
@tenderlove then docs are wrong in Kernel#Array
tenderlove commentedon May 15, 2012
@Serabe how so?
Serabe commentedon May 15, 2012
@tenderlove afaik,
Kernel#Arrayshould callto_aif object does not respondto_aryor returnsnil. Let me check rubyspec.Serabe commentedon May 15, 2012
@tenderlove the problem is that
to_ainNodereturns[]. So it is a bug (my apologies about this).tenderlove commentedon May 15, 2012
Serabe commentedon May 15, 2012
@tenderlove and it is the expected behaviour since
to_acomes fromEnumerableandeachgoes through attributes. Thento_arywould go inNode, in whichEnumerableis being included.tenderlove commentedon May 15, 2012
@Serabe no problem! 😄
Our implementation of
to_areturns a list of all the attributes for a particular node.to_adoesn't really make sense in the context of a text node since a text node can never have attributes. I think in this case we either need to removeto_aon the text node, or return[self]fromto_aryso thatArray()works.Serabe commentedon May 15, 2012
@tenderlove then I would expect any node (since any node can come from a search) to respond to
to_aryas[self]not justText. Otherwise changing the query in @mislav's code could break it.flavorjones commentedon May 24, 2012
@tenderlove, @Serabe - sorry for jumping in so late.
My understanding is that, because
XML::Nodeimplements#each, and also mixes inEnumerable, then we get an implementation of#to_a. Ya?I think we should reconsider a Node being Enumerable in Nokogiri 2.0. My strawman proposal is to remove it in favor of a better-defined Attributes class which would be Enumerable (and very Hash-y).
This would help us address some other lingering problems around, e.g., how SAX parsers handle node attributes; and the odd API we have around
Node#attributesandXML::Attr.Thoughts?
/cc @ender672 @yokolet
Serabe commentedon May 24, 2012
+1 I really like your idea.
El 24/05/2012 19:47, "Mike Dalessio" <
reply@reply.github.com>
escribió:
tenderlove commentedon May 24, 2012
👍
I'd like to keep the hashy syntax for nodes, but removing enumerable seems fine.
flavorjones commentedon May 29, 2012
Slated for 2.0
flavorjones commentedon Feb 17, 2016
Closing, captured in ROADMAP.md for now.