Skip to content

Commit

Permalink
store node locations for font-face rules (#368)
Browse files Browse the repository at this point in the history
It's a pretty whacky setup with the double
`__unstable_uniqueWithLocations`, but it does the job. And it's
`unstable` anyway.

closes #354
  • Loading branch information
bartveneman authored Dec 29, 2023
1 parent dd058fa commit e3b2e7b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
89 changes: 89 additions & 0 deletions src/atrules/atrules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,95 @@ AtRules('finds @font-face', () => {
assert.equal(actual, expected)
})

AtRules('finds @font-face', () => {
const fixture = `
@font-face {
font-family: Arial;
src: url("https://url-to-arial.woff");
}
@font-face {
font-display: swap;
font-family: Test;
font-stretch: condensed;
font-style: italic;
font-weight: 700;
font-variant: no-common-ligatures proportional-nums;
font-feature-settings: "liga" 0;
font-variation-settings: "xhgt" 0.7;
src: local("Input Mono");
unicode-range: U+0025-00FF;
}
@font-face {
font-family: 'Input Mono';
src: local('Input Mono') url("https://url-to-input-mono.woff");
}
@font-face {
font-family: MyHelvetica;
src: local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url(MgOpenModernaBold.ttf);
font-weight: bold;
}
/* Duplicate @font-face in Media Query */
@media (min-width: 1000px) {
@font-face {
font-family: 'Input Mono';
src: local('Input Mono') url("https://url-to-input-mono.woff");
}
}`
const actual = analyze(fixture, {
useUnstableLocations: true
}).atrules.fontface.__unstable_uniqueWithLocations
const expected = {
total: 5,
totalUnique: 5,
unique: {
5: 1,
100: 1,
463: 1,
590: 1,
850: 1,
},
__unstable__uniqueWithLocations: {
5: [{
line: 2,
column: 5,
offset: 5,
length: 89,
}],
100: [{
line: 7,
column: 5,
offset: 100,
length: 357,
}],
463: [{
line: 20,
column: 5,
offset: 463,
length: 121,
}],
590: [{
line: 25,
column: 5,
offset: 590,
length: 173,
}],
850: [{
line: 33,
column: 7,
offset: 850,
length: 127,
}],
},
uniquenessRatio: 1
}

assert.equal(actual, expected)
})

AtRules('handles @font-face encoding issues (GH-307)', () => {
// Actual CSS once found in a <style> tag on vistaprint.nl
// CSSTree parses it without errors, but analyzer failed on it;
Expand Down
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export function analyze(css, options = {}) {
let totalAtRules = 0
/** @type {Record<string: string>}[]} */
let fontfaces = []
let fontfaces_with_loc = new Collection(useLocations)
let layers = new Collection(useLocations)
let imports = new Collection(useLocations)
let medias = new Collection(useLocations)
Expand Down Expand Up @@ -181,6 +182,10 @@ export function analyze(css, options = {}) {
if (atRuleName === 'font-face') {
let descriptors = {}

if (useLocations) {
fontfaces_with_loc.p(node.loc.start.offset, node.loc)
}

node.block.children.forEach(descriptor => {
// Ignore 'Raw' nodes in case of CSS syntax errors
if (descriptor.type === Declaration) {
Expand Down Expand Up @@ -642,12 +647,14 @@ export function analyze(css, options = {}) {
}),
},
atrules: {
fontface: {
fontface: assign({
total: fontFacesCount,
totalUnique: fontFacesCount,
unique: fontfaces,
uniquenessRatio: fontFacesCount === 0 ? 0 : 1
},
uniquenessRatio: fontFacesCount === 0 ? 0 : 1,
}, useLocations ? {
__unstable_uniqueWithLocations: fontfaces_with_loc.c(),
} : {}),
import: imports.c(),
media: assign(
medias.c(),
Expand Down

0 comments on commit e3b2e7b

Please sign in to comment.