codemirror3, thanks Roberto
This commit is contained in:
@@ -5,8 +5,9 @@
|
||||
<title>CodeMirror: LESS mode</title>
|
||||
<link rel="stylesheet" href="../../lib/codemirror.css">
|
||||
<script src="../../lib/codemirror.js"></script>
|
||||
<script src="../../addon/edit/matchbrackets.js"></script>
|
||||
<script src="less.js"></script>
|
||||
<style>.CodeMirror {background: #f8f8f8; border: 1px solid #ddd; font-size:12px} .CodeMirror-scroll {height: 400px}</style>
|
||||
<style>.CodeMirror {background: #f8f8f8; border: 1px solid #ddd; font-size:12px; height: 400px}</style>
|
||||
<link rel="stylesheet" href="../../doc/docs.css">
|
||||
<link rel="stylesheet" href="../../theme/lesser-dark.css">
|
||||
</head>
|
||||
|
||||
+37
-45
@@ -7,18 +7,12 @@
|
||||
CodeMirror.defineMode("less", function(config) {
|
||||
var indentUnit = config.indentUnit, type;
|
||||
function ret(style, tp) {type = tp; return style;}
|
||||
//html tags
|
||||
var tags = "a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote body br button canvas caption cite code col colgroup command datalist dd del details dfn dir div dl dt em embed fieldset figcaption figure font footer form frame frameset h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe img input ins keygen kbd label legend li link map mark menu meta meter nav noframes noscript object ol optgroup option output p param pre progress q rp rt ruby s samp script section select small source span strike strong style sub summary sup table tbody td textarea tfoot th thead time title tr track tt u ul var video wbr".split(' ');
|
||||
|
||||
function inTagsArray(val){
|
||||
for(var i=0; i<tags.length; i++)if(val === tags[i])return true;
|
||||
}
|
||||
|
||||
|
||||
var selectors = /(^\:root$|^\:nth\-child$|^\:nth\-last\-child$|^\:nth\-of\-type$|^\:nth\-last\-of\-type$|^\:first\-child$|^\:last\-child$|^\:first\-of\-type$|^\:last\-of\-type$|^\:only\-child$|^\:only\-of\-type$|^\:empty$|^\:link|^\:visited$|^\:active$|^\:hover$|^\:focus$|^\:target$|^\:lang$|^\:enabled^\:disabled$|^\:checked$|^\:first\-line$|^\:first\-letter$|^\:before$|^\:after$|^\:not$|^\:required$|^\:invalid$)/;
|
||||
|
||||
|
||||
function tokenBase(stream, state) {
|
||||
var ch = stream.next();
|
||||
|
||||
|
||||
if (ch == "@") {stream.eatWhile(/[\w\-]/); return ret("meta", stream.current());}
|
||||
else if (ch == "/" && stream.eat("*")) {
|
||||
state.tokenize = tokenCComment;
|
||||
@@ -41,7 +35,7 @@ CodeMirror.defineMode("less", function(config) {
|
||||
}else{
|
||||
if(type == "string" || type == "(")return ret("string", "string");
|
||||
if(state.stack[state.stack.length-1] != undefined)return ret(null, ch);
|
||||
stream.eatWhile(/[\a-zA-Z0-9\-_.\s]/);
|
||||
stream.eatWhile(/[\a-zA-Z0-9\-_.\s]/);
|
||||
if( /\/|\)|#/.test(stream.peek() || (stream.eatSpace() && stream.peek() == ")")) || stream.eol() )return ret("string", "string"); // let url(/images/logo.png) without quotes return as string
|
||||
}
|
||||
}
|
||||
@@ -58,7 +52,7 @@ CodeMirror.defineMode("less", function(config) {
|
||||
return ret(null, "select-op");
|
||||
}
|
||||
else if (/[;{}:\[\]()~\|]/.test(ch)) {
|
||||
if(ch == ":"){
|
||||
if(ch == ":"){
|
||||
stream.eatWhile(/[a-z\\\-]/);
|
||||
if( selectors.test(stream.current()) ){
|
||||
return ret("tag", "tag");
|
||||
@@ -69,7 +63,7 @@ CodeMirror.defineMode("less", function(config) {
|
||||
if( selectors.test(stream.current().substring(1)) )return ret("tag", "tag");
|
||||
return ret(null, ch);
|
||||
}else{
|
||||
return ret(null, ch);
|
||||
return ret(null, ch);
|
||||
}
|
||||
}else if(ch == "~"){
|
||||
if(type == "r")return ret("string", "string");
|
||||
@@ -77,7 +71,7 @@ CodeMirror.defineMode("less", function(config) {
|
||||
return ret(null, ch);
|
||||
}
|
||||
}
|
||||
else if (ch == ".") {
|
||||
else if (ch == ".") {
|
||||
if(type == "(" || type == "string")return ret("string", "string"); // allow url(../image.png)
|
||||
stream.eatWhile(/[\a-zA-Z0-9\-_]/);
|
||||
if(stream.peek() == " ")stream.eatSpace();
|
||||
@@ -106,7 +100,7 @@ CodeMirror.defineMode("less", function(config) {
|
||||
else return ret("number", "unit");
|
||||
}else{//when not a valid hexvalue in the current stream e.g. #footer
|
||||
stream.eatWhile(/[\w\\\-]/);
|
||||
return ret("atom", "tag");
|
||||
return ret("atom", "tag");
|
||||
}
|
||||
}else{//when not a valid hexvalue length
|
||||
stream.eatWhile(/[\w\\\-]/);
|
||||
@@ -126,17 +120,15 @@ CodeMirror.defineMode("less", function(config) {
|
||||
return ret("string", "string");
|
||||
}else if(stream.peek() == "<" || stream.peek() == ">"){
|
||||
return ret("tag", "tag");
|
||||
}else if( /\(/.test(stream.peek()) ){
|
||||
}else if( /\(/.test(stream.peek()) ){
|
||||
return ret(null, ch);
|
||||
}else if (stream.peek() == "/" && state.stack[state.stack.length-1] != undefined){ // url(dir/center/image.png)
|
||||
return ret("string", "string");
|
||||
}else if( stream.current().match(/\-\d|\-.\d/) ){ // match e.g.: -5px -0.4 etc... only colorize the minus sign
|
||||
//commment out these 2 comment if you want the minus sign to be parsed as null -500px
|
||||
//stream.backUp(stream.current().length-1);
|
||||
//return ret(null, ch); //console.log( stream.current() );
|
||||
//return ret(null, ch); //console.log( stream.current() );
|
||||
return ret("number", "unit");
|
||||
}else if( inTagsArray(stream.current().toLowerCase()) ){ // match html tags
|
||||
return ret("tag", "tag");
|
||||
}else if( /\/|[\s\)]/.test(stream.peek() || stream.eol() || (stream.eatSpace() && stream.peek() == "/")) && stream.current().indexOf(".") !== -1){
|
||||
if(stream.current().substring(stream.current().length-1,stream.current().length) == "{"){
|
||||
stream.backUp(1);
|
||||
@@ -156,30 +148,30 @@ CodeMirror.defineMode("less", function(config) {
|
||||
stream.next();
|
||||
var t_v = stream.peek() == ":" ? true : false;
|
||||
if(!t_v){
|
||||
var old_pos = stream.pos;
|
||||
var sc = stream.current().length;
|
||||
stream.eatWhile(/[a-z\\\-]/);
|
||||
var new_pos = stream.pos;
|
||||
if(stream.current().substring(sc-1).match(selectors) != null){
|
||||
stream.backUp(new_pos-(old_pos-1));
|
||||
return ret("tag", "tag");
|
||||
} else stream.backUp(new_pos-(old_pos-1));
|
||||
}else{
|
||||
stream.backUp(1);
|
||||
}
|
||||
if(t_v)return ret("tag", "tag"); else return ret("variable", "variable");
|
||||
}else{
|
||||
return ret("variable", "variable");
|
||||
var old_pos = stream.pos;
|
||||
var sc = stream.current().length;
|
||||
stream.eatWhile(/[a-z\\\-]/);
|
||||
var new_pos = stream.pos;
|
||||
if(stream.current().substring(sc-1).match(selectors) != null){
|
||||
stream.backUp(new_pos-(old_pos-1));
|
||||
return ret("tag", "tag");
|
||||
} else stream.backUp(new_pos-(old_pos-1));
|
||||
}else{
|
||||
stream.backUp(1);
|
||||
}
|
||||
if(t_v)return ret("tag", "tag"); else return ret("variable", "variable");
|
||||
}else{
|
||||
return ret("variable", "variable");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function tokenSComment(stream, state) { // SComment = Slash comment
|
||||
stream.skipToEnd();
|
||||
state.tokenize = tokenBase;
|
||||
return ret("comment", "comment");
|
||||
}
|
||||
|
||||
|
||||
function tokenCComment(stream, state) {
|
||||
var maybeEnd = false, ch;
|
||||
while ((ch = stream.next()) != null) {
|
||||
@@ -191,7 +183,7 @@ CodeMirror.defineMode("less", function(config) {
|
||||
}
|
||||
return ret("comment", "comment");
|
||||
}
|
||||
|
||||
|
||||
function tokenSGMLComment(stream, state) {
|
||||
var dashes = 0, ch;
|
||||
while ((ch = stream.next()) != null) {
|
||||
@@ -203,7 +195,7 @@ CodeMirror.defineMode("less", function(config) {
|
||||
}
|
||||
return ret("comment", "comment");
|
||||
}
|
||||
|
||||
|
||||
function tokenString(quote) {
|
||||
return function(stream, state) {
|
||||
var escaped = false, ch;
|
||||
@@ -216,18 +208,18 @@ CodeMirror.defineMode("less", function(config) {
|
||||
return ret("string", "string");
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
startState: function(base) {
|
||||
startState: function(base) {
|
||||
return {tokenize: tokenBase,
|
||||
baseIndent: base || 0,
|
||||
stack: []};
|
||||
},
|
||||
|
||||
|
||||
token: function(stream, state) {
|
||||
if (stream.eatSpace()) return null;
|
||||
var style = state.tokenize(stream, state);
|
||||
|
||||
|
||||
var context = state.stack[state.stack.length-1];
|
||||
if (type == "hash" && context == "rule") style = "atom";
|
||||
else if (style == "variable") {
|
||||
@@ -237,7 +229,7 @@ CodeMirror.defineMode("less", function(config) {
|
||||
/[\s,|\s\)|\s]/.test(stream.peek()) ? "tag" : type;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (context == "rule" && /^[\{\};]$/.test(type))
|
||||
state.stack.pop();
|
||||
if (type == "{") {
|
||||
@@ -249,18 +241,18 @@ CodeMirror.defineMode("less", function(config) {
|
||||
else if (context == "{" && type != "comment") state.stack.push("rule");
|
||||
return style;
|
||||
},
|
||||
|
||||
|
||||
indent: function(state, textAfter) {
|
||||
var n = state.stack.length;
|
||||
if (/^\}/.test(textAfter))
|
||||
n -= state.stack[state.stack.length-1] == "rule" ? 2 : 1;
|
||||
return state.baseIndent + n * indentUnit;
|
||||
},
|
||||
|
||||
|
||||
electricChars: "}"
|
||||
};
|
||||
});
|
||||
|
||||
CodeMirror.defineMIME("text/x-less", "less");
|
||||
if (!CodeMirror.mimeModes.hasOwnProperty("text/css"))
|
||||
CodeMirror.defineMIME("text/css", "less");
|
||||
CodeMirror.defineMIME("text/css", "less");
|
||||
|
||||
Reference in New Issue
Block a user