Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: repair Safari broken rules * refactor: remove CSSStyleSheet methods that are unsupported in Chrome * test: add tests for Safari broken rules * feat: add support for Safari broken rules for addRule * chore: increase size-limit to 2.5 kB * test: fix tests for IE11 * refactor: use another approach for fixing Safari bug * refactor: simplify approach to fix Safari bug * refactor: use arrow functions * refactor: fix replacement * chore: run build before size-limit * fix: apply broken rule fix only to basic stylesheet * test: use regexp to match content property value with broken rules bug * refactor: simplify pattern building approach & add some docs * refactor: use more correct placeholder matching * refactor: remove unnecessary code * refactor: apply bundle size optimizations
- Loading branch information
Showing
8 changed files
with
218 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module.exports = [ | ||
{ | ||
path: 'dist/adoptedStyleSheets.js', | ||
limit: '2 kB', | ||
limit: '2.5 kB', | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {bootstrapper} from './shared'; | ||
|
||
export const hasBrokenRules = (function () { | ||
const style = bootstrapper.createElement('style'); | ||
style.textContent = '.x{content:"y"}'; | ||
bootstrapper.body.appendChild(style); | ||
|
||
return (style.sheet!.cssRules[0] as CSSStyleRule).style.content !== '"y"'; | ||
})(); | ||
|
||
const brokenRulePatterns = [/content:\s*["']/gm]; | ||
|
||
/** | ||
* Adds a special symbol "%" to the broken rule that forces the internal Safari | ||
* CSS property string converter to add quotes around the value. This function | ||
* should be only used for the internal basic stylesheet hidden in the | ||
* bootstrapper because it pollutes the user content with the placeholder | ||
* symbols. Use the `getCssText` function to remove the placeholder from the | ||
* CSS string. | ||
* | ||
* @param content | ||
*/ | ||
export function fixBrokenRules(content: string): string { | ||
return brokenRulePatterns.reduce( | ||
(acc, pattern) => acc.replace(pattern, '$&%%%'), | ||
content, | ||
); | ||
} | ||
|
||
const placeholderPatterns = [/(content:\s*["'])%%%/gm]; | ||
|
||
/** | ||
* Removes the placeholder added by `fixBrokenRules` function from the received | ||
* rule string. | ||
*/ | ||
export const getCssText = hasBrokenRules | ||
? (rule: CSSRule) => | ||
placeholderPatterns.reduce( | ||
(acc, pattern) => acc.replace(pattern, '$1'), | ||
rule.cssText, | ||
) | ||
: (rule: CSSRule) => rule.cssText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.