shady-css-parser
Advanced tools
Comparing version 0.0.5 to 0.0.6
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports"], factory); | ||
define(['exports'], factory); | ||
} else if (typeof exports !== "undefined") { | ||
@@ -14,3 +14,3 @@ factory(exports); | ||
})(this, function (exports) { | ||
"use strict"; | ||
'use strict'; | ||
@@ -28,10 +28,10 @@ Object.defineProperty(exports, "__esModule", { | ||
var nodeType = { | ||
stylesheet: 1, | ||
comment: 2, | ||
atRule: 3, | ||
ruleset: 4, | ||
expression: 5, | ||
declaration: 6, | ||
rulelist: 7, | ||
discarded: 8 | ||
stylesheet: 'stylesheet', | ||
comment: 'comment', | ||
atRule: 'atRule', | ||
ruleset: 'ruleset', | ||
expression: 'expression', | ||
declaration: 'declaration', | ||
rulelist: 'rulelist', | ||
discarded: 'discarded' | ||
}; | ||
@@ -357,2 +357,4 @@ exports.matcher = matcher; | ||
this.end = end; | ||
this.previous = null; | ||
this.next = null; | ||
} | ||
@@ -368,2 +370,3 @@ | ||
Token.type = { | ||
none: 0, | ||
whitespace: 1, | ||
@@ -442,3 +445,4 @@ string: 2, | ||
var currentToken = Symbol('currentToken'); | ||
var nextToken = Symbol('nextToken'); | ||
var cursorToken = Symbol('cursorToken'); | ||
var getNextToken = Symbol('getNextToken'); | ||
@@ -450,3 +454,3 @@ var Tokenizer = function () { | ||
this.cssText = cssText; | ||
this.offset = 0; | ||
this[cursorToken] = new _token.Token(_token.Token.type.none, 0, 0); | ||
this[currentToken] = null; | ||
@@ -462,3 +466,3 @@ } | ||
} else { | ||
token = this[nextToken](); | ||
token = this[getNextToken](); | ||
} | ||
@@ -484,3 +488,3 @@ | ||
Tokenizer.prototype[nextToken] = function () { | ||
Tokenizer.prototype[getNextToken] = function () { | ||
var character = this.cssText[this.offset]; | ||
@@ -504,3 +508,5 @@ var token = undefined; | ||
this.offset = token.end; | ||
token.previous = this[cursorToken]; | ||
this[cursorToken].next = token; | ||
this[cursorToken] = token; | ||
return token; | ||
@@ -579,6 +585,11 @@ }; | ||
_createClass(Tokenizer, [{ | ||
key: 'offset', | ||
get: function () { | ||
return this[cursorToken].end; | ||
} | ||
}, { | ||
key: 'currentToken', | ||
get: function () { | ||
if (this[currentToken] == null) { | ||
this[currentToken] = this[nextToken](); | ||
this[currentToken] = this[getNextToken](); | ||
} | ||
@@ -742,5 +753,5 @@ | ||
Parser.prototype.parseDeclarationOrRuleset = function parseDeclarationOrRuleset(tokenizer) { | ||
var rule = ''; | ||
var ruleStart = null; | ||
var ruleEnd = null; | ||
var colon = null; | ||
@@ -757,2 +768,6 @@ while (tokenizer.currentToken) { | ||
} else { | ||
if (tokenizer.currentToken.is(_token.Token.type.colon)) { | ||
colon = tokenizer.currentToken; | ||
} | ||
if (!ruleStart) { | ||
@@ -766,7 +781,5 @@ ruleStart = tokenizer.advance(); | ||
rule = tokenizer.slice(ruleStart, ruleEnd); | ||
if (tokenizer.currentToken.is(_token.Token.type.propertyBoundary)) { | ||
var colonIndex = rule.indexOf(':'); | ||
var value = rule.substr(colonIndex + 1).trim(); | ||
var declarationName = tokenizer.slice(ruleStart, colon.previous); | ||
var expressionValue = tokenizer.slice(colon.next, ruleEnd).trim(); | ||
@@ -777,4 +790,4 @@ if (tokenizer.currentToken.is(_token.Token.type.semicolon)) { | ||
return this.nodeFactory.declaration(rule.substr(0, colonIndex), this.nodeFactory.expression(value)); | ||
} else if (rule[rule.length - 1] === ':') { | ||
return this.nodeFactory.declaration(declarationName, this.nodeFactory.expression(expressionValue)); | ||
} else if (colon && colon === ruleEnd) { | ||
var rulelist = this.parseRulelist(tokenizer); | ||
@@ -786,5 +799,5 @@ | ||
return this.nodeFactory.declaration(rule.substr(0, rule.length - 1), rulelist); | ||
return this.nodeFactory.declaration(tokenizer.slice(ruleStart, ruleEnd.previous), rulelist); | ||
} else { | ||
return this.nodeFactory.ruleset(rule.trim(), this.parseRulelist(tokenizer)); | ||
return this.nodeFactory.ruleset(tokenizer.slice(ruleStart, ruleEnd), this.parseRulelist(tokenizer)); | ||
} | ||
@@ -791,0 +804,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){if("function"==typeof define&&define.amd)define(["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={exports:{}};t(n.exports),e.common=n.exports}}(this,function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var t={whitespace:/\s/,whitespaceGreedy:/(\s+)/g,commentGreedy:/(\*\/)/g,boundary:/[\(\)\{\}'"@;:\s]/,stringBoundary:/['"]/},n={stylesheet:1,comment:2,atRule:3,ruleset:4,expression:5,declaration:6,rulelist:7,discarded:8};e.matcher=t,e.nodeType=n}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./common"],t);else if("undefined"!=typeof exports)t(exports,require("./common"));else{var n={exports:{}};t(n.exports,e.common),e.nodeFactory=n.exports}}(this,function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0}),e.NodeFactory=void 0;var r=function(){function e(){n(this,e)}return e.prototype.stylesheet=function(e){return{type:t.nodeType.stylesheet,rules:e}},e.prototype.atRule=function(e,n,r){return{type:t.nodeType.atRule,name:e,parameters:n,rulelist:r}},e.prototype.comment=function(e){return{type:t.nodeType.comment,value:e}},e.prototype.rulelist=function(e){return{type:t.nodeType.rulelist,rules:e}},e.prototype.ruleset=function(e,n){return{type:t.nodeType.ruleset,selector:e,rulelist:n}},e.prototype.declaration=function(e,n){return{type:t.nodeType.declaration,name:e,value:n}},e.prototype.expression=function(e){return{type:t.nodeType.expression,text:e}},e.prototype.discarded=function(e){return{type:t.nodeType.discarded,text:e}},e}();e.NodeFactory=r}),function(e,t){if("function"==typeof define&&define.amd)define(["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={exports:{}};t(n.exports),e.nodeVisitor=n.exports}}(this,function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),r=Symbol("path"),o=function(){function e(){t(this,e),this[r]=[]}return e.prototype.visit=function(e){var t=void 0;return this[e.type]&&(this[r].push(e),t=this[e.type](e),this[r].pop()),t},n(e,[{key:"path",get:function(){return this[r]}}]),e}();e.NodeVisitor=o}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./common","./node-visitor"],t);else if("undefined"!=typeof exports)t(exports,require("./common"),require("./node-visitor"));else{var n={exports:{}};t(n.exports,e.common,e.nodeVisitor),e.stringifier=n.exports}}(this,function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(e,"__esModule",{value:!0}),e.Stringifier=void 0;var s=function(e){function n(){return r(this,n),o(this,e.apply(this,arguments))}return i(n,e),n.prototype.stringify=function(e){return this.visit(e)||""},n.prototype[t.nodeType.stylesheet]=function(e){for(var t="",n=0;n<e.rules.length;++n)t+=this.visit(e.rules[n]);return t},n.prototype[t.nodeType.atRule]=function(e){return"@"+e.name+(e.parameters?" "+e.parameters:"")+(e.rulelist?""+this.visit(e.rulelist):";")},n.prototype[t.nodeType.rulelist]=function(e){for(var t="{",n=0;n<e.rules.length;++n)t+=this.visit(e.rules[n]);return t+"}"},n.prototype[t.nodeType.comment]=function(e){return""+e.value},n.prototype[t.nodeType.ruleset]=function(e){return""+e.selector+this.visit(e.rulelist)},n.prototype[t.nodeType.declaration]=function(e){return e.name+":"+this.visit(e.value)+";"},n.prototype[t.nodeType.expression]=function(e){return""+e.text},n.prototype[t.nodeType.discarded]=function(e){return""},n}(n.NodeVisitor);e.Stringifier=s}),function(e,t){if("function"==typeof define&&define.amd)define(["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={exports:{}};t(n.exports),e.token=n.exports}}(this,function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function e(n,r,o){t(this,e),this.type=n,this.start=r,this.end=o}return e.prototype.is=function(e){return(this.type&e)===e},e}();n.type={whitespace:1,string:2,comment:4,word:8,boundary:16,propertyBoundary:32,openParenthesis:80,closeParenthesis:144,at:272,openBrace:528,closeBrace:1072,semicolon:2096,colon:4120};var r={"(":n.type.openParenthesis,")":n.type.closeParenthesis,":":n.type.colon,"@":n.type.at,"{":n.type.openBrace,"}":n.type.closeBrace,";":n.type.semicolon,"-":n.type.hyphen,_:n.type.underscore};e.Token=n,e.boundaryTokenTypes=r}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./common","./token"],t);else if("undefined"!=typeof exports)t(exports,require("./common"),require("./token"));else{var n={exports:{}};t(n.exports,e.common,e.token),e.tokenizer=n.exports}}(this,function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0}),e.Tokenizer=void 0;var o=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=Symbol("currentToken"),s=Symbol("nextToken"),u=function(){function e(t){r(this,e),this.cssText=t,this.offset=0,this[i]=null}return e.prototype.advance=function(){var e=void 0;return null!=this[i]?(e=this[i],this[i]=null):e=this[s](),e},e.prototype.slice=function(e,t){return t=t||e,this.cssText.substring(e.start,t.end)},e.prototype.flush=function(){for(var e=[];this.currentToken;)e.push(this.advance());return e},e.prototype[s]=function(){var e=this.cssText[this.offset],n=void 0;return this[i]=null,this.offset>=this.cssText.length?null:(n=t.matcher.whitespace.test(e)?this.tokenizeWhitespace(this.offset):t.matcher.stringBoundary.test(e)?this.tokenizeString(this.offset):"/"===e&&"*"===this.cssText[this.offset+1]?this.tokenizeComment(this.offset):t.matcher.boundary.test(e)?this.tokenizeBoundary(this.offset):this.tokenizeWord(this.offset),this.offset=n.end,n)},e.prototype.tokenizeString=function(e){for(var t=this.cssText[e],r=!1,o=e,i=void 0;i=this.cssText[++e];)if(r)r=!1;else{if(i===t){++e;break}"\\"===i&&(r=!0)}return new n.Token(n.Token.type.string,o,e)},e.prototype.tokenizeWord=function(e){for(var r=e,o=void 0;(o=this.cssText[e])&&!t.matcher.boundary.test(o);)e++;return new n.Token(n.Token.type.word,r,e)},e.prototype.tokenizeWhitespace=function(e){var r=e;t.matcher.whitespaceGreedy.lastIndex=e;var o=t.matcher.whitespaceGreedy.exec(this.cssText);return null!=o&&o.index===e&&(e=t.matcher.whitespaceGreedy.lastIndex),new n.Token(n.Token.type.whitespace,r,e)},e.prototype.tokenizeComment=function(e){var r=e;t.matcher.commentGreedy.lastIndex=e;var o=t.matcher.commentGreedy.exec(this.cssText);return e=null==o?this.cssText.length:t.matcher.commentGreedy.lastIndex,new n.Token(n.Token.type.comment,r,e)},e.prototype.tokenizeBoundary=function(e){var t=n.boundaryTokenTypes[this.cssText[e]]||n.Token.type.boundary;return new n.Token(t,e,e+1)},o(e,[{key:"currentToken",get:function(){return null==this[i]&&(this[i]=this[s]()),this[i]}}]),e}();e.Tokenizer=u}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./tokenizer","./token","./node-factory"],t);else if("undefined"!=typeof exports)t(exports,require("./tokenizer"),require("./token"),require("./node-factory"));else{var n={exports:{}};t(n.exports,e.tokenizer,e.token,e.nodeFactory),e.parser=n.exports}}(this,function(e,t,n,r){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0}),e.Parser=void 0;var i=function(){function e(){var t=arguments.length<=0||void 0===arguments[0]?new r.NodeFactory:arguments[0];o(this,e),this.nodeFactory=t}return e.prototype.parse=function(e){return this.parseStylesheet(new t.Tokenizer(e))},e.prototype.parseStylesheet=function(e){return this.nodeFactory.stylesheet(this.parseRules(e))},e.prototype.parseRules=function(e){for(var t=[];e.currentToken;){var n=this.parseRule(e);n&&t.push(n)}return t},e.prototype.parseRule=function(e){return e.currentToken.is(n.Token.type.whitespace)?(e.advance(),null):e.currentToken.is(n.Token.type.comment)?this.parseComment(e):e.currentToken.is(n.Token.type.word)?this.parseDeclarationOrRuleset(e):e.currentToken.is(n.Token.type.propertyBoundary)?this.parseUnknown(e):e.currentToken.is(n.Token.type.at)?this.parseAtRule(e):this.parseUnknown(e)},e.prototype.parseComment=function(e){return this.nodeFactory.comment(e.slice(e.advance()))},e.prototype.parseUnknown=function(e){for(var t=e.advance(),r=void 0;e.currentToken&&e.currentToken.is(n.Token.type.boundary);)r=e.advance();return this.nodeFactory.discarded(e.slice(t,r))},e.prototype.parseAtRule=function(e){for(var t="",r=null,o=null,i=null;e.currentToken;)if(e.currentToken.is(n.Token.type.whitespace))e.advance();else if(!t&&e.currentToken.is(n.Token.type.at)){e.advance();for(var s=e.currentToken,u=void 0;e.currentToken&&e.currentToken.is(n.Token.type.word);)u=e.advance();t=e.slice(s,u)}else{if(e.currentToken.is(n.Token.type.openBrace)){r=this.parseRulelist(e);break}if(e.currentToken.is(n.Token.type.propertyBoundary)){e.advance();break}null==o?o=e.advance():i=e.advance()}return this.nodeFactory.atRule(t,o?e.slice(o,i):"",r)},e.prototype.parseRulelist=function(e){var t=[];for(e.advance();e.currentToken;){if(e.currentToken.is(n.Token.type.closeBrace)){e.advance();break}var r=this.parseRule(e);r&&t.push(r)}return this.nodeFactory.rulelist(t)},e.prototype.parseDeclarationOrRuleset=function(e){for(var t="",r=null,o=null;e.currentToken;)if(e.currentToken.is(n.Token.type.whitespace))e.advance();else if(e.currentToken.is(n.Token.type.openParenthesis))for(;e.currentToken&&!e.currentToken.is(n.Token.type.closeParenthesis);)e.advance();else{if(e.currentToken.is(n.Token.type.openBrace)||e.currentToken.is(n.Token.type.propertyBoundary))break;r?o=e.advance():r=e.advance()}if(t=e.slice(r,o),e.currentToken.is(n.Token.type.propertyBoundary)){var i=t.indexOf(":"),s=t.substr(i+1).trim();return e.currentToken.is(n.Token.type.semicolon)&&e.advance(),this.nodeFactory.declaration(t.substr(0,i),this.nodeFactory.expression(s))}if(":"===t[t.length-1]){var u=this.parseRulelist(e);return e.currentToken.is(n.Token.type.semicolon)&&e.advance(),this.nodeFactory.declaration(t.substr(0,t.length-1),u)}return this.nodeFactory.ruleset(t.trim(),this.parseRulelist(e))},e}();e.Parser=i}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./shady-css/common","./shady-css/token","./shady-css/tokenizer","./shady-css/node-factory","./shady-css/node-visitor","./shady-css/stringifier","./shady-css/parser"],t);else if("undefined"!=typeof exports)t(exports,require("./shady-css/common"),require("./shady-css/token"),require("./shady-css/tokenizer"),require("./shady-css/node-factory"),require("./shady-css/node-visitor"),require("./shady-css/stringifier"),require("./shady-css/parser"));else{var n={exports:{}};t(n.exports,e.common,e.token,e.tokenizer,e.nodeFactory,e.nodeVisitor,e.stringifier,e.parser),e.shadyCss=n.exports}}(this,function(e,t,n,r,o,i,s,u){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),Object.defineProperty(e,"nodeType",{enumerable:!0,get:function(){return t.nodeType}}),Object.defineProperty(e,"Token",{enumerable:!0,get:function(){return n.Token}}),Object.defineProperty(e,"Tokenizer",{enumerable:!0,get:function(){return r.Tokenizer}}),Object.defineProperty(e,"NodeFactory",{enumerable:!0,get:function(){return o.NodeFactory}}),Object.defineProperty(e,"NodeVisitor",{enumerable:!0,get:function(){return i.NodeVisitor}}),Object.defineProperty(e,"Stringifier",{enumerable:!0,get:function(){return s.Stringifier}}),Object.defineProperty(e,"Parser",{enumerable:!0,get:function(){return u.Parser}})}); | ||
!function(e,t){if("function"==typeof define&&define.amd)define(["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={exports:{}};t(n.exports),e.common=n.exports}}(this,function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var t={whitespace:/\s/,whitespaceGreedy:/(\s+)/g,commentGreedy:/(\*\/)/g,boundary:/[\(\)\{\}'"@;:\s]/,stringBoundary:/['"]/},n={stylesheet:"stylesheet",comment:"comment",atRule:"atRule",ruleset:"ruleset",expression:"expression",declaration:"declaration",rulelist:"rulelist",discarded:"discarded"};e.matcher=t,e.nodeType=n}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./common"],t);else if("undefined"!=typeof exports)t(exports,require("./common"));else{var n={exports:{}};t(n.exports,e.common),e.nodeFactory=n.exports}}(this,function(e,t){"use strict";function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0}),e.NodeFactory=void 0;var r=function(){function e(){n(this,e)}return e.prototype.stylesheet=function(e){return{type:t.nodeType.stylesheet,rules:e}},e.prototype.atRule=function(e,n,r){return{type:t.nodeType.atRule,name:e,parameters:n,rulelist:r}},e.prototype.comment=function(e){return{type:t.nodeType.comment,value:e}},e.prototype.rulelist=function(e){return{type:t.nodeType.rulelist,rules:e}},e.prototype.ruleset=function(e,n){return{type:t.nodeType.ruleset,selector:e,rulelist:n}},e.prototype.declaration=function(e,n){return{type:t.nodeType.declaration,name:e,value:n}},e.prototype.expression=function(e){return{type:t.nodeType.expression,text:e}},e.prototype.discarded=function(e){return{type:t.nodeType.discarded,text:e}},e}();e.NodeFactory=r}),function(e,t){if("function"==typeof define&&define.amd)define(["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={exports:{}};t(n.exports),e.nodeVisitor=n.exports}}(this,function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),r=Symbol("path"),o=function(){function e(){t(this,e),this[r]=[]}return e.prototype.visit=function(e){var t=void 0;return this[e.type]&&(this[r].push(e),t=this[e.type](e),this[r].pop()),t},n(e,[{key:"path",get:function(){return this[r]}}]),e}();e.NodeVisitor=o}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./common","./node-visitor"],t);else if("undefined"!=typeof exports)t(exports,require("./common"),require("./node-visitor"));else{var n={exports:{}};t(n.exports,e.common,e.nodeVisitor),e.stringifier=n.exports}}(this,function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(e,"__esModule",{value:!0}),e.Stringifier=void 0;var s=function(e){function n(){return r(this,n),o(this,e.apply(this,arguments))}return i(n,e),n.prototype.stringify=function(e){return this.visit(e)||""},n.prototype[t.nodeType.stylesheet]=function(e){for(var t="",n=0;n<e.rules.length;++n)t+=this.visit(e.rules[n]);return t},n.prototype[t.nodeType.atRule]=function(e){return"@"+e.name+(e.parameters?" "+e.parameters:"")+(e.rulelist?""+this.visit(e.rulelist):";")},n.prototype[t.nodeType.rulelist]=function(e){for(var t="{",n=0;n<e.rules.length;++n)t+=this.visit(e.rules[n]);return t+"}"},n.prototype[t.nodeType.comment]=function(e){return""+e.value},n.prototype[t.nodeType.ruleset]=function(e){return""+e.selector+this.visit(e.rulelist)},n.prototype[t.nodeType.declaration]=function(e){return e.name+":"+this.visit(e.value)+";"},n.prototype[t.nodeType.expression]=function(e){return""+e.text},n.prototype[t.nodeType.discarded]=function(e){return""},n}(n.NodeVisitor);e.Stringifier=s}),function(e,t){if("function"==typeof define&&define.amd)define(["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={exports:{}};t(n.exports),e.token=n.exports}}(this,function(e){"use strict";function t(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var n=function(){function e(n,r,o){t(this,e),this.type=n,this.start=r,this.end=o,this.previous=null,this.next=null}return e.prototype.is=function(e){return(this.type&e)===e},e}();n.type={none:0,whitespace:1,string:2,comment:4,word:8,boundary:16,propertyBoundary:32,openParenthesis:80,closeParenthesis:144,at:272,openBrace:528,closeBrace:1072,semicolon:2096,colon:4120};var r={"(":n.type.openParenthesis,")":n.type.closeParenthesis,":":n.type.colon,"@":n.type.at,"{":n.type.openBrace,"}":n.type.closeBrace,";":n.type.semicolon,"-":n.type.hyphen,_:n.type.underscore};e.Token=n,e.boundaryTokenTypes=r}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./common","./token"],t);else if("undefined"!=typeof exports)t(exports,require("./common"),require("./token"));else{var n={exports:{}};t(n.exports,e.common,e.token),e.tokenizer=n.exports}}(this,function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0}),e.Tokenizer=void 0;var o=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=Symbol("currentToken"),s=Symbol("cursorToken"),u=Symbol("getNextToken"),c=function(){function e(t){r(this,e),this.cssText=t,this[s]=new n.Token(n.Token.type.none,0,0),this[i]=null}return e.prototype.advance=function(){var e=void 0;return null!=this[i]?(e=this[i],this[i]=null):e=this[u](),e},e.prototype.slice=function(e,t){return t=t||e,this.cssText.substring(e.start,t.end)},e.prototype.flush=function(){for(var e=[];this.currentToken;)e.push(this.advance());return e},e.prototype[u]=function(){var e=this.cssText[this.offset],n=void 0;return this[i]=null,this.offset>=this.cssText.length?null:(n=t.matcher.whitespace.test(e)?this.tokenizeWhitespace(this.offset):t.matcher.stringBoundary.test(e)?this.tokenizeString(this.offset):"/"===e&&"*"===this.cssText[this.offset+1]?this.tokenizeComment(this.offset):t.matcher.boundary.test(e)?this.tokenizeBoundary(this.offset):this.tokenizeWord(this.offset),n.previous=this[s],this[s].next=n,this[s]=n,n)},e.prototype.tokenizeString=function(e){for(var t=this.cssText[e],r=!1,o=e,i=void 0;i=this.cssText[++e];)if(r)r=!1;else{if(i===t){++e;break}"\\"===i&&(r=!0)}return new n.Token(n.Token.type.string,o,e)},e.prototype.tokenizeWord=function(e){for(var r=e,o=void 0;(o=this.cssText[e])&&!t.matcher.boundary.test(o);)e++;return new n.Token(n.Token.type.word,r,e)},e.prototype.tokenizeWhitespace=function(e){var r=e;t.matcher.whitespaceGreedy.lastIndex=e;var o=t.matcher.whitespaceGreedy.exec(this.cssText);return null!=o&&o.index===e&&(e=t.matcher.whitespaceGreedy.lastIndex),new n.Token(n.Token.type.whitespace,r,e)},e.prototype.tokenizeComment=function(e){var r=e;t.matcher.commentGreedy.lastIndex=e;var o=t.matcher.commentGreedy.exec(this.cssText);return e=null==o?this.cssText.length:t.matcher.commentGreedy.lastIndex,new n.Token(n.Token.type.comment,r,e)},e.prototype.tokenizeBoundary=function(e){var t=n.boundaryTokenTypes[this.cssText[e]]||n.Token.type.boundary;return new n.Token(t,e,e+1)},o(e,[{key:"offset",get:function(){return this[s].end}},{key:"currentToken",get:function(){return null==this[i]&&(this[i]=this[u]()),this[i]}}]),e}();e.Tokenizer=c}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./tokenizer","./token","./node-factory"],t);else if("undefined"!=typeof exports)t(exports,require("./tokenizer"),require("./token"),require("./node-factory"));else{var n={exports:{}};t(n.exports,e.tokenizer,e.token,e.nodeFactory),e.parser=n.exports}}(this,function(e,t,n,r){"use strict";function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0}),e.Parser=void 0;var i=function(){function e(){var t=arguments.length<=0||void 0===arguments[0]?new r.NodeFactory:arguments[0];o(this,e),this.nodeFactory=t}return e.prototype.parse=function(e){return this.parseStylesheet(new t.Tokenizer(e))},e.prototype.parseStylesheet=function(e){return this.nodeFactory.stylesheet(this.parseRules(e))},e.prototype.parseRules=function(e){for(var t=[];e.currentToken;){var n=this.parseRule(e);n&&t.push(n)}return t},e.prototype.parseRule=function(e){return e.currentToken.is(n.Token.type.whitespace)?(e.advance(),null):e.currentToken.is(n.Token.type.comment)?this.parseComment(e):e.currentToken.is(n.Token.type.word)?this.parseDeclarationOrRuleset(e):e.currentToken.is(n.Token.type.propertyBoundary)?this.parseUnknown(e):e.currentToken.is(n.Token.type.at)?this.parseAtRule(e):this.parseUnknown(e)},e.prototype.parseComment=function(e){return this.nodeFactory.comment(e.slice(e.advance()))},e.prototype.parseUnknown=function(e){for(var t=e.advance(),r=void 0;e.currentToken&&e.currentToken.is(n.Token.type.boundary);)r=e.advance();return this.nodeFactory.discarded(e.slice(t,r))},e.prototype.parseAtRule=function(e){for(var t="",r=null,o=null,i=null;e.currentToken;)if(e.currentToken.is(n.Token.type.whitespace))e.advance();else if(!t&&e.currentToken.is(n.Token.type.at)){e.advance();for(var s=e.currentToken,u=void 0;e.currentToken&&e.currentToken.is(n.Token.type.word);)u=e.advance();t=e.slice(s,u)}else{if(e.currentToken.is(n.Token.type.openBrace)){r=this.parseRulelist(e);break}if(e.currentToken.is(n.Token.type.propertyBoundary)){e.advance();break}null==o?o=e.advance():i=e.advance()}return this.nodeFactory.atRule(t,o?e.slice(o,i):"",r)},e.prototype.parseRulelist=function(e){var t=[];for(e.advance();e.currentToken;){if(e.currentToken.is(n.Token.type.closeBrace)){e.advance();break}var r=this.parseRule(e);r&&t.push(r)}return this.nodeFactory.rulelist(t)},e.prototype.parseDeclarationOrRuleset=function(e){for(var t=null,r=null,o=null;e.currentToken;)if(e.currentToken.is(n.Token.type.whitespace))e.advance();else if(e.currentToken.is(n.Token.type.openParenthesis))for(;e.currentToken&&!e.currentToken.is(n.Token.type.closeParenthesis);)e.advance();else{if(e.currentToken.is(n.Token.type.openBrace)||e.currentToken.is(n.Token.type.propertyBoundary))break;e.currentToken.is(n.Token.type.colon)&&(o=e.currentToken),t?r=e.advance():t=e.advance()}if(e.currentToken.is(n.Token.type.propertyBoundary)){var i=e.slice(t,o.previous),s=e.slice(o.next,r).trim();return e.currentToken.is(n.Token.type.semicolon)&&e.advance(),this.nodeFactory.declaration(i,this.nodeFactory.expression(s))}if(o&&o===r){var u=this.parseRulelist(e);return e.currentToken.is(n.Token.type.semicolon)&&e.advance(),this.nodeFactory.declaration(e.slice(t,r.previous),u)}return this.nodeFactory.ruleset(e.slice(t,r),this.parseRulelist(e))},e}();e.Parser=i}),function(e,t){if("function"==typeof define&&define.amd)define(["exports","./shady-css/common","./shady-css/token","./shady-css/tokenizer","./shady-css/node-factory","./shady-css/node-visitor","./shady-css/stringifier","./shady-css/parser"],t);else if("undefined"!=typeof exports)t(exports,require("./shady-css/common"),require("./shady-css/token"),require("./shady-css/tokenizer"),require("./shady-css/node-factory"),require("./shady-css/node-visitor"),require("./shady-css/stringifier"),require("./shady-css/parser"));else{var n={exports:{}};t(n.exports,e.common,e.token,e.tokenizer,e.nodeFactory,e.nodeVisitor,e.stringifier,e.parser),e.shadyCss=n.exports}}(this,function(e,t,n,r,o,i,s,u){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),Object.defineProperty(e,"nodeType",{enumerable:!0,get:function(){return t.nodeType}}),Object.defineProperty(e,"Token",{enumerable:!0,get:function(){return n.Token}}),Object.defineProperty(e,"Tokenizer",{enumerable:!0,get:function(){return r.Tokenizer}}),Object.defineProperty(e,"NodeFactory",{enumerable:!0,get:function(){return o.NodeFactory}}),Object.defineProperty(e,"NodeVisitor",{enumerable:!0,get:function(){return i.NodeVisitor}}),Object.defineProperty(e,"Stringifier",{enumerable:!0,get:function(){return s.Stringifier}}),Object.defineProperty(e,"Parser",{enumerable:!0,get:function(){return u.Parser}})}); |
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports"], factory); | ||
define(['exports'], factory); | ||
} else if (typeof exports !== "undefined") { | ||
@@ -14,3 +14,3 @@ factory(exports); | ||
})(this, function (exports) { | ||
"use strict"; | ||
'use strict'; | ||
@@ -28,10 +28,10 @@ Object.defineProperty(exports, "__esModule", { | ||
var nodeType = { | ||
stylesheet: 1, | ||
comment: 2, | ||
atRule: 3, | ||
ruleset: 4, | ||
expression: 5, | ||
declaration: 6, | ||
rulelist: 7, | ||
discarded: 8 | ||
stylesheet: 'stylesheet', | ||
comment: 'comment', | ||
atRule: 'atRule', | ||
ruleset: 'ruleset', | ||
expression: 'expression', | ||
declaration: 'declaration', | ||
rulelist: 'rulelist', | ||
discarded: 'discarded' | ||
}; | ||
@@ -38,0 +38,0 @@ exports.matcher = matcher; |
@@ -148,5 +148,5 @@ (function (global, factory) { | ||
Parser.prototype.parseDeclarationOrRuleset = function parseDeclarationOrRuleset(tokenizer) { | ||
var rule = ''; | ||
var ruleStart = null; | ||
var ruleEnd = null; | ||
var colon = null; | ||
@@ -163,2 +163,6 @@ while (tokenizer.currentToken) { | ||
} else { | ||
if (tokenizer.currentToken.is(_token.Token.type.colon)) { | ||
colon = tokenizer.currentToken; | ||
} | ||
if (!ruleStart) { | ||
@@ -172,7 +176,5 @@ ruleStart = tokenizer.advance(); | ||
rule = tokenizer.slice(ruleStart, ruleEnd); | ||
if (tokenizer.currentToken.is(_token.Token.type.propertyBoundary)) { | ||
var colonIndex = rule.indexOf(':'); | ||
var value = rule.substr(colonIndex + 1).trim(); | ||
var declarationName = tokenizer.slice(ruleStart, colon.previous); | ||
var expressionValue = tokenizer.slice(colon.next, ruleEnd).trim(); | ||
@@ -183,4 +185,4 @@ if (tokenizer.currentToken.is(_token.Token.type.semicolon)) { | ||
return this.nodeFactory.declaration(rule.substr(0, colonIndex), this.nodeFactory.expression(value)); | ||
} else if (rule[rule.length - 1] === ':') { | ||
return this.nodeFactory.declaration(declarationName, this.nodeFactory.expression(expressionValue)); | ||
} else if (colon && colon === ruleEnd) { | ||
var rulelist = this.parseRulelist(tokenizer); | ||
@@ -192,5 +194,5 @@ | ||
return this.nodeFactory.declaration(rule.substr(0, rule.length - 1), rulelist); | ||
return this.nodeFactory.declaration(tokenizer.slice(ruleStart, ruleEnd.previous), rulelist); | ||
} else { | ||
return this.nodeFactory.ruleset(rule.trim(), this.parseRulelist(tokenizer)); | ||
return this.nodeFactory.ruleset(tokenizer.slice(ruleStart, ruleEnd), this.parseRulelist(tokenizer)); | ||
} | ||
@@ -197,0 +199,0 @@ }; |
@@ -33,2 +33,4 @@ (function (global, factory) { | ||
this.end = end; | ||
this.previous = null; | ||
this.next = null; | ||
} | ||
@@ -44,2 +46,3 @@ | ||
Token.type = { | ||
none: 0, | ||
whitespace: 1, | ||
@@ -46,0 +49,0 @@ string: 2, |
@@ -46,3 +46,4 @@ (function (global, factory) { | ||
var currentToken = Symbol('currentToken'); | ||
var nextToken = Symbol('nextToken'); | ||
var cursorToken = Symbol('cursorToken'); | ||
var getNextToken = Symbol('getNextToken'); | ||
@@ -54,3 +55,3 @@ var Tokenizer = function () { | ||
this.cssText = cssText; | ||
this.offset = 0; | ||
this[cursorToken] = new _token.Token(_token.Token.type.none, 0, 0); | ||
this[currentToken] = null; | ||
@@ -66,3 +67,3 @@ } | ||
} else { | ||
token = this[nextToken](); | ||
token = this[getNextToken](); | ||
} | ||
@@ -88,3 +89,3 @@ | ||
Tokenizer.prototype[nextToken] = function () { | ||
Tokenizer.prototype[getNextToken] = function () { | ||
var character = this.cssText[this.offset]; | ||
@@ -108,3 +109,5 @@ var token = undefined; | ||
this.offset = token.end; | ||
token.previous = this[cursorToken]; | ||
this[cursorToken].next = token; | ||
this[cursorToken] = token; | ||
return token; | ||
@@ -183,6 +186,11 @@ }; | ||
_createClass(Tokenizer, [{ | ||
key: 'offset', | ||
get: function () { | ||
return this[cursorToken].end; | ||
} | ||
}, { | ||
key: 'currentToken', | ||
get: function () { | ||
if (this[currentToken] == null) { | ||
this[currentToken] = this[nextToken](); | ||
this[currentToken] = this[getNextToken](); | ||
} | ||
@@ -189,0 +197,0 @@ |
{ | ||
"name": "shady-css-parser", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "A fast, small and flexible CSS parser.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -32,12 +32,12 @@ /** | ||
const nodeType = { | ||
stylesheet: 1, | ||
comment: 2, | ||
atRule: 3, | ||
ruleset: 4, | ||
expression: 5, | ||
declaration: 6, | ||
rulelist: 7, | ||
discarded: 8 | ||
stylesheet: 'stylesheet', | ||
comment: 'comment', | ||
atRule: 'atRule', | ||
ruleset: 'ruleset', | ||
expression: 'expression', | ||
declaration: 'declaration', | ||
rulelist: 'rulelist', | ||
discarded: 'discarded' | ||
}; | ||
export { matcher, nodeType }; |
@@ -204,5 +204,5 @@ /** | ||
parseDeclarationOrRuleset(tokenizer) { | ||
let rule = ''; | ||
let ruleStart = null; | ||
let ruleEnd = null; | ||
let colon = null; | ||
@@ -221,2 +221,6 @@ while (tokenizer.currentToken) { | ||
} else { | ||
if (tokenizer.currentToken.is(Token.type.colon)) { | ||
colon = tokenizer.currentToken; | ||
} | ||
if (!ruleStart) { | ||
@@ -230,9 +234,7 @@ ruleStart = tokenizer.advance(); | ||
rule = tokenizer.slice(ruleStart, ruleEnd); | ||
// A ruleset never contains or ends with a semi-colon. | ||
if (tokenizer.currentToken.is(Token.type.propertyBoundary)) { | ||
let colonIndex = rule.indexOf(':'); | ||
let declarationName = tokenizer.slice(ruleStart, colon.previous); | ||
// TODO(cdata): is .trim() bad for performance? | ||
let value = rule.substr(colonIndex + 1).trim(); | ||
let expressionValue = tokenizer.slice(colon.next, ruleEnd).trim(); | ||
@@ -244,5 +246,6 @@ if (tokenizer.currentToken.is(Token.type.semicolon)) { | ||
return this.nodeFactory.declaration( | ||
rule.substr(0, colonIndex), | ||
this.nodeFactory.expression(value)); | ||
} else if (rule[rule.length - 1] === ':') { | ||
declarationName, | ||
this.nodeFactory.expression(expressionValue)); | ||
// This is the case for a mixin-like structure: | ||
} else if (colon && colon === ruleEnd) { | ||
let rulelist = this.parseRulelist(tokenizer); | ||
@@ -255,5 +258,8 @@ | ||
return this.nodeFactory.declaration( | ||
rule.substr(0, rule.length - 1), rulelist); | ||
tokenizer.slice(ruleStart, ruleEnd.previous), rulelist); | ||
// Otherwise, this is a ruleset:kkkkk | ||
} else { | ||
return this.nodeFactory.ruleset(rule.trim(), this.parseRulelist(tokenizer)); | ||
return this.nodeFactory.ruleset( | ||
tokenizer.slice(ruleStart, ruleEnd), | ||
this.parseRulelist(tokenizer)); | ||
} | ||
@@ -260,0 +266,0 @@ } |
@@ -27,2 +27,4 @@ /** | ||
this.end = end; | ||
this.previous = null; | ||
this.next = null; | ||
} | ||
@@ -49,2 +51,3 @@ | ||
Token.type = { | ||
none: 0, | ||
whitespace: 1, | ||
@@ -51,0 +54,0 @@ string: 2, |
@@ -15,3 +15,4 @@ /** | ||
const currentToken = Symbol('currentToken'); | ||
const nextToken = Symbol('nextToken'); | ||
const cursorToken = Symbol('cursorToken'); | ||
const getNextToken = Symbol('getNextToken'); | ||
@@ -26,9 +27,26 @@ /** | ||
* @param {string} cssText The raw CSS string to be tokenized. | ||
* | ||
*/ | ||
constructor(cssText) { | ||
this.cssText = cssText; | ||
this.offset = 0; | ||
/** | ||
* Tracks the position of the tokenizer in the source string. | ||
* Also the default head of the Token linked list. | ||
* @type {!Token} | ||
* @private | ||
*/ | ||
this[cursorToken] = new Token(Token.type.none, 0, 0); | ||
/** | ||
* Holds a reference to a Token that is "next" in the source string, often | ||
* due to having been peeked at. | ||
* @type {?Token} | ||
* @readonly | ||
*/ | ||
this[currentToken] = null; | ||
} | ||
get offset() { | ||
return this[cursorToken].end; | ||
} | ||
/** | ||
@@ -42,3 +60,3 @@ * The current token that will be returned by a call to `advance`. This | ||
if (this[currentToken] == null) { | ||
this[currentToken] = this[nextToken](); | ||
this[currentToken] = this[getNextToken](); | ||
} | ||
@@ -60,3 +78,3 @@ | ||
} else { | ||
token = this[nextToken](); | ||
token = this[getNextToken](); | ||
} | ||
@@ -100,3 +118,3 @@ return token; | ||
*/ | ||
[nextToken]() { | ||
[getNextToken]() { | ||
let character = this.cssText[this.offset]; | ||
@@ -121,3 +139,5 @@ let token; | ||
this.offset = token.end; | ||
token.previous = this[cursorToken]; | ||
this[cursorToken].next = token; | ||
this[cursorToken] = token; | ||
@@ -124,0 +144,0 @@ return token; |
@@ -12,2 +12,3 @@ /** | ||
import { expect } from 'chai'; | ||
import { Token } from '../src/shady-css/token'; | ||
@@ -30,2 +31,22 @@ function expectTokenType(token, type) { | ||
export { expectTokenType, expectTokenSequence }; | ||
function linkedTokens(tokens) { | ||
tokens.reduce(function(l, r) { | ||
if (l) { | ||
l.next = r; | ||
} | ||
if (r) { | ||
r.previous = l; | ||
} | ||
return r; | ||
}, new Token(Token.type.none, 0, 0)); | ||
return tokens; | ||
} | ||
export { | ||
expectTokenType, | ||
expectTokenSequence, | ||
linkedTokens | ||
}; |
@@ -13,13 +13,12 @@ /** | ||
import * as fixtures from './fixtures'; | ||
import { DebugNodeFactory } from './debug-node-factory'; | ||
import { Parser } from '../src/shady-css/parser'; | ||
import { nodeType } from '../src/shady-css/common'; | ||
const nodeFactory = new DebugNodeFactory(); | ||
describe('Parser', () => { | ||
let parser; | ||
let nodeFactory; | ||
beforeEach(() => { | ||
parser = new Parser(nodeFactory); | ||
parser = new Parser(); | ||
nodeFactory = parser.nodeFactory; | ||
}); | ||
@@ -26,0 +25,0 @@ |
@@ -20,18 +20,18 @@ /** | ||
it('can identify strings', () => { | ||
expect(new Tokenizer('"foo"').advance()).to.be.eql( | ||
new Token(Token.type.string, 0, 5)); | ||
expect(new Tokenizer('"foo"').flush()).to.be.eql( | ||
helpers.linkedTokens([new Token(Token.type.string, 0, 5)])); | ||
}); | ||
it('can identify comments', () => { | ||
expect(new Tokenizer('/*foo*/').advance()).to.be.eql( | ||
new Token(Token.type.comment, 0, 7)); | ||
expect(new Tokenizer('/*foo*/').flush()).to.be.eql( | ||
helpers.linkedTokens([new Token(Token.type.comment, 0, 7)])); | ||
}); | ||
it('can identify words', () => { | ||
expect(new Tokenizer('font-family').advance()).to.be.eql( | ||
new Token(Token.type.word, 0, 11)); | ||
expect(new Tokenizer('font-family').flush()).to.be.eql( | ||
helpers.linkedTokens([new Token(Token.type.word, 0, 11)])); | ||
}); | ||
it('can identify boundaries', () => { | ||
expect(new Tokenizer('@{};()').flush()).to.be.eql([ | ||
expect(new Tokenizer('@{};()').flush()).to.be.eql(helpers.linkedTokens([ | ||
new Token(Token.type.at, 0, 1), | ||
@@ -43,3 +43,3 @@ new Token(Token.type.openBrace, 1, 2), | ||
new Token(Token.type.closeParenthesis, 5, 6) | ||
]); | ||
])); | ||
}); | ||
@@ -46,0 +46,0 @@ }); |
129544
2981
35