pseudo-dom 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +961 -4
- package/browser/pseudo-dom.js +701 -405
- package/browser/pseudo-dom.min.js +1 -1
- package/dist/interfaces/PseudoElement.d.ts +28 -17
- package/dist/interfaces/PseudoHTMLCollection.d.ts +22 -1
- package/dist/services/ElementService.d.ts +64 -1
- package/dist/services/ElementService.js +123 -0
- package/dist/services/ElementService.min.js +1 -1
- package/dist/services/HTMLCollectionService.d.ts +46 -0
- package/dist/services/HTMLCollectionService.js +68 -1
- package/dist/services/HTMLCollectionService.min.js +1 -1
- package/dist/services/NodeService.d.ts +49 -1
- package/dist/services/NodeService.js +103 -7
- package/dist/services/NodeService.min.js +1 -1
- package/package.json +1 -1
|
@@ -42,7 +42,7 @@ class NodeService extends EventTargetService_1.default {
|
|
|
42
42
|
this.ownerDocumentStore = null
|
|
43
43
|
this.nodeId = NodeService.nextNodeId++
|
|
44
44
|
this.nodeNameValue = ''
|
|
45
|
-
this.
|
|
45
|
+
this.childList = (0, generateNodeList_1.default)()
|
|
46
46
|
this.parent = null
|
|
47
47
|
this.listLinker = null
|
|
48
48
|
}
|
|
@@ -52,11 +52,11 @@ class NodeService extends EventTargetService_1.default {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
get childNodes () {
|
|
55
|
-
return this.
|
|
55
|
+
return this.childList
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
get firstChild () {
|
|
59
|
-
return this.
|
|
59
|
+
return this.childList.first ? this.childList.first.data : null
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
get isConnected () {
|
|
@@ -65,7 +65,7 @@ class NodeService extends EventTargetService_1.default {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
get lastChild () {
|
|
68
|
-
return this.
|
|
68
|
+
return this.childList.last ? this.childList.last.data : null
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
get nextSibling () {
|
|
@@ -178,6 +178,102 @@ class NodeService extends EventTargetService_1.default {
|
|
|
178
178
|
return this.nodeName === other.nodeName && this.nodeValue === other.nodeValue
|
|
179
179
|
}
|
|
180
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Add nodes (strings become text nodes) as the last children of this node, in the order given.
|
|
183
|
+
* @param {...(PseudoNode|string)} nodes The nodes (or text) to add
|
|
184
|
+
* @throws {Error} When this kind of node cannot have children
|
|
185
|
+
*/
|
|
186
|
+
append (...nodes) {
|
|
187
|
+
nodes.forEach(node => this.appendChild(this.toChildNode(node)))
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Add nodes (strings become text nodes) as the first children of this node, in the order given.
|
|
192
|
+
* @param {...(PseudoNode|string)} nodes The nodes (or text) to add
|
|
193
|
+
* @throws {Error} When this kind of node cannot have children
|
|
194
|
+
*/
|
|
195
|
+
prepend (...nodes) {
|
|
196
|
+
const reference = this.firstChild
|
|
197
|
+
nodes.forEach(node => this.insertBefore(this.toChildNode(node), reference))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Remove every child of this node and put the given nodes (strings become text nodes) in their place, in order.
|
|
202
|
+
* @param {...(PseudoNode|string)} nodes The nodes (or text) to add
|
|
203
|
+
* @throws {Error} When this kind of node cannot have children
|
|
204
|
+
*/
|
|
205
|
+
replaceChildren (...nodes) {
|
|
206
|
+
while (this.firstChild) {
|
|
207
|
+
this.removeChild(this.firstChild)
|
|
208
|
+
}
|
|
209
|
+
this.append(...nodes)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Add nodes (strings become text nodes) as this node's previous siblings, in order. Does nothing when this node has
|
|
214
|
+
* no parent.
|
|
215
|
+
* @param {...(PseudoNode|string)} nodes The nodes (or text) to add
|
|
216
|
+
*/
|
|
217
|
+
before (...nodes) {
|
|
218
|
+
const parent = this.parentNode
|
|
219
|
+
if (!parent) {
|
|
220
|
+
return
|
|
221
|
+
}
|
|
222
|
+
nodes.forEach(node => parent.insertBefore(this.toChildNode(node), this))
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Add nodes (strings become text nodes) as this node's next siblings, in order. Does nothing when this node has no
|
|
227
|
+
* parent.
|
|
228
|
+
* @param {...(PseudoNode|string)} nodes The nodes (or text) to add
|
|
229
|
+
*/
|
|
230
|
+
after (...nodes) {
|
|
231
|
+
const parent = this.parentNode
|
|
232
|
+
if (!parent) {
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
const reference = this.nextSibling
|
|
236
|
+
nodes.forEach(node => parent.insertBefore(this.toChildNode(node), reference))
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Put the given nodes (strings become text nodes) where this node is, in order, then remove this node. Does nothing
|
|
241
|
+
* when this node has no parent.
|
|
242
|
+
* @param {...(PseudoNode|string)} nodes The nodes (or text) to put in this node's place
|
|
243
|
+
*/
|
|
244
|
+
replaceWith (...nodes) {
|
|
245
|
+
const parent = this.parentNode
|
|
246
|
+
if (!parent) {
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
nodes.forEach(node => parent.insertBefore(this.toChildNode(node), this))
|
|
250
|
+
parent.removeChild(this)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Remove this node from its parent. Does nothing when it has no parent.
|
|
255
|
+
*/
|
|
256
|
+
remove () {
|
|
257
|
+
if (this.parentNode) {
|
|
258
|
+
this.parentNode.removeChild(this)
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Turn a value given to append / prepend / before / after / replaceWith / replaceChildren into a node: a string
|
|
264
|
+
* becomes a text node belonging to this node's document, anything else is returned as it is.
|
|
265
|
+
* @param {PseudoNode|string} value The value to add
|
|
266
|
+
* @returns {PseudoNode}
|
|
267
|
+
*/
|
|
268
|
+
toChildNode (value) {
|
|
269
|
+
if (typeof value !== 'string') {
|
|
270
|
+
return value
|
|
271
|
+
}
|
|
272
|
+
const text = new TextService(value)
|
|
273
|
+
text.ownerDocumentStore = this.ownerDocument
|
|
274
|
+
return text
|
|
275
|
+
}
|
|
276
|
+
|
|
181
277
|
/**
|
|
182
278
|
* Called each time a node has been inserted as a child of this node, so that nodes which need to react to children
|
|
183
279
|
* (for example elements applying default events) can do so.
|
|
@@ -266,7 +362,7 @@ class NodeService extends EventTargetService_1.default {
|
|
|
266
362
|
}
|
|
267
363
|
|
|
268
364
|
hasChildNodes () {
|
|
269
|
-
return this.
|
|
365
|
+
return this.childList.length > 0
|
|
270
366
|
}
|
|
271
367
|
|
|
272
368
|
/**
|
|
@@ -305,7 +401,7 @@ class NodeService extends EventTargetService_1.default {
|
|
|
305
401
|
const linker = new TreeLinker_1.TreeLinker({
|
|
306
402
|
data: newNode
|
|
307
403
|
})
|
|
308
|
-
this.
|
|
404
|
+
this.childList.insertBefore(referenceNode ? referenceNode.listLinker : null, linker)
|
|
309
405
|
const inserted = newNode
|
|
310
406
|
inserted.parent = this
|
|
311
407
|
inserted.listLinker = linker
|
|
@@ -383,7 +479,7 @@ class NodeService extends EventTargetService_1.default {
|
|
|
383
479
|
throw new Error('The node to be removed is not a child of this node.')
|
|
384
480
|
}
|
|
385
481
|
const removed = childElement
|
|
386
|
-
this.
|
|
482
|
+
this.childList.remove(removed.listLinker)
|
|
387
483
|
removed.parent = null
|
|
388
484
|
removed.listLinker = null
|
|
389
485
|
return childElement
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.every.js"),require("core-js/modules/esnext.iterator.for-each.js");var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.CommentService=exports.TextService=exports.NodeService=void 0;const generateNodeList_1=__importDefault(require("../factories/generateNodeList")),TreeLinker_1=require("collect-your-stuff/dist/collections/linked-tree-list/TreeLinker"),EventTargetService_1=__importDefault(require("./EventTargetService"));class NodeService extends EventTargetService_1.default{constructor(){super(),this.nodeValueStore=null,this.ownerDocumentStore=null,this.nodeId=NodeService.nextNodeId++,this.nodeNameValue="",this.
|
|
1
|
+
"use strict";require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.every.js"),require("core-js/modules/esnext.iterator.for-each.js");var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.CommentService=exports.TextService=exports.NodeService=void 0;const generateNodeList_1=__importDefault(require("../factories/generateNodeList")),TreeLinker_1=require("collect-your-stuff/dist/collections/linked-tree-list/TreeLinker"),EventTargetService_1=__importDefault(require("./EventTargetService"));class NodeService extends EventTargetService_1.default{constructor(){super(),this.nodeValueStore=null,this.ownerDocumentStore=null,this.nodeId=NodeService.nextNodeId++,this.nodeNameValue="",this.childList=(0,generateNodeList_1.default)(),this.parent=null,this.listLinker=null}get baseURI(){return window.location||"/"}get childNodes(){return this.childList}get firstChild(){return this.childList.first?this.childList.first.data:null}get isConnected(){return this.getRootNode().nodeType===NodeService.DOCUMENT_NODE}get lastChild(){return this.childList.last?this.childList.last.data:null}get nextSibling(){return this.listLinker&&this.listLinker.next?this.listLinker.next.data:null}get nodeName(){return this.nodeNameValue||""}get nodeType(){return NodeService.DEFAULT_NODE}get nodeValue(){return this.nodeValueStore}set nodeValue(e){this.nodeValueStore=e}get ownerDocument(){if(this.nodeType===NodeService.DOCUMENT_NODE)return null;const e=this.getRootNode();return e.nodeType===NodeService.DOCUMENT_NODE?e:this.ownerDocumentStore}get parentNode(){return this.parent}get parentElement(){return this.parent&&this.parent.nodeType===NodeService.ELEMENT_NODE?this.parent:null}get previousSibling(){return this.listLinker&&this.listLinker.prev?this.listLinker.prev.data:null}get textContent(){if(this.nodeType===NodeService.DOCUMENT_NODE||this.nodeType===NodeService.DOCUMENT_TYPE_NODE)return null;let e="";return Array.from(this.childNodes).forEach(t=>{t.nodeType===NodeService.TEXT_NODE?e+=t.nodeValue:t.nodeType!==NodeService.COMMENT_NODE&&(e+=t.textContent)}),e}set textContent(e){if(this.nodeType!==NodeService.DOCUMENT_NODE&&this.nodeType!==NodeService.DOCUMENT_TYPE_NODE){for(;this.firstChild;)this.removeChild(this.firstChild);if(null!=e&&""!==String(e)){const t=new TextService(String(e));t.ownerDocumentStore=this.ownerDocument,this.appendChild(t)}}}appendChild(e){return this.insertBefore(e,null)}get acceptsChildren(){return!0}cloneShallow(){const e=new this.constructor;return e.nodeValue=this.nodeValue,e.ownerDocumentStore=this.ownerDocumentStore,e}equalsShallow(e){return this.nodeName===e.nodeName&&this.nodeValue===e.nodeValue}append(...e){e.forEach(e=>this.appendChild(this.toChildNode(e)))}prepend(...e){const t=this.firstChild;e.forEach(e=>this.insertBefore(this.toChildNode(e),t))}replaceChildren(...e){for(;this.firstChild;)this.removeChild(this.firstChild);this.append(...e)}before(...e){const t=this.parentNode;t&&e.forEach(e=>t.insertBefore(this.toChildNode(e),this))}after(...e){const t=this.parentNode;if(!t)return;const r=this.nextSibling;e.forEach(e=>t.insertBefore(this.toChildNode(e),r))}replaceWith(...e){const t=this.parentNode;t&&(e.forEach(e=>t.insertBefore(this.toChildNode(e),this)),t.removeChild(this))}remove(){this.parentNode&&this.parentNode.removeChild(this)}toChildNode(e){if("string"!=typeof e)return e;const t=new TextService(e);return t.ownerDocumentStore=this.ownerDocument,t}childInserted(e){}cloneNode(e=!1){const t=this.cloneShallow();return e&&Array.from(this.childNodes).forEach(e=>t.appendChild(e.cloneNode(!0))),t}compareDocumentPosition(e){if(e===this)return 0;const t=e=>{const t=[];for(let r=e;r;r=r.parentNode)t.unshift(r);return t},r=t(this),i=t(e);if(r[0]!==i[0]){const t=e.nodeId<this.nodeId?NodeService.DOCUMENT_POSITION_PRECEDING:NodeService.DOCUMENT_POSITION_FOLLOWING;return NodeService.DOCUMENT_POSITION_DISCONNECTED|NodeService.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC|t}if(i.length<r.length&&i.every((e,t)=>r[t]===e))return NodeService.DOCUMENT_POSITION_CONTAINS|NodeService.DOCUMENT_POSITION_PRECEDING;if(r.length<i.length&&r.every((e,t)=>i[t]===e))return NodeService.DOCUMENT_POSITION_CONTAINED_BY|NodeService.DOCUMENT_POSITION_FOLLOWING;let n=0;for(;r[n]===i[n];)++n;for(let e=r[n].nextSibling;e;e=e.nextSibling)if(e===i[n])return NodeService.DOCUMENT_POSITION_FOLLOWING;return NodeService.DOCUMENT_POSITION_PRECEDING}contains(e){let t=e;for(;t;){if(t===this)return!0;t=t.parentNode}return!1}getRootNode(e={composed:!1}){return this.parent?this.parent.getRootNode(e):this}hasChildNodes(){return this.childList.length>0}insertBefore(e,t=null){if(!this.acceptsChildren)throw new Error("This kind of node cannot have children.");if(null!==t&&t.parentNode!==this)throw new Error("The node before which the new node is to be inserted is not a child of this node.");if(e===t)return e;if("function"==typeof e.contains&&e.contains(this))throw new Error("The new node cannot be inserted into itself or one of its own descendants.");if(e.nodeType===NodeService.DOCUMENT_FRAGMENT_NODE){for(;e.firstChild;)this.insertBefore(e.firstChild,t);return e}e.parentNode&&e.parentNode.removeChild(e);const r=new TreeLinker_1.TreeLinker({data:e});this.childList.insertBefore(t?t.listLinker:null,r);const i=e;return i.parent=this,i.listLinker=r,this.childInserted(i),e}isDefaultNamespace(e){return null===e}isEqualNode(e){if(!e||e.nodeType!==this.nodeType||!this.equalsShallow(e))return!1;const t=Array.from(this.childNodes),r=Array.from(e.childNodes);return t.length===r.length&&t.every((e,t)=>e.isEqualNode(r[t]))}isSameNode(e){return this===e}lookupPrefix(e){return null}lookupNamespaceURI(e){return null}normalize(){let e=this.firstChild;for(;e;)if(e.nodeType===NodeService.TEXT_NODE){let t=e.nodeValue,r=e.nextSibling;for(;r&&r.nodeType===NodeService.TEXT_NODE;){t+=r.nodeValue;const e=r.nextSibling;this.removeChild(r),r=e}e.nodeValue=t,""===t&&this.removeChild(e),e=r}else e.normalize(),e=e.nextSibling}removeChild(e){if(!e||e.parentNode!==this)throw new Error("The node to be removed is not a child of this node.");const t=e;return this.childList.remove(t.listLinker),t.parent=null,t.listLinker=null,e}replaceChild(e,t){if(!t||t.parentNode!==this)throw new Error("The node to be replaced is not a child of this node.");if(e===t)return t;let r=t.nextSibling;return r===e&&(r=e.nextSibling),this.removeChild(t),this.insertBefore(e,r),t}}exports.NodeService=NodeService,NodeService.DEFAULT_NODE=0,NodeService.ELEMENT_NODE=1,NodeService.ATTRIBUTE_NODE=2,NodeService.TEXT_NODE=3,NodeService.CDATA_SECTION_NODE=4,NodeService.ENTITY_REFERENCE_NODE=5,NodeService.ENTITY_NODE=6,NodeService.PROCESSING_INSTRUCTION_NODE=7,NodeService.COMMENT_NODE=8,NodeService.DOCUMENT_NODE=9,NodeService.DOCUMENT_TYPE_NODE=10,NodeService.DOCUMENT_FRAGMENT_NODE=11,NodeService.NOTATION_NODE=12,NodeService.DOCUMENT_POSITION_DISCONNECTED=1,NodeService.DOCUMENT_POSITION_PRECEDING=2,NodeService.DOCUMENT_POSITION_FOLLOWING=4,NodeService.DOCUMENT_POSITION_CONTAINS=8,NodeService.DOCUMENT_POSITION_CONTAINED_BY=16,NodeService.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC=32,NodeService.nextNodeId=1;class TextService extends NodeService{constructor(e=""){super(),this.nodeValue=String(e)}get acceptsChildren(){return!1}get nodeName(){return"#text"}get nodeType(){return NodeService.TEXT_NODE}get data(){return this.nodeValue}set data(e){this.nodeValue=String(e)}get length(){return this.data.length}get textContent(){return this.data}set textContent(e){this.data=null===e?"":String(e)}get wholeText(){let e=this;for(;e.previousSibling&&e.previousSibling.nodeType===NodeService.TEXT_NODE;)e=e.previousSibling;let t="";for(let r=e;r&&r.nodeType===NodeService.TEXT_NODE;r=r.nextSibling)t+=r.nodeValue;return t}splitText(e){if(e<0||e>this.length)throw new Error("The offset is beyond the end of the text.");const t=new TextService(this.data.slice(e));return t.ownerDocumentStore=this.ownerDocumentStore,this.data=this.data.slice(0,e),this.parentNode&&this.parentNode.insertBefore(t,this.nextSibling),t}cloneShallow(){const e=new TextService(this.data);return e.ownerDocumentStore=this.ownerDocumentStore,e}}exports.TextService=TextService;class CommentService extends NodeService{constructor(e=""){super(),this.nodeValue=String(e)}get acceptsChildren(){return!1}get nodeName(){return"#comment"}get nodeType(){return NodeService.COMMENT_NODE}get data(){return this.nodeValue}set data(e){this.nodeValue=String(e)}get length(){return this.data.length}get textContent(){return this.data}set textContent(e){this.data=null===e?"":String(e)}cloneShallow(){const e=new CommentService(this.data);return e.ownerDocumentStore=this.ownerDocumentStore,e}}exports.CommentService=CommentService;
|