2016-07-19 10:21:09 -04:00
|
|
|
/**
|
2022-05-20 16:51:24 +02:00
|
|
|
* Escape HTML string
|
|
|
|
|
* @param {string} str - string to escape
|
|
|
|
|
* @return {string} escaped string
|
2016-07-19 10:21:09 -04:00
|
|
|
*/
|
2022-05-20 16:51:24 +02:00
|
|
|
|
|
|
|
|
const escapeHtml = str =>
|
|
|
|
|
str.replace(/[&<>'"]/g, tag => ({
|
|
|
|
|
'&': '&',
|
|
|
|
|
'<': '<',
|
|
|
|
|
'>': '>',
|
|
|
|
|
"'": ''',
|
|
|
|
|
'"': '"'
|
|
|
|
|
}[tag]))
|
|
|
|
|
|
2016-07-19 10:21:09 -04:00
|
|
|
|
|
|
|
|
/**
|
2022-05-20 16:51:24 +02:00
|
|
|
* Unescape HTML string
|
|
|
|
|
* @param {string} str - string to unescape
|
|
|
|
|
* @return {string} unescaped string
|
2016-07-19 10:21:09 -04:00
|
|
|
*/
|
2022-05-20 16:51:24 +02:00
|
|
|
|
|
|
|
|
const unescapeHtml = str =>
|
|
|
|
|
str.replace('&', '&')
|
|
|
|
|
.replace('<', '<')
|
|
|
|
|
.replace('>', '>')
|
|
|
|
|
.replace(''', "'")
|
|
|
|
|
.replace('"', '"')
|
|
|
|
|
|
2016-07-19 10:21:09 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get element data attributes
|
2022-05-20 16:51:24 +02:00
|
|
|
* @param {HTMLElement} node - node element
|
|
|
|
|
* @return {array} node data
|
2016-07-19 10:21:09 -04:00
|
|
|
*/
|
2022-05-20 16:51:24 +02:00
|
|
|
|
|
|
|
|
const getNodeData = node => {
|
|
|
|
|
|
2016-07-19 10:21:09 -04:00
|
|
|
// All attributes
|
2022-05-20 16:51:24 +02:00
|
|
|
const attributes = node.attributes
|
2016-07-19 10:21:09 -04:00
|
|
|
|
|
|
|
|
// Regex Pattern
|
2022-05-20 16:51:24 +02:00
|
|
|
const pattern = /^data\-(.+)$/
|
2016-07-19 10:21:09 -04:00
|
|
|
|
|
|
|
|
// Output
|
2022-05-20 16:51:24 +02:00
|
|
|
const data = {}
|
2016-07-19 10:21:09 -04:00
|
|
|
|
|
|
|
|
for (let i in attributes) {
|
|
|
|
|
if (!attributes[i]) {
|
2022-05-20 16:51:24 +02:00
|
|
|
continue
|
2016-07-19 10:21:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attributes name (ex: data-module)
|
2022-05-20 16:51:24 +02:00
|
|
|
let name = attributes[i].name
|
2016-07-19 10:21:09 -04:00
|
|
|
|
|
|
|
|
// This happens.
|
|
|
|
|
if (!name) {
|
2022-05-20 16:51:24 +02:00
|
|
|
continue
|
2016-07-19 10:21:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-20 16:51:24 +02:00
|
|
|
let match = name.match(pattern)
|
2016-07-19 10:21:09 -04:00
|
|
|
if (!match) {
|
2022-05-20 16:51:24 +02:00
|
|
|
continue
|
2016-07-19 10:21:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If this throws an error, you have some
|
|
|
|
|
// serious problems in your HTML.
|
2022-05-20 16:51:24 +02:00
|
|
|
data[match[1]] = getData(node.getAttribute(name))
|
2016-12-16 16:12:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
2022-05-20 16:51:24 +02:00
|
|
|
|
2016-12-16 16:12:06 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-20 16:51:24 +02:00
|
|
|
|
|
|
|
|
|
2016-12-16 16:12:06 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse value to data type.
|
|
|
|
|
*
|
|
|
|
|
* @link https://github.com/jquery/jquery/blob/3.1.1/src/data.js
|
2022-05-20 16:51:24 +02:00
|
|
|
* @param {string} data - value to convert
|
|
|
|
|
* @return {mixed} value in its natural data type
|
2016-12-16 16:12:06 -05:00
|
|
|
*/
|
2022-05-20 16:51:24 +02:00
|
|
|
|
|
|
|
|
const rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/
|
|
|
|
|
const getData = data => {
|
2016-12-16 16:12:06 -05:00
|
|
|
if (data === 'true') {
|
2022-05-20 16:51:24 +02:00
|
|
|
return true
|
2016-12-16 16:12:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data === 'false') {
|
2022-05-20 16:51:24 +02:00
|
|
|
return false
|
2016-12-16 16:12:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data === 'null') {
|
2022-05-20 16:51:24 +02:00
|
|
|
return null
|
2016-12-16 16:12:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only convert to a number if it doesn't change the string
|
|
|
|
|
if (data === +data+'') {
|
2022-05-20 16:51:24 +02:00
|
|
|
return +data
|
2016-12-16 16:12:06 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-20 16:51:24 +02:00
|
|
|
if (rbrace.test(data)) {
|
|
|
|
|
return JSON.parse(data)
|
2016-07-19 10:21:09 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-20 16:51:24 +02:00
|
|
|
return data
|
2016-07-19 10:21:09 -04:00
|
|
|
}
|
2019-06-25 14:21:03 -04:00
|
|
|
|
2022-05-20 16:51:24 +02:00
|
|
|
|
2019-06-25 14:21:03 -04:00
|
|
|
/**
|
|
|
|
|
* Returns an array containing all the parent nodes of the given node
|
2022-05-20 16:51:24 +02:00
|
|
|
* @param {HTMLElement} $el - DOM Element
|
|
|
|
|
* @return {array} parent nodes
|
2019-06-25 14:21:03 -04:00
|
|
|
*/
|
2022-05-20 16:51:24 +02:00
|
|
|
|
|
|
|
|
const getParents = $el => {
|
|
|
|
|
|
2019-06-25 14:21:03 -04:00
|
|
|
// Set up a parent array
|
2022-05-20 16:51:24 +02:00
|
|
|
let parents = []
|
2019-06-25 14:21:03 -04:00
|
|
|
|
|
|
|
|
// Push each parent element to the array
|
2022-05-20 16:51:24 +02:00
|
|
|
for (; $el && $el !== document; $el = $el.parentNode) {
|
|
|
|
|
parents.push($el)
|
2019-06-25 14:21:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return our parent array
|
2022-05-20 16:51:24 +02:00
|
|
|
return parents
|
2019-06-25 14:21:03 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-20 16:51:24 +02:00
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
escapeHtml,
|
|
|
|
|
unescapeHtml,
|
|
|
|
|
getNodeData,
|
|
|
|
|
getData,
|
|
|
|
|
getParents,
|
|
|
|
|
}
|