Skip to content

Commit ae1aacb

Browse files
virtulischearon
andcommitted
Allow quotes within font-family names
Add fix to the changelog Use regexp syntax for "string" Co-authored-by: Caleb Hearon <[email protected]>
1 parent 83a5126 commit ae1aacb

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ This release notably changes to using N-API. 🎉
3939
* Fix the improper parsing of rgb functions issue. (#2300)
4040
* Fix issue related to improper parsing of leading and trailing whitespaces in CSS color. (#2301)
4141
* RGB functions should support real numbers now instead of just integers. (#2339)
42+
* Allow alternate or properly escaped quotes *within* font-family names
4243

4344
2.11.2
4445
==================

lib/parse-font.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const styles = 'italic|oblique'
99
const variants = 'small-caps'
1010
const stretches = 'ultra-condensed|extra-condensed|condensed|semi-condensed|semi-expanded|expanded|extra-expanded|ultra-expanded'
1111
const units = 'px|pt|pc|in|cm|mm|%|em|ex|ch|rem|q'
12-
const string = '\'([^\']+)\'|"([^"]+)"|[\\w\\s-]+'
12+
const string = /'((\\'|[^'])+)'|"((\\"|[^"])+)"|[\w\s-]+/.source
1313

1414
// [ [ <‘font-style’> || <font-variant-css21> || <‘font-weight’> || <‘font-stretch’> ]?
1515
// <‘font-size’> [ / <‘line-height’> ]? <‘font-family’> ]
@@ -18,6 +18,9 @@ const weightRe = new RegExp(`(${weights}) +`, 'i')
1818
const styleRe = new RegExp(`(${styles}) +`, 'i')
1919
const variantRe = new RegExp(`(${variants}) +`, 'i')
2020
const stretchRe = new RegExp(`(${stretches}) +`, 'i')
21+
const familyRe = new RegExp(string, 'g')
22+
const unquoteRe = /^['"](.*)['"]$/
23+
const unescapeRe = /\\(['"])/g
2124
const sizeFamilyRe = new RegExp(
2225
`([\\d\\.]+)(${units}) *((?:${string})( *, *(?:${string}))*)`)
2326

@@ -46,6 +49,12 @@ module.exports = str => {
4649
const sizeFamily = sizeFamilyRe.exec(str)
4750
if (!sizeFamily) return // invalid
4851

52+
const names = sizeFamily[3]
53+
.match(familyRe)
54+
// remove actual bounding quotes, if any, unescape any remaining quotes inside
55+
.map(s => s.trim().replace(unquoteRe, '$1').replace(unescapeRe, '$1'))
56+
.filter(s => !!s)
57+
4958
// Default values and required properties
5059
const font = {
5160
weight: 'normal',
@@ -54,7 +63,7 @@ module.exports = str => {
5463
variant: 'normal',
5564
size: parseFloat(sizeFamily[1]),
5665
unit: sizeFamily[2],
57-
family: sizeFamily[3].replace(/["']/g, '').replace(/ *, */g, ',')
66+
family: names.join(',')
5867
}
5968

6069
// Optional, unordered properties.

test/canvas.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ describe('Canvas', function () {
8888
'20px "new century schoolbook", serif',
8989
{ size: 20, unit: 'px', family: 'new century schoolbook,serif' },
9090
'20px "Arial bold 300"', // synthetic case with weight keyword inside family
91-
{ size: 20, unit: 'px', family: 'Arial bold 300', variant: 'normal' }
91+
{ size: 20, unit: 'px', family: 'Arial bold 300', variant: 'normal' },
92+
`50px "Helvetica 'Neue'", "foo \\"bar\\" baz" , "Someone's weird \\'edge\\' case", sans-serif`,
93+
{ size: 50, unit: 'px', family: `Helvetica 'Neue',foo "bar" baz,Someone's weird 'edge' case,sans-serif` }
9294
]
9395

9496
for (let i = 0, len = tests.length; i < len; ++i) {

0 commit comments

Comments
 (0)