pseudo-dom 0.1.0 → 0.2.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 (31) hide show
  1. package/README.md +496 -72
  2. package/browser/pseudo-dom.js +619 -284
  3. package/browser/pseudo-dom.min.js +1 -1
  4. package/dist/classes/PseudoHTMLDocument.d.ts +2 -2
  5. package/dist/classes/PseudoHTMLDocument.js +10 -21
  6. package/dist/classes/PseudoHTMLDocument.min.js +1 -1
  7. package/dist/classes/PseudoNodeList.js +14 -8
  8. package/dist/classes/PseudoNodeList.min.js +1 -1
  9. package/dist/factories/generateNode.d.ts +2 -10
  10. package/dist/factories/generateNode.js +9 -42
  11. package/dist/factories/generateNode.min.js +1 -1
  12. package/dist/functions/getParentNodes.d.ts +8 -6
  13. package/dist/functions/getParentNodes.js +16 -9
  14. package/dist/functions/getParentNodes.min.js +1 -1
  15. package/dist/functions/getParentNodesFromAttribute.d.ts +7 -7
  16. package/dist/functions/getParentNodesFromAttribute.js +21 -1
  17. package/dist/functions/getParentNodesFromAttribute.min.js +1 -1
  18. package/dist/functions.d.ts +1 -1
  19. package/dist/services/DocumentFragmentService.d.ts +9 -0
  20. package/dist/services/DocumentFragmentService.js +16 -1
  21. package/dist/services/DocumentFragmentService.min.js +1 -1
  22. package/dist/services/DocumentService.d.ts +8 -0
  23. package/dist/services/DocumentService.js +15 -1
  24. package/dist/services/DocumentService.min.js +1 -1
  25. package/dist/services/ElementService.d.ts +5 -6
  26. package/dist/services/ElementService.js +17 -13
  27. package/dist/services/ElementService.min.js +1 -1
  28. package/dist/services/NodeService.d.ts +31 -16
  29. package/dist/services/NodeService.js +90 -33
  30. package/dist/services/NodeService.min.js +1 -1
  31. package/package.json +2 -2
@@ -1,7 +1,5 @@
1
1
  'use strict'
2
2
 
3
- require('core-js/modules/esnext.iterator.constructor.js')
4
- require('core-js/modules/esnext.iterator.for-each.js')
5
3
  const __importDefault = void 0 && (void 0).__importDefault || function (mod) {
6
4
  return mod && mod.__esModule
7
5
  ? mod
@@ -19,6 +17,7 @@ exports.NodeService = void 0
19
17
  * @version 1.0.0
20
18
  */
21
19
  const generateNodeList_1 = __importDefault(require('../factories/generateNodeList'))
20
+ const TreeLinker_1 = require('collect-your-stuff/dist/collections/linked-tree-list/TreeLinker')
22
21
  const EventTargetService_1 = __importDefault(require('./EventTargetService'))
23
22
  /**
24
23
  * Simulate the behaviour of the Node Class when there is no DOM available.
@@ -41,8 +40,7 @@ class NodeService extends EventTargetService_1.default {
41
40
  this.nodeNameValue = ''
42
41
  this.children = (0, generateNodeList_1.default)()
43
42
  this.parent = null
44
- this.next = null
45
- this.prev = null
43
+ this.listLinker = null
46
44
  }
47
45
 
48
46
  get baseURI () {
@@ -66,7 +64,7 @@ class NodeService extends EventTargetService_1.default {
66
64
  }
67
65
 
68
66
  get nextSibling () {
69
- return this.isConnected ? this.next : null
67
+ return this.listLinker && this.listLinker.next ? this.listLinker.next.data : null
70
68
  }
71
69
 
72
70
  get nodeName () {
@@ -98,7 +96,7 @@ class NodeService extends EventTargetService_1.default {
98
96
  }
99
97
 
100
98
  get previousSibling () {
101
- return this.isConnected ? this.prev : null
99
+ return this.listLinker && this.listLinker.prev ? this.listLinker.prev.data : null
102
100
  }
103
101
 
104
102
  get textContent () {
@@ -110,15 +108,20 @@ class NodeService extends EventTargetService_1.default {
110
108
  }
111
109
 
112
110
  /**
113
- *
114
- * @param {PseudoNode} childNode
115
- * @returns {PseudoNode}
111
+ * Add a node as the last child of this node (a node which is already in a tree is moved).
112
+ * @param {PseudoNode} childNode The node to add
113
+ * @returns {PseudoNode} The added node
116
114
  */
117
115
  appendChild (childNode) {
118
- this.children.append(childNode)
119
- return childNode
116
+ return this.insertBefore(childNode, null)
120
117
  }
121
118
 
119
+ /**
120
+ * Called each time a node has been inserted as a child of this node, so that nodes which need to react to children
121
+ * (for example elements applying default events) can do so.
122
+ * @param {NodeService} child The node which was inserted
123
+ */
124
+ childInserted (child) {}
122
125
  /**
123
126
  * Not implemented yet.
124
127
  * @throws {Error}
@@ -136,11 +139,19 @@ class NodeService extends EventTargetService_1.default {
136
139
  }
137
140
 
138
141
  /**
139
- * Not implemented yet.
140
- * @throws {Error}
142
+ * Check whether a node is this node or one of its descendants.
143
+ * @param {PseudoNode|null} otherNode The node to look for
144
+ * @returns {boolean}
141
145
  */
142
146
  contains (otherNode) {
143
- throw new Error('NodeService.contains() is not implemented yet.')
147
+ let current = otherNode
148
+ while (current) {
149
+ if (current === this) {
150
+ return true
151
+ }
152
+ current = current.parentNode
153
+ }
154
+ return false
144
155
  }
145
156
 
146
157
  getRootNode (options = {
@@ -154,11 +165,44 @@ class NodeService extends EventTargetService_1.default {
154
165
  }
155
166
 
156
167
  /**
157
- * Not implemented yet.
158
- * @throws {Error}
168
+ * Insert a node as a child of this node, before the given child (or at the end when there is none). A node which is
169
+ * already in a tree is moved, and the children of a document fragment are moved in order.
170
+ * @param {PseudoNode} newNode The node to insert
171
+ * @param {PseudoNode|null} [referenceNode=null] The child of this node to insert before, or null to insert at the end
172
+ * @returns {PseudoNode} The inserted node
173
+ * @throws {Error} When the reference node is not a child of this node, or the new node is this node or contains it
159
174
  */
160
- insertBefore (newNode, referenceNode) {
161
- throw new Error('NodeService.insertBefore() is not implemented yet.')
175
+ insertBefore (newNode, referenceNode = null) {
176
+ if (referenceNode !== null && referenceNode.parentNode !== this) {
177
+ throw new Error('The node before which the new node is to be inserted is not a child of this node.')
178
+ }
179
+ if (newNode === referenceNode) {
180
+ // Inserting a node before itself leaves it where it is
181
+ return newNode
182
+ }
183
+ if (typeof newNode.contains === 'function' && newNode.contains(this)) {
184
+ throw new Error('The new node cannot be inserted into itself or one of its own descendants.')
185
+ }
186
+ if (newNode.nodeType === NodeService.DOCUMENT_FRAGMENT_NODE) {
187
+ // The children of a fragment are inserted (moved) in order, and the fragment is left empty
188
+ while (newNode.firstChild) {
189
+ this.insertBefore(newNode.firstChild, referenceNode)
190
+ }
191
+ return newNode
192
+ }
193
+ if (newNode.parentNode) {
194
+ // A node can only be in one place, so it is moved from where it was
195
+ newNode.parentNode.removeChild(newNode)
196
+ }
197
+ const linker = new TreeLinker_1.TreeLinker({
198
+ data: newNode
199
+ })
200
+ this.children.insertBefore(referenceNode ? referenceNode.listLinker : null, linker)
201
+ const inserted = newNode
202
+ inserted.parent = this
203
+ inserted.listLinker = linker
204
+ this.childInserted(inserted)
205
+ return newNode
162
206
  }
163
207
 
164
208
  isDefaultNamespace (namespaceURI) {
@@ -187,31 +231,44 @@ class NodeService extends EventTargetService_1.default {
187
231
 
188
232
  normalize () {}
189
233
  /**
190
- * Remove the given child from this node.
191
- * @param {PseudoNode} childElement The child node, or its TreeLinker from the children list
192
- * @returns {PseudoNode}
234
+ * Remove a child from this node, it no longer has a parent or siblings afterwards.
235
+ * @param {PseudoNode} childElement The child node to remove
236
+ * @returns {PseudoNode} The removed node
193
237
  * @throws {Error} When the node is not a child of this node
194
238
  */
195
239
  removeChild (childElement) {
196
- let found = null
197
- this.children.forEach(linker => {
198
- if (found === null && (linker === childElement || linker.data === childElement)) {
199
- found = linker
200
- }
201
- })
202
- if (found === null) {
240
+ if (!childElement || childElement.parentNode !== this) {
203
241
  throw new Error('The node to be removed is not a child of this node.')
204
242
  }
205
- this.children.remove(found)
206
- return found.data
243
+ const removed = childElement
244
+ this.children.remove(removed.listLinker)
245
+ removed.parent = null
246
+ removed.listLinker = null
247
+ return childElement
207
248
  }
208
249
 
209
250
  /**
210
- * Not implemented yet.
211
- * @throws {Error}
251
+ * Replace a child of this node with another node (which is moved if it is already in a tree).
252
+ * @param {PseudoNode} newChild The node which takes the place
253
+ * @param {PseudoNode} oldChild The child of this node to replace
254
+ * @returns {PseudoNode} The replaced node
255
+ * @throws {Error} When the old node is not a child of this node
212
256
  */
213
257
  replaceChild (newChild, oldChild) {
214
- throw new Error('NodeService.replaceChild() is not implemented yet.')
258
+ if (!oldChild || oldChild.parentNode !== this) {
259
+ throw new Error('The node to be replaced is not a child of this node.')
260
+ }
261
+ if (newChild === oldChild) {
262
+ return oldChild
263
+ }
264
+ // The new node goes where the old one was, which is before the old node's next sibling (unless that is the new node)
265
+ let reference = oldChild.nextSibling
266
+ if (reference === newChild) {
267
+ reference = newChild.nextSibling
268
+ }
269
+ this.removeChild(oldChild)
270
+ this.insertBefore(newChild, reference)
271
+ return oldChild
215
272
  }
216
273
  }
217
274
  exports.NodeService = NodeService
@@ -1 +1 @@
1
- "use strict";require("core-js/modules/esnext.iterator.constructor.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.NodeService=void 0;const generateNodeList_1=__importDefault(require("../factories/generateNodeList")),EventTargetService_1=__importDefault(require("./EventTargetService"));class NodeService extends EventTargetService_1.default{constructor(){super(),this.nodeValueStore="",this.textContentStore="",this.nodeNameValue="",this.children=(0,generateNodeList_1.default)(),this.parent=null,this.next=null,this.prev=null}get baseURI(){return window.location||"/"}get childNodes(){return this.children}get firstChild(){return this.children.first?this.children.first.data:null}get isConnected(){return!!this.parent}get lastChild(){return this.children.last?this.children.last.data:null}get nextSibling(){return this.isConnected?this.next: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(){return null}get parentNode(){return this.parent}get parentElement(){return this.parent&&this.parent.nodeType===NodeService.ELEMENT_NODE?this.parent:null}get previousSibling(){return this.isConnected?this.prev:null}get textContent(){return this.textContentStore}set textContent(e){this.textContentStore=e}appendChild(e){return this.children.append(e),e}cloneNode(e=!1){throw new Error(`NodeService.cloneNode(${e}) is not implemented yet.`)}compareDocumentPosition(e){throw new Error("NodeService.compareDocumentPosition() is not implemented yet.")}contains(e){throw new Error("NodeService.contains() is not implemented yet.")}getRootNode(e={composed:!1}){return this.parent?this.parent.getRootNode(e):this}hasChildNodes(){return this.children.length>0}insertBefore(e,t){throw new Error("NodeService.insertBefore() is not implemented yet.")}isDefaultNamespace(e){return null===e}isEqualNode(e){throw new Error("NodeService.isEqualNode() is not implemented yet.")}isSameNode(e){return this===e}lookupPrefix(e){return null}lookupNamespaceURI(e){return null}normalize(){}removeChild(e){let t=null;if(this.children.forEach(r=>{null!==t||r!==e&&r.data!==e||(t=r)}),null===t)throw new Error("The node to be removed is not a child of this node.");return this.children.remove(t),t.data}replaceChild(e,t){throw new Error("NodeService.replaceChild() is not implemented yet.")}}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;
1
+ "use strict";var __importDefault=function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),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="",this.textContentStore="",this.nodeNameValue="",this.children=(0,generateNodeList_1.default)(),this.parent=null,this.listLinker=null}get baseURI(){return window.location||"/"}get childNodes(){return this.children}get firstChild(){return this.children.first?this.children.first.data:null}get isConnected(){return!!this.parent}get lastChild(){return this.children.last?this.children.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(){return null}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(){return this.textContentStore}set textContent(e){this.textContentStore=e}appendChild(e){return this.insertBefore(e,null)}childInserted(e){}cloneNode(e=!1){throw new Error(`NodeService.cloneNode(${e}) is not implemented yet.`)}compareDocumentPosition(e){throw new Error("NodeService.compareDocumentPosition() is not implemented yet.")}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.children.length>0}insertBefore(e,t=null){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.children.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){throw new Error("NodeService.isEqualNode() is not implemented yet.")}isSameNode(e){return this===e}lookupPrefix(e){return null}lookupNamespaceURI(e){return null}normalize(){}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.children.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;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pseudo-dom",
3
3
  "description": "Mock the DOM on the server side for managing state and testing",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "main": "dist/main.js",
6
6
  "types": "dist/main.d.ts",
7
7
  "author": "Joshua Heagle",
@@ -18,7 +18,7 @@
18
18
  "watch:test": "gulp watchTest"
19
19
  },
20
20
  "dependencies": {
21
- "collect-your-stuff": "^1.2.8",
21
+ "collect-your-stuff": "^2.1.0",
22
22
  "core-js": "^3.50.0"
23
23
  },
24
24
  "devDependencies": {