Skip to content

Commit abf1741

Browse files
committed
standardize utility method calls
1 parent 4c4e26e commit abf1741

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/undom.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class Node {
3232
}
3333
get nextSibling() {
3434
let p = this.parentNode;
35-
if (p) return p.childNodes[findWhere(p.childNodes, this, true) + 1];
35+
if (p) return p.childNodes[findWhere(p.childNodes, this, true, true) + 1];
3636
}
3737
get previousSibling() {
3838
let p = this.parentNode;
39-
if (p) return p.childNodes[findWhere(p.childNodes, this, true) - 1];
39+
if (p) return p.childNodes[findWhere(p.childNodes, this, true, true) - 1];
4040
}
4141
get firstChild() {
4242
return this.childNodes[0];
@@ -51,7 +51,8 @@ class Node {
5151
insertBefore(child, ref) {
5252
child.remove();
5353
child.parentNode = this;
54-
!ref ? this.childNodes.push(child) : splice(this.childNodes, ref, child);
54+
if (ref) splice(this.childNodes, ref, child, true);
55+
else this.childNodes.push(child);
5556
return child;
5657
}
5758
replaceChild(child, ref) {
@@ -62,7 +63,7 @@ class Node {
6263
}
6364
}
6465
removeChild(child) {
65-
splice(this.childNodes, child);
66+
splice(this.childNodes, child, false, true);
6667
return child;
6768
}
6869
remove() {
@@ -114,23 +115,23 @@ class Element extends Node {
114115
}
115116

116117
setAttributeNS(ns, name, value) {
117-
let attr = findWhere(this.attributes, createAttributeFilter(ns, name));
118+
let attr = findWhere(this.attributes, createAttributeFilter(ns, name), false, false);
118119
if (!attr) this.attributes.push(attr = { ns, name });
119120
attr.value = String(value);
120121
}
121122
getAttributeNS(ns, name) {
122-
let attr = findWhere(this.attributes, createAttributeFilter(ns, name));
123+
let attr = findWhere(this.attributes, createAttributeFilter(ns, name), false, false);
123124
return attr && attr.value;
124125
}
125126
removeAttributeNS(ns, name) {
126-
splice(this.attributes, createAttributeFilter(ns, name));
127+
splice(this.attributes, createAttributeFilter(ns, name), false, false);
127128
}
128129

129130
addEventListener(type, handler) {
130131
(this.__handlers[toLower(type)] || (this.__handlers[toLower(type)] = [])).push(handler);
131132
}
132133
removeEventListener(type, handler) {
133-
splice(this.__handlers[toLower(type)], handler, 0, true);
134+
splice(this.__handlers[toLower(type)], handler, false, true);
134135
}
135136
dispatchEvent(event) {
136137
let t = event.target = this,
@@ -195,7 +196,7 @@ class Event {
195196
*/
196197
export default function createDocument() {
197198
let document = new Document();
198-
assign(document, document.defaultView = { document, Document, Node, Text, Element, SVGElement:Element, Event });
199+
assign(document, document.defaultView = { document, Document, Node, Text, Element, SVGElement: Element, Event });
199200
document.appendChild(
200201
document.documentElement = document.createElement('html')
201202
);

src/util.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ export function toLower(str) {
66
return String(str).toLowerCase();
77
}
88

9-
export function splice(arr, item, add, byValueOnly) {
10-
let i = arr ? findWhere(arr, item, true, byValueOnly) : -1;
9+
export function splice(arr, item, add, byValue) {
10+
let i = arr ? findWhere(arr, item, true, byValue) : -1;
1111
if (~i) add ? arr.splice(i, 0, add) : arr.splice(i, 1);
1212
return i;
1313
}
1414

15-
export function findWhere(arr, fn, returnIndex, byValueOnly) {
15+
export function findWhere(arr, fn, returnIndex, byValue) {
1616
let i = arr.length;
17-
while (i--) if (typeof fn==='function' && !byValueOnly ? fn(arr[i]) : arr[i]===fn) break;
17+
while (i--) if (byValue ? arr[i]===fn : fn(arr[i])) break;
1818
return returnIndex ? i : arr[i];
1919
}
2020

0 commit comments

Comments
 (0)