-
Notifications
You must be signed in to change notification settings - Fork 2
/
libxmlHTMLNode.swift
executable file
·245 lines (214 loc) · 7.37 KB
/
libxmlHTMLNode.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**@file libxmlHTMLNode.swift
Kanna
Copyright (c) 2015 Atsushi Kiwaki (@_tid_)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import Foundation
import libxml2
/**
libxmlHTMLNode
*/
final class libxmlHTMLNode: XMLElement {
var text: String? {
return libxmlGetNodeContent(nodePtr)
}
var toHTML: String? {
let buf = xmlBufferCreate()
htmlNodeDump(buf, docPtr, nodePtr)
let html = String(cString: UnsafePointer<UInt8>((buf?.pointee.content)!))
xmlBufferFree(buf)
return html
}
var toXML: String? {
let buf = xmlBufferCreate()
xmlNodeDump(buf, docPtr, nodePtr, 0, 0)
let html = String(cString: UnsafePointer<UInt8>((buf?.pointee.content)!))
xmlBufferFree(buf)
return html
}
var innerHTML: String? {
guard let html = toHTML else { return nil }
return html
.replacingOccurrences(of: "</[^>]*>$", with: "", options: .regularExpression, range: nil)
.replacingOccurrences(of: "^<[^>]*>", with: "", options: .regularExpression, range: nil)
}
var className: String? {
self["class"]
}
var tagName: String? {
get {
guard let name = nodePtr.pointee.name else {
return nil
}
return String(cString: name)
}
set {
if let newValue = newValue {
xmlNodeSetName(nodePtr, newValue)
}
}
}
var content: String? {
get { text }
set {
if let newValue = newValue {
let v = escape(newValue)
xmlNodeSetContent(nodePtr, v)
}
}
}
var parent: XMLElement? {
get {
libxmlHTMLNode(document: doc, docPtr: docPtr, node: nodePtr.pointee.parent)
}
set {
if let node = newValue as? libxmlHTMLNode {
node.addChild(self)
}
}
}
var nextSibling: XMLElement? {
node(from: xmlNextElementSibling(nodePtr))
}
var previousSibling: XMLElement? {
node(from: xmlPreviousElementSibling(nodePtr))
}
private weak var weakDocument: XMLDocument?
private var document: XMLDocument?
private var docPtr: htmlDocPtr
private var nodePtr: xmlNodePtr
private var doc: XMLDocument? {
weakDocument ?? document
}
public func attributes() -> [AnyHashable : Any] {
var attributes: [AnyHashable: Any] = [:]
var attr = nodePtr.pointee.properties
while attr != nil {
let mem = attr?.pointee
if let tagName = String(validatingUTF8: UnsafeRawPointer((mem?.name)!).assumingMemoryBound(to: CChar.self)) {
if let children = mem?.children, let value = libxmlGetNodeContent(children) {
attributes[tagName] = value
}
}
attr = attr?.pointee.next
}
return attributes
}
subscript(attributeName: String) -> String? {
get {
var attr = nodePtr.pointee.properties
while attr != nil {
let mem = attr?.pointee
if let tagName = String(validatingUTF8: UnsafeRawPointer((mem?.name)!).assumingMemoryBound(to: CChar.self)) {
if attributeName == tagName {
if let children = mem?.children {
return libxmlGetNodeContent(children)
} else {
return ""
}
}
}
attr = attr?.pointee.next
}
return nil
}
set(newValue) {
if let newValue = newValue {
xmlSetProp(nodePtr, attributeName, newValue)
} else {
xmlUnsetProp(nodePtr, attributeName)
}
}
}
init(document: XMLDocument?, docPtr: xmlDocPtr) {
self.weakDocument = document
self.docPtr = docPtr
self.nodePtr = xmlDocGetRootElement(docPtr)
}
init(document: XMLDocument?, docPtr: xmlDocPtr, node: xmlNodePtr) {
self.document = document
self.docPtr = docPtr
self.nodePtr = node
}
// MARK: Searchable
func xpath(_ xpath: String, namespaces: [String: String]? = nil) -> XPathObject {
guard let doc = doc else { return .none }
return XPath(doc: doc, docPtr: docPtr, nodePtr: nodePtr).xpath(xpath, namespaces: namespaces)
}
func css(_ selector: String, namespaces: [String: String]? = nil) -> XPathObject {
guard let doc = doc else { return .none }
return XPath(doc: doc, docPtr: docPtr, nodePtr: nodePtr).css(selector, namespaces: namespaces)
}
func addPrevSibling(_ node: XMLElement) {
guard let node = node as? libxmlHTMLNode else {
return
}
xmlAddPrevSibling(nodePtr, node.nodePtr)
}
func addNextSibling(_ node: XMLElement) {
guard let node = node as? libxmlHTMLNode else {
return
}
xmlAddNextSibling(nodePtr, node.nodePtr)
}
func addChild(_ node: XMLElement) {
guard let node = node as? libxmlHTMLNode else {
return
}
xmlUnlinkNode(node.nodePtr)
xmlAddChild(nodePtr, node.nodePtr)
}
func removeChild(_ node: XMLElement) {
guard let node = node as? libxmlHTMLNode else {
return
}
xmlUnlinkNode(node.nodePtr)
xmlFree(node.nodePtr)
}
private func node(from ptr: xmlNodePtr?) -> XMLElement? {
guard let doc = doc, let nodePtr = ptr else {
return nil
}
return libxmlHTMLNode(document: doc, docPtr: docPtr, node: nodePtr)
}
}
private func libxmlGetNodeContent(_ nodePtr: xmlNodePtr) -> String? {
let content = xmlNodeGetContent(nodePtr)
defer {
#if swift(>=4.1)
content?.deallocate()
#else
content?.deallocate(capacity: 1)
#endif
}
if let result = String(validatingUTF8: UnsafeRawPointer(content!).assumingMemoryBound(to: CChar.self)) {
return result
}
return nil
}
let entities = [
"&": "&",
"<": "<",
">": ">"
]
private func escape(_ str: String) -> String {
var newStr = str
for (unesc, esc) in entities {
newStr = newStr.replacingOccurrences(of: unesc, with: esc, options: .regularExpression, range: nil)
}
return newStr
}