node-html-parser 6.0.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +127 -47
- package/dist/main.js +2 -0
- package/dist/nodes/html.d.ts +2 -2
- package/dist/nodes/html.js +2 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [6.1.0](https://github.com/taoqf/node-fast-html-parser/compare/v6.0.0...v6.1.0) (2022-09-19)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* Add docs ([8a38eed](https://github.com/taoqf/node-fast-html-parser/commit/8a38eedab6b20906ee89dea86c4271960afbad2d))
|
|
11
|
+
|
|
5
12
|
## [6.0.0](https://github.com/taoqf/node-fast-html-parser/compare/v5.4.2-0...v6.0.0) (2022-09-08)
|
|
6
13
|
|
|
7
14
|
|
package/README.md
CHANGED
|
@@ -101,186 +101,266 @@ Parse the data provided, and return the root of the generated DOM.
|
|
|
101
101
|
|
|
102
102
|
Parse the data provided, return true if the given data is valid, and return false if not.
|
|
103
103
|
|
|
104
|
+
## Class
|
|
105
|
+
|
|
106
|
+
```mermaid
|
|
107
|
+
classDiagram
|
|
108
|
+
direction TB
|
|
109
|
+
class HTMLElement{
|
|
110
|
+
this trimRight()
|
|
111
|
+
this removeWhitespace()
|
|
112
|
+
Node[] querySelectorAll(string selector)
|
|
113
|
+
Node querySelector(string selector)
|
|
114
|
+
HTMLElement[] getElementsByTagName(string tagName)
|
|
115
|
+
Node closest(string selector)
|
|
116
|
+
Node appendChild(Node node)
|
|
117
|
+
this insertAdjacentHTML('beforebegin' | 'afterbegin' | 'beforeend' | 'afterend' where, string html)
|
|
118
|
+
this setAttribute(string key, string value)
|
|
119
|
+
this setAttributes(Record~string, string~ attrs)
|
|
120
|
+
this removeAttribute(string key)
|
|
121
|
+
string getAttribute(string key)
|
|
122
|
+
this exchangeChild(Node oldNode, Node newNode)
|
|
123
|
+
this removeChild(Node node)
|
|
124
|
+
string toString()
|
|
125
|
+
this set_content(string content)
|
|
126
|
+
this set_content(Node content)
|
|
127
|
+
this set_content(Node[] content)
|
|
128
|
+
this remove()
|
|
129
|
+
this replaceWith((string | Node)[] ...nodes)
|
|
130
|
+
ClassList classList
|
|
131
|
+
HTMLElement clone()
|
|
132
|
+
HTMLElement getElementById(string id)
|
|
133
|
+
string text
|
|
134
|
+
string rawText
|
|
135
|
+
string tagName
|
|
136
|
+
string structuredText
|
|
137
|
+
string structure
|
|
138
|
+
Node firstChild
|
|
139
|
+
Node lastChild
|
|
140
|
+
Node nextSibling
|
|
141
|
+
HTMLElement nextElementSibling
|
|
142
|
+
Node previousSibling
|
|
143
|
+
HTMLElement previousElementSibling
|
|
144
|
+
string innerHTML
|
|
145
|
+
string outerHTML
|
|
146
|
+
string textContent
|
|
147
|
+
Record~string, string~ attributes
|
|
148
|
+
[number, number] range
|
|
149
|
+
}
|
|
150
|
+
class Node{
|
|
151
|
+
<<abstract>>
|
|
152
|
+
string toString()
|
|
153
|
+
Node clone()
|
|
154
|
+
this remove()
|
|
155
|
+
number nodeType
|
|
156
|
+
string innerText
|
|
157
|
+
string textContent
|
|
158
|
+
}
|
|
159
|
+
class ClassList{
|
|
160
|
+
add(string c)
|
|
161
|
+
replace(string c1, string c2)
|
|
162
|
+
remove(string c)
|
|
163
|
+
toggle(string c)
|
|
164
|
+
boolean contains(string c)
|
|
165
|
+
number length
|
|
166
|
+
string[] value
|
|
167
|
+
string toString()
|
|
168
|
+
}
|
|
169
|
+
class CommentNode{
|
|
170
|
+
CommentNode clone()
|
|
171
|
+
string toString()
|
|
172
|
+
}
|
|
173
|
+
class TextNode{
|
|
174
|
+
TextNode clone()
|
|
175
|
+
string toString()
|
|
176
|
+
string rawText
|
|
177
|
+
string trimmedRawText
|
|
178
|
+
string trimmedText
|
|
179
|
+
string text
|
|
180
|
+
boolean isWhitespace
|
|
181
|
+
}
|
|
182
|
+
Node --|> HTMLElement
|
|
183
|
+
Node --|> CommentNode
|
|
184
|
+
Node --|> TextNode
|
|
185
|
+
Node ..> ClassList
|
|
186
|
+
```
|
|
187
|
+
|
|
104
188
|
## HTMLElement Methods
|
|
105
189
|
|
|
106
|
-
###
|
|
190
|
+
### trimRight()
|
|
107
191
|
|
|
108
192
|
Trim element from right (in block) after seeing pattern in a TextNode.
|
|
109
193
|
|
|
110
|
-
###
|
|
194
|
+
### removeWhitespace()
|
|
111
195
|
|
|
112
196
|
Remove whitespaces in this sub tree.
|
|
113
197
|
|
|
114
|
-
###
|
|
198
|
+
### querySelectorAll(selector)
|
|
115
199
|
|
|
116
200
|
Query CSS selector to find matching nodes.
|
|
117
201
|
|
|
118
202
|
Note: Full range of CSS3 selectors supported since v3.0.0.
|
|
119
203
|
|
|
120
|
-
###
|
|
204
|
+
### querySelector(selector)
|
|
121
205
|
|
|
122
206
|
Query CSS Selector to find matching node.
|
|
123
207
|
|
|
124
|
-
###
|
|
208
|
+
### getElementsByTagName(tagName)
|
|
125
209
|
|
|
126
210
|
Get all elements with the specified tagName.
|
|
127
211
|
|
|
128
212
|
Note: Use * for all elements.
|
|
129
213
|
|
|
130
|
-
###
|
|
214
|
+
### closest(selector)
|
|
131
215
|
|
|
132
216
|
Query closest element by css selector.
|
|
133
217
|
|
|
134
|
-
###
|
|
218
|
+
### appendChild(node)
|
|
135
219
|
|
|
136
220
|
Append a child node to childNodes
|
|
137
221
|
|
|
138
|
-
###
|
|
222
|
+
### insertAdjacentHTML(where, html)
|
|
139
223
|
|
|
140
224
|
Parses the specified text as HTML and inserts the resulting nodes into the DOM tree at a specified position.
|
|
141
225
|
|
|
142
|
-
###
|
|
226
|
+
### setAttribute(key: string, value: string)
|
|
143
227
|
|
|
144
228
|
Set `value` to `key` attribute.
|
|
145
229
|
|
|
146
|
-
###
|
|
230
|
+
### setAttributes(attrs: Record<string, string>)
|
|
147
231
|
|
|
148
232
|
Set attributes of the element.
|
|
149
233
|
|
|
150
|
-
###
|
|
234
|
+
### removeAttribute(key: string)
|
|
151
235
|
|
|
152
236
|
Remove `key` attribute.
|
|
153
237
|
|
|
154
|
-
###
|
|
238
|
+
### getAttribute(key: string)
|
|
155
239
|
|
|
156
240
|
Get `key` attribute.
|
|
157
241
|
|
|
158
|
-
###
|
|
242
|
+
### exchangeChild(oldNode: Node, newNode: Node)
|
|
159
243
|
|
|
160
244
|
Exchanges given child with new child.
|
|
161
245
|
|
|
162
|
-
###
|
|
246
|
+
### removeChild(node: Node)
|
|
163
247
|
|
|
164
248
|
Remove child node.
|
|
165
249
|
|
|
166
|
-
###
|
|
250
|
+
### toString()
|
|
167
251
|
|
|
168
252
|
Same as [outerHTML](#htmlelementouterhtml)
|
|
169
253
|
|
|
170
|
-
###
|
|
254
|
+
### set_content(content: string | Node | Node[])
|
|
171
255
|
|
|
172
256
|
Set content. **Notice**: Do not set content of the **root** node.
|
|
173
257
|
|
|
174
|
-
###
|
|
258
|
+
### remove()
|
|
175
259
|
|
|
176
260
|
Remove current element.
|
|
177
261
|
|
|
178
|
-
###
|
|
262
|
+
### replaceWith(...nodes: (string | Node)[])
|
|
179
263
|
|
|
180
264
|
Replace current element with other node(s).
|
|
181
265
|
|
|
182
|
-
###
|
|
266
|
+
### classList
|
|
183
267
|
|
|
184
|
-
####
|
|
268
|
+
#### classList.add
|
|
185
269
|
|
|
186
270
|
Add class name.
|
|
187
271
|
|
|
188
|
-
####
|
|
272
|
+
#### classList.replace(old: string, new: string)
|
|
189
273
|
|
|
190
274
|
Replace class name with another one.
|
|
191
275
|
|
|
192
|
-
####
|
|
276
|
+
#### classList.remove()
|
|
193
277
|
|
|
194
278
|
Remove class name.
|
|
195
279
|
|
|
196
|
-
####
|
|
280
|
+
#### classList.toggle(className: string):void
|
|
197
281
|
|
|
198
282
|
Toggle class. Remove it if it is already included, otherwise add.
|
|
199
283
|
|
|
200
|
-
####
|
|
284
|
+
#### classList.contains(className: string): boolean
|
|
201
285
|
|
|
202
286
|
Returns true if the classname is already in the classList.
|
|
203
287
|
|
|
204
|
-
####
|
|
288
|
+
#### classList.value
|
|
205
289
|
|
|
206
290
|
Get class names.
|
|
207
291
|
|
|
208
|
-
####
|
|
292
|
+
#### clone()
|
|
209
293
|
|
|
210
294
|
Clone a node.
|
|
211
295
|
|
|
212
|
-
####
|
|
296
|
+
#### getElementById(id: string): HTMLElement;
|
|
213
297
|
|
|
214
298
|
Get element by it's ID.
|
|
215
299
|
|
|
216
300
|
## HTMLElement Properties
|
|
217
301
|
|
|
218
|
-
###
|
|
302
|
+
### text
|
|
219
303
|
|
|
220
304
|
Get unescaped text value of current node and its children. Like `innerText`.
|
|
221
305
|
(slow for the first time)
|
|
222
306
|
|
|
223
|
-
###
|
|
307
|
+
### rawText
|
|
224
308
|
|
|
225
309
|
Get escaped (as-is) text value of current node and its children. May have
|
|
226
310
|
`&` in it. (fast)
|
|
227
311
|
|
|
228
|
-
###
|
|
312
|
+
### tagName
|
|
229
313
|
|
|
230
314
|
Get or Set tag name of HTMLElement. Notice: the returned value would be an uppercase string.
|
|
231
315
|
|
|
232
|
-
###
|
|
316
|
+
### structuredText
|
|
233
317
|
|
|
234
318
|
Get structured Text.
|
|
235
319
|
|
|
236
|
-
###
|
|
320
|
+
### structure
|
|
237
321
|
|
|
238
322
|
Get DOM structure.
|
|
239
323
|
|
|
240
|
-
###
|
|
324
|
+
### firstChild
|
|
241
325
|
|
|
242
326
|
Get first child node.
|
|
243
327
|
|
|
244
|
-
###
|
|
328
|
+
### lastChild
|
|
245
329
|
|
|
246
330
|
Get last child node.
|
|
247
331
|
|
|
248
|
-
###
|
|
332
|
+
### innerHTML
|
|
249
333
|
|
|
250
334
|
Set or Get innerHTML.
|
|
251
335
|
|
|
252
|
-
###
|
|
336
|
+
### outerHTML
|
|
253
337
|
|
|
254
338
|
Get outerHTML.
|
|
255
339
|
|
|
256
|
-
###
|
|
340
|
+
### nextSibling
|
|
257
341
|
|
|
258
342
|
Returns a reference to the next child node of the current element's parent.
|
|
259
343
|
|
|
260
|
-
###
|
|
344
|
+
### nextElementSibling
|
|
261
345
|
|
|
262
346
|
Returns a reference to the next child element of the current element's parent.
|
|
263
347
|
|
|
264
|
-
###
|
|
348
|
+
### previousSibling
|
|
265
349
|
|
|
266
350
|
Returns a reference to the previous child node of the current element's parent.
|
|
267
351
|
|
|
268
|
-
###
|
|
352
|
+
### previousElementSibling
|
|
269
353
|
|
|
270
354
|
Returns a reference to the previous child element of the current element's parent.
|
|
271
355
|
|
|
272
|
-
###
|
|
356
|
+
### textContent
|
|
273
357
|
|
|
274
358
|
Get or Set textContent of current element, more efficient than [set_content](#htmlelementset_contentcontent-string--node--node).
|
|
275
359
|
|
|
276
|
-
###
|
|
277
|
-
|
|
278
|
-
Get all attributes of current element. **Notice: do not try to change the returned value.**
|
|
279
|
-
|
|
280
|
-
### HTMLElement#classList
|
|
360
|
+
### attributes
|
|
281
361
|
|
|
282
362
|
Get all attributes of current element. **Notice: do not try to change the returned value.**
|
|
283
363
|
|
|
284
|
-
###
|
|
364
|
+
### range
|
|
285
365
|
|
|
286
366
|
Corresponding source code start and end indexes (ie [ 0, 40 ])
|
package/dist/main.js
CHANGED
|
@@ -731,6 +731,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
|
|
|
731
731
|
});
|
|
732
732
|
resetParent([this], null);
|
|
733
733
|
parent.childNodes = __spreadArray(__spreadArray(__spreadArray([], parent.childNodes.slice(0, idx), true), resetParent(content, parent), true), parent.childNodes.slice(idx + 1), true);
|
|
734
|
+
return this;
|
|
734
735
|
};
|
|
735
736
|
Object.defineProperty(HTMLElement.prototype, "outerHTML", {
|
|
736
737
|
get: function () {
|
|
@@ -1127,6 +1128,7 @@ define("nodes/html", ["require", "exports", "css-select", "he", "back", "matcher
|
|
|
1127
1128
|
if (key === 'id') {
|
|
1128
1129
|
this.id = value;
|
|
1129
1130
|
}
|
|
1131
|
+
return this;
|
|
1130
1132
|
};
|
|
1131
1133
|
/**
|
|
1132
1134
|
* Replace all the attributes of the HTMLElement by the provided attributes
|
package/dist/nodes/html.d.ts
CHANGED
|
@@ -98,7 +98,7 @@ export default class HTMLElement extends Node {
|
|
|
98
98
|
get innerHTML(): string;
|
|
99
99
|
set innerHTML(content: string);
|
|
100
100
|
set_content(content: string | Node | Node[], options?: Partial<Options>): this;
|
|
101
|
-
replaceWith(...nodes: (string | Node)[]):
|
|
101
|
+
replaceWith(...nodes: (string | Node)[]): this;
|
|
102
102
|
get outerHTML(): string;
|
|
103
103
|
/**
|
|
104
104
|
* Trim element from right (in block) after seeing pattern in a TextNode.
|
|
@@ -183,7 +183,7 @@ export default class HTMLElement extends Node {
|
|
|
183
183
|
* @param {string} key The attribute name
|
|
184
184
|
* @param {string} value The value to set, or null / undefined to remove an attribute
|
|
185
185
|
*/
|
|
186
|
-
setAttribute(key: string, value: string):
|
|
186
|
+
setAttribute(key: string, value: string): this;
|
|
187
187
|
/**
|
|
188
188
|
* Replace all the attributes of the HTMLElement by the provided attributes
|
|
189
189
|
* @param {Attributes} attributes the new attribute set
|
package/dist/nodes/html.js
CHANGED
|
@@ -398,6 +398,7 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
398
398
|
});
|
|
399
399
|
resetParent([this], null);
|
|
400
400
|
parent.childNodes = __spreadArray(__spreadArray(__spreadArray([], parent.childNodes.slice(0, idx), true), resetParent(content, parent), true), parent.childNodes.slice(idx + 1), true);
|
|
401
|
+
return this;
|
|
401
402
|
};
|
|
402
403
|
Object.defineProperty(HTMLElement.prototype, "outerHTML", {
|
|
403
404
|
get: function () {
|
|
@@ -794,6 +795,7 @@ var HTMLElement = /** @class */ (function (_super) {
|
|
|
794
795
|
if (key === 'id') {
|
|
795
796
|
this.id = value;
|
|
796
797
|
}
|
|
798
|
+
return this;
|
|
797
799
|
};
|
|
798
800
|
/**
|
|
799
801
|
* Replace all the attributes of the HTMLElement by the provided attributes
|
package/package.json
CHANGED