Skip to content

Commit

Permalink
Merge pull request #214 from gverger/master
Browse files Browse the repository at this point in the history
Fix Font-Face processing
  • Loading branch information
Ffloriel committed Sep 1, 2019
2 parents fd29336 + cd28879 commit 8795e96
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
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

0 comments on commit 8795e96

Please sign in to comment.