You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mode-css.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is Ajax.org Code Editor (ACE).
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Ajax.org B.V.
  18. * Portions created by the Initial Developer are Copyright (C) 2010
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Fabian Jakobs <fabian AT ajax DOT org>
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. ace.define('ace/mode/css', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/mode/text', 'ace/tokenizer', 'ace/mode/css_highlight_rules', 'ace/mode/matching_brace_outdent', 'ace/worker/worker_client', 'ace/mode/folding/cstyle'], function(require, exports, module) {
  38. var oop = require("../lib/oop");
  39. var TextMode = require("./text").Mode;
  40. var Tokenizer = require("../tokenizer").Tokenizer;
  41. var CssHighlightRules = require("./css_highlight_rules").CssHighlightRules;
  42. var MatchingBraceOutdent = require("./matching_brace_outdent").MatchingBraceOutdent;
  43. var WorkerClient = require("../worker/worker_client").WorkerClient;
  44. var CStyleFoldMode = require("./folding/cstyle").FoldMode;
  45. var Mode = function() {
  46. this.$tokenizer = new Tokenizer(new CssHighlightRules().getRules(), "i");
  47. this.$outdent = new MatchingBraceOutdent();
  48. this.foldingRules = new CStyleFoldMode();
  49. };
  50. oop.inherits(Mode, TextMode);
  51. (function() {
  52. this.foldingRules = "cStyle";
  53. this.getNextLineIndent = function(state, line, tab) {
  54. var indent = this.$getIndent(line);
  55. // ignore braces in comments
  56. var tokens = this.$tokenizer.getLineTokens(line, state).tokens;
  57. if (tokens.length && tokens[tokens.length-1].type == "comment") {
  58. return indent;
  59. }
  60. var match = line.match(/^.*\{\s*$/);
  61. if (match) {
  62. indent += tab;
  63. }
  64. return indent;
  65. };
  66. this.checkOutdent = function(state, line, input) {
  67. return this.$outdent.checkOutdent(line, input);
  68. };
  69. this.autoOutdent = function(state, doc, row) {
  70. this.$outdent.autoOutdent(doc, row);
  71. };
  72. this.createWorker = function(session) {
  73. var worker = new WorkerClient(["ace"], "worker-css.js", "ace/mode/css_worker", "Worker");
  74. worker.attachToDocument(session.getDocument());
  75. worker.on("csslint", function(e) {
  76. var errors = [];
  77. e.data.forEach(function(message) {
  78. errors.push({
  79. row: message.line - 1,
  80. column: message.col - 1,
  81. text: message.message,
  82. type: message.type,
  83. lint: message
  84. });
  85. });
  86. session.setAnnotations(errors);
  87. });
  88. return worker;
  89. };
  90. }).call(Mode.prototype);
  91. exports.Mode = Mode;
  92. });
  93. ace.define('ace/mode/css_highlight_rules', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/lib/lang', 'ace/mode/text_highlight_rules'], function(require, exports, module) {
  94. var oop = require("../lib/oop");
  95. var lang = require("../lib/lang");
  96. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  97. var CssHighlightRules = function() {
  98. var properties = lang.arrayToMap(
  99. ("animation-fill-mode|alignment-adjust|alignment-baseline|animation-delay|animation-direction|animation-duration|animation-iteration-count|animation-name|animation-play-state|animation-timing-function|animation|appearance|azimuth|backface-visibility|background-attachment|background-break|background-clip|background-color|background-image|background-origin|background-position|background-repeat|background-size|background|baseline-shift|binding|bleed|bookmark-label|bookmark-level|bookmark-state|bookmark-target|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|border|bottom|box-align|box-decoration-break|box-direction|box-flex-group|box-flex|box-lines|box-ordinal-group|box-orient|box-pack|box-shadow|box-sizing|break-after|break-before|break-inside|caption-side|clear|clip|color-profile|color|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-after|cue-before|cue|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|float-offset|float|font-family|font-size|font-size-adjust|font-stretch|font-style|font-variant|font-weight|font|grid-columns|grid-rows|hanging-punctuation|height|hyphenate-after|hyphenate-before|hyphenate-character|hyphenate-lines|hyphenate-resource|hyphens|icon|image-orientation|image-rendering|image-resolution|inline-box-align|left|letter-spacing|line-height|line-stacking-ruby|line-stacking-shift|line-stacking-strategy|line-stacking|list-style-image|list-style-position|list-style-type|list-style|margin-bottom|margin-left|margin-right|margin-top|margin|mark-after|mark-before|mark|marks|marquee-direction|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|opacity|orphans|outline-color|outline-offset|outline-style|outline-width|outline|overflow-style|overflow-x|overflow-y|overflow|padding-bottom|padding-left|padding-right|padding-top|padding|page-break-after|page-break-before|page-break-inside|page-policy|page|pause-after|pause-before|pause|perspective-origin|perspective|phonemes|pitch-range|pitch|play-during|position|presentation-level|punctuation-trim|quotes|rendering-intent|resize|rest-after|rest-before|rest|richness|right|rotation-point|rotation|ruby-align|ruby-overhang|ruby-position|ruby-span|size|speak-header|speak-numeral|speak-punctuation|speak|speech-rate|stress|string-set|table-layout|target-name|target-new|target-position|target|text-align-last|text-align|text-decoration|text-emphasis|text-height|text-indent|text-justify|text-outline|text-shadow|text-transform|text-wrap|top|transform-origin|transform-style|transform|transition-delay|transition-duration|transition-property|transition-timing-function|transition|unicode-bidi|vertical-align|visibility|voice-balance|voice-duration|voice-family|voice-pitch-range|voice-pitch|voice-rate|voice-stress|voice-volume|volume|white-space-collapse|white-space|widows|width|word-break|word-spacing|word-wrap|z-index").split("|")
  100. );
  101. var functions = lang.arrayToMap(
  102. ("rgb|rgba|url|attr|counter|counters").split("|")
  103. );
  104. var constants = lang.arrayToMap(
  105. ("absolute|after-edge|after|all-scroll|all|alphabetic|always|antialiased|armenian|auto|avoid-column|avoid-page|avoid|balance|baseline|before-edge|before|below|bidi-override|block-line-height|block|bold|bolder|border-box|both|bottom|box|break-all|break-word|capitalize|caps-height|caption|center|central|char|circle|cjk-ideographic|clone|close-quote|col-resize|collapse|column|consider-shifts|contain|content-box|cover|crosshair|cubic-bezier|dashed|decimal-leading-zero|decimal|default|disabled|disc|disregard-shifts|distribute-all-lines|distribute-letter|distribute-space|distribute|dotted|double|e-resize|ease-in|ease-in-out|ease-out|ease|ellipsis|end|exclude-ruby|fill|fixed|font-size|font|georgian|glyphs|grid-height|groove|hand|hanging|hebrew|help|hidden|hiragana-iroha|hiragana|horizontal|icon|ideograph-alpha|ideograph-numeric|ideograph-parenthesis|ideograph-space|ideographic|inactive|include-ruby|inherit|initial|inline-block|inline-box|inline-line-height|inline-table|inline|inset|inside|inter-ideograph|inter-word|invert|italic|justify|katakana-iroha|katakana|keep-all|last|left|lighter|line-edge|line-through|line|linear|list-item|local|loose|lower-alpha|lower-greek|lower-latin|lower-roman|lowercase|lr-tb|ltr|mathematical|max-height|max-size|medium|menu|message-box|middle|move|n-resize|ne-resize|newspaper|no-change|no-close-quote|no-drop|no-open-quote|no-repeat|none|normal|not-allowed|nowrap|nw-resize|oblique|open-quote|outset|outside|overline|padding-box|page|pointer|pre-line|pre-wrap|pre|preserve-3d|progress|relative|repeat-x|repeat-y|repeat|replaced|reset-size|ridge|right|round|row-resize|rtl|s-resize|scroll|se-resize|separate|slice|small-caps|small-caption|solid|space|square|start|static|status-bar|step-end|step-start|steps|stretch|strict|sub|super|sw-resize|table-caption|table-cell|table-column-group|table-column|table-footer-group|table-header-group|table-row-group|table-row|table|tb-rl|text-after-edge|text-before-edge|text-bottom|text-size|text-top|text|thick|thin|top|transparent|underline|upper-alpha|upper-latin|upper-roman|uppercase|use-script|vertical-ideographic|vertical-text|visible|w-resize|wait|whitespace|z-index|zero").split("|")
  106. );
  107. var colors = lang.arrayToMap(
  108. ("aqua|black|blue|fuchsia|gray|green|lime|maroon|navy|olive|orange|" +
  109. "purple|red|silver|teal|white|yellow").split("|")
  110. );
  111. var fonts = lang.arrayToMap(
  112. ("arial|century|comic|courier|garamond|georgia|helvetica|impact|lucida|" +
  113. "symbol|system|tahoma|times|trebuchet|utopia|verdana|webdings|sans-serif|" +
  114. "serif|monospace").split("|")
  115. );
  116. // regexp must not have capturing parentheses. Use (?:) instead.
  117. // regexps are ordered -> the first match is used
  118. var numRe = "\\-?(?:(?:[0-9]+)|(?:[0-9]*\\.[0-9]+))";
  119. var pseudoElements = "(\\:+)\\b(after|before|first-letter|first-line|moz-selection|selection)\\b";
  120. var pseudoClasses = "(:)\\b(active|checked|disabled|empty|enabled|first-child|first-of-type|focus|hover|indeterminate|invalid|last-child|last-of-type|link|not|nth-child|nth-last-child|nth-last-of-type|nth-of-type|only-child|only-of-type|required|root|target|valid|visited)\\b";
  121. var base_ruleset = [
  122. {
  123. token : "comment", // multi line comment
  124. merge : true,
  125. regex : "\\/\\*",
  126. next : "ruleset_comment"
  127. }, {
  128. token : "string", // single line
  129. regex : '["](?:(?:\\\\.)|(?:[^"\\\\]))*?["]'
  130. }, {
  131. token : "string", // single line
  132. regex : "['](?:(?:\\\\.)|(?:[^'\\\\]))*?[']"
  133. }, {
  134. token : ["constant.numeric", "keyword"],
  135. regex : "(" + numRe + ")(ch|cm|deg|em|ex|fr|gd|grad|Hz|in|kHz|mm|ms|pc|pt|px|rad|rem|s|turn|vh|vm|vw|%)"
  136. }, {
  137. token : ["constant.numeric"],
  138. regex : "([0-9]+)"
  139. }, {
  140. token : "constant.numeric", // hex6 color
  141. regex : "#[a-f0-9]{6}"
  142. }, {
  143. token : "constant.numeric", // hex3 color
  144. regex : "#[a-f0-9]{3}"
  145. }, {
  146. token : ["punctuation", "entity.other.attribute-name.pseudo-element.css"],
  147. regex : pseudoElements
  148. }, {
  149. token : ["punctuation", "entity.other.attribute-name.pseudo-class.css"],
  150. regex : pseudoClasses
  151. }, {
  152. token : function(value) {
  153. if (properties.hasOwnProperty(value.toLowerCase())) {
  154. return "support.type";
  155. }
  156. else if (functions.hasOwnProperty(value.toLowerCase())) {
  157. return "support.function";
  158. }
  159. else if (constants.hasOwnProperty(value.toLowerCase())) {
  160. return "support.constant";
  161. }
  162. else if (colors.hasOwnProperty(value.toLowerCase())) {
  163. return "support.constant.color";
  164. }
  165. else if (fonts.hasOwnProperty(value.toLowerCase())) {
  166. return "support.constant.fonts";
  167. }
  168. else {
  169. return "text";
  170. }
  171. },
  172. regex : "\\-?[a-zA-Z_][a-zA-Z0-9_\\-]*"
  173. }
  174. ];
  175. var ruleset = lang.copyArray(base_ruleset);
  176. ruleset.unshift({
  177. token : "paren.rparen",
  178. regex : "\\}",
  179. next: "start"
  180. });
  181. var media_ruleset = lang.copyArray( base_ruleset );
  182. media_ruleset.unshift({
  183. token : "paren.rparen",
  184. regex : "\\}",
  185. next: "media"
  186. });
  187. var base_comment = [{
  188. token : "comment", // comment spanning whole line
  189. merge : true,
  190. regex : ".+"
  191. }];
  192. var comment = lang.copyArray(base_comment);
  193. comment.unshift({
  194. token : "comment", // closing comment
  195. regex : ".*?\\*\\/",
  196. next : "start"
  197. });
  198. var media_comment = lang.copyArray(base_comment);
  199. media_comment.unshift({
  200. token : "comment", // closing comment
  201. regex : ".*?\\*\\/",
  202. next : "media"
  203. });
  204. var ruleset_comment = lang.copyArray(base_comment);
  205. ruleset_comment.unshift({
  206. token : "comment", // closing comment
  207. regex : ".*?\\*\\/",
  208. next : "ruleset"
  209. });
  210. this.$rules = {
  211. "start" : [{
  212. token : "comment", // multi line comment
  213. merge : true,
  214. regex : "\\/\\*",
  215. next : "comment"
  216. }, {
  217. token: "paren.lparen",
  218. regex: "\\{",
  219. next: "ruleset"
  220. }, {
  221. token: "string",
  222. regex: "@.*?{",
  223. next: "media"
  224. },{
  225. token: "keyword",
  226. regex: "#[a-z0-9-_]+"
  227. },{
  228. token: "variable",
  229. regex: "\\.[a-z0-9-_]+"
  230. },{
  231. token: "string",
  232. regex: ":[a-z0-9-_]+"
  233. },{
  234. token: "constant",
  235. regex: "[a-z0-9-_]+"
  236. }],
  237. "media" : [ {
  238. token : "comment", // multi line comment
  239. merge : true,
  240. regex : "\\/\\*",
  241. next : "media_comment"
  242. }, {
  243. token: "paren.lparen",
  244. regex: "\\{",
  245. next: "media_ruleset"
  246. },{
  247. token: "string",
  248. regex: "\\}",
  249. next: "start"
  250. },{
  251. token: "keyword",
  252. regex: "#[a-z0-9-_]+"
  253. },{
  254. token: "variable",
  255. regex: "\\.[a-z0-9-_]+"
  256. },{
  257. token: "string",
  258. regex: ":[a-z0-9-_]+"
  259. },{
  260. token: "constant",
  261. regex: "[a-z0-9-_]+"
  262. }],
  263. "comment" : comment,
  264. "ruleset" : ruleset,
  265. "ruleset_comment" : ruleset_comment,
  266. "media_ruleset" : media_ruleset,
  267. "media_comment" : media_comment
  268. };
  269. };
  270. oop.inherits(CssHighlightRules, TextHighlightRules);
  271. exports.CssHighlightRules = CssHighlightRules;
  272. });
  273. ace.define('ace/mode/matching_brace_outdent', ['require', 'exports', 'module' , 'ace/range'], function(require, exports, module) {
  274. var Range = require("../range").Range;
  275. var MatchingBraceOutdent = function() {};
  276. (function() {
  277. this.checkOutdent = function(line, input) {
  278. if (! /^\s+$/.test(line))
  279. return false;
  280. return /^\s*\}/.test(input);
  281. };
  282. this.autoOutdent = function(doc, row) {
  283. var line = doc.getLine(row);
  284. var match = line.match(/^(\s*\})/);
  285. if (!match) return 0;
  286. var column = match[1].length;
  287. var openBracePos = doc.findMatchingBracket({row: row, column: column});
  288. if (!openBracePos || openBracePos.row == row) return 0;
  289. var indent = this.$getIndent(doc.getLine(openBracePos.row));
  290. doc.replace(new Range(row, 0, row, column-1), indent);
  291. };
  292. this.$getIndent = function(line) {
  293. var match = line.match(/^(\s+)/);
  294. if (match) {
  295. return match[1];
  296. }
  297. return "";
  298. };
  299. }).call(MatchingBraceOutdent.prototype);
  300. exports.MatchingBraceOutdent = MatchingBraceOutdent;
  301. });
  302. ace.define('ace/mode/folding/cstyle', ['require', 'exports', 'module' , 'ace/lib/oop', 'ace/range', 'ace/mode/folding/fold_mode'], function(require, exports, module) {
  303. var oop = require("../../lib/oop");
  304. var Range = require("../../range").Range;
  305. var BaseFoldMode = require("./fold_mode").FoldMode;
  306. var FoldMode = exports.FoldMode = function() {};
  307. oop.inherits(FoldMode, BaseFoldMode);
  308. (function() {
  309. this.foldingStartMarker = /(\{|\[)[^\}\]]*$|^\s*(\/\*)/;
  310. this.foldingStopMarker = /^[^\[\{]*(\}|\])|^[\s\*]*(\*\/)/;
  311. this.getFoldWidgetRange = function(session, foldStyle, row) {
  312. var line = session.getLine(row);
  313. var match = line.match(this.foldingStartMarker);
  314. if (match) {
  315. var i = match.index;
  316. if (match[1])
  317. return this.openingBracketBlock(session, match[1], row, i);
  318. var range = session.getCommentFoldRange(row, i + match[0].length);
  319. range.end.column -= 2;
  320. return range;
  321. }
  322. if (foldStyle !== "markbeginend")
  323. return;
  324. var match = line.match(this.foldingStopMarker);
  325. if (match) {
  326. var i = match.index + match[0].length;
  327. if (match[2]) {
  328. var range = session.getCommentFoldRange(row, i);
  329. range.end.column -= 2;
  330. return range;
  331. }
  332. var end = {row: row, column: i};
  333. var start = session.$findOpeningBracket(match[1], end);
  334. if (!start)
  335. return;
  336. start.column++;
  337. end.column--;
  338. return Range.fromPoints(start, end);
  339. }
  340. };
  341. }).call(FoldMode.prototype);
  342. });
  343. ace.define('ace/mode/folding/fold_mode', ['require', 'exports', 'module' , 'ace/range'], function(require, exports, module) {
  344. var Range = require("../../range").Range;
  345. var FoldMode = exports.FoldMode = function() {};
  346. (function() {
  347. this.foldingStartMarker = null;
  348. this.foldingStopMarker = null;
  349. // must return "" if there's no fold, to enable caching
  350. this.getFoldWidget = function(session, foldStyle, row) {
  351. var line = session.getLine(row);
  352. if (this.foldingStartMarker.test(line))
  353. return "start";
  354. if (foldStyle == "markbeginend"
  355. && this.foldingStopMarker
  356. && this.foldingStopMarker.test(line))
  357. return "end";
  358. return "";
  359. };
  360. this.getFoldWidgetRange = function(session, foldStyle, row) {
  361. return null;
  362. };
  363. this.indentationBlock = function(session, row, column) {
  364. var re = /^\s*/;
  365. var startRow = row;
  366. var endRow = row;
  367. var line = session.getLine(row);
  368. var startColumn = column || line.length;
  369. var startLevel = line.match(re)[0].length;
  370. var maxRow = session.getLength()
  371. while (++row < maxRow) {
  372. line = session.getLine(row);
  373. var level = line.match(re)[0].length;
  374. if (level == line.length)
  375. continue;
  376. if (level <= startLevel)
  377. break;
  378. endRow = row;
  379. }
  380. if (endRow > startRow) {
  381. var endColumn = session.getLine(endRow).length;
  382. return new Range(startRow, startColumn, endRow, endColumn);
  383. }
  384. };
  385. this.openingBracketBlock = function(session, bracket, row, column, typeRe, allowBlankLine) {
  386. var start = {row: row, column: column + 1};
  387. var end = session.$findClosingBracket(bracket, start, typeRe, allowBlankLine);
  388. if (!end)
  389. return;
  390. var fw = session.foldWidgets[end.row];
  391. if (fw == null)
  392. fw = this.getFoldWidget(session, end.row);
  393. if (fw == "start") {
  394. end.row --;
  395. end.column = session.getLine(end.row).length;
  396. }
  397. return Range.fromPoints(start, end);
  398. };
  399. }).call(FoldMode.prototype);
  400. });