sass.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("sass", function(config) {
  13. function tokenRegexp(words) {
  14. return new RegExp("^" + words.join("|"));
  15. }
  16. var keywords = ["true", "false", "null", "auto"];
  17. var keywordsRegexp = new RegExp("^" + keywords.join("|"));
  18. var operators = ["\\(", "\\)", "=", ">", "<", "==", ">=", "<=", "\\+", "-",
  19. "\\!=", "/", "\\*", "%", "and", "or", "not", ";","\\{","\\}",":"];
  20. var opRegexp = tokenRegexp(operators);
  21. var pseudoElementsRegexp = /^::?[a-zA-Z_][\w\-]*/;
  22. function urlTokens(stream, state) {
  23. var ch = stream.peek();
  24. if (ch === ")") {
  25. stream.next();
  26. state.tokenizer = tokenBase;
  27. return "operator";
  28. } else if (ch === "(") {
  29. stream.next();
  30. stream.eatSpace();
  31. return "operator";
  32. } else if (ch === "'" || ch === '"') {
  33. state.tokenizer = buildStringTokenizer(stream.next());
  34. return "string";
  35. } else {
  36. state.tokenizer = buildStringTokenizer(")", false);
  37. return "string";
  38. }
  39. }
  40. function comment(indentation, multiLine) {
  41. return function(stream, state) {
  42. if (stream.sol() && stream.indentation() <= indentation) {
  43. state.tokenizer = tokenBase;
  44. return tokenBase(stream, state);
  45. }
  46. if (multiLine && stream.skipTo("*/")) {
  47. stream.next();
  48. stream.next();
  49. state.tokenizer = tokenBase;
  50. } else {
  51. stream.skipToEnd();
  52. }
  53. return "comment";
  54. };
  55. }
  56. function buildStringTokenizer(quote, greedy) {
  57. if (greedy == null) { greedy = true; }
  58. function stringTokenizer(stream, state) {
  59. var nextChar = stream.next();
  60. var peekChar = stream.peek();
  61. var previousChar = stream.string.charAt(stream.pos-2);
  62. var endingString = ((nextChar !== "\\" && peekChar === quote) || (nextChar === quote && previousChar !== "\\"));
  63. if (endingString) {
  64. if (nextChar !== quote && greedy) { stream.next(); }
  65. state.tokenizer = tokenBase;
  66. return "string";
  67. } else if (nextChar === "#" && peekChar === "{") {
  68. state.tokenizer = buildInterpolationTokenizer(stringTokenizer);
  69. stream.next();
  70. return "operator";
  71. } else {
  72. return "string";
  73. }
  74. }
  75. return stringTokenizer;
  76. }
  77. function buildInterpolationTokenizer(currentTokenizer) {
  78. return function(stream, state) {
  79. if (stream.peek() === "}") {
  80. stream.next();
  81. state.tokenizer = currentTokenizer;
  82. return "operator";
  83. } else {
  84. return tokenBase(stream, state);
  85. }
  86. };
  87. }
  88. function indent(state) {
  89. if (state.indentCount == 0) {
  90. state.indentCount++;
  91. var lastScopeOffset = state.scopes[0].offset;
  92. var currentOffset = lastScopeOffset + config.indentUnit;
  93. state.scopes.unshift({ offset:currentOffset });
  94. }
  95. }
  96. function dedent(state) {
  97. if (state.scopes.length == 1) return;
  98. state.scopes.shift();
  99. }
  100. function tokenBase(stream, state) {
  101. var ch = stream.peek();
  102. // Comment
  103. if (stream.match("/*")) {
  104. state.tokenizer = comment(stream.indentation(), true);
  105. return state.tokenizer(stream, state);
  106. }
  107. if (stream.match("//")) {
  108. state.tokenizer = comment(stream.indentation(), false);
  109. return state.tokenizer(stream, state);
  110. }
  111. // Interpolation
  112. if (stream.match("#{")) {
  113. state.tokenizer = buildInterpolationTokenizer(tokenBase);
  114. return "operator";
  115. }
  116. // Strings
  117. if (ch === '"' || ch === "'") {
  118. stream.next();
  119. state.tokenizer = buildStringTokenizer(ch);
  120. return "string";
  121. }
  122. if(!state.cursorHalf){// state.cursorHalf === 0
  123. // first half i.e. before : for key-value pairs
  124. // including selectors
  125. if (ch === ".") {
  126. stream.next();
  127. if (stream.match(/^[\w-]+/)) {
  128. indent(state);
  129. return "atom";
  130. } else if (stream.peek() === "#") {
  131. indent(state);
  132. return "atom";
  133. }
  134. }
  135. if (ch === "#") {
  136. stream.next();
  137. // ID selectors
  138. if (stream.match(/^[\w-]+/)) {
  139. indent(state);
  140. return "atom";
  141. }
  142. if (stream.peek() === "#") {
  143. indent(state);
  144. return "atom";
  145. }
  146. }
  147. // Variables
  148. if (ch === "$") {
  149. stream.next();
  150. stream.eatWhile(/[\w-]/);
  151. return "variable-2";
  152. }
  153. // Numbers
  154. if (stream.match(/^-?[0-9\.]+/))
  155. return "number";
  156. // Units
  157. if (stream.match(/^(px|em|in)\b/))
  158. return "unit";
  159. if (stream.match(keywordsRegexp))
  160. return "keyword";
  161. if (stream.match(/^url/) && stream.peek() === "(") {
  162. state.tokenizer = urlTokens;
  163. return "atom";
  164. }
  165. if (ch === "=") {
  166. // Match shortcut mixin definition
  167. if (stream.match(/^=[\w-]+/)) {
  168. indent(state);
  169. return "meta";
  170. }
  171. }
  172. if (ch === "+") {
  173. // Match shortcut mixin definition
  174. if (stream.match(/^\+[\w-]+/)){
  175. return "variable-3";
  176. }
  177. }
  178. if(ch === "@"){
  179. if(stream.match(/@extend/)){
  180. if(!stream.match(/\s*[\w]/))
  181. dedent(state);
  182. }
  183. }
  184. // Indent Directives
  185. if (stream.match(/^@(else if|if|media|else|for|each|while|mixin|function)/)) {
  186. indent(state);
  187. return "meta";
  188. }
  189. // Other Directives
  190. if (ch === "@") {
  191. stream.next();
  192. stream.eatWhile(/[\w-]/);
  193. return "meta";
  194. }
  195. if (stream.eatWhile(/[\w-]/)){
  196. if(stream.match(/ *: *[\w-\+\$#!\("']/,false)){
  197. return "property";
  198. }
  199. else if(stream.match(/ *:/,false)){
  200. indent(state);
  201. state.cursorHalf = 1;
  202. return "atom";
  203. }
  204. else if(stream.match(/ *,/,false)){
  205. return "atom";
  206. }
  207. else{
  208. indent(state);
  209. return "atom";
  210. }
  211. }
  212. if(ch === ":"){
  213. if (stream.match(pseudoElementsRegexp)){ // could be a pseudo-element
  214. return "keyword";
  215. }
  216. stream.next();
  217. state.cursorHalf=1;
  218. return "operator";
  219. }
  220. } // cursorHalf===0 ends here
  221. else{
  222. if (ch === "#") {
  223. stream.next();
  224. // Hex numbers
  225. if (stream.match(/[0-9a-fA-F]{6}|[0-9a-fA-F]{3}/)){
  226. if(!stream.peek()){
  227. state.cursorHalf = 0;
  228. }
  229. return "number";
  230. }
  231. }
  232. // Numbers
  233. if (stream.match(/^-?[0-9\.]+/)){
  234. if(!stream.peek()){
  235. state.cursorHalf = 0;
  236. }
  237. return "number";
  238. }
  239. // Units
  240. if (stream.match(/^(px|em|in)\b/)){
  241. if(!stream.peek()){
  242. state.cursorHalf = 0;
  243. }
  244. return "unit";
  245. }
  246. if (stream.match(keywordsRegexp)){
  247. if(!stream.peek()){
  248. state.cursorHalf = 0;
  249. }
  250. return "keyword";
  251. }
  252. if (stream.match(/^url/) && stream.peek() === "(") {
  253. state.tokenizer = urlTokens;
  254. if(!stream.peek()){
  255. state.cursorHalf = 0;
  256. }
  257. return "atom";
  258. }
  259. // Variables
  260. if (ch === "$") {
  261. stream.next();
  262. stream.eatWhile(/[\w-]/);
  263. if(!stream.peek()){
  264. state.cursorHalf = 0;
  265. }
  266. return "variable-3";
  267. }
  268. // bang character for !important, !default, etc.
  269. if (ch === "!") {
  270. stream.next();
  271. if(!stream.peek()){
  272. state.cursorHalf = 0;
  273. }
  274. return stream.match(/^[\w]+/) ? "keyword": "operator";
  275. }
  276. if (stream.match(opRegexp)){
  277. if(!stream.peek()){
  278. state.cursorHalf = 0;
  279. }
  280. return "operator";
  281. }
  282. // attributes
  283. if (stream.eatWhile(/[\w-]/)) {
  284. if(!stream.peek()){
  285. state.cursorHalf = 0;
  286. }
  287. return "attribute";
  288. }
  289. //stream.eatSpace();
  290. if(!stream.peek()){
  291. state.cursorHalf = 0;
  292. return null;
  293. }
  294. } // else ends here
  295. if (stream.match(opRegexp))
  296. return "operator";
  297. // If we haven't returned by now, we move 1 character
  298. // and return an error
  299. stream.next();
  300. return null;
  301. }
  302. function tokenLexer(stream, state) {
  303. if (stream.sol()) state.indentCount = 0;
  304. var style = state.tokenizer(stream, state);
  305. var current = stream.current();
  306. if (current === "@return" || current === "}"){
  307. dedent(state);
  308. }
  309. if (style !== null) {
  310. var startOfToken = stream.pos - current.length;
  311. var withCurrentIndent = startOfToken + (config.indentUnit * state.indentCount);
  312. var newScopes = [];
  313. for (var i = 0; i < state.scopes.length; i++) {
  314. var scope = state.scopes[i];
  315. if (scope.offset <= withCurrentIndent)
  316. newScopes.push(scope);
  317. }
  318. state.scopes = newScopes;
  319. }
  320. return style;
  321. }
  322. return {
  323. startState: function() {
  324. return {
  325. tokenizer: tokenBase,
  326. scopes: [{offset: 0, type: "sass"}],
  327. indentCount: 0,
  328. cursorHalf: 0, // cursor half tells us if cursor lies after (1)
  329. // or before (0) colon (well... more or less)
  330. definedVars: [],
  331. definedMixins: []
  332. };
  333. },
  334. token: function(stream, state) {
  335. var style = tokenLexer(stream, state);
  336. state.lastToken = { style: style, content: stream.current() };
  337. return style;
  338. },
  339. indent: function(state) {
  340. return state.scopes[0].offset;
  341. }
  342. };
  343. });
  344. CodeMirror.defineMIME("text/x-sass", "sass");
  345. });