From 4908a5cffcc80852fbccd58b21a3a5b3e3e23bf9 Mon Sep 17 00:00:00 2001 From: Kara Erickson Date: Thu, 22 Aug 2019 16:06:15 -0700 Subject: [PATCH] refactor(core): remove unused attribute utilities from DomAdapters (#32278) PR Close #32278 --- packages/core/test/linker/integration_spec.ts | 13 +++++-------- .../src/browser/browser_adapter.ts | 19 +------------------ .../platform-browser/src/dom/dom_adapter.ts | 4 ---- .../testing/src/browser_util.ts | 12 +++++++++++- .../src/web_workers/worker/worker_adapter.ts | 4 ---- 5 files changed, 17 insertions(+), 35 deletions(-) diff --git a/packages/core/test/linker/integration_spec.ts b/packages/core/test/linker/integration_spec.ts index 23065ea86a..67f30b574d 100644 --- a/packages/core/test/linker/integration_spec.ts +++ b/packages/core/test/linker/integration_spec.ts @@ -2080,9 +2080,8 @@ function declareTests(config?: {useJit: boolean}) { TestBed.overrideComponent(SomeCmp, {set: {template}}); const fixture = TestBed.createComponent(SomeCmp); - const useEl = getDOM().firstChild(fixture.nativeElement); - expect(getDOM().getAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href')) - .toEqual('#id'); + const useEl = getDOM().firstChild(fixture.nativeElement) as Element; + expect(useEl.getAttributeNS('http://www.w3.org/1999/xlink', 'href')).toEqual('#id'); }); it('should support binding to attributes with namespace', () => { @@ -2092,19 +2091,17 @@ function declareTests(config?: {useJit: boolean}) { const fixture = TestBed.createComponent(SomeCmp); const cmp = fixture.componentInstance; - const useEl = getDOM().firstChild(fixture.nativeElement); + const useEl = getDOM().firstChild(fixture.nativeElement) as Element; cmp.value = '#id'; fixture.detectChanges(); - expect(getDOM().getAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href')) - .toEqual('#id'); + expect(useEl.getAttributeNS('http://www.w3.org/1999/xlink', 'href')).toEqual('#id'); cmp.value = null; fixture.detectChanges(); - expect(getDOM().hasAttributeNS(useEl, 'http://www.w3.org/1999/xlink', 'href')) - .toEqual(false); + expect(useEl.hasAttributeNS('http://www.w3.org/1999/xlink', 'href')).toEqual(false); }); }); } diff --git a/packages/platform-browser/src/browser/browser_adapter.ts b/packages/platform-browser/src/browser/browser_adapter.ts index 28caa7f589..b2ec2c9f9f 100644 --- a/packages/platform-browser/src/browser/browser_adapter.ts +++ b/packages/platform-browser/src/browser/browser_adapter.ts @@ -258,27 +258,13 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter { return styleValue ? value == styleValue : value.length > 0; } tagName(element: any): string { return element.tagName; } - attributeMap(element: any): Map { - const res = new Map(); - 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 { return element.hasAttribute(attribute); } - hasAttributeNS(element: Element, ns: string, attribute: string): boolean { - return element.hasAttributeNS(ns, attribute); - } getAttribute(element: Element, attribute: string): string|null { 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); } setAttributeNS(element: Element, ns: string, name: string, value: string) { element.setAttributeNS(ns, name, value); @@ -370,9 +356,6 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter { setData(element: Element, name: string, value: string) { 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); } // TODO(tbosch): move this into a separate environment class once we have it supportsWebAnimation(): boolean { diff --git a/packages/platform-browser/src/dom/dom_adapter.ts b/packages/platform-browser/src/dom/dom_adapter.ts index f98752e039..e917c5ef56 100644 --- a/packages/platform-browser/src/dom/dom_adapter.ts +++ b/packages/platform-browser/src/dom/dom_adapter.ts @@ -113,11 +113,8 @@ export abstract class DomAdapter { abstract getStyle(element: any, styleName: string): string; abstract hasStyle(element: any, styleName: string, styleValue?: string): boolean; abstract tagName(element: any): string; - abstract attributeMap(element: any): Map; 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 getAttributeNS(element: any, ns: string, attribute: string): string|null; abstract setAttribute(element: any, name: string, value: string): any; abstract setAttributeNS(element: any, ns: string, name: string, value: string): any; abstract removeAttribute(element: any, attribute: string): any; @@ -150,7 +147,6 @@ export abstract class DomAdapter { abstract getUserAgent(): string; abstract setData(element: any, name: string, value: string): any; abstract getComputedStyle(element: any): any; - abstract getData(element: any, name: string): string|null; abstract supportsWebAnimation(): boolean; abstract performanceNow(): number; abstract getAnimationPrefix(): string; diff --git a/packages/platform-browser/testing/src/browser_util.ts b/packages/platform-browser/testing/src/browser_util.ts index e260633f7a..cb4b61bcd8 100644 --- a/packages/platform-browser/testing/src/browser_util.ts +++ b/packages/platform-browser/testing/src/browser_util.ts @@ -107,6 +107,16 @@ export function normalizeCSS(css: string): string { .replace(/\[(.+)=([^"\]]+)\]/g, (...match: string[]) => `[${match[1]}="${match[2]}"]`); } +function getAttributeMap(element: any): Map { + const res = new Map(); + 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']; export function stringifyElement(el: any /** TODO #9100 */): string { let result = ''; @@ -117,7 +127,7 @@ export function stringifyElement(el: any /** TODO #9100 */): string { result += `<${tagName}`; // Attributes in an ordered way - const attributeMap = getDOM().attributeMap(el); + const attributeMap = getAttributeMap(el); const sortedKeys = Array.from(attributeMap.keys()).sort(); for (const key of sortedKeys) { const lowerCaseKey = key.toLowerCase(); diff --git a/packages/platform-webworker/src/web_workers/worker/worker_adapter.ts b/packages/platform-webworker/src/web_workers/worker/worker_adapter.ts index faf47c3673..58ecba433f 100644 --- a/packages/platform-webworker/src/web_workers/worker/worker_adapter.ts +++ b/packages/platform-webworker/src/web_workers/worker/worker_adapter.ts @@ -117,11 +117,8 @@ export class WorkerDomAdapter extends DomAdapter { throw 'not implemented'; } tagName(element: any): string { throw 'not implemented'; } - attributeMap(element: any): Map { 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'; } - getAttributeNS(element: any, ns: string, attribute: string): 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'; } removeAttribute(element: any, attribute: string) { throw 'not implemented'; } @@ -154,7 +151,6 @@ export class WorkerDomAdapter extends DomAdapter { getUserAgent(): string { return 'Fake user agent'; } setData(element: any, name: string, value: string) { throw 'not implemented'; } getComputedStyle(element: any): any { throw 'not implemented'; } - getData(element: any, name: string): string { throw 'not implemented'; } performanceNow(): number { throw 'not implemented'; } getAnimationPrefix(): string { throw 'not implemented'; } getTransitionEnd(): string { throw 'not implemented'; }