rodneyrehm / viewport-units-buggyfill

Making viewport units (vh|vw|vmin|vmax) work properly in Mobile Safari.
MIT License
1.6k stars 151 forks source link

viewportUnitExpression is matching base64 images #104

Open qq191734369 opened 5 years ago

qq191734369 commented 5 years ago

Hello, I have a problem that the Regx can match 'vh vw' in the base64 image url, for example: base64 So, the image can't be displayed. I have seen the issue #34 and #86. But the Latest code has not fixed this bug. I have an solution that can solve this problem: imageurl test Is this the best way ?...

rodneyrehm commented 5 years ago

Hey there! Sorry for the late reply :(

Your approach would break for background: url(data:image…) 10vw 10vh no-repeat for the same reasons #86 does. We need to make sure that the 123vw is actually part of the url. The value might be pretty complex. However, crafting a regular expression that can do this without being prone to catastrophic backtracking isn't easy.

That said, I wonder if this shouldn't be done in replaceValues() anyway, something along the lines of:

'url(hello world) world'.replace(/(world)/g, (match, captureGroup, offset, text) => {
  var inUnclosedUrl = text.slice(0, offset).match(/url\s*\([^)]*$/);
  if (inUnclosedUrl) {
    return match;
  } else {
    return 'mars';
  }
})

// "url(hello world) mars"