@@ -156,6 +156,8 @@ var ShadowCSS = {
156156 this . insertPolyfillDirectives ( def . rootStyles ) ;
157157 this . insertPolyfillRules ( def . rootStyles ) ;
158158 var cssText = this . stylesToShimmedCssText ( def . scopeStyles , name ) ;
159+ // note: we only need to do rootStyles since these are unscoped.
160+ cssText += this . extractPolyfillUnscopedRules ( def . rootStyles ) ;
159161 // provide shimmedStyle for user extensibility
160162 def . shimmedStyle = cssTextToStyle ( cssText ) ;
161163 if ( root ) {
@@ -263,6 +265,37 @@ var ShadowCSS = {
263265 r += cssText . substring ( l , cssText . length ) ;
264266 return r ;
265267 } ,
268+ /*
269+ * Process styles to add rules which will only apply under the polyfill
270+ * and do not process via CSSOM. (CSSOM is destructive to rules on rare
271+ * occasions, e.g. -webkit-calc on Safari.)
272+ * For example, we convert this rule:
273+ *
274+ * (comment start) @polyfill-unscoped-rule menu-item {
275+ * ... } (comment end)
276+ *
277+ * to this:
278+ *
279+ * menu-item {...}
280+ *
281+ **/
282+ extractPolyfillUnscopedRules : function ( styles ) {
283+ var cssText = '' ;
284+ if ( styles ) {
285+ Array . prototype . forEach . call ( styles , function ( s ) {
286+ cssText += this . extractPolyfillUnscopedRulesFromCssText (
287+ s . textContent ) + '\n\n' ;
288+ } , this ) ;
289+ }
290+ return cssText ;
291+ } ,
292+ extractPolyfillUnscopedRulesFromCssText : function ( cssText ) {
293+ var r = '' , l = 0 , matches ;
294+ while ( matches = cssPolyfillUnscopedRuleCommentRe . exec ( cssText ) ) {
295+ r += matches [ 1 ] . slice ( 0 , - 1 ) + '\n\n' ;
296+ }
297+ return r ;
298+ } ,
266299 // apply @host and scope shimming
267300 stylesToShimmedCssText : function ( styles , name ) {
268301 return this . shimAtHost ( styles , name ) + this . shimScoping ( styles , name ) ;
@@ -434,6 +467,7 @@ var hostRuleRe = /@host[^{]*{(([^}]*?{[^{]*?}[\s\S]*?)+)}/gim,
434467 cssCommentRe = / \/ \* [ ^ * ] * \* + ( [ ^ / * ] [ ^ * ] * \* + ) * \/ / gim,
435468 cssPolyfillCommentRe = / \/ \* \s * @ p o l y f i l l ( [ ^ * ] * \* + ( [ ^ / * ] [ ^ * ] * \* + ) * \/ ) ( [ ^ { ] * ?) { / gim,
436469 cssPolyfillRuleCommentRe = / \/ \* \s @ p o l y f i l l - r u l e ( [ ^ * ] * \* + ( [ ^ / * ] [ ^ * ] * \* + ) * ) \/ / gim,
470+ cssPolyfillUnscopedRuleCommentRe = / \/ \* \s @ p o l y f i l l - u n s c o p e d - r u l e ( [ ^ * ] * \* + ( [ ^ / * ] [ ^ * ] * \* + ) * ) \/ / gim,
437471 cssPseudoRe = / : : ( x - [ ^ \s { , ( ] * ) / gim,
438472 selectorReSuffix = '([>\\s~+\[.,{:][\\s\\S]*)?$' ,
439473 hostRe = / @ h o s t / gim,
0 commit comments