refactor(core): remove testing-only style utils from DomAdapters (#32291)
PR Close #32291
This commit is contained in:

committed by
Miško Hevery

parent
46caf88b2c
commit
ede5786d1e
@ -196,12 +196,8 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
|
||||
getElementsByTagName(element: any, name: string): HTMLElement[] {
|
||||
return element.getElementsByTagName(name);
|
||||
}
|
||||
classList(element: any): any[] { return Array.prototype.slice.call(element.classList, 0); }
|
||||
addClass(element: any, className: string) { element.classList.add(className); }
|
||||
removeClass(element: any, className: string) { element.classList.remove(className); }
|
||||
hasClass(element: any, className: string): boolean {
|
||||
return element.classList.contains(className);
|
||||
}
|
||||
setStyle(element: any, styleName: string, styleValue: string) {
|
||||
element.style[styleName] = styleValue;
|
||||
}
|
||||
@ -210,11 +206,8 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
|
||||
// see https://github.com/angular/angular/issues/7916
|
||||
element.style[stylename] = '';
|
||||
}
|
||||
|
||||
getStyle(element: any, stylename: string): string { return element.style[stylename]; }
|
||||
hasStyle(element: any, styleName: string, styleValue?: string|null): boolean {
|
||||
const value = this.getStyle(element, styleName) || '';
|
||||
return styleValue ? value == styleValue : value.length > 0;
|
||||
}
|
||||
|
||||
getAttribute(element: Element, attribute: string): string|null {
|
||||
return element.getAttribute(attribute);
|
||||
|
@ -80,19 +80,16 @@ export abstract class DomAdapter {
|
||||
abstract getDistributedNodes(el: any): Node[];
|
||||
abstract clone /*<T extends Node>*/ (node: Node /*T*/): Node /*T*/;
|
||||
abstract getElementsByTagName(element: any, name: string): HTMLElement[];
|
||||
abstract classList(element: any): any[];
|
||||
abstract addClass(element: any, className: string): any;
|
||||
abstract removeClass(element: any, className: string): any;
|
||||
abstract hasClass(element: any, className: string): boolean;
|
||||
abstract setStyle(element: any, styleName: string, styleValue: string): any;
|
||||
abstract removeStyle(element: any, styleName: string): any;
|
||||
abstract getStyle(element: any, styleName: string): string;
|
||||
abstract hasStyle(element: any, styleName: string, styleValue?: string): boolean;
|
||||
|
||||
// Used by Meta
|
||||
abstract getAttribute(element: any, attribute: string): string|null;
|
||||
|
||||
// Used by platform-server
|
||||
abstract addClass(element: any, className: string): any;
|
||||
abstract removeClass(element: any, className: string): any;
|
||||
abstract getStyle(element: any, styleName: string): any;
|
||||
abstract setStyle(element: any, styleName: string, styleValue: string): any;
|
||||
abstract removeStyle(element: any, styleName: string): any;
|
||||
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;
|
||||
|
@ -199,3 +199,16 @@ export function setCookie(name: string, value: string) {
|
||||
export function supportsWebAnimation(): boolean {
|
||||
return typeof(<any>Element).prototype['animate'] === 'function';
|
||||
}
|
||||
|
||||
export function hasStyle(element: any, styleName: string, styleValue?: string | null): boolean {
|
||||
const value = element.style[styleName] || '';
|
||||
return styleValue ? value == styleValue : value.length > 0;
|
||||
}
|
||||
|
||||
export function hasClass(element: any, className: string): boolean {
|
||||
return element.classList.contains(className);
|
||||
}
|
||||
|
||||
export function sortedClassList(element: any): any[] {
|
||||
return Array.prototype.slice.call(element.classList, 0).sort();
|
||||
}
|
||||
|
@ -10,7 +10,8 @@
|
||||
import {Type, ɵglobal as global} from '@angular/core';
|
||||
import {ComponentFixture} from '@angular/core/testing';
|
||||
import {By, ɵgetDOM as getDOM} from '@angular/platform-browser';
|
||||
import {isCommentNode} from './browser_util';
|
||||
|
||||
import {hasClass, hasStyle, isCommentNode} from './browser_util';
|
||||
|
||||
|
||||
|
||||
@ -186,7 +187,7 @@ _global.beforeEach(function() {
|
||||
function buildError(isNot: boolean) {
|
||||
return function(actual: any, className: string) {
|
||||
return {
|
||||
pass: getDOM().hasClass(actual, className) == !isNot,
|
||||
pass: hasClass(actual, className) == !isNot,
|
||||
get message() {
|
||||
return `Expected ${actual.outerHTML} ${isNot ? 'not ' : ''}to contain the CSS class "${className}"`;
|
||||
}
|
||||
@ -200,12 +201,11 @@ _global.beforeEach(function() {
|
||||
compare: function(actual: any, styles: {[k: string]: string}|string) {
|
||||
let allPassed: boolean;
|
||||
if (typeof styles === 'string') {
|
||||
allPassed = getDOM().hasStyle(actual, styles);
|
||||
allPassed = hasStyle(actual, styles);
|
||||
} else {
|
||||
allPassed = Object.keys(styles).length !== 0;
|
||||
Object.keys(styles).forEach(prop => {
|
||||
allPassed = allPassed && getDOM().hasStyle(actual, prop, styles[prop]);
|
||||
});
|
||||
Object.keys(styles).forEach(
|
||||
prop => { allPassed = allPassed && hasStyle(actual, prop, styles[prop]); });
|
||||
}
|
||||
|
||||
return {
|
||||
|
Reference in New Issue
Block a user