Last active
January 21, 2024 10:30
-
-
Save stephaned68/2186958166016c4dfe272bdc675efbf3 to your computer and use it in GitHub Desktop.
Roll20 Mod Scripts HTML Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* HTML helper functions | |
*/ | |
const hh = { | |
/** | |
* Return a string of style definitions | |
* @param {object} list key-value pairs | |
* @returns {string} style property | |
*/ | |
getStyle: function (list) { | |
let style = ""; | |
for (const prop in list) { | |
style += | |
prop.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase() + | |
":" + | |
list[prop] + | |
";"; | |
} | |
return style; | |
}, | |
/** | |
* Return an HTML element string | |
* @param {string} tag HTML element tag | |
* @param {string} content HTML element content | |
* @param {object} attributes key-value pairs of attributes | |
* @returns {string} HTML element | |
*/ | |
getElement: function (tag, content, attributes = {}) { | |
let html = `<${tag}`; | |
for (const [prop, value] of Object.entries(attributes)) { | |
html += ` ${prop | |
.replace(/([a-z])([A-Z])/g, "$1-$2") | |
.toLowerCase()}="${value}"`; | |
} | |
if (content === null) html += " />"; | |
else html += `>${content}</${tag}>`; | |
return html; | |
}, | |
/** | |
* Return a string with HTML elements siblings | |
* @param {array} elements list of HTML elements to create | |
* @returns {string} HTML elements | |
*/ | |
getElements: function (elements) { | |
let html = ""; | |
if (Array.isArray(elements)) { | |
elements.forEach((element) => { | |
html += htmlElement( | |
element.tag, | |
element.content || null, | |
element.attributes || {} | |
); | |
}); | |
} | |
return html; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A set of HTML Helper functions I use in my Roll20 MOD scripts to generate HTML chat messages