/
element.js
322 lines (225 loc) · 5.51 KB
/
element.js
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//const Node = require('./node.js')
//const Elements = require('./elements.js').Elements
const cache = new WeakMap()
class Element /*extends Node*/{
constructor(dom,$){
if(cache.has(dom)){
return cache.get(dom)
}
this.dom = dom
this._$ = $
cache.set(dom, this)
}
get cheerio(){
return this._$(this.dom)
}
attr(attributeName){ //String
return this.cheerio.attr(attributeName) || ""
}
child(index){ //Element
return new Element(this.cheerio.children().get(index), this._$)
}
children(){ //Elements
return new Elements(this.cheerio.children(), this._$)
}
className(){//String
return this.attr("class")
}
classNames(){//String[]
return this.className.split(" ")
}
data(){//String
}
filter(){
}
firstElementSibling(){//Element
}
getAllElements(){//Elements
}
hasAttr(attributeName){//Boolean
return !!this.attr(attributeName)
}
hasClass(className){//Boolean
return this.classNames.some(v=>v==className)
}
hasText(){//Boolean
}
html(){//String
return this.cheerio.html()
}
id(){//String
return this.attr("id")
}
lastElementSibling(){//Element
}
nextElementSibling(){//Element
}
nextElementSiblings(){//Elements
}
ownText(){//String
return this.dom.children
.filter(node=>node.nodeType==3) // nodeType 3 means Text
.map(v=>v.data)
.join("")
}
parent(){//Element
if (this.cheerio.parent().length>0)
return new Element(this.cheerio.parent().get(0), this._$)
else
return this.root()
// return new Element(this.cheerio.parent().get(0), this._$)
}
parents(){//Elements
return new Elements(this.cheerio.parents(), this._$)
}
previousElementSibling(){//Element
}
previousElementSiblings(){//Elements
}
root(){//Element
// return new Element(this._$.root(), this._$)
return new Document(this._$.root()[0], this._$)
}
select(cssQuery){//Elements
return new Elements(this.cheerio.find(cssQuery), this._$)
}
selectFirst(cssQuery){//Element
return this.select(cssQuery).first()
}
siblingElements(){//Elements
}
tagName(){//String
return this.dom.name
}
text(){//String
function travel(dom){
return dom.children.map(v=>v.type=="tag" ? travel(v) : v.type=="text" ? v.data.trim() : "").filter(v=>v!="").join(" ")
}
return travel(this.dom)
// return this.cheerio.text()
}
toString(){
return this.cheerio.toString()
}
val(){//String
}
wholeText(){//String
// return this.text().replace(/<.+?>/g,"") // correct?
return this.text() // correct?
}
}
exports.Element = Element
class Document extends Element{
constructor(dom,$){
return super(dom,$)
}
body(){//Element
return this.selectFirst("body")
}
charset(){//Charset? String?
}
head(){//Element
return this.selectFirst("head")
}
nodeName(){//String
}
parent(){//Elemen
return null
}
title(){//String
return this.selectFirst("title")
}
}
exports.Document = Document
class Elements{//extends List
constructor(cheerio,$){
this.cheerio = cheerio
this._$ = $
}
attr(attributeKey){//String
let res = ""
for(let i=0; i<this.length; i++){
if(this.get(i).hasAttr(attributeKey)){
res = this.get(i).attr(attributeKey)
break
}
}
return res
}
comments(){//Comment[]?
}
eachAttr(attributeKey){//String[]
return this.toArray()
.map(elem=>elem.attr(attributeKey))
.filter(v=>v!="")
}
eachText(){//String[]
return this.toArray().map(elem=>elem.text())
}
eq(index){//Elements
return new Elements(this.cheerio.eq(index), this._$)
}
filter(){
}
first(){//Element
return (this.length>0) ? this.get(0) : null
}
get(index){//Element
return new Element(this.cheerio.get(index), this._$)
}
hasAttr(){//Boolean
}
hasClass(){//Boolean
}
hasText(){//Boolean
}
html(){//String
return this.toArray()
.map(elem=>elem.html().trim())
.join("\n")
}
last(){//Element
return (this.length>0) ? this.get(this.cheerio.length-1) : null
}
get length(){
return this.size()
}
next(){//Elements
}
nextAll(){//Elements
}
not(query){//Elements
}
parents(){//Elements
}
prev(){//Elements
}
prevAll(){//Elements
}
select(cssQuery){//Elements
return new Elements(this.cheerio.find(cssQuery), this._$)
}
selectFirst(cssQuery){//Element
return this.select(cssQuery).first()
}
size(){
return this.cheerio.length
}
text(){//String
return this.toArray().map(elem=>elem.text()).join(" ")
// return this.cheerio.text()
}
toArray(){//Element[]
const res = []
for(let i=0; i<this.length; i++){
res.push(this.get(i))
}
return res
}
toString(){//String
return this.cheerio.toString()
}
val(){//String
}
}
exports.Elements = Elements