-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(html-tags): Add a helper to create html-tags
- Loading branch information
Showing
2 changed files
with
77 additions
and
22 deletions.
There are no files selected for viewing
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,73 @@ | ||
// @ts-check | ||
/* eslint-disable */ | ||
/// <reference path="../typings.d.ts" /> | ||
/* eslint-enable */ | ||
/** | ||
* @file | ||
* This file provides to helper to create html as a object repesentation as | ||
* thoses objects are easier to modify than pure string representations | ||
* | ||
* Usage: | ||
* ``` | ||
* const element = createHtmlTagObject('h1', {class: 'demo'}, 'Hello World'); | ||
* const html = htmlTagObjectToString(element); | ||
* console.log(html) // -> <h1 class="demo">Hello World</h1> | ||
* ``` | ||
*/ | ||
|
||
/** | ||
* All html tag elements which must not contain innerHTML | ||
* @see https://www.w3.org/TR/html5/syntax.html#void-elements | ||
*/ | ||
const voidTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']; | ||
|
||
/** | ||
* Turn a tag definition into a html string | ||
* @param {HtmlTagObject} tagDefinition | ||
* A tag element according to the htmlWebpackPlugin object notation | ||
* | ||
* @param xhtml {boolean} | ||
* Wether the generated html should add closing slashes to be xhtml compliant | ||
*/ | ||
function htmlTagObjectToString (tagDefinition, xhtml) { | ||
const attributes = Object.keys(tagDefinition.attributes || {}) | ||
.filter(function (attributeName) { | ||
return tagDefinition.attributes[attributeName] !== false; | ||
}) | ||
.map(function (attributeName) { | ||
if (tagDefinition.attributes[attributeName] === true) { | ||
return xhtml ? attributeName + '="' + attributeName + '"' : attributeName; | ||
} | ||
return attributeName + '="' + tagDefinition.attributes[attributeName] + '"'; | ||
}); | ||
return '<' + [tagDefinition.tagName].concat(attributes).join(' ') + (tagDefinition.voidTag && xhtml ? '/' : '') + '>' + | ||
(tagDefinition.innerHTML || '') + | ||
(tagDefinition.voidTag ? '' : '</' + tagDefinition.tagName + '>'); | ||
} | ||
|
||
/** | ||
* Static helper to create a tag object to be get injected into the dom | ||
* | ||
* @param {string} tagName | ||
* the name of the tage e.g. 'div' | ||
* | ||
* @param {{[attributeName: string]: string|boolean}} [attributes] | ||
* tag attributes e.g. `{ 'class': 'example', disabled: true }` | ||
* | ||
* @param {string} [innerHTML] | ||
* | ||
* @returns {HtmlTagObject} | ||
*/ | ||
function createHtmlTagObject (tagName, attributes, innerHTML) { | ||
return { | ||
tagName: tagName, | ||
voidTag: voidTags.indexOf(tagName) !== -1, | ||
attributes: attributes || {}, | ||
innerHTML: innerHTML | ||
}; | ||
} | ||
|
||
module.exports = { | ||
createHtmlTagObject: createHtmlTagObject, | ||
htmlTagObjectToString: htmlTagObjectToString | ||
}; |