Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions web/packages/core/src/internal/register-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,126 @@ export function registerElement(
continue;
} else {
window.customElements.define(externalName, elementClass);

if (elementName === "ruffle-embed") {
const orig = Object.getOwnPropertyDescriptor(
Document.prototype,
"embeds",
);
if (orig?.get) {
const CACHE_SYM: unique symbol = Symbol(
"ruffle_embeds_cache",
);
interface CachedCollection extends HTMLCollection {
[CACHE_SYM]?: true;
}
Object.defineProperty(Document.prototype, "embeds", {
get(this: Document): CachedCollection {
const existing = (
this as unknown as Record<
symbol,
HTMLCollection
>
Comment on lines +105 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you extract this to a variable?

)[CACHE_SYM];
if (existing) {
return existing;
}

const nodes = (): NodeListOf<Element> =>
this.querySelectorAll(
"embed, ruffle-embed",
);

const base = Object.create(
HTMLCollection.prototype,
) as HTMLCollection;

Object.defineProperty(base, "length", {
enumerable: true,
configurable: true,
get() {
return nodes().length;
},
});

base.item = function (
index: number,
): Element | null {
return nodes()[index] ?? null;
};

base.namedItem = function (
name: string,
): Element | null {
const list = nodes();
for (const el of list) {
const htmlEl = el as HTMLElement;
if (
name &&
(htmlEl.getAttribute("name") ===
name ||
htmlEl.id === name)
) {
return htmlEl;
}
}
return null;
};

(base as Iterable<Element>)[Symbol.iterator] =
function* (): Iterator<Element> {
for (const el of nodes()) {
yield el;
}
};

const proxy = new Proxy(base, {
get(target, prop, receiver) {
if (typeof prop === "string") {
const index = Number(prop);
if (
!Number.isNaN(index) &&
index >= 0
) {
return nodes()[index];
}
}
return Reflect.get(
target,
prop,
receiver,
);
},
has(target, prop) {
if (typeof prop === "string") {
const index = Number(prop);
if (
!Number.isNaN(index) &&
index >= 0
) {
return index < nodes().length;
}
}
return Reflect.has(target, prop);
},
}) as CachedCollection;

proxy[CACHE_SYM] = true;

(
this as unknown as Record<
symbol,
CachedCollection
>
)[CACHE_SYM] = proxy;

return proxy;
},
configurable: true,
enumerable: true,
});
}
}
}

privateRegistry[elementName] = {
Expand Down
16 changes: 16 additions & 0 deletions web/packages/selfhosted/test/polyfill/document_embeds/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>DOCUMENT EMBEDS</title>
</head>

<body>
<div id="test-container">
<embed id="emb1" name="fl" src="/test_assets/example.swf" width="200" height="200"></embed>
<embed id="emb2" name="fl" src="/test_assets/example.swf" width="200" height="200"></embed>
<embed id="emb3" name="fl" src="/test_assets/example.swf" width="200" height="200"></embed>
</div>
<ul id="output"></ul>
</body>
</html>
50 changes: 50 additions & 0 deletions web/packages/selfhosted/test/polyfill/document_embeds/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { injectRuffleAndWait, openTest, playAndMonitor } from "../../utils.js";
import { expect } from "chai";

describe("Document embeds", () => {
it("loads the test", async () => {
await openTest(browser, `polyfill/document_embeds`);
});

it("Accesses the right number of elements with ruffle", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: other cases start with a lowercase letter

async function countDocumentEmbeds() {
return await browser.execute(() => {
return document.embeds?.length ?? 0;
});
}

async function documentEmbedsSelfIdentity() {
return await browser.execute(() => {
return document.embeds === document.embeds;
});
}

async function removeEl(selector: string) {
const el = await $(selector);
await browser.execute((element) => {
element.remove();
}, el);
}

await injectRuffleAndWait(browser);
await playAndMonitor(
browser,
await browser.$("#test-container").$("ruffle-embed#emb1"),
);
await playAndMonitor(
browser,
await browser.$("#test-container").$("ruffle-embed#emb2"),
);
await playAndMonitor(
browser,
await browser.$("#test-container").$("ruffle-embed#emb3"),
);
Comment on lines +30 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those needed? Even with autoplay disabled embeds should work, right?

const documentEmbedsIdentity = await documentEmbedsSelfIdentity();
expect(documentEmbedsIdentity).to.equal(true);
const embeds1 = await countDocumentEmbeds();
expect(embeds1).to.equal(3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think you can inline embeds1, embeds2, and documentEmbedsIdentity, they don't make the code more readable.

await removeEl("#emb1");
const embeds2 = await countDocumentEmbeds();
expect(embeds2).to.equal(2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we dynamically add an embed at the end and check if the number went up?

});
});