Files
shields/badge-maker/lib/xml.js
chris48s e8c78d55b3 Migrate flat, flat-square, plastic and social to use XmlElement (#6883)
* start changelog entry for v4

* migrate Flat/FlatSquare/Plastic to use XmlElement

* move brightnessThreshold into colorsForBackground

* move old renderLogo function inline into social()

this is the only place it is now used

* use XmlElement in social()

* don't quote numbers if we don't need to

* remove intermediate calls to .render()

leave everything as XmlElement objects right till the end
then make one final call to .render()
which cascades aaall the way through the tree

* factor out code for assembling logo element

* use scale consts in social

* remove NullElement

now we've removed all the intermediate calls to render()
we can just use an empty string

* write leftlink so it doesn't look like a bool

Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
2021-08-21 20:44:14 +01:00

97 lines
2.4 KiB
JavaScript

/**
* @module
*/
'use strict'
function stripXmlWhitespace(xml) {
return xml.replace(/>\s+/g, '>').replace(/<\s+/g, '<').trim()
}
function escapeXml(s) {
if (typeof s === 'number') {
return s
} else if (s === undefined || typeof s !== 'string') {
return undefined
} else {
return s
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
}
}
/**
* Representation of an XML element
*/
class XmlElement {
/**
* Xml Element Constructor
*
* @param {object} attrs Refer to individual attrs
* @param {string} attrs.name
* Name of the XML tag
* @param {Array.<string|module:badge-maker/lib/xml-element~XmlElement>} [attrs.content=[]]
* Array of objects to render inside the tag. content may contain a mix of
* string and XmlElement objects. If content is `[]` or ommitted the
* element will be rendered as a self-closing element.
* @param {object} [attrs.attrs={}]
* Object representing the tag's attributes as name/value pairs
*/
constructor({ name, content = [], attrs = {} }) {
this.name = name
this.content = content
this.attrs = attrs
}
/**
* Render the XML element to a string, applying appropriate escaping
*
* @returns {string} String representation of the XML element
*/
render() {
const attrsStr = Object.entries(this.attrs)
.map(([k, v]) => ` ${k}="${escapeXml(v)}"`)
.join('')
if (this.content.length > 0) {
const content = this.content
.map(function (el) {
if (typeof el.render === 'function') {
return el.render()
} else {
return escapeXml(el)
}
})
.join(' ')
return stripXmlWhitespace(
`<${this.name}${attrsStr}>${content}</${this.name}>`
)
}
return stripXmlWhitespace(`<${this.name}${attrsStr}/>`)
}
}
/**
* Convenience class. Sometimes it is useful to return an object that behaves
* like an XmlElement but renders multiple XML tags (not wrapped in a <g>).
*/
class ElementList {
constructor({ content = [] }) {
this.content = content
}
render() {
return this.content.reduce(
(acc, el) =>
typeof el.render === 'function'
? acc + el.render()
: acc + escapeXml(el),
''
)
}
}
module.exports = { escapeXml, stripXmlWhitespace, XmlElement, ElementList }