Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css|sass|scss] Add @apply custom property support #281

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 75 additions & 16 deletions src/css/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,13 +446,15 @@ function checkAtrule(i) {

// If token is part of an @-rule, save the rule's type to token.
// @keyframes:
if (l = checkKeyframesRule(i)) tokens[i].atrule_type = 4;
if (l = checkKeyframesRule(i)) tokens[i].atrule_type = 1;
// @apply --custom-property:
else if (l = checkApplyRule(i)) tokens[i].atrule_type = 2;
// @-rule with ruleset:
else if (l = checkAtruler(i)) tokens[i].atrule_type = 1;
else if (l = checkAtruler(i)) tokens[i].atrule_type = 3;
// Block @-rule:
else if (l = checkAtruleb(i)) tokens[i].atrule_type = 2;
else if (l = checkAtruleb(i)) tokens[i].atrule_type = 4;
// Single-line @-rule:
else if (l = checkAtrules(i)) tokens[i].atrule_type = 3;
else if (l = checkAtrules(i)) tokens[i].atrule_type = 5;
else return 0;

// If token is part of an @-rule, save the rule's length to token:
Expand All @@ -468,10 +470,11 @@ function checkAtrule(i) {
function getAtrule() {
const childType = tokens[pos].atrule_type;

if (childType === 1) return getAtruler(); // @-rule with ruleset
if (childType === 2) return getAtruleb(); // Block @-rule
if (childType === 3) return getAtrules(); // Single-line @-rule
if (childType === 4) return getKeyframesRule();
if (childType === 1) return getKeyframesRule();
if (childType === 2) return getApplyRule();
if (childType === 3) return getAtruler(); // @-rule with ruleset
if (childType === 4) return getAtruleb(); // Block @-rule
if (childType === 5) return getAtrules(); // Single-line @-rule
}

/**
Expand Down Expand Up @@ -653,6 +656,47 @@ function getAtrules() {
return newNode(type, content, line, column);
}

/**
* @param {Number} i Token's index number
* @returns {Number}
*/
function checkApplyRule(i) {
const start = i;
let l;

if (i >= tokensLength) return 0;

if (l = checkAtkeyword(i)) i += l;
else return 0;

const atruleName = joinValues2(i - l, l);
if (atruleName.toLowerCase().indexOf('apply') === -1) return 0;

if (l = checkSC(i)) i += l;

if (l = checkCustomProperty(i)) i += l;
else return 0;

return i - start;
}

/**
* @returns {Node}
*/
function getApplyRule() {
const type = NodeType.AtruleType;
const token = tokens[pos];
const line = token.ln;
const column = token.col;
let content = [].concat(
getAtkeyword(),
getSC(),
getCustomProperty()
);

return newNode(type, content, line, column);
}

/**
* Check if token is part of a block (e.g. `{...}`).
* @param {Number} i Token's index number
Expand Down Expand Up @@ -2285,11 +2329,11 @@ function checkPseudoc(i) {

if (i >= tokensLength || tokens[i].type !== TokenType.Colon) return 0;

if (l = checkPseudoClass1(i)) tokens[i].pseudoClassType = 1;
else if (l = checkPseudoClass2(i)) tokens[i].pseudoClassType = 2;
else if (l = checkPseudoClass3(i)) tokens[i].pseudoClassType = 3;
if (l = checkPseudoClass3(i)) tokens[i].pseudoClassType = 3;
else if (l = checkPseudoClass4(i)) tokens[i].pseudoClassType = 4;
else if (l = checkPseudoClass5(i)) tokens[i].pseudoClassType = 5;
else if (l = checkPseudoClass1(i)) tokens[i].pseudoClassType = 1;
else if (l = checkPseudoClass2(i)) tokens[i].pseudoClassType = 2;
else if (l = checkPseudoClass6(i)) tokens[i].pseudoClassType = 6;
else return 0;

Expand Down Expand Up @@ -3343,11 +3387,13 @@ function checkValue(i) {
let _i;

while (i < tokensLength) {
if (checkDeclDelim(i)) break;

s = checkSC(i);
_i = i + s;

if (l = _checkValue(_i)) i += l + s;
else break;
if (!l || checkBlock(i - l)) break;
}

tokens[start].value_end = i;
Expand All @@ -3365,10 +3411,21 @@ function getValue() {
const column = token.col;
const end = tokens[pos].value_end;
let content = [];
let _pos;
let s;

while (pos < end) {
if (tokens[pos].value_child) content.push(_getValue());
else content = content.concat(getSC());
s = checkSC(pos);
_pos = pos + s;

if (checkDeclDelim(_pos)) break;

if (!_checkValue(_pos)) break;

if (s) content = content.concat(getSC());
content.push(_getValue());

if (checkBlock(_pos)) break;
}

return newNode(type, content, line, column);
Expand All @@ -3383,9 +3440,10 @@ function _checkValue(i) {

if (l = checkProgid(i)) tokens[i].value_child = 1;
else if (l = checkVhash(i)) tokens[i].value_child = 2;
else if (l = checkAny(i)) tokens[i].value_child = 3;
else if (l = checkBlock(i)) tokens[i].value_child = 3;
else if (l = checkOperator(i)) tokens[i].value_child = 4;
else if (l = checkImportant(i)) tokens[i].value_child = 5;
else if (l = checkAny(i)) tokens[i].value_child = 6;

return l;
}
Expand All @@ -3397,9 +3455,10 @@ function _getValue() {
const childType = tokens[pos].value_child;
if (childType === 1) return getProgid();
if (childType === 2) return getVhash();
if (childType === 3) return getAny();
if (childType === 3) return getBlock();
if (childType === 4) return getOperator();
if (childType === 5) return getImportant();
if (childType === 6) return getAny();
}

/**
Expand Down
62 changes: 53 additions & 9 deletions src/sass/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,15 @@ function checkAtrule(i) {

// If token is part of an @-rule, save the rule's type to token.
// @keyframes:
if (l = checkKeyframesRule(i)) tokens[i].atrule_type = 4;
if (l = checkKeyframesRule(i)) tokens[i].atrule_type = 1;
// @apply --custom-property:
else if (l = checkApplyRule(i)) tokens[i].atrule_type = 2;
// @-rule with ruleset:
else if (l = checkAtruler(i)) tokens[i].atrule_type = 1;
else if (l = checkAtruler(i)) tokens[i].atrule_type = 3;
// Block @-rule:
else if (l = checkAtruleb(i)) tokens[i].atrule_type = 2;
else if (l = checkAtruleb(i)) tokens[i].atrule_type = 4;
// Single-line @-rule:
else if (l = checkAtrules(i)) tokens[i].atrule_type = 3;
else if (l = checkAtrules(i)) tokens[i].atrule_type = 5;
else return 0;

// If token is part of an @-rule, save the rule's length to token:
Expand All @@ -612,10 +614,11 @@ function checkAtrule(i) {
function getAtrule() {
const childType = tokens[pos].atrule_type;

if (childType === 1) return getAtruler(); // @-rule with ruleset
if (childType === 2) return getAtruleb(); // Block @-rule
if (childType === 3) return getAtrules(); // Single-line @-rule
if (childType === 4) return getKeyframesRule();
if (childType === 1) return getKeyframesRule();
if (childType === 2) return getApplyRule();
if (childType === 3) return getAtruler(); // @-rule with ruleset
if (childType === 4) return getAtruleb(); // Block @-rule
if (childType === 5) return getAtrules(); // Single-line @-rule
}

/**
Expand Down Expand Up @@ -785,6 +788,47 @@ function getAtrules() {
return newNode(type, content, line, column);
}

/**
* @param {Number} i Token's index number
* @returns {Number}
*/
function checkApplyRule(i) {
const start = i;
let l;

if (i >= tokensLength) return 0;

if (l = checkAtkeyword(i)) i += l;
else return 0;

const atruleName = joinValues2(i - l, l);
if (atruleName.toLowerCase().indexOf('apply') === -1) return 0;

if (l = checkSC(i)) i += l;

if (l = checkCustomProperty(i)) i += l;
else return 0;

return i - start;
}

/**
* @returns {Node}
*/
function getApplyRule() {
const type = NodeType.AtruleType;
const token = tokens[pos];
const line = token.ln;
const column = token.col;
let content = [].concat(
getAtkeyword(),
getSC(),
getCustomProperty()
);

return newNode(type, content, line, column);
}

/**
* Checks if token is part of a block (e.g. `{...}`).
*
Expand Down Expand Up @@ -874,7 +918,7 @@ function checkBlockdecl1(i) {
[2, 4, 6, 8].indexOf(tokens[start].include_type) === -1) return 0;

if (tokens[start].bd_kind === 6 &&
tokens[start].atrule_type === 3) return 0;
tokens[start].atrule_type === 5) return 0;

while (i < tokensLength) {
if (l = checkDeclDelim(i))
Expand Down
60 changes: 52 additions & 8 deletions src/scss/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,13 +557,15 @@ function checkAtrule(i) {

// If token is part of an @-rule, save the rule's type to token.
// @keyframes:
if (l = checkKeyframesRule(i)) tokens[i].atrule_type = 4;
if (l = checkKeyframesRule(i)) tokens[i].atrule_type = 1;
// @apply --custom-property:
else if (l = checkApplyRule(i)) tokens[i].atrule_type = 2;
// @-rule with ruleset:
else if (l = checkAtruler(i)) tokens[i].atrule_type = 1;
else if (l = checkAtruler(i)) tokens[i].atrule_type = 3;
// Block @-rule:
else if (l = checkAtruleb(i)) tokens[i].atrule_type = 2;
else if (l = checkAtruleb(i)) tokens[i].atrule_type = 4;
// Single-line @-rule:
else if (l = checkAtrules(i)) tokens[i].atrule_type = 3;
else if (l = checkAtrules(i)) tokens[i].atrule_type = 5;
else return 0;

// If token is part of an @-rule, save the rule's length to token:
Expand All @@ -579,10 +581,11 @@ function checkAtrule(i) {
function getAtrule() {
const childType = tokens[pos].atrule_type;

if (childType === 1) return getAtruler(); // @-rule with ruleset
if (childType === 2) return getAtruleb(); // Block @-rule
if (childType === 3) return getAtrules(); // Single-line @-rule
if (childType === 4) return getKeyframesRule();
if (childType === 1) return getKeyframesRule();
if (childType === 2) return getApplyRule();
if (childType === 3) return getAtruler(); // @-rule with ruleset
if (childType === 4) return getAtruleb(); // Block @-rule
if (childType === 5) return getAtrules(); // Single-line @-rule
}

/**
Expand Down Expand Up @@ -764,6 +767,47 @@ function getAtrules() {
return newNode(type, content, line, column);
}

/**
* @param {Number} i Token's index number
* @returns {Number}
*/
function checkApplyRule(i) {
const start = i;
let l;

if (i >= tokensLength) return 0;

if (l = checkAtkeyword(i)) i += l;
else return 0;

const atruleName = joinValues2(i - l, l);
if (atruleName.toLowerCase().indexOf('apply') === -1) return 0;

if (l = checkSC(i)) i += l;

if (l = checkCustomProperty(i)) i += l;
else return 0;

return i - start;
}

/**
* @returns {Node}
*/
function getApplyRule() {
const type = NodeType.AtruleType;
const token = tokens[pos];
const line = token.ln;
const column = token.col;
let content = [].concat(
getAtkeyword(),
getSC(),
getCustomProperty()
);

return newNode(type, content, line, column);
}

/**
* Check if token is part of a block (e.g. `{...}`).
* @param {Number} i Token's index number
Expand Down
1 change: 1 addition & 0 deletions test/css/atrule/14.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@apply --custom-property
Loading