From cd2887951f91be698669f4140a04e9791f076693 Mon Sep 17 00:00:00 2001 From: Guillaume Verger Date: Wed, 21 Aug 2019 16:00:12 +0200 Subject: [PATCH] Fix Font-Face processing Fixes https://github.com/FullHuman/purgecss/issues/79 --- __tests__/purgecssDefault.test.js | 3 +++ __tests__/test_examples/font_face/font_face.css | 14 +++++++++++++- __tests__/test_examples/font_face/font_face.html | 3 ++- src/index.js | 15 +++++++++++++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/__tests__/purgecssDefault.test.js b/__tests__/purgecssDefault.test.js index 38b57f9c..94bba893 100644 --- a/__tests__/purgecssDefault.test.js +++ b/__tests__/purgecssDefault.test.js @@ -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 diff --git a/__tests__/test_examples/font_face/font_face.css b/__tests__/test_examples/font_face/font_face.css index 16bd9b35..fe2bba98 100644 --- a/__tests__/test_examples/font_face/font_face.css +++ b/__tests__/test_examples/font_face/font_face.css @@ -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; @@ -16,7 +23,12 @@ color: black; } -used { +.used { color: red; font-family: 'Cerebri Sans'; } + +.used2 { + color: blue; + font-family: Cerebri Bold, serif; +} diff --git a/__tests__/test_examples/font_face/font_face.html b/__tests__/test_examples/font_face/font_face.html index a3274924..2c92e293 100644 --- a/__tests__/test_examples/font_face/font_face.html +++ b/__tests__/test_examples/font_face/font_face.html @@ -2,6 +2,7 @@
+
- \ No newline at end of file + diff --git a/src/index.js b/src/index.js index 859fbd37..4c29a68d 100644 --- a/src/index.js +++ b/src/index.js @@ -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 @@ -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) + } } } } @@ -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 }) }