| Class | HTML5::TreeBuilders::Base::TreeBuilder |
| In: |
lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb
|
| Parent: | Object |
Base treebuilder implementation
| activeFormattingElements | [RW] | |
| document | [RW] | |
| formPointer | [RW] | |
| head_pointer | [RW] | |
| open_elements | [RW] |
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 104
104: def initialize
105: reset
106: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 184
184: def clearActiveFormattingElements
185: {} until @activeFormattingElements.empty? || @activeFormattingElements.pop == Marker
186: end
Create an element but don‘t insert it anywhere
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 214
214: def createElement(name, attributes)
215: element = @elementClass.new(name)
216: element.attributes = attributes
217: return element
218: end
Check if an element exists between the end of the active formatting elements and the last marker. If it does, return it, else return false
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 191
191: def elementInActiveFormattingElements(name)
192: @activeFormattingElements.reverse.each do |element|
193: # Check for Marker first because if it's a Marker it doesn't have a
194: # name attribute.
195: break if element == Marker
196: return element if element.name == name
197: end
198: return false
199: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 121
121: def elementInScope(target, tableVariant=false)
122: # Exit early when possible.
123: return true if @open_elements[-1].name == target
124:
125: # AT How about while true and simply set node to [-1] and set it to
126: # [-2] at the end...
127: @open_elements.reverse.each do |element|
128: if element.name == target
129: return true
130: elsif element.name == 'table'
131: return false
132: elsif not tableVariant and SCOPING_ELEMENTS.include?(element.name)
133: return false
134: elsif element.name == 'html'
135: return false
136: end
137: end
138: assert false # We should never reach this point
139: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 302
302: def generateImpliedEndTags(exclude=nil)
303: name = @open_elements[-1].name
304:
305: # XXX td, th and tr are not actually needed
306: if (%w[dd dt li p td th tr].include?(name) and name != exclude)
307: @open_elements.pop
308: # XXX This is not entirely what the specification says. We should
309: # investigate it more closely.
310: generateImpliedEndTags(exclude)
311: end
312: end
Get the foster parent element, and sibling to insert before (or nil) when inserting a misnested table node
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 274
274: def getTableMisnestedNodePosition
275: #The foster parent element is the one which comes before the most
276: #recently opened table element
277: #XXX - this is really inelegant
278: lastTable = nil
279: fosterParent = nil
280: insertBefore = nil
281: @open_elements.reverse.each do |element|
282: if element.name == "table"
283: lastTable = element
284: break
285: end
286: end
287: if lastTable
288: #XXX - we should really check that this parent is actually a
289: #node here
290: if lastTable.parent
291: fosterParent = lastTable.parent
292: insertBefore = lastTable
293: else
294: fosterParent = @open_elements[@open_elements.index(lastTable) - 1]
295: end
296: else
297: fosterParent = @open_elements[0]
298: end
299: return fosterParent, insertBefore
300: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 314
314: def get_document
315: @document
316: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 318
318: def get_fragment
319: #assert @inner_html
320: fragment = @fragmentClass.new
321: @open_elements[0].reparentChildren(fragment)
322: return fragment
323: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 201
201: def insertDoctype(name, public_id, system_id)
202: doctype = @doctypeClass.new(name)
203: doctype.public_id = public_id
204: doctype.system_id = system_id
205: @document.appendChild(doctype)
206: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 259
259: def insertText(data, parent=nil)
260: parent = @open_elements[-1] if parent.nil?
261:
262: if (not(@insert_from_table) or (@insert_from_table and not TABLE_INSERT_MODE_ELEMENTS.include?(@open_elements[-1].name)))
263: parent.insertText(data)
264: else
265: #We should be in the InTable mode. This means we want to do
266: #special magic element rearranging
267: parent, insertBefore = getTableMisnestedNodePosition
268: parent.insertText(data, insertBefore)
269: end
270: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 208
208: def insert_comment(data, parent=nil)
209: parent = @open_elements[-1] if parent.nil?
210: parent.appendChild(@commentClass.new(data))
211: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 227
227: def insert_element(name, attributes)
228: send(@insert_element, name, attributes)
229: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 231
231: def insert_elementNormal(name, attributes)
232: element = @elementClass.new(name)
233: element.attributes = attributes
234: @open_elements.last.appendChild(element)
235: @open_elements.push(element)
236: return element
237: end
Create an element and insert it into the tree
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 240
240: def insert_elementTable(name, attributes)
241: element = @elementClass.new(name)
242: element.attributes = attributes
243: if TABLE_INSERT_MODE_ELEMENTS.include?(@open_elements.last.name)
244: #We should be in the InTable mode. This means we want to do
245: #special magic element rearranging
246: parent, insertBefore = getTableMisnestedNodePosition
247: if insertBefore.nil?
248: parent.appendChild(element)
249: else
250: parent.insertBefore(element, insertBefore)
251: end
252: @open_elements.push(element)
253: else
254: return insert_elementNormal(name, attributes)
255: end
256: return element
257: end
Switch the function used to insert an element from the normal one to the misnested table one and back again
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 222
222: def insert_from_table=(value)
223: @insert_from_table = value
224: @insert_element = value ? :insert_elementTable : :insert_elementNormal
225: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 141
141: def reconstructActiveFormattingElements
142: # Within this algorithm the order of steps described in the
143: # specification is not quite the same as the order of steps in the
144: # code. It should still do the same though.
145:
146: # Step 1: stop the algorithm when there's nothing to do.
147: return if @activeFormattingElements.empty?
148:
149: # Step 2 and step 3: we start with the last element. So i is -1.
150: i = -1
151: entry = @activeFormattingElements[i]
152: return if entry == Marker or @open_elements.include?(entry)
153:
154: # Step 6
155: until entry == Marker or @open_elements.include?(entry)
156: # Step 5: let entry be one earlier in the list.
157: i -= 1
158: begin
159: entry = @activeFormattingElements[i]
160: rescue
161: # Step 4: at this point we need to jump to step 8. By not doing
162: # i += 1 which is also done in step 7 we achieve that.
163: break
164: end
165: end
166: while true
167: # Step 7
168: i += 1
169:
170: # Step 8
171: clone = @activeFormattingElements[i].cloneNode
172:
173: # Step 9
174: element = insert_element(clone.name, clone.attributes)
175:
176: # Step 10
177: @activeFormattingElements[i] = element
178:
179: # Step 11
180: break if element == @activeFormattingElements[-1]
181: end
182: end
# File lib/feed_tools/vendor/html5/lib/html5/treebuilders/base.rb, line 108
108: def reset
109: @open_elements = []
110: @activeFormattingElements = []
111:
112: #XXX - rename these to headElement, formElement
113: @head_pointer = nil
114: @formPointer = nil
115:
116: self.insert_from_table = false
117:
118: @document = @documentClass.new
119: end