Comments (4)
MDN has some details about the flexible varaitions that the CSS url()
supports.
Based on the description and some googling, I'm not sure that this formal spec can be succinctly represented by a single regex.
Nearly all answers and examples for this specific parsing task that I can find use a naive variation of what this library is already doing.
from fontaine.
I’ve played with bringing in lightningcss and using a url visitor to aggregate all valid urls in a font face rule instead of the URL_RE behavior
I believe it works well, except, it will behave differently for the existing test case of invalid syntax on the family name when the quotes aren’t closed properly.
I’ll look to open a PR and possibly hide it behind a feature flag
from fontaine.
Using lightningcss to parse the @font-face rules definitely works, however, it would be in a stricter, more standards compliant way.
This means potential behavioral differences to how this library currently behaves, which is using some relatively naive regexes that make it usually do the right thing even when it's presented with relatively or very invalid @font-face
rule definitions....
Happy to pursue this avenue if there's interest, otherwise, maybe a URL_RE
update with a better regex (but still imperfect) would be sufficient
from fontaine.
This may be a good regex to use for the URL_RE
in lieu of using a full blown CSS AST parser:
url\((?:(?:"((?:\\.|[^"\\])*)")|(?:'((?:\\.|[^'\\])*)')|((?:[^'")\\]*(?:\\.[^'")\\]*)*)))\)
Seems to work reasonably well and accounts for most of the ways a valid CSS url()
can be specified, including unquoted URLs with escaped parens, or quoted URLs with parens (no escaping needed, as per spec)
Works with the following input URLs
url("https://somesite.com/static/ok.ttf")
url("http://somesite.com/static/ok.ttf")
url("http://www.somesite.com:8080/static/ok.woff2")
url("https://www.somesite.com:443/static/ok.woff2")
url("https://ok.)woff")
url('https://somesite.com/static/ok.ttf')
url('http://somesite.com/static/ok.ttf')
url('http://www.somesite.com:8080/static/ok.woff2')
url('https://www.somesite.com:443/static/ok.woff2')
url('https://ok.)woff')
url(https://somesite.com/static/ok.ttf)
url(http://somesite.com/static/ok.ttf)
url(http://www.somesite.com:8080/static/ok.woff2)
url(https://www.somesite.com:443/static/ok.woff2)
url(https://ok.\)woff)
url(ok)
url("ok")
url('ok')
url('is )ok.woff')
url(what\ ablut)
url(isthis\)ok.woff)
url(../node_modules/some-font/with\ \(parens\)/ok.woff)
url("../node_modules/some-font-with (parens)/ok.woff")
url('../node_modules/some-font-with (parens)/ok.woff')
url(data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o\as+fa==)
See https://regex101.com/r/7eVGaW/2
from fontaine.
Related Issues (20)
- Need help configuring vite.config.mjs
- getMetricsForFamily returns null for font families containing multiple spaces (e.g., "IBM Plex Mono")
- getMetricsForFamily returns null for all fonts on 0.3.0 HOT 3
- CLI/programmatic usage HOT 2
- Customize the font overridden name HOT 3
- Dependency Dashboard
- Gatsby instructions HOT 6
- Fontaine does not process fonts attached via Fontsource HOT 1
- Action Required: Fix Renovate Configuration
- Usage with tailwind HOT 1
- Doesn't seem to work with Webpack
- Maige Usage
- Maige Usage
- Fonts CLS seem to be worse? HOT 1
- expose as postcss plugin HOT 1
- Parse font faces and consider font weight HOT 10
- offer 'minimal' installation
- add framework examples HOT 5
- enable `size-adjust` metric calculation HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fontaine.