-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
jsdom-patches.js
257 lines (227 loc) · 7.19 KB
/
jsdom-patches.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* This file is used in src/globals.js to patch jsdom-jscore.
*
* Node.prototype.contains is implemented as a simple recursive function.
*
* Node.prototype.insertBefore is re-implemented (code copied) with the
* WrongDocumentError exception disabled.
*
* Element.prototype.matches is aliased to Element.prototype.matchesSelector.
*
* Getters are defined on the Node.prototype for the following properties:
* parentElement, previousElementSibling, nextElementSibling.
*/
/**
* External dependencies
*/
import jsdom from 'jsdom-jscore-rn';
import jsdomLevel1Core from 'jsdom-jscore-rn/lib/jsdom/level1/core';
// Must be called to initialize jsdom before patching prototypes.
jsdom.html( '', null, null );
const { core } = jsdomLevel1Core.dom.level1;
const { Node, Element, CharacterData } = core;
// Exception codes.
const { NO_MODIFICATION_ALLOWED_ERR, HIERARCHY_REQUEST_ERR, NOT_FOUND_ERR } =
core;
/**
* Simple recursive implementation of Node.contains method
*
* @param {number} otherNode Another node (may be the same node).
* @return {boolean} true if otherNode is a descendant of this node, or is this
* node, false otherwise.
*
* This function is necessary in the mobile environment, because there are code
* paths that make use of functions in the Gutenberg (web) project, which has
* expectation that this is implemented (as it is in the browser environment).
*/
Node.prototype.contains = function ( otherNode ) {
return (
this === otherNode ||
Array.prototype.some.call( this._childNodes, ( childNode ) => {
return childNode.contains( otherNode );
} )
);
};
/**
* Copy of insertBefore function from jsdom-jscore, WRONG_DOCUMENT_ERR exception
* disabled.
*
* @param {Object} newChild The node to be insterted.
* @param {Object} refChild The node before which newChild is inserted.
* @return {Object} the newly inserted child node
*
* This function is modified here to remove the WRONG_DOCUMENT_ERR exception
* that is no longer part of the DOM spec for this function.
* see: https://github.com/jsdom/jsdom/issues/717 for more information, * and:
* https://dom.spec.whatwg.org/#dom-node-insertbefore for the latest spec.
*/
Node.prototype.insertBefore = function (
/* Node */ newChild,
/* Node */ refChild
) {
if ( this._readonly === true ) {
throw new core.DOMException(
NO_MODIFICATION_ALLOWED_ERR,
'Attempting to modify a read-only node'
);
}
// Adopt unowned children, for weird nodes like DocumentType.
if ( ! newChild._ownerDocument ) {
newChild._ownerDocument = this._ownerDocument;
}
/*
* This is commented out to prevent WrongDocumentError
* see: https://github.com/jsdom/jsdom/issues/717
*
// TODO - if (!newChild) then?
if (newChild._ownerDocument !== this._ownerDocument) {
throw new core.DOMException(WRONG_DOCUMENT_ERR);
}
*/
if ( newChild.nodeType && newChild.nodeType === newChild.ATTRIBUTE_NODE ) {
throw new core.DOMException( HIERARCHY_REQUEST_ERR );
}
// Search for parents matching the newChild.
let current = this;
do {
if ( current === newChild ) {
throw new core.DOMException( HIERARCHY_REQUEST_ERR );
}
} while ( ( current = current._parentNode ) );
// Fragments are merged into the element.
if ( newChild.nodeType === newChild.DOCUMENT_FRAGMENT_NODE ) {
let tmpNode,
i = newChild._childNodes.length;
while ( i-- > 0 ) {
tmpNode = newChild.removeChild( newChild.firstChild );
this.insertBefore( tmpNode, refChild );
}
} else if ( newChild === refChild ) {
return newChild;
} else {
// If the newChild is already in the tree elsewhere, remove it first.
if ( newChild._parentNode ) {
newChild._parentNode.removeChild( newChild );
}
if ( refChild === null ) {
// eslint-disable-next-line no-var
var refChildIndex = this._childNodes.length;
} else {
// eslint-disable-next-line no-redeclare, no-var
var refChildIndex = this._indexOf( refChild );
if ( refChildIndex === -1 ) {
throw new core.DOMException( NOT_FOUND_ERR );
}
}
Array.prototype.splice.call(
this._childNodes,
refChildIndex,
0,
newChild
);
newChild._parentNode = this;
if ( this._attached && newChild._attach ) {
newChild._attach();
}
this._modified();
}
return newChild;
}; // Raises(DOMException);
/*
* This is merely an alias (polyfill not needed).
* see: https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
*
* This function is necessary in the mobile environment, because there are code
* paths that make use of functions in the Gutenberg (web) project, which has
* expectation that this is implemented (as it is in the browser environment).
*/
Element.prototype.matches = Element.prototype.matchesSelector;
/*
* Implementation of Element.prototype.closest that it's missing from the jsdom-jscore fork we're using.
* See https://github.com/wordpress-mobile/gutenberg-mobile/issues/1625
*/
Element.prototype.closest = function ( selector ) {
let el = this;
while ( el ) {
if ( el.matches( selector ) ) {
return el;
}
el = el.parentElement;
}
return null;
};
/**
* Helper function to check if a node implements the NonDocumentTypeChildNode
* interface
*
* @param {Object} node Node to check
* @return {boolean} true if node is a NonDocumentTypeChildNode, false otherwise
*
* This function is needed to implement the previousElementSibling and
* nextElementSibling properties.
* See: https://dom.spec.whatwg.org/#interface-nondocumenttypechildnode
*/
function isNonDocumentTypeChildNode( node ) {
return node instanceof Element || node instanceof CharacterData;
}
// $FlowFixMe
Object.defineProperties( Node.prototype, {
/*
* This defines parentElement property on the Node prototype using a getter.
* See: https://dom.spec.whatwg.org/#parent-element
*/
parentElement: {
get() {
const parent = this.parentNode;
if ( parent && parent.nodeType === parent.ELEMENT_NODE ) {
return parent;
}
return null;
},
},
/*
* This defines previousElementSibling property on the Node prototype using a
* getter.
* See: https://dom.spec.whatwg.org/#dom-nondocumenttypechildnode-previouselementsibling
*/
previousElementSibling: {
get() {
// Property is undefined if node is not a NonDocumentTypeChildNode.
if ( ! isNonDocumentTypeChildNode( this ) ) {
return;
}
let sibling = this.previousSibling;
while ( sibling && sibling.nodeType !== sibling.ELEMENT_NODE ) {
sibling = sibling.previousSibling;
}
return sibling;
},
},
/*
* This defines nextElementSibling property on the Node prototype using a
* getter.
* See: https://dom.spec.whatwg.org/#dom-nondocumenttypechildnode-nextelementsibling
*/
nextElementSibling: {
get() {
// Property is undefined if node is not a NonDocumentTypeChildNode.
if ( ! isNonDocumentTypeChildNode( this ) ) {
return;
}
let sibling = this.nextSibling;
while ( sibling && sibling.nodeType !== sibling.ELEMENT_NODE ) {
sibling = sibling.nextSibling;
}
return sibling;
},
},
} );
class DOMParser {
// This is required for the stripHTML function, but it doesn't necessarily
// conform to the DOM standard.
// See https://github.com/wordpress-mobile/gutenberg-mobile/pull/1771
parseFromString( string ) {
return jsdom.html( string );
}
}
global.DOMParser = DOMParser;