1
0

less.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. LESS mode - http://www.lesscss.org/
  3. Ported to CodeMirror by Peter Kroon <plakroon@gmail.com>
  4. Report bugs/issues here: https://github.com/marijnh/CodeMirror/issues GitHub: @peterkroon
  5. */
  6. CodeMirror.defineMode("less", function(config) {
  7. var indentUnit = config.indentUnit, type;
  8. function ret(style, tp) {type = tp; return style;}
  9. 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$)/;
  10. function tokenBase(stream, state) {
  11. var ch = stream.next();
  12. if (ch == "@") {stream.eatWhile(/[\w\-]/); return ret("meta", stream.current());}
  13. else if (ch == "/" && stream.eat("*")) {
  14. state.tokenize = tokenCComment;
  15. return tokenCComment(stream, state);
  16. }
  17. else if (ch == "<" && stream.eat("!")) {
  18. state.tokenize = tokenSGMLComment;
  19. return tokenSGMLComment(stream, state);
  20. }
  21. else if (ch == "=") ret(null, "compare");
  22. else if (ch == "|" && stream.eat("=")) return ret(null, "compare");
  23. else if (ch == "\"" || ch == "'") {
  24. state.tokenize = tokenString(ch);
  25. return state.tokenize(stream, state);
  26. }
  27. else if (ch == "/") { // e.g.: .png will not be parsed as a class
  28. if(stream.eat("/")){
  29. state.tokenize = tokenSComment;
  30. return tokenSComment(stream, state);
  31. }else{
  32. if(type == "string" || type == "(")return ret("string", "string");
  33. if(state.stack[state.stack.length-1] != undefined)return ret(null, ch);
  34. stream.eatWhile(/[\a-zA-Z0-9\-_.\s]/);
  35. if( /\/|\)|#/.test(stream.peek() || (stream.eatSpace() && stream.peek() == ")")) || stream.eol() )return ret("string", "string"); // let url(/images/logo.png) without quotes return as string
  36. }
  37. }
  38. else if (ch == "!") {
  39. stream.match(/^\s*\w*/);
  40. return ret("keyword", "important");
  41. }
  42. else if (/\d/.test(ch)) {
  43. stream.eatWhile(/[\w.%]/);
  44. return ret("number", "unit");
  45. }
  46. else if (/[,+<>*\/]/.test(ch)) {
  47. if(stream.peek() == "=" || type == "a")return ret("string", "string");
  48. return ret(null, "select-op");
  49. }
  50. else if (/[;{}:\[\]()~\|]/.test(ch)) {
  51. if(ch == ":"){
  52. stream.eatWhile(/[a-z\\\-]/);
  53. if( selectors.test(stream.current()) ){
  54. return ret("tag", "tag");
  55. }else if(stream.peek() == ":"){//::-webkit-search-decoration
  56. stream.next();
  57. stream.eatWhile(/[a-z\\\-]/);
  58. if(stream.current().match(/\:\:\-(o|ms|moz|webkit)\-/))return ret("string", "string");
  59. if( selectors.test(stream.current().substring(1)) )return ret("tag", "tag");
  60. return ret(null, ch);
  61. }else{
  62. return ret(null, ch);
  63. }
  64. }else if(ch == "~"){
  65. if(type == "r")return ret("string", "string");
  66. }else{
  67. return ret(null, ch);
  68. }
  69. }
  70. else if (ch == ".") {
  71. if(type == "(" || type == "string")return ret("string", "string"); // allow url(../image.png)
  72. stream.eatWhile(/[\a-zA-Z0-9\-_]/);
  73. if(stream.peek() == " ")stream.eatSpace();
  74. if(stream.peek() == ")")return ret("number", "unit");//rgba(0,0,0,.25);
  75. return ret("tag", "tag");
  76. }
  77. else if (ch == "#") {
  78. //we don't eat white-space, we want the hex color and or id only
  79. stream.eatWhile(/[A-Za-z0-9]/);
  80. //check if there is a proper hex color length e.g. #eee || #eeeEEE
  81. if(stream.current().length == 4 || stream.current().length == 7){
  82. if(stream.current().match(/[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}/,false) != null){//is there a valid hex color value present in the current stream
  83. //when not a valid hex value, parse as id
  84. if(stream.current().substring(1) != stream.current().match(/[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}/,false))return ret("atom", "tag");
  85. //eat white-space
  86. stream.eatSpace();
  87. //when hex value declaration doesn't end with [;,] but is does with a slash/cc comment treat it as an id, just like the other hex values that don't end with[;,]
  88. if( /[\/<>.(){!$%^&*_\-\\?=+\|#'~`]/.test(stream.peek()) )return ret("atom", "tag");
  89. //#time { color: #aaa }
  90. else if(stream.peek() == "}" )return ret("number", "unit");
  91. //we have a valid hex color value, parse as id whenever an element/class is defined after the hex(id) value e.g. #eee aaa || #eee .aaa
  92. else if( /[a-zA-Z\\]/.test(stream.peek()) )return ret("atom", "tag");
  93. //when a hex value is on the end of a line, parse as id
  94. else if(stream.eol())return ret("atom", "tag");
  95. //default
  96. else return ret("number", "unit");
  97. }else{//when not a valid hexvalue in the current stream e.g. #footer
  98. stream.eatWhile(/[\w\\\-]/);
  99. return ret("atom", "tag");
  100. }
  101. }else{//when not a valid hexvalue length
  102. stream.eatWhile(/[\w\\\-]/);
  103. return ret("atom", "tag");
  104. }
  105. }
  106. else if (ch == "&") {
  107. stream.eatWhile(/[\w\-]/);
  108. return ret(null, ch);
  109. }
  110. else {
  111. stream.eatWhile(/[\w\\\-_%.{]/);
  112. if(type == "string"){
  113. return ret("string", "string");
  114. }else if(stream.current().match(/(^http$|^https$)/) != null){
  115. stream.eatWhile(/[\w\\\-_%.{:\/]/);
  116. return ret("string", "string");
  117. }else if(stream.peek() == "<" || stream.peek() == ">"){
  118. return ret("tag", "tag");
  119. }else if( /\(/.test(stream.peek()) ){
  120. return ret(null, ch);
  121. }else if (stream.peek() == "/" && state.stack[state.stack.length-1] != undefined){ // url(dir/center/image.png)
  122. return ret("string", "string");
  123. }else if( stream.current().match(/\-\d|\-.\d/) ){ // match e.g.: -5px -0.4 etc... only colorize the minus sign
  124. //commment out these 2 comment if you want the minus sign to be parsed as null -500px
  125. //stream.backUp(stream.current().length-1);
  126. //return ret(null, ch); //console.log( stream.current() );
  127. return ret("number", "unit");
  128. }else if( /\/|[\s\)]/.test(stream.peek() || stream.eol() || (stream.eatSpace() && stream.peek() == "/")) && stream.current().indexOf(".") !== -1){
  129. if(stream.current().substring(stream.current().length-1,stream.current().length) == "{"){
  130. stream.backUp(1);
  131. return ret("tag", "tag");
  132. }//end if
  133. stream.eatSpace();
  134. if( /[{<>.a-zA-Z\/]/.test(stream.peek()) || stream.eol() )return ret("tag", "tag"); // e.g. button.icon-plus
  135. return ret("string", "string"); // let url(/images/logo.png) without quotes return as string
  136. }else if( stream.eol() || stream.peek() == "[" || stream.peek() == "#" || type == "tag" ){
  137. if(stream.current().substring(stream.current().length-1,stream.current().length) == "{")stream.backUp(1);
  138. return ret("tag", "tag");
  139. }else if(type == "compare" || type == "a" || type == "("){
  140. return ret("string", "string");
  141. }else if(type == "|" || stream.current() == "-" || type == "["){
  142. return ret(null, ch);
  143. }else if(stream.peek() == ":") {
  144. stream.next();
  145. var t_v = stream.peek() == ":" ? true : false;
  146. if(!t_v){
  147. var old_pos = stream.pos;
  148. var sc = stream.current().length;
  149. stream.eatWhile(/[a-z\\\-]/);
  150. var new_pos = stream.pos;
  151. if(stream.current().substring(sc-1).match(selectors) != null){
  152. stream.backUp(new_pos-(old_pos-1));
  153. return ret("tag", "tag");
  154. } else stream.backUp(new_pos-(old_pos-1));
  155. }else{
  156. stream.backUp(1);
  157. }
  158. if(t_v)return ret("tag", "tag"); else return ret("variable", "variable");
  159. }else{
  160. return ret("variable", "variable");
  161. }
  162. }
  163. }
  164. function tokenSComment(stream, state) { // SComment = Slash comment
  165. stream.skipToEnd();
  166. state.tokenize = tokenBase;
  167. return ret("comment", "comment");
  168. }
  169. function tokenCComment(stream, state) {
  170. var maybeEnd = false, ch;
  171. while ((ch = stream.next()) != null) {
  172. if (maybeEnd && ch == "/") {
  173. state.tokenize = tokenBase;
  174. break;
  175. }
  176. maybeEnd = (ch == "*");
  177. }
  178. return ret("comment", "comment");
  179. }
  180. function tokenSGMLComment(stream, state) {
  181. var dashes = 0, ch;
  182. while ((ch = stream.next()) != null) {
  183. if (dashes >= 2 && ch == ">") {
  184. state.tokenize = tokenBase;
  185. break;
  186. }
  187. dashes = (ch == "-") ? dashes + 1 : 0;
  188. }
  189. return ret("comment", "comment");
  190. }
  191. function tokenString(quote) {
  192. return function(stream, state) {
  193. var escaped = false, ch;
  194. while ((ch = stream.next()) != null) {
  195. if (ch == quote && !escaped)
  196. break;
  197. escaped = !escaped && ch == "\\";
  198. }
  199. if (!escaped) state.tokenize = tokenBase;
  200. return ret("string", "string");
  201. };
  202. }
  203. return {
  204. startState: function(base) {
  205. return {tokenize: tokenBase,
  206. baseIndent: base || 0,
  207. stack: []};
  208. },
  209. token: function(stream, state) {
  210. if (stream.eatSpace()) return null;
  211. var style = state.tokenize(stream, state);
  212. var context = state.stack[state.stack.length-1];
  213. if (type == "hash" && context == "rule") style = "atom";
  214. else if (style == "variable") {
  215. if (context == "rule") style = null; //"tag"
  216. else if (!context || context == "@media{") {
  217. style = stream.current() == "when" ? "variable" :
  218. /[\s,|\s\)|\s]/.test(stream.peek()) ? "tag" : type;
  219. }
  220. }
  221. if (context == "rule" && /^[\{\};]$/.test(type))
  222. state.stack.pop();
  223. if (type == "{") {
  224. if (context == "@media") state.stack[state.stack.length-1] = "@media{";
  225. else state.stack.push("{");
  226. }
  227. else if (type == "}") state.stack.pop();
  228. else if (type == "@media") state.stack.push("@media");
  229. else if (context == "{" && type != "comment") state.stack.push("rule");
  230. return style;
  231. },
  232. indent: function(state, textAfter) {
  233. var n = state.stack.length;
  234. if (/^\}/.test(textAfter))
  235. n -= state.stack[state.stack.length-1] == "rule" ? 2 : 1;
  236. return state.baseIndent + n * indentUnit;
  237. },
  238. electricChars: "}"
  239. };
  240. });
  241. CodeMirror.defineMIME("text/x-less", "less");
  242. if (!CodeMirror.mimeModes.hasOwnProperty("text/css"))
  243. CodeMirror.defineMIME("text/css", "less");