Skip to content

Commit

Permalink
Use the font-family library to parse font-family property in postcss ast
Browse files Browse the repository at this point in the history
Prevents the backslash from being kept in "font-family: Font Awesome\ 5 Free"

Fixes #128
  • Loading branch information
papandreou committed Dec 12, 2020
1 parent 35bed43 commit bc19327
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ function groupTextsByFontFamilyProps(
if (family === undefined) {
return [];
}

// Find all the families in the traced font-family that we have @font-face declarations for:
const families = fontFamily
.parse(family)
Expand Down Expand Up @@ -746,7 +745,9 @@ async function subsetFonts(
node.walkDecls((declaration) => {
const propName = declaration.prop.toLowerCase();
if (propName === 'font-family') {
fontFaceDeclaration[propName] = unquote(declaration.value);
fontFaceDeclaration[propName] = fontFamily.parse(
declaration.value
)[0];
} else {
fontFaceDeclaration[propName] = declaration.value;
}
Expand Down Expand Up @@ -1273,7 +1274,7 @@ These glyphs are used on your site, but they don't exist in the font you applied
}

if (propName === 'font-family') {
name = unquote(value);
name = fontFamily.parse(value)[0];
} else if (propName === 'src') {
const fontRelations = cssAsset.outgoingRelations.filter(
(relation) => relation.node === rule
Expand Down Expand Up @@ -1577,7 +1578,9 @@ These glyphs are used on your site, but they don't exist in the font you applied
const fontFamilies = cssListHelpers.splitByCommas(cssRule.value);
for (let i = 0; i < fontFamilies.length; i += 1) {
const subsetFontFamily =
webfontNameMap[unquote(fontFamilies[i]).toLowerCase()];
webfontNameMap[
fontFamily.parse(fontFamilies[i])[0].toLowerCase()
];
if (subsetFontFamily && !fontFamilies.includes(subsetFontFamily)) {
fontFamilies.splice(
i,
Expand Down
24 changes: 24 additions & 0 deletions test/subsetFonts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const expect = require('unexpected')
.clone()
.use(require('unexpected-sinon'))
.use(require('unexpected-set'))
.use(require('assetgraph/test/unexpectedAssetGraph'));

const AssetGraph = require('assetgraph');
Expand Down Expand Up @@ -4570,6 +4571,29 @@ describe('subsetFonts', function () {
await assetGraph.populate();
await subsetFonts(assetGraph);
});

it('should handle escaped characters in font-family', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'../testdata/subsetFonts/font-family-with-escape/'
),
});
const [htmlAsset] = await assetGraph.loadAssets('index.html');
await assetGraph.populate();
const { fontInfo } = await subsetFonts(assetGraph);
expect(fontInfo, 'to satisfy', [
{ fontUsages: [{ fontFamilies: new Set(['Font Awesome 5 Free']) }] },
]);
expect(
htmlAsset.text,
'to contain',
"font-family: 'Font Awesome 5 Free__subset', Font Awesome\\ 5 Free;"
).and(
'to contain',
"font: 12px 'Font Awesome 5 Free__subset', 'Font Awesome 5 Free'"
);
});
});

describe('with non-truetype fonts in the mix', function () {
Expand Down
Binary file not shown.
25 changes: 25 additions & 0 deletions testdata/subsetFonts/font-family-with-escape/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: Font Awesome\ 5 Free;
font-style: normal;
font-weight: 400;
src: url(OpenSans.ttf) format('truetype');
}

.foo {
font-family: Font Awesome\ 5 Free;
}
.bar {
/* The bareword version doesn't work due to /~https://github.com/bramstein/css-font-parser/issues/6 */
font: 12px 'Font Awesome 5 Free';
}
</style>
</head>
<body>
<div class="foo">Hello!</div>
<div class="bar">World!</div>
</body>
</html>

0 comments on commit bc19327

Please sign in to comment.