Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit c38f458

Browse files
committed
feat(css_shim): implement polyfill-unscoped-next-selector
1 parent 7a745d5 commit c38f458

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

lib/core_dom/css_shim.dart

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,23 @@ String shimCssText(String css, String tag) =>
3838
* * `:host(.x)`
3939
*
4040
* When the shim is not powerful enough, you can fall back on the polyfill-next-selector
41-
* directive.
41+
* and the polyfill-unscoped-next-selector directives.
4242
*
4343
* polyfill-next-selector {content: 'x > y'}
4444
* z {}
4545
*
4646
* Becomes:
4747
*
48-
* x[tag] > y[tag]
48+
* x[tag] > y[tag] {}
49+
*
50+
* And:
51+
*
52+
* polyfill-unscoped-next-selector {content: 'x > y'}
53+
* z {}
54+
*
55+
* Becomes:
56+
*
57+
* x > y {}
4958
*
5059
* See http://www.polymer-project.org/docs/polymer/styling.html#at-polyfill
5160
*
@@ -64,6 +73,16 @@ class _CssShim {
6473
caseSensitive: false,
6574
multiLine: true
6675
);
76+
static final RegExp POLYFILL_UNSCOPED_NEXT_SELECTOR_DIRECTIVE = new RegExp(
77+
r"polyfill-unscoped-next-selector"
78+
r"[^}]*"
79+
r"content\:[\s]*"
80+
r"'([^']*)'"
81+
r"[^}]*}"
82+
r"([^{]*)",
83+
caseSensitive: false,
84+
multiLine: true
85+
);
6786
static final int NEXT_SELECTOR_CONTENT = 1;
6887

6988
static final String HOST_TOKEN = '-host-element';
@@ -79,6 +98,8 @@ class _CssShim {
7998
static final RegExp COLON_HOST = new RegExp('($HOST_TOKEN$PAREN_SUFFIX',
8099
caseSensitive: false, multiLine: true);
81100

101+
static final List<String> DO_NOT_SCOPE = ['polyfill-unscoped-next-selector'];
102+
82103
final String tag;
83104
final String attr;
84105

@@ -88,12 +109,16 @@ class _CssShim {
88109
String shimCssText(String css) {
89110
final preprocessed = convertColonHost(applyPolyfillNextSelectorDirective(css));
90111
final rules = cssToRules(preprocessed);
91-
return scopeRules(rules);
112+
final scoped = scopeRules(rules);
113+
return applyPolyfillUnscopedNextSelectorDirective(scoped);
92114
}
93115

94116
String applyPolyfillNextSelectorDirective(String css) =>
95117
css.replaceAllMapped(POLYFILL_NEXT_SELECTOR_DIRECTIVE, (m) => m[NEXT_SELECTOR_CONTENT]);
96118

119+
String applyPolyfillUnscopedNextSelectorDirective(String css) =>
120+
css.replaceAllMapped(POLYFILL_UNSCOPED_NEXT_SELECTOR_DIRECTIVE, (m) => m[NEXT_SELECTOR_CONTENT]);
121+
97122
String convertColonHost(String css) {
98123
css = css.replaceAll(":host", HOST_TOKEN);
99124

@@ -174,7 +199,10 @@ class _CssShim {
174199
}
175200

176201
String insertAttrSuffixIntoSelectorPart(String p) {
177-
final shouldInsert = p.isNotEmpty && !SELECTOR_SPLITS.contains(p) && !p.contains(attr);
202+
final shouldInsert = p.isNotEmpty &&
203+
!SELECTOR_SPLITS.contains(p) &&
204+
!p.contains(attr) &&
205+
!DO_NOT_SCOPE.contains(p);
178206
return shouldInsert ? insertAttr(p) : p;
179207
}
180208

test/core_dom/css_shim_spec.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,14 @@ main() {
7575
expect(s(':host(.x,.y) {}', "a")).toEqual('a.x, a.y {}');
7676
});
7777

78-
it("should insert directives", () {
78+
it("should support polyfill-next-selector", () {
7979
var css = s("polyfill-next-selector {content: 'x > y'} z {}", "a");
8080
expect(css).toEqual('x[a]>y[a] {}');
8181
});
82+
83+
it("should support polyfill-unscoped-next-selector", () {
84+
var css = s("polyfill-unscoped-next-selector {content: 'x > y'} z {}", "a");
85+
expect(css).toEqual('x > y{}');
86+
});
8287
});
8388
}

0 commit comments

Comments
 (0)