OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 (function(scope) { |
| 6 // TODO(terry): Remove shimShadowDOMStyling2 until wrap/unwrap from a |
| 7 // dart:html Element to a JS DOM node is available. |
| 8 /** |
| 9 * Given the content of a STYLE tag and the name of a component shim the CSS |
| 10 * and return the new scoped CSS to replace the STYLE's content. The content |
| 11 * is replaced in Dart's implementation of PolymerElement. |
| 12 */ |
| 13 function shimShadowDOMStyling2(styleContent, name) { |
| 14 if (window.ShadowDOMPolyfill) { |
| 15 var content = this.convertPolyfillDirectives(styleContent, name); |
| 16 |
| 17 // applyShimming calls shimAtHost and shipScoping |
| 18 // shimAtHost code: |
| 19 var r = '', l=0, matches; |
| 20 while (matches = hostRuleRe.exec(content)) { |
| 21 r += content.substring(l, matches.index); |
| 22 r += this.scopeHostCss(matches[1], name); |
| 23 l = hostRuleRe.lastIndex; |
| 24 } |
| 25 r += content.substring(l, content.length); |
| 26 var re = new RegExp('^' + name + selectorReSuffix, 'm'); |
| 27 var atHostCssText = rulesToCss(this.findAtHostRules(cssToRules(r), re)); |
| 28 |
| 29 // shimScoping code: |
| 30 // strip comments for easier processing |
| 31 content = content.replace(cssCommentRe, ''); |
| 32 |
| 33 content = this.convertPseudos(content); |
| 34 var rules = cssToRules(content); |
| 35 var cssText = this.scopeRules(rules, name); |
| 36 |
| 37 return atHostCssText + cssText; |
| 38 } |
| 39 } |
| 40 |
| 41 // Minimal copied code from ShadowCSS, that is not exposed in |
| 42 // PlatForm.ShadowCSS (local code). |
| 43 var hostRuleRe = /@host[^{]*{(([^}]*?{[^{]*?}[\s\S]*?)+)}/gim, |
| 44 cssCommentRe = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//gim, |
| 45 selectorReSuffix = '([>\\s~+\[.,{:][\\s\\S]*)?$'; |
| 46 |
| 47 function cssToRules(cssText) { |
| 48 var style = document.createElement('style'); |
| 49 style.textContent = cssText; |
| 50 document.head.appendChild(style); |
| 51 var rules = style.sheet.cssRules; |
| 52 style.parentNode.removeChild(style); |
| 53 return rules; |
| 54 } |
| 55 |
| 56 function rulesToCss(cssRules) { |
| 57 for (var i=0, css=[]; i < cssRules.length; i++) { |
| 58 css.push(cssRules[i].cssText); |
| 59 } |
| 60 return css.join('\n\n'); |
| 61 } |
| 62 |
| 63 // exports |
| 64 scope.ShadowCSS.shimShadowDOMStyling2 = shimShadowDOMStyling2; |
| 65 })(window.Platform); |
OLD | NEW |