Skip to content

Commit e3b2e7b

Browse files
authored
store node locations for font-face rules (#368)
It's a pretty whacky setup with the double `__unstable_uniqueWithLocations`, but it does the job. And it's `unstable` anyway. closes #354
1 parent dd058fa commit e3b2e7b

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

src/atrules/atrules.test.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,95 @@ AtRules('finds @font-face', () => {
199199
assert.equal(actual, expected)
200200
})
201201

202+
AtRules('finds @font-face', () => {
203+
const fixture = `
204+
@font-face {
205+
font-family: Arial;
206+
src: url("https://url-to-arial.woff");
207+
}
208+
209+
@font-face {
210+
font-display: swap;
211+
font-family: Test;
212+
font-stretch: condensed;
213+
font-style: italic;
214+
font-weight: 700;
215+
font-variant: no-common-ligatures proportional-nums;
216+
font-feature-settings: "liga" 0;
217+
font-variation-settings: "xhgt" 0.7;
218+
src: local("Input Mono");
219+
unicode-range: U+0025-00FF;
220+
}
221+
222+
@font-face {
223+
font-family: 'Input Mono';
224+
src: local('Input Mono') url("https://url-to-input-mono.woff");
225+
}
226+
227+
@font-face {
228+
font-family: MyHelvetica;
229+
src: local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url(MgOpenModernaBold.ttf);
230+
font-weight: bold;
231+
}
232+
233+
/* Duplicate @font-face in Media Query */
234+
@media (min-width: 1000px) {
235+
@font-face {
236+
font-family: 'Input Mono';
237+
src: local('Input Mono') url("https://url-to-input-mono.woff");
238+
}
239+
}`
240+
const actual = analyze(fixture, {
241+
useUnstableLocations: true
242+
}).atrules.fontface.__unstable_uniqueWithLocations
243+
const expected = {
244+
total: 5,
245+
totalUnique: 5,
246+
unique: {
247+
5: 1,
248+
100: 1,
249+
463: 1,
250+
590: 1,
251+
850: 1,
252+
},
253+
__unstable__uniqueWithLocations: {
254+
5: [{
255+
line: 2,
256+
column: 5,
257+
offset: 5,
258+
length: 89,
259+
}],
260+
100: [{
261+
line: 7,
262+
column: 5,
263+
offset: 100,
264+
length: 357,
265+
}],
266+
463: [{
267+
line: 20,
268+
column: 5,
269+
offset: 463,
270+
length: 121,
271+
}],
272+
590: [{
273+
line: 25,
274+
column: 5,
275+
offset: 590,
276+
length: 173,
277+
}],
278+
850: [{
279+
line: 33,
280+
column: 7,
281+
offset: 850,
282+
length: 127,
283+
}],
284+
},
285+
uniquenessRatio: 1
286+
}
287+
288+
assert.equal(actual, expected)
289+
})
290+
202291
AtRules('handles @font-face encoding issues (GH-307)', () => {
203292
// Actual CSS once found in a <style> tag on vistaprint.nl
204293
// CSSTree parses it without errors, but analyzer failed on it;

src/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export function analyze(css, options = {}) {
100100
let totalAtRules = 0
101101
/** @type {Record<string: string>}[]} */
102102
let fontfaces = []
103+
let fontfaces_with_loc = new Collection(useLocations)
103104
let layers = new Collection(useLocations)
104105
let imports = new Collection(useLocations)
105106
let medias = new Collection(useLocations)
@@ -181,6 +182,10 @@ export function analyze(css, options = {}) {
181182
if (atRuleName === 'font-face') {
182183
let descriptors = {}
183184

185+
if (useLocations) {
186+
fontfaces_with_loc.p(node.loc.start.offset, node.loc)
187+
}
188+
184189
node.block.children.forEach(descriptor => {
185190
// Ignore 'Raw' nodes in case of CSS syntax errors
186191
if (descriptor.type === Declaration) {
@@ -642,12 +647,14 @@ export function analyze(css, options = {}) {
642647
}),
643648
},
644649
atrules: {
645-
fontface: {
650+
fontface: assign({
646651
total: fontFacesCount,
647652
totalUnique: fontFacesCount,
648653
unique: fontfaces,
649-
uniquenessRatio: fontFacesCount === 0 ? 0 : 1
650-
},
654+
uniquenessRatio: fontFacesCount === 0 ? 0 : 1,
655+
}, useLocations ? {
656+
__unstable_uniqueWithLocations: fontfaces_with_loc.c(),
657+
} : {}),
651658
import: imports.c(),
652659
media: assign(
653660
medias.c(),

0 commit comments

Comments
 (0)