Skip to content

Commit d8c52da

Browse files
committed
fix(server): track SVG/MathML foreign context in viewport-height scanner
1 parent 5739e5c commit d8c52da

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

‎apps/server/src/htmlRender/HtmlRender.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ describe("HtmlRender", () => {
162162
'<svg><title/></svg><div style="height: 100vh">Content</div>',
163163
'<svg><title data-label="1 > 0" /></svg><style>body{height:100vh}</style>',
164164
'<svg><title style="height: 100vh"/></svg>',
165+
"<svg><title><style>body{min-height:100vh}</style></title></svg>",
166+
'<svg><title><div style="height: 100vh">Content</div></title></svg>',
167+
'<textarea style="height: 100vh"/>Example</textarea>',
165168
]) {
166169
const error = yield* htmlRender.prepare(page).pipe(Effect.flip);
167170
expect(error, page).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError);
@@ -195,6 +198,12 @@ describe("HtmlRender", () => {
195198
expect(yield* htmlRender.prepare(plaintext)).toContain(plaintext);
196199
const textarea = `<textarea data-path=/example/>${example}</textarea>`;
197200
expect(yield* htmlRender.prepare(textarea)).toContain(textarea);
201+
const selfClosingTextarea = `<textarea/>${example}</textarea>`;
202+
expect(yield* htmlRender.prepare(selfClosingTextarea)).toContain(selfClosingTextarea);
203+
const afterSelfClosingSvg = `<svg/><title>${example}</title>`;
204+
expect(yield* htmlRender.prepare(afterSelfClosingSvg)).toContain(afterSelfClosingSvg);
205+
const svgForeignObject = `<svg><foreignObject><textarea>${example}</textarea></foreignObject></svg>`;
206+
expect(yield* htmlRender.prepare(svgForeignObject)).toContain(svgForeignObject);
198207
}).pipe(Effect.provide(layerTest)),
199208
);
200209

‎apps/server/src/htmlRender/HtmlRender.ts‎

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,16 @@ const HTML_CSS_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|[^>])*
111111
const HTML_TEXT_TAG_ATTRIBUTES = String.raw`(?:${HTML_CSS_ATTRIBUTE_VALUE}|/(?!>)|[^>/])*`;
112112
const HTML_CSS_SOURCE = new RegExp(
113113
String.raw`<!--[\s\S]*?(?:-->|$)|<script(?=[\s/>])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)([\s\S]*?)(?:</script\s*>|$)|<style(?=[\s/>])[^>]*(?:>|$)([\s\S]*?)(?:</style\s*>|$)` +
114-
String.raw`|<(?<textElement>textarea|title|xmp|noembed|noframes|noscript)(?=[\t\n\f\r />])(${HTML_TEXT_TAG_ATTRIBUTES})(?:/>|(?:>|$)[\s\S]*?(?:</\k<textElement>(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)|$))` +
115-
String.raw`|<plaintext(?=[\t\n\f\r />])(${HTML_CSS_TAG_ATTRIBUTES})(?:>|$)[\s\S]*$|<[a-z](${HTML_CSS_TAG_ATTRIBUTES})`,
114+
String.raw`|</(svg|math|title|desc|foreignobject)(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)` +
115+
String.raw`|<([a-z][^\t\n\f\r />]*)(${HTML_TEXT_TAG_ATTRIBUTES})(\/?)(?:>|$)`,
116116
"gi",
117117
);
118+
const HTML_TEXT_END_PATTERNS = new Map(
119+
["textarea", "title", "xmp", "noembed", "noframes", "noscript"].map((tag) => [
120+
tag,
121+
new RegExp(String.raw`</${tag}(?=[\t\n\f\r />])${HTML_CSS_TAG_ATTRIBUTES}(?:>|$)`, "gi"),
122+
]),
123+
);
118124
// Inside a tag, skip other attributes' quoted values, such as a title that mentions style=.
119125
// A quote opens a value only right after `=`; an unquoted value such as `it's` or `a="b` runs to a space.
120126
const TAG_STYLE =
@@ -155,18 +161,51 @@ const findViewportHeight = (source: string, pattern: RegExp) => {
155161
};
156162

157163
const rejectViewportHeight = (html: string) => {
158-
for (const [, scriptTag, script, sheet, , textTag, plaintextTag, tag] of html.matchAll(
159-
HTML_CSS_SOURCE,
160-
)) {
161-
const attributes = textTag ?? plaintextTag ?? tag;
164+
const pattern = new RegExp(HTML_CSS_SOURCE.source, HTML_CSS_SOURCE.flags);
165+
const foreignStack: Array<{ readonly tag: string; readonly foreign: boolean }> = [];
166+
for (let match = pattern.exec(html); match !== null; match = pattern.exec(html)) {
167+
const [, scriptTag, script, sheet, closeTag, tagName, tagAttributes, selfClosing] = match;
168+
if (closeTag !== undefined) {
169+
const closed = closeTag.toLowerCase();
170+
for (let index = foreignStack.length - 1; index >= 0; index -= 1) {
171+
if (foreignStack[index]?.tag === closed) {
172+
foreignStack.length = index;
173+
break;
174+
}
175+
}
176+
continue;
177+
}
162178
const found =
163179
script !== undefined
164180
? (findViewportHeight(scriptTag ?? "", TAG_STYLE) ??
165181
findViewportHeight(script, SCRIPT_STYLE))
166-
: attributes !== undefined
167-
? findViewportHeight(attributes, TAG_STYLE)
182+
: tagAttributes !== undefined
183+
? findViewportHeight(tagAttributes, TAG_STYLE)
168184
: findInCss(sheet ?? "");
169185
if (found !== undefined) return Effect.fail(new HtmlRenderViewportHeightError({ found }));
186+
if (tagName === undefined) continue;
187+
const tag = tagName.toLowerCase();
188+
const inForeign = foreignStack.at(-1)?.foreign ?? false;
189+
if (!inForeign) {
190+
if (tag === "plaintext") break;
191+
const endPattern = HTML_TEXT_END_PATTERNS.get(tag);
192+
if (endPattern !== undefined) {
193+
endPattern.lastIndex = pattern.lastIndex;
194+
if (endPattern.exec(html) === null) break;
195+
pattern.lastIndex = endPattern.lastIndex;
196+
continue;
197+
}
198+
}
199+
if (!selfClosing) {
200+
if (tag === "svg" || tag === "math") {
201+
foreignStack.push({ tag, foreign: true });
202+
} else if (
203+
foreignStack.at(-1)?.tag === "svg" &&
204+
(tag === "title" || tag === "desc" || tag === "foreignobject")
205+
) {
206+
foreignStack.push({ tag, foreign: false });
207+
}
208+
}
170209
}
171210
return Effect.void;
172211
};

0 commit comments

Comments
 (0)