Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uniq-ification of strings that might contain non-UTF-16 chars #87

Merged
merged 1 commit into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ function groupTextsByFontFamilyProps(
const fontFamilies = new Set(
textsPropsArray.map(obj => obj.props['font-family'])
);
const pageText = _.uniq(texts.join(''))
.sort()
.join('');

const pageText = [...new Set([...texts.join('')])].sort().join('');

let smallestOriginalSize;
let smallestOriginalFormat;
Expand Down Expand Up @@ -750,7 +749,9 @@ async function subsetFonts(
// Merge subset values, unique glyphs, sort
for (const htmlAssetTextWithProps of htmlAssetTextsWithProps) {
for (const fontUsage of htmlAssetTextWithProps.fontUsages) {
fontUsage.text = _.uniq(globalFontUsage[fontUsage.fontUrl].join(''))
fontUsage.text = [
...new Set([...globalFontUsage[fontUsage.fontUrl].join('')])
]
.sort()
.join('');
}
Expand Down
23 changes: 23 additions & 0 deletions test/subsetFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4307,6 +4307,29 @@ describe('subsetFonts', function() {
]);
});
});

describe('with two pages that have different non-UTF-16 characters', function() {
it('should not break when combining the characters', async function() {
const assetGraph = new AssetGraph({
root: pathModule.resolve(__dirname, '../testdata/subsetFonts/emojis/')
});
await assetGraph.loadAssets(['index-1.html', 'index-2.html']);
await assetGraph.populate();
assetGraph.on('warn', () => {}); // Ignore warning about IBMPlexSans-Regular.woff not containing the emojis
const { fontInfo } = await subsetFonts(assetGraph);
expect(fontInfo, 'to have length', 2);
expect(fontInfo, 'to satisfy', [
{
htmlAsset: /\/index-1.html$/,
fontUsages: [{ pageText: ' 🤗🤞', text: ' 👊🤗🤞' }]
},
{
htmlAsset: /\/index-2\.html$/,
fontUsages: [{ pageText: ' 👊🤗', text: ' 👊🤗🤞' }]
}
]);
});
});
});

describe('with non-truetype fonts in the mix', function() {
Expand Down
Binary file not shown.
22 changes: 22 additions & 0 deletions testdata/subsetFonts/emojis/index-1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
@font-face {
font-family: 'IBM Plex Sans';
font-style: normal;
font-weight: 400;
src: url('IBMPlexSans-Regular.woff') format('woff');
}
body {
font-family: 'IBM Plex Sans', sans-serif;
font-style: normal;
font-weight: 400;
}
</style>
</head>
<body>
🤗🤞
</body>
</html>
23 changes: 23 additions & 0 deletions testdata/subsetFonts/emojis/index-2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<style>
@font-face {
font-family: 'IBM Plex Sans';
font-style: normal;
font-weight: 400;
src: url('IBMPlexSans-Regular.woff') format('woff');
}

body {
font-family: 'IBM Plex Sans', sans-serif;
font-style: normal;
font-weight: 400;
}
</style>
</head>
<body>
👊🤗
</body>
</html>