Skip to content

Commit

Permalink
Adds font-face analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman committed May 6, 2018
1 parent f567f82 commit dbef7ed
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 22 deletions.
21 changes: 19 additions & 2 deletions src/analyzer/atrules/fontfaces.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
const uniquer = require('../../utils/uniquer')

module.exports = atRules => {
const all = atRules
.filter(rule => rule.type === 'font-face')
.map(rule => rule.params)
.map(rule => rule.descriptors)

// Tricky bit: uniqueness will be based on the `src` of the @font-face
const uniqueWithCount = uniquer(
all.map(ff => ff.src)
).uniqueWithCount.map(item => {
// Once we have a list of unique @font-faces,
// we'll map it back to the original values again
return {
count: item.count,
value: all.find(ff => ff.src === item.value)
}
})

return {
total: all.length
total: all.length,
unique: uniqueWithCount.map(item => item.value),
totalUnique: uniqueWithCount.length,
uniqueWithCount
}
}
26 changes: 24 additions & 2 deletions src/parser/atrules.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
const AT_RULES_WITH_DESCRIPTORS = ['font-face']

function addDescriptorsToAtRuleFromTree(atRule, tree) {
if (!AT_RULES_WITH_DESCRIPTORS.includes(atRule.type)) {
return atRule
}

const descriptors = {}
tree.walkDecls(declaration => {
descriptors[declaration.prop] = declaration.value
})

return {
...atRule,
descriptors
}
}

module.exports = tree => {
const atrules = []

tree.walkAtRules(rule => {
atrules.push({
const atRule = {
type: rule.name.trim(),
params: rule.params.trim()
})
}

return atrules.push(
addDescriptorsToAtRuleFromTree(atRule, rule)
)
})

return atrules
Expand Down
21 changes: 10 additions & 11 deletions src/utils/uniquer.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
function stringCompare(a, b) {
return a
.toLowerCase()
.localeCompare(
b.toLowerCase()
)
}

module.exports = (values, sortFn) => {
if (!sortFn) {
sortFn = (a, b) => {
return a
.toLowerCase()
.localeCompare(
b.toLowerCase()
)
}
}
sortFn = sortFn || stringCompare

const reduced = Array.from(
values.reduce((map, value) => {
// Create a Map of unique values and their counts
map.set(value, map.get(value) + 1 || 1)
return map
return map.set(value, map.get(value) + 1 || 1)
}, new Map())
).map(value => {
// Create an array of [{value, count}]
Expand Down
50 changes: 45 additions & 5 deletions test/analyzer/atrules/fontfaces/input.css
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
.rule {
.regular-rule {
color: red
}

/**
* All bells and whistles
*/

@font-face {
/* empty */
font-display: swap;
font-family: monospace;
font-stretch: condensed;
font-style: italic;
font-weight: bold;
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;
}

/**
* Intentional duplicate to test uniqueness
*/
@font-face {
font-family: monospace;
font-variant: normal;
font-weight: normal;
src: url('https://example.com');
font-family: 'Example Duplicate';
}

@font-face {
src: url('https://example.com');
font-family: 'Example Duplicate';
}

/**
* These examples are taken from https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face#Descriptors
*/

@font-face {
font-family: MyHelvetica;
src: local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url(MgOpenModernaBold.ttf);
font-weight: bold;
}

@font-face {
font-family: "Bitstream Vera Serif Bold";
src: url("https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf");
}

@font-face {
font-family: "Open Sans";
src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"), url("/fonts/OpenSans-Regular-webfont.woff") format("woff");
}
80 changes: 79 additions & 1 deletion test/analyzer/atrules/fontfaces/output.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,81 @@
{
"total": 2
"total": 6,
"totalUnique": 5,
"unique": [
{
"font-display": "swap",
"font-family": "monospace",
"font-stretch": "condensed",
"font-style": "italic",
"font-weight": "bold",
"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-family": "MyHelvetica",
"src": "local(\"Helvetica Neue Bold\"), local(\"HelveticaNeue-Bold\"), url(MgOpenModernaBold.ttf)",
"font-weight": "bold"
},
{
"src": "url('https://example.com')",
"font-family": "'Example Duplicate'"
},
{
"font-family": "\"Open Sans\"",
"src": "url(\"/fonts/OpenSans-Regular-webfont.woff2\") format(\"woff2\"), url(\"/fonts/OpenSans-Regular-webfont.woff\") format(\"woff\")"
},
{
"font-family": "\"Bitstream Vera Serif Bold\"",
"src": "url(\"https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf\")"
}
],
"uniqueWithCount": [
{
"count": 1,
"value": {
"font-display": "swap",
"font-family": "monospace",
"font-stretch": "condensed",
"font-style": "italic",
"font-weight": "bold",
"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"
}
},
{
"count": 1,
"value": {
"font-family": "MyHelvetica",
"src": "local(\"Helvetica Neue Bold\"), local(\"HelveticaNeue-Bold\"), url(MgOpenModernaBold.ttf)",
"font-weight": "bold"
}
},
{
"count": 2,
"value": {
"src": "url('https://example.com')",
"font-family": "'Example Duplicate'"
}
},
{
"count": 1,
"value": {
"font-family": "\"Open Sans\"",
"src": "url(\"/fonts/OpenSans-Regular-webfont.woff2\") format(\"woff2\"), url(\"/fonts/OpenSans-Regular-webfont.woff\") format(\"woff\")"
}
},
{
"count": 1,
"value": {
"font-family": "\"Bitstream Vera Serif Bold\"",
"src": "url(\"https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf\")"
}
}
]
}
5 changes: 4 additions & 1 deletion test/analyzer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ test('Returns the correct analysis object structure', async t => {
uniqueWithCount: []
},
fontfaces: {
total: 0
total: 0,
totalUnique: 0,
unique: [],
uniqueWithCount: []
},
imports: {
total: 0,
Expand Down

0 comments on commit dbef7ed

Please sign in to comment.