Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

more-css

Package Overview
Dependencies
Maintainers
1
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

more-css - npm Package Compare versions

Comparing version 0.5.5 to 0.6.0

server/parser/Func.js

2

package.json
{
"name": "more-css",
"version": "0.5.5",
"version": "0.6.0",
"description": "a css pre-compiler & agressive compressor",

@@ -5,0 +5,0 @@ "maintainers": [

@@ -33,2 +33,6 @@ var Rule = require('./Rule'),

self.addMatch(new CompleteEqual(Token.HACK, '\\9\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\9'));
self.addMatch(new RegMatch(Token.ID, /^[a-z_\-*][\w\-_]+/i));

@@ -90,6 +94,2 @@ self.addMatch(new RegMatch(Token.ID, /^(\\[a-z\d]{4})+/i));

self.addMatch(new RegMatch(Token.ID, /^[.#]?[a-z_][\w\-_.#]*/i));
self.addMatch(new CompleteEqual(Token.HACK, '\\9\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\9'));
}).methods({

@@ -96,0 +96,0 @@ values: function() {

@@ -6,2 +6,3 @@ var CssLexer = require('./lexer/CssLexer'),

Node = require('./parser/Node'),
Func = require('./parser/Func'),
character = require('./util/character'),

@@ -17,2 +18,3 @@ compress = require('./compress'),

preIndex,
preIndex2,
stack,

@@ -24,2 +26,3 @@ varHash,

styleMap,
funcMap,
levels,

@@ -41,3 +44,3 @@ exArr,

}
preIndex = index;
preIndex2 = preIndex = index;
stack = [];

@@ -49,2 +52,3 @@ varHash = {};

styleMap = {};
funcMap = {};
levels = [];

@@ -116,3 +120,130 @@ exArr = [];

}
function join(node, ignore, inHead, isSelectors, isSelector, isVar, isImport, isExtend, prev, next) {
function preFn(node, ignore) {
var isToken = node.name() == Node.TOKEN;
var isVirtual = isToken && node.token().type() == Token.VIRTUAL;
if(!isToken) {
if(node.name() == Node.FN) {
var leaves = node.leaves();
if(leaves[0].leaves().type() != Token.VIRTUAL) {
while(ignore[++preIndex2]) {}
}
var id = leaves[1].leaves().content();
while(ignore[++preIndex2]) {}
var params = leaves[3].leaves();
while(ignore[++preIndex2]) {}
var p = [];
var phash = {};
params.forEach(function(o, i) {
if(i % 2 == 0) {
var v = o.leaves().content();
p.push(v);
phash[v] = Math.floor(i / 2);
}
while(ignore[++preIndex2]) {}
});
while(ignore[++preIndex2]) {}
var block = leaves[5];
var b = block.leaves().slice(0);
b.shift();
while(ignore[++preIndex2]) {}
b.pop();
var s = '';
var fhash = {};
b.forEach(function(style) {
style = style.leaves();
var key = style[0].leaves()[0].leaves();
s += key.content();
while(ignore[++preIndex2]) {
var ig = ignore[preIndex2];
s += ig.content().replace(/[\r\n]/g, '');
}
s += style[1].leaves().content();
while(ignore[++preIndex2]) {
var ig = ignore[preIndex2];
s += ig.content().replace(/[\r\n]/g, '');
}
var values = style[2].leaves();
values.forEach(function(value) {
var v = value.leaves().content();
var noDollar = v.replace(/^\$/, '');
if(phash.hasOwnProperty(noDollar) || phash.hasOwnProperty('$' + noDollar)) {
fhash[s.length] = {
v: v
};
if(phash.hasOwnProperty(noDollar)) {
fhash[s.length].index = phash[noDollar];
}
else {
fhash[s.length].index = phash['$' + noDollar];
}
}
else if(v.charAt(0) == '$') {
fhash[s.length] = {
v: v,
index: -1
};
}
s += v;
while(ignore[++preIndex2]) {
var ig = ignore[preIndex2];
s += ig.content().replace(/[\r\n]/g, '');
}
});
var end = style[3].leaves();
if(end.type() != Node.VIRTUAL) {
s += end.content();
while(ignore[++preIndex2]) {
var ig = ignore[preIndex2];
s += ig.content().replace(/[\r\n]/g, '');
}
}
});
while(ignore[++preIndex2]) {}
funcMap[id] = new Func(id, p, s, fhash);
}
else {
node.leaves().forEach(function(leaf) {
preFn(leaf, ignore);
});
}
}
else if(!isVirtual) {
while(ignore[++preIndex2]) {}
}
}
function compilerFn(node, ignore, idx) {
var leaves = node.leaves(),
id = leaves[0].leaves().content();
if(funcMap.hasOwnProperty(id)) {
//fncall��ͷ�����ڵ���id��(���հ׼���
while(ignore[++idx]) {}
while(ignore[++idx]) {}
var fn = funcMap[id];
var p = [];
leaves[2].leaves().forEach(function(o, i) {
if(i % 2 == 0) {
var vs = o.leaves();
var s = '';
vs.forEach(function(v, j) {
s += v.leaves().content();
while(ignore[++idx]) {
var ig = ignore[idx];
s += ig.content().replace(/[\r\n]/g, '');
}
});
p.push(s);
}
else{
while(ignore[++idx]) {}
}
});
var s = fn.compile(p, varHash);
res += s;
}
else {
console.error('no function declared: ' + id);
}
}
function join(node, ignore, inHead, isSelectors, isSelector, isVar, isImport, isExtend, isFn, prev, next) {
var isToken = node.name() == Node.TOKEN,

@@ -160,3 +291,4 @@ isVirtual = isToken && node.token().type() == Token.VIRTUAL;

}
else if(!isExtend) {
//�̳кͷ���ֱ�Ӻ���
else if(!isExtend && !isFn) {
//����less��~String����﷨

@@ -222,6 +354,12 @@ if(autoSplit && token.type() == Token.STRING) {

}
else if(node.name() == Node.FN || node.name() == Node.FNC) {
isFn = true;
if(node.name() == Node.FNC) {
compilerFn(node, ignore, index);
}
}
var leaves = node.leaves();
//�ݹ��ӽڵ�
leaves.forEach(function(leaf, i) {
join(leaf, ignore, inHead, isSelectors, isSelector, isVar, isImport, isExtend, leaves[i - 1], leaves[i + 1]);
join(leaf, ignore, inHead, isSelectors, isSelector, isVar, isImport, isExtend, isFn, leaves[i - 1], leaves[i + 1]);
});

@@ -383,5 +521,6 @@ if(node.name() == Node.STYLESET & !inHead) {

exports.parse = function(code, vars, style) {
exports.parse = function(code, vars, style, func) {
vars = vars || {};
style = style || {};
func = func || {};
var lexer = new CssLexer(new CssRule()),

@@ -409,3 +548,8 @@ parser = new Parser(lexer),

});
//�����ʼ������
Object.keys(func).forEach(function(k) {
funcMap[k] = style[k];
});
preVar(node, ignore);
preFn(node, ignore);
join(node, ignore);

@@ -439,2 +583,5 @@ extend();

};
exports.fns = function() {
return funcMap;
};
exports.compress = function(src, agressive) {

@@ -441,0 +588,0 @@ src = src || '';

@@ -15,3 +15,6 @@ var Class = require('../util/Class'),

}).methods({
name: function() {
name: function(t) {
if(t) {
this.type = t;
}
return this.type;

@@ -63,4 +66,9 @@ },

EXTEND: 'extend',
FORMAT: 'format'
FORMAT: 'format',
FN: 'function',
PARAMS: 'params',
FNC: 'fncall',
CPARAMS: 'cparams',
CPARAM: 'cparam'
});
module.exports = Node;

@@ -51,2 +51,13 @@ var Class = require('../util/Class'),

else {
for(var i = this.index; i < this.length; i++) {
var token = this.tokens[i];
if(!S[token.type()]) {
if(token.content() == '(' && this.look.type() == Token.ID) {
return this.fn();
}
else {
break;
}
}
}
return this.styleset();

@@ -83,2 +94,4 @@ }

return this.page();
case '@function':
return this.fn();
default:

@@ -143,3 +156,3 @@ //����less

if(this.look && this.look.content() == '{') {
node.add(this.block());
node.add(this.block(true));
}

@@ -294,2 +307,77 @@ return node;

},
fn: function() {
var node = new Node(Node.FN);
if(this.look.content() == '@function') {
node.add(this.match());
}
else {
node.add(new Node(Node.TOKEN, new Token(Token.VIRTUAL, '@function')));
}
node.add(this.match(Token.ID));
node.add(this.match('('));
node.add(this.params());
node.add(this.match(')'));
node.add(this.block());
return node;
},
params: function() {
var node = new Node(Node.PARAMS);
var hash = {};
while(this.look) {
if(this.look.type() == Token.VARS) {
var v = this.look.content().replace(/^$/, '');
if(hash.hasOwnProperty(v)) {
this.error('duplicate params');
}
hash[v] = true;
node.add(this.match());
if(this.look && this.look.content() == ',') {
node.add(this.match());
}
}
else if(this.look.type() == Token.ID) {
var v = this.look.content();
if(hash.hasOwnProperty(v)) {
this.error('duplicate params');
}
hash[v] = true;
node.add(this.match());
if(this.look && this.look.content() == ',') {
node.add(this.match());
}
}
else {
break;
}
}
return node;
},
fnc: function() {
var node = new Node(Node.FNC);
node.add(this.match(Token.ID));
node.add(this.match('('));
node.add(this.cparams());
node.add(this.match(')'));
node.add(this.match(';'));
return node;
},
cparams: function() {
var node = new Node(Node.CPARAMS);
if(this.look.content() != ')') {
node.add(this.cparam());
}
while(this.look && this.look.content() != ')') {
node.add(this.match(','));
node.add(this.cparam());
}
return node;
},
cparam: function() {
var node = new Node(Node.CPARAM);
node.add(this.match());
while(this.look && [',', ')'].indexOf(this.look.content()) == -1) {
node.add(this.match());
}
return node;
},
styleset: function(numCanBeKey) {

@@ -357,3 +445,3 @@ var node = new Node(Node.STYLESET);

},
block: function() {
block: function(single) {
var node = new Node(Node.BLOCK);

@@ -371,2 +459,6 @@ node.add(this.match('{'));

}
else if(token.content() == '(') {
node.add(this.fnc());
break;
}
for(var j = i; j < this.length; j++) {

@@ -373,0 +465,0 @@ token = this.tokens[j];

@@ -34,2 +34,6 @@ define(function(require, exports, module) {

self.addMatch(new CompleteEqual(Token.HACK, '\\9\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\9'));
self.addMatch(new RegMatch(Token.ID, /^[a-z_\-*][\w\-_]+/i));

@@ -91,6 +95,2 @@ self.addMatch(new RegMatch(Token.ID, /^(\\[a-z\d]{4})+/i));

self.addMatch(new RegMatch(Token.ID, /^[.#]?[a-z_][\w\-_.#]*/i));
self.addMatch(new CompleteEqual(Token.HACK, '\\9\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\9'));
}).methods({

@@ -97,0 +97,0 @@ values: function() {

@@ -7,2 +7,3 @@ define(function(require, exports) {

Node = require('./parser/Node'),
Func = require('./parser/Func'),
character = require('./util/character'),

@@ -18,2 +19,3 @@ compress = require('./compress'),

preIndex,
preIndex2,
stack,

@@ -25,2 +27,3 @@ varHash,

styleMap,
funcMap,
levels,

@@ -42,3 +45,3 @@ exArr,

}
preIndex = index;
preIndex2 = preIndex = index;
stack = [];

@@ -50,2 +53,3 @@ varHash = {};

styleMap = {};
funcMap = {};
levels = [];

@@ -117,3 +121,130 @@ exArr = [];

}
function join(node, ignore, inHead, isSelectors, isSelector, isVar, isImport, isExtend, prev, next) {
function preFn(node, ignore) {
var isToken = node.name() == Node.TOKEN;
var isVirtual = isToken && node.token().type() == Token.VIRTUAL;
if(!isToken) {
if(node.name() == Node.FN) {
var leaves = node.leaves();
if(leaves[0].leaves().type() != Token.VIRTUAL) {
while(ignore[++preIndex2]) {}
}
var id = leaves[1].leaves().content();
while(ignore[++preIndex2]) {}
var params = leaves[3].leaves();
while(ignore[++preIndex2]) {}
var p = [];
var phash = {};
params.forEach(function(o, i) {
if(i % 2 == 0) {
var v = o.leaves().content();
p.push(v);
phash[v] = Math.floor(i / 2);
}
while(ignore[++preIndex2]) {}
});
while(ignore[++preIndex2]) {}
var block = leaves[5];
var b = block.leaves().slice(0);
b.shift();
while(ignore[++preIndex2]) {}
b.pop();
var s = '';
var fhash = {};
b.forEach(function(style) {
style = style.leaves();
var key = style[0].leaves()[0].leaves();
s += key.content();
while(ignore[++preIndex2]) {
var ig = ignore[preIndex2];
s += ig.content().replace(/[\r\n]/g, '');
}
s += style[1].leaves().content();
while(ignore[++preIndex2]) {
var ig = ignore[preIndex2];
s += ig.content().replace(/[\r\n]/g, '');
}
var values = style[2].leaves();
values.forEach(function(value) {
var v = value.leaves().content();
var noDollar = v.replace(/^\$/, '');
if(phash.hasOwnProperty(noDollar) || phash.hasOwnProperty('$' + noDollar)) {
fhash[s.length] = {
v: v
};
if(phash.hasOwnProperty(noDollar)) {
fhash[s.length].index = phash[noDollar];
}
else {
fhash[s.length].index = phash['$' + noDollar];
}
}
else if(v.charAt(0) == '$') {
fhash[s.length] = {
v: v,
index: -1
};
}
s += v;
while(ignore[++preIndex2]) {
var ig = ignore[preIndex2];
s += ig.content().replace(/[\r\n]/g, '');
}
});
var end = style[3].leaves();
if(end.type() != Node.VIRTUAL) {
s += end.content();
while(ignore[++preIndex2]) {
var ig = ignore[preIndex2];
s += ig.content().replace(/[\r\n]/g, '');
}
}
});
while(ignore[++preIndex2]) {}
funcMap[id] = new Func(id, p, s, fhash);
}
else {
node.leaves().forEach(function(leaf) {
preFn(leaf, ignore);
});
}
}
else if(!isVirtual) {
while(ignore[++preIndex2]) {}
}
}
function compilerFn(node, ignore, idx) {
var leaves = node.leaves(),
id = leaves[0].leaves().content();
if(funcMap.hasOwnProperty(id)) {
//fncall��ͷ�����ڵ���id��(���հ׼���
while(ignore[++idx]) {}
while(ignore[++idx]) {}
var fn = funcMap[id];
var p = [];
leaves[2].leaves().forEach(function(o, i) {
if(i % 2 == 0) {
var vs = o.leaves();
var s = '';
vs.forEach(function(v, j) {
s += v.leaves().content();
while(ignore[++idx]) {
var ig = ignore[idx];
s += ig.content().replace(/[\r\n]/g, '');
}
});
p.push(s);
}
else{
while(ignore[++idx]) {}
}
});
var s = fn.compile(p, varHash);
res += s;
}
else {
console.error('no function declared: ' + id);
}
}
function join(node, ignore, inHead, isSelectors, isSelector, isVar, isImport, isExtend, isFn, prev, next) {
var isToken = node.name() == Node.TOKEN,

@@ -161,3 +292,4 @@ isVirtual = isToken && node.token().type() == Token.VIRTUAL;

}
else if(!isExtend) {
//�̳кͷ���ֱ�Ӻ���
else if(!isExtend && !isFn) {
//����less��~String����﷨

@@ -223,6 +355,12 @@ if(autoSplit && token.type() == Token.STRING) {

}
else if(node.name() == Node.FN || node.name() == Node.FNC) {
isFn = true;
if(node.name() == Node.FNC) {
compilerFn(node, ignore, index);
}
}
var leaves = node.leaves();
//�ݹ��ӽڵ�
leaves.forEach(function(leaf, i) {
join(leaf, ignore, inHead, isSelectors, isSelector, isVar, isImport, isExtend, leaves[i - 1], leaves[i + 1]);
join(leaf, ignore, inHead, isSelectors, isSelector, isVar, isImport, isExtend, isFn, leaves[i - 1], leaves[i + 1]);
});

@@ -384,5 +522,6 @@ if(node.name() == Node.STYLESET & !inHead) {

exports.parse = function(code, vars, style) {
exports.parse = function(code, vars, style, func) {
vars = vars || {};
style = style || {};
func = func || {};
var lexer = new CssLexer(new CssRule()),

@@ -410,3 +549,8 @@ parser = new Parser(lexer),

});
//�����ʼ������
Object.keys(func).forEach(function(k) {
funcMap[k] = style[k];
});
preVar(node, ignore);
preFn(node, ignore);
join(node, ignore);

@@ -440,2 +584,5 @@ extend();

};
exports.fns = function() {
return funcMap;
};
exports.compress = function(src, agressive) {

@@ -442,0 +589,0 @@ src = src || '';

@@ -16,3 +16,6 @@ define(function(require, exports, module) {

}).methods({
name: function() {
name: function(t) {
if(t) {
this.type = t;
}
return this.type;

@@ -64,5 +67,10 @@ },

EXTEND: 'extend',
FORMAT: 'format'
FORMAT: 'format',
FN: 'function',
PARAMS: 'params',
FNC: 'fncall',
CPARAMS: 'cparams',
CPARAM: 'cparam'
});
module.exports = Node;
});

@@ -52,2 +52,13 @@ define(function(require, exports, module) {

else {
for(var i = this.index; i < this.length; i++) {
var token = this.tokens[i];
if(!S[token.type()]) {
if(token.content() == '(' && this.look.type() == Token.ID) {
return this.fn();
}
else {
break;
}
}
}
return this.styleset();

@@ -84,2 +95,4 @@ }

return this.page();
case '@function':
return this.fn();
default:

@@ -144,3 +157,3 @@ //����less

if(this.look && this.look.content() == '{') {
node.add(this.block());
node.add(this.block(true));
}

@@ -295,2 +308,77 @@ return node;

},
fn: function() {
var node = new Node(Node.FN);
if(this.look.content() == '@function') {
node.add(this.match());
}
else {
node.add(new Node(Node.TOKEN, new Token(Token.VIRTUAL, '@function')));
}
node.add(this.match(Token.ID));
node.add(this.match('('));
node.add(this.params());
node.add(this.match(')'));
node.add(this.block());
return node;
},
params: function() {
var node = new Node(Node.PARAMS);
var hash = {};
while(this.look) {
if(this.look.type() == Token.VARS) {
var v = this.look.content().replace(/^$/, '');
if(hash.hasOwnProperty(v)) {
this.error('duplicate params');
}
hash[v] = true;
node.add(this.match());
if(this.look && this.look.content() == ',') {
node.add(this.match());
}
}
else if(this.look.type() == Token.ID) {
var v = this.look.content();
if(hash.hasOwnProperty(v)) {
this.error('duplicate params');
}
hash[v] = true;
node.add(this.match());
if(this.look && this.look.content() == ',') {
node.add(this.match());
}
}
else {
break;
}
}
return node;
},
fnc: function() {
var node = new Node(Node.FNC);
node.add(this.match(Token.ID));
node.add(this.match('('));
node.add(this.cparams());
node.add(this.match(')'));
node.add(this.match(';'));
return node;
},
cparams: function() {
var node = new Node(Node.CPARAMS);
if(this.look.content() != ')') {
node.add(this.cparam());
}
while(this.look && this.look.content() != ')') {
node.add(this.match(','));
node.add(this.cparam());
}
return node;
},
cparam: function() {
var node = new Node(Node.CPARAM);
node.add(this.match());
while(this.look && [',', ')'].indexOf(this.look.content()) == -1) {
node.add(this.match());
}
return node;
},
styleset: function(numCanBeKey) {

@@ -358,3 +446,3 @@ var node = new Node(Node.STYLESET);

},
block: function() {
block: function(single) {
var node = new Node(Node.BLOCK);

@@ -372,2 +460,6 @@ node.add(this.match('{'));

}
else if(token.content() == '(') {
node.add(this.fnc());
break;
}
for(var j = i; j < this.length; j++) {

@@ -374,0 +466,0 @@ token = this.tokens[j];

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc