Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
78 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
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 |
---|---|---|
@@ -1,51 +1,23 @@ | ||
const postcssValuesParser = require('postcss-values-parser'); | ||
const postcssValueParser = require('postcss-value-parser'); | ||
|
||
module.exports = function stripLocalTokens(cssValue) { | ||
const tokens = postcssValuesParser(cssValue).tokens; | ||
for (let i = 0; i < tokens.length - 3; i += 1) { | ||
if ( | ||
tokens[i][0] === 'word' && | ||
tokens[i][1].toLowerCase() === 'local' && | ||
tokens[i + 1][0] === '(' && | ||
(tokens[i + 2][0] === 'string' || tokens[i + 2][0] === 'word') && | ||
tokens[i + 3][0] === ')' | ||
) { | ||
let startIndex = i; | ||
let numTokensToRemove = 4; | ||
let commaBefore = false; | ||
let commaAfter = false; | ||
while ( | ||
startIndex > 0 && | ||
['space', 'comma'].includes(tokens[startIndex - 1][0]) | ||
) { | ||
if (tokens[startIndex - 1][0] === 'comma') { | ||
commaBefore = true; | ||
const rootNode = postcssValueParser(cssValue); | ||
for (let i = 0; i < rootNode.nodes.length; i += 1) { | ||
const node = rootNode.nodes[i]; | ||
if (node.type === 'function' && node.value.toLowerCase() === 'local') { | ||
let numTokensToRemove = 1; | ||
if (i + 1 < rootNode.nodes.length) { | ||
const nextToken = rootNode.nodes[i + 1]; | ||
if (nextToken.type === 'div' && nextToken.value === ',') { | ||
numTokensToRemove += 1; | ||
if (i + 2 < rootNode.nodes.length) { | ||
rootNode.nodes[i + 2].before = node.before; | ||
} | ||
} | ||
startIndex -= 1; | ||
numTokensToRemove += 1; | ||
} | ||
|
||
while ( | ||
startIndex + numTokensToRemove < tokens.length && | ||
['space', 'comma'].includes(tokens[startIndex + numTokensToRemove][0]) | ||
) { | ||
if (tokens[startIndex + numTokensToRemove][0] === 'comma') { | ||
commaAfter = true; | ||
} | ||
numTokensToRemove += 1; | ||
} | ||
if (commaBefore && commaAfter) { | ||
tokens.splice( | ||
startIndex, | ||
numTokensToRemove, | ||
['comma', ','], | ||
['space', ' '] | ||
); | ||
} else { | ||
tokens.splice(startIndex, numTokensToRemove); | ||
} | ||
i = startIndex - 1; | ||
rootNode.nodes.splice(i, numTokensToRemove); | ||
i -= 1; | ||
} | ||
} | ||
return tokens.map(token => token[1]).join(''); | ||
return postcssValueParser.stringify(rootNode); | ||
}; |
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,33 @@ | ||
const expect = require('unexpected') | ||
.clone() | ||
.use(require('unexpected-set')); | ||
|
||
const extractReferencedCustomPropertyNames = require('../lib/extractReferencedCustomPropertyNames'); | ||
|
||
describe('extractReferencedCustomPropertyNames', function() { | ||
it('should return the empty set when no custom properties are referenced', function() { | ||
expect( | ||
extractReferencedCustomPropertyNames('foo(bar), local(abc), bla-bla'), | ||
'to equal', | ||
new Set() | ||
); | ||
}); | ||
|
||
it('should return the name of a referenced custom property', function() { | ||
expect( | ||
extractReferencedCustomPropertyNames('foo(bar), var(--abc), bla-bla'), | ||
'to equal', | ||
new Set(['--abc']) | ||
); | ||
}); | ||
|
||
it('should return the names of multiple referenced custom properties', function() { | ||
expect( | ||
extractReferencedCustomPropertyNames( | ||
'foo(bar), var(--abc), bla-bla, var(--def)' | ||
), | ||
'to equal', | ||
new Set(['--abc', '--def']) | ||
); | ||
}); | ||
}); |
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