File size: 1,649 Bytes
bdfd65a e5643f8 bdfd65a fcd0eab cb89283 fcd0eab cb89283 fcd0eab e492ddc |
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 |
/**
* Turn a string into a node
* @param {String} htmlString to convert
* @return {HTMLElement|Node} Converted node element
*/
export const createNodeFromString = (htmlString) => {
const div = document.createElement('div');
div.innerHTML = htmlString.trim();
// Change this to div.childNodes to support multiple top-level nodes
return div.firstChild;
};
/**
* Gets the CSS property from the given element
* @param {HTMLElement|Node} element
* @param {string} propertyName
* @param {boolean} prefixVendor
* @return {string}
*/
export const getStyleProperty = (element, propertyName, prefixVendor = false) => {
if (prefixVendor) {
const prefixes = ['', '-webkit-', '-ms-', 'moz-', '-o-'];
for (let counter = 0; counter < prefixes.length; counter++) {
const prefixedProperty = prefixes[counter] + propertyName;
const foundValue = getStyleProperty(element, prefixedProperty);
if (foundValue) {
return foundValue;
}
}
return '';
}
let propertyValue = '';
if (element.currentStyle) {
propertyValue = element.currentStyle[propertyName];
} else if (document.defaultView && document.defaultView.getComputedStyle) {
propertyValue = document.defaultView
.getComputedStyle(element, null)
.getPropertyValue(propertyName);
}
return propertyValue && propertyValue.toLowerCase ? propertyValue.toLowerCase() : propertyValue;
};
/**
* Checks if the passed element is dom object or not
* @param element
* @returns {boolean}
*/
export const isDomElement = function (element) {
return element && typeof element === 'object' && 'nodeType' in element;
};
|