Skip to content

Commit

Permalink
Merge pull request #902 from assetgraph/test/issue901
Browse files Browse the repository at this point in the history
subsetFonts: Error out when encountering @font-face { unicode-range: ... }
  • Loading branch information
Munter committed Jul 16, 2018
2 parents 4b2e51a + b2f30cd commit 4b835e0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/transforms/subsetFonts.js
Expand Up @@ -575,6 +575,19 @@ module.exports = ({
});

if (accumulatedFontFaceDeclarations.length > 0) {
const seenFontFaceCombos = new Set();
for (const fontFace of accumulatedFontFaceDeclarations) {
const key = `${fontFace['font-family']}/${fontFace['font-style']}/${
fontFace['font-weight']
}}`;
if (seenFontFaceCombos.has(key)) {
throw new Error(
'Multiple @font-face with the same font-family/font-style/font-weight (maybe with different unicode-range?) are not supported'
);
}
seenFontFaceCombos.add(key);
}

const textByProps = getTextByFontProperties(
htmlAsset,
memoizedGetCssRulesByProperty
Expand Down
22 changes: 22 additions & 0 deletions test/transforms/subsetFonts.js
Expand Up @@ -2810,6 +2810,28 @@ describe('transforms/subsetFonts', function() {
});
});

it('should error out on multiple @font-face declarations with the same family/weight/style/stretch', async function() {
httpception();

const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'../../testdata/transforms/subsetFonts/woff2-original/'
)
});
await assetGraph.loadAssets('index.html');
await assetGraph.populate({
followRelations: {
crossorigin: false
}
});
await expect(
assetGraph.subsetFonts(),
'to be rejected with',
'subsetFonts transform: Multiple @font-face with the same font-family/font-style/font-weight (maybe with different unicode-range?) are not supported'
);
});

it('should emit a warning when subsetting invalid fonts', function() {
httpception();

Expand Down
Binary file not shown.
Binary file not shown.
34 changes: 34 additions & 0 deletions testdata/transforms/subsetFonts/woff2-original/index.html
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html><head>
<style>
/* latin-ext */
@font-face {
font-family: 'Roboto Slab';
font-style: normal;
font-weight: 300;
src: local('Roboto Slab Light'), local('RobotoSlab-Light'),
url(BngRUXZYTXPIvIBgJJSb6u9mxLCIwR2oefDofMY.woff2)
format('woff2');
unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB,
U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
font-family: 'Roboto Slab';
font-style: normal;
font-weight: 300;
src: local('Roboto Slab Light'), local('RobotoSlab-Light'),
url(BngRUXZYTXPIvIBgJJSb6u9mxLCGwR2oefDo.woff2)
format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215,
U+FEFF, U+FFFD;
}
* {
font-family: Roboto Slab;
}
</style>
</head>
<body>
<p>Hello, world!</p>
</body></html>

0 comments on commit 4b835e0

Please sign in to comment.