From c8f015a7426352ac9346e3b57aa46f0bac5d177b Mon Sep 17 00:00:00 2001 From: Vivek Date: Mon, 27 Jul 2026 18:56:29 +0530 Subject: [PATCH 01/18] fix(core): stop SSR instantiating a component named inside a comment The custom-element walk matched tags with a flat regex over assembled markup, so a registered tag name written inside an HTML comment was constructed and rendered as a real element. The damage went well past a wasted render: the replacement consumed the rest of the comment INCLUDING its closing `-->`, so the markup after it was swallowed by an unterminated comment. Whether it happened at all depended on whether the name in the comment happened to be a registered component, which is what made it look random rather than reproducible. Found by writing an ordinary explanatory comment in the website's root layout that mentioned copy-cmd, which grew a real copy button out of nowhere. The walk now computes the comment ranges once and skips any match inside one. Two details keep it faithful to how a browser reads the same bytes: an unterminated ``, so the remaining markup was swallowed + * by an unterminated comment. Whether it happened at all depended on whether + * the name in the comment happened to be a registered component, which made it + * look random. + * + * Two details that keep this faithful to how a browser parses the same bytes: + * + * - An unterminated `', lt + 4); + const end = close === -1 ? html.length : close + 3; + ranges.push([lt, end]); + i = end; + continue; + } + const raw = /^<(script|style)\b/i.exec(html.slice(lt, lt + 8)); + if (raw) { + const closeTag = new RegExp(``, 'i').exec(html.slice(lt)); + i = closeTag ? lt + closeTag.index + closeTag[0].length : html.length; + continue; + } + i = lt + 1; + } + return ranges; +} + +/** + * Is `index` inside one of `ranges`? Ranges are ascending and non-overlapping, + * and callers walk matches left to right, so this stops as soon as a range + * starts past the index. + * + * @param {[number, number][]} ranges + * @param {number} index + * @returns {boolean} + */ +function inRanges(ranges, index) { + for (const [start, end] of ranges) { + if (start > index) return false; + if (index < end) return true; + } + return false; +} + /** * Scan an HTML string for registered custom elements and inject * Declarative Shadow DOM (`