Skip to content

Commit 2b9cc85

Browse files
benleshalxhub
authored andcommitted
fix(ivy): Ensure proper namespace is used to create elements in JIT (angular#28144)
PR Close angular#28144
1 parent a58fd21 commit 2b9cc85

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

packages/core/test/linker/integration_spec.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,25 +1852,24 @@ function declareTests(config?: {useJit: boolean}) {
18521852
expect(firstAttribute.namespaceURI).toEqual('http://www.w3.org/1999/xlink');
18531853
});
18541854

1855-
fixmeIvy('FW-811: Align HTML namespaces between Ivy and Render2')
1856-
.it('should support foreignObjects with document fragments', () => {
1857-
TestBed.configureTestingModule({declarations: [MyComp]});
1858-
const template =
1859-
'<svg><foreignObject><xhtml:div><p>Test</p></xhtml:div></foreignObject></svg>';
1860-
TestBed.overrideComponent(MyComp, {set: {template}});
1861-
const fixture = TestBed.createComponent(MyComp);
1855+
it('should support foreignObjects with document fragments', () => {
1856+
TestBed.configureTestingModule({declarations: [MyComp]});
1857+
const template =
1858+
'<svg><foreignObject><xhtml:div><p>Test</p></xhtml:div></foreignObject></svg>';
1859+
TestBed.overrideComponent(MyComp, {set: {template}});
1860+
const fixture = TestBed.createComponent(MyComp);
18621861

1863-
const el = fixture.nativeElement;
1864-
const svg = getDOM().childNodes(el)[0];
1865-
const foreignObject = getDOM().childNodes(svg)[0];
1866-
const p = getDOM().childNodes(foreignObject)[0];
1867-
expect(getDOM().getProperty(<Element>svg, 'namespaceURI'))
1868-
.toEqual('http://www.w3.org/2000/svg');
1869-
expect(getDOM().getProperty(<Element>foreignObject, 'namespaceURI'))
1870-
.toEqual('http://www.w3.org/2000/svg');
1871-
expect(getDOM().getProperty(<Element>p, 'namespaceURI'))
1872-
.toEqual('http://www.w3.org/1999/xhtml');
1873-
});
1862+
const el = fixture.nativeElement;
1863+
const svg = getDOM().childNodes(el)[0];
1864+
const foreignObject = getDOM().childNodes(svg)[0];
1865+
const p = getDOM().childNodes(foreignObject)[0];
1866+
expect(getDOM().getProperty(<Element>svg, 'namespaceURI'))
1867+
.toEqual('http://www.w3.org/2000/svg');
1868+
expect(getDOM().getProperty(<Element>foreignObject, 'namespaceURI'))
1869+
.toEqual('http://www.w3.org/2000/svg');
1870+
expect(getDOM().getProperty(<Element>p, 'namespaceURI'))
1871+
.toEqual('http://www.w3.org/1999/xhtml');
1872+
});
18741873
});
18751874

18761875
describe('attributes', () => {

packages/platform-browser/src/dom/dom_renderer.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ class DefaultDomRenderer2 implements Renderer2 {
111111

112112
createElement(name: string, namespace?: string): any {
113113
if (namespace) {
114-
return document.createElementNS(NAMESPACE_URIS[namespace], name);
114+
// In cases where Ivy (not ViewEngine) is giving us the actual namespace, the look up by key
115+
// will result in undefined, so we just return the namespace here.
116+
return document.createElementNS(NAMESPACE_URIS[namespace] || namespace, name);
115117
}
116118

117119
return document.createElement(name);
@@ -154,6 +156,8 @@ class DefaultDomRenderer2 implements Renderer2 {
154156
setAttribute(el: any, name: string, value: string, namespace?: string): void {
155157
if (namespace) {
156158
name = `${namespace}:${name}`;
159+
// TODO(benlesh): Ivy may cause issues here because it's passing around
160+
// full URIs for namespaces, therefore this lookup will fail.
157161
const namespaceUri = NAMESPACE_URIS[namespace];
158162
if (namespaceUri) {
159163
el.setAttributeNS(namespaceUri, name, value);
@@ -167,10 +171,15 @@ class DefaultDomRenderer2 implements Renderer2 {
167171

168172
removeAttribute(el: any, name: string, namespace?: string): void {
169173
if (namespace) {
174+
// TODO(benlesh): Ivy may cause issues here because it's passing around
175+
// full URIs for namespaces, therefore this lookup will fail.
170176
const namespaceUri = NAMESPACE_URIS[namespace];
171177
if (namespaceUri) {
172178
el.removeAttributeNS(namespaceUri, name);
173179
} else {
180+
// TODO(benlesh): Since ivy is passing around full URIs for namespaces
181+
// this could result in properties like `http://www.w3.org/2000/svg:cx="123"`,
182+
// which is wrong.
174183
el.removeAttribute(`${namespace}:${name}`);
175184
}
176185
} else {

0 commit comments

Comments
 (0)