-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebNode.swift
81 lines (67 loc) · 2.55 KB
/
WebNode.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import SRTCore
import JavaScriptKitMock
public class WebNode: WebEventTarget & CustomStringConvertible {
public override init() {
self._childNodes = WebMutableNodeList()
super.init()
}
private let _childNodes: WebMutableNodeList
private weak var _parentNode: WebNode?
public var childNodes: WebNodeList { _childNodes }
public var firstChild: WebNode? { _childNodes._items.first }
public var nextSibling: WebNode? {
guard let parent = _parentNode else { return nil }
guard let index = parent._childNodes.index(of: self) else { return nil }
return parent._childNodes.item(index + 1)
}
public var parentNode: WebNode? { _parentNode }
public var previousSibling: WebNode? {
guard let parent = _parentNode else { return nil }
guard let index = parent._childNodes.index(of: self) else { return nil }
return parent._childNodes.item(index - 1)
}
public func appendChild(_ node: WebNode) {
_childNodes._items.append(node)
node._parentNode = self
}
public func insertBefore(_ node: WebNode, _ ref: WebNode?) throws {
if let ref {
let index = try _childNodes.index(of: ref).unwrap("ref")
_childNodes._items.insert(node, at: index)
node._parentNode = self
} else {
appendChild(node)
}
}
public func remove() {
parentNode?.removeChild(self)
}
public func removeChild(_ node: WebNode) {
guard let index = _childNodes.index(of: node) else { return }
_childNodes._items.remove(at: index)
node._parentNode = nil
}
public var description: String {
let p = PrettyPrinter()
write(to: p)
return p.output
}
internal func write(to p: PrettyPrinter) {
fatalError("override is unimplemented: type=\(type(of: self))")
}
public override func _get_property(_ name: String) -> JSValue {
switch name {
case "childNodes": childNodes.jsValue
case "firstChild": firstChild.jsValue
case "nextSibling": nextSibling.jsValue
case "parentNode": parentNode.jsValue
case "previousSibling": previousSibling.jsValue
case "description": description.jsValue
case "appendChild": JSFunction(Self.appendChild).jsValue
case "insertBefore": JSFunction(Self.insertBefore).jsValue
case "remove": JSFunction(Self.remove).jsValue
case "removeChild": JSFunction(Self.removeChild).jsValue
default: super._get_property(name)
}
}
}