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 Font-Face processing #214

Merged
merged 1 commit into from
Sep 1, 2019
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
3 changes: 3 additions & 0 deletions __tests__/purgecssDefault.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ describe('purge methods with files and default extractor', () => {
fontFace: true
}).purge()[0].css
})
it("keep @font-face 'Cerebri Bold'", () => {
expect(purgecssResult.includes(`src: url('../fonts/CerebriSans-Bold.eot?')`)).toBe(true)
})
it("keep @font-face 'Cerebri Sans'", () => {
expect(purgecssResult.includes(`src: url('../fonts/CerebriSans-Regular.eot?')`)).toBe(
true
Expand Down
14 changes: 13 additions & 1 deletion __tests__/test_examples/font_face/font_face.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
src: url('../fonts/CerebriSans-Regular.eot?') format('eot'), url('../fonts/CerebriSans-Regular.otf') format('opentype'), url('../fonts/CerebriSans-Regular.svg#Cerebri_Sans') format('svg'), url('../fonts/CerebriSans-Regular.ttf') format('truetype'), url('../fonts/CerebriSans-Regular.woff') format('woff');
}

@font-face {
font-family: 'Cerebri Bold';
font-weight: 400;
font-style: normal;
src: url('../fonts/CerebriSans-Bold.eot?') format('eot'), url('../fonts/CerebriSans-Bold.otf') format('opentype'), url('../fonts/CerebriSans-Bold.svg#Cerebri_Sans') format('svg'), url('../fonts/CerebriSans-Bold.ttf') format('truetype'), url('../fonts/CerebriSans-Bold.woff') format('woff');
}

@font-face {
font-family: 'OtherFont';
font-weight: 400;
Expand All @@ -16,7 +23,12 @@
color: black;
}

used {
.used {
color: red;
font-family: 'Cerebri Sans';
}

.used2 {
color: blue;
font-family: Cerebri Bold, serif;
}
3 changes: 2 additions & 1 deletion __tests__/test_examples/font_face/font_face.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<body>
<div class="used"></div>
<div class="used2"></div>
</body>

</html>
</html>
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ class Purgecss {
}
}

/**
* Strips quotes at the end and at the end of a string
* @param {string} value the string to be stripped
*/
stripQuotes(value: string) {
return value.replace(/(^["'])|(["']$)/g, '')
}

/**
* Extract the selectors present in the passed string using a purgecss extractor
* @param {array} content Array of content
Expand Down Expand Up @@ -354,7 +362,10 @@ class Purgecss {
}
if (this.options.fontFace) {
if (prop === 'font-family') {
this.usedFontFaces.add(value)
for (const fontName of value.split(',')) {
const cleanedFontFace = this.stripQuotes(fontName.trim())
this.usedFontFaces.add(cleanedFontFace)
}
}
}
}
Expand All @@ -381,7 +392,7 @@ class Purgecss {
for (const { prop, value } of node.nodes) {
if (prop === 'font-family') {
this.atRules.fontFace.push({
name: value,
name: this.stripQuotes(value),
node
})
}
Expand Down