pseudo-dom 0.7.0 → 0.8.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.
Files changed (41) hide show
  1. package/README.md +262 -236
  2. package/browser/pseudo-dom.js +628 -548
  3. package/browser/pseudo-dom.min.js +1 -1
  4. package/dist/classes/PseudoHTMLDocument.d.ts +41 -67
  5. package/dist/classes/PseudoHTMLDocument.js +74 -144
  6. package/dist/classes/PseudoHTMLDocument.min.js +1 -1
  7. package/dist/factories/cssSelectAdapter.d.ts +23 -23
  8. package/dist/factories/cssSelectAdapter.js +87 -87
  9. package/dist/factories/query.d.ts +33 -33
  10. package/dist/factories/query.js +66 -66
  11. package/dist/functions/getElementById.d.ts +13 -0
  12. package/dist/functions/getElementById.js +33 -0
  13. package/dist/functions/getElementById.min.js +1 -0
  14. package/dist/interfaces/PseudoDocument.d.ts +25 -0
  15. package/dist/interfaces/PseudoDocumentFragment.d.ts +10 -3
  16. package/dist/interfaces/PseudoElement.d.ts +702 -702
  17. package/dist/services/DocumentFragmentService.d.ts +21 -13
  18. package/dist/services/DocumentFragmentService.js +42 -24
  19. package/dist/services/DocumentFragmentService.min.js +1 -1
  20. package/dist/services/DocumentService.d.ts +28 -1
  21. package/dist/services/DocumentService.js +64 -15
  22. package/dist/services/DocumentService.min.js +1 -1
  23. package/dist/services/ElementService.d.ts +191 -191
  24. package/dist/services/ElementService.js +426 -426
  25. package/dist/services/HTMLCollectionService.d.ts +54 -54
  26. package/dist/services/HTMLCollectionService.js +92 -92
  27. package/dist/services/NodeService.d.ts +297 -297
  28. package/dist/services/NodeService.js +721 -721
  29. package/package.json +2 -2
  30. package/dist/classes.d.ts +0 -20
  31. package/dist/classes.js +0 -31
  32. package/dist/classes.min.js +0 -1
  33. package/dist/factories/generateNode.d.ts +0 -32
  34. package/dist/factories/generateNode.js +0 -85
  35. package/dist/factories/generateNode.min.js +0 -1
  36. package/dist/factories.d.ts +0 -11
  37. package/dist/factories.js +0 -27
  38. package/dist/factories.min.js +0 -1
  39. package/dist/functions.d.ts +0 -5
  40. package/dist/functions.js +0 -23
  41. package/dist/functions.min.js +0 -1
@@ -1,67 +1,41 @@
1
- /**
2
- * @file Substitute for the DOM HTMLDocument Class.
3
- * @author Joshua Heagle <joshuaheagle@gmail.com>
4
- * @version 1.0.0
5
- */
6
- /**
7
- *
8
- * @type {PseudoHTMLElement}
9
- */
10
- import { HTMLElementService as PseudoHTMLElement } from '../services/HTMLElementService';
11
- import { TextService, CommentService } from '../services/NodeService';
12
- import { DocumentFragmentService } from '../services/DocumentFragmentService';
13
- import { PseudoNode } from '../interfaces/PseudoNode';
14
- /**
15
- * Simulate the behaviour of the HTMLDocument Class when there is no DOM available.
16
- * @author Joshua Heagle <joshuaheagle@gmail.com>
17
- * @class
18
- * @augments PseudoHTMLElement
19
- * @property {PseudoHTMLElement} head - A reference to the Head child element
20
- * @property {PseudoHTMLElement} body - A reference to the Body child element
21
- * @property {function} createElement - Generate a new PseudoHTMLElement (which is not in the document until it is appended)
22
- */
23
- declare class PseudoHTMLDocument extends PseudoHTMLElement {
24
- private head;
25
- private body;
26
- /**
27
- * The root HTML element is acts as the parent to all HTML elements in the document.
28
- * @constructor
29
- */
30
- constructor();
31
- get nodeName(): string;
32
- get nodeType(): number;
33
- get textContent(): string | null;
34
- set textContent(text: string | null);
35
- /**
36
- * Make an element of the given type which belongs to this document but is not added anywhere until it is appended.
37
- * @param {string} tagName - Tag Name is a string representing the type of Dom element this represents
38
- * @returns {PseudoHTMLElement}
39
- */
40
- createElement(tagName?: string): PseudoHTMLElement;
41
- /**
42
- * Make a text node which belongs to this document.
43
- * @param {string} [data=''] The text
44
- * @returns {TextService}
45
- */
46
- createTextNode(data?: string): TextService;
47
- /**
48
- * Make a comment which belongs to this document.
49
- * @param {string} [data=''] The comment
50
- * @returns {CommentService}
51
- */
52
- createComment(data?: string): CommentService;
53
- /**
54
- * Make an empty document fragment which belongs to this document, a container for nodes which can be built up and
55
- * then inserted in one go.
56
- * @returns {DocumentFragmentService}
57
- */
58
- createDocumentFragment(): DocumentFragmentService;
59
- /**
60
- * Make a copy of this document. The copy has no parent or listeners, and a deep copy has copies of everything in the
61
- * document (a shallow one is an empty document).
62
- * @param {boolean} [deep=false] Copy everything in the document as well
63
- * @returns {PseudoNode}
64
- */
65
- cloneNode(deep?: boolean): PseudoNode;
66
- }
67
- export default PseudoHTMLDocument;
1
+ /**
2
+ * @file Substitute for the DOM HTMLDocument Class.
3
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
4
+ * @version 1.0.0
5
+ */
6
+ import { HTMLElementService as PseudoHTMLElement } from '../services/HTMLElementService';
7
+ import { DocumentService } from '../services/DocumentService';
8
+ import { NodeService } from '../services/NodeService';
9
+ /**
10
+ * Simulate the behaviour of the HTMLDocument Class when there is no DOM available. Like the real HTMLDocument, this
11
+ * only adds the html/head/body structure on top of what Document already gives (createElement, createTextNode,
12
+ * createComment, createDocumentFragment, getElementById, textContent always null).
13
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
14
+ * @class
15
+ * @augments DocumentService
16
+ * @property {PseudoHTMLElement} head - A reference to the Head child element
17
+ * @property {PseudoHTMLElement} body - A reference to the Body child element
18
+ */
19
+ declare class PseudoHTMLDocument extends DocumentService {
20
+ head: PseudoHTMLElement | null;
21
+ body: PseudoHTMLElement | null;
22
+ /**
23
+ * The root HTML element is acts as the parent to all HTML elements in the document.
24
+ * @constructor
25
+ */
26
+ constructor();
27
+ /**
28
+ * A copy of this document with none of its html/head/body (cloneNode, from the inherited cloneShallow hook, fills
29
+ * them back in, deep copies own document's, empty otherwise - see cloneNode).
30
+ * @returns {PseudoHTMLDocument}
31
+ */
32
+ protected cloneShallow(): NodeService;
33
+ /**
34
+ * Make a copy of this document. The copy has no parent or listeners, and a deep copy has copies of everything in
35
+ * the document (a shallow one is an empty document).
36
+ * @param {boolean} [deep=false] Copy everything in the document as well
37
+ * @returns {PseudoHTMLDocument}
38
+ */
39
+ cloneNode(deep?: boolean): PseudoHTMLDocument;
40
+ }
41
+ export default PseudoHTMLDocument;
@@ -1,144 +1,74 @@
1
- 'use strict'
2
-
3
- require('core-js/modules/esnext.iterator.constructor.js')
4
- require('core-js/modules/esnext.iterator.find.js')
5
- require('core-js/modules/esnext.iterator.for-each.js')
6
- Object.defineProperty(exports, '__esModule', {
7
- value: true
8
- })
9
- /**
10
- * @file Substitute for the DOM HTMLDocument Class.
11
- * @author Joshua Heagle <joshuaheagle@gmail.com>
12
- * @version 1.0.0
13
- */
14
- /**
15
- *
16
- * @type {PseudoHTMLElement}
17
- */
18
- const HTMLElementService_1 = require('../services/HTMLElementService')
19
- const NodeService_1 = require('../services/NodeService')
20
- const DocumentFragmentService_1 = require('../services/DocumentFragmentService')
21
- /**
22
- * Simulate the behaviour of the HTMLDocument Class when there is no DOM available.
23
- * @author Joshua Heagle <joshuaheagle@gmail.com>
24
- * @class
25
- * @augments PseudoHTMLElement
26
- * @property {PseudoHTMLElement} head - A reference to the Head child element
27
- * @property {PseudoHTMLElement} body - A reference to the Body child element
28
- * @property {function} createElement - Generate a new PseudoHTMLElement (which is not in the document until it is appended)
29
- */
30
- class PseudoHTMLDocument extends HTMLElementService_1.HTMLElementService {
31
- /**
32
- * The root HTML element is acts as the parent to all HTML elements in the document.
33
- * @constructor
34
- */
35
- constructor () {
36
- super()
37
- const html = new HTMLElementService_1.HTMLElementService({
38
- tagName: 'html'
39
- })
40
- this.appendChild(html)
41
- /**
42
- * Create document head element
43
- * @type {PseudoHTMLElement}
44
- */
45
- this.head = new HTMLElementService_1.HTMLElementService({
46
- tagName: 'head'
47
- })
48
- html.appendChild(this.head)
49
- /**
50
- * Create document body element
51
- * @type {PseudoHTMLElement}
52
- */
53
- this.body = new HTMLElementService_1.HTMLElementService({
54
- tagName: 'body'
55
- })
56
- html.appendChild(this.body)
57
- }
58
-
59
- get nodeName () {
60
- return '#document'
61
- }
62
-
63
- get nodeType () {
64
- return NodeService_1.NodeService.DOCUMENT_NODE
65
- }
66
-
67
- // A document has no text of its own, and setting it does nothing
68
- get textContent () {
69
- return null
70
- }
71
-
72
- set textContent (text) {}
73
- /**
74
- * Make an element of the given type which belongs to this document but is not added anywhere until it is appended.
75
- * @param {string} tagName - Tag Name is a string representing the type of Dom element this represents
76
- * @returns {PseudoHTMLElement}
77
- */
78
- createElement (tagName = 'div') {
79
- // Like the DOM, the new element is not added anywhere: it has no parent until it is appended
80
- const element = new HTMLElementService_1.HTMLElementService({
81
- tagName
82
- })
83
- element.ownerDocumentStore = this
84
- return element
85
- }
86
-
87
- /**
88
- * Make a text node which belongs to this document.
89
- * @param {string} [data=''] The text
90
- * @returns {TextService}
91
- */
92
- createTextNode (data = '') {
93
- const text = new NodeService_1.TextService(data)
94
- text.ownerDocumentStore = this
95
- return text
96
- }
97
-
98
- /**
99
- * Make a comment which belongs to this document.
100
- * @param {string} [data=''] The comment
101
- * @returns {CommentService}
102
- */
103
- createComment (data = '') {
104
- const comment = new NodeService_1.CommentService(data)
105
- comment.ownerDocumentStore = this
106
- return comment
107
- }
108
-
109
- /**
110
- * Make an empty document fragment which belongs to this document, a container for nodes which can be built up and
111
- * then inserted in one go.
112
- * @returns {DocumentFragmentService}
113
- */
114
- createDocumentFragment () {
115
- const fragment = new DocumentFragmentService_1.DocumentFragmentService()
116
- fragment.ownerDocumentStore = this
117
- return fragment
118
- }
119
-
120
- /**
121
- * Make a copy of this document. The copy has no parent or listeners, and a deep copy has copies of everything in the
122
- * document (a shallow one is an empty document).
123
- * @param {boolean} [deep=false] Copy everything in the document as well
124
- * @returns {PseudoNode}
125
- */
126
- cloneNode (deep = false) {
127
- const copy = new PseudoHTMLDocument()
128
- // The new document starts out with its own html, head and body, a copy has only what was copied from this one
129
- while (copy.firstChild) {
130
- copy.removeChild(copy.firstChild)
131
- }
132
- copy.head = null
133
- copy.body = null
134
- if (deep) {
135
- Array.from(this.childNodes).forEach(child => copy.appendChild(child.cloneNode(true)))
136
- const html = Array.from(copy.childNodes).find(child => child.tagName === 'html')
137
- const inHtml = tagName => html ? Array.from(html.childNodes).find(child => child.tagName === tagName) || null : null
138
- copy.head = inHtml('head')
139
- copy.body = inHtml('body')
140
- }
141
- return copy
142
- }
143
- }
144
- exports.default = PseudoHTMLDocument
1
+ 'use strict'
2
+
3
+ require('core-js/modules/esnext.iterator.constructor.js')
4
+ require('core-js/modules/esnext.iterator.find.js')
5
+ Object.defineProperty(exports, '__esModule', {
6
+ value: true
7
+ })
8
+ const DocumentService_1 = require('../services/DocumentService')
9
+ /**
10
+ * Simulate the behaviour of the HTMLDocument Class when there is no DOM available. Like the real HTMLDocument, this
11
+ * only adds the html/head/body structure on top of what Document already gives (createElement, createTextNode,
12
+ * createComment, createDocumentFragment, getElementById, textContent always null).
13
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
14
+ * @class
15
+ * @augments DocumentService
16
+ * @property {PseudoHTMLElement} head - A reference to the Head child element
17
+ * @property {PseudoHTMLElement} body - A reference to the Body child element
18
+ */
19
+ class PseudoHTMLDocument extends DocumentService_1.DocumentService {
20
+ /**
21
+ * The root HTML element is acts as the parent to all HTML elements in the document.
22
+ * @constructor
23
+ */
24
+ constructor () {
25
+ super()
26
+ const html = this.createElement('html')
27
+ this.appendChild(html)
28
+ /**
29
+ * Create document head element
30
+ * @type {PseudoHTMLElement}
31
+ */
32
+ this.head = this.createElement('head')
33
+ html.appendChild(this.head)
34
+ /**
35
+ * Create document body element
36
+ * @type {PseudoHTMLElement}
37
+ */
38
+ this.body = this.createElement('body')
39
+ html.appendChild(this.body)
40
+ }
41
+
42
+ /**
43
+ * A copy of this document with none of its html/head/body (cloneNode, from the inherited cloneShallow hook, fills
44
+ * them back in, deep copies own document's, empty otherwise - see cloneNode).
45
+ * @returns {PseudoHTMLDocument}
46
+ */
47
+ cloneShallow () {
48
+ const copy = new this.constructor()
49
+ while (copy.firstChild) {
50
+ copy.removeChild(copy.firstChild)
51
+ }
52
+ copy.head = null
53
+ copy.body = null
54
+ return copy
55
+ }
56
+
57
+ /**
58
+ * Make a copy of this document. The copy has no parent or listeners, and a deep copy has copies of everything in
59
+ * the document (a shallow one is an empty document).
60
+ * @param {boolean} [deep=false] Copy everything in the document as well
61
+ * @returns {PseudoHTMLDocument}
62
+ */
63
+ cloneNode (deep = false) {
64
+ const copy = super.cloneNode(deep)
65
+ if (deep) {
66
+ const html = Array.from(copy.childNodes).find(child => child.tagName === 'html')
67
+ const inHtml = tagName => html ? Array.from(html.childNodes).find(child => child.tagName === tagName) || null : null
68
+ copy.head = inHtml('head')
69
+ copy.body = inHtml('body')
70
+ }
71
+ return copy
72
+ }
73
+ }
74
+ exports.default = PseudoHTMLDocument
@@ -1 +1 @@
1
- "use strict";require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.find.js"),require("core-js/modules/esnext.iterator.for-each.js"),Object.defineProperty(exports,"__esModule",{value:!0});const HTMLElementService_1=require("../services/HTMLElementService"),NodeService_1=require("../services/NodeService"),DocumentFragmentService_1=require("../services/DocumentFragmentService");class PseudoHTMLDocument extends HTMLElementService_1.HTMLElementService{constructor(){super();const e=new HTMLElementService_1.HTMLElementService({tagName:"html"});this.appendChild(e),this.head=new HTMLElementService_1.HTMLElementService({tagName:"head"}),e.appendChild(this.head),this.body=new HTMLElementService_1.HTMLElementService({tagName:"body"}),e.appendChild(this.body)}get nodeName(){return"#document"}get nodeType(){return NodeService_1.NodeService.DOCUMENT_NODE}get textContent(){return null}set textContent(e){}createElement(e="div"){const t=new HTMLElementService_1.HTMLElementService({tagName:e});return t.ownerDocumentStore=this,t}createTextNode(e=""){const t=new NodeService_1.TextService(e);return t.ownerDocumentStore=this,t}createComment(e=""){const t=new NodeService_1.CommentService(e);return t.ownerDocumentStore=this,t}createDocumentFragment(){const e=new DocumentFragmentService_1.DocumentFragmentService;return e.ownerDocumentStore=this,e}cloneNode(e=!1){const t=new PseudoHTMLDocument;for(;t.firstChild;)t.removeChild(t.firstChild);if(t.head=null,t.body=null,e){Array.from(this.childNodes).forEach(e=>t.appendChild(e.cloneNode(!0)));const e=Array.from(t.childNodes).find(e=>"html"===e.tagName),r=t=>e&&Array.from(e.childNodes).find(e=>e.tagName===t)||null;t.head=r("head"),t.body=r("body")}return t}}exports.default=PseudoHTMLDocument;
1
+ "use strict";require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.find.js"),Object.defineProperty(exports,"__esModule",{value:!0});const DocumentService_1=require("../services/DocumentService");class PseudoHTMLDocument extends DocumentService_1.DocumentService{constructor(){super();const e=this.createElement("html");this.appendChild(e),this.head=this.createElement("head"),e.appendChild(this.head),this.body=this.createElement("body"),e.appendChild(this.body)}cloneShallow(){const e=new this.constructor;for(;e.firstChild;)e.removeChild(e.firstChild);return e.head=null,e.body=null,e}cloneNode(e=!1){const t=super.cloneNode(e);if(e){const e=Array.from(t.childNodes).find(e=>"html"===e.tagName),o=t=>e&&Array.from(e.childNodes).find(e=>e.tagName===t)||null;t.head=o("head"),t.body=o("body")}return t}}exports.default=PseudoHTMLDocument;
@@ -1,23 +1,23 @@
1
- /**
2
- * Maps pseudo-dom's own Node / Element API onto the Adapter interface css-select needs to query a tree which is not
3
- * domutils' own (css-select's own Adapter<Node, ElementNode> type). Every method here is one pseudo-dom already has
4
- * under a different name; nothing here reimplements DOM behaviour.
5
- * @memberOf module:factories
6
- * @type {Object}
7
- */
8
- export declare const cssSelectAdapter: {
9
- isTag: (node: any) => boolean;
10
- existsOne: (test: (elem: any) => boolean, elems: Array<any>) => boolean;
11
- getAttributeValue: (elem: any, name: string) => string | undefined;
12
- getChildren: (node: any) => Array<any>;
13
- getName: (elem: any) => string;
14
- getParent: (node: any) => any | null;
15
- getSiblings: (node: any) => Array<any>;
16
- prevElementSibling: (node: any) => any | null;
17
- getText: (node: any) => string;
18
- hasAttrib: (elem: any, name: string) => boolean;
19
- removeSubsets: (nodes: Array<any>) => Array<any>;
20
- findAll: (test: (elem: any) => boolean, nodes: Array<any>) => Array<any>;
21
- findOne: (test: (elem: any) => boolean, nodes: Array<any>) => any | null;
22
- };
23
- export default cssSelectAdapter;
1
+ /**
2
+ * Maps pseudo-dom's own Node / Element API onto the Adapter interface css-select needs to query a tree which is not
3
+ * domutils' own (css-select's own Adapter<Node, ElementNode> type). Every method here is one pseudo-dom already has
4
+ * under a different name; nothing here reimplements DOM behaviour.
5
+ * @memberOf module:factories
6
+ * @type {Object}
7
+ */
8
+ export declare const cssSelectAdapter: {
9
+ isTag: (node: any) => boolean;
10
+ existsOne: (test: (elem: any) => boolean, elems: Array<any>) => boolean;
11
+ getAttributeValue: (elem: any, name: string) => string | undefined;
12
+ getChildren: (node: any) => Array<any>;
13
+ getName: (elem: any) => string;
14
+ getParent: (node: any) => any | null;
15
+ getSiblings: (node: any) => Array<any>;
16
+ prevElementSibling: (node: any) => any | null;
17
+ getText: (node: any) => string;
18
+ hasAttrib: (elem: any, name: string) => boolean;
19
+ removeSubsets: (nodes: Array<any>) => Array<any>;
20
+ findAll: (test: (elem: any) => boolean, nodes: Array<any>) => Array<any>;
21
+ findOne: (test: (elem: any) => boolean, nodes: Array<any>) => any | null;
22
+ };
23
+ export default cssSelectAdapter;
@@ -1,87 +1,87 @@
1
- 'use strict'
2
-
3
- require('core-js/modules/esnext.iterator.constructor.js')
4
- require('core-js/modules/esnext.iterator.filter.js')
5
- require('core-js/modules/esnext.iterator.for-each.js')
6
- require('core-js/modules/esnext.iterator.some.js')
7
- Object.defineProperty(exports, '__esModule', {
8
- value: true
9
- })
10
- exports.cssSelectAdapter = void 0
11
- /**
12
- * @file The css-select Adapter which lets it query pseudo-dom's own tree, instead of the domutils-based tree it
13
- * defaults to.
14
- * @author Joshua Heagle <joshuaheagle@gmail.com>
15
- * @version 1.0.0
16
- */
17
- const NodeService_1 = require('../services/NodeService')
18
- /**
19
- * Walk up from a node (not including it) to find the nearest element, in the given direction.
20
- * @param {*} node The node to start from
21
- * @param {'nextSibling'|'previousSibling'} direction Which sibling reference to follow
22
- * @returns {*|null}
23
- */
24
- const nearestElementSibling = (node, direction) => {
25
- let current = node ? node[direction] : null
26
- while (current && current.nodeType !== NodeService_1.NodeService.ELEMENT_NODE) {
27
- current = current[direction]
28
- }
29
- return current || null
30
- }
31
- /**
32
- * Maps pseudo-dom's own Node / Element API onto the Adapter interface css-select needs to query a tree which is not
33
- * domutils' own (css-select's own Adapter<Node, ElementNode> type). Every method here is one pseudo-dom already has
34
- * under a different name; nothing here reimplements DOM behaviour.
35
- * @memberOf module:factories
36
- * @type {Object}
37
- */
38
- exports.cssSelectAdapter = {
39
- isTag: node => !!node && node.nodeType === NodeService_1.NodeService.ELEMENT_NODE,
40
- existsOne: (test, elems) => elems.some(elem => exports.cssSelectAdapter.isTag(elem) && (test(elem) || exports.cssSelectAdapter.existsOne(test, exports.cssSelectAdapter.getChildren(elem)))),
41
- // pseudo-dom's className / classList are kept in sync with each other, but not with a literal 'class' attribute
42
- // (there is no such special-cased attribute here, unlike a real DOM) - so the 'class' css-select needs for class
43
- // selectors is read from className instead.
44
- getAttributeValue: (elem, name) => {
45
- const value = name === 'class' ? elem.className : elem.getAttribute(name)
46
- return value === null || value === undefined ? undefined : value
47
- },
48
- getChildren: node => Array.from(node.childNodes),
49
- getName: elem => elem.tagName,
50
- getParent: node => node.parentNode,
51
- // Unlike jQuery's siblings(), this is expected to include the node itself
52
- getSiblings: node => node.parentNode ? Array.from(node.parentNode.childNodes) : [node],
53
- prevElementSibling: node => nearestElementSibling(node, 'previousSibling'),
54
- getText: node => node.textContent || '',
55
- hasAttrib: (elem, name) => elem.hasAttribute(name),
56
- removeSubsets: nodes => nodes.filter((node, index) => !nodes.some((other, otherIndex) => otherIndex !== index && typeof other.contains === 'function' && other !== node && other.contains(node))),
57
- findAll: (test, nodes) => {
58
- const found = []
59
- const visit = list => list.forEach(node => {
60
- if (!exports.cssSelectAdapter.isTag(node)) {
61
- return
62
- }
63
- if (test(node)) {
64
- found.push(node)
65
- }
66
- visit(exports.cssSelectAdapter.getChildren(node))
67
- })
68
- visit(nodes)
69
- return found
70
- },
71
- findOne: (test, nodes) => {
72
- for (const node of nodes) {
73
- if (!exports.cssSelectAdapter.isTag(node)) {
74
- continue
75
- }
76
- if (test(node)) {
77
- return node
78
- }
79
- const inChildren = exports.cssSelectAdapter.findOne(test, exports.cssSelectAdapter.getChildren(node))
80
- if (inChildren) {
81
- return inChildren
82
- }
83
- }
84
- return null
85
- }
86
- }
87
- exports.default = exports.cssSelectAdapter
1
+ 'use strict'
2
+
3
+ require('core-js/modules/esnext.iterator.constructor.js')
4
+ require('core-js/modules/esnext.iterator.filter.js')
5
+ require('core-js/modules/esnext.iterator.for-each.js')
6
+ require('core-js/modules/esnext.iterator.some.js')
7
+ Object.defineProperty(exports, '__esModule', {
8
+ value: true
9
+ })
10
+ exports.cssSelectAdapter = void 0
11
+ /**
12
+ * @file The css-select Adapter which lets it query pseudo-dom's own tree, instead of the domutils-based tree it
13
+ * defaults to.
14
+ * @author Joshua Heagle <joshuaheagle@gmail.com>
15
+ * @version 1.0.0
16
+ */
17
+ const NodeService_1 = require('../services/NodeService')
18
+ /**
19
+ * Walk up from a node (not including it) to find the nearest element, in the given direction.
20
+ * @param {*} node The node to start from
21
+ * @param {'nextSibling'|'previousSibling'} direction Which sibling reference to follow
22
+ * @returns {*|null}
23
+ */
24
+ const nearestElementSibling = (node, direction) => {
25
+ let current = node ? node[direction] : null
26
+ while (current && current.nodeType !== NodeService_1.NodeService.ELEMENT_NODE) {
27
+ current = current[direction]
28
+ }
29
+ return current || null
30
+ }
31
+ /**
32
+ * Maps pseudo-dom's own Node / Element API onto the Adapter interface css-select needs to query a tree which is not
33
+ * domutils' own (css-select's own Adapter<Node, ElementNode> type). Every method here is one pseudo-dom already has
34
+ * under a different name; nothing here reimplements DOM behaviour.
35
+ * @memberOf module:factories
36
+ * @type {Object}
37
+ */
38
+ exports.cssSelectAdapter = {
39
+ isTag: node => !!node && node.nodeType === NodeService_1.NodeService.ELEMENT_NODE,
40
+ existsOne: (test, elems) => elems.some(elem => exports.cssSelectAdapter.isTag(elem) && (test(elem) || exports.cssSelectAdapter.existsOne(test, exports.cssSelectAdapter.getChildren(elem)))),
41
+ // pseudo-dom's className / classList are kept in sync with each other, but not with a literal 'class' attribute
42
+ // (there is no such special-cased attribute here, unlike a real DOM) - so the 'class' css-select needs for class
43
+ // selectors is read from className instead.
44
+ getAttributeValue: (elem, name) => {
45
+ const value = name === 'class' ? elem.className : elem.getAttribute(name)
46
+ return value === null || value === undefined ? undefined : value
47
+ },
48
+ getChildren: node => Array.from(node.childNodes),
49
+ getName: elem => elem.tagName,
50
+ getParent: node => node.parentNode,
51
+ // Unlike jQuery's siblings(), this is expected to include the node itself
52
+ getSiblings: node => node.parentNode ? Array.from(node.parentNode.childNodes) : [node],
53
+ prevElementSibling: node => nearestElementSibling(node, 'previousSibling'),
54
+ getText: node => node.textContent || '',
55
+ hasAttrib: (elem, name) => elem.hasAttribute(name),
56
+ removeSubsets: nodes => nodes.filter((node, index) => !nodes.some((other, otherIndex) => otherIndex !== index && typeof other.contains === 'function' && other !== node && other.contains(node))),
57
+ findAll: (test, nodes) => {
58
+ const found = []
59
+ const visit = list => list.forEach(node => {
60
+ if (!exports.cssSelectAdapter.isTag(node)) {
61
+ return
62
+ }
63
+ if (test(node)) {
64
+ found.push(node)
65
+ }
66
+ visit(exports.cssSelectAdapter.getChildren(node))
67
+ })
68
+ visit(nodes)
69
+ return found
70
+ },
71
+ findOne: (test, nodes) => {
72
+ for (const node of nodes) {
73
+ if (!exports.cssSelectAdapter.isTag(node)) {
74
+ continue
75
+ }
76
+ if (test(node)) {
77
+ return node
78
+ }
79
+ const inChildren = exports.cssSelectAdapter.findOne(test, exports.cssSelectAdapter.getChildren(node))
80
+ if (inChildren) {
81
+ return inChildren
82
+ }
83
+ }
84
+ return null
85
+ }
86
+ }
87
+ exports.default = exports.cssSelectAdapter
@@ -1,33 +1,33 @@
1
- /**
2
- * All of the elements below (not including) scope which match the selector, in tree order.
3
- * @memberOf module:factories
4
- * @param {string} selector A CSS selector
5
- * @param {*} scope The node to search below
6
- * @returns {Array<*>}
7
- */
8
- export declare const querySelectorAll: (selector: string, scope: any) => Array<any>;
9
- /**
10
- * The first element below (not including) scope which matches the selector, in tree order, or null when there is none.
11
- * @memberOf module:factories
12
- * @param {string} selector A CSS selector
13
- * @param {*} scope The node to search below
14
- * @returns {*|null}
15
- */
16
- export declare const querySelector: (selector: string, scope: any) => any | null;
17
- /**
18
- * Whether an element itself (not its descendants) matches the selector.
19
- * @memberOf module:factories
20
- * @param {*} element The element to test
21
- * @param {string} selector A CSS selector
22
- * @returns {boolean}
23
- */
24
- export declare const matches: (element: any, selector: string) => boolean;
25
- /**
26
- * The nearest ancestor of an element (starting with the element itself) which matches the selector, or null when
27
- * none of them do.
28
- * @memberOf module:factories
29
- * @param {*} element The element to start from
30
- * @param {string} selector A CSS selector
31
- * @returns {*|null}
32
- */
33
- export declare const closest: (element: any, selector: string) => any | null;
1
+ /**
2
+ * All of the elements below (not including) scope which match the selector, in tree order.
3
+ * @memberOf module:factories
4
+ * @param {string} selector A CSS selector
5
+ * @param {*} scope The node to search below
6
+ * @returns {Array<*>}
7
+ */
8
+ export declare const querySelectorAll: (selector: string, scope: any) => Array<any>;
9
+ /**
10
+ * The first element below (not including) scope which matches the selector, in tree order, or null when there is none.
11
+ * @memberOf module:factories
12
+ * @param {string} selector A CSS selector
13
+ * @param {*} scope The node to search below
14
+ * @returns {*|null}
15
+ */
16
+ export declare const querySelector: (selector: string, scope: any) => any | null;
17
+ /**
18
+ * Whether an element itself (not its descendants) matches the selector.
19
+ * @memberOf module:factories
20
+ * @param {*} element The element to test
21
+ * @param {string} selector A CSS selector
22
+ * @returns {boolean}
23
+ */
24
+ export declare const matches: (element: any, selector: string) => boolean;
25
+ /**
26
+ * The nearest ancestor of an element (starting with the element itself) which matches the selector, or null when
27
+ * none of them do.
28
+ * @memberOf module:factories
29
+ * @param {*} element The element to start from
30
+ * @param {string} selector A CSS selector
31
+ * @returns {*|null}
32
+ */
33
+ export declare const closest: (element: any, selector: string) => any | null;