Class: HTML::Node
- Defined in:
- lib/action_controller/vendor/html-scanner/html/node.rb
Overview
The base class of all nodes, textual and otherwise, in an HTML document.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
The array of children of this node.
-
#line ⇒ Object
readonly
The line number of the input where this node was begun.
-
#parent ⇒ Object
readonly
The parent node of this node.
-
#position ⇒ Object
readonly
The byte position in the input where this node was begun.
Class Method Summary collapse
Instance Method Summary collapse
-
#find(conditions) ⇒ Object
Search the children of this node for the first node for which #find returns non
nil. -
#find_all(conditions) ⇒ Object
Search for all nodes that match the given conditions, and return them as an array.
-
#initialize(parent, line = 0, pos = 0) ⇒ Node
constructor
Create a new node as a child of the given parent.
-
#match(conditions) ⇒ Object
Return false (subclasses must override this to provide specific matching behavior.)
conditionsmay be of any type. -
#tag? ⇒ Boolean
Returns
false. -
#to_s ⇒ Object
Return a textual representation of the node.
- #validate_conditions(conditions) ⇒ Object
Constructor Details
#initialize(parent, line = 0, pos = 0) ⇒ Node
Create a new node as a child of the given parent.
71 72 73 74 75 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 71 def initialize(parent, line=0, pos=0) @parent = parent @children = [] @line, @position = line, pos end |
Instance Attribute Details
#children ⇒ Object (readonly)
The array of children of this node. Not all nodes have children.
58 59 60 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 58 def children @children end |
#line ⇒ Object (readonly)
The line number of the input where this node was begun
65 66 67 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 65 def line @line end |
#parent ⇒ Object (readonly)
The parent node of this node. All nodes have a parent, except for the root node.
62 63 64 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 62 def parent @parent end |
#position ⇒ Object (readonly)
The byte position in the input where this node was begun
68 69 70 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 68 def position @position end |
Class Method Details
.parse(parent, line, pos, content) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 122 def parse(parent, line, pos, content) if content !~ /^<\S/ Text.new(parent, line, pos, content) else scanner = StringScanner.new(content) scanner.skip(/</) or raise "expected <" closing = ( scanner.scan(/\//) ? :close : nil ) return Text.new(parent, line, pos, content) unless name = scanner.scan(/[\w:]+/) name.downcase! unless closing scanner.skip(/\s*/) attributes = {} while attr = scanner.scan(/[-\w:]+/) value = true if scanner.scan(/\s*=\s*/) if delim = scanner.scan(/['"]/) value = "" while text = scanner.scan(/[^#{delim}\\]+|./) case text when "\\" then value << text value << scanner.getch when delim break else value << text end end else value = scanner.scan(/[^\s>\/]+/) end end attributes[attr.downcase] = value scanner.skip(/\s*/) end closing = ( scanner.scan(/\//) ? :self : nil ) end scanner.scan(/\s*>/) or raise "expected > (got #{scanner.rest.inspect} for #{content}, #{attributes.inspect})" Tag.new(parent, line, pos, name, attributes, closing) end end |
Instance Method Details
#find(conditions) ⇒ Object
Search the children of this node for the first node for which #find returns non nil. Returns the result of the #find call that succeeded.
92 93 94 95 96 97 98 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 92 def find(conditions) @children.each do |child| node = child.find(conditions) return node if node end nil end |
#find_all(conditions) ⇒ Object
Search for all nodes that match the given conditions, and return them as an array.
102 103 104 105 106 107 108 109 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 102 def find_all(conditions) matches = [] matches << self if match(conditions) @children.each do |child| matches.concat child.find_all(conditions) end matches end |
#match(conditions) ⇒ Object
Return false (subclasses must override this to provide specific matching behavior.) conditions may be of any type.
86 87 88 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 86 def match(conditions) false end |
#tag? ⇒ Boolean
Returns false. Subclasses may override this if they define a kind of tag.
113 114 115 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 113 def tag? false end |
#to_s ⇒ Object
Return a textual representation of the node.
78 79 80 81 82 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 78 def to_s s = "" @children.each { |child| s << child.to_s } s end |
#validate_conditions(conditions) ⇒ Object
117 118 119 |
# File 'lib/action_controller/vendor/html-scanner/html/node.rb', line 117 def validate_conditions(conditions) Conditions === conditions ? conditions : Conditions.new(conditions) end |