Skip to content

Commit 537f943

Browse files
committed
refactor(compiler): remove direct accesses to DOM
Closes angular#898
1 parent ed2600e commit 537f943

File tree

7 files changed

+72
-29
lines changed

7 files changed

+72
-29
lines changed

modules/angular2/src/core/compiler/shadow_dom_emulation/shadow_css.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -523,28 +523,8 @@ var _polyfillHostRe = RegExpWrapper.create(_polyfillHost, 'im');
523523
var _colonHostRe = RegExpWrapper.create(':host', 'im');
524524
var _colonHostContextRe = RegExpWrapper.create(':host-context', 'im');
525525

526-
function _cssTextToStyle(cssText: string) {
527-
return DOM.createStyleElement(cssText);
528-
}
529-
530526
function _cssToRules(cssText: string) {
531-
var style = _cssTextToStyle(cssText);
532-
DOM.appendChild(DOM.defaultDoc().head, style);
533-
var rules = [];
534-
if (isPresent(style.sheet)) {
535-
// TODO(sorvell): Firefox throws when accessing the rules of a stylesheet
536-
// with an @import
537-
// https://bugzilla.mozilla.org/show_bug.cgi?id=625013
538-
try {
539-
rules = style.sheet.cssRules;
540-
} catch(e) {
541-
//
542-
}
543-
} else {
544-
// console.warn('sheet not found', style);
545-
}
546-
DOM.remove(style);
547-
return rules;
527+
return DOM.cssToRules(cssText);
548528
}
549529

550530
function _withCssRules(cssText: string, callback: Function) {

modules/angular2/src/core/compiler/url_resolver.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export class UrlResolver {
1212

1313
resolve(baseUrl: string, url: string): string {
1414
if (isBlank(baseUrl)) {
15-
UrlResolver.a.href = url;
16-
return UrlResolver.a.href;
15+
DOM.resolveAndSetHref(UrlResolver.a, url, null);
16+
return DOM.getHref(UrlResolver.a);
1717
}
1818

1919
if (isBlank(url) || url == '') return baseUrl;
@@ -28,8 +28,8 @@ export class UrlResolver {
2828
return url;
2929
}
3030

31-
UrlResolver.a.href = baseUrl + '/../' + url;
32-
return UrlResolver.a.href;
31+
DOM.resolveAndSetHref(UrlResolver.a, baseUrl, url);
32+
return DOM.getHref(UrlResolver.a);
3333
}
3434
}
3535

modules/angular2/src/dom/browser_adapter.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ library angular.core.facade.dom;
22

33
import 'dart:html';
44
import 'dart:js' show JsObject;
5-
import 'dom_adapter.dart' show setRootDomAdapter, DomAdapter;
5+
import 'dom_adapter.dart' show setRootDomAdapter;
6+
import 'generic_browser_adapter.dart' show GenericBrowserDomAdapter;
67
import '../facade/browser.dart';
78

89
// WARNING: Do not expose outside this class. Parsing HTML using this
@@ -13,7 +14,7 @@ class _IdentitySanitizer implements NodeTreeSanitizer {
1314

1415
final _identitySanitizer = new _IdentitySanitizer();
1516

16-
class BrowserDomAdapter extends DomAdapter {
17+
class BrowserDomAdapter extends GenericBrowserDomAdapter {
1718
static void makeCurrent() {
1819
setRootDomAdapter(new BrowserDomAdapter());
1920
}
@@ -196,4 +197,7 @@ class BrowserDomAdapter extends DomAdapter {
196197
bool isStyleRule(CssRule rule) => rule is CssStyleRule;
197198
bool isMediaRule(CssRule rule) => rule is CssMediaRule;
198199
bool isKeyframesRule(CssRule rule) => rule is CssKeyframesRule;
200+
String getHref(AnchorElement element) {
201+
return element.href;
202+
}
199203
}

modules/angular2/src/dom/browser_adapter.es6

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {List, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
22
import {isPresent} from 'angular2/src/facade/lang';
3-
import {DomAdapter, setRootDomAdapter} from './dom_adapter';
3+
import {setRootDomAdapter} from './dom_adapter';
4+
import {GenericBrowserDomAdapter} from './generic_browser_adapter';
45

56
var _attrToPropMap = {
67
'inner-html': 'innerHTML',
78
'readonly': 'readOnly',
89
'tabindex': 'tabIndex',
910
};
1011

11-
export class BrowserDomAdapter extends DomAdapter {
12+
export class BrowserDomAdapter extends GenericBrowserDomAdapter {
1213
static makeCurrent() {
1314
setRootDomAdapter(new BrowserDomAdapter());
1415
}
@@ -264,4 +265,7 @@ export class BrowserDomAdapter extends DomAdapter {
264265
isKeyframesRule(rule): boolean {
265266
return rule.type === CSSRule.KEYFRAMES_RULE;
266267
}
268+
getHref(el:Element): string {
269+
return el.href;
270+
}
267271
}

modules/angular2/src/dom/dom_adapter.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,13 @@ export class DomAdapter {
240240
isKeyframesRule(rule): boolean {
241241
throw _abstract();
242242
}
243+
getHref(element): string {
244+
throw _abstract();
245+
}
246+
resolveAndSetHref(element, baseUrl:string, href:string) {
247+
throw _abstract();
248+
}
249+
cssToRules(css:string): List {
250+
throw _abstract();
251+
}
243252
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {ABSTRACT} from 'angular2/src/facade/lang';
2+
import {List, ListWrapper} from 'angular2/src/facade/collection';
3+
import {isPresent} from 'angular2/src/facade/lang';
4+
import {DomAdapter} from './dom_adapter';
5+
6+
/**
7+
* Provides DOM operations in any browser environment.
8+
*/
9+
@ABSTRACT()
10+
export class GenericBrowserDomAdapter extends DomAdapter {
11+
resolveAndSetHref(el, baseUrl:string, href:string) {
12+
el.href = href == null ? baseUrl : baseUrl + '/../' + href;
13+
}
14+
cssToRules(css:string): List {
15+
var style = this.createStyleElement(css);
16+
this.appendChild(this.defaultDoc().head, style);
17+
var rules = ListWrapper.create();
18+
if (isPresent(style.sheet)) {
19+
// TODO(sorvell): Firefox throws when accessing the rules of a stylesheet
20+
// with an @import
21+
// https://bugzilla.mozilla.org/show_bug.cgi?id=625013
22+
try {
23+
var rawRules = style.sheet.cssRules;
24+
rules = ListWrapper.createFixedSize(rawRules.length);
25+
for (var i=0; i<rawRules.length; i++) {
26+
rules[i] = rawRules[i];
27+
}
28+
} catch(e) {
29+
//
30+
}
31+
} else {
32+
// console.warn('sheet not found', style);
33+
}
34+
this.remove(style);
35+
return rules;
36+
}
37+
}

modules/angular2/src/dom/html5lib_adapter.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,13 @@ class Html5LibDomAdapter implements DomAdapter {
226226
bool isKeyframesRule(rule) {
227227
throw 'not implemented';
228228
}
229+
String getHref(element) {
230+
throw 'not implemented';
231+
}
232+
void resolveAndSetHref(element, baseUrl, href) {
233+
throw 'not implemented';
234+
}
235+
List cssToRules(String css) {
236+
throw 'not implemented';
237+
}
229238
}

0 commit comments

Comments
 (0)