/
githubmirror
/
webpack
Обзор
Документация
Войти
/
githubmirror
/
webpack
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
test/configCases/html/iframe-srcdoc/index.js
98 строк
4 KB
Sebastian Beltran
feat(html): implement asset URL rewriting for <iframe srcdoc> in HTML (#21226)
23 июн 2026, 23:33
Не верифицирован
23 июн 2026, 23:33
164a6ff
Код
Авторство
О чём код?
import page from "./page.html"; it("should rewrite asset URLs inside <iframe srcdoc>", () => { expect(typeof page).toBe("string"); expect(page).toMatchSnapshot(); // No original (unresolved) asset URL survives. expect(page).not.toContain("./pixel.png"); expect(page).not.toContain("./other.png"); // `src` is rewritten and the processed HTML re-escaped for the attribute, // exercising all three escapes: `"` -> `"`, `'` -> `'`, `&` -> `&`. expect(page).toMatch( /srcdoc="<img src="handled-pixel\.png" alt="a'b&c">"/ ); // Markup without assets passes through unchanged (idempotent). expect(page).toContain('srcdoc="<p>hello <b>world</b></p>"'); // Formatting-only markup (tags but no asset token: no `=`, `url(`, `@import`) // is left verbatim and spins up no nested module — see the `__STATS__` check // below and `SRCDOC_ASSET_REGEXP` in HtmlParser. expect(page).toContain( 'srcdoc="<article><h1>Heading</h1><p>Formatting only, no assets & nothing to rewrite.</p></article>"' ); // Multiple assets in one document are all rewritten. expect(page).toMatch( /srcdoc="<img src="handled-pixel\.png"><img src="handled-other\.png">"/ ); // An entity inside the URL (`.` = `.`) is decoded before resolving. expect(page).not.toContain("pixel.png"); expect(page).toMatch(/srcdoc="<img src="handled-pixel\.png">"/); // CSS `url()` inside a nested `style` attribute composes with the CSS // pipeline: the url is rewritten and the inner quotes re-escaped for srcdoc. expect(page).toMatch( /srcdoc="<div style="background: url\(handled-pixel\.png\)">styled<\/div>"/ ); // A single-quoted `srcdoc='...'` attribute is handled too. expect(page).toMatch(/srcdoc='<img src="handled-pixel\.png">'/); // Non-`img` asset references (here `<link href>`) are rewritten as well. expect(page).toMatch( /srcdoc="<link rel="icon" href="handled-pixel\.png">"/ ); // URLs inside srcdoc go through the same pipeline as top-level HTML: a // resolvable asset is emitted/rewritten, while absolute and protocol-relative // URLs become the `data:,` ignored-asset (delegated, not special-cased). expect(page).toMatch( /srcdoc="<img src="handled-pixel\.png"><img src="data:,"><img src="data:,">"/ ); // Nested `<iframe srcdoc>` recurses; the multi-level escaping turns the inner // `"` into `&quot;` while the innermost asset is still rewritten. expect(page).toMatch( /srcdoc="<iframe srcdoc="<img src=&quot;handled-pixel\.png&quot;>">"/ ); // A full document (DOCTYPE + html/head/body, the canonical srcdoc form per // the HTML spec) is parsed in document mode; assets in head and body resolve. expect(page).toMatch( /srcdoc="<!DOCTYPE html><html><head><link rel="icon" href="handled-pixel\.png"><\/head><body><img src="handled-other\.png"><\/body><\/html>"/ ); // The exact syntactic form the HTML spec defines for srcdoc (comments + ASCII // whitespace, an optional DOCTYPE, the `html` document element, more comments) // parses cleanly: comments and DOCTYPE are preserved, the inner asset resolves. expect(page).toMatch( /srcdoc="<!-- lead --> <!DOCTYPE html> <!-- mid --> <html><body><img src="handled-pixel\.png"><\/body><\/html> <!-- trail -->"/ ); // Plain text (no markup) is left untouched — no nested module is spun up. expect(page).toContain('srcdoc="no assets here"'); }); it("should not spin up a nested module for asset-free srcdoc markup", () => { const ids = __STATS__.modules.map((m) => m.identifier || m.name || ""); // Positive control: `<img src="./pixel.png">` (an asset-bearing document) // IS turned into a nested `data:text/html` module. expect(ids).toContainEqual( expect.stringContaining( "data:text/html;base64,PGltZyBzcmM9Ii4vcGl4ZWwucG5nIj4=" ) ); // `<p>hello <b>world</b></p>` rewrites to itself, so it must NOT become a // module (the pre-filter skips markup with no asset-bearing token). expect(ids).not.toContainEqual( expect.stringContaining( "data:text/html;base64,PHA+aGVsbG8gPGI+d29ybGQ8L2I+PC9wPg==" ) ); });