-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
253 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* eslint-disable no-restricted-syntax, no-continue, no-await-in-loop */ | ||
/** | ||
* @link https://github.com/studiometa/twig-toolkit | ||
* @copyright Studio Meta | ||
* @license https://github.com/studiometa/twig-toolkit/blob/master/LICENSE | ||
*/ | ||
import toKebab from 'kebab-case'; | ||
|
||
/** | ||
* @typdef {string | Record<string, boolean> | Record<number, Classes>} Classes | ||
*/ | ||
|
||
/** | ||
* Html class. | ||
*/ | ||
export default class Html { | ||
static SELF_CLOSING_TAGS = [ | ||
'area', | ||
'base', | ||
'br', | ||
'col', | ||
'command', | ||
'embed', | ||
'hr', | ||
'img', | ||
'input', | ||
'keygen', | ||
'link', | ||
'meta', | ||
'param', | ||
'source', | ||
'track', | ||
'wbr', | ||
]; | ||
|
||
/** | ||
* Render classes. | ||
* @param {Classes} classes | ||
* @return {string} | ||
*/ | ||
static renderClass(classes) { | ||
if (!classes) { | ||
return ''; | ||
} | ||
|
||
if (typeof classes === 'string') { | ||
return classes; | ||
} | ||
|
||
if (Array.isArray(classes)) { | ||
return classes.map((c) => Html.renderClass(c)).join(' '); | ||
} | ||
|
||
return Object.entries(classes) | ||
.reduce((acc, [key, value]) => { | ||
if (value && key !== '_keys') { | ||
acc.push(key); | ||
} | ||
|
||
return acc; | ||
}, []) | ||
.join(' '); | ||
} | ||
|
||
/** | ||
* Render a style attribute. | ||
* @param {Record<string, string|number>} styles | ||
* @return {string} | ||
*/ | ||
static renderStyleAttribute(styles) { | ||
if (!styles) { | ||
return ''; | ||
} | ||
|
||
const renderedStyles = []; | ||
|
||
for (const [key, value] of Object.entries(styles)) { | ||
if (key === '_keys' || (typeof value === 'boolean' && !value) || value === '') { | ||
continue; | ||
} | ||
renderedStyles.push(`${toKebab(key)}: ${value};`); | ||
} | ||
return renderedStyles.join(' '); | ||
} | ||
|
||
/** | ||
* Render attributes. | ||
* | ||
* @param {Record<string, any>} attributes | ||
* @return {string} | ||
*/ | ||
static renderAttributes(attributes) { | ||
if (!attributes) { | ||
return ''; | ||
} | ||
|
||
const renderedAttributes = ['']; | ||
|
||
for (let [key, value] of Object.entries(attributes)) { | ||
if (key === '_keys') { | ||
continue; | ||
} | ||
|
||
key = toKebab(key); | ||
if (typeof value === 'boolean') { | ||
if (value) { | ||
renderedAttributes.push(key); | ||
} | ||
continue; | ||
} | ||
if (key === 'class') { | ||
value = Html.renderClass(value); | ||
} | ||
if (key === 'style' && typeof value !== 'string') { | ||
value = Html.renderStyleAttribute(value); | ||
} | ||
if (typeof value !== 'string') { | ||
if (value instanceof Map) { | ||
value = JSON.stringify(Html.mapToObject(value)); | ||
} else { | ||
value = JSON.stringify(value); | ||
} | ||
} | ||
|
||
renderedAttributes.push(`${key}="${value}"`); | ||
} | ||
|
||
return renderedAttributes.join(' '); | ||
} | ||
|
||
/** | ||
* Convert a map to an object. | ||
* | ||
* @param {Map} map | ||
* @return {Record<string, any>} | ||
*/ | ||
static mapToObject(map) { | ||
const obj = {}; | ||
for (const [key, value] of map.entries()) { | ||
if (value instanceof Map) { | ||
obj[key] = Html.mapToObject(value); | ||
} else { | ||
obj[key] = value; | ||
} | ||
} | ||
return obj; | ||
} | ||
|
||
/** | ||
* Render a tag. | ||
* | ||
* @param {string} name | ||
* @param {Record<string, any>} attributes | ||
* @param {string} content | ||
* @return {string} | ||
*/ | ||
static renderTag(name, attributes, content = '') { | ||
const formattedAttributes = Html.renderAttributes(attributes); | ||
if (Html.SELF_CLOSING_TAGS.includes(name)) { | ||
return `<${name}${formattedAttributes} />`; | ||
} | ||
return `<${name}${formattedAttributes}>\n${content}\n</${name}>`; | ||
} | ||
} |