OLD | NEW |
| (Empty) |
1 <!-- | |
2 @license | |
3 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | |
4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | |
5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | |
7 Code distributed by Google as part of the polymer project is also | |
8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | |
9 --> | |
10 <link rel="import" href="style-util.html"> | |
11 <link rel="import" href="style-transformer.html"> | |
12 <link rel="import" href="style-defaults.html"> | |
13 | |
14 <!-- | |
15 | |
16 The `x-style` extension of the native `<style>` element allows defining styles | |
17 in the main document that can take advantage of several special features of | |
18 Polymer's styling system: | |
19 | |
20 * Document styles defined in an `x-style` will be shimmed to ensure they do | |
21 not leak into local DOM when running on browsers without non-native Shadow DOM. | |
22 * Shadow DOM-specific `/deep/` and `::shadow` combinators will be shimmed on | |
23 browsers without non-native Shadow DOM. | |
24 * Custom properties used by Polymer's experimental shim for cross-scope styling | |
25 may be defined in an `x-style`. | |
26 | |
27 Example: | |
28 | |
29 ```html | |
30 <!doctype html> | |
31 <html> | |
32 <head> | |
33 <script src="components/webcomponentsjs/webcomponents-lite.js"></script> | |
34 <link rel="import" href="components/polymer/polymer.html"> | |
35 | |
36 <style is="x-style"> | |
37 | |
38 /* Will be prevented from affecting local DOM of Polymer elements */ | |
39 * { | |
40 box-sizing: border-box; | |
41 } | |
42 | |
43 /* Can use /deep/ and ::shadow combinators */ | |
44 body /deep/ .my-special-view::shadow #thing-inside { | |
45 background: yellow; | |
46 } | |
47 | |
48 /* Custom properties that inherit down the document tree may be defined */ | |
49 body { | |
50 --my-toolbar-title-color: green; | |
51 } | |
52 | |
53 </style> | |
54 | |
55 </head> | |
56 <body> | |
57 | |
58 ... | |
59 | |
60 </body> | |
61 </html> | |
62 ``` | |
63 | |
64 Note, all features of `x-style` are available when defining styles as part of Po
lymer elements (e.g. `<style>` elements within `<dom-module>`'s used for definin
g Polymer elements. The `x-style` extension should only be used for defining doc
ument styles, outside of a custom element's local DOM. | |
65 | |
66 --> | |
67 | |
68 <script> | |
69 (function() { | |
70 | |
71 Polymer({ | |
72 | |
73 is: 'x-style', | |
74 extends: 'style', | |
75 | |
76 created: function() { | |
77 var rules = Polymer.StyleUtil.parser.parse(this.textContent); | |
78 this.applyProperties(rules); | |
79 // TODO(sorvell): since custom rules must match directly, they tend to be | |
80 // made with selectors like `*`. | |
81 // We *remove them here* so they don't apply too widely and nerf recalc. | |
82 // This means that normal properties mixe in rules with custom | |
83 // properties will *not* apply. | |
84 var cssText = Polymer.StyleUtil.parser.stringify(rules); | |
85 this.textContent = this.scopeCssText(cssText); | |
86 }, | |
87 | |
88 scopeCssText: function(cssText) { | |
89 return Polymer.Settings.useNativeShadow ? | |
90 cssText : | |
91 Polymer.StyleUtil.toCssText(cssText, function(rule) { | |
92 Polymer.StyleTransformer.rootRule(rule); | |
93 }); | |
94 }, | |
95 | |
96 applyProperties: function(rules) { | |
97 var cssText = ''; | |
98 Polymer.StyleUtil.forEachStyleRule(rules, function(rule) { | |
99 if (rule.cssText.match(CUSTOM_RULE)) { | |
100 // TODO(sorvell): use parser.stringify, it needs an option not to | |
101 // strip custom properties. | |
102 cssText += rule.selector + ' {\n' + rule.cssText + '\n}\n'; | |
103 } | |
104 }); | |
105 if (cssText) { | |
106 Polymer.StyleDefaults.applyCss(cssText); | |
107 } | |
108 } | |
109 | |
110 }); | |
111 | |
112 var CUSTOM_RULE = /--[^;{'"]*\:/; | |
113 | |
114 })(); | |
115 </script> | |
OLD | NEW |