Skip to content

Commit 0af6009

Browse files
committed
feat(web-components): support foreignObject inside an SVG
1 parent 3d6cb9b commit 0af6009

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/utils/brisa-element/index.test.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -1103,10 +1103,33 @@ describe("utils", () => {
11031103
);
11041104
});
11051105

1106-
it.todo(
1107-
"should SVG work with foreingObject setting correctly the namespace outside the foreingObject node",
1108-
() => {},
1109-
);
1106+
it("should SVG work with foreingObject setting correctly the namespace outside the foreingObject node", () => {
1107+
function SVG({}, { h }: any) {
1108+
return h("svg", { width: "12cm", height: "12cm" }, [
1109+
"foreignObject",
1110+
{ width: "100%", height: "100%" },
1111+
["div", { xmlns: "http://www.w3.org/1999/xhtml" }, "test"],
1112+
]);
1113+
}
1114+
1115+
customElements.define("test-svg", brisaElement(SVG));
1116+
document.body.innerHTML = "<test-svg />";
1117+
1118+
const testSVG = document.querySelector("test-svg") as HTMLElement;
1119+
const svg = testSVG?.shadowRoot?.querySelector("svg") as SVGElement;
1120+
const foreignObject = testSVG?.shadowRoot?.querySelector(
1121+
"foreignObject",
1122+
) as SVGElement;
1123+
const div = testSVG?.shadowRoot?.querySelector("div") as HTMLElement;
1124+
1125+
expect(svg.namespaceURI).toBe("http://www.w3.org/2000/svg");
1126+
expect(foreignObject.namespaceURI).toBe("http://www.w3.org/2000/svg");
1127+
expect(div.namespaceURI).toBe("http://www.w3.org/1999/xhtml");
1128+
1129+
expect(testSVG?.shadowRoot?.innerHTML).toBe(
1130+
'<svg width="12cm" height="12cm"><foreignobject width="100%" height="100%"><div xmlns="http://www.w3.org/1999/xhtml">test</div></foreignobject></svg>',
1131+
);
1132+
});
11101133

11111134
it.todo(
11121135
"should reactively update the DOM after adding a new property to the web-component",

src/utils/brisa-element/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const createElement = (
2626
parent?: HTMLElement | DocumentFragment,
2727
) => {
2828
return tagName === "svg" ||
29-
(parent as HTMLElement)?.namespaceURI === SVG_NAMESPACE
29+
((parent as HTMLElement)?.namespaceURI === SVG_NAMESPACE &&
30+
lowercase((parent as HTMLElement).tagName) !== "foreignobject")
3031
? document.createElementNS(SVG_NAMESPACE, tagName)
3132
: document.createElement(tagName);
3233
};

src/utils/log-table/index.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { describe, it, expect, mock, afterEach } from "bun:test";
22
import logTable from ".";
3+
import getConstants from "../../constants";
34

45
const originalConsoleLog = console.log;
6+
const { LOG_PREFIX } = getConstants();
57

68
describe("utils", () => {
79
afterEach(() => {
810
console.log = originalConsoleLog;
911
});
1012
describe("logTable", () => {
1113
it("should log a table", () => {
12-
const info = "[ \u001B[34minfo\u001B[0m ] ";
14+
const info = LOG_PREFIX.INFO;
1315
const data = [
1416
{ name: "John", age: "23" },
1517
{ name: "Jane", age: "42" },

0 commit comments

Comments
 (0)