refactor(core): remove unused attribute utilities from DomAdapters (#32278)

PR Close #32278
This commit is contained in:
Kara Erickson 2019-08-22 16:06:15 -07:00 committed by atscott
parent c3f9893d81
commit 4908a5cffc
5 changed files with 17 additions and 35 deletions

View File

@ -2080,9 +2080,8 @@ function declareTests(config?: {useJit: boolean}) {
TestBed.overrideComponent(SomeCmp, {set: {template}}); TestBed.overrideComponent(SomeCmp, {set: {template}});
const fixture = TestBed.createComponent(SomeCmp); const fixture = TestBed.createComponent(SomeCmp);
const useEl = getDOM().firstChild(fixture.nativeElement); const useEl = getDOM().firstChild(fixture.nativeElement) as Element;
expect(getDOM().getAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href')) expect(useEl.getAttributeNS('http://www.w3.org/1999/xlink', 'href')).toEqual('#id');
.toEqual('#id');
}); });
it('should support binding to attributes with namespace', () => { it('should support binding to attributes with namespace', () => {
@ -2092,19 +2091,17 @@ function declareTests(config?: {useJit: boolean}) {
const fixture = TestBed.createComponent(SomeCmp); const fixture = TestBed.createComponent(SomeCmp);
const cmp = fixture.componentInstance; const cmp = fixture.componentInstance;
const useEl = getDOM().firstChild(fixture.nativeElement); const useEl = getDOM().firstChild(fixture.nativeElement) as Element;
cmp.value = '#id'; cmp.value = '#id';
fixture.detectChanges(); fixture.detectChanges();
expect(getDOM().getAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href')) expect(useEl.getAttributeNS('http://www.w3.org/1999/xlink', 'href')).toEqual('#id');
.toEqual('#id');
cmp.value = null; cmp.value = null;
fixture.detectChanges(); fixture.detectChanges();
expect(getDOM().hasAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href')) expect(useEl.hasAttributeNS('http://www.w3.org/1999/xlink', 'href')).toEqual(false);
.toEqual(false);
}); });
}); });
} }

View File

@ -258,27 +258,13 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
return styleValue ? value == styleValue : value.length > 0; return styleValue ? value == styleValue : value.length > 0;
} }
tagName(element: any): string { return element.tagName; } tagName(element: any): string { return element.tagName; }
attributeMap(element: any): Map<string, string> {
const res = new Map<string, string>();
const elAttrs = element.attributes;
for (let i = 0; i < elAttrs.length; i++) {
const attrib = elAttrs.item(i);
res.set(attrib.name, attrib.value);
}
return res;
}
hasAttribute(element: Element, attribute: string): boolean { hasAttribute(element: Element, attribute: string): boolean {
return element.hasAttribute(attribute); return element.hasAttribute(attribute);
} }
hasAttributeNS(element: Element, ns: string, attribute: string): boolean {
return element.hasAttributeNS(ns, attribute);
}
getAttribute(element: Element, attribute: string): string|null { getAttribute(element: Element, attribute: string): string|null {
return element.getAttribute(attribute); return element.getAttribute(attribute);
} }
getAttributeNS(element: Element, ns: string, name: string): string|null {
return element.getAttributeNS(ns, name);
}
setAttribute(element: Element, name: string, value: string) { element.setAttribute(name, value); } setAttribute(element: Element, name: string, value: string) { element.setAttribute(name, value); }
setAttributeNS(element: Element, ns: string, name: string, value: string) { setAttributeNS(element: Element, ns: string, name: string, value: string) {
element.setAttributeNS(ns, name, value); element.setAttributeNS(ns, name, value);
@ -370,9 +356,6 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
setData(element: Element, name: string, value: string) { setData(element: Element, name: string, value: string) {
this.setAttribute(element, 'data-' + name, value); this.setAttribute(element, 'data-' + name, value);
} }
getData(element: Element, name: string): string|null {
return this.getAttribute(element, 'data-' + name);
}
getComputedStyle(element: any): any { return getComputedStyle(element); } getComputedStyle(element: any): any { return getComputedStyle(element); }
// TODO(tbosch): move this into a separate environment class once we have it // TODO(tbosch): move this into a separate environment class once we have it
supportsWebAnimation(): boolean { supportsWebAnimation(): boolean {

View File

@ -113,11 +113,8 @@ export abstract class DomAdapter {
abstract getStyle(element: any, styleName: string): string; abstract getStyle(element: any, styleName: string): string;
abstract hasStyle(element: any, styleName: string, styleValue?: string): boolean; abstract hasStyle(element: any, styleName: string, styleValue?: string): boolean;
abstract tagName(element: any): string; abstract tagName(element: any): string;
abstract attributeMap(element: any): Map<string, string>;
abstract hasAttribute(element: any, attribute: string): boolean; abstract hasAttribute(element: any, attribute: string): boolean;
abstract hasAttributeNS(element: any, ns: string, attribute: string): boolean;
abstract getAttribute(element: any, attribute: string): string|null; abstract getAttribute(element: any, attribute: string): string|null;
abstract getAttributeNS(element: any, ns: string, attribute: string): string|null;
abstract setAttribute(element: any, name: string, value: string): any; abstract setAttribute(element: any, name: string, value: string): any;
abstract setAttributeNS(element: any, ns: string, name: string, value: string): any; abstract setAttributeNS(element: any, ns: string, name: string, value: string): any;
abstract removeAttribute(element: any, attribute: string): any; abstract removeAttribute(element: any, attribute: string): any;
@ -150,7 +147,6 @@ export abstract class DomAdapter {
abstract getUserAgent(): string; abstract getUserAgent(): string;
abstract setData(element: any, name: string, value: string): any; abstract setData(element: any, name: string, value: string): any;
abstract getComputedStyle(element: any): any; abstract getComputedStyle(element: any): any;
abstract getData(element: any, name: string): string|null;
abstract supportsWebAnimation(): boolean; abstract supportsWebAnimation(): boolean;
abstract performanceNow(): number; abstract performanceNow(): number;
abstract getAnimationPrefix(): string; abstract getAnimationPrefix(): string;

View File

@ -107,6 +107,16 @@ export function normalizeCSS(css: string): string {
.replace(/\[(.+)=([^"\]]+)\]/g, (...match: string[]) => `[${match[1]}="${match[2]}"]`); .replace(/\[(.+)=([^"\]]+)\]/g, (...match: string[]) => `[${match[1]}="${match[2]}"]`);
} }
function getAttributeMap(element: any): Map<string, string> {
const res = new Map<string, string>();
const elAttrs = element.attributes;
for (let i = 0; i < elAttrs.length; i++) {
const attrib = elAttrs.item(i);
res.set(attrib.name, attrib.value);
}
return res;
}
const _selfClosingTags = ['br', 'hr', 'input']; const _selfClosingTags = ['br', 'hr', 'input'];
export function stringifyElement(el: any /** TODO #9100 */): string { export function stringifyElement(el: any /** TODO #9100 */): string {
let result = ''; let result = '';
@ -117,7 +127,7 @@ export function stringifyElement(el: any /** TODO #9100 */): string {
result += `<${tagName}`; result += `<${tagName}`;
// Attributes in an ordered way // Attributes in an ordered way
const attributeMap = getDOM().attributeMap(el); const attributeMap = getAttributeMap(el);
const sortedKeys = Array.from(attributeMap.keys()).sort(); const sortedKeys = Array.from(attributeMap.keys()).sort();
for (const key of sortedKeys) { for (const key of sortedKeys) {
const lowerCaseKey = key.toLowerCase(); const lowerCaseKey = key.toLowerCase();

View File

@ -117,11 +117,8 @@ export class WorkerDomAdapter extends DomAdapter {
throw 'not implemented'; throw 'not implemented';
} }
tagName(element: any): string { throw 'not implemented'; } tagName(element: any): string { throw 'not implemented'; }
attributeMap(element: any): Map<string, string> { throw 'not implemented'; }
hasAttribute(element: any, attribute: string): boolean { throw 'not implemented'; } hasAttribute(element: any, attribute: string): boolean { throw 'not implemented'; }
hasAttributeNS(element: any, ns: string, attribute: string): boolean { throw 'not implemented'; }
getAttribute(element: any, attribute: string): string { throw 'not implemented'; } getAttribute(element: any, attribute: string): string { throw 'not implemented'; }
getAttributeNS(element: any, ns: string, attribute: string): string { throw 'not implemented'; }
setAttribute(element: any, name: string, value: string) { throw 'not implemented'; } setAttribute(element: any, name: string, value: string) { throw 'not implemented'; }
setAttributeNS(element: any, ns: string, name: string, value: string) { throw 'not implemented'; } setAttributeNS(element: any, ns: string, name: string, value: string) { throw 'not implemented'; }
removeAttribute(element: any, attribute: string) { throw 'not implemented'; } removeAttribute(element: any, attribute: string) { throw 'not implemented'; }
@ -154,7 +151,6 @@ export class WorkerDomAdapter extends DomAdapter {
getUserAgent(): string { return 'Fake user agent'; } getUserAgent(): string { return 'Fake user agent'; }
setData(element: any, name: string, value: string) { throw 'not implemented'; } setData(element: any, name: string, value: string) { throw 'not implemented'; }
getComputedStyle(element: any): any { throw 'not implemented'; } getComputedStyle(element: any): any { throw 'not implemented'; }
getData(element: any, name: string): string { throw 'not implemented'; }
performanceNow(): number { throw 'not implemented'; } performanceNow(): number { throw 'not implemented'; }
getAnimationPrefix(): string { throw 'not implemented'; } getAnimationPrefix(): string { throw 'not implemented'; }
getTransitionEnd(): string { throw 'not implemented'; } getTransitionEnd(): string { throw 'not implemented'; }