als-document 0.5.1 → 0.6.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/document/document.js +192 -0
- package/document/test.js +678 -0
- package/document.js +372 -142
- package/document.min.js +1 -0
- package/index.js +6 -0
- package/package.json +3 -3
- package/parser/parser.js +89 -40
- package/parser/test.js +7 -7
- package/query/query.js +1 -1
- package/query/readme.md +4 -1
- package/readme.md +249 -59
- package/selector/selector.js +14 -13
- package/test/test.js +3 -1
- package/parser/readme.md +0 -121
- package/selector/readme.md +0 -74
package/readme.md
CHANGED
|
@@ -1,62 +1,135 @@
|
|
|
1
|
-
#
|
|
1
|
+
# als-document
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
Build virtual dom from string and manage it, similar you do it on browser.
|
|
4
|
+
Now it's fast and simple!
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
## About
|
|
7
|
+
Als-document is a library which includes 4 instruments:
|
|
7
8
|
* Html parser - for fast html string parsing and building dom tree
|
|
8
9
|
* Html query parser - for destructing css/html query to selectors to array of objects
|
|
9
10
|
* Html selector - for selecting elements with css/html query inside parsed dom tree
|
|
11
|
+
* Document - extending html selector with managing tools
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
- [als-document](#als-document)
|
|
15
|
+
- [About](#about)
|
|
16
|
+
- [Basics](#basics)
|
|
17
|
+
- [Example](#example)
|
|
18
|
+
- [Element properties and methods](#element-properties-and-methods)
|
|
19
|
+
- [Selecting elements](#selecting-elements)
|
|
20
|
+
- [Extra abilities](#extra-abilities)
|
|
21
|
+
- [Add/rearrange/remove element](#addrearrangeremove-element)
|
|
22
|
+
- [Example](#example-1)
|
|
23
|
+
- [Changing attributes](#changing-attributes)
|
|
24
|
+
- [Inline style](#inline-style)
|
|
25
|
+
- [Classlist](#classlist)
|
|
26
|
+
- [HtmlParser documentation](#htmlparser-documentation)
|
|
27
|
+
- [new in 0.6](#new-in-06)
|
|
28
|
+
- [Syntax](#syntax)
|
|
29
|
+
- [Frontend example](#frontend-example)
|
|
30
|
+
- [Backend example](#backend-example)
|
|
31
|
+
- [Query documentation](#query-documentation)
|
|
32
|
+
- [Syntax](#syntax-1)
|
|
33
|
+
- [Example](#example-2)
|
|
34
|
+
- [Attribs and check function](#attribs-and-check-function)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
## Basics
|
|
39
|
+
You can use als-document on node.js and in browser.
|
|
40
|
+
|
|
41
|
+
For using in browser, just include:
|
|
42
|
+
``node_modules/als-document/document.js``
|
|
43
|
+
or ``node_modules/als-document/document.min.js``.
|
|
10
44
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Everything does Query class support, can be selected (so far available all, except pseudos).
|
|
15
|
-
Selector can be used on frontend and on backend.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
### Basics
|
|
45
|
+
```html
|
|
46
|
+
<script src="node_modules/als-document/document.min.js"></script>
|
|
47
|
+
```
|
|
19
48
|
|
|
20
|
-
|
|
49
|
+
For using on backend, require from ``als-document``.
|
|
21
50
|
```javascript
|
|
22
|
-
let
|
|
23
|
-
doc.$(query:string):object | null
|
|
24
|
-
doc.$$(query:string):array
|
|
25
|
-
doc.$(query:string).$(query:qeury).$$(query:qeury)
|
|
51
|
+
let {HtmlSelector,HtmlParser,Query,Document} = require('als-document')
|
|
26
52
|
```
|
|
27
53
|
|
|
28
|
-
|
|
29
|
-
* ``$`` - method return first element or null
|
|
30
|
-
* ``$$`` - return collection or empty array
|
|
31
|
-
* Each element, has ``$`` and ``$$`` methods for selecting descendants
|
|
54
|
+
Basicly, you need only Document. Because Document uses all the rest. But you can use any class separatly too.
|
|
32
55
|
|
|
33
56
|
|
|
34
|
-
|
|
57
|
+
### Example
|
|
35
58
|
```javascript
|
|
36
|
-
let
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
<script>
|
|
47
|
-
let htmlText = `
|
|
48
|
-
<div>
|
|
49
|
-
<span>Another one</span>
|
|
50
|
-
<div>Some text</div>
|
|
51
|
-
</div>
|
|
59
|
+
let htmlText = /*html*/`
|
|
60
|
+
<!DOCTYPE html>
|
|
61
|
+
<html lang="en">
|
|
62
|
+
<head>
|
|
63
|
+
<title>Test document</title>
|
|
64
|
+
</head>
|
|
65
|
+
<body>
|
|
66
|
+
<div id="test">Hello world!</div>
|
|
67
|
+
</body>
|
|
68
|
+
</html>
|
|
52
69
|
`
|
|
53
|
-
let doc = new
|
|
54
|
-
let
|
|
55
|
-
|
|
70
|
+
let doc = new Document(htmlText)
|
|
71
|
+
let body = doc.$('body')
|
|
72
|
+
let div = body.$('#test')
|
|
73
|
+
console.log(div.innerHTML) // Hello world!
|
|
74
|
+
div.before = '<div id="test1">Another div</div>'
|
|
75
|
+
console.log(div.prev.id) // test1
|
|
56
76
|
```
|
|
57
77
|
|
|
58
78
|
|
|
59
|
-
|
|
79
|
+
## Element properties and methods
|
|
80
|
+
Each element (except root and text elements, which has no all) has:
|
|
81
|
+
* Variables
|
|
82
|
+
* ``attribs`` - element's attributes as object
|
|
83
|
+
* ``parent`` - parent element
|
|
84
|
+
* ``children`` - array of children include text nodes
|
|
85
|
+
* ``type`` - tag or text or root
|
|
86
|
+
* ``classList`` - array with classes and methods for managing classList
|
|
87
|
+
* ``add`` - adds new class to classList
|
|
88
|
+
* ``remove`` - removes class from classList
|
|
89
|
+
* ``toggle`` - toggle class inside classList
|
|
90
|
+
* ``index`` - start index of element inside elements list
|
|
91
|
+
* ``endIndex`` - end index of element inside elements list
|
|
92
|
+
* ``id`` - getter for element's id (return id or null)
|
|
93
|
+
* ``level`` - level in dom tree
|
|
94
|
+
* ``text`` - parsed text for tag and for text element
|
|
95
|
+
* ``tab`` - space for formating innerHTML. default:tab= ' '
|
|
96
|
+
* ``n`` - separate elements in innerHTML by n. default n = '\n'
|
|
97
|
+
* ``style`` - Proxy array of styles with camelCase property name
|
|
98
|
+
* getters
|
|
99
|
+
* ``innerText`` - concats all children's text together | ''
|
|
100
|
+
* ``innerHTML`` - return innerHTML for element
|
|
101
|
+
* ``outerHTML`` - return outerHTML for element
|
|
102
|
+
* ``ancestors`` - return array of ancestors
|
|
103
|
+
* ``root`` - root element
|
|
104
|
+
* ``html`` - HtmlParser object for all elements
|
|
105
|
+
* ``$elements`` - all parsed elements
|
|
106
|
+
* ``next`` - next element or null
|
|
107
|
+
* ``prev`` - previous element or null
|
|
108
|
+
* ``childIndex`` - index of child in parent.children
|
|
109
|
+
* ``id`` - getting id of element
|
|
110
|
+
* Setters
|
|
111
|
+
* ``before`` - adding new/existing element before the element
|
|
112
|
+
* ``after`` - adding new/existing element after the element
|
|
113
|
+
* ``first`` - adding new/existing element as first child of the element
|
|
114
|
+
* ``last`` - adding new/existing element as last child of the element
|
|
115
|
+
* Methods
|
|
116
|
+
* ``getAttribute(name)`` - return value of attribute or null
|
|
117
|
+
* ``attr(name,value``) - get,set,remove attribute
|
|
118
|
+
* ``remove()`` - removing element
|
|
119
|
+
* ``insert(element,position)`` - inserting/rearanging element
|
|
120
|
+
* ``$`` - for selecting any descendant of the element
|
|
121
|
+
* ``$$`` - for selecting collection of descendants of the element
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
## Selecting elements
|
|
125
|
+
For select elements inside virtual dom, you can use ``HtmlSelector`` or ``Document``.
|
|
126
|
+
If you need only selecting, use ``HtmlSelector``, otherwise use ``Document``.
|
|
127
|
+
Document is extended HtmlSelector.
|
|
128
|
+
|
|
129
|
+
two methods:
|
|
130
|
+
* ``$`` - for selecting first element
|
|
131
|
+
* ``$$`` - for selecting collection
|
|
132
|
+
|
|
60
133
|
```javascript
|
|
61
134
|
let {HtmlSelector} = require('als-document')
|
|
62
135
|
let htmlText = `
|
|
@@ -67,12 +140,16 @@ let htmlText = `
|
|
|
67
140
|
`
|
|
68
141
|
let doc = new HtmlSelector(htmlText)
|
|
69
142
|
let span = doc.$('div>span')
|
|
143
|
+
let divs = doc.$$('div')
|
|
70
144
|
```
|
|
71
145
|
|
|
146
|
+
So far, available almost all possible selectors, without pseudo class and pseudo element selector.
|
|
147
|
+
For example, you can select ``div>.some span+input.some-class[checked]``.
|
|
148
|
+
But you can't select ``input:checked``.
|
|
72
149
|
|
|
73
150
|
### Extra abilities
|
|
74
151
|
Usualy, browser selector can select by tag name,class, attribute or id.
|
|
75
|
-
HtmlSelector, has extra selecting options, since id,style,class,events and innerHTML - can be selected as attribute.
|
|
152
|
+
HtmlSelector/Document, has extra selecting options, since id,style,class,events and innerHTML - can be selected as attribute.
|
|
76
153
|
|
|
77
154
|
For example you can do those things:
|
|
78
155
|
```javascript
|
|
@@ -82,13 +159,113 @@ doc.$('[class*="btn"][class*="danger"]') //class includes "btn" and "danger"
|
|
|
82
159
|
doc.$('[id^="tab"]') //id starts with "tab-"
|
|
83
160
|
doc.$('[inner$="00"]') //innerText ends with ".00"
|
|
84
161
|
```
|
|
85
|
-
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
## Add/rearrange/remove element
|
|
165
|
+
Document has methods and setters for changing virtual dom. Here are the methods:
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
let newElement = string || object // html text or another selected element
|
|
169
|
+
let position = number [0,1,2,3] // 0-before element,1-first child,2-last-child,3-after element
|
|
170
|
+
element.insert(newElement,position):object // insert new element or rearange existing element
|
|
171
|
+
element.remove() // removig existing element from dom tree
|
|
172
|
+
element.before = newElement // adding new element before the element
|
|
173
|
+
element.after = newElement // adding new element after the element
|
|
174
|
+
element.first = newElement // adding new element as first child of element
|
|
175
|
+
element.last = newElement // adding new element as last child of element
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
> before,after,first,last are getters which uses insert method.
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
### Example
|
|
182
|
+
```javascript
|
|
183
|
+
// create new element and insert it before first div
|
|
184
|
+
doc.$('div').insert('<div>new element</div>',0)
|
|
185
|
+
doc.$('div').before = '<div>new element</div>'
|
|
186
|
+
// Rearange div#some as first child for first div
|
|
187
|
+
doc.$('div').insert(doc.$('div#some'),1)
|
|
188
|
+
doc.$('div').first = doc.$('div#some')
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
## Changing attributes
|
|
193
|
+
There are few ways to get element's attributes:
|
|
194
|
+
```javascript
|
|
195
|
+
doc.$('div').attribs.name // available on HtmlParser and Document
|
|
196
|
+
doc.$('div').getAttribute(name) // available on HtmlParser and Document
|
|
197
|
+
doc.$('div').attr(name) // not available on HtmlParser
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
For setting or removing attributes, use attr method (available only in Document).
|
|
201
|
+
Here the syntax:
|
|
202
|
+
```javascript
|
|
203
|
+
doc.$('input').attr('checked','') // add checked attribute
|
|
204
|
+
doc.$('div#some').attr('title','Some title') // set title="Some title"
|
|
205
|
+
doc.$('input').attr('disabled',null) // remove disabled attribute
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
If you changing the class attribute with attr, classList changing too.
|
|
209
|
+
|
|
210
|
+
> Don't remove or change style and id with attr
|
|
211
|
+
> Dont't remove class with attr
|
|
212
|
+
|
|
213
|
+
element.id has getter and setter
|
|
214
|
+
* getter - return id | null
|
|
215
|
+
* setter - changing id in element.attribs.id
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
### Inline style
|
|
219
|
+
``element.style`` is a Proxy object, which means you can't get all style properties as object.
|
|
220
|
+
To get styles object use ``element.style.target``.
|
|
221
|
+
|
|
222
|
+
Syntax:
|
|
223
|
+
```javascript
|
|
224
|
+
let element = doc.$('[style]')
|
|
225
|
+
// Get style object with camelCase property names
|
|
226
|
+
element.style.target
|
|
227
|
+
// Get value for specific style property
|
|
228
|
+
element.style.backgroundColor // value || undefined
|
|
229
|
+
// Set new value for existing or for new property
|
|
230
|
+
element.style.backgroundColor = 'blue'
|
|
231
|
+
// Remove property from style and from attribs.style
|
|
232
|
+
delete element.style.backgroundColor
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
### Classlist
|
|
237
|
+
``ClassList`` is array with existing classes splited from ``attribs.class``.
|
|
238
|
+
|
|
239
|
+
Syntax:
|
|
240
|
+
```javascript
|
|
241
|
+
let element = doc.$('[class]')
|
|
242
|
+
// The folowing methods, changing element.attribs.class too.
|
|
243
|
+
element.add(newClassName)
|
|
244
|
+
element.remove(existingClassName)
|
|
245
|
+
element.toggle(className)
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
## HtmlParser documentation
|
|
86
250
|
|
|
87
251
|
HtmlParser is a class which build dom tree from html string.
|
|
88
252
|
|
|
89
253
|
* HtmlParser removes all html comments and they not included in dom tree.
|
|
90
254
|
* Contrary to regular dom, attribute includes class,id and style as attribute in addition to classList, id and style(as array) inside element's object.
|
|
91
255
|
|
|
256
|
+
### new in 0.6
|
|
257
|
+
* fixed
|
|
258
|
+
* bug with text for elements with events - solved
|
|
259
|
+
* elements for single tag - solved
|
|
260
|
+
* Updates
|
|
261
|
+
* added root getter to each element
|
|
262
|
+
* added $elements getter to each element
|
|
263
|
+
* next and prev,childIndex now getters
|
|
264
|
+
* added childIndex,next,prev,ancestors,parent to text element
|
|
265
|
+
* root has innerHTML
|
|
266
|
+
* option to format innerHTML
|
|
267
|
+
* by default element.tab = ' '
|
|
268
|
+
* by default element.n = '\n'
|
|
92
269
|
|
|
93
270
|
### Syntax
|
|
94
271
|
|
|
@@ -103,8 +280,6 @@ HtmlParser.parse(html):object // parsed.root
|
|
|
103
280
|
Each element, except root and text elements has:
|
|
104
281
|
* attribs - element's attributes
|
|
105
282
|
* parent - parent element
|
|
106
|
-
* next - next element or null
|
|
107
|
-
* prev - previous element or null
|
|
108
283
|
* children - array of children include text nodes
|
|
109
284
|
* type - tag or text or root
|
|
110
285
|
* classList - array with classes
|
|
@@ -113,10 +288,18 @@ Each element, except root and text elements has:
|
|
|
113
288
|
* endIndex - end index of element inside elements list
|
|
114
289
|
* level - level in dom tree
|
|
115
290
|
* text - parsed text for tag and for text element
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
*
|
|
291
|
+
* tab - space for formating innerHTML. default:tab= ' '
|
|
292
|
+
* n - separate elements in innerHTML by n. default n = '\n'
|
|
293
|
+
* getters
|
|
294
|
+
* innerText - concats all children's text together | ''
|
|
295
|
+
* innerHTML - return innerHTML for element
|
|
296
|
+
* outerHTML - return outerHTML for element
|
|
297
|
+
* ancestors - return array of ancestors
|
|
298
|
+
* root - root element
|
|
299
|
+
* added $elements - all parsed elements
|
|
300
|
+
* next - next element or null
|
|
301
|
+
* prev - previous element or null
|
|
302
|
+
* childIndex - index of child in parent.children
|
|
120
303
|
* getAttribute(name) - return value of attribute or null
|
|
121
304
|
* style:[] - array of styles with camelCase property name
|
|
122
305
|
|
|
@@ -125,6 +308,7 @@ Example for parsed.document
|
|
|
125
308
|
```javascript
|
|
126
309
|
{
|
|
127
310
|
type:'root',
|
|
311
|
+
...,
|
|
128
312
|
children:[
|
|
129
313
|
{
|
|
130
314
|
attribs: {},
|
|
@@ -178,23 +362,25 @@ Example for parsed.document
|
|
|
178
362
|
|
|
179
363
|
|
|
180
364
|
### Frontend example
|
|
365
|
+
You can use:
|
|
366
|
+
* node_modules/als-document/document.js - original file
|
|
367
|
+
* node_modules/als-document/document.min.js - minimized file
|
|
181
368
|
|
|
182
369
|
```html
|
|
183
|
-
<script src="
|
|
370
|
+
<script src="node_modules/als-document/document.min.js"></script>
|
|
184
371
|
<script>
|
|
185
|
-
let result = new HtmlParser(htmlString)
|
|
186
|
-
console.log(result.root)
|
|
187
|
-
|
|
188
|
-
// Or with static method
|
|
189
|
-
console.log(HtmlParser.parse(html))
|
|
372
|
+
let result = new HtmlParser(htmlString)
|
|
373
|
+
console.log(result.root)
|
|
190
374
|
|
|
375
|
+
// Or with static method
|
|
376
|
+
console.log(HtmlParser.parse(html))
|
|
191
377
|
</script>
|
|
192
378
|
```
|
|
193
379
|
|
|
194
380
|
### Backend example
|
|
195
381
|
|
|
196
382
|
```javascript
|
|
197
|
-
const {HtmlParser} = require('als-
|
|
383
|
+
const {HtmlParser} = require('als-document')
|
|
198
384
|
let result = new HtmlParser(htmlString)
|
|
199
385
|
console.log(result.root)
|
|
200
386
|
|
|
@@ -203,15 +389,18 @@ console.log(HtmlParser.parse(html))
|
|
|
203
389
|
```
|
|
204
390
|
|
|
205
391
|
|
|
206
|
-
## Query
|
|
392
|
+
## Query documentation
|
|
207
393
|
Query is a class for parsing selectors inside html query. Query not supporting pseudo selectors so far.
|
|
208
394
|
You can use Query on frontend and on backend.
|
|
209
395
|
|
|
210
396
|
Query can be used on fronten and on backend.
|
|
397
|
+
You can use:
|
|
398
|
+
* node_modules/als-document/document.js - original file
|
|
399
|
+
* node_modules/als-document/document.min.js - minimized file
|
|
211
400
|
|
|
212
401
|
Frontend:
|
|
213
402
|
```html
|
|
214
|
-
<script src="
|
|
403
|
+
<script src="node_modules/als-document/document.min.js"></script>
|
|
215
404
|
```
|
|
216
405
|
|
|
217
406
|
Backend:
|
|
@@ -334,3 +523,4 @@ let s = Query.get('[test^="some"]')[0]
|
|
|
334
523
|
console.log(s.attribs[0].check('some value test')) // true
|
|
335
524
|
```
|
|
336
525
|
|
|
526
|
+
|
package/selector/selector.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
+
let Query = require('../query/query')
|
|
2
|
+
let HtmlParser = require("../parser/parser")
|
|
3
|
+
|
|
1
4
|
class HtmlSelector {
|
|
2
5
|
constructor(html) {
|
|
3
6
|
if(typeof html == 'string') {
|
|
4
|
-
html = new HtmlParser(html)
|
|
5
|
-
this.
|
|
6
|
-
this.elements = html.elements
|
|
7
|
-
this.makeSelectable()
|
|
7
|
+
this.html = new HtmlParser(html)
|
|
8
|
+
this.elements.forEach((element,i) => {this.makeSelectable(element)});
|
|
8
9
|
} else console.log('Parameter is not string')
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
})
|
|
11
|
+
get elements() {return this.html.elements}
|
|
12
|
+
|
|
13
|
+
makeSelectable(element) {
|
|
14
|
+
if(element.type == 'tag' && element.status !== 'close') {
|
|
15
|
+
element.$$ = (query) => this.$$(query,element.index,element.endIndex)
|
|
16
|
+
element.$ = (query) => this.$(query,element.index,element.endIndex)
|
|
17
|
+
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
$(query,start=0,end=this.elements.length) {
|
|
@@ -120,6 +120,7 @@ class HtmlSelector {
|
|
|
120
120
|
if(passedTests == attribs.length) return true
|
|
121
121
|
else return false
|
|
122
122
|
}
|
|
123
|
+
|
|
123
124
|
}
|
|
124
125
|
|
|
125
|
-
|
|
126
|
+
module.exports = HtmlSelector
|
package/test/test.js
CHANGED
|
@@ -4,12 +4,14 @@ let Test = require('als-test')
|
|
|
4
4
|
let parser = require('../parser/test')
|
|
5
5
|
let query = require('../query/test')
|
|
6
6
|
let selector = require('../selector/test')
|
|
7
|
+
let document = require('../document/test')
|
|
7
8
|
|
|
8
9
|
async function run() {
|
|
9
10
|
await Test.run('Document tests',[
|
|
10
11
|
parser,
|
|
11
12
|
query,
|
|
12
|
-
selector
|
|
13
|
+
selector,
|
|
14
|
+
document
|
|
13
15
|
],{html1,html2})
|
|
14
16
|
|
|
15
17
|
Test.summary()
|
package/parser/readme.md
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
## HtmlParser
|
|
2
|
-
|
|
3
|
-
HtmlParser is a class which build dom tree from html string.
|
|
4
|
-
|
|
5
|
-
* HtmlParser removes all html comments and they not included in dom tree.
|
|
6
|
-
* Contrary to regular dom, attribute includes class,id and style as attribute in addition to classList, id and style(as array) inside element's object.
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
### Syntax
|
|
10
|
-
|
|
11
|
-
```javascript
|
|
12
|
-
let parsed = new HtmlParser(htmlString:string):instanceof HtmlParser
|
|
13
|
-
parsed.root : circular object
|
|
14
|
-
|
|
15
|
-
// static method
|
|
16
|
-
HtmlParser.parse(html):object // parsed.root
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Each element, except root and text elements has:
|
|
20
|
-
* attribs - element's attributes
|
|
21
|
-
* parent - parent element
|
|
22
|
-
* next - next element or null
|
|
23
|
-
* prev - previous element or null
|
|
24
|
-
* children - array of children include text nodes
|
|
25
|
-
* type - tag or text or root
|
|
26
|
-
* classList - array with classes
|
|
27
|
-
* index - start index of element inside elements list
|
|
28
|
-
* id - element's id or null
|
|
29
|
-
* endIndex - end index of element inside elements list
|
|
30
|
-
* level - level in dom tree
|
|
31
|
-
* text - parsed text for tag and for text element
|
|
32
|
-
* innerText:getter - concats all children's text together | ''
|
|
33
|
-
* innerHTML:getter - return innerHTML for element
|
|
34
|
-
* outerHTML:getter - return outerHTML for element
|
|
35
|
-
* ancestors:getter - return array of ancestors
|
|
36
|
-
* getAttribute(name) - return value of attribute or null
|
|
37
|
-
* style:[] - array of styles with camelCase property name
|
|
38
|
-
|
|
39
|
-
Example for parsed.document
|
|
40
|
-
|
|
41
|
-
```javascript
|
|
42
|
-
{
|
|
43
|
-
type:'root',
|
|
44
|
-
children:[
|
|
45
|
-
{
|
|
46
|
-
attribs: {},
|
|
47
|
-
index: 0,
|
|
48
|
-
prev:null,
|
|
49
|
-
next:{...}
|
|
50
|
-
tag: "!DOCTYPE html",
|
|
51
|
-
type: "tag",
|
|
52
|
-
...
|
|
53
|
-
},
|
|
54
|
-
{
|
|
55
|
-
attribs: {lang:'en'},
|
|
56
|
-
prev:{...},
|
|
57
|
-
next:null,
|
|
58
|
-
classList:[],
|
|
59
|
-
children:[
|
|
60
|
-
{
|
|
61
|
-
attribs: {},
|
|
62
|
-
children:[...]
|
|
63
|
-
prev:null,
|
|
64
|
-
next:{...}
|
|
65
|
-
classList:[],
|
|
66
|
-
index: 2,
|
|
67
|
-
tag: "head",
|
|
68
|
-
parent:{tag:'html',...} // reference to parent
|
|
69
|
-
type: "tag",
|
|
70
|
-
...
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
attribs: {},
|
|
74
|
-
children:[...]
|
|
75
|
-
index: 10,
|
|
76
|
-
prev:{...},
|
|
77
|
-
next:null,
|
|
78
|
-
classList:[],
|
|
79
|
-
tag: "body",
|
|
80
|
-
parent:{tag:'html',...} // reference to parent
|
|
81
|
-
type: "tag",
|
|
82
|
-
...
|
|
83
|
-
},
|
|
84
|
-
]
|
|
85
|
-
index: 1,
|
|
86
|
-
tag: "html",
|
|
87
|
-
parent:{type:'root',...} // reference to parent
|
|
88
|
-
type: "tag",
|
|
89
|
-
...
|
|
90
|
-
}
|
|
91
|
-
]
|
|
92
|
-
}
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
### Frontend example
|
|
97
|
-
|
|
98
|
-
```html
|
|
99
|
-
<script src="/node_modules/als-document/parser/parser.js"></script>
|
|
100
|
-
<script>
|
|
101
|
-
let result = new HtmlParser(htmlString)
|
|
102
|
-
console.log(result.root)
|
|
103
|
-
|
|
104
|
-
// Or with static method
|
|
105
|
-
console.log(HtmlParser.parse(html))
|
|
106
|
-
|
|
107
|
-
</script>
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### Backend example
|
|
111
|
-
|
|
112
|
-
```javascript
|
|
113
|
-
const {HtmlParser} = require('als-htmlparser')
|
|
114
|
-
let result = new HtmlParser(htmlString)
|
|
115
|
-
console.log(result.root)
|
|
116
|
-
|
|
117
|
-
// Or with static method
|
|
118
|
-
console.log(HtmlParser.parse(html))
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
|
package/selector/readme.md
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
## Selector
|
|
2
|
-
|
|
3
|
-
Selector is a class which uses HtmlParser for parsing html and Query for parsing html/css query.
|
|
4
|
-
Everything does Query class support, can be selected (so far available all, except pseudos).
|
|
5
|
-
Selector can be used on frontend and on backend.
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
### Basics
|
|
9
|
-
|
|
10
|
-
**Syntax**
|
|
11
|
-
```javascript
|
|
12
|
-
let doc = new HtmlSelector(htmlString:string):instanceof HtmlSelector
|
|
13
|
-
doc.$(query:string):object | null
|
|
14
|
-
doc.$$(query:string):array
|
|
15
|
-
doc.$(query:string).$(query:qeury).$$(query:qeury)
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
* ``query`` - html/css query for selecting
|
|
19
|
-
* ``$`` - method return first element or null
|
|
20
|
-
* ``$$`` - return collection or empty array
|
|
21
|
-
* Each element, has ``$`` and ``$$`` methods for selecting descendants
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
**Example**
|
|
25
|
-
```javascript
|
|
26
|
-
let doc = new HtmlSelector(htmlString)
|
|
27
|
-
let body = doc.$('body') // querySelector('body'):element|null
|
|
28
|
-
let divs = body.$$('div') // body.querySelectorAll('div'):array
|
|
29
|
-
console.log(divs)
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
#### FrontEnd usage
|
|
34
|
-
```html
|
|
35
|
-
<script src="/node_modules/als-document/document.js"></script>
|
|
36
|
-
<script>
|
|
37
|
-
let htmlText = `
|
|
38
|
-
<div>
|
|
39
|
-
<span>Another one</span>
|
|
40
|
-
<div>Some text</div>
|
|
41
|
-
</div>
|
|
42
|
-
`
|
|
43
|
-
let doc = new HtmlSelector(htmlText)
|
|
44
|
-
let span = doc.$('div>span')
|
|
45
|
-
</script>
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
#### BackEnd usage
|
|
50
|
-
```javascript
|
|
51
|
-
let {HtmlSelector} = require('als-document')
|
|
52
|
-
let htmlText = `
|
|
53
|
-
<div>
|
|
54
|
-
<span>Another one</span>
|
|
55
|
-
<div>Some text</div>
|
|
56
|
-
</div>
|
|
57
|
-
`
|
|
58
|
-
let doc = new HtmlSelector(htmlText)
|
|
59
|
-
let span = doc.$('div>span')
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
### Extra abilities
|
|
64
|
-
Usualy, browser selector can select by tag name,class, attribute or id.
|
|
65
|
-
HtmlSelector, has extra selecting options, since id,style,class,events and innerHTML - can be selected as attribute.
|
|
66
|
-
|
|
67
|
-
For example you can do those things:
|
|
68
|
-
```javascript
|
|
69
|
-
doc.$('[style*="display"]') //style includes "display"
|
|
70
|
-
doc.$('[onclick*="console.log"]') //event includes "console"
|
|
71
|
-
doc.$('[class*="btn"][class*="danger"]') //class includes "btn" and "danger"
|
|
72
|
-
doc.$('[id^="tab"]') //id starts with "tab-"
|
|
73
|
-
doc.$('[inner$="00"]') //innerText ends with ".00"
|
|
74
|
-
```
|