-
Notifications
You must be signed in to change notification settings - Fork 11
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
5 changed files
with
247 additions
and
141 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 |
---|---|---|
@@ -1,137 +1,2 @@ | ||
import { tokenTypes as TYPE } from 'css-tree'; | ||
import * as LessVariableReference from './less/LessVariableReference.js'; | ||
import * as LessVariable from './less/LessVariable.js'; | ||
import * as LessEscaping from './less/LessEscaping.js'; | ||
import * as LessNamespace from './less/LessNamespace.js'; | ||
import * as SassVariable from './sass/SassVariable.js'; | ||
import * as SassInterpolation from './sass/SassInterpolation.js'; | ||
import * as SassNamespace from './sass/SassNamespace.js'; | ||
|
||
const NUMBERSIGN = 0x0023; // U+0023 NUMBER SIGN (#) | ||
const DOLLARSIGN = 0x0024; // U+0024 DOLLAR SIGN ($) | ||
const PERCENTAGESIGN = 0x0025; // U+0025 PERCENTAGE SIGN (%) | ||
const FULLSTOP = 0x002E; // U+002E FULL STOP (.) | ||
const GREATERTHANSIGN = 0x003E; // U+003E GREATER-THAN SIGN (>) | ||
const COMMERCIALAT = 0x0040; // U+0040 COMMERCIAL AT (@) | ||
const TILDE = 0x007E; // U+007E TILDE (~) | ||
|
||
// custom error | ||
class PreprocessorExtensionError { | ||
constructor() { | ||
this.type = 'PreprocessorExtensionError'; | ||
} | ||
} | ||
|
||
export function less(syntaxConfig) { | ||
// new node types | ||
syntaxConfig.node.LessVariableReference = LessVariableReference; | ||
syntaxConfig.node.LessVariable = LessVariable; | ||
syntaxConfig.node.LessEscaping = LessEscaping; | ||
syntaxConfig.node.LessNamespace = LessNamespace; | ||
|
||
// extend parser value parser | ||
const originalGetNode = syntaxConfig.scope.Value.getNode; | ||
syntaxConfig.scope.Value.getNode = function(context) { | ||
let node = null; | ||
|
||
switch (this.tokenType) { | ||
case TYPE.AtKeyword: // less: @var | ||
node = this.LessVariable(); | ||
break; | ||
|
||
case TYPE.Hash: { | ||
let sc = 0; | ||
let tokenType = 0; | ||
|
||
// deprecated | ||
do { | ||
tokenType = this.lookupType(++sc); | ||
if (tokenType !== TYPE.WhiteSpace && tokenType !== TYPE.Comment) { | ||
break; | ||
} | ||
} while (tokenType !== TYPE.EOF); | ||
|
||
if (this.isDelim(FULLSTOP, sc) || /* preferred */ | ||
this.isDelim(GREATERTHANSIGN, sc) /* deprecated */) { | ||
node = this.LessNamespace(); | ||
} | ||
|
||
break; | ||
} | ||
|
||
case TYPE.Delim: | ||
switch (this.source.charCodeAt(this.tokenStart)) { | ||
case COMMERCIALAT: // less: @@var | ||
if (this.lookupType(1) === TYPE.AtKeyword) { | ||
node = this.LessVariableReference(); | ||
} | ||
break; | ||
|
||
case TILDE: // less: ~"asd" | ~'asd' | ||
node = this.LessEscaping(); | ||
break; | ||
|
||
|
||
} | ||
|
||
break; | ||
} | ||
|
||
// currently we can't validate values that contain less/sass extensions | ||
if (node !== null) { | ||
throw new PreprocessorExtensionError(); | ||
} | ||
|
||
return originalGetNode.call(this, context); | ||
}; | ||
|
||
return syntaxConfig; | ||
} | ||
|
||
export function sass(syntaxConfig) { | ||
// new node types | ||
syntaxConfig.node.SassVariable = SassVariable; | ||
syntaxConfig.node.SassInterpolation = SassInterpolation; | ||
syntaxConfig.node.SassNamespace = SassNamespace; | ||
|
||
// extend parser value parser | ||
const originalGetNode = syntaxConfig.scope.Value.getNode; | ||
syntaxConfig.scope.Value.getNode = function(context) { | ||
let node = null; | ||
|
||
switch (this.tokenType) { | ||
case TYPE.Ident: | ||
if (this.isDelim(FULLSTOP, 1)) { | ||
node = this.SassNamespace(); | ||
} | ||
break; | ||
|
||
case TYPE.Delim: | ||
switch (this.source.charCodeAt(this.tokenStart)) { | ||
case DOLLARSIGN: // sass: $var | ||
node = this.SassVariable(); | ||
break; | ||
|
||
case NUMBERSIGN: // sass: #{ } | ||
if (this.lookupType(1) === TYPE.LeftCurlyBracket) { | ||
node = this.SassInterpolation(this.scope.Value, this.readSequence); | ||
} | ||
break; | ||
|
||
case PERCENTAGESIGN: // sass: 5 % 4 | ||
node = this.Operator(); | ||
break; | ||
} | ||
break; | ||
} | ||
|
||
// currently we can't validate values that contain less/sass extensions | ||
if (node !== null) { | ||
throw new PreprocessorExtensionError(); | ||
} | ||
|
||
return originalGetNode.call(this, context); | ||
}; | ||
|
||
return syntaxConfig; | ||
}; | ||
export { default as less } from './less/index.js'; | ||
export { default as sass } from './sass/index.js'; |
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,88 @@ | ||
import { tokenTypes as TYPE } from 'css-tree'; | ||
import * as LessVariableReference from './LessVariableReference.js'; | ||
import * as LessVariable from './LessVariable.js'; | ||
import * as LessEscaping from './LessEscaping.js'; | ||
import * as LessNamespace from './LessNamespace.js'; | ||
|
||
const FULLSTOP = 0x002E; // U+002E FULL STOP (.) | ||
const GREATERTHANSIGN = 0x003E; // U+003E GREATER-THAN SIGN (>) | ||
const COMMERCIALAT = 0x0040; // U+0040 COMMERCIAL AT (@) | ||
const TILDE = 0x007E; // U+007E TILDE (~) | ||
|
||
// custom error | ||
class PreprocessorExtensionError { | ||
constructor() { | ||
this.type = 'PreprocessorExtensionError'; | ||
} | ||
} | ||
|
||
export default function less(syntaxConfig) { | ||
// new node types | ||
syntaxConfig.node.LessVariableReference = LessVariableReference; | ||
syntaxConfig.node.LessVariable = LessVariable; | ||
syntaxConfig.node.LessEscaping = LessEscaping; | ||
syntaxConfig.node.LessNamespace = LessNamespace; | ||
|
||
// custom at-rules | ||
syntaxConfig.atrules.plugin = { | ||
prelude: '<string>' | ||
}; | ||
|
||
// extend parser value parser | ||
const originalGetNode = syntaxConfig.scope.Value.getNode; | ||
syntaxConfig.scope.Value.getNode = function(context) { | ||
let node = null; | ||
|
||
switch (this.tokenType) { | ||
case TYPE.AtKeyword: // less: @var | ||
node = this.LessVariable(); | ||
break; | ||
|
||
case TYPE.Hash: { | ||
let sc = 0; | ||
let tokenType = 0; | ||
|
||
// deprecated | ||
do { | ||
tokenType = this.lookupType(++sc); | ||
if (tokenType !== TYPE.WhiteSpace && tokenType !== TYPE.Comment) { | ||
break; | ||
} | ||
} while (tokenType !== TYPE.EOF); | ||
|
||
if (this.isDelim(FULLSTOP, sc) || /* preferred */ | ||
this.isDelim(GREATERTHANSIGN, sc) /* deprecated */) { | ||
node = this.LessNamespace(); | ||
} | ||
|
||
break; | ||
} | ||
|
||
case TYPE.Delim: | ||
switch (this.source.charCodeAt(this.tokenStart)) { | ||
case COMMERCIALAT: // less: @@var | ||
if (this.lookupType(1) === TYPE.AtKeyword) { | ||
node = this.LessVariableReference(); | ||
} | ||
break; | ||
|
||
case TILDE: // less: ~"asd" | ~'asd' | ||
node = this.LessEscaping(); | ||
break; | ||
|
||
|
||
} | ||
|
||
break; | ||
} | ||
|
||
// currently we can't validate values that contain less/sass extensions | ||
if (node !== null) { | ||
throw new PreprocessorExtensionError(); | ||
} | ||
|
||
return originalGetNode.call(this, context); | ||
}; | ||
|
||
return syntaxConfig; | ||
} |
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,120 @@ | ||
import { tokenTypes as TYPE } from 'css-tree'; | ||
import * as SassVariable from './SassVariable.js'; | ||
import * as SassInterpolation from './SassInterpolation.js'; | ||
import * as SassNamespace from './SassNamespace.js'; | ||
|
||
const NUMBERSIGN = 0x0023; // U+0023 NUMBER SIGN (#) | ||
const DOLLARSIGN = 0x0024; // U+0024 DOLLAR SIGN ($) | ||
const PERCENTAGESIGN = 0x0025; // U+0025 PERCENTAGE SIGN (%) | ||
const FULLSTOP = 0x002E; // U+002E FULL STOP (.) | ||
|
||
// custom error | ||
class PreprocessorExtensionError { | ||
constructor() { | ||
this.type = 'PreprocessorExtensionError'; | ||
} | ||
} | ||
|
||
export default function sass(syntaxConfig) { | ||
// new node types | ||
syntaxConfig.node.SassVariable = SassVariable; | ||
syntaxConfig.node.SassInterpolation = SassInterpolation; | ||
syntaxConfig.node.SassNamespace = SassNamespace; | ||
|
||
// custom at-rules | ||
syntaxConfig.atrules['at-root'] = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.content = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.debug = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.each = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.else = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.error = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.extend = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.for = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.forward = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.function = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.if = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.import = { | ||
prelude: syntaxConfig.atrules.import.prelude + '| <string>#' // FIXME: fix prelude extension in css-tree | ||
}; | ||
syntaxConfig.atrules.include = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.mixin = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.return = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.use = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.warn = { | ||
prelude: '<any-value>' | ||
}; | ||
syntaxConfig.atrules.while = { | ||
prelude: '<any-value>' | ||
}; | ||
|
||
// extend parser value parser | ||
const originalGetNode = syntaxConfig.scope.Value.getNode; | ||
syntaxConfig.scope.Value.getNode = function(context) { | ||
let node = null; | ||
|
||
switch (this.tokenType) { | ||
case TYPE.Ident: | ||
if (this.isDelim(FULLSTOP, 1)) { | ||
node = this.SassNamespace(); | ||
} | ||
break; | ||
|
||
case TYPE.Delim: | ||
switch (this.source.charCodeAt(this.tokenStart)) { | ||
case DOLLARSIGN: // sass: $var | ||
node = this.SassVariable(); | ||
break; | ||
|
||
case NUMBERSIGN: // sass: #{ } | ||
if (this.lookupType(1) === TYPE.LeftCurlyBracket) { | ||
node = this.SassInterpolation(this.scope.Value, this.readSequence); | ||
} | ||
break; | ||
|
||
case PERCENTAGESIGN: // sass: 5 % 4 | ||
node = this.Operator(); | ||
break; | ||
} | ||
break; | ||
} | ||
|
||
// currently we can't validate values that contain less/sass extensions | ||
if (node !== null) { | ||
throw new PreprocessorExtensionError(); | ||
} | ||
|
||
return originalGetNode.call(this, context); | ||
}; | ||
|
||
return syntaxConfig; | ||
}; |
Oops, something went wrong.