Skip to content
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

[DOC] Doc for XML::Node#<< #3427

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module XML
# - Nokogiri::XML::Searchable
# - Ruby core's {Enumerable}[https://docs.ruby-lang.org/en/master/Enumerable.html]
#
# == Subclasses
#
# Each of the following classes is a subclass of XML::Node,
# and so inherits all the methods and constants mentioned above:
#
Expand Down Expand Up @@ -301,15 +303,36 @@ def wrap(node_or_tags)
self
end

###
# Add +node_or_tags+ as a child of this Node.
# :call-seq:
# self << object -> self
#
# +node_or_tags+ can be a Nokogiri::XML::Node, a ::DocumentFragment, a ::NodeSet, or a String
# containing markup.
# Adds a child to +self+ as the last child;
# returns +self+.
#
# Returns +self+, to support chaining of calls (e.g., root << child1 << child2)
# If +object+ is a string, parses it into a new \XML::Node
# and adds that node as the last child:
#
# Also see related method +add_child+.
# node_foo = XML::Node.new('foo', doc)
# node_foo << '<bat />'
# # => #(Element:0x10bf30 { name = "foo", children = [ #(Element:0x10e294 { name = "bat" })] })
#
# If +object+ is an XML::NodeSet
# or any kind of \XML::Node (see {Subclasses}[rdoc-ref:XML::Node@Subclasses]),
# adds it as the last child:
#
# node_foo = XML::Node.new('foo', doc)
# node_bar = XML::Node.new('bar', doc)
# node_baz = XML::Node.new('baz', doc)
# node_foo << node_bar << node_baz # Note the chained method calls.
# # =>
# #(Element:0xe2c70 {
# name = "foo",
# children = [ #(Element:0xe833c { name = "bar" }), #(Element:0xeda08 { name = "baz" })]
# })
#
# Raises +ArgumentError+ if +object+ is not a string, an XML::Node, or an XML::NodeSet.
#
# Related: #add_child.
def <<(node_or_tags)
add_child(node_or_tags)
self
Expand Down
Loading