X Tutup
Skip to content

Commit c6afea6

Browse files
vicbMatias Niemela
authored andcommitted
fix(DomRenderer): correctly handle namespaced attributes
1 parent ce10fe9 commit c6afea6

File tree

12 files changed

+107
-10
lines changed

12 files changed

+107
-10
lines changed

modules/angular2/src/compiler/html_parser.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {HtmlAst, HtmlAttrAst, HtmlTextAst, HtmlElementAst} from './html_ast';
1616
import {Injectable} from 'angular2/src/core/di';
1717
import {HtmlToken, HtmlTokenType, tokenizeHtml} from './html_lexer';
1818
import {ParseError, ParseLocation, ParseSourceSpan} from './parse_util';
19-
import {HtmlTagDefinition, getHtmlTagDefinition, getNsPrefix} from './html_tags';
19+
import {HtmlTagDefinition, getHtmlTagDefinition, getNsPrefix, mergeNsAndName} from './html_tags';
2020

2121
export class HtmlTreeError extends ParseError {
2222
static create(elementName: string, location: ParseLocation, msg: string): HtmlTreeError {
@@ -238,10 +238,6 @@ class TreeBuilder {
238238
}
239239
}
240240

241-
function mergeNsAndName(prefix: string, localName: string): string {
242-
return isPresent(prefix) ? `@${prefix}:${localName}` : localName;
243-
}
244-
245241
function getElementFullName(prefix: string, localName: string,
246242
parentElement: HtmlElementAst): string {
247243
if (isBlank(prefix)) {

modules/angular2/src/compiler/html_tags.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,7 @@ export function splitNsName(elementName: string): string[] {
420420
export function getNsPrefix(elementName: string): string {
421421
return splitNsName(elementName)[0];
422422
}
423+
424+
export function mergeNsAndName(prefix: string, localName: string): string {
425+
return isPresent(prefix) ? `@${prefix}:${localName}` : localName;
426+
}

modules/angular2/src/compiler/template_parser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import {Parser, AST, ASTWithSource} from 'angular2/src/core/change_detection/cha
77
import {TemplateBinding} from 'angular2/src/core/change_detection/parser/ast';
88
import {CompileDirectiveMetadata, CompilePipeMetadata} from './directive_metadata';
99
import {HtmlParser} from './html_parser';
10-
import {splitNsName} from './html_tags';
10+
import {splitNsName, mergeNsAndName} from './html_tags';
1111
import {ParseSourceSpan, ParseError, ParseLocation} from './parse_util';
1212
import {RecursiveAstVisitor, BindingPipe} from 'angular2/src/core/change_detection/parser/ast';
1313

14-
1514
import {
1615
ElementAst,
1716
BoundElementPropertyAst,
@@ -584,6 +583,12 @@ class TemplateParseVisitor implements HtmlAstVisitor {
584583
} else {
585584
if (parts[0] == ATTRIBUTE_PREFIX) {
586585
boundPropertyName = parts[1];
586+
let nsSeparatorIdx = boundPropertyName.indexOf(':');
587+
if (nsSeparatorIdx > -1) {
588+
let ns = boundPropertyName.substring(0, nsSeparatorIdx);
589+
let name = boundPropertyName.substring(nsSeparatorIdx + 1);
590+
boundPropertyName = mergeNsAndName(ns, name);
591+
}
587592
bindingType = PropertyBindingType.Attribute;
588593
} else if (parts[0] == CLASS_PREFIX) {
589594
boundPropertyName = parts[1];

modules/angular2/src/platform/browser/browser_adapter.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,15 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
365365
bool hasAttribute(Element element, String attribute) =>
366366
element.attributes.containsKey(attribute);
367367

368+
bool hasAttributeNS(Element element, String ns, String attribute) =>
369+
element.getAttributeNS(ns, attribute) != null;
370+
368371
String getAttribute(Element element, String attribute) =>
369372
element.getAttribute(attribute);
370373

374+
String getAttributeNS(Element element, String ns, String attribute) =>
375+
element.getAttributeNS(ns, attribute);
376+
371377
void setAttribute(Element element, String name, String value) {
372378
element.setAttribute(name, value);
373379
}
@@ -382,6 +388,10 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
382388
element.attributes.remove(name);
383389
}
384390

391+
void removeAttributeNS(Element element, String ns, String name) {
392+
element.getNamespacedAttributes(ns).remove(name);
393+
}
394+
385395
Node templateAwareRoot(Element el) => el is TemplateElement ? el.content : el;
386396

387397
HtmlDocument createHtmlDocument() =>

modules/angular2/src/platform/browser/browser_adapter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,19 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
225225
return res;
226226
}
227227
hasAttribute(element, attribute: string): boolean { return element.hasAttribute(attribute); }
228+
hasAttributeNS(element, ns: string, attribute: string): boolean {
229+
return element.hasAttributeNS(ns, attribute);
230+
}
228231
getAttribute(element, attribute: string): string { return element.getAttribute(attribute); }
232+
getAttributeNS(element, ns: string, name: string): string {
233+
return element.getAttributeNS(ns, name);
234+
}
229235
setAttribute(element, name: string, value: string) { element.setAttribute(name, value); }
230236
setAttributeNS(element, ns: string, name: string, value: string) {
231237
element.setAttributeNS(ns, name, value);
232238
}
233239
removeAttribute(element, attribute: string) { element.removeAttribute(attribute); }
240+
removeAttributeNS(element, ns: string, name: string) { element.removeAttributeNS(ns, name); }
234241
templateAwareRoot(el): any { return this.isTemplateElement(el) ? this.content(el) : el; }
235242
createHtmlDocument(): HTMLDocument {
236243
return document.implementation.createHTMLDocument('fakeTitle');

modules/angular2/src/platform/dom/dom_adapter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ export abstract class DomAdapter {
9797
abstract tagName(element): string;
9898
abstract attributeMap(element): Map<string, string>;
9999
abstract hasAttribute(element, attribute: string): boolean;
100+
abstract hasAttributeNS(element, ns: string, attribute: string): boolean;
100101
abstract getAttribute(element, attribute: string): string;
102+
abstract getAttributeNS(element, ns: string, attribute: string): string;
101103
abstract setAttribute(element, name: string, value: string);
102104
abstract setAttributeNS(element, ns: string, name: string, value: string);
103105
abstract removeAttribute(element, attribute: string);
106+
abstract removeAttributeNS(element, ns: string, attribute: string);
104107
abstract templateAwareRoot(el);
105108
abstract createHtmlDocument(): HTMLDocument;
106109
abstract defaultDoc(): HTMLDocument;

modules/angular2/src/platform/dom/dom_renderer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,14 @@ export class DomRenderer implements Renderer {
185185
if (isPresent(attrNs)) {
186186
DOM.setAttributeNS(renderElement, attrNs, attributeName, attributeValue);
187187
} else {
188-
DOM.setAttribute(renderElement, nsAndName[1], attributeValue);
188+
DOM.setAttribute(renderElement, attributeName, attributeValue);
189189
}
190190
} else {
191-
DOM.removeAttribute(renderElement, attributeName);
191+
if (isPresent(attrNs)) {
192+
DOM.removeAttributeNS(renderElement, attrNs, nsAndName[1]);
193+
} else {
194+
DOM.removeAttribute(renderElement, attributeName);
195+
}
192196
}
193197
}
194198

@@ -332,4 +336,4 @@ function splitNamespace(name: string): string[] {
332336
}
333337
let match = RegExpWrapper.firstMatch(NS_PREFIX_RE, name);
334338
return [match[1], match[2]];
335-
}
339+
}

modules/angular2/src/platform/server/abstract_html_adapter.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,21 @@ abstract class AbstractHtml5LibAdapter implements DomAdapter {
295295
return element.attributes.keys.any((key) => '$key' == attribute);
296296
}
297297

298+
hasAttributeNS(element, String ns, String attribute) {
299+
throw 'not implemented';
300+
}
301+
298302
getAttribute(element, String attribute) {
299303
// `attributes` keys can be {@link AttributeName}s.
300304
var key = element.attributes.keys.firstWhere((key) => '$key' == attribute,
301305
orElse: () {});
302306
return element.attributes[key];
303307
}
304308

309+
getAttributeNS(element, String ns, String attribute) {
310+
throw 'not implemented';
311+
}
312+
305313
setAttribute(element, String name, String value) {
306314
element.attributes[name] = value;
307315
}
@@ -314,6 +322,10 @@ abstract class AbstractHtml5LibAdapter implements DomAdapter {
314322
element.attributes.remove(attribute);
315323
}
316324

325+
removeAttributeNS(element, String ns, String attribute) {
326+
throw 'not implemented';
327+
}
328+
317329
templateAwareRoot(el) => el;
318330

319331
createHtmlDocument() {

modules/angular2/src/platform/server/parse5_adapter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,13 @@ export class Parse5DomAdapter extends DomAdapter {
429429
hasAttribute(element, attribute: string): boolean {
430430
return element.attribs && element.attribs.hasOwnProperty(attribute);
431431
}
432+
hasAttributeNS(element, ns: string, attribute: string): boolean { throw 'not implemented'; }
432433
getAttribute(element, attribute: string): string {
433434
return element.attribs && element.attribs.hasOwnProperty(attribute) ?
434435
element.attribs[attribute] :
435436
null;
436437
}
438+
getAttributeNS(element, ns: string, attribute: string): string { throw 'not implemented'; }
437439
setAttribute(element, attribute: string, value: string) {
438440
if (attribute) {
439441
element.attribs[attribute] = value;
@@ -448,6 +450,7 @@ export class Parse5DomAdapter extends DomAdapter {
448450
StringMapWrapper.delete(element.attribs, attribute);
449451
}
450452
}
453+
removeAttributeNS(element, ns: string, name: string) { throw 'not implemented'; }
451454
templateAwareRoot(el): any { return this.isTemplateElement(el) ? this.content(el) : el; }
452455
createHtmlDocument(): Document {
453456
var newDoc = treeAdapter.createDocument();

modules/angular2/test/core/linker/integration_spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,48 @@ function declareTests() {
18591859
}));
18601860

18611861
});
1862+
1863+
describe('attributes', () => {
1864+
1865+
it('should support attributes with namespace',
1866+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder,
1867+
async) => {
1868+
tcb.overrideView(SomeCmp, new ViewMetadata({template: '<svg:use xlink:href="#id" />'}))
1869+
.createAsync(SomeCmp)
1870+
.then((fixture) => {
1871+
let useEl = DOM.firstChild(fixture.debugElement.nativeElement);
1872+
expect(DOM.getAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href'))
1873+
.toEqual('#id');
1874+
async.done();
1875+
});
1876+
}));
1877+
1878+
it('should support binding to attributes with namespace',
1879+
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder,
1880+
async) => {
1881+
tcb.overrideView(SomeCmp,
1882+
new ViewMetadata({template: '<svg:use [attr.xlink:href]="value" />'}))
1883+
.createAsync(SomeCmp)
1884+
.then((fixture) => {
1885+
let cmp = fixture.debugElement.componentInstance;
1886+
let useEl = DOM.firstChild(fixture.debugElement.nativeElement);
1887+
1888+
cmp.value = "#id";
1889+
fixture.detectChanges();
1890+
1891+
expect(DOM.getAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href'))
1892+
.toEqual('#id');
1893+
1894+
cmp.value = null;
1895+
fixture.detectChanges();
1896+
1897+
expect(DOM.hasAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href'))
1898+
.toEqual(false);
1899+
1900+
async.done();
1901+
});
1902+
}));
1903+
});
18621904
}
18631905
});
18641906
}
@@ -2438,3 +2480,8 @@ class DirectiveWithPropDecorators {
24382480

24392481
fireEvent(msg) { ObservableWrapper.callEmit(this.event, msg); }
24402482
}
2483+
2484+
@Component({selector: 'some-cmp'})
2485+
class SomeCmp {
2486+
value: any;
2487+
}

0 commit comments

Comments
 (0)
X Tutup