UNPKG

svgdom

Version:

Straightforward DOM implementation for SVG, HTML and XML

94 lines (73 loc) 2.21 kB
// Ensure to six-based hex export const fullHex = function (hex) { return hex.length === 4 ? [ '#', hex.substring(1, 2), hex.substring(1, 2), hex.substring(2, 3), hex.substring(2, 3), hex.substring(3, 4), hex.substring(3, 4) ].join('') : hex } export const hexToRGB = function (valOrMap) { if (typeof valOrMap instanceof Map) { for (const [ key, val ] of valOrMap) { valOrMap.set(key, hexToRGB(val)) } return valOrMap } if (!/#[0-9a-f]{3,6}/.test(valOrMap)) { return valOrMap } valOrMap = fullHex(valOrMap) return 'rgb(' + [ parseInt(valOrMap.slice(1, 3), 16), parseInt(valOrMap.slice(3, 5), 16), parseInt(valOrMap.slice(5, 7), 16) ].join(',') + ')' } export function decamelize (s) { return String(s).replace(/([a-z])([A-Z])/g, function (m, g1, g2) { return g1 + '-' + g2.toLowerCase() }) } export function camelCase (s) { return String(s).replace(/([a-z])-([a-z])/g, function (m, g1, g2) { return g1 + g2.toUpperCase() }) } export function removeQuotes (str) { if (str.startsWith('"') || str.startsWith("'")) { return str.slice(1, -1) } return str } export function htmlEntities (str) { return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;') } export function unhtmlEntities (str) { return String(str).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace('&quot;', '"') } export function cdata (str) { return `<![CDATA[${str}]]>` } export function comment (str) { return `<!--${str}-->` } export const splitNotInBrackets = (str, delimiter) => { var roundBrackets = 0 var squareBrackets = 0 var lastIndex = 0 var split = [] var ch; var i; var il for (i = 0, il = str.length; i < il; ++i) { ch = str.charAt(i) if (ch === delimiter && !roundBrackets && !squareBrackets) { split.push(str.slice(lastIndex, i).trim()) lastIndex = i + 1 continue } if (ch === '(') ++roundBrackets else if (ch === ')') --roundBrackets else if (ch === '[') ++squareBrackets else if (ch === ']') --squareBrackets } split.push(str.slice(lastIndex).trim()) return split }