|
| 1 | +const WHITESPACE_SENSITIVE_TAGS = new Set(['pre', 'textarea']) |
| 2 | + |
| 3 | +const BLOCK_TAGS = new Set([ |
| 4 | + 'address', |
| 5 | + 'article', |
| 6 | + 'aside', |
| 7 | + 'blockquote', |
| 8 | + 'body', |
| 9 | + 'br', |
| 10 | + 'div', |
| 11 | + 'dd', |
| 12 | + 'dl', |
| 13 | + 'dt', |
| 14 | + 'fieldset', |
| 15 | + 'figcaption', |
| 16 | + 'figure', |
| 17 | + 'footer', |
| 18 | + 'form', |
| 19 | + 'h1', |
| 20 | + 'h2', |
| 21 | + 'h3', |
| 22 | + 'h4', |
| 23 | + 'h5', |
| 24 | + 'h6', |
| 25 | + 'head', |
| 26 | + 'header', |
| 27 | + 'hr', |
| 28 | + 'html', |
| 29 | + 'li', |
| 30 | + 'main', |
| 31 | + 'nav', |
| 32 | + 'ol', |
| 33 | + 'p', |
| 34 | + 'pre', |
| 35 | + 'section', |
| 36 | + 'table', |
| 37 | + 'tbody', |
| 38 | + 'td', |
| 39 | + 'tfoot', |
| 40 | + 'th', |
| 41 | + 'thead', |
| 42 | + 'tr', |
| 43 | + 'ul' |
| 44 | +]) |
| 45 | + |
| 46 | +const TAG_PATTERN = /^<\s*(\/?)\s*([a-zA-Z][\w:-]*)([\s\S]*?)(\/?)\s*>$/ |
| 47 | +const ATTRIBUTE_PATTERN = /([\w:-]+)(?:\s*=\s*("[^"]*"|'[^']*'|[^\s"'>]+))?/g |
| 48 | + |
| 49 | +interface ParsedTag { |
| 50 | + closing: boolean |
| 51 | + name: string |
| 52 | + selfClosing: boolean |
| 53 | +} |
| 54 | + |
| 55 | +const parseTag = (token: string): ParsedTag | undefined => { |
| 56 | + const match = TAG_PATTERN.exec(token) |
| 57 | + if (match === null) { |
| 58 | + return undefined |
| 59 | + } |
| 60 | + return { |
| 61 | + closing: match[1] === '/', |
| 62 | + name: match[2].toLowerCase(), |
| 63 | + selfClosing: match[4] === '/' |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const normalizeTag = (token: string): string => { |
| 68 | + const match = TAG_PATTERN.exec(token) |
| 69 | + if (match === null) { |
| 70 | + return token |
| 71 | + } |
| 72 | + const [, closing, name, attributeSource, selfClosing] = match |
| 73 | + const attributes = Array.from(attributeSource.matchAll(ATTRIBUTE_PATTERN)) |
| 74 | + .map(([, attributeName, attributeValue]) => |
| 75 | + attributeValue === undefined |
| 76 | + ? attributeName.toLowerCase() |
| 77 | + : `${attributeName.toLowerCase()}=${normalizeAttributeValue(attributeValue)}` |
| 78 | + ) |
| 79 | + .sort() |
| 80 | + const renderedAttributes = attributes.length === 0 ? '' : ` ${attributes.join(' ')}` |
| 81 | + return `<${closing}${name.toLowerCase()}${renderedAttributes}${selfClosing}>` |
| 82 | +} |
| 83 | + |
| 84 | +const normalizeAttributeValue = (value: string): string => { |
| 85 | + const unquoted = |
| 86 | + (value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'")) |
| 87 | + ? value.slice(1, -1) |
| 88 | + : value |
| 89 | + return `"${unquoted}"` |
| 90 | +} |
| 91 | + |
| 92 | +const isBlockBoundary = (token: string | undefined): boolean => { |
| 93 | + if (token === undefined) { |
| 94 | + return true |
| 95 | + } |
| 96 | + const tag = parseTag(token) |
| 97 | + return tag !== undefined && BLOCK_TAGS.has(tag.name) |
| 98 | +} |
| 99 | + |
| 100 | +export const normalizeHtml = (html: string): string => { |
| 101 | + const tokens = html.split(/(<[^>]*>)/).filter((token) => token !== '') |
| 102 | + const normalized: string[] = [] |
| 103 | + let whitespaceSensitiveDepth = 0 |
| 104 | + |
| 105 | + tokens.forEach((token, index) => { |
| 106 | + const tag = token.startsWith('<') ? parseTag(token) : undefined |
| 107 | + |
| 108 | + if (tag !== undefined) { |
| 109 | + if (tag.closing && WHITESPACE_SENSITIVE_TAGS.has(tag.name)) { |
| 110 | + whitespaceSensitiveDepth = Math.max(0, whitespaceSensitiveDepth - 1) |
| 111 | + } |
| 112 | + normalized.push(normalizeTag(token)) |
| 113 | + if (!tag.closing && !tag.selfClosing && WHITESPACE_SENSITIVE_TAGS.has(tag.name)) { |
| 114 | + whitespaceSensitiveDepth += 1 |
| 115 | + } |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + if (token.startsWith('<') || whitespaceSensitiveDepth > 0) { |
| 120 | + normalized.push(token) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + const previousToken = tokens[index - 1] |
| 125 | + const nextToken = tokens[index + 1] |
| 126 | + |
| 127 | + if (token.trim() === '') { |
| 128 | + if (!isBlockBoundary(previousToken) && !isBlockBoundary(nextToken)) { |
| 129 | + normalized.push(' ') |
| 130 | + } |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + let text = token.replace(/\s+/g, ' ') |
| 135 | + if (isBlockBoundary(previousToken)) { |
| 136 | + text = text.replace(/^ /, '') |
| 137 | + } |
| 138 | + if (isBlockBoundary(nextToken)) { |
| 139 | + text = text.replace(/ $/, '') |
| 140 | + } |
| 141 | + normalized.push(text) |
| 142 | + }) |
| 143 | + |
| 144 | + return normalized.join('') |
| 145 | +} |
0 commit comments