Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit d95cb09

Browse files
ThomasBurlesonmmalerba
authored andcommitted
fix(build): remove use of Angular private API (#195)
**flex-layout** uses private APIs from @angular/core. These apis are no longer available in A4. NOTE: `custom-matchers.ts` still uses the private API for Angular 2.x unit testing. Further work is need to identify a reasonable work-around for all versions of Angular. Fixes #193.
1 parent f9f16f5 commit d95cb09

File tree

4 files changed

+122
-33
lines changed

4 files changed

+122
-33
lines changed

src/lib/flexbox/api/base.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
*/
88
import {ElementRef, Renderer, OnDestroy} from '@angular/core';
99

10-
import {__platform_browser_private__} from '@angular/platform-browser';
11-
const getDOM = __platform_browser_private__.getDOM;
12-
1310
import {applyCssPrefixes} from '../../utils/auto-prefixer';
1411

1512
import {ResponsiveActivation, KeyOptions} from '../responsive/responsive-activation';
@@ -92,7 +89,7 @@ export abstract class BaseFxDirective implements OnDestroy {
9289
*/
9390
protected _getDisplayStyle(source?: HTMLElement): string {
9491
let element: HTMLElement = source || this._elementRef.nativeElement;
95-
let value = (element.style as any)['display'] || getDOM().getComputedStyle(element)['display'];
92+
let value = (element.style as any)['display'] || getComputedStyle(element)['display'];
9693
return value.trim();
9794
}
9895

src/lib/flexbox/api/flex.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ import {MockMatchMedia} from '../../media-query/mock/mock-match-media';
1515
import {MatchMedia} from '../../media-query/match-media';
1616
import {FlexLayoutModule} from '../_module';
1717

18-
import {__platform_browser_private__} from '@angular/platform-browser';
19-
import {customMatchers, expect} from '../../utils/testing/custom-matchers';
18+
import {customMatchers, expect } from '../../utils/testing/custom-matchers';
19+
import { _dom as _ } from '../../utils/testing/dom-tools';
20+
2021
import {
2122
makeExpectDOMFrom,
2223
makeExpectDOMForQuery,
@@ -25,7 +26,6 @@ import {
2526
queryFor
2627
} from '../../utils/testing/helpers';
2728

28-
const getDOM = __platform_browser_private__.getDOM;
2929
const isIE = !!document["documentMode"];
3030

3131
describe('flex directive', () => {
@@ -65,11 +65,11 @@ describe('flex directive', () => {
6565
fRef.detectChanges();
6666

6767
let dom = fRef.debugElement.children[0].nativeElement;
68-
let isBox = getDOM().hasStyle(dom, 'box-sizing', 'border-box');
69-
let hasFlex = getDOM().hasStyle(dom, 'flex', '1 1 1e-09px') || // IE
70-
getDOM().hasStyle(dom, 'flex', '1 1 1e-9px') || // Chrome
71-
getDOM().hasStyle(dom, 'flex', '1 1 0.000000001px') || // Safari
72-
getDOM().hasStyle(dom, 'flex', '1 1 0px');
68+
let isBox = _.hasStyle(dom, 'box-sizing', 'border-box');
69+
let hasFlex = _.hasStyle(dom, 'flex', '1 1 1e-09px') || // IE
70+
_.hasStyle(dom, 'flex', '1 1 1e-9px') || // Chrome
71+
_.hasStyle(dom, 'flex', '1 1 0.000000001px') || // Safari
72+
_.hasStyle(dom, 'flex', '1 1 0px');
7373

7474
expect(isBox).toBeTruthy();
7575
expect(hasFlex).toBeTruthy();
@@ -182,7 +182,7 @@ describe('flex directive', () => {
182182
fRef.detectChanges();
183183

184184
let dom = fRef.debugElement.children[0].nativeElement;
185-
let maxWidthStyle = getDOM().getStyle(dom, 'max-width');
185+
let maxWidthStyle = _.getStyle(dom, 'max-width');
186186

187187
expect(maxWidthStyle).toBeFalsy();
188188
});
@@ -198,7 +198,7 @@ describe('flex directive', () => {
198198
fRef.detectChanges();
199199

200200
let dom = fRef.debugElement.children[0].nativeElement;
201-
let minWidthStyle = getDOM().getStyle(dom, 'min-width');
201+
let minWidthStyle = _.getStyle(dom, 'min-width');
202202

203203
expect(minWidthStyle).toBeFalsy();
204204
});

src/lib/utils/testing/custom-matchers.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {__platform_browser_private__} from '@angular/platform-browser';
10-
import {applyCssPrefixes} from '../auto-prefixer';
11-
129
declare var global: any;
13-
14-
const getDOM = __platform_browser_private__.getDOM;
1510
const _global = <any>(typeof window === 'undefined' ? global : window);
1611

12+
import {_dom as _} from './dom-tools';
13+
import {applyCssPrefixes} from '../auto-prefixer';
14+
1715
export const expect: (actual: any) => NgMatchers = <any> _global.expect;
1816

1917
/**
@@ -101,7 +99,7 @@ export const customMatchers: jasmine.CustomMatcherFactories = {
10199
function buildError(isNot: boolean) {
102100
return function (actual: any, className: string) {
103101
return {
104-
pass: getDOM().hasClass(actual, className) == !isNot,
102+
pass: _.hasClass(actual, className) == !isNot,
105103
get message() {
106104
return `
107105
Expected ${actual.outerHTML} ${isNot ? 'not ' : ''}
@@ -118,7 +116,7 @@ export const customMatchers: jasmine.CustomMatcherFactories = {
118116
compare: function (actual: any, styles: {[k: string]: string}|string) {
119117
let allPassed: boolean;
120118
if (typeof styles === 'string') {
121-
allPassed = getDOM().hasStyle(actual, styles);
119+
allPassed = _.hasStyle(actual, styles);
122120
} else {
123121
allPassed = Object.keys(styles).length !== 0;
124122
Object.keys(styles).forEach(prop => {
@@ -149,43 +147,43 @@ export const customMatchers: jasmine.CustomMatcherFactories = {
149147
*/
150148
function hasPrefixedStyles(actual, key, value) {
151149
value = value !== "*" ? value.trim() : undefined;
152-
let hasStyle = getDOM().hasStyle(actual, key, value);
153-
if (!hasStyle) {
150+
let elHasStyle = _.hasStyle(actual, key, value);
151+
if (!elHasStyle) {
154152
let prefixedStyles = applyCssPrefixes({[key]: value});
155153
Object.keys(prefixedStyles).forEach(prop => {
156154
// Search for optional prefixed values
157-
hasStyle = hasStyle || getDOM().hasStyle(actual, prop, prefixedStyles[prop]);
155+
elHasStyle = elHasStyle || _.hasStyle(actual, prop, prefixedStyles[prop]);
158156
});
159157
}
160-
return hasStyle;
158+
return elHasStyle;
161159
}
162160

163161
function elementText(n: any): string {
164162
const hasNodes = (m: any) => {
165-
const children = getDOM().childNodes(m);
163+
const children = _.childNodes(m);
166164
return children && children["length"];
167165
};
168166

169167
if (n instanceof Array) {
170168
return n.map(elementText).join('');
171169
}
172170

173-
if (getDOM().isCommentNode(n)) {
171+
if (_.isCommentNode(n)) {
174172
return '';
175173
}
176174

177-
if (getDOM().isElementNode(n) && getDOM().tagName(n) == 'CONTENT') {
178-
return elementText(Array.prototype.slice.apply(getDOM().getDistributedNodes(n)));
175+
if (_.isElementNode(n) && _.tagName(n) == 'CONTENT') {
176+
return elementText(Array.prototype.slice.apply(_.getDistributedNodes(n)));
179177
}
180178

181-
if (getDOM().hasShadowRoot(n)) {
182-
return elementText(getDOM().childNodesAsList(getDOM().getShadowRoot(n)));
179+
if (_.hasShadowRoot(n)) {
180+
return elementText(_.childNodesAsList(_.getShadowRoot(n)));
183181
}
184182

185183
if (hasNodes(n)) {
186-
return elementText(getDOM().childNodesAsList(n));
184+
return elementText(_.childNodesAsList(n));
187185
}
188186

189-
return getDOM().getText(n);
187+
return _.getText(n);
190188
}
191189

src/lib/utils/testing/dom-tools.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/**
10+
* Exported DOM accessor utility functions
11+
*/
12+
export const _dom = {
13+
hasStyle,
14+
getDistributedNodes,
15+
getShadowRoot,
16+
getText,
17+
getStyle,
18+
childNodes,
19+
childNodesAsList,
20+
hasClass,
21+
hasShadowRoot,
22+
isCommentNode,
23+
isElementNode,
24+
isPresent,
25+
isShadowRoot,
26+
tagName
27+
};
28+
29+
// ******************************************************************************************
30+
// These functions are cloned from
31+
// * @angular/platform-browser/src/browser/GenericBrowserDomAdapter
32+
// and are to be used ONLY internally in custom-matchers.ts and Unit Tests
33+
// ******************************************************************************************
34+
35+
function getStyle(element: any, stylename: string): string {
36+
return element.style[stylename];
37+
}
38+
39+
function hasStyle(element: any, styleName: string, styleValue: string = null): boolean {
40+
const value = this.getStyle(element, styleName) || '';
41+
return styleValue ? value == styleValue : value.length > 0;
42+
}
43+
44+
function getDistributedNodes(el: HTMLElement): Node[] {
45+
return (<any>el).getDistributedNodes();
46+
}
47+
48+
function getShadowRoot(el: HTMLElement): DocumentFragment {
49+
return (<any>el).shadowRoot;
50+
}
51+
52+
function getText(el: Node): string {
53+
return el.textContent;
54+
}
55+
56+
function childNodesAsList(el: Node): any[] {
57+
const childNodes = el.childNodes;
58+
const res = new Array(childNodes.length);
59+
for (let i = 0; i < childNodes.length; i++) {
60+
res[i] = childNodes[i];
61+
}
62+
return res;
63+
}
64+
65+
function hasClass(element: any, className: string): boolean {
66+
return element.classList.contains(className);
67+
}
68+
69+
function childNodes(el: any): Node[] {
70+
return el.childNodes;
71+
}
72+
73+
function hasShadowRoot(node: any): boolean {
74+
return isPresent(node.shadowRoot) && node instanceof HTMLElement;
75+
}
76+
77+
function isCommentNode(node: Node): boolean {
78+
return node.nodeType === Node.COMMENT_NODE;
79+
}
80+
81+
function isElementNode(node: Node): boolean {
82+
return node.nodeType === Node.ELEMENT_NODE;
83+
}
84+
85+
function isShadowRoot(node: any): boolean {
86+
return node instanceof DocumentFragment;
87+
}
88+
89+
function isPresent(obj: any): boolean {
90+
return obj != null;
91+
}
92+
function tagName(element: any): string {
93+
return element.tagName;
94+
}

0 commit comments

Comments
 (0)