Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
34 changes: 34 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,40 @@ export function registerElement(
continue;
} else {
window.customElements.define(externalName, elementClass);

if (elementName === "ruffle-embed") {
const orig = Object.getOwnPropertyDescriptor(
Document.prototype,
"embeds",
);

if (orig && orig.get) {
Object.defineProperty(Document.prototype, "embeds", {
get() {
const nodes = this.querySelectorAll(
"embed, ruffle-embed",
);

const list = Array.from(nodes) as Element[];
(list as unknown as HTMLCollection).item = (
i: number,
): Element | null => list[i] ?? null;

(list as unknown as HTMLCollection).namedItem =
(name: string): Element | null =>
list.find((el) => {
const htmlEl = el as HTMLElement;
return (
htmlEl.getAttribute("name") ===
name || htmlEl.id === name
);
}) ?? null;

return list;
},
});
}
}
}

privateRegistry[elementName] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<li>There are 3 embeds for document.embeds</li>
<li>There are 2 embeds for document.embeds</li>
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>
55 changes: 55 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,55 @@
import { injectRuffleAndWait, openTest, playAndMonitor } from "../../utils.js";
import { use, expect } from "chai";
import chaiHtml from "chai-html";
import fs from "fs";

use(chaiHtml);

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

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?

await browser.execute(() => {
function countDocumentEmbeds() {
const output = document.getElementById("output");
const els = document.embeds;
const len = document.createElement("li");
if (els && "length" in els && els.length) {
len.textContent = `There are ${els.length} embeds for document.embeds`;
}

output?.appendChild(len);
}

countDocumentEmbeds();
const emb1 = document.getElementById("emb1");
if (emb1) {
emb1.remove();
}
countDocumentEmbeds();
});
const actual = await browser
.$("#output")
.getHTML({ includeSelectorTag: false, pierceShadowRoot: false });
const expected = fs.readFileSync(
`${import.meta.dirname}/expected.html`,
"utf8",
);
expect(actual).html.to.equal(expected);
});
});