Files
moli/moli-core/tests/fixtures/document-encoding-parent.js
T
ldm0 c38dd589e8 fix(navigation): inherit document encoding from eligible containers
Capture the actual container Document's encoding and origin for child
navigation. Apply the same-origin and UTF-16 restrictions using the final
response URL and sandbox flags, including response CSP.

Decode local Blob and data navigations from response bytes. Keep popup
fallback independent of its opener and use UTF-8 for srcdoc snapshots.

Exercise nested frames, redirects, sandboxing, encoding precedence and
local URLs over real HTTP. Correct the existing MIME fixture's popup
fallback expectation while retaining all 64 iframe and popup cases.
2026-11-02 06:03:23 +08:00

39 lines
1.8 KiB
JavaScript

(() => {
const q = new URLSearchParams(location.search);
const token = q.get('token');
addEventListener('error', event => top.postMessage({token, error: event.message}, '*'));
top.postMessage({token: token + ':parent', charset: document.characterSet}, '*');
const child = new URL('/encoding-child', q.get('childOrigin') || location.href);
for (const name of ['token', 'header', 'meta', 'csp', 'bom']) {
if (q.has(name)) child.searchParams.set(name, q.get(name));
}
const report = '<script>addEventListener("load",()=>{' +
'const target=opener?opener.top:top;' +
'target.postMessage({token:' + JSON.stringify(token) +
',charset:document.characterSet,text:document.querySelector("p").textContent},"*")})</script>';
const markup = '<!doctype html><body><p>\xa2</p>' + report;
let target = child.href;
if (q.has('redirectOrigin')) {
target = q.get('redirectOrigin') + '/redirect?to=' + encodeURIComponent(target);
}
const mode = q.get('mode');
if (mode === 'data') {
target = 'data:text/html,' + Array.from(markup, c => '%' + c.charCodeAt(0).toString(16).padStart(2, '0')).join('');
} else if (mode === 'blob') {
target = URL.createObjectURL(new Blob([Uint8Array.from(markup, c => c.charCodeAt(0))], {type: 'text/html'}));
}
if (q.has('popup')) {
const popup = open(target, '_blank');
addEventListener('message', event => { if (event.data === 'cleanup') popup.close(); });
} else {
const frame = document.createElement('iframe');
if (q.has('sandbox')) frame.setAttribute('sandbox', q.get('sandbox'));
if (mode === 'srcdoc') frame.srcdoc = markup;
else frame.src = target;
document.body.append(frame);
}
addEventListener('message', event => {
if (event.data === 'cleanup' && mode === 'blob') URL.revokeObjectURL(target);
});
})()