Skip to content

Commit

Permalink
WIP, carried over from assetgraph/assetgraph#989
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Aug 18, 2019
1 parent ab4c113 commit 3855d2c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
37 changes: 19 additions & 18 deletions lib/injectSubsetDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,28 @@ function injectSubsetDefinitions(cssValue, webfontNameMap, replaceOriginal) {
const subsetFontNames = new Set(
Object.values(webfontNameMap).map(name => name.toLowerCase())
);
const tokens = postcssValuesParser(cssValue).tokens;
let resultStr = '';
const rootNode = postcssValuesParser.parse(cssValue);
let isPreceededByWords = false;
for (let i = 0; i < tokens.length; i += 1) {
const token = tokens[i];
for (const [i, node] of rootNode.nodes.entries()) {
let possibleFontFamily;
let lastFontFamilyTokenIndex = i;
if (token[0] === 'string') {
possibleFontFamily = unquote(token[1]);
} else if (token[0] === 'word') {
if (node.type === 'quoted') {
possibleFontFamily = unquote(node.value);
} else if (node.type === 'word') {
if (!isPreceededByWords) {
const wordSequence = [];
for (let j = i; j < tokens.length; j += 1) {
if (tokens[j][0] === 'word') {
wordSequence.push(tokens[j][1]);
for (let j = i; j < rootNode.nodes.length; j += 1) {
if (rootNode.nodes[j].type === 'word') {
wordSequence.push(rootNode.nodes[j].value);
lastFontFamilyTokenIndex = j;
} else if (tokens[j][0] !== 'space') {
} else {
break;
}
}
possibleFontFamily = wordSequence.join(' ');
}
isPreceededByWords = true;
} else if (token[0] !== 'space') {
} else {
isPreceededByWords = false;
}
if (possibleFontFamily) {
Expand All @@ -37,10 +35,14 @@ function injectSubsetDefinitions(cssValue, webfontNameMap, replaceOriginal) {
// Bail out, a subset font is already listed
return cssValue;
} else if (webfontNameMap[possibleFontFamilyLowerCase]) {
resultStr += `'${webfontNameMap[possibleFontFamilyLowerCase].replace(
/'/g,
"\\'"
)}'`;
rootNode.insertBefore(
node,
`'${webfontNameMap[possibleFontFamilyLowerCase].replace(
/'/g,
"\\'"
)}', `
);
return rootNode.toString();
if (replaceOriginal) {
tokens.splice(i, lastFontFamilyTokenIndex - i + 1);
i -= 1;
Expand All @@ -50,9 +52,8 @@ function injectSubsetDefinitions(cssValue, webfontNameMap, replaceOriginal) {
}
}
}
resultStr += token[1];
}
return resultStr;
return cssValue;
}

module.exports = injectSubsetDefinitions;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"font-tracer": "^1.1.0",
"fontkit": "^1.8.0",
"lodash.groupby": "^4.6.0",
"postcss-values-parser": "^2.0.1",
"postcss-values-parser": "^3.0.5",
"pretty-bytes": "^5.1.0",
"urltools": "^0.4.1",
"yargs": "^12.0.2"
Expand Down
2 changes: 1 addition & 1 deletion test/injectSubsetDefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('injectSubsetDefinitions', function() {
expect(
injectSubsetDefinitions('times new roman', webfontNameMap),
'to equal',
"'times new roman__subset', times new roman"
"'times new roman__subset', times new roman"
);
});

Expand Down

0 comments on commit 3855d2c

Please sign in to comment.