2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2003-06-10 05:31:01 +04:00
|
|
|
require "rexml/node"
|
|
|
|
|
|
|
|
module REXML
|
2008-10-01 17:46:53 +04:00
|
|
|
##
|
|
|
|
# A Child object is something contained by a parent, and this class
|
|
|
|
# contains methods to support that. Most user code will not use this
|
|
|
|
# class directly.
|
|
|
|
class Child
|
|
|
|
include Node
|
2011-05-19 04:07:25 +04:00
|
|
|
attr_reader :parent # The Parent of this object
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
# Constructor. Any inheritors of this class should call super to make
|
|
|
|
# sure this method is called.
|
|
|
|
# parent::
|
|
|
|
# if supplied, the parent of this child will be set to the
|
|
|
|
# supplied value, and self will be added to the parent
|
|
|
|
def initialize( parent = nil )
|
2009-03-06 06:56:38 +03:00
|
|
|
@parent = nil
|
|
|
|
# Declare @parent, but don't define it. The next line sets the
|
2008-10-01 17:46:53 +04:00
|
|
|
# parent.
|
|
|
|
parent.add( self ) if parent
|
|
|
|
end
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
# Replaces this object with another object. Basically, calls
|
|
|
|
# Parent.replace_child
|
|
|
|
#
|
|
|
|
# Returns:: self
|
|
|
|
def replace_with( child )
|
|
|
|
@parent.replace_child( self, child )
|
|
|
|
self
|
|
|
|
end
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
# Removes this child from the parent.
|
|
|
|
#
|
|
|
|
# Returns:: self
|
|
|
|
def remove
|
|
|
|
unless @parent.nil?
|
|
|
|
@parent.delete self
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
# Sets the parent of this child to the supplied argument.
|
|
|
|
#
|
|
|
|
# other::
|
|
|
|
# Must be a Parent object. If this object is the same object as the
|
|
|
|
# existing parent of this child, no action is taken. Otherwise, this
|
|
|
|
# child is removed from the current parent (if one exists), and is added
|
|
|
|
# to the new parent.
|
|
|
|
# Returns:: The parent added
|
|
|
|
def parent=( other )
|
|
|
|
return @parent if @parent == other
|
|
|
|
@parent.delete self if defined? @parent and @parent
|
|
|
|
@parent = other
|
|
|
|
end
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
alias :next_sibling :next_sibling_node
|
|
|
|
alias :previous_sibling :previous_sibling_node
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
# Sets the next sibling of this child. This can be used to insert a child
|
|
|
|
# after some other child.
|
|
|
|
# a = Element.new("a")
|
|
|
|
# b = a.add_element("b")
|
|
|
|
# c = Element.new("c")
|
|
|
|
# b.next_sibling = c
|
|
|
|
# # => <a><b/><c/></a>
|
|
|
|
def next_sibling=( other )
|
|
|
|
parent.insert_after self, other
|
|
|
|
end
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2009-03-06 06:56:38 +03:00
|
|
|
# Sets the previous sibling of this child. This can be used to insert a
|
2008-10-01 17:46:53 +04:00
|
|
|
# child before some other child.
|
|
|
|
# a = Element.new("a")
|
|
|
|
# b = a.add_element("b")
|
|
|
|
# c = Element.new("c")
|
|
|
|
# b.previous_sibling = c
|
|
|
|
# # => <a><b/><c/></a>
|
|
|
|
def previous_sibling=(other)
|
|
|
|
parent.insert_before self, other
|
|
|
|
end
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
# Returns:: the document this child belongs to, or nil if this child
|
|
|
|
# belongs to no document
|
|
|
|
def document
|
|
|
|
return parent.document unless parent.nil?
|
|
|
|
nil
|
|
|
|
end
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
# This doesn't yet handle encodings
|
|
|
|
def bytes
|
2010-11-08 23:59:01 +03:00
|
|
|
document.encoding
|
2003-06-10 05:31:01 +04:00
|
|
|
|
2008-10-01 17:46:53 +04:00
|
|
|
to_s
|
|
|
|
end
|
|
|
|
end
|
2003-06-10 05:31:01 +04:00
|
|
|
end
|