|
| 1 | +import MagicString from 'magic-string'; |
| 2 | +import { parse, preprocess, walk } from 'svelte/compiler'; |
| 3 | + |
| 4 | +const VoidElements = [ |
| 5 | + 'area', |
| 6 | + 'base', |
| 7 | + 'br', |
| 8 | + 'col', |
| 9 | + 'embed', |
| 10 | + 'hr', |
| 11 | + 'img', |
| 12 | + 'input', |
| 13 | + 'keygen', |
| 14 | + 'link', |
| 15 | + 'menuitem', |
| 16 | + 'meta', |
| 17 | + 'param', |
| 18 | + 'source', |
| 19 | + 'track', |
| 20 | + 'wbr' |
| 21 | +]; |
| 22 | + |
| 23 | +const SVGElements = [ |
| 24 | + 'altGlyph', |
| 25 | + 'altGlyphDef', |
| 26 | + 'altGlyphItem', |
| 27 | + 'animate', |
| 28 | + 'animateColor', |
| 29 | + 'animateMotion', |
| 30 | + 'animateTransform', |
| 31 | + 'circle', |
| 32 | + 'clipPath', |
| 33 | + 'color-profile', |
| 34 | + 'cursor', |
| 35 | + 'defs', |
| 36 | + 'desc', |
| 37 | + 'discard', |
| 38 | + 'ellipse', |
| 39 | + 'feBlend', |
| 40 | + 'feColorMatrix', |
| 41 | + 'feComponentTransfer', |
| 42 | + 'feComposite', |
| 43 | + 'feConvolveMatrix', |
| 44 | + 'feDiffuseLighting', |
| 45 | + 'feDisplacementMap', |
| 46 | + 'feDistantLight', |
| 47 | + 'feDropShadow', |
| 48 | + 'feFlood', |
| 49 | + 'feFuncA', |
| 50 | + 'feFuncB', |
| 51 | + 'feFuncG', |
| 52 | + 'feFuncR', |
| 53 | + 'feGaussianBlur', |
| 54 | + 'feImage', |
| 55 | + 'feMerge', |
| 56 | + 'feMergeNode', |
| 57 | + 'feMorphology', |
| 58 | + 'feOffset', |
| 59 | + 'fePointLight', |
| 60 | + 'feSpecularLighting', |
| 61 | + 'feSpotLight', |
| 62 | + 'feTile', |
| 63 | + 'feTurbulence', |
| 64 | + 'filter', |
| 65 | + 'font', |
| 66 | + 'font-face', |
| 67 | + 'font-face-format', |
| 68 | + 'font-face-name', |
| 69 | + 'font-face-src', |
| 70 | + 'font-face-uri', |
| 71 | + 'foreignObject', |
| 72 | + 'g', |
| 73 | + 'glyph', |
| 74 | + 'glyphRef', |
| 75 | + 'hatch', |
| 76 | + 'hatchpath', |
| 77 | + 'hkern', |
| 78 | + 'image', |
| 79 | + 'line', |
| 80 | + 'linearGradient', |
| 81 | + 'marker', |
| 82 | + 'mask', |
| 83 | + 'mesh', |
| 84 | + 'meshgradient', |
| 85 | + 'meshpatch', |
| 86 | + 'meshrow', |
| 87 | + 'metadata', |
| 88 | + 'missing-glyph', |
| 89 | + 'mpath', |
| 90 | + 'path', |
| 91 | + 'pattern', |
| 92 | + 'polygon', |
| 93 | + 'polyline', |
| 94 | + 'radialGradient', |
| 95 | + 'rect', |
| 96 | + 'set', |
| 97 | + 'solidcolor', |
| 98 | + 'stop', |
| 99 | + 'svg', |
| 100 | + 'switch', |
| 101 | + 'symbol', |
| 102 | + 'text', |
| 103 | + 'textPath', |
| 104 | + 'tref', |
| 105 | + 'tspan', |
| 106 | + 'unknown', |
| 107 | + 'use', |
| 108 | + 'view', |
| 109 | + 'vkern' |
| 110 | +]; |
| 111 | + |
| 112 | +/** @param {string} source */ |
| 113 | +export async function remove_self_closing_tags(source) { |
| 114 | + const preprocessed = await preprocess(source, { |
| 115 | + script: ({ content }) => ({ |
| 116 | + code: content |
| 117 | + .split('\n') |
| 118 | + .map((line) => ' '.repeat(line.length)) |
| 119 | + .join('\n') |
| 120 | + }), |
| 121 | + style: ({ content }) => ({ |
| 122 | + code: content |
| 123 | + .split('\n') |
| 124 | + .map((line) => ' '.repeat(line.length)) |
| 125 | + .join('\n') |
| 126 | + }) |
| 127 | + }); |
| 128 | + const ast = parse(preprocessed.code); |
| 129 | + const ms = new MagicString(source); |
| 130 | + /** @type {Array<() => void>} */ |
| 131 | + const updates = []; |
| 132 | + let is_foreign = false; |
| 133 | + let is_custom_element = false; |
| 134 | + |
| 135 | + walk(/** @type {any} */ (ast.html), { |
| 136 | + /** @param {Record<string, any>} node */ |
| 137 | + enter(node) { |
| 138 | + if (node.type === 'Options') { |
| 139 | + const namespace = node.attributes.find( |
| 140 | + /** @param {any} a */ |
| 141 | + (a) => a.type === 'Attribute' && a.name === 'namespace' |
| 142 | + ); |
| 143 | + if (namespace?.value[0].data === 'foreign') { |
| 144 | + is_foreign = true; |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + is_custom_element = node.attributes.some( |
| 149 | + /** @param {any} a */ |
| 150 | + (a) => a.type === 'Attribute' && (a.name === 'customElement' || a.name === 'tag') |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + if (node.type === 'Element' || node.type === 'Slot') { |
| 155 | + const is_self_closing = source[node.end - 2] === '/'; |
| 156 | + if ( |
| 157 | + !is_self_closing || |
| 158 | + VoidElements.includes(node.name) || |
| 159 | + SVGElements.includes(node.name) || |
| 160 | + !/^[a-zA-Z0-9_-]+$/.test(node.name) |
| 161 | + ) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + let start = node.end - 2; |
| 166 | + if (source[start - 1] === ' ') { |
| 167 | + start--; |
| 168 | + } |
| 169 | + updates.push(() => { |
| 170 | + if (node.type === 'Element' || is_custom_element) { |
| 171 | + ms.update(start, node.end, `></${node.name}>`); |
| 172 | + } |
| 173 | + }); |
| 174 | + } |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + if (is_foreign) { |
| 179 | + return source; |
| 180 | + } |
| 181 | + |
| 182 | + updates.forEach((update) => update()); |
| 183 | + return ms.toString(); |
| 184 | +} |
0 commit comments