als-document 0.5.1 → 0.6.1

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