als-document 0.1.1 → 0.5.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,200 +1,336 @@
1
- # Als-Document
1
+ # Als-document
2
2
 
3
- *If something wrong or not working properly, please write me to: sh.mashkanta@gmail.com*
3
+ Builded from scratch and tested.
4
+ All tested with als-test.
4
5
 
6
+ Als-document is a library which includes 3 instruments:
7
+ * Html parser - for fast html string parsing and building dom tree
8
+ * Html query parser - for destructing css/html query to selectors to array of objects
9
+ * Html selector - for selecting elements with css/html query inside parsed dom tree
5
10
 
6
- Document is a class which gets html as string and return new object with DOM tree.
7
- You can add or remove elements from DOM tree and modify each element.
8
- You can select elements and collections and read and modify them with given instruments.
11
+ ## Selector
9
12
 
10
- **update**
11
- * querySelector for attributes fixed
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.
12
16
 
13
17
 
14
- ## Write and Read files
15
- Document have 2 static methods to read and write files.
16
- The syntax:
18
+ ### Basics
19
+
20
+ **Syntax**
17
21
  ```javascript
18
- Document.writeFile(filePath,obj,encoding = 'utf-8')
19
- Document.readFile(filePath,encoding = 'utf-8')
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)
20
26
  ```
21
27
 
22
- * ``filepath`` - filepath can be string (absolute path to file) or array for joining.
23
- * ``obj`` - obj can be string or object for stringify.
24
- * ``encoding`` - encoding for read or write file
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
32
+
25
33
 
26
- Example:
34
+ **Example**
27
35
  ```javascript
28
- let {Document} = require('als-document')
29
- let html = Document.readFile([__dirname,'index.html'])
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)
30
40
  ```
31
41
 
32
42
 
33
- ## Creating new object
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>
52
+ `
53
+ let doc = new HtmlSelector(htmlText)
54
+ let span = doc.$('div>span')
55
+ </script>
56
+ ```
34
57
 
35
- Document constructor get single string parameter - the outerHTML for converting to virtual DOM tree.
36
58
 
59
+ #### BackEnd usage
37
60
  ```javascript
38
- let document = new Document(html) // html has to be string
39
- document.domTree // includes virtual DOM tree as array of elements
61
+ let {HtmlSelector} = require('als-document')
62
+ let htmlText = `
63
+ <div>
64
+ <span>Another one</span>
65
+ <div>Some text</div>
66
+ </div>
67
+ `
68
+ let doc = new HtmlSelector(htmlText)
69
+ let span = doc.$('div>span')
40
70
  ```
41
71
 
42
72
 
43
- ## QuerySelector for single element
44
- Then document object has created, you can select elements or collections.
45
- For selecting single element, use ``$(selector)`` and for selecting collections ``$$(selector)``.
46
-
47
- **Selecting element**
73
+ ### Extra abilities
74
+ 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.
48
76
 
77
+ For example you can do those things:
49
78
  ```javascript
50
- document.$('div') // select first div in document
51
- document.$('div.some') // select first div element with some class
79
+ doc.$('[style*="display"]') //style includes "display"
80
+ doc.$('[onclick*="console.log"]') //event includes "console"
81
+ doc.$('[class*="btn"][class*="danger"]') //class includes "btn" and "danger"
82
+ doc.$('[id^="tab"]') //id starts with "tab-"
83
+ doc.$('[inner$="00"]') //innerText ends with ".00"
52
84
  ```
85
+ ## HtmlParser
53
86
 
54
- At this time, selector supports this:
55
- * Selects all elements - ``*``
56
- * element - ``div``
57
- * class - ``.some-class``
58
- * id - ``#some-id``
59
- * parent - ``div > p``
60
- * next - ``div + p``
61
- * previous - ``p ~ ul``
62
- * attribute - ``[some-attribute="some value"]``
63
- * ``[prop]``
64
- * ``[prop~=value]``
65
- * ``[prop|=value]``
66
- * ``[prop^="value"]``
67
- * ``[prop$="value"]``
68
- * ``[prop*="value"]``
87
+ HtmlParser is a class which build dom tree from html string.
69
88
 
89
+ * HtmlParser removes all html comments and they not included in dom tree.
90
+ * 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.
70
91
 
71
- The folowing, **won't work**: ``div p``.
72
92
 
93
+ ### Syntax
73
94
 
74
- Each returned element, has the folowing:
75
95
  ```javascript
76
- {
77
- parent, // parent element
78
- prev, // previous element (null if no exists)
79
- next, // next element (null if no exists)
80
- innerText, // innner text of element and it's childNodes separated by |
81
- children, // array of childNodes(elements and text nodes) - includes text element too
82
- tagName, // tag name of element
83
- id, // id of element if exists (not included in attributes)
84
- attributes, // object of attributes (id not included)
85
- classList, // array of classes and add and remove methods
86
- $(selector),
87
- $$(selector),
88
- json(), // remove all methods and circular objects from object
89
- remove(), // remove this element
90
- add(element/outerHtml,place),
91
- add0(element/outerHtml),
92
- add1(element/outerHtml),
93
- add2(element/outerHtml),
94
- add3(element/outerHtml),
95
- }
96
+ let parsed = new HtmlParser(htmlString:string):instanceof HtmlParser
97
+ parsed.root : circular object
98
+
99
+ // static method
100
+ HtmlParser.parse(html):object // parsed.root
96
101
  ```
97
102
 
98
- Text node has the folowing:
103
+ Each element, except root and text elements has:
104
+ * attribs - element's attributes
105
+ * parent - parent element
106
+ * next - next element or null
107
+ * prev - previous element or null
108
+ * children - array of children include text nodes
109
+ * type - tag or text or root
110
+ * classList - array with classes
111
+ * index - start index of element inside elements list
112
+ * id - element's id or null
113
+ * endIndex - end index of element inside elements list
114
+ * level - level in dom tree
115
+ * 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
120
+ * getAttribute(name) - return value of attribute or null
121
+ * style:[] - array of styles with camelCase property name
122
+
123
+ Example for parsed.document
124
+
99
125
  ```javascript
100
126
  {
101
- text,
102
- prev,
103
- next
127
+ type:'root',
128
+ children:[
129
+ {
130
+ attribs: {},
131
+ index: 0,
132
+ prev:null,
133
+ next:{...}
134
+ tag: "!DOCTYPE html",
135
+ type: "tag",
136
+ ...
137
+ },
138
+ {
139
+ attribs: {lang:'en'},
140
+ prev:{...},
141
+ next:null,
142
+ classList:[],
143
+ children:[
144
+ {
145
+ attribs: {},
146
+ children:[...]
147
+ prev:null,
148
+ next:{...}
149
+ classList:[],
150
+ index: 2,
151
+ tag: "head",
152
+ parent:{tag:'html',...} // reference to parent
153
+ type: "tag",
154
+ ...
155
+ },
156
+ {
157
+ attribs: {},
158
+ children:[...]
159
+ index: 10,
160
+ prev:{...},
161
+ next:null,
162
+ classList:[],
163
+ tag: "body",
164
+ parent:{tag:'html',...} // reference to parent
165
+ type: "tag",
166
+ ...
167
+ },
168
+ ]
169
+ index: 1,
170
+ tag: "html",
171
+ parent:{type:'root',...} // reference to parent
172
+ type: "tag",
173
+ ...
174
+ }
175
+ ]
104
176
  }
105
177
  ```
106
178
 
107
- Comment node:
108
- ```javascript
109
- tagName:comment,
110
- comment // comment it self
111
- ```
112
179
 
113
- You can add or remove classes with classList methods.
114
- Example:
115
- ```javascript
116
- let element = document.$('div')
117
- element.classList.remove('some')
118
- element.classList.add('another')
119
- element.classList.add('onemore')
120
- ```
180
+ ### Frontend example
121
181
 
122
- Also you can change element's id:
123
- ```javascript
124
- let element = document.$('div')
125
- element.id = 'new-id'
126
- ```
182
+ ```html
183
+ <script src="/node_modules/als-document/parser/parser.js"></script>
184
+ <script>
185
+ let result = new HtmlParser(htmlString)
186
+ console.log(result.root)
127
187
 
128
- ## Element methods
188
+ // Or with static method
189
+ console.log(HtmlParser.parse(html))
129
190
 
130
- ```javascript
131
- json() // remove all methods and circular objects from object
132
- remove() // remove this element
133
- add(element/outerHtml,place) // adding AdjacentHTML or AdjacentElement to place(0-3)
134
- add0(element/outerHtml) // adding AdjacentHTML or AdjacentElement beforebegin
135
- add1(element/outerHtml) // adding AdjacentHTML or AdjacentElement afterbegin
136
- add2(element/outerHtml) // adding AdjacentHTML or AdjacentElement beforeend
137
- add3(element/outerHtml) // adding AdjacentHTML or AdjacentElement afterend
191
+ </script>
138
192
  ```
139
193
 
140
- Example:
194
+ ### Backend example
195
+
141
196
  ```javascript
142
- let document = new Document(html)
143
- let a = document.$('a')
144
- let div = document.$('div')
145
- div.add2('<div id="test">Hello world</div>')
146
- div.add3(a)
147
- a.remove()
197
+ const {HtmlParser} = require('als-htmlparser')
198
+ let result = new HtmlParser(htmlString)
199
+ console.log(result.root)
200
+
201
+ // Or with static method
202
+ console.log(HtmlParser.parse(html))
148
203
  ```
149
204
 
150
205
 
151
- Create new element with ``Document.newElement(outerHtml)``
206
+ ## Query
207
+ Query is a class for parsing selectors inside html query. Query not supporting pseudo selectors so far.
208
+ You can use Query on frontend and on backend.
152
209
 
153
- ```javascript
154
- Document.newElement('<div id="test">Hello world</div>')
210
+ Query can be used on fronten and on backend.
211
+
212
+ Frontend:
213
+ ```html
214
+ <script src="/node_modules/als-document/query/query.js"></script>
155
215
  ```
156
216
 
217
+ Backend:
218
+ ```javascript
219
+ let {Query} = require('als-document')
220
+ ```
157
221
 
158
- ## QuerySelector for Collection ``$$()``
159
- To select few elements, use ``$$(selector)`` method.
222
+ ### Syntax
160
223
 
161
224
  ```javascript
162
- document.$$('div') // return collection of all div elements
225
+ let queryObj = new Query(qeury:string): instanceof Query
226
+ let selectors = queryObj.selectors:string
227
+ // or
228
+ let selectors = Query.get(q1:string):string
163
229
  ```
164
230
 
165
- The collection is array which has the elements and two methods: ``each`` and ``parse``.
231
+ ``query`` - html/css query
166
232
 
167
- ``each`` method gets callback function with 3 parameters: element it self, index of the element in collection and collection itself.
168
233
 
169
- Here example:
234
+ ### Example
235
+
170
236
  ```javascript
171
- let array = []
172
- document.$$('div').each((element,index,collection) => {
173
- if(element.innerText.includes('some text'))
174
- array.push(element)
175
- })
237
+ let q1 = 'html>body>div.tabs~.some[type $= "radio and some"]>p+div>.some-id .tab-content~input[disabled] div.some'
238
+ let result = new Query(q1).selectors
239
+ let result1 = Query.get(q1)
240
+ // result and result1 has to be same
241
+ console.log(result)
176
242
  ```
177
243
 
178
- ``parse`` method, gets two parameters: ``part`` and ``fn`` and return array with results.
179
- * ``part`` is a part of element. It can be innerText, id, tagName or any property inside attributes.
180
- * ``fn`` is a filter function which gets content of part. If return true, content will be included.
181
-
182
- Example:
244
+ Result:
183
245
  ```javascript
184
- new Document(htmlText).$$('div')
185
- .parse('innerText',
186
- content=> (content.length > 0) ? true : false)
246
+ [
247
+ {
248
+ "query": "div.some",
249
+ "tag": "div",
250
+ "classList": [
251
+ "some"
252
+ ],
253
+ "ancestors": [
254
+ {
255
+ "query": ".some-id",
256
+ "classList": [
257
+ "some-id"
258
+ ],
259
+ "parents": [
260
+ {
261
+ "query": "div",
262
+ "tag": "div"
263
+ }
264
+ ],
265
+ "prev": {
266
+ "query": "p",
267
+ "tag": "p",
268
+ "parents": [
269
+ {
270
+ "query": ".some[0]",
271
+ "classList": [
272
+ "some"
273
+ ],
274
+ "attribs": [
275
+ {
276
+ check:(f),
277
+ "query": "[type$=\"radio and some\"]",
278
+ "name": "type",
279
+ "value": "radio and some",
280
+ "sign": "$="
281
+ }
282
+ ]
283
+ }
284
+ ],
285
+ "prevAny": {
286
+ "query": "div.tabs",
287
+ "tag": "div",
288
+ "classList": [
289
+ "tabs"
290
+ ],
291
+ "parents": [
292
+ {
293
+ "query": "html",
294
+ "tag": "html"
295
+ },
296
+ {
297
+ "query": "body",
298
+ "tag": "body"
299
+ }
300
+ ]
301
+ },
302
+ "group": "html>body>div.tabs~.some[0]>p"
303
+ },
304
+ "group": "html>body>div.tabs~.some[0]>p+div>.some-id"
305
+ },
306
+ {
307
+ "query": "input[1]",
308
+ "tag": "input",
309
+ "attribs": [
310
+ {
311
+ "query": "[disabled]",
312
+ "name": "disabled"
313
+ }
314
+ ],
315
+ "prevAny": {
316
+ "query": ".tab-content",
317
+ "classList": [
318
+ "tab-content"
319
+ ]
320
+ },
321
+ "group": ".tab-content~input[1]"
322
+ }
323
+ ],
324
+ "group": "html>body>div.tabs~.some[type $= \"radio and some\"]>p+div>.some-id .tab-content~input[disabled] div.some"
325
+ }
326
+ ]
187
327
  ```
188
328
 
189
- ## Building html
329
+ ### Attribs and check function
330
+ if attribute has value, attrib object will contain check function with one parameter for value to check.
190
331
 
191
- For building html again, use ``build`` method.
192
- Example:
193
332
  ```javascript
194
- let element = document.$('div')
195
- element.classList.add('another')
196
- element.classList.remove('some')
197
- element.id = 'new-id'
198
- document.build() // return new html text
199
- document.build([__dirname,'new-index.html']) // will create a file with new html text
333
+ let s = Query.get('[test^="some"]')[0]
334
+ console.log(s.attribs[0].check('some value test')) // true
200
335
  ```
336
+
@@ -0,0 +1,74 @@
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
+ ```
@@ -0,0 +1,125 @@
1
+ class HtmlSelector {
2
+ constructor(html) {
3
+ if(typeof html == 'string') {
4
+ html = new HtmlParser(html)
5
+ this.html = html
6
+ this.elements = html.elements
7
+ this.makeSelectable()
8
+ } else console.log('Parameter is not string')
9
+ }
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
+ })
18
+ }
19
+
20
+ $(query,start=0,end=this.elements.length) {
21
+ return this.$$(query,start,end,true)
22
+ }
23
+
24
+ $$(query,start=0,end=this.elements.length,single=false) {
25
+ let result = []
26
+ this.selectors = new Query(query).selectors
27
+ this.query = query
28
+ this.selectors.forEach(selector => {
29
+ for(let i=start; i<end; i++) {
30
+ let el = this.elements[i]
31
+ if(this.checkElement(el,selector) && !result.includes(el)) result.push(el)
32
+ if(single && result.length == 1) break
33
+ }
34
+ });
35
+ if(single && result.length == 1) return result[0]
36
+ else if(single && result.length == 0) return null
37
+ else return result
38
+ }
39
+
40
+ checkElement(el,selector) {
41
+ if(selector == undefined) return true
42
+ if(el == null) return false
43
+ let {tag,classList,attribs,id,prev,ancestors,parents,prevAny} = selector
44
+
45
+ if(el.status == 'close' || el.type == 'text') return false
46
+ if(id !== undefined && el.id == undefined) return false
47
+ if(id && id !== el.id) return false
48
+ if(tag && el.tag == undefined) return false
49
+ else if(tag && tag !== el.tag) return false
50
+ if(classList !== undefined && el.classList == undefined) return false
51
+ else if(classList !== undefined && Array.isArray(el.classList)) {
52
+ if(classList.every(e => el.classList.includes(e)) == false) return false
53
+ }
54
+ if(this.checkAttribs(attribs,el) == false) return false
55
+ if(this.checkElement(el.prev,prev) == false) return false
56
+ if(this.checkAncestors(el.ancestors,ancestors) == false) return false
57
+ if(this.checkParents(el.ancestors,parents) == false) return false
58
+ if(el.parent) {
59
+ if(this.prevAny(el.parent.children,el.childIndex,prevAny) == false) return false
60
+ }
61
+ return true
62
+ }
63
+
64
+ prevAny(children=[],index,prevAny) {
65
+ let size = children.length
66
+ if((size == 0 || index == 0) && prevAny) return false
67
+ for(let i=index; i>=0; i--) {
68
+ if(this.checkElement(children[i],prevAny)) return true
69
+ }
70
+ return false
71
+ }
72
+
73
+ checkAncestors(ancestors=[],selectorAncestors=[]) {
74
+ let count = 0
75
+ if(selectorAncestors.length == 0) return true
76
+ let endIndex = ancestors.length-1
77
+ let selectorIndex = selectorAncestors.length-1
78
+ while(selectorIndex>=0) {
79
+ for(let i=endIndex; i>=0; i--) {
80
+ endIndex=i-1
81
+ if(this.checkElement(ancestors[i],selectorAncestors[selectorIndex]) == true) {
82
+ count++
83
+ break
84
+ }
85
+ }
86
+ selectorIndex--
87
+ }
88
+ if(count == selectorAncestors.length) return true
89
+ else return false
90
+ }
91
+
92
+ checkParents(ancestors=[],selectorParents=[]) {
93
+ if(selectorParents.length == 0) return true
94
+ if(ancestors.length < selectorParents.length) return false
95
+ let index = ancestors.length-1
96
+ for(let i=selectorParents.length-1; i>=0; i--) {
97
+ if(this.checkElement(ancestors[index],selectorParents[i]) == false) return false
98
+ index--
99
+ }
100
+ return true
101
+ }
102
+
103
+ checkAttribs(attribs=[],el) {
104
+ let elAttribs = el.attribs
105
+ let names = Object.keys(elAttribs)
106
+ let passedTests = 0
107
+ if(attribs) for(let i=0; i<attribs.length; i++) {
108
+ let {name,value,check} = attribs[i]
109
+ if(name == 'inner' && value !== undefined && check && el.innerText) {
110
+ if(check(el.innerText)) passedTests++
111
+ }
112
+
113
+ if(!names.includes(name)) continue
114
+ else if(value == undefined) passedTests++
115
+ else if(value && elAttribs[name]) {
116
+ if(check(elAttribs[name]) == false) continue
117
+ else passedTests++
118
+ }
119
+ }
120
+ if(passedTests == attribs.length) return true
121
+ else return false
122
+ }
123
+ }
124
+
125
+ try {module.exports = HtmlSelector} catch{}