Skip to content

Truly independent web browser

2.2 KB
Raw Blame
<!DOCTYPE html>
<meta charset="utf-8">
<script src="../include.js"></script>
<script>
    test(() => {
        let testCounter = 1;
        function testPart(part) {
            println(`${testCounter++}. ${JSON.stringify(part())}`);
        }

        // 1. TextEncoder encode
        testPart(() => new TextEncoder().encode('Well Hello Friends πŸ˜€!'));

        // 2. TextEncoder encodeInto
        testPart(() => {
            const buffer = new Uint8Array(32);
            new TextEncoder().encodeInto('Well Hello Friends πŸ˜€!', buffer);
            return buffer
        });

        // 3. TextEncoder encodeInto result read
        testPart(() => {
            const buffer = new Uint8Array(32);
            const result = new TextEncoder().encodeInto('Well Hello Friends πŸ˜€!', buffer);
            return result.read;
        });

        // 3. TextEncoder encodeInto result written
        testPart(() => {
            const buffer = new Uint8Array(32);
            const result = new TextEncoder().encodeInto('Well Hello Friends πŸ˜€!', buffer);
            return result.written;
        });

        // 5. TextEncoder encode uses receiver realm for returned Uint8Array
        testPart(() => {
            const iframe = document.createElement("iframe");
            document.body.appendChild(iframe);
            const iframeWindow = iframe.contentWindow;
            const iframeEncoder = new iframeWindow.TextEncoder();
            const result = TextEncoder.prototype.encode.call(iframeEncoder, "x");
            iframe.remove();
            return [
                result instanceof Uint8Array,
                result instanceof iframeWindow.Uint8Array,
            ];
        });

        // 6. TextEncoder encode is not tied to the prototype's realm
        testPart(() => {
            const iframe = document.createElement("iframe");
            document.body.appendChild(iframe);
            const iframeWindow = iframe.contentWindow;
            const result = iframeWindow.TextEncoder.prototype.encode.call(new TextEncoder(), "x");
            iframe.remove();
            return [
                result instanceof Uint8Array,
                result instanceof iframeWindow.Uint8Array,
            ];
        });
    });
</script>