Skip to content

Commit

Permalink
Don't leave stylesheets behind that only contain comments after remov…
Browse files Browse the repository at this point in the history
…ing @font-face rules from them

#160 (comment)
  • Loading branch information
papandreou committed Jul 25, 2022
1 parent 2f19088 commit 3dfd9df
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,12 @@ function getCodepoints(text) {
return codepoints;
}

function cssAssetIsEmpty(cssAsset) {
return cssAsset.parseTree.nodes.every(
(node) => node.type === 'comment' && !node.text.startsWith('!')
);
}

async function subsetFonts(
assetGraph,
{
Expand Down Expand Up @@ -1252,8 +1258,9 @@ These glyphs are used on your site, but they don't exist in the font you applied
cssAsset.markDirty();
maybeEmptyCssAssets.add(cssAsset);
}

for (const cssAsset of maybeEmptyCssAssets) {
if (cssAsset.isEmpty) {
if (cssAssetIsEmpty(cssAsset)) {
for (const incomingRelation of cssAsset.incomingRelations) {
incomingRelation.detach();
}
Expand Down
34 changes: 34 additions & 0 deletions test/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,40 @@ describe('subsetFonts', function () {
});
});

describe('when the stylesheet containing the original @font-face declarations did not contain anything else but a comment', function () {
it('should be removed', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'../testdata/subsetFonts/local-with-no-css-rules-in-font-face-stylesheet-only-comment/'
),
});
const [htmlAsset] = await assetGraph.loadAssets('index.html');
await assetGraph.populate();
await subsetFonts(assetGraph);
expect(htmlAsset.text, 'not to contain', '<style>');
});
});

describe('when the stylesheet containing the original @font-face declarations did not contain anything else but a license comment', function () {
it('should be preserved', async function () {
const assetGraph = new AssetGraph({
root: pathModule.resolve(
__dirname,
'../testdata/subsetFonts/local-with-no-css-rules-in-font-face-stylesheet-only-license-comment/'
),
});
const [htmlAsset] = await assetGraph.loadAssets('index.html');
await assetGraph.populate();
await subsetFonts(assetGraph);
expect(
htmlAsset.text,
'to contain',
'<style>/*! preserve me because of the exclamation mark */'
);
});
});

describe('with unused variants', function () {
it('should provide a @font-face declaration for the __subset version of an unused variant', async function () {
httpception();
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: local('Open Sans Regular'), local('OpenSans-Regular'), url(OpenSans.ttf) format('truetype');
}
/* foo */
</style>
</head>
<body>
<h1 style="font-family: Open Sans">Local font files</h1>
</body>
</html>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<style>/*! preserve me because of the exclamation mark */
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: local('Open Sans Regular'), local('OpenSans-Regular'), url(OpenSans.ttf) format('truetype');
}
</style>
</head>
<body>
<h1 style="font-family: Open Sans">Local font files</h1>
</body>
</html>

0 comments on commit 3dfd9df

Please sign in to comment.