|
| 1 | +// @ts-check |
| 2 | +/* eslint-disable */ |
| 3 | +/// <reference path="../typings.d.ts" /> |
| 4 | +/* eslint-enable */ |
| 5 | +/** |
| 6 | + * @file |
| 7 | + * This file provides to helper to create html as a object repesentation as |
| 8 | + * thoses objects are easier to modify than pure string representations |
| 9 | + * |
| 10 | + * Usage: |
| 11 | + * ``` |
| 12 | + * const element = createHtmlTagObject('h1', {class: 'demo'}, 'Hello World'); |
| 13 | + * const html = htmlTagObjectToString(element); |
| 14 | + * console.log(html) // -> <h1 class="demo">Hello World</h1> |
| 15 | + * ``` |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * All html tag elements which must not contain innerHTML |
| 20 | + * @see https://www.w3.org/TR/html5/syntax.html#void-elements |
| 21 | + */ |
| 22 | +const voidTags = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']; |
| 23 | + |
| 24 | +/** |
| 25 | + * Turn a tag definition into a html string |
| 26 | + * @param {HtmlTagObject} tagDefinition |
| 27 | + * A tag element according to the htmlWebpackPlugin object notation |
| 28 | + * |
| 29 | + * @param xhtml {boolean} |
| 30 | + * Wether the generated html should add closing slashes to be xhtml compliant |
| 31 | + */ |
| 32 | +function htmlTagObjectToString (tagDefinition, xhtml) { |
| 33 | + const attributes = Object.keys(tagDefinition.attributes || {}) |
| 34 | + .filter(function (attributeName) { |
| 35 | + return tagDefinition.attributes[attributeName] !== false; |
| 36 | + }) |
| 37 | + .map(function (attributeName) { |
| 38 | + if (tagDefinition.attributes[attributeName] === true) { |
| 39 | + return xhtml ? attributeName + '="' + attributeName + '"' : attributeName; |
| 40 | + } |
| 41 | + return attributeName + '="' + tagDefinition.attributes[attributeName] + '"'; |
| 42 | + }); |
| 43 | + return '<' + [tagDefinition.tagName].concat(attributes).join(' ') + (tagDefinition.voidTag && xhtml ? '/' : '') + '>' + |
| 44 | + (tagDefinition.innerHTML || '') + |
| 45 | + (tagDefinition.voidTag ? '' : '</' + tagDefinition.tagName + '>'); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Static helper to create a tag object to be get injected into the dom |
| 50 | + * |
| 51 | + * @param {string} tagName |
| 52 | + * the name of the tage e.g. 'div' |
| 53 | + * |
| 54 | + * @param {{[attributeName: string]: string|boolean}} [attributes] |
| 55 | + * tag attributes e.g. `{ 'class': 'example', disabled: true }` |
| 56 | + * |
| 57 | + * @param {string} [innerHTML] |
| 58 | + * |
| 59 | + * @returns {HtmlTagObject} |
| 60 | + */ |
| 61 | +function createHtmlTagObject (tagName, attributes, innerHTML) { |
| 62 | + return { |
| 63 | + tagName: tagName, |
| 64 | + voidTag: voidTags.indexOf(tagName) !== -1, |
| 65 | + attributes: attributes || {}, |
| 66 | + innerHTML: innerHTML |
| 67 | + }; |
| 68 | +} |
| 69 | + |
| 70 | +module.exports = { |
| 71 | + createHtmlTagObject: createHtmlTagObject, |
| 72 | + htmlTagObjectToString: htmlTagObjectToString |
| 73 | +}; |
0 commit comments