Skip to content

Commit

Permalink
new hack, kw and hack can be out of {}
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Jun 18, 2014
1 parent 76b9357 commit c05ba0d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 54 deletions.
39 changes: 14 additions & 25 deletions src/lexer/CssLexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,11 @@ var CssLexer = Lexer.extend(function(rule) {
}
else {
if(this.rule.keyWords().hasOwnProperty(s)) {
if(this.depth || this.media || this.import) {
token.type(Token.KEYWORD);
this.isKw = true;
this.isUrl = false;
this.isVar = false;
this.isSelector = false;
}
else {
token.type(Token.IGNORE);
}
token.type(Token.KEYWORD);
this.isKw = true;
this.isUrl = false;
this.isVar = false;
this.isSelector = false;
}
else {
token.type(Token.SELECTOR);
Expand Down Expand Up @@ -151,9 +146,6 @@ var CssLexer = Lexer.extend(function(rule) {
this.isVar = false;
break;
case Token.IMPORTANT:
if(!this.depth || !this.isValue) {
token.type(Token.IGNORE);
}
this.isUrl = false;
this.afterHackBracket = false;
this.isVar = false;
Expand Down Expand Up @@ -188,7 +180,7 @@ var CssLexer = Lexer.extend(function(rule) {
this.isVar = false;
break;
case '[':
if(!this.isValue && !this.isSelector && this.depth) {
if(!this.isValue && !this.isSelector) {
token.type(Token.HACK);
}
this.bracket = true;
Expand All @@ -197,7 +189,7 @@ var CssLexer = Lexer.extend(function(rule) {
this.isVar = false;
break;
case ']':
if(!this.isValue && !this.isSelector && this.depth) {
if(!this.isValue && !this.isSelector) {
token.type(Token.HACK);
this.afterHackBracket = true;
}
Expand All @@ -206,7 +198,7 @@ var CssLexer = Lexer.extend(function(rule) {
this.isVar = false;
break;
case ';':
if(this.bracket && !this.isValue && !this.isSelector && this.depth
if(this.bracket && !this.isValue && !this.isSelector
|| this.afterHackBracket) {
token.type(Token.HACK);
}
Expand Down Expand Up @@ -326,15 +318,12 @@ var CssLexer = Lexer.extend(function(rule) {
//如果有未匹配的,css默认忽略,查找下一个;
var j = this.code.indexOf(';', this.index);
if(j == -1) {
j = this.code.length;
if(this.depth) {
j = this.code.indexOf('}', this.index);
if (j == -1) {
j = this.code.length;
}
else {
j--;
}
j = this.code.indexOf('}', this.index);
if(j == -1) {
j = this.code.length;
}
else if(this.depth) {
j--;
}
}
var s = this.code.slice(this.index - 1, ++j);
Expand Down
2 changes: 2 additions & 0 deletions src/lexer/rule/CssRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ var CssRule = Rule.extend(function() {
});

self.addMatch(new CompleteEqual(Token.HACK, '\\9\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\0/'));
self.addMatch(new CompleteEqual(Token.HACK, '\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\9'));
self.addMatch(new CompleteEqual(Token.HACK, '\\,'));
self.addMatch(new CompleteEqual(Token.HACK, '!ie'));
self.addMatch(new CompleteEqual(Token.HACK, '-moz-'), null, true);
self.addMatch(new CompleteEqual(Token.HACK, '-webkit-'), null, true);
self.addMatch(new CompleteEqual(Token.HACK, '-ms-'), null, true);
Expand Down
20 changes: 16 additions & 4 deletions tests/csslexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ describe('csslexer', function() {
expect(join(tokens)).to.eql(['p', '{', 'background', ':', 'url', '(', 'xxx', ')', ' ', 'no-repeat', '}']);
expect(type(tokens)).to.eql([21, 8, 10, 8, 15, 8, 7, 8, 1, 15, 8]);
});
it('outof {} is useless', function() {
it('outof {}', function() {
var lexer = homunculus.getLexer('css');
var tokens = lexer.parse('color:#333');
expect(join(tokens)).to.eql(['color', ':', '#333']);
expect(type(tokens)).to.eql([-2, 8, 21]);
expect(type(tokens)).to.eql([10, 8, 4]);
});
it('#number', function(){
var lexer = homunculus.getLexer('css');
Expand All @@ -222,11 +222,11 @@ describe('csslexer', function() {
expect(join(tokens)).to.eql(['p', '{', 'margin', ':', '0', '!important', '}']);
expect(type(tokens)).to.eql([21, 8, 10, 8, 4, 18, 8]);
});
it('!important out of {} is useless', function() {
it('!important out of {}', function() {
var lexer = homunculus.getLexer('css');
var tokens = lexer.parse('margin:0!important');
expect(join(tokens)).to.eql(['margin', ':', '0', '!important']);
expect(type(tokens)).to.eql([-2, 8, 4, -2]);
expect(type(tokens)).to.eql([10, 8, 4, 18]);
});
it('repeat kw', function() {
var lexer = homunculus.getLexer('css');
Expand Down Expand Up @@ -296,6 +296,18 @@ describe('csslexer', function() {
expect(join(tokens)).to.eql(['p', '{', '[', ';', 'width', ':', '0', ';', ']', ';', '}']);
expect(type(tokens)).to.eql([21, 8, 17, 17, 10, 8, 4, 8, 17, 17, 8]);
});
it('\\,', function() {
var lexer = homunculus.getLexer('css');
var tokens = lexer.parse('@media \\0screen\\,');
expect(join(tokens)).to.eql(['@media', ' ', '\\0', 'screen', '\\,']);
expect(type(tokens)).to.eql([12, 1, 17, 15, 17]);
});
it('\\,', function() {
var lexer = homunculus.getLexer('css');
var tokens = lexer.parse('@media \\0screen\\,');
expect(join(tokens)).to.eql(['@media', ' ', '\\0', 'screen', '\\,']);
expect(type(tokens)).to.eql([12, 1, 17, 15, 17]);
});
});
describe('unknow', function() {
it('with {}', function() {
Expand Down
39 changes: 14 additions & 25 deletions web/lexer/CssLexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,11 @@ define(function(require, exports, module) {
}
else {
if(this.rule.keyWords().hasOwnProperty(s)) {
if(this.depth || this.media || this.import) {
token.type(Token.KEYWORD);
this.isKw = true;
this.isUrl = false;
this.isVar = false;
this.isSelector = false;
}
else {
token.type(Token.IGNORE);
}
token.type(Token.KEYWORD);
this.isKw = true;
this.isUrl = false;
this.isVar = false;
this.isSelector = false;
}
else {
token.type(Token.SELECTOR);
Expand Down Expand Up @@ -152,9 +147,6 @@ define(function(require, exports, module) {
this.isVar = false;
break;
case Token.IMPORTANT:
if(!this.depth || !this.isValue) {
token.type(Token.IGNORE);
}
this.isUrl = false;
this.afterHackBracket = false;
this.isVar = false;
Expand Down Expand Up @@ -189,7 +181,7 @@ define(function(require, exports, module) {
this.isVar = false;
break;
case '[':
if(!this.isValue && !this.isSelector && this.depth) {
if(!this.isValue && !this.isSelector) {
token.type(Token.HACK);
}
this.bracket = true;
Expand All @@ -198,7 +190,7 @@ define(function(require, exports, module) {
this.isVar = false;
break;
case ']':
if(!this.isValue && !this.isSelector && this.depth) {
if(!this.isValue && !this.isSelector) {
token.type(Token.HACK);
this.afterHackBracket = true;
}
Expand All @@ -207,7 +199,7 @@ define(function(require, exports, module) {
this.isVar = false;
break;
case ';':
if(this.bracket && !this.isValue && !this.isSelector && this.depth
if(this.bracket && !this.isValue && !this.isSelector
|| this.afterHackBracket) {
token.type(Token.HACK);
}
Expand Down Expand Up @@ -327,15 +319,12 @@ define(function(require, exports, module) {
//如果有未匹配的,css默认忽略,查找下一个;
var j = this.code.indexOf(';', this.index);
if(j == -1) {
j = this.code.length;
if(this.depth) {
j = this.code.indexOf('}', this.index);
if (j == -1) {
j = this.code.length;
}
else {
j--;
}
j = this.code.indexOf('}', this.index);
if(j == -1) {
j = this.code.length;
}
else if(this.depth) {
j--;
}
}
var s = this.code.slice(this.index - 1, ++j);
Expand Down
2 changes: 2 additions & 0 deletions web/lexer/rule/CssRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ 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, '\\0'));
self.addMatch(new CompleteEqual(Token.HACK, '\\9'));
self.addMatch(new CompleteEqual(Token.HACK, '\\,'));
self.addMatch(new CompleteEqual(Token.HACK, '!ie'));
self.addMatch(new CompleteEqual(Token.HACK, '-moz-'), null, true);
self.addMatch(new CompleteEqual(Token.HACK, '-webkit-'), null, true);
self.addMatch(new CompleteEqual(Token.HACK, '-ms-'), null, true);
Expand Down

0 comments on commit c05ba0d

Please sign in to comment.