-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP, carried over from assetgraph/assetgraph#989
Awaiting shellscape/postcss-values-parser#75
- Loading branch information
1 parent
0d59351
commit 2ae3c49
Showing
5 changed files
with
145 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const postcssValuesParser = require('postcss-values-parser'); | ||
const unquote = require('./unquote'); | ||
|
||
function injectSubsetDefinitions(cssValue, webfontNameMap, replaceOriginal) { | ||
const subsetFontNames = new Set( | ||
Object.values(webfontNameMap).map(name => name.toLowerCase()) | ||
); | ||
const tokens = postcssValuesParser(cssValue).tokens; | ||
let resultStr = ''; | ||
let isPreceededByWords = false; | ||
for (let i = 0; i < tokens.length; i += 1) { | ||
const token = tokens[i]; | ||
let possibleFontFamily; | ||
let lastFontFamilyTokenIndex = i; | ||
if (token[0] === 'string') { | ||
possibleFontFamily = unquote(token[1]); | ||
} else if (token[0] === 'word') { | ||
if (!isPreceededByWords) { | ||
const wordSequence = []; | ||
for (let j = i; j < tokens.length; j += 1) { | ||
if (tokens[j][0] === 'word') { | ||
wordSequence.push(tokens[j][1]); | ||
lastFontFamilyTokenIndex = j; | ||
} else if (tokens[j][0] !== 'space') { | ||
break; | ||
} | ||
} | ||
possibleFontFamily = wordSequence.join(' '); | ||
} | ||
isPreceededByWords = true; | ||
} else if (token[0] !== 'space') { | ||
isPreceededByWords = false; | ||
} | ||
if (possibleFontFamily) { | ||
const possibleFontFamilyLowerCase = possibleFontFamily.toLowerCase(); | ||
if (subsetFontNames.has(possibleFontFamilyLowerCase)) { | ||
// Bail out, a subset font is already listed | ||
return cssValue; | ||
} else if (webfontNameMap[possibleFontFamilyLowerCase]) { | ||
resultStr += `'${webfontNameMap[possibleFontFamilyLowerCase].replace( | ||
/'/g, | ||
"\\'" | ||
)}'`; | ||
if (replaceOriginal) { | ||
tokens.splice(i, lastFontFamilyTokenIndex - i + 1); | ||
i -= 1; | ||
continue; | ||
} else { | ||
resultStr += ', '; | ||
} | ||
} | ||
} | ||
resultStr += token[1]; | ||
} | ||
return resultStr; | ||
} | ||
|
||
module.exports = injectSubsetDefinitions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- lib/util/fonts/injectSubsetDefinitions.js | ||
+++ lib/util/fonts/injectSubsetDefinitions.js | ||
@@ -5,28 +5,26 @@ function injectSubsetDefinitions(cssValue, webfontNameMap) { | ||
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; | ||
- 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]); | ||
- } else if (tokens[j][0] !== 'space') { | ||
+ for (let j = i; j < rootNode.nodes.length; j += 1) { | ||
+ if (rootNode.nodes[j].type === 'word') { | ||
+ wordSequence.push(rootNode.nodes[j].value); | ||
+ } else { | ||
break; | ||
} | ||
} | ||
possibleFontFamily = wordSequence.join(' '); | ||
} | ||
isPreceededByWords = true; | ||
- } else if (token[0] !== 'space') { | ||
+ } else { | ||
isPreceededByWords = false; | ||
} | ||
if (possibleFontFamily) { | ||
@@ -35,15 +33,18 @@ function injectSubsetDefinitions(cssValue, webfontNameMap) { | ||
if (subsetFontNames.has(possibleFontFamilyLowerCase)) { | ||
return cssValue; | ||
} else if (webfontNameMap[possibleFontFamilyLowerCase]) { | ||
- resultStr += `'${webfontNameMap[possibleFontFamilyLowerCase].replace( | ||
- /'/g, | ||
- "\\'" | ||
- )}', `; | ||
+ rootNode.insertBefore( | ||
+ node, | ||
+ `'${webfontNameMap[possibleFontFamilyLowerCase].replace( | ||
+ /'/g, | ||
+ "\\'" | ||
+ )}', ` | ||
+ ); | ||
+ return rootNode.toString(); | ||
} | ||
} | ||
- resultStr += token[1]; | ||
} | ||
- return resultStr; | ||
+ return cssValue; | ||
} | ||
|
||
module.exports = injectSubsetDefinitions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters