Rev | Line | |
---|
[18425] | 1 | require "rexml/parseexception"
|
---|
| 2 |
|
---|
| 3 | module REXML
|
---|
| 4 | # Represents a node in the tree. Nodes are never encountered except as
|
---|
| 5 | # superclasses of other objects. Nodes have siblings.
|
---|
| 6 | module Node
|
---|
| 7 | # @return the next sibling (nil if unset)
|
---|
| 8 | def next_sibling_node
|
---|
| 9 | return nil if @parent.nil?
|
---|
| 10 | @parent[ @parent.index(self) + 1 ]
|
---|
| 11 | end
|
---|
| 12 |
|
---|
| 13 | # @return the previous sibling (nil if unset)
|
---|
| 14 | def previous_sibling_node
|
---|
| 15 | return nil if @parent.nil?
|
---|
| 16 | ind = @parent.index(self)
|
---|
| 17 | return nil if ind == 0
|
---|
| 18 | @parent[ ind - 1 ]
|
---|
| 19 | end
|
---|
| 20 |
|
---|
| 21 | def to_s indent=-1
|
---|
| 22 | rv = ""
|
---|
| 23 | write rv,indent
|
---|
| 24 | rv
|
---|
| 25 | end
|
---|
| 26 |
|
---|
| 27 | def indent to, ind
|
---|
| 28 | if @parent and @parent.context and not @parent.context[:indentstyle].nil? then
|
---|
| 29 | indentstyle = @parent.context[:indentstyle]
|
---|
| 30 | else
|
---|
| 31 | indentstyle = ' '
|
---|
| 32 | end
|
---|
| 33 | to << indentstyle*ind unless ind<1
|
---|
| 34 | end
|
---|
| 35 |
|
---|
| 36 | def parent?
|
---|
| 37 | false;
|
---|
| 38 | end
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | # Visit all subnodes of +self+ recursively
|
---|
| 42 | def each_recursive(&block) # :yields: node
|
---|
| 43 | self.elements.each {|node|
|
---|
| 44 | block.call(node)
|
---|
| 45 | node.each_recursive(&block)
|
---|
| 46 | }
|
---|
| 47 | end
|
---|
| 48 |
|
---|
| 49 | # Find (and return) first subnode (recursively) for which the block
|
---|
| 50 | # evaluates to true. Returns +nil+ if none was found.
|
---|
| 51 | def find_first_recursive(&block) # :yields: node
|
---|
| 52 | each_recursive {|node|
|
---|
| 53 | return node if block.call(node)
|
---|
| 54 | }
|
---|
| 55 | return nil
|
---|
| 56 | end
|
---|
| 57 |
|
---|
| 58 | # Returns the position that +self+ holds in its parent's array, indexed
|
---|
| 59 | # from 1.
|
---|
| 60 | def index_in_parent
|
---|
| 61 | parent.index(self)+1
|
---|
| 62 | end
|
---|
| 63 | end
|
---|
| 64 | end
|
---|
Note:
See
TracBrowser
for help on using the repository browser.