Skip to content
This repository was archived by the owner on Aug 21, 2022. It is now read-only.

Commit 89423b9

Browse files
committed
fix Array.contains, del DOM4 methods
fix: Array.contains() for non-Array objects del: .prepend(), .append(), .before(), .after() and .replace() methods excluded from the W3C DOM 4 editor's draft w3c/dom@87000ba 53
1 parent 6e28315 commit 89423b9

File tree

5 files changed

+45
-129
lines changed

5 files changed

+45
-129
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ global | `FormData()`<sup>[12](#FormData)</sup>, `Set()`, `Map()`, `WeakSet()`,
8787
`Math` generics | `.trunc()`, `.sign()`
8888
`Function.prototype` | `.bind()`
8989
`Text.prototype` | `.textContent`
90-
`HTMLElement.prototype` | `.append()`, `.prepend()`, `.after()`, `.before()`, `.replace()`, `.remove()`, `.matches()`, `.addEventListener()`, `.removeEventListner()`, `.dispatchEvent()`, `.children`<sup>[7](#HTMLElement.prototype.children)</sup>, `.firstElementChild`, `.lastElementChild`, `.childElementCount`, `.nextElementSibling`, `.previousElementSibling`, `.textContent`, `.classList`<sup>[8](#HTMLElement.prototype.classList)</sup>, `.dataset`<sup>[9](#HTMLElement.prototype.dataset)</sup>
90+
`HTMLElement.prototype` | `.matches()`, `.addEventListener()`, `.removeEventListner()`, `.dispatchEvent()`, `.children`<sup>[7](#HTMLElement.prototype.children)</sup>, `.firstElementChild`, `.lastElementChild`, `.childElementCount`, `.nextElementSibling`, `.previousElementSibling`, `.textContent`, `.classList`<sup>[8](#HTMLElement.prototype.classList)</sup>, `.dataset`<sup>[9](#HTMLElement.prototype.dataset)</sup>
9191
`HTMLScriptElement.prototype` | `.onload()`<sup>[10](#HTMLScriptElement.prototype.onload)</sup>, `.onerror()`<sup>[10](#HTMLScriptElement.prototype.onload)</sup>
9292
`CSSStyleDeclaration.prototype` | `.getPropertyValue()`, `.removeProperty()`, `.setProperty()`, `.cssFloat`, `.opacity`
9393
`document` | `.head`, `.createEvent()`<sup>[11](#document.createEvent)</sup>
@@ -203,7 +203,7 @@ i18n('currency', {cost: 100}) // → '100 руб.'
203203
`.parse()` converts a HTML code into a document fragment
204204
```javascript
205205
var docFragment = lib.html.parse('<h1>Example</h1><p>...</p>');
206-
document.body.append(docFragment);
206+
document.body.appendChild(docFragment);
207207
```
208208

209209
####lib.html.escape()

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
##v0.7.0
44

5+
- fix: `Array.contains()` for non-Array objects
6+
- del: `.prepend()`, `.append()`, `.before()`, `.after()` and `.replace()` methods ([excluded from the W3C DOM 4 editor's draft](https://github.com/w3c/dom/commit/87000ba632e83f40a2384a4a15f3b913ef167653))
57
- `lib.Template()` now returns a function
68

79
##v0.6.1

src/polyfill/array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ if (!Array.prototype.contains) {
9595
}
9696
return false;
9797
}
98-
return -1 != this.indexOf(anything, position);
98+
return -1 != Array.prototype.indexOf.call(this, anything, position);
9999
};
100100
}

src/polyfill/htmlelement.js

Lines changed: 38 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -147,137 +147,51 @@ Object.defineProperty(HTMLElement.prototype, 'dataset', {
147147

148148
};
149149

150-
//DOM4 w3c.github.io/dom
151-
'append' in document.createDocumentFragment() || new function () {
152-
153-
var ELEMENT_NODE = 1,
154-
proto = HTMLElement.prototype,
155-
api = {
156-
157-
before: function (/* ...nodes */) {
158-
//todo IE8 removedNode.parentNode ≠ null
159-
var parentNode = this.parentNode;
160-
if (parentNode) {
161-
parentNode.insertBefore(mutationMacro(arguments), this);
162-
}
163-
},
164-
165-
after: function (/* ...nodes */) {
166-
var parentNode = this.parentNode,
167-
nextSibling,
168-
nodes;
169-
if (parentNode) {
170-
nodes = mutationMacro(arguments);
171-
nextSibling = this.nextSibling;
172-
if (nextSibling) {
173-
parentNode.insertBefore(nodes, nextSibling);
174-
} else {
175-
parentNode.appendChild(nodes);
176-
}
177-
}
178-
},
179-
180-
replace: function (/* ...nodes */) {
181-
var parentNode = this.parentNode;
182-
if (parentNode) {
183-
parentNode.replaceChild(mutationMacro(arguments), this);
184-
}
185-
},
186-
187-
remove: function () {
188-
var parentNode = this.parentNode;
189-
if (parentNode) {
190-
parentNode.removeChild(this);
191-
}
192-
},
193-
194-
append: function (/* ...nodes */) {
195-
this.appendChild(mutationMacro(arguments));
196-
},
197-
198-
prepend: function () {
199-
this.insertBefore(mutationMacro(arguments), this.firstChild);
200-
},
201-
202-
querySelector: proto.querySelector,
203-
204-
querySelectorAll: proto.querySelectorAll,
205-
206-
matches: [
207-
proto.matchesSelector,
208-
proto.oMatchesSelector,
209-
proto.msMatchesSelector,
210-
proto.mozMatchesSelector,
211-
proto.webkitMatchesSelector,
212-
function (selector) {
213-
var contains,
214-
root;
215-
if (this === document) {
216-
//if docFragment.constructor ≡ document.constructor
217-
return false;
218-
}
150+
document.documentElement.matches || new function () {
151+
152+
var proto = HTMLElement.prototype;
153+
154+
proto.matches = [
155+
proto.matchesSelector,
156+
proto.oMatchesSelector,
157+
proto.msMatchesSelector,
158+
proto.mozMatchesSelector,
159+
proto.webkitMatchesSelector
160+
].find(Boolean);
161+
162+
if (!proto.matches) {
163+
proto.matches = new function () {
164+
var ELEMENT_NODE = 1;
165+
function isContains(root, element, selector) {
166+
return Array.contains(root.querySelectorAll(selector), element);
167+
}
168+
return function (selector) {
169+
var contains,
219170
root = this.parentNode;
220-
if (root) {
221-
if (ELEMENT_NODE == root.nodeType) {
222-
root = root.ownerDocument;
223-
}
224-
return isContains(root, this, selector);
171+
if (root) {
172+
if (ELEMENT_NODE == root.nodeType) {
173+
root = root.ownerDocument;
225174
}
226-
root = document.createDocumentFragment();
227-
root.appendChild(this);
228-
contains = isContains(root, this, selector);
229-
root.removeChild(this);
175+
return isContains(root, this, selector);
230176
}
231-
].find(Boolean)
232-
177+
root = document.createDocumentFragment();
178+
root.appendChild(this);
179+
contains = isContains(root, this, selector);
180+
root.removeChild(this);
181+
};
233182
};
234-
235-
function isContains(root, element, selector) {
236-
return -1 != Array.indexOf(root.querySelectorAll(selector), element);
237183
}
238184

239-
function mutationMacro(nodes) {
240-
var node,
241-
fragment,
242-
length = nodes.length,
243-
i;
244-
if (1 == length) {
245-
node = nodes[0];
246-
if ('string' == typeof node) {
247-
return document.createTextNode(node);
248-
}
249-
return node;
250-
}
251-
fragment = document.createDocumentFragment();
252-
nodes = Array.from(nodes);
253-
i = 0;
254-
while (i < length) {
255-
node = nodes[i];
256-
if ('string' == typeof node) {
257-
node = document.createTextNode(node);
258-
}
259-
fragment.appendChild(node);
260-
i++;
261-
}
262-
return fragment;
263-
}
264-
265-
function implement(key) {
266-
if (!(key in proto)) {
267-
proto[key] = api[key];
268-
}
269-
}
270-
271-
Object.keys(api).forEach(implement);
185+
};
272186

273-
proto = document.constructor.prototype;
274-
['querySelector', 'querySelectorAll'].forEach(implement);
187+
new function () {
275188

276-
proto = document.createDocumentFragment().constructor.prototype;
277-
[
278-
'append', 'prepend',
279-
'querySelector', 'querySelectorAll',
280-
'matches'
281-
].forEach(implement);
189+
var docFragment = document.createDocumentFragment(),
190+
target = docFragment.constructor.prototype,
191+
source = HTMLElement.prototype;
192+
if (!docFragment.querySelector) {
193+
target.querySelector = source.querySelector;
194+
target.querySelectorAll = source.querySelectorAll;
195+
}
282196

283197
};

src/polyfill/ltie10/htmlelement.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Object.defineProperty(HTMLElement.prototype, 'classList', {
3636
this._update();
3737
length = this.length;
3838
Array.forEach(arguments, function (token) {
39-
if (-1 == Array.indexOf(this, token)) {
39+
if (!Array.contains(this, token)) {
4040
Array.push(this, token);
4141
}
4242
}, this);
@@ -72,7 +72,7 @@ Object.defineProperty(HTMLElement.prototype, 'classList', {
7272

7373
contains: function (token) {
7474
this._update();
75-
return -1 != Array.indexOf(this, token);
75+
return Array.contains(this, token);
7676
},
7777

7878
toString: function () {

0 commit comments

Comments
 (0)