Skip to content

Kernel.Array() method fails to wrap text node #679

Closed

Description

@mislav
require 'nokogiri'
doc = Nokogiri('<a>b</a>')
text_node = doc.at('/a/text()')
Array(text_node)
#=> []

I expected: [<text_node>]

# Nokogiri (1.5.2)
    ---
    warnings: []
    nokogiri: 1.5.2
    ruby:
      version: 1.9.3
      platform: x86_64-darwin11.2.0
      description: ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
      engine: ruby
    libxml:
      binding: extension
      compiled: 2.7.8
      loaded: 2.7.8

Activity

  1. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @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.

  2. tenderlove commented on May 15, 2012

    @tenderlove
    Member

    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.

    /cc @flavorjones @ender672

  3. tenderlove commented on May 15, 2012

    @tenderlove
    Member

    Oops, forgot to cc @yokolet

  4. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    I think the problem is using a core method in a way is not intended to.

    Kernel#Array()
    Returns arg as an Array. First tries to call arg.to_ary, then arg.to_a.

    Array::[]
    Returns a new array populated with the given objects.

    to_ary and to_a does not make sense in a simple object just to use parenthesis instead of brackets.

  5. mislav commented on May 15, 2012

    @mislav
    Author

    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.

    Array(elements).each do |element|
      # ...
    end
  6. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @mislav at always returns nil or a Node.

  7. mislav commented on May 15, 2012

    @mislav
    Author

    @Serabe at is only for the purpose of demonstration of the bug. My user code is different. At several points, I want to avoid the check:

    elements = [elements] unless Array === elements
    elements.each ...
  8. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @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.

  9. tenderlove commented on May 15, 2012

    @tenderlove
    Member

    @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. :-)

  10. mislav commented on May 15, 2012

    @mislav
    Author

    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.

  11. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @tenderlove because if you expect to Array(any_node) to return [any_node] you should add to_a to Node, not to Text.

  12. 1 remaining item

  13. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @tenderlove then docs are wrong in Kernel#Array

  14. tenderlove commented on May 15, 2012

    @tenderlove
    Member

    @Serabe how so?

  15. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @tenderlove afaik, Kernel#Array should call to_a if object does not respond to_ary or returns nil. Let me check rubyspec.

  16. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @tenderlove the problem is that to_ain Node returns []. So it is a bug (my apologies about this).

  17. tenderlove commented on May 15, 2012

    @tenderlove
    Member
    irb(main):001:0> require 'nokogiri'
    => true
    irb(main):002:0> doc = Nokogiri('<a>b</a>')
    => #<Nokogiri::XML::Document:0x3fc0e5c48570 name="document" children=[#<Nokogiri::XML::Element:0x3fc0e5c481ec name="a" children=[#<Nokogiri::XML::Text:0x3fc0e5c35fb0 "b">]>]>
    irb(main):003:0> text_node = doc.at('/a/text()')
    => #<Nokogiri::XML::Text:0x3fc0e5c35fb0 "b">
    irb(main):004:0> text_node.to_a
    => []
    irb(main):005:0>
    
  18. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @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.

  19. tenderlove commented on May 15, 2012

    @tenderlove
    Member

    @Serabe no problem! 😄

    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.

  20. Serabe commented on May 15, 2012

    @Serabe
    Contributor

    @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.

  21. flavorjones commented on May 24, 2012

    @flavorjones
    Member

    @tenderlove, @Serabe - sorry for jumping in so late.

    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.

    Thoughts?

    /cc @ender672 @yokolet

  22. Serabe commented on May 24, 2012

    @Serabe
    Contributor

    +1 I really like your idea.
    El 24/05/2012 19:47, "Mike Dalessio" <
    reply@reply.github.com>
    escribió:

    @tenderlove, @Serabe - sorry for jumping in so late.

    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.

    Thoughts?

    /cc @ender672 @yokolet


    Reply to this email directly or view it on GitHub:
    https://github.com/tenderlove/nokogiri/issues/679#issuecomment-5912872

  23. tenderlove commented on May 24, 2012

    @tenderlove
    Member

    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.

  24. flavorjones commented on May 29, 2012

    @flavorjones
    Member

    Slated for 2.0

  25. flavorjones commented on Feb 17, 2016

    @flavorjones
    Member

    Closing, captured in ROADMAP.md for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions