Skip to content

Commit

Permalink
Prevent invalid-regular expression syntax error in Safari < 16.4
Browse files Browse the repository at this point in the history
PR flackr#177 added regular expressions to parse CSSNumericValue objects
using lookbehind. Safari only supports lookbehind starting from
version 16.4 [1]. In earlier versions loading the polyfill causes an
error of the form:

```
SyntaxError: Invalid regular expression: invalid group specifier name
```

Compile RegExp on demand to ensure the polyfill can be loaded without
errors. Since the relevant functions are only called initially when
parsing options, the performance overhead appears limited.

[1] https://caniuse.com/js-regexp-lookbehind
  • Loading branch information
tf committed Jan 8, 2024
1 parent fa54f02 commit 153ed40
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/proxy-cssom.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function installCSSOM() {
*/
function parseCSSMultiplication(str) {
let values = [];
const tokens = str.split(/(?<!\([^\)]*)([*])(?![^\(]*\))/);
const tokens = str.split(new RegExp('(?<!\\([^\\)]*)([*])(?![^\\(]*\\))'));
values.push(parseCSSDivision(tokens.shift()));
while (tokens.length) {
tokens.shift(); // Consume operator '*'
Expand All @@ -113,7 +113,7 @@ export function installCSSOM() {
*/
function parseCSSDivision(str) {
let values = [];
const tokens = str.split(/(?<!\([^\)]*)([/])(?![^\(]*\))/);
const tokens = str.split(new RegExp('(?<!\\([^\\)]*)([/])(?![^\\(]*\\))'));
values.push(parseCSSNumericValue(tokens.shift()));
while (tokens.length) {
tokens.shift(); // Consume operator '/'
Expand All @@ -129,7 +129,7 @@ export function installCSSOM() {
*/
function parseCSSMathSum(str) {
let values = [];
const tokens = str.split(/(?<!\([^\)]*)(\s[+-]\s)(?![^\(]*\))/);
const tokens = str.split(new RegExp('(?<!\\([^\\)]*)(\\s[+-]\\s)(?![^\\(]*\\))'));
values.push(parseCSSMultiplication(tokens.shift()));
while (tokens.length) {
let op = tokens.shift();
Expand Down
2 changes: 1 addition & 1 deletion src/scroll-timeline-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ function parseInset(value) {
// Parse string parts to
if (typeof value === 'string') {
// Split value into separate parts
const stringParts = value.split(/(?<!\([^\)]*)\s(?![^\(]*\))/);
const stringParts = value.split(new RegExp('(?<!\\([^\\)]*)\\s(?![^\\(]*\\))'));
parts = stringParts.map(str => {
if (str.trim() === 'auto') {
return 'auto';
Expand Down

0 comments on commit 153ed40

Please sign in to comment.