scss_highlight_rules.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../lib/oop");
  33. var lang = require("../lib/lang");
  34. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  35. var ScssHighlightRules = function() {
  36. var properties = lang.arrayToMap( (function () {
  37. var browserPrefix = ("-webkit-|-moz-|-o-|-ms-|-svg-|-pie-|-khtml-").split("|");
  38. var prefixProperties = ("appearance|background-clip|background-inline-policy|background-origin|" +
  39. "background-size|binding|border-bottom-colors|border-left-colors|" +
  40. "border-right-colors|border-top-colors|border-end|border-end-color|" +
  41. "border-end-style|border-end-width|border-image|border-start|" +
  42. "border-start-color|border-start-style|border-start-width|box-align|" +
  43. "box-direction|box-flex|box-flexgroup|box-ordinal-group|box-orient|" +
  44. "box-pack|box-sizing|column-count|column-gap|column-width|column-rule|" +
  45. "column-rule-width|column-rule-style|column-rule-color|float-edge|" +
  46. "font-feature-settings|font-language-override|force-broken-image-icon|" +
  47. "image-region|margin-end|margin-start|opacity|outline|outline-color|" +
  48. "outline-offset|outline-radius|outline-radius-bottomleft|" +
  49. "outline-radius-bottomright|outline-radius-topleft|outline-radius-topright|" +
  50. "outline-style|outline-width|padding-end|padding-start|stack-sizing|" +
  51. "tab-size|text-blink|text-decoration-color|text-decoration-line|" +
  52. "text-decoration-style|transform|transform-origin|transition|" +
  53. "transition-delay|transition-duration|transition-property|" +
  54. "transition-timing-function|user-focus|user-input|user-modify|user-select|" +
  55. "window-shadow|border-radius").split("|");
  56. var properties = ("azimuth|background-attachment|background-color|background-image|" +
  57. "background-position|background-repeat|background|border-bottom-color|" +
  58. "border-bottom-style|border-bottom-width|border-bottom|border-collapse|" +
  59. "border-color|border-left-color|border-left-style|border-left-width|" +
  60. "border-left|border-right-color|border-right-style|border-right-width|" +
  61. "border-right|border-spacing|border-style|border-top-color|" +
  62. "border-top-style|border-top-width|border-top|border-width|border|bottom|" +
  63. "box-shadow|box-sizing|caption-side|clear|clip|color|content|counter-increment|" +
  64. "counter-reset|cue-after|cue-before|cue|cursor|direction|display|" +
  65. "elevation|empty-cells|float|font-family|font-size-adjust|font-size|" +
  66. "font-stretch|font-style|font-variant|font-weight|font|height|left|" +
  67. "letter-spacing|line-height|list-style-image|list-style-position|" +
  68. "list-style-type|list-style|margin-bottom|margin-left|margin-right|" +
  69. "margin-top|marker-offset|margin|marks|max-height|max-width|min-height|" +
  70. "min-width|opacity|orphans|outline-color|" +
  71. "outline-style|outline-width|outline|overflow|overflow-x|overflow-y|padding-bottom|" +
  72. "padding-left|padding-right|padding-top|padding|page-break-after|" +
  73. "page-break-before|page-break-inside|page|pause-after|pause-before|" +
  74. "pause|pitch-range|pitch|play-during|position|quotes|richness|right|" +
  75. "size|speak-header|speak-numeral|speak-punctuation|speech-rate|speak|" +
  76. "stress|table-layout|text-align|text-decoration|text-indent|" +
  77. "text-shadow|text-transform|top|unicode-bidi|vertical-align|" +
  78. "visibility|voice-family|volume|white-space|widows|width|word-spacing|" +
  79. "z-index").split("|");
  80. //The return array
  81. var ret = [];
  82. //All prefixProperties will get the browserPrefix in
  83. //the begning by join the prefixProperties array with the value of browserPrefix
  84. for (var i=0, ln=browserPrefix.length; i<ln; i++) {
  85. Array.prototype.push.apply(
  86. ret,
  87. (( browserPrefix[i] + prefixProperties.join("|" + browserPrefix[i]) ).split("|"))
  88. );
  89. }
  90. //Add also prefixProperties and properties without any browser prefix
  91. Array.prototype.push.apply(ret, prefixProperties);
  92. Array.prototype.push.apply(ret, properties);
  93. return ret;
  94. })() );
  95. var functions = lang.arrayToMap(
  96. ("hsl|hsla|rgb|rgba|url|attr|counter|counters|abs|adjust_color|adjust_hue|" +
  97. "alpha|join|blue|ceil|change_color|comparable|complement|darken|desaturate|" +
  98. "floor|grayscale|green|hue|if|invert|join|length|lighten|lightness|mix|" +
  99. "nth|opacify|opacity|percentage|quote|red|round|saturate|saturation|" +
  100. "scale_color|transparentize|type_of|unit|unitless|unqoute").split("|")
  101. );
  102. var constants = lang.arrayToMap(
  103. ("absolute|all-scroll|always|armenian|auto|baseline|below|bidi-override|" +
  104. "block|bold|bolder|border-box|both|bottom|break-all|break-word|capitalize|center|" +
  105. "char|circle|cjk-ideographic|col-resize|collapse|content-box|crosshair|dashed|" +
  106. "decimal-leading-zero|decimal|default|disabled|disc|" +
  107. "distribute-all-lines|distribute-letter|distribute-space|" +
  108. "distribute|dotted|double|e-resize|ellipsis|fixed|georgian|groove|" +
  109. "hand|hebrew|help|hidden|hiragana-iroha|hiragana|horizontal|" +
  110. "ideograph-alpha|ideograph-numeric|ideograph-parenthesis|" +
  111. "ideograph-space|inactive|inherit|inline-block|inline|inset|inside|" +
  112. "inter-ideograph|inter-word|italic|justify|katakana-iroha|katakana|" +
  113. "keep-all|left|lighter|line-edge|line-through|line|list-item|loose|" +
  114. "lower-alpha|lower-greek|lower-latin|lower-roman|lowercase|lr-tb|ltr|" +
  115. "medium|middle|move|n-resize|ne-resize|newspaper|no-drop|no-repeat|" +
  116. "nw-resize|none|normal|not-allowed|nowrap|oblique|outset|outside|" +
  117. "overline|pointer|progress|relative|repeat-x|repeat-y|repeat|right|" +
  118. "ridge|row-resize|rtl|s-resize|scroll|se-resize|separate|small-caps|" +
  119. "solid|square|static|strict|super|sw-resize|table-footer-group|" +
  120. "table-header-group|tb-rl|text-bottom|text-top|text|thick|thin|top|" +
  121. "transparent|underline|upper-alpha|upper-latin|upper-roman|uppercase|" +
  122. "vertical-ideographic|vertical-text|visible|w-resize|wait|whitespace|" +
  123. "zero").split("|")
  124. );
  125. var colors = lang.arrayToMap(
  126. ("aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|" +
  127. "purple|red|silver|teal|white|yellow").split("|")
  128. );
  129. var keywords = lang.arrayToMap(
  130. ("@mixin|@extend|@include|@import|@media|@debug|@warn|@if|@for|@each|@while|@else|@font-face|@-webkit-keyframes|if|and|!default|module|def|end|declare").split("|")
  131. )
  132. var tags = lang.arrayToMap(
  133. ("a|abbr|acronym|address|applet|area|article|aside|audio|b|base|basefont|bdo|" +
  134. "big|blockquote|body|br|button|canvas|caption|center|cite|code|col|colgroup|" +
  135. "command|datalist|dd|del|details|dfn|dir|div|dl|dt|em|embed|fieldset|" +
  136. "figcaption|figure|font|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|" +
  137. "header|hgroup|hr|html|i|iframe|img|input|ins|keygen|kbd|label|legend|li|" +
  138. "link|map|mark|menu|meta|meter|nav|noframes|noscript|object|ol|optgroup|" +
  139. "option|output|p|param|pre|progress|q|rp|rt|ruby|s|samp|script|section|select|" +
  140. "small|source|span|strike|strong|style|sub|summary|sup|table|tbody|td|" +
  141. "textarea|tfoot|th|thead|time|title|tr|tt|u|ul|var|video|wbr|xmp").split("|")
  142. );
  143. // regexp must not have capturing parentheses. Use (?:) instead.
  144. // regexps are ordered -> the first match is used
  145. var numRe = "\\-?(?:(?:[0-9]+)|(?:[0-9]*\\.[0-9]+))";
  146. // regexp must not have capturing parentheses. Use (?:) instead.
  147. // regexps are ordered -> the first match is used
  148. this.$rules = {
  149. "start" : [
  150. {
  151. token : "comment",
  152. regex : "\\/\\/.*$"
  153. },
  154. {
  155. token : "comment", // multi line comment
  156. regex : "\\/\\*",
  157. next : "comment"
  158. }, {
  159. token : "string", // single line
  160. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  161. }, {
  162. token : "string", // multi line string start
  163. regex : '["].*\\\\$',
  164. next : "qqstring"
  165. }, {
  166. token : "string", // single line
  167. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  168. }, {
  169. token : "string", // multi line string start
  170. regex : "['].*\\\\$",
  171. next : "qstring"
  172. }, {
  173. token : "constant.numeric",
  174. regex : numRe + "(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)"
  175. }, {
  176. token : "constant.numeric", // hex6 color
  177. regex : "#[a-f0-9]{6}"
  178. }, {
  179. token : "constant.numeric", // hex3 color
  180. regex : "#[a-f0-9]{3}"
  181. }, {
  182. token : "constant.numeric",
  183. regex : numRe
  184. }, {
  185. token : ["support.function", "string", "support.function"],
  186. regex : "(url\\()(.*)(\\))"
  187. }, {
  188. token : function(value) {
  189. if (properties.hasOwnProperty(value.toLowerCase()))
  190. return "support.type";
  191. if (keywords.hasOwnProperty(value))
  192. return "keyword";
  193. else if (constants.hasOwnProperty(value))
  194. return "constant.language";
  195. else if (functions.hasOwnProperty(value))
  196. return "support.function";
  197. else if (colors.hasOwnProperty(value.toLowerCase()))
  198. return "support.constant.color";
  199. else if (tags.hasOwnProperty(value.toLowerCase()))
  200. return "variable.language";
  201. else
  202. return "text";
  203. },
  204. regex : "\\-?[@a-z_][@a-z0-9_\\-]*"
  205. }, {
  206. token : "variable",
  207. regex : "[a-z_\\-$][a-z0-9_\\-$]*\\b"
  208. }, {
  209. token: "variable.language",
  210. regex: "#[a-z0-9-_]+"
  211. }, {
  212. token: "variable.language",
  213. regex: "\\.[a-z0-9-_]+"
  214. }, {
  215. token: "variable.language",
  216. regex: ":[a-z0-9-_]+"
  217. }, {
  218. token: "constant",
  219. regex: "[a-z0-9-_]+"
  220. }, {
  221. token : "keyword.operator",
  222. regex : "<|>|<=|>=|==|!=|-|%|#|\\+|\\$|\\+|\\*"
  223. }, {
  224. token : "paren.lparen",
  225. regex : "[[({]"
  226. }, {
  227. token : "paren.rparen",
  228. regex : "[\\])}]"
  229. }, {
  230. token : "text",
  231. regex : "\\s+"
  232. }, {
  233. caseInsensitive: true
  234. }
  235. ],
  236. "comment" : [
  237. {
  238. token : "comment", // closing comment
  239. regex : ".*?\\*\\/",
  240. next : "start"
  241. }, {
  242. token : "comment", // comment spanning whole line
  243. regex : ".+"
  244. }
  245. ],
  246. "qqstring" : [
  247. {
  248. token : "string",
  249. regex : '(?:(?:\\\\.)|(?:[^"\\\\]))*?"',
  250. next : "start"
  251. }, {
  252. token : "string",
  253. regex : '.+'
  254. }
  255. ],
  256. "qstring" : [
  257. {
  258. token : "string",
  259. regex : "(?:(?:\\\\.)|(?:[^'\\\\]))*?'",
  260. next : "start"
  261. }, {
  262. token : "string",
  263. regex : '.+'
  264. }
  265. ]
  266. };
  267. };
  268. oop.inherits(ScssHighlightRules, TextHighlightRules);
  269. exports.ScssHighlightRules = ScssHighlightRules;
  270. });