Added safe checks to DomHandler's methods
parent
f6ee238924
commit
7a01f9e32f
|
@ -1,19 +1,25 @@
|
||||||
export default {
|
export default {
|
||||||
|
|
||||||
innerWidth(el) {
|
innerWidth(el) {
|
||||||
|
if (el) {
|
||||||
let width = el.offsetWidth;
|
let width = el.offsetWidth;
|
||||||
let style = getComputedStyle(el);
|
let style = getComputedStyle(el);
|
||||||
|
|
||||||
width += parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
|
width += parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
|
||||||
return width;
|
return width;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
width(el) {
|
width(el) {
|
||||||
|
if (el) {
|
||||||
let width = el.offsetWidth;
|
let width = el.offsetWidth;
|
||||||
let style = getComputedStyle(el);
|
let style = getComputedStyle(el);
|
||||||
|
|
||||||
width -= parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
|
width -= parseFloat(style.paddingLeft) + parseFloat(style.paddingRight);
|
||||||
return width;
|
return width;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
getWindowScrollTop() {
|
getWindowScrollTop() {
|
||||||
|
@ -37,9 +43,7 @@ export default {
|
||||||
|
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getOuterHeight(el, margin) {
|
getOuterHeight(el, margin) {
|
||||||
|
@ -53,9 +57,7 @@ export default {
|
||||||
|
|
||||||
return height;
|
return height;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getClientHeight(el, margin) {
|
getClientHeight(el, margin) {
|
||||||
|
@ -68,9 +70,8 @@ export default {
|
||||||
}
|
}
|
||||||
|
|
||||||
return height;
|
return height;
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
getViewport() {
|
getViewport() {
|
||||||
|
@ -81,29 +82,39 @@ export default {
|
||||||
w = win.innerWidth || e.clientWidth || g.clientWidth,
|
w = win.innerWidth || e.clientWidth || g.clientWidth,
|
||||||
h = win.innerHeight || e.clientHeight || g.clientHeight;
|
h = win.innerHeight || e.clientHeight || g.clientHeight;
|
||||||
|
|
||||||
return {width: w, height: h};
|
return { width: w, height: h };
|
||||||
},
|
},
|
||||||
|
|
||||||
getOffset(el) {
|
getOffset(el) {
|
||||||
var rect = el.getBoundingClientRect();
|
if (el) {
|
||||||
|
let rect = el.getBoundingClientRect();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
top: rect.top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0),
|
top: rect.top + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0),
|
||||||
left: rect.left + (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0),
|
left: rect.left + (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0),
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
top: 'auto',
|
||||||
|
left: 'auto'
|
||||||
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
index(element) {
|
index(element) {
|
||||||
|
if (element) {
|
||||||
let children = element.parentNode.childNodes;
|
let children = element.parentNode.childNodes;
|
||||||
let num = 0;
|
let num = 0;
|
||||||
for (var i = 0; i < children.length; i++) {
|
for (let i = 0; i < children.length; i++) {
|
||||||
if (children[i] === element) return num;
|
if (children[i] === element) return num;
|
||||||
if (children[i].nodeType === 1) num++;
|
if (children[i].nodeType === 1) num++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
},
|
},
|
||||||
|
|
||||||
addMultipleClasses(element, className) {
|
addMultipleClasses(element, className) {
|
||||||
|
if (element && className) {
|
||||||
if (element.classList) {
|
if (element.classList) {
|
||||||
let styles = className.split(' ');
|
let styles = className.split(' ');
|
||||||
for (let i = 0; i < styles.length; i++) {
|
for (let i = 0; i < styles.length; i++) {
|
||||||
|
@ -117,20 +128,25 @@ export default {
|
||||||
element.className += ' ' + styles[i];
|
element.className += ' ' + styles[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
addClass(element, className) {
|
addClass(element, className) {
|
||||||
|
if (element && className) {
|
||||||
if (element.classList)
|
if (element.classList)
|
||||||
element.classList.add(className);
|
element.classList.add(className);
|
||||||
else
|
else
|
||||||
element.className += ' ' + className;
|
element.className += ' ' + className;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
removeClass(element, className) {
|
removeClass(element, className) {
|
||||||
|
if (element && className) {
|
||||||
if (element.classList)
|
if (element.classList)
|
||||||
element.classList.remove(className);
|
element.classList.remove(className);
|
||||||
else
|
else
|
||||||
element.className = element.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
|
element.className = element.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
hasClass(element, className) {
|
hasClass(element, className) {
|
||||||
|
@ -145,32 +161,42 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
find(element, selector) {
|
find(element, selector) {
|
||||||
return element.querySelectorAll(selector);
|
return element ? element.querySelectorAll(selector) : [];
|
||||||
},
|
},
|
||||||
|
|
||||||
findSingle(element, selector) {
|
findSingle(element, selector) {
|
||||||
|
if (element) {
|
||||||
return element.querySelector(selector);
|
return element.querySelector(selector);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
},
|
},
|
||||||
|
|
||||||
getHeight(el) {
|
getHeight(el) {
|
||||||
|
if (el) {
|
||||||
let height = el.offsetHeight;
|
let height = el.offsetHeight;
|
||||||
let style = getComputedStyle(el);
|
let style = getComputedStyle(el);
|
||||||
|
|
||||||
height -= parseFloat(style.paddingTop) + parseFloat(style.paddingBottom) + parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
|
height -= parseFloat(style.paddingTop) + parseFloat(style.paddingBottom) + parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
|
||||||
|
|
||||||
return height;
|
return height;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
getWidth(el) {
|
getWidth(el) {
|
||||||
|
if (el) {
|
||||||
let width = el.offsetWidth;
|
let width = el.offsetWidth;
|
||||||
let style = getComputedStyle(el);
|
let style = getComputedStyle(el);
|
||||||
|
|
||||||
width -= parseFloat(style.paddingLeft) + parseFloat(style.paddingRight) + parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth);
|
width -= parseFloat(style.paddingLeft) + parseFloat(style.paddingRight) + parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth);
|
||||||
|
|
||||||
return width;
|
return width;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
absolutePosition(element, target) {
|
absolutePosition(element, target) {
|
||||||
|
if (element) {
|
||||||
let elementDimensions = element.offsetParent ? { width: element.offsetWidth, height: element.offsetHeight } : this.getHiddenElementDimensions(element)
|
let elementDimensions = element.offsetParent ? { width: element.offsetWidth, height: element.offsetHeight } : this.getHiddenElementDimensions(element)
|
||||||
let elementOuterHeight = elementDimensions.height;
|
let elementOuterHeight = elementDimensions.height;
|
||||||
let elementOuterWidth = elementDimensions.width;
|
let elementOuterWidth = elementDimensions.width;
|
||||||
|
@ -202,9 +228,11 @@ export default {
|
||||||
|
|
||||||
element.style.top = top + 'px';
|
element.style.top = top + 'px';
|
||||||
element.style.left = left + 'px';
|
element.style.left = left + 'px';
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
relativePosition(element, target) {
|
relativePosition(element, target) {
|
||||||
|
if (element) {
|
||||||
let elementDimensions = element.offsetParent ? { width: element.offsetWidth, height: element.offsetHeight } : this.getHiddenElementDimensions(element);
|
let elementDimensions = element.offsetParent ? { width: element.offsetWidth, height: element.offsetHeight } : this.getHiddenElementDimensions(element);
|
||||||
const targetHeight = target.offsetHeight;
|
const targetHeight = target.offsetHeight;
|
||||||
const targetOffset = target.getBoundingClientRect();
|
const targetOffset = target.getBoundingClientRect();
|
||||||
|
@ -238,6 +266,7 @@ export default {
|
||||||
|
|
||||||
element.style.top = top + 'px';
|
element.style.top = top + 'px';
|
||||||
element.style.left = left + 'px';
|
element.style.left = left + 'px';
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getParents(element, parents = []) {
|
getParents(element, parents = []) {
|
||||||
|
@ -277,6 +306,7 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
getHiddenElementOuterHeight(element) {
|
getHiddenElementOuterHeight(element) {
|
||||||
|
if (element) {
|
||||||
element.style.visibility = 'hidden';
|
element.style.visibility = 'hidden';
|
||||||
element.style.display = 'block';
|
element.style.display = 'block';
|
||||||
let elementHeight = element.offsetHeight;
|
let elementHeight = element.offsetHeight;
|
||||||
|
@ -284,9 +314,12 @@ export default {
|
||||||
element.style.visibility = 'visible';
|
element.style.visibility = 'visible';
|
||||||
|
|
||||||
return elementHeight;
|
return elementHeight;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
getHiddenElementOuterWidth(element) {
|
getHiddenElementOuterWidth(element) {
|
||||||
|
if (element) {
|
||||||
element.style.visibility = 'hidden';
|
element.style.visibility = 'hidden';
|
||||||
element.style.display = 'block';
|
element.style.display = 'block';
|
||||||
let elementWidth = element.offsetWidth;
|
let elementWidth = element.offsetWidth;
|
||||||
|
@ -294,10 +327,13 @@ export default {
|
||||||
element.style.visibility = 'visible';
|
element.style.visibility = 'visible';
|
||||||
|
|
||||||
return elementWidth;
|
return elementWidth;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
getHiddenElementDimensions(element) {
|
getHiddenElementDimensions(element) {
|
||||||
var dimensions = {};
|
if (element) {
|
||||||
|
let dimensions = {};
|
||||||
element.style.visibility = 'hidden';
|
element.style.visibility = 'hidden';
|
||||||
element.style.display = 'block';
|
element.style.display = 'block';
|
||||||
dimensions.width = element.offsetWidth;
|
dimensions.width = element.offsetWidth;
|
||||||
|
@ -306,14 +342,17 @@ export default {
|
||||||
element.style.visibility = 'visible';
|
element.style.visibility = 'visible';
|
||||||
|
|
||||||
return dimensions;
|
return dimensions;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
},
|
},
|
||||||
|
|
||||||
fadeIn(element, duration) {
|
fadeIn(element, duration) {
|
||||||
|
if (element) {
|
||||||
element.style.opacity = 0;
|
element.style.opacity = 0;
|
||||||
|
|
||||||
var last = +new Date();
|
let last = +new Date();
|
||||||
var opacity = 0;
|
let opacity = 0;
|
||||||
var tick = function () {
|
let tick = function () {
|
||||||
opacity = +element.style.opacity + (new Date().getTime() - last) / duration;
|
opacity = +element.style.opacity + (new Date().getTime() - last) / duration;
|
||||||
element.style.opacity = opacity;
|
element.style.opacity = opacity;
|
||||||
last = +new Date();
|
last = +new Date();
|
||||||
|
@ -324,10 +363,12 @@ export default {
|
||||||
};
|
};
|
||||||
|
|
||||||
tick();
|
tick();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
fadeOut(element, ms) {
|
fadeOut(element, ms) {
|
||||||
var opacity = 1,
|
if (element) {
|
||||||
|
let opacity = 1,
|
||||||
interval = 50,
|
interval = 50,
|
||||||
duration = ms,
|
duration = ms,
|
||||||
gap = interval / duration;
|
gap = interval / duration;
|
||||||
|
@ -342,6 +383,7 @@ export default {
|
||||||
|
|
||||||
element.style.opacity = opacity;
|
element.style.opacity = opacity;
|
||||||
}, interval);
|
}, interval);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
getUserAgent() {
|
getUserAgent() {
|
||||||
|
@ -349,9 +391,9 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
appendChild(element, target) {
|
appendChild(element, target) {
|
||||||
if(this.isElement(target))
|
if (this.isElement(target))
|
||||||
target.appendChild(element);
|
target.appendChild(element);
|
||||||
else if(target.el && target.elElement)
|
else if (target.el && target.elElement)
|
||||||
target.elElement.appendChild(element);
|
target.elElement.appendChild(element);
|
||||||
else
|
else
|
||||||
throw new Error('Cannot append ' + target + ' to ' + element);
|
throw new Error('Cannot append ' + target + ' to ' + element);
|
||||||
|
@ -378,14 +420,14 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
clearSelection() {
|
clearSelection() {
|
||||||
if(window.getSelection) {
|
if (window.getSelection) {
|
||||||
if(window.getSelection().empty) {
|
if (window.getSelection().empty) {
|
||||||
window.getSelection().empty();
|
window.getSelection().empty();
|
||||||
} else if(window.getSelection().removeAllRanges && window.getSelection().rangeCount > 0 && window.getSelection().getRangeAt(0).getClientRects().length > 0) {
|
} else if (window.getSelection().removeAllRanges && window.getSelection().rangeCount > 0 && window.getSelection().getRangeAt(0).getClientRects().length > 0) {
|
||||||
window.getSelection().removeAllRanges();
|
window.getSelection().removeAllRanges();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(document['selection'] && document['selection'].empty) {
|
else if (document['selection'] && document['selection'].empty) {
|
||||||
try {
|
try {
|
||||||
document['selection'].empty();
|
document['selection'].empty();
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
|
@ -395,7 +437,7 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
calculateScrollbarWidth() {
|
calculateScrollbarWidth() {
|
||||||
if(this.calculatedScrollbarWidth != null)
|
if (this.calculatedScrollbarWidth != null)
|
||||||
return this.calculatedScrollbarWidth;
|
return this.calculatedScrollbarWidth;
|
||||||
|
|
||||||
let scrollDiv = document.createElement("div");
|
let scrollDiv = document.createElement("div");
|
||||||
|
@ -411,7 +453,7 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
getBrowser() {
|
getBrowser() {
|
||||||
if(!this.browser) {
|
if (!this.browser) {
|
||||||
let matched = this.resolveUserAgent();
|
let matched = this.resolveUserAgent();
|
||||||
this.browser = {};
|
this.browser = {};
|
||||||
|
|
||||||
|
@ -446,7 +488,7 @@ export default {
|
||||||
},
|
},
|
||||||
|
|
||||||
isVisible(element) {
|
isVisible(element) {
|
||||||
return element.offsetParent != null;
|
return element && element.offsetParent != null;
|
||||||
},
|
},
|
||||||
|
|
||||||
invokeElementMethod(element, methodName, args) {
|
invokeElementMethod(element, methodName, args) {
|
||||||
|
@ -457,12 +499,14 @@ export default {
|
||||||
return !!(typeof window !== 'undefined' && window.document && window.document.createElement);
|
return !!(typeof window !== 'undefined' && window.document && window.document.createElement);
|
||||||
},
|
},
|
||||||
|
|
||||||
getFocusableElements(element) {
|
getFocusableElements(element, selector = '') {
|
||||||
let focusableElements = this.find(element, `button:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
|
let focusableElements = this.find(element, `button:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
|
||||||
[href][clientHeight][clientWidth]:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
|
[href][clientHeight][clientWidth]:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
|
||||||
input:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]), select:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
|
input:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
|
||||||
textarea:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]), [tabIndex]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden]),
|
select:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
|
||||||
[contenteditable]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])`
|
textarea:not([tabindex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
|
||||||
|
[tabIndex]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector},
|
||||||
|
[contenteditable]:not([tabIndex = "-1"]):not([disabled]):not([style*="display:none"]):not([hidden])${selector}`
|
||||||
);
|
);
|
||||||
|
|
||||||
let visibleFocusableElements = [];
|
let visibleFocusableElements = [];
|
||||||
|
@ -474,8 +518,8 @@ export default {
|
||||||
return visibleFocusableElements;
|
return visibleFocusableElements;
|
||||||
},
|
},
|
||||||
|
|
||||||
getFirstFocusableElement(element) {
|
getFirstFocusableElement(element, selector) {
|
||||||
const focusableElements = this.getFocusableElements(element);
|
const focusableElements = this.getFocusableElements(element, selector);
|
||||||
return focusableElements.length > 0 ? focusableElements[0] : null;
|
return focusableElements.length > 0 ? focusableElements[0] : null;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue