als-document 0.6.11 → 0.7.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/index.js DELETED
@@ -1,6 +0,0 @@
1
- let HtmlSelector = require('./selector/selector')
2
- let HtmlParser = require('./parser/parser')
3
- let Query = require('./query/query')
4
- let Document = require('./document/document')
5
-
6
- module.exports = {HtmlSelector,HtmlParser,Query,Document}
package/parser/parser.js DELETED
@@ -1,340 +0,0 @@
1
- class HtmlParser {
2
- static parse(html) {
3
- let result = new HtmlParser(html)
4
- return result.root
5
- }
6
- constructor(html='') {
7
- if(this.checkHtml(html)) {
8
- this.indexes = []
9
- this.events = []
10
- this.html = this.htmlString = html
11
- this.htmlString = this.htmlString.replace(/\<\!\-\-([\S\s]*?)\-\-\>/gm,'') // remove all comments
12
- this.removeScripts()
13
- this.removeStyles()
14
- this.removeEventStrings()
15
- this.root = this.parse()
16
- this.clean()
17
- }
18
- }
19
-
20
- clean() {
21
- let toDelete = ['events','indexes','scripts','styles','htmlString','html']
22
- toDelete.forEach(name => {
23
- delete this[name]
24
- });
25
- }
26
-
27
- checkHtml(html,isGood=false) {
28
- if(html == '') console.log('html parameter is empty')
29
- else if(typeof html !== 'string') console.log(`html parameter has to be string. Recieved ${typeof html}`)
30
- else isGood = true
31
- return isGood
32
- }
33
-
34
- removeScripts(scripts=[]) {
35
- let $scripts = this.htmlString.match(/\<script(.*?)\>[\S\s]*?\<\/script\>/gm)
36
- if($scripts !== null) $scripts.forEach((script,i) => {
37
- let inner = script.replace(/^\<script(.*?)\>/,'').replace(/\<\/script\>$/,'')
38
- scripts.push(inner)
39
- this.htmlString = this.htmlString.replace(inner,`{{{{script ${scripts.length-1}`)
40
- });
41
- this.scripts = scripts
42
- }
43
- removeStyles(styles=[]) {
44
- let $styles = this.htmlString.match(/\<style\>[\S\s]*?\<\/style\>/gm)
45
- if($styles !== null) $styles.forEach((style,i) => {
46
- let inner = style.replace(/^\<style\>/,'').replace(/\<\/style\>$/,'')
47
- styles.push(inner)
48
- this.htmlString = this.htmlString.replace(inner,`{{{{style ${styles.length-1}`)
49
- });
50
- this.styles = styles
51
- }
52
-
53
- removeEventStrings() {
54
- // let eventsWithHtml = this.htmlString.match(/on\w*\s*?\=\s*?["|'|`](.*?(<[^>]*>)(\'|\`).*?)["|'|`]/g)
55
- let eventsWithHtml = this.htmlString.match(/\son\w*\s*?\=\s*?\"(.*?)\"/g)
56
- if(eventsWithHtml !== null) {
57
- eventsWithHtml.forEach(event => {
58
- let array = event.split('=')
59
- let value = array.filter((v,i) => i>0 && i!=='').join('=')
60
- value = value.replace(/^\"/,'').replace(/\"$/,'')
61
- this.events.push(value)
62
- let newEvent = event.replace(value,`{{{{event ${this.events.length-1}`)
63
- this.htmlString = this.htmlString.replace(event,newEvent)
64
- })
65
- }
66
- }
67
-
68
- parse(htmlString=this.htmlString) {
69
- // Parse tags
70
- // let elements = htmlString.match(/<[^>]*>/g)
71
- let elements = htmlString.match(/<("[^"]*"|'[^']*'|[^'">])*>/g)
72
- elements.forEach((tag,index) => {
73
- elements[index] = this.parseElement(tag)
74
- htmlString = htmlString.replace(tag,`<tag${index}>`)
75
- });
76
- // Parse inner text
77
- let inners = htmlString.match(/tag[\s\S]*?\</g)
78
- for (let i = inners.length-1; i >=0 ; i--) {
79
- let inner = inners[i];
80
- let tagIndex = inner.match(/(\d*)\>/)[1]
81
- inner = inner.replace(/tag.*\>/,'').slice(0, -1).trim()
82
- if(inner.length > 0) {
83
- elements.splice(parseInt(tagIndex)+1, 0, inner);
84
- }
85
- }
86
- this.elements = elements
87
- let root = this.getPairs()
88
- root.level = 0
89
- root.elements = this.elements
90
- this.innerHTML(root)
91
- return root
92
- }
93
-
94
- lookForPair(element,startIndex,parent,level) {
95
- element.parent = parent
96
- element.children = []
97
- let {tag} = element
98
- let count = 0
99
- let endIndex
100
- for (let index = startIndex+1; index < this.elements.length; index++) {
101
- const el = this.elements[index];
102
- if(el.tag == tag) {
103
- if(el.status == 'close') {
104
- if(count == 0) {
105
- endIndex = index
106
- el.level = level
107
- break
108
- } else count--
109
- } else if(el.status == 'open') count++
110
- }
111
- }
112
- if(!endIndex) endIndex = startIndex+1
113
- element.endIndex = endIndex
114
- let child = this.getPairs(element,startIndex+1,endIndex,level+1)
115
- return child
116
- }
117
-
118
- getPairs(parent={type:'root',children:[]},startIndex = 0,endIndex=this.elements.length,level=0,childIndex=0) {
119
- for(let index = startIndex; index < endIndex; index++) {
120
- if(this.indexes.includes(index)) continue
121
- let element = this.elements[index];
122
- let child
123
- if(typeof element == 'string') child = element
124
- else if(element.type == 'tag') {
125
- if(element.status == 'single') {
126
- child = element
127
- child.parent = parent
128
- } else if(element.status == 'open') {
129
- child = this.lookForPair(element,index,parent,level)
130
- } else element.index = index
131
- }
132
- this.addChild(parent,child,index,level)
133
- childIndex++
134
- }
135
- return parent
136
- }
137
-
138
- addScriptsAndStyles(parent,child,index) {
139
- if(parent.tag == 'script') {
140
- let scriptIndex = child.match(/(?<=\{\{\{\{script\s)(\d*)?/)
141
- if(scriptIndex !== null) {
142
- let i = parseInt(scriptIndex[0])
143
- if(typeof i == 'number') child = this.scripts[i]
144
- }
145
- }
146
- if(parent.tag == 'style') {
147
- let stylesIndex = child.match(/(?<=\{\{\{\{style\s)(\d*)?/)
148
- if(stylesIndex !== null) {
149
- let i = parseInt(stylesIndex[0])
150
- if(typeof i == 'number') child = this.styles[i]
151
- }
152
- }
153
- this.elements[index] = child
154
- return child
155
- }
156
-
157
- addChild(parent,child,index,level) {
158
- if(typeof child == 'string') {
159
- child = this.addScriptsAndStyles(parent,child,index)
160
- child = {type:'text',text:child,parent}
161
- this.elements[index] = child
162
- }
163
- if(child) {
164
- delete child.status
165
- if(child.type !== 'text') {
166
- this.innerHTML(child)
167
- this.outerHTML(child)
168
- this.innerText(child)
169
- }
170
- this.getElements(child)
171
- this.getAncestors(child)
172
- this.getAttribute(child)
173
- this.nextAndPrev(child)
174
- child.index = index
175
- child.level = level
176
- parent.children.push(child)
177
- }
178
- this.indexes.push(index)
179
- }
180
-
181
- getAttribute(element) {
182
- element.getAttribute = function(name) {
183
- let array = Object.keys(this.attribs).filter(key => key == name)
184
- return array.length > 0 ? this.attribs[array[0]] : null
185
- }
186
- }
187
-
188
- getAncestors(element) {
189
- Object.defineProperty(element, 'ancestors', { get() {
190
- let ancestors = []
191
- let parent = this.parent
192
- if(parent) while(parent.parent) {
193
- ancestors.unshift(parent)
194
- parent = parent.parent
195
- }
196
- return ancestors
197
- }});
198
- }
199
-
200
- nextAndPrev(element) {
201
- Object.defineProperty(element, 'childIndex', {
202
- get() {return this.parent.children.map(o => o.index).indexOf(this.index)}
203
- });
204
- Object.defineProperty(element, 'prev', {
205
- get() {
206
- let i = this.childIndex, brothers = this.parent.children
207
- return i == 0 ? null : brothers[i-1]
208
- }
209
- });
210
- Object.defineProperty(element, 'next', {
211
- get() {
212
- let i = this.childIndex, brothers = this.parent.children
213
- return i == brothers.length-1 ? null : brothers[i+1]
214
- }
215
- });
216
- }
217
-
218
- getElements(element,self=this) {
219
- if(element.type !== 'text')
220
- Object.defineProperty(element, 'elements', {
221
- configurable:true,
222
- get() {
223
- let endIndex = isNaN(this.endIndex) ? this.index + 1 : this.endIndex+1
224
- return self.elements.slice(this.index,endIndex)
225
- }
226
- });
227
- Object.defineProperty(element, '$elements', {
228
- configurable:true,
229
- get() {return self.elements}
230
- });
231
- Object.defineProperty(element, 'root', {
232
- configurable:true,
233
- get() {return self.root}
234
- });
235
- Object.defineProperty(element, 'html', {
236
- configurable:true,
237
- get() {return self}
238
- });
239
- }
240
-
241
- outerHTML(element) {
242
- Object.defineProperty(element, 'outerHTML', { get() {
243
- let {elements} = this
244
- return elements[0].text + this.innerHTML + elements[elements.length-1].text
245
- }});
246
- }
247
-
248
- innerText(element) {
249
- Object.defineProperty(element, 'innerText', { get() {
250
- if(element.children)
251
- return element.children.map(child => child.type == 'text' ? child.text : '').join('')
252
- else return ''
253
- }})
254
- }
255
- innerHTML(element) {
256
- element.tab = ' '
257
- element.n = '\n'
258
- Object.defineProperty(element, 'innerHTML', { get() {
259
- // let tab = ' ',result = '',firstLevel,space=''
260
- let {tab,n} = this
261
- let result = '',firstLevel,space=''
262
- let {elements} = this
263
- let endIndex = elements.length
264
- let end = endIndex-1,start=1
265
- if(this.type == 'root') {
266
- end = endIndex
267
- start = 0
268
- }
269
- for(let i = start; i < end; i++) {
270
- let element = elements[i]
271
- let {level,text} = element
272
- if(firstLevel == undefined) firstLevel = level
273
- if(i == end) space = ''
274
- else if(level) space = Array.from(Array(level-firstLevel).keys()).map(n => tab).join('')
275
- result += space + text
276
- if(endIndex > 3) result += n
277
- }
278
- return result
279
- }});
280
- }
281
-
282
- parseElement(tagString) {
283
- let type = 'tag'
284
- if(tagString == '<!DOCTYPE html>') return {tag:'!DOCTYPE html',status:'single',attribs:{},type,text:tagString}
285
- let status = 'close'
286
- let tag = tagString.match(/(?<=\<\/)(\w*\-?)*/)
287
- if(tag == null) {
288
- tag = tagString.match(/(?<=\<)(\w*\-?)*/)
289
- if(tag) {
290
- tag = tag[0]
291
- if(HtmlParser.singleTags.includes(tag)) status='single'
292
- else status = 'open'
293
- }
294
- } else tag = tag[0]
295
- let {classList,attribs,style,id,text} = this.parseAttributes(tagString)
296
- let obj = {tag,status,attribs,type,classList,text,style,id}
297
- return obj
298
- }
299
- static singleTags = ['comment','area','base','br','col','command','embed','hr','img','input','keygen','link','meta','param','source','track','wbr']
300
-
301
- parseAttributes(tagString,classList=[],attribs={},style={},id=null) {
302
- let text = tagString
303
- let attributes = tagString.match(/(?<=\s)(\w*\-?)*(\s*?\=\s*?\"[\s\S]*?\")?/g)
304
- if(attributes) attributes.forEach(attribString => {
305
- let array = attribString.split('=')
306
- let name = array[0]
307
- if(array.length>1 && name !== '') {
308
- let value = array.filter((v,i) => i>0).join('=')
309
- value = value.trim().replace(/^\"/,'').replace(/\"$/m,'')
310
- let eventIndex = value.match(/(?<=\{\{\{\{event\s)(\d*)?/)
311
- if(eventIndex !== null) {
312
- value = this.events[eventIndex[0]]
313
- text = tagString.replace(`{{{{event ${eventIndex[0]}`,value)
314
- }
315
- if(name == 'class') classList = value.split(/\s\s?\s?/)
316
- else if(name == 'style') style = this.parseInlineCss(value)
317
- else if(name == 'id') id = value
318
- attribs[name] = value
319
- } else if(name !== '') attribs[name] = undefined
320
- });
321
- return {classList,attribs,style,id,text}
322
- }
323
-
324
- parseInlineCss(textCss) {
325
- let rules = textCss.split(';')
326
- let styles = {}
327
- rules.forEach(rule => {
328
- let [prop,value] = rule.trim().split(':')
329
- if(rule !== '') {
330
- if(prop.match(/\w*\-\w*(-\w*)?/) !== null) {
331
- let words = prop.split('-')
332
- prop = words.map((w,i) => i==0 ? w : w[0].toUpperCase() + w.slice(1)).join('')
333
- }
334
- styles[prop] = value.trim()
335
- }
336
- });
337
- return styles
338
- }
339
- }
340
- try {module.exports = HtmlParser} catch {}
package/parser/test.js DELETED
@@ -1,233 +0,0 @@
1
- let Test = require('als-test')
2
- let {equal,greater,smaller,$greater, $smaller,mesureTime} = Test
3
- let Parser = require('./parser')
4
- // const htmlparser2 = require("htmlparser2");
5
-
6
- module.exports = new Test('HtmlParser tests',[
7
- {
8
- title:'Parse html for html1',
9
- result:function({html1}){
10
- this.vars.root1 = Parser.parse(html1)
11
- if(this.vars.root1.type == 'root')
12
- return `succesfully parsed with Parser.parse(html)`
13
- else {
14
- this.terminate = true
15
- return 'Something went wrong..'
16
- }
17
- },
18
- },
19
- // {
20
- // title:'Compare to htmlparser2',
21
- // expected:mesureTime(function({html2}){htmlparser2.parseDocument(html2);}),
22
- // result:mesureTime(function({html2}){Parser.parse(html2)}),
23
- // action:greater
24
- // },
25
- {
26
- title:'Parse html for html2',
27
- result:function({html2}){
28
- this.vars.root2 = Parser.parse(html2)
29
- if(this.vars.root2.type == 'root')
30
- return `succesfully parsed with Parser.parse(html)`
31
- else {
32
- this.terminate = true
33
- return 'Something went wrong..'
34
- }
35
- },
36
- },
37
- {
38
- title:'root1 element has 2 children',
39
- expected:2,
40
- result:function({root1}){
41
- return root1.children.length
42
- },
43
- action:equal
44
- },
45
- {
46
- title:'Check if element is right',
47
- expected:'body',
48
- result:function({root1}){
49
- this.vars.body1 = root1.children[1].children[1]
50
- return this.vars.body1.tag
51
- },
52
- action:equal
53
- },
54
- {
55
- title:'Check if element right',
56
- expected:'head',
57
- result:function({root1}){
58
- this.vars.head1 = root1.children[1].children[0]
59
- return this.vars.head1.tag
60
- },
61
- action:equal
62
- },
63
- {
64
- title:'body element has 4 children',
65
- expected:4,
66
- result:function({body1}){
67
- return body1.children.length
68
- },
69
- action:equal
70
- },
71
- {
72
- title:'Read title text',
73
- expected:'Document',
74
- result:function({head1}){
75
- return head1.children[3].children[0].text
76
- },
77
- action:equal
78
- },
79
- {
80
- title:'innerHTML',
81
- expected:'tab3 content',
82
- result:function({body1}){
83
- return body1.children[3].children[6].innerHTML
84
- },
85
- action:equal
86
- },
87
- {
88
- title:'innerHTML',
89
- expected:669,
90
- result:function({body1}){
91
- return body1.children[3].innerHTML.length
92
- },
93
- action:equal
94
- },
95
- {
96
- title:'outerHTML',
97
- expected:'<div class="tab-content p2 transition1">tab3 content</div>'.length,
98
- result:function({body1}){
99
- return body1.children[3].children[6].outerHTML.length
100
- },
101
- action:equal
102
- },
103
- {
104
- title:'ancestors',
105
- expected:3,
106
- result:function({body1}){
107
- return body1.children[3].children[6].ancestors.length
108
- },
109
- action:equal
110
- },
111
- {
112
- title:'elements',
113
- expected:25,
114
- result:function({body1}){
115
- return body1.children[3].elements.length
116
- },
117
- action:equal
118
- },
119
- {
120
- title:'children length',
121
- expected:15,
122
- result:function({root2}){
123
- this.vars.body2 = root2.children[0].children[1]
124
- return this.vars.body2.children.length
125
- },
126
- action:equal
127
- },
128
- {
129
- title:'id',
130
- expected:'tab2',
131
- result:function({body1}){
132
- return body1.children[3].children[1].id
133
- },
134
- action:equal
135
- },
136
- {
137
- title:'classList',
138
- expected:5,
139
- result:function({body2}){
140
- return body2.children[2].children[0].children[4].classList.length
141
- },
142
- action:equal
143
- },
144
- {
145
- title:'style',
146
- expected:'marginTop',
147
- result:function({body2}){
148
- let style = body2.children[2].children[0].children[4].style
149
- return Object.keys(style)[0]
150
- },
151
- action:equal
152
- },
153
- {
154
- title:'style length',
155
- expected:5,
156
- result:function({body2}){
157
- let style = body2.children[4].style
158
- return Object.keys(style).length
159
- },
160
- action:equal
161
- },
162
- {
163
- title:'style with multiple styles',
164
- expected:'605px',
165
- result:function({body2}){
166
- let style = body2.children[4].style
167
- return style.left
168
- },
169
- action:equal
170
- },
171
- {
172
- title:'attributs length',
173
- expected:6,
174
- result:function({body2}){
175
- let attribs = body2.children[2].children[0].children[0].attribs
176
- return Object.keys(attribs).length
177
- },
178
- action:equal
179
- },
180
- {
181
- title:'attribute content',
182
- expected:'_blank',
183
- result:function({body2}){
184
- return body2.children[2].children[0].children[0].attribs.target
185
- },
186
- action:equal
187
- },
188
- {
189
- title:'next',
190
- expected:'a#menuButton',
191
- result:function({body2}){
192
- let {tag,id} = body2.children[2].children[0].children[0].next
193
- return `${tag}#${id}`
194
- },
195
- action:equal
196
- },
197
- {
198
- title:'prev',
199
- expected:'a#tryhome',
200
- result:function({body2}){
201
- let {tag,id} = body2.children[2].children[0].children[1].prev
202
- return `${tag}#${id}`
203
- },
204
- action:equal
205
- },
206
- {
207
- title:'parent',
208
- expected:'div.w3-bar',
209
- result:function({body2}){
210
- let {tag,classList} = body2.children[2].children[0].children[1].parent
211
- return `${tag}.${classList[0]}`
212
- },
213
- action:equal
214
- },
215
- {
216
- title:'Single tags doesnt have children',
217
- expected:undefined,
218
- result:function({root2}){
219
- let head = root2.children[0].children[0]
220
- return head.children[1].children
221
- },
222
- action:equal
223
- },
224
- {
225
- title:'Text tag doesnt have children',
226
- expected:undefined,
227
- result:function({root2}){
228
- let head = root2.children[0].children[0]
229
- return head.children[0].children[0].children
230
- },
231
- action:equal
232
- },
233
- ])