5 # A Child object is something contained by a parent, and this class
6 # contains methods to support that. Most user code will not use this
10 attr_reader :parent # The Parent of this object
12 # Constructor. Any inheritors of this class should call super to make
13 # sure this method is called.
15 # if supplied, the parent of this child will be set to the
16 # supplied value, and self will be added to the parent
17 def initialize( parent = nil )
19 # Declare @parent, but don't define it. The next line sets the
21 parent.add( self ) if parent
24 # Replaces this object with another object. Basically, calls
25 # Parent.replace_child
28 def replace_with( child )
29 @parent.replace_child( self, child )
33 # Removes this child from the parent.
43 # Sets the parent of this child to the supplied argument.
46 # Must be a Parent object. If this object is the same object as the
47 # existing parent of this child, no action is taken. Otherwise, this
48 # child is removed from the current parent (if one exists), and is added
50 # Returns:: The parent added
52 return @parent if @parent == other
53 @parent.delete self if defined? @parent and @parent
57 alias :next_sibling :next_sibling_node
58 alias :previous_sibling :previous_sibling_node
60 # Sets the next sibling of this child. This can be used to insert a child
61 # after some other child.
62 # a = Element.new("a")
63 # b = a.add_element("b")
64 # c = Element.new("c")
66 # # => <a><b/><c/></a>
67 def next_sibling=( other )
68 parent.insert_after self, other
71 # Sets the previous sibling of this child. This can be used to insert a
72 # child before some other child.
73 # a = Element.new("a")
74 # b = a.add_element("b")
75 # c = Element.new("c")
76 # b.previous_sibling = c
77 # # => <a><b/><c/></a>
78 def previous_sibling=(other)
79 parent.insert_before self, other
82 # Returns:: the document this child belongs to, or nil if this child
83 # belongs to no document
85 return parent.document unless parent.nil?
89 # This doesn't yet handle encodings
91 encoding = document.encoding