Updates and fixes

This commit is contained in:
2025-10-25 00:04:37 -04:00
parent 743fc51873
commit 58a8419984
8 changed files with 174 additions and 13 deletions

View File

@@ -147,6 +147,43 @@ public class HTMLNode : XMLNode, IGlobalContainer {
return nil
}
public func iterate(_ index:Int, _ skipTextNodes:Bool, _ cb:(HTMLNode, Int)->()) -> Int {
if (skipTextNodes && self is HTMLText) {
return index
}
cb(self, index)
var newIndex = index + 1
for eachChild in children {
newIndex = eachChild.iterate(newIndex, skipTextNodes, cb)
}
return newIndex
}
public func flatten() -> [HTMLNode] {
let count = countElements()
let result:[HTMLNode] = Array(unsafeUninitializedCapacity: count+1) { buffer, initializedCount in
initializedCount = self.addTo(array: &buffer, index: 0)
}
return result
}
public func countElements() -> Int {
var result = 1
for eachChild in children {
result += eachChild.countElements()
}
return result
}
private func addTo(array:inout UnsafeMutableBufferPointer<HTMLNode>, index:Int) -> Int {
array.initializeElement(at: index, to: self)
var newIndex = index + 1
for eachChild in children {
newIndex = eachChild.addTo(array: &array, index: newIndex)
}
return newIndex
}
public override func renderAttributes() -> String {
var result = super.renderAttributes()
var first = result.count == 0