stylus.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. // Stylus mode created by Dmitry Kiselyov http://git.io/AaRB
  4. (function(mod) {
  5. if (typeof exports == "object" && typeof module == "object") // CommonJS
  6. mod(require("../../lib/codemirror"));
  7. else if (typeof define == "function" && define.amd) // AMD
  8. define(["../../lib/codemirror"], mod);
  9. else // Plain browser env
  10. mod(CodeMirror);
  11. })(function(CodeMirror) {
  12. "use strict";
  13. CodeMirror.defineMode("stylus", function(config) {
  14. var indentUnit = config.indentUnit,
  15. indentUnitString = '',
  16. tagKeywords = keySet(tagKeywords_),
  17. tagVariablesRegexp = /^(a|b|i|s|col|em)$/i,
  18. propertyKeywords = keySet(propertyKeywords_),
  19. nonStandardPropertyKeywords = keySet(nonStandardPropertyKeywords_),
  20. valueKeywords = keySet(valueKeywords_),
  21. colorKeywords = keySet(colorKeywords_),
  22. documentTypes = keySet(documentTypes_),
  23. documentTypesRegexp = wordRegexp(documentTypes_),
  24. mediaFeatures = keySet(mediaFeatures_),
  25. mediaTypes = keySet(mediaTypes_),
  26. fontProperties = keySet(fontProperties_),
  27. operatorsRegexp = /^\s*([.]{2,3}|&&|\|\||\*\*|[?!=:]?=|[-+*\/%<>]=?|\?:|\~)/,
  28. wordOperatorKeywordsRegexp = wordRegexp(wordOperatorKeywords_),
  29. blockKeywords = keySet(blockKeywords_),
  30. vendorPrefixesRegexp = new RegExp(/^\-(moz|ms|o|webkit)-/i),
  31. commonAtoms = keySet(commonAtoms_),
  32. firstWordMatch = "",
  33. states = {},
  34. ch,
  35. style,
  36. type,
  37. override;
  38. while (indentUnitString.length < indentUnit) indentUnitString += ' ';
  39. /**
  40. * Tokenizers
  41. */
  42. function tokenBase(stream, state) {
  43. firstWordMatch = stream.string.match(/(^[\w-]+\s*=\s*$)|(^\s*[\w-]+\s*=\s*[\w-])|(^\s*(\.|#|@|\$|\&|\[|\d|\+|::?|\{|\>|~|\/)?\s*[\w-]*([a-z0-9-]|\*|\/\*)(\(|,)?)/);
  44. state.context.line.firstWord = firstWordMatch ? firstWordMatch[0].replace(/^\s*/, "") : "";
  45. state.context.line.indent = stream.indentation();
  46. ch = stream.peek();
  47. // Line comment
  48. if (stream.match("//")) {
  49. stream.skipToEnd();
  50. return ["comment", "comment"];
  51. }
  52. // Block comment
  53. if (stream.match("/*")) {
  54. state.tokenize = tokenCComment;
  55. return tokenCComment(stream, state);
  56. }
  57. // String
  58. if (ch == "\"" || ch == "'") {
  59. stream.next();
  60. state.tokenize = tokenString(ch);
  61. return state.tokenize(stream, state);
  62. }
  63. // Def
  64. if (ch == "@") {
  65. stream.next();
  66. stream.eatWhile(/[\w\\-]/);
  67. return ["def", stream.current()];
  68. }
  69. // ID selector or Hex color
  70. if (ch == "#") {
  71. stream.next();
  72. // Hex color
  73. if (stream.match(/^[0-9a-f]{3}([0-9a-f]([0-9a-f]{2}){0,2})?\b(?!-)/i)) {
  74. return ["atom", "atom"];
  75. }
  76. // ID selector
  77. if (stream.match(/^[a-z][\w-]*/i)) {
  78. return ["builtin", "hash"];
  79. }
  80. }
  81. // Vendor prefixes
  82. if (stream.match(vendorPrefixesRegexp)) {
  83. return ["meta", "vendor-prefixes"];
  84. }
  85. // Numbers
  86. if (stream.match(/^-?[0-9]?\.?[0-9]/)) {
  87. stream.eatWhile(/[a-z%]/i);
  88. return ["number", "unit"];
  89. }
  90. // !important|optional
  91. if (ch == "!") {
  92. stream.next();
  93. return [stream.match(/^(important|optional)/i) ? "keyword": "operator", "important"];
  94. }
  95. // Class
  96. if (ch == "." && stream.match(/^\.[a-z][\w-]*/i)) {
  97. return ["qualifier", "qualifier"];
  98. }
  99. // url url-prefix domain regexp
  100. if (stream.match(documentTypesRegexp)) {
  101. if (stream.peek() == "(") state.tokenize = tokenParenthesized;
  102. return ["property", "word"];
  103. }
  104. // Mixins / Functions
  105. if (stream.match(/^[a-z][\w-]*\(/i)) {
  106. stream.backUp(1);
  107. return ["keyword", "mixin"];
  108. }
  109. // Block mixins
  110. if (stream.match(/^(\+|-)[a-z][\w-]*\(/i)) {
  111. stream.backUp(1);
  112. return ["keyword", "block-mixin"];
  113. }
  114. // Parent Reference BEM naming
  115. if (stream.string.match(/^\s*&/) && stream.match(/^[-_]+[a-z][\w-]*/)) {
  116. return ["qualifier", "qualifier"];
  117. }
  118. // / Root Reference & Parent Reference
  119. if (stream.match(/^(\/|&)(-|_|:|\.|#|[a-z])/)) {
  120. stream.backUp(1);
  121. return ["variable-3", "reference"];
  122. }
  123. if (stream.match(/^&{1}\s*$/)) {
  124. return ["variable-3", "reference"];
  125. }
  126. // Word operator
  127. if (stream.match(wordOperatorKeywordsRegexp)) {
  128. return ["operator", "operator"];
  129. }
  130. // Word
  131. if (stream.match(/^\$?[-_]*[a-z0-9]+[\w-]*/i)) {
  132. // Variable
  133. if (stream.match(/^(\.|\[)[\w-\'\"\]]+/i, false)) {
  134. if (!wordIsTag(stream.current())) {
  135. stream.match('.');
  136. return ["variable-2", "variable-name"];
  137. }
  138. }
  139. return ["variable-2", "word"];
  140. }
  141. // Operators
  142. if (stream.match(operatorsRegexp)) {
  143. return ["operator", stream.current()];
  144. }
  145. // Delimiters
  146. if (/[:;,{}\[\]\(\)]/.test(ch)) {
  147. stream.next();
  148. return [null, ch];
  149. }
  150. // Non-detected items
  151. stream.next();
  152. return [null, null];
  153. }
  154. /**
  155. * Token comment
  156. */
  157. function tokenCComment(stream, state) {
  158. var maybeEnd = false, ch;
  159. while ((ch = stream.next()) != null) {
  160. if (maybeEnd && ch == "/") {
  161. state.tokenize = null;
  162. break;
  163. }
  164. maybeEnd = (ch == "*");
  165. }
  166. return ["comment", "comment"];
  167. }
  168. /**
  169. * Token string
  170. */
  171. function tokenString(quote) {
  172. return function(stream, state) {
  173. var escaped = false, ch;
  174. while ((ch = stream.next()) != null) {
  175. if (ch == quote && !escaped) {
  176. if (quote == ")") stream.backUp(1);
  177. break;
  178. }
  179. escaped = !escaped && ch == "\\";
  180. }
  181. if (ch == quote || !escaped && quote != ")") state.tokenize = null;
  182. return ["string", "string"];
  183. };
  184. }
  185. /**
  186. * Token parenthesized
  187. */
  188. function tokenParenthesized(stream, state) {
  189. stream.next(); // Must be "("
  190. if (!stream.match(/\s*[\"\')]/, false))
  191. state.tokenize = tokenString(")");
  192. else
  193. state.tokenize = null;
  194. return [null, "("];
  195. }
  196. /**
  197. * Context management
  198. */
  199. function Context(type, indent, prev, line) {
  200. this.type = type;
  201. this.indent = indent;
  202. this.prev = prev;
  203. this.line = line || {firstWord: "", indent: 0};
  204. }
  205. function pushContext(state, stream, type, indent) {
  206. indent = indent >= 0 ? indent : indentUnit;
  207. state.context = new Context(type, stream.indentation() + indent, state.context);
  208. return type;
  209. }
  210. function popContext(state, currentIndent) {
  211. var contextIndent = state.context.indent - indentUnit;
  212. currentIndent = currentIndent || false;
  213. state.context = state.context.prev;
  214. if (currentIndent) state.context.indent = contextIndent;
  215. return state.context.type;
  216. }
  217. function pass(type, stream, state) {
  218. return states[state.context.type](type, stream, state);
  219. }
  220. function popAndPass(type, stream, state, n) {
  221. for (var i = n || 1; i > 0; i--)
  222. state.context = state.context.prev;
  223. return pass(type, stream, state);
  224. }
  225. /**
  226. * Parser
  227. */
  228. function wordIsTag(word) {
  229. return word.toLowerCase() in tagKeywords;
  230. }
  231. function wordIsProperty(word) {
  232. word = word.toLowerCase();
  233. return word in propertyKeywords || word in fontProperties;
  234. }
  235. function wordIsBlock(word) {
  236. return word.toLowerCase() in blockKeywords;
  237. }
  238. function wordIsVendorPrefix(word) {
  239. return word.toLowerCase().match(vendorPrefixesRegexp);
  240. }
  241. function wordAsValue(word) {
  242. var wordLC = word.toLowerCase();
  243. var override = "variable-2";
  244. if (wordIsTag(word)) override = "tag";
  245. else if (wordIsBlock(word)) override = "block-keyword";
  246. else if (wordIsProperty(word)) override = "property";
  247. else if (wordLC in valueKeywords || wordLC in commonAtoms) override = "atom";
  248. else if (wordLC == "return" || wordLC in colorKeywords) override = "keyword";
  249. // Font family
  250. else if (word.match(/^[A-Z]/)) override = "string";
  251. return override;
  252. }
  253. function typeIsBlock(type, stream) {
  254. return ((endOfLine(stream) && (type == "{" || type == "]" || type == "hash" || type == "qualifier")) || type == "block-mixin");
  255. }
  256. function typeIsInterpolation(type, stream) {
  257. return type == "{" && stream.match(/^\s*\$?[\w-]+/i, false);
  258. }
  259. function typeIsPseudo(type, stream) {
  260. return type == ":" && stream.match(/^[a-z-]+/, false);
  261. }
  262. function startOfLine(stream) {
  263. return stream.sol() || stream.string.match(new RegExp("^\\s*" + escapeRegExp(stream.current())));
  264. }
  265. function endOfLine(stream) {
  266. return stream.eol() || stream.match(/^\s*$/, false);
  267. }
  268. function firstWordOfLine(line) {
  269. var re = /^\s*[-_]*[a-z0-9]+[\w-]*/i;
  270. var result = typeof line == "string" ? line.match(re) : line.string.match(re);
  271. return result ? result[0].replace(/^\s*/, "") : "";
  272. }
  273. /**
  274. * Block
  275. */
  276. states.block = function(type, stream, state) {
  277. if ((type == "comment" && startOfLine(stream)) ||
  278. (type == "," && endOfLine(stream)) ||
  279. type == "mixin") {
  280. return pushContext(state, stream, "block", 0);
  281. }
  282. if (typeIsInterpolation(type, stream)) {
  283. return pushContext(state, stream, "interpolation");
  284. }
  285. if (endOfLine(stream) && type == "]") {
  286. if (!/^\s*(\.|#|:|\[|\*|&)/.test(stream.string) && !wordIsTag(firstWordOfLine(stream))) {
  287. return pushContext(state, stream, "block", 0);
  288. }
  289. }
  290. if (typeIsBlock(type, stream)) {
  291. return pushContext(state, stream, "block");
  292. }
  293. if (type == "}" && endOfLine(stream)) {
  294. return pushContext(state, stream, "block", 0);
  295. }
  296. if (type == "variable-name") {
  297. if (stream.string.match(/^\s?\$[\w-\.\[\]\'\"]+$/) || wordIsBlock(firstWordOfLine(stream))) {
  298. return pushContext(state, stream, "variableName");
  299. }
  300. else {
  301. return pushContext(state, stream, "variableName", 0);
  302. }
  303. }
  304. if (type == "=") {
  305. if (!endOfLine(stream) && !wordIsBlock(firstWordOfLine(stream))) {
  306. return pushContext(state, stream, "block", 0);
  307. }
  308. return pushContext(state, stream, "block");
  309. }
  310. if (type == "*") {
  311. if (endOfLine(stream) || stream.match(/\s*(,|\.|#|\[|:|{)/,false)) {
  312. override = "tag";
  313. return pushContext(state, stream, "block");
  314. }
  315. }
  316. if (typeIsPseudo(type, stream)) {
  317. return pushContext(state, stream, "pseudo");
  318. }
  319. if (/@(font-face|media|supports|(-moz-)?document)/.test(type)) {
  320. return pushContext(state, stream, endOfLine(stream) ? "block" : "atBlock");
  321. }
  322. if (/@(-(moz|ms|o|webkit)-)?keyframes$/.test(type)) {
  323. return pushContext(state, stream, "keyframes");
  324. }
  325. if (/@extends?/.test(type)) {
  326. return pushContext(state, stream, "extend", 0);
  327. }
  328. if (type && type.charAt(0) == "@") {
  329. // Property Lookup
  330. if (stream.indentation() > 0 && wordIsProperty(stream.current().slice(1))) {
  331. override = "variable-2";
  332. return "block";
  333. }
  334. if (/(@import|@require|@charset)/.test(type)) {
  335. return pushContext(state, stream, "block", 0);
  336. }
  337. return pushContext(state, stream, "block");
  338. }
  339. if (type == "reference" && endOfLine(stream)) {
  340. return pushContext(state, stream, "block");
  341. }
  342. if (type == "(") {
  343. return pushContext(state, stream, "parens");
  344. }
  345. if (type == "vendor-prefixes") {
  346. return pushContext(state, stream, "vendorPrefixes");
  347. }
  348. if (type == "word") {
  349. var word = stream.current();
  350. override = wordAsValue(word);
  351. if (override == "property") {
  352. if (startOfLine(stream)) {
  353. return pushContext(state, stream, "block", 0);
  354. } else {
  355. override = "atom";
  356. return "block";
  357. }
  358. }
  359. if (override == "tag") {
  360. // tag is a css value
  361. if (/embed|menu|pre|progress|sub|table/.test(word)) {
  362. if (wordIsProperty(firstWordOfLine(stream))) {
  363. override = "atom";
  364. return "block";
  365. }
  366. }
  367. // tag is an attribute
  368. if (stream.string.match(new RegExp("\\[\\s*" + word + "|" + word +"\\s*\\]"))) {
  369. override = "atom";
  370. return "block";
  371. }
  372. // tag is a variable
  373. if (tagVariablesRegexp.test(word)) {
  374. if ((startOfLine(stream) && stream.string.match(/=/)) ||
  375. (!startOfLine(stream) &&
  376. !stream.string.match(/^(\s*\.|#|\&|\[|\/|>|\*)/) &&
  377. !wordIsTag(firstWordOfLine(stream)))) {
  378. override = "variable-2";
  379. if (wordIsBlock(firstWordOfLine(stream))) return "block";
  380. return pushContext(state, stream, "block", 0);
  381. }
  382. }
  383. if (endOfLine(stream)) return pushContext(state, stream, "block");
  384. }
  385. if (override == "block-keyword") {
  386. override = "keyword";
  387. // Postfix conditionals
  388. if (stream.current(/(if|unless)/) && !startOfLine(stream)) {
  389. return "block";
  390. }
  391. return pushContext(state, stream, "block");
  392. }
  393. if (word == "return") return pushContext(state, stream, "block", 0);
  394. // Placeholder selector
  395. if (override == "variable-2" && stream.string.match(/^\s?\$[\w-\.\[\]\'\"]+$/)) {
  396. return pushContext(state, stream, "block");
  397. }
  398. }
  399. return state.context.type;
  400. };
  401. /**
  402. * Parens
  403. */
  404. states.parens = function(type, stream, state) {
  405. if (type == "(") return pushContext(state, stream, "parens");
  406. if (type == ")") {
  407. if (state.context.prev.type == "parens") {
  408. return popContext(state);
  409. }
  410. if ((stream.string.match(/^[a-z][\w-]*\(/i) && endOfLine(stream)) ||
  411. wordIsBlock(firstWordOfLine(stream)) ||
  412. /(\.|#|:|\[|\*|&|>|~|\+|\/)/.test(firstWordOfLine(stream)) ||
  413. (!stream.string.match(/^-?[a-z][\w-\.\[\]\'\"]*\s*=/) &&
  414. wordIsTag(firstWordOfLine(stream)))) {
  415. return pushContext(state, stream, "block");
  416. }
  417. if (stream.string.match(/^[\$-]?[a-z][\w-\.\[\]\'\"]*\s*=/) ||
  418. stream.string.match(/^\s*(\(|\)|[0-9])/) ||
  419. stream.string.match(/^\s+[a-z][\w-]*\(/i) ||
  420. stream.string.match(/^\s+[\$-]?[a-z]/i)) {
  421. return pushContext(state, stream, "block", 0);
  422. }
  423. if (endOfLine(stream)) return pushContext(state, stream, "block");
  424. else return pushContext(state, stream, "block", 0);
  425. }
  426. if (type && type.charAt(0) == "@" && wordIsProperty(stream.current().slice(1))) {
  427. override = "variable-2";
  428. }
  429. if (type == "word") {
  430. var word = stream.current();
  431. override = wordAsValue(word);
  432. if (override == "tag" && tagVariablesRegexp.test(word)) {
  433. override = "variable-2";
  434. }
  435. if (override == "property" || word == "to") override = "atom";
  436. }
  437. if (type == "variable-name") {
  438. return pushContext(state, stream, "variableName");
  439. }
  440. if (typeIsPseudo(type, stream)) {
  441. return pushContext(state, stream, "pseudo");
  442. }
  443. return state.context.type;
  444. };
  445. /**
  446. * Vendor prefixes
  447. */
  448. states.vendorPrefixes = function(type, stream, state) {
  449. if (type == "word") {
  450. override = "property";
  451. return pushContext(state, stream, "block", 0);
  452. }
  453. return popContext(state);
  454. };
  455. /**
  456. * Pseudo
  457. */
  458. states.pseudo = function(type, stream, state) {
  459. if (!wordIsProperty(firstWordOfLine(stream.string))) {
  460. stream.match(/^[a-z-]+/);
  461. override = "variable-3";
  462. if (endOfLine(stream)) return pushContext(state, stream, "block");
  463. return popContext(state);
  464. }
  465. return popAndPass(type, stream, state);
  466. };
  467. /**
  468. * atBlock
  469. */
  470. states.atBlock = function(type, stream, state) {
  471. if (type == "(") return pushContext(state, stream, "atBlock_parens");
  472. if (typeIsBlock(type, stream)) {
  473. return pushContext(state, stream, "block");
  474. }
  475. if (typeIsInterpolation(type, stream)) {
  476. return pushContext(state, stream, "interpolation");
  477. }
  478. if (type == "word") {
  479. var word = stream.current().toLowerCase();
  480. if (/^(only|not|and|or)$/.test(word))
  481. override = "keyword";
  482. else if (documentTypes.hasOwnProperty(word))
  483. override = "tag";
  484. else if (mediaTypes.hasOwnProperty(word))
  485. override = "attribute";
  486. else if (mediaFeatures.hasOwnProperty(word))
  487. override = "property";
  488. else if (nonStandardPropertyKeywords.hasOwnProperty(word))
  489. override = "string-2";
  490. else override = wordAsValue(stream.current());
  491. if (override == "tag" && endOfLine(stream)) {
  492. return pushContext(state, stream, "block");
  493. }
  494. }
  495. if (type == "operator" && /^(not|and|or)$/.test(stream.current())) {
  496. override = "keyword";
  497. }
  498. return state.context.type;
  499. };
  500. states.atBlock_parens = function(type, stream, state) {
  501. if (type == "{" || type == "}") return state.context.type;
  502. if (type == ")") {
  503. if (endOfLine(stream)) return pushContext(state, stream, "block");
  504. else return pushContext(state, stream, "atBlock");
  505. }
  506. if (type == "word") {
  507. var word = stream.current().toLowerCase();
  508. override = wordAsValue(word);
  509. if (/^(max|min)/.test(word)) override = "property";
  510. if (override == "tag") {
  511. tagVariablesRegexp.test(word) ? override = "variable-2" : override = "atom";
  512. }
  513. return state.context.type;
  514. }
  515. return states.atBlock(type, stream, state);
  516. };
  517. /**
  518. * Keyframes
  519. */
  520. states.keyframes = function(type, stream, state) {
  521. if (stream.indentation() == "0" && ((type == "}" && startOfLine(stream)) || type == "]" || type == "hash"
  522. || type == "qualifier" || wordIsTag(stream.current()))) {
  523. return popAndPass(type, stream, state);
  524. }
  525. if (type == "{") return pushContext(state, stream, "keyframes");
  526. if (type == "}") {
  527. if (startOfLine(stream)) return popContext(state, true);
  528. else return pushContext(state, stream, "keyframes");
  529. }
  530. if (type == "unit" && /^[0-9]+\%$/.test(stream.current())) {
  531. return pushContext(state, stream, "keyframes");
  532. }
  533. if (type == "word") {
  534. override = wordAsValue(stream.current());
  535. if (override == "block-keyword") {
  536. override = "keyword";
  537. return pushContext(state, stream, "keyframes");
  538. }
  539. }
  540. if (/@(font-face|media|supports|(-moz-)?document)/.test(type)) {
  541. return pushContext(state, stream, endOfLine(stream) ? "block" : "atBlock");
  542. }
  543. if (type == "mixin") {
  544. return pushContext(state, stream, "block", 0);
  545. }
  546. return state.context.type;
  547. };
  548. /**
  549. * Interpolation
  550. */
  551. states.interpolation = function(type, stream, state) {
  552. if (type == "{") popContext(state) && pushContext(state, stream, "block");
  553. if (type == "}") {
  554. if (stream.string.match(/^\s*(\.|#|:|\[|\*|&|>|~|\+|\/)/i) ||
  555. (stream.string.match(/^\s*[a-z]/i) && wordIsTag(firstWordOfLine(stream)))) {
  556. return pushContext(state, stream, "block");
  557. }
  558. if (!stream.string.match(/^(\{|\s*\&)/) ||
  559. stream.match(/\s*[\w-]/,false)) {
  560. return pushContext(state, stream, "block", 0);
  561. }
  562. return pushContext(state, stream, "block");
  563. }
  564. if (type == "variable-name") {
  565. return pushContext(state, stream, "variableName", 0);
  566. }
  567. if (type == "word") {
  568. override = wordAsValue(stream.current());
  569. if (override == "tag") override = "atom";
  570. }
  571. return state.context.type;
  572. };
  573. /**
  574. * Extend/s
  575. */
  576. states.extend = function(type, stream, state) {
  577. if (type == "[" || type == "=") return "extend";
  578. if (type == "]") return popContext(state);
  579. if (type == "word") {
  580. override = wordAsValue(stream.current());
  581. return "extend";
  582. }
  583. return popContext(state);
  584. };
  585. /**
  586. * Variable name
  587. */
  588. states.variableName = function(type, stream, state) {
  589. if (type == "string" || type == "[" || type == "]" || stream.current().match(/^(\.|\$)/)) {
  590. if (stream.current().match(/^\.[\w-]+/i)) override = "variable-2";
  591. return "variableName";
  592. }
  593. return popAndPass(type, stream, state);
  594. };
  595. return {
  596. startState: function(base) {
  597. return {
  598. tokenize: null,
  599. state: "block",
  600. context: new Context("block", base || 0, null)
  601. };
  602. },
  603. token: function(stream, state) {
  604. if (!state.tokenize && stream.eatSpace()) return null;
  605. style = (state.tokenize || tokenBase)(stream, state);
  606. if (style && typeof style == "object") {
  607. type = style[1];
  608. style = style[0];
  609. }
  610. override = style;
  611. state.state = states[state.state](type, stream, state);
  612. return override;
  613. },
  614. indent: function(state, textAfter, line) {
  615. var cx = state.context,
  616. ch = textAfter && textAfter.charAt(0),
  617. indent = cx.indent,
  618. lineFirstWord = firstWordOfLine(textAfter),
  619. lineIndent = line.match(/^\s*/)[0].replace(/\t/g, indentUnitString).length,
  620. prevLineFirstWord = state.context.prev ? state.context.prev.line.firstWord : "",
  621. prevLineIndent = state.context.prev ? state.context.prev.line.indent : lineIndent;
  622. if (cx.prev &&
  623. (ch == "}" && (cx.type == "block" || cx.type == "atBlock" || cx.type == "keyframes") ||
  624. ch == ")" && (cx.type == "parens" || cx.type == "atBlock_parens") ||
  625. ch == "{" && (cx.type == "at"))) {
  626. indent = cx.indent - indentUnit;
  627. } else if (!(/(\})/.test(ch))) {
  628. if (/@|\$|\d/.test(ch) ||
  629. /^\{/.test(textAfter) ||
  630. /^\s*\/(\/|\*)/.test(textAfter) ||
  631. /^\s*\/\*/.test(prevLineFirstWord) ||
  632. /^\s*[\w-\.\[\]\'\"]+\s*(\?|:|\+)?=/i.test(textAfter) ||
  633. /^(\+|-)?[a-z][\w-]*\(/i.test(textAfter) ||
  634. /^return/.test(textAfter) ||
  635. wordIsBlock(lineFirstWord)) {
  636. indent = lineIndent;
  637. } else if (/(\.|#|:|\[|\*|&|>|~|\+|\/)/.test(ch) || wordIsTag(lineFirstWord)) {
  638. if (/\,\s*$/.test(prevLineFirstWord)) {
  639. indent = prevLineIndent;
  640. } else if (/^\s+/.test(line) && (/(\.|#|:|\[|\*|&|>|~|\+|\/)/.test(prevLineFirstWord) || wordIsTag(prevLineFirstWord))) {
  641. indent = lineIndent <= prevLineIndent ? prevLineIndent : prevLineIndent + indentUnit;
  642. } else {
  643. indent = lineIndent;
  644. }
  645. } else if (!/,\s*$/.test(line) && (wordIsVendorPrefix(lineFirstWord) || wordIsProperty(lineFirstWord))) {
  646. if (wordIsBlock(prevLineFirstWord)) {
  647. indent = lineIndent <= prevLineIndent ? prevLineIndent : prevLineIndent + indentUnit;
  648. } else if (/^\{/.test(prevLineFirstWord)) {
  649. indent = lineIndent <= prevLineIndent ? lineIndent : prevLineIndent + indentUnit;
  650. } else if (wordIsVendorPrefix(prevLineFirstWord) || wordIsProperty(prevLineFirstWord)) {
  651. indent = lineIndent >= prevLineIndent ? prevLineIndent : lineIndent;
  652. } else if (/^(\.|#|:|\[|\*|&|@|\+|\-|>|~|\/)/.test(prevLineFirstWord) ||
  653. /=\s*$/.test(prevLineFirstWord) ||
  654. wordIsTag(prevLineFirstWord) ||
  655. /^\$[\w-\.\[\]\'\"]/.test(prevLineFirstWord)) {
  656. indent = prevLineIndent + indentUnit;
  657. } else {
  658. indent = lineIndent;
  659. }
  660. }
  661. }
  662. return indent;
  663. },
  664. electricChars: "}",
  665. blockCommentStart: "/*",
  666. blockCommentEnd: "*/",
  667. blockCommentContinue: " * ",
  668. lineComment: "//",
  669. fold: "indent"
  670. };
  671. });
  672. // developer.mozilla.org/en-US/docs/Web/HTML/Element
  673. var tagKeywords_ = ["a","abbr","address","area","article","aside","audio", "b", "base","bdi", "bdo","bgsound","blockquote","body","br","button","canvas","caption","cite", "code","col","colgroup","data","datalist","dd","del","details","dfn","div", "dl","dt","em","embed","fieldset","figcaption","figure","footer","form","h1", "h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe", "img","input","ins","kbd","keygen","label","legend","li","link","main","map", "mark","marquee","menu","menuitem","meta","meter","nav","nobr","noframes", "noscript","object","ol","optgroup","option","output","p","param","pre", "progress","q","rp","rt","ruby","s","samp","script","section","select", "small","source","span","strong","style","sub","summary","sup","table","tbody","td","textarea","tfoot","th","thead","time","tr","track", "u","ul","var","video"];
  674. // github.com/codemirror/CodeMirror/blob/master/mode/css/css.js
  675. // Note, "url-prefix" should precede "url" in order to match correctly in documentTypesRegexp
  676. var documentTypes_ = ["domain", "regexp", "url-prefix", "url"];
  677. var mediaTypes_ = ["all","aural","braille","handheld","print","projection","screen","tty","tv","embossed"];
  678. var mediaFeatures_ = ["width","min-width","max-width","height","min-height","max-height","device-width","min-device-width","max-device-width","device-height","min-device-height","max-device-height","aspect-ratio","min-aspect-ratio","max-aspect-ratio","device-aspect-ratio","min-device-aspect-ratio","max-device-aspect-ratio","color","min-color","max-color","color-index","min-color-index","max-color-index","monochrome","min-monochrome","max-monochrome","resolution","min-resolution","max-resolution","scan","grid","dynamic-range","video-dynamic-range"];
  679. var propertyKeywords_ = ["align-content","align-items","align-self","alignment-adjust","alignment-baseline","anchor-point","animation","animation-delay","animation-direction","animation-duration","animation-fill-mode","animation-iteration-count","animation-name","animation-play-state","animation-timing-function","appearance","azimuth","backface-visibility","background","background-attachment","background-clip","background-color","background-image","background-origin","background-position","background-repeat","background-size","baseline-shift","binding","bleed","bookmark-label","bookmark-level","bookmark-state","bookmark-target","border","border-bottom","border-bottom-color","border-bottom-left-radius","border-bottom-right-radius","border-bottom-style","border-bottom-width","border-collapse","border-color","border-image","border-image-outset","border-image-repeat","border-image-slice","border-image-source","border-image-width","border-left","border-left-color","border-left-style","border-left-width","border-radius","border-right","border-right-color","border-right-style","border-right-width","border-spacing","border-style","border-top","border-top-color","border-top-left-radius","border-top-right-radius","border-top-style","border-top-width","border-width","bottom","box-decoration-break","box-shadow","box-sizing","break-after","break-before","break-inside","caption-side","clear","clip","color","color-profile","column-count","column-fill","column-gap","column-rule","column-rule-color","column-rule-style","column-rule-width","column-span","column-width","columns","content","counter-increment","counter-reset","crop","cue","cue-after","cue-before","cursor","direction","display","dominant-baseline","drop-initial-after-adjust","drop-initial-after-align","drop-initial-before-adjust","drop-initial-before-align","drop-initial-size","drop-initial-value","elevation","empty-cells","fit","fit-position","flex","flex-basis","flex-direction","flex-flow","flex-grow","flex-shrink","flex-wrap","float","float-offset","flow-from","flow-into","font","font-feature-settings","font-family","font-kerning","font-language-override","font-size","font-size-adjust","font-stretch","font-style","font-synthesis","font-variant","font-variant-alternates","font-variant-caps","font-variant-east-asian","font-variant-ligatures","font-variant-numeric","font-variant-position","font-weight","grid","grid-area","grid-auto-columns","grid-auto-flow","grid-auto-position","grid-auto-rows","grid-column","grid-column-end","grid-column-start","grid-row","grid-row-end","grid-row-start","grid-template","grid-template-areas","grid-template-columns","grid-template-rows","hanging-punctuation","height","hyphens","icon","image-orientation","image-rendering","image-resolution","inline-box-align","justify-content","left","letter-spacing","line-break","line-height","line-stacking","line-stacking-ruby","line-stacking-shift","line-stacking-strategy","list-style","list-style-image","list-style-position","list-style-type","margin","margin-bottom","margin-left","margin-right","margin-top","marker-offset","marks","marquee-direction","marquee-loop","marquee-play-count","marquee-speed","marquee-style","max-height","max-width","min-height","min-width","move-to","nav-down","nav-index","nav-left","nav-right","nav-up","object-fit","object-position","opacity","order","orphans","outline","outline-color","outline-offset","outline-style","outline-width","overflow","overflow-style","overflow-wrap","overflow-x","overflow-y","padding","padding-bottom","padding-left","padding-right","padding-top","page","page-break-after","page-break-before","page-break-inside","page-policy","pause","pause-after","pause-before","perspective","perspective-origin","pitch","pitch-range","play-during","position","presentation-level","punctuation-trim","quotes","region-break-after","region-break-before","region-break-inside","region-fragment","rendering-intent","resize","rest","rest-after","rest-before","richness","right","rotation","rotation-point","ruby-align","ruby-overhang","ruby-position","ruby-span","shape-image-threshold","shape-inside","shape-margin","shape-outside","size","speak","speak-as","speak-header","speak-numeral","speak-punctuation","speech-rate","stress","string-set","tab-size","table-layout","target","target-name","target-new","target-position","text-align","text-align-last","text-decoration","text-decoration-color","text-decoration-line","text-decoration-skip","text-decoration-style","text-emphasis","text-emphasis-color","text-emphasis-position","text-emphasis-style","text-height","text-indent","text-justify","text-outline","text-overflow","text-shadow","text-size-adjust","text-space-collapse","text-transform","text-underline-position","text-wrap","top","transform","transform-origin","transform-style","transition","transition-delay","transition-duration","transition-property","transition-timing-function","unicode-bidi","vertical-align","visibility","voice-balance","voice-duration","voice-family","voice-pitch","voice-range","voice-rate","voice-stress","voice-volume","volume","white-space","widows","width","will-change","word-break","word-spacing","word-wrap","z-index","clip-path","clip-rule","mask","enable-background","filter","flood-color","flood-opacity","lighting-color","stop-color","stop-opacity","pointer-events","color-interpolation","color-interpolation-filters","color-rendering","fill","fill-opacity","fill-rule","image-rendering","marker","marker-end","marker-mid","marker-start","shape-rendering","stroke","stroke-dasharray","stroke-dashoffset","stroke-linecap","stroke-linejoin","stroke-miterlimit","stroke-opacity","stroke-width","text-rendering","baseline-shift","dominant-baseline","glyph-orientation-horizontal","glyph-orientation-vertical","text-anchor","writing-mode","font-smoothing","osx-font-smoothing"];
  680. var nonStandardPropertyKeywords_ = ["scrollbar-arrow-color","scrollbar-base-color","scrollbar-dark-shadow-color","scrollbar-face-color","scrollbar-highlight-color","scrollbar-shadow-color","scrollbar-3d-light-color","scrollbar-track-color","shape-inside","searchfield-cancel-button","searchfield-decoration","searchfield-results-button","searchfield-results-decoration","zoom"];
  681. var fontProperties_ = ["font-family","src","unicode-range","font-variant","font-feature-settings","font-stretch","font-weight","font-style"];
  682. var colorKeywords_ = ["aliceblue","antiquewhite","aqua","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brown","burlywood","cadetblue","chartreuse","chocolate","coral","cornflowerblue","cornsilk","crimson","cyan","darkblue","darkcyan","darkgoldenrod","darkgray","darkgreen","darkkhaki","darkmagenta","darkolivegreen","darkorange","darkorchid","darkred","darksalmon","darkseagreen","darkslateblue","darkslategray","darkturquoise","darkviolet","deeppink","deepskyblue","dimgray","dodgerblue","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","ghostwhite","gold","goldenrod","gray","grey","green","greenyellow","honeydew","hotpink","indianred","indigo","ivory","khaki","lavender","lavenderblush","lawngreen","lemonchiffon","lightblue","lightcoral","lightcyan","lightgoldenrodyellow","lightgray","lightgreen","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslategray","lightsteelblue","lightyellow","lime","limegreen","linen","magenta","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurple","mediumseagreen","mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream","mistyrose","moccasin","navajowhite","navy","oldlace","olive","olivedrab","orange","orangered","orchid","palegoldenrod","palegreen","paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","rebeccapurple","red","rosybrown","royalblue","saddlebrown","salmon","sandybrown","seagreen","seashell","sienna","silver","skyblue","slateblue","slategray","snow","springgreen","steelblue","tan","teal","thistle","tomato","turquoise","violet","wheat","white","whitesmoke","yellow","yellowgreen"];
  683. var valueKeywords_ = ["above","absolute","activeborder","additive","activecaption","afar","after-white-space","ahead","alias","all","all-scroll","alphabetic","alternate","always","amharic","amharic-abegede","antialiased","appworkspace","arabic-indic","armenian","asterisks","attr","auto","avoid","avoid-column","avoid-page","avoid-region","background","backwards","baseline","below","bidi-override","binary","bengali","blink","block","block-axis","bold","bolder","border","border-box","both","bottom","break","break-all","break-word","bullets","button","button-bevel","buttonface","buttonhighlight","buttonshadow","buttontext","calc","cambodian","capitalize","caps-lock-indicator","caption","captiontext","caret","cell","center","checkbox","circle","cjk-decimal","cjk-earthly-branch","cjk-heavenly-stem","cjk-ideographic","clear","clip","close-quote","col-resize","collapse","column","compact","condensed","conic-gradient","contain","content","contents","content-box","context-menu","continuous","copy","counter","counters","cover","crop","cross","crosshair","currentcolor","cursive","cyclic","dashed","decimal","decimal-leading-zero","default","default-button","destination-atop","destination-in","destination-out","destination-over","devanagari","disc","discard","disclosure-closed","disclosure-open","document","dot-dash","dot-dot-dash","dotted","double","down","e-resize","ease","ease-in","ease-in-out","ease-out","element","ellipse","ellipsis","embed","end","ethiopic","ethiopic-abegede","ethiopic-abegede-am-et","ethiopic-abegede-gez","ethiopic-abegede-ti-er","ethiopic-abegede-ti-et","ethiopic-halehame-aa-er","ethiopic-halehame-aa-et","ethiopic-halehame-am-et","ethiopic-halehame-gez","ethiopic-halehame-om-et","ethiopic-halehame-sid-et","ethiopic-halehame-so-et","ethiopic-halehame-ti-er","ethiopic-halehame-ti-et","ethiopic-halehame-tig","ethiopic-numeric","ew-resize","expanded","extends","extra-condensed","extra-expanded","fantasy","fast","fill","fixed","flat","flex","footnotes","forwards","from","geometricPrecision","georgian","graytext","groove","gujarati","gurmukhi","hand","hangul","hangul-consonant","hebrew","help","hidden","hide","high","higher","highlight","highlighttext","hiragana","hiragana-iroha","horizontal","hsl","hsla","icon","ignore","inactiveborder","inactivecaption","inactivecaptiontext","infinite","infobackground","infotext","inherit","initial","inline","inline-axis","inline-block","inline-flex","inline-table","inset","inside","intrinsic","invert","italic","japanese-formal","japanese-informal","justify","kannada","katakana","katakana-iroha","keep-all","khmer","korean-hangul-formal","korean-hanja-formal","korean-hanja-informal","landscape","lao","large","larger","left","level","lighter","line-through","linear","linear-gradient","lines","list-item","listbox","listitem","local","logical","loud","lower","lower-alpha","lower-armenian","lower-greek","lower-hexadecimal","lower-latin","lower-norwegian","lower-roman","lowercase","ltr","malayalam","match","matrix","matrix3d","media-controls-background","media-current-time-display","media-fullscreen-button","media-mute-button","media-play-button","media-return-to-realtime-button","media-rewind-button","media-seek-back-button","media-seek-forward-button","media-slider","media-sliderthumb","media-time-remaining-display","media-volume-slider","media-volume-slider-container","media-volume-sliderthumb","medium","menu","menulist","menulist-button","menulist-text","menulist-textfield","menutext","message-box","middle","min-intrinsic","mix","mongolian","monospace","move","multiple","myanmar","n-resize","narrower","ne-resize","nesw-resize","no-close-quote","no-drop","no-open-quote","no-repeat","none","normal","not-allowed","nowrap","ns-resize","numbers","numeric","nw-resize","nwse-resize","oblique","octal","open-quote","optimizeLegibility","optimizeSpeed","oriya","oromo","outset","outside","outside-shape","overlay","overline","padding","padding-box","painted","page","paused","persian","perspective","plus-darker","plus-lighter","pointer","polygon","portrait","pre","pre-line","pre-wrap","preserve-3d","progress","push-button","radial-gradient","radio","read-only","read-write","read-write-plaintext-only","rectangle","region","relative","repeat","repeating-linear-gradient","repeating-radial-gradient","repeating-conic-gradient","repeat-x","repeat-y","reset","reverse","rgb","rgba","ridge","right","rotate","rotate3d","rotateX","rotateY","rotateZ","round","row-resize","rtl","run-in","running","s-resize","sans-serif","scale","scale3d","scaleX","scaleY","scaleZ","scroll","scrollbar","scroll-position","se-resize","searchfield","searchfield-cancel-button","searchfield-decoration","searchfield-results-button","searchfield-results-decoration","semi-condensed","semi-expanded","separate","serif","show","sidama","simp-chinese-formal","simp-chinese-informal","single","skew","skewX","skewY","skip-white-space","slide","slider-horizontal","slider-vertical","sliderthumb-horizontal","sliderthumb-vertical","slow","small","small-caps","small-caption","smaller","solid","somali","source-atop","source-in","source-out","source-over","space","spell-out","square","square-button","standard","start","static","status-bar","stretch","stroke","sub","subpixel-antialiased","super","sw-resize","symbolic","symbols","table","table-caption","table-cell","table-column","table-column-group","table-footer-group","table-header-group","table-row","table-row-group","tamil","telugu","text","text-bottom","text-top","textarea","textfield","thai","thick","thin","threeddarkshadow","threedface","threedhighlight","threedlightshadow","threedshadow","tibetan","tigre","tigrinya-er","tigrinya-er-abegede","tigrinya-et","tigrinya-et-abegede","to","top","trad-chinese-formal","trad-chinese-informal","translate","translate3d","translateX","translateY","translateZ","transparent","ultra-condensed","ultra-expanded","underline","up","upper-alpha","upper-armenian","upper-greek","upper-hexadecimal","upper-latin","upper-norwegian","upper-roman","uppercase","urdu","url","var","vertical","vertical-text","visible","visibleFill","visiblePainted","visibleStroke","visual","w-resize","wait","wave","wider","window","windowframe","windowtext","words","x-large","x-small","xor","xx-large","xx-small","bicubic","optimizespeed","grayscale","row","row-reverse","wrap","wrap-reverse","column-reverse","flex-start","flex-end","space-between","space-around", "unset"];
  684. var wordOperatorKeywords_ = ["in","and","or","not","is not","is a","is","isnt","defined","if unless"],
  685. blockKeywords_ = ["for","if","else","unless", "from", "to"],
  686. commonAtoms_ = ["null","true","false","href","title","type","not-allowed","readonly","disabled"],
  687. commonDef_ = ["@font-face", "@keyframes", "@media", "@viewport", "@page", "@host", "@supports", "@block", "@css"];
  688. var hintWords = tagKeywords_.concat(documentTypes_,mediaTypes_,mediaFeatures_,
  689. propertyKeywords_,nonStandardPropertyKeywords_,
  690. colorKeywords_,valueKeywords_,fontProperties_,
  691. wordOperatorKeywords_,blockKeywords_,
  692. commonAtoms_,commonDef_);
  693. function wordRegexp(words) {
  694. words = words.sort(function(a,b){return b > a;});
  695. return new RegExp("^((" + words.join(")|(") + "))\\b");
  696. }
  697. function keySet(array) {
  698. var keys = {};
  699. for (var i = 0; i < array.length; ++i) keys[array[i]] = true;
  700. return keys;
  701. }
  702. function escapeRegExp(text) {
  703. return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  704. }
  705. CodeMirror.registerHelper("hintWords", "stylus", hintWords);
  706. CodeMirror.defineMIME("text/x-styl", "stylus");
  707. });