This commit is contained in:
Nabil Chatbi 2017-03-28 22:47:11 +02:00
parent f668a35503
commit 02c4381a1f
7 changed files with 62 additions and 17 deletions

View File

@ -19,13 +19,13 @@ import Foundation
* name.
* </p>
*
* @author Jonathan Hedley, jonathan@hedley.net
*
*/
open class Attributes: NSCopying {
open static var dataPrefix: String = "data-"
fileprivate var attributes: OrderedDictionary<String, Attribute> = OrderedDictionary<String, Attribute>()
private var attributes: OrderedDictionary<String, Attribute> = OrderedDictionary<String, Attribute>()
// linked hash map to preserve insertion order.
// null be default as so many elements have no attributes -- saves a good chunk of memory
@ -157,13 +157,13 @@ open class Attributes: NSCopying {
attributes.putAll(all: incoming.attributes)
}
open func iterator() -> IndexingIterator<Array<Attribute>> {
if (attributes.isEmpty) {
let args: [Attribute] = []
return args.makeIterator()
}
return attributes.orderedValues.makeIterator()
}
// open func iterator() -> IndexingIterator<Array<Attribute>> {
// if (attributes.isEmpty) {
// let args: [Attribute] = []
// return args.makeIterator()
// }
// return attributes.orderedValues.makeIterator()
// }
/**
Get the attributes as a List, for iteration. Do not modify the keys of the attributes via this view, as changes

View File

@ -11,7 +11,6 @@ import Foundation
/**
* Collects a list of elements that match the supplied criteria.
*
* @author Jonathan Hedley
*/
open class Collector {

View File

@ -141,7 +141,7 @@ public class Evaluator {
open override func matches(_ root: Element, _ element: Element)throws->Bool {
if let values = element.getAttributes() {
for attribute in values.iterator() {
for attribute in values {
if (attribute.getKey().lowercased().hasPrefix(keyPrefix)) {
return true
}

View File

@ -361,7 +361,7 @@ enum HtmlTreeBuilderState: String, HtmlTreeBuilderStateProtocol {
tb.error(self)
// merge attributes onto real html
let html: Element = tb.getStack()[0]
for attribute in startTag.getAttributes().iterator() {
for attribute in startTag.getAttributes() {
if (!html.hasAttr(attribute.getKey())) {
html.getAttributes()?.put(attribute: attribute)
}
@ -377,7 +377,7 @@ enum HtmlTreeBuilderState: String, HtmlTreeBuilderStateProtocol {
} else {
tb.framesetOk(false)
let body: Element = stack[1]
for attribute: Attribute in startTag.getAttributes().iterator() {
for attribute: Attribute in startTag.getAttributes() {
if (!body.hasAttr(attribute.getKey())) {
body.getAttributes()?.put(attribute: attribute)
}
@ -532,7 +532,7 @@ enum HtmlTreeBuilderState: String, HtmlTreeBuilderStateProtocol {
// input
let inputAttribs: Attributes = Attributes()
for attr: Attribute in startTag._attributes.iterator() {
for attr: Attribute in startTag._attributes {
if (!Constants.InBodyStartInputAttribs.contains(attr.getKey())) {
inputAttribs.put(attribute: attr)
}

View File

@ -390,3 +390,45 @@ extension OrderedDictionary: Equatable {
//public func == <Key: Equatable, Value: Equatable>(lhs: OrderedDictionary<Key, Value>, rhs: OrderedDictionary<Key, Value>) -> Bool {
// return lhs._orderedKeys == rhs._orderedKeys && lhs._keysToValues == rhs._keysToValues
//}
/**
* Elements IteratorProtocol.
*/
public struct OrderedDictionaryIterator<Key: Hashable, Value: Equatable>: IteratorProtocol {
/// Elements reference
let orderedDictionary: OrderedDictionary<Key, Value>
//current element index
var index = 0
/// Initializer
init(_ od: OrderedDictionary<Key, Value>) {
self.orderedDictionary = od
}
/// Advances to the next element and returns it, or `nil` if no next element
mutating public func next() -> Value? {
let result = index < orderedDictionary.orderedKeys.count ? orderedDictionary[orderedDictionary.orderedKeys[index]] : nil
index += 1;
return result
}
}
/**
* Elements Extension Sequence.
*/
extension OrderedDictionary: Sequence {
/// Returns an iterator over the elements of this sequence.
func generate()->OrderedDictionaryIterator<Key, Value>
{
return OrderedDictionaryIterator(self)
}
}

View File

@ -49,7 +49,7 @@ open class ParseSettings {
open func normalizeAttributes(_ attributes: Attributes)throws ->Attributes {
if (!preserveAttributeCase) {
for attr in attributes.iterator() {
for attr in attributes {
try attr.setKey(key: attr.getKey().lowercased())
}
}
@ -57,3 +57,8 @@ open class ParseSettings {
}
}

View File

@ -10,8 +10,7 @@ import Foundation
/**
An XML Declaration.
@author Jonathan Hedley, jonathan@hedley.net */
*/
public class XmlDeclaration: Node {
private let _name: String
private let isProcessingInstruction: Bool // <! if true, <? if false, declaration (and last data char should be ?)