mode-xml.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. ace.define("ace/mode/xml_highlight_rules",[], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("../lib/oop");
  4. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  5. var XmlHighlightRules = function(normalize) {
  6. var tagRegex = "[_:a-zA-Z\xc0-\uffff][-_:.a-zA-Z0-9\xc0-\uffff]*";
  7. this.$rules = {
  8. start : [
  9. {token : "string.cdata.xml", regex : "<\\!\\[CDATA\\[", next : "cdata"},
  10. {
  11. token : ["punctuation.instruction.xml", "keyword.instruction.xml"],
  12. regex : "(<\\?)(" + tagRegex + ")", next : "processing_instruction"
  13. },
  14. {token : "comment.start.xml", regex : "<\\!--", next : "comment"},
  15. {
  16. token : ["xml-pe.doctype.xml", "xml-pe.doctype.xml"],
  17. regex : "(<\\!)(DOCTYPE)(?=[\\s])", next : "doctype", caseInsensitive: true
  18. },
  19. {include : "tag"},
  20. {token : "text.end-tag-open.xml", regex: "</"},
  21. {token : "text.tag-open.xml", regex: "<"},
  22. {include : "reference"},
  23. {defaultToken : "text.xml"}
  24. ],
  25. processing_instruction : [{
  26. token : "entity.other.attribute-name.decl-attribute-name.xml",
  27. regex : tagRegex
  28. }, {
  29. token : "keyword.operator.decl-attribute-equals.xml",
  30. regex : "="
  31. }, {
  32. include: "whitespace"
  33. }, {
  34. include: "string"
  35. }, {
  36. token : "punctuation.xml-decl.xml",
  37. regex : "\\?>",
  38. next : "start"
  39. }],
  40. doctype : [
  41. {include : "whitespace"},
  42. {include : "string"},
  43. {token : "xml-pe.doctype.xml", regex : ">", next : "start"},
  44. {token : "xml-pe.xml", regex : "[-_a-zA-Z0-9:]+"},
  45. {token : "punctuation.int-subset", regex : "\\[", push : "int_subset"}
  46. ],
  47. int_subset : [{
  48. token : "text.xml",
  49. regex : "\\s+"
  50. }, {
  51. token: "punctuation.int-subset.xml",
  52. regex: "]",
  53. next: "pop"
  54. }, {
  55. token : ["punctuation.markup-decl.xml", "keyword.markup-decl.xml"],
  56. regex : "(<\\!)(" + tagRegex + ")",
  57. push : [{
  58. token : "text",
  59. regex : "\\s+"
  60. },
  61. {
  62. token : "punctuation.markup-decl.xml",
  63. regex : ">",
  64. next : "pop"
  65. },
  66. {include : "string"}]
  67. }],
  68. cdata : [
  69. {token : "string.cdata.xml", regex : "\\]\\]>", next : "start"},
  70. {token : "text.xml", regex : "\\s+"},
  71. {token : "text.xml", regex : "(?:[^\\]]|\\](?!\\]>))+"}
  72. ],
  73. comment : [
  74. {token : "comment.end.xml", regex : "-->", next : "start"},
  75. {defaultToken : "comment.xml"}
  76. ],
  77. reference : [{
  78. token : "constant.language.escape.reference.xml",
  79. regex : "(?:&#[0-9]+;)|(?:&#x[0-9a-fA-F]+;)|(?:&[a-zA-Z0-9_:\\.-]+;)"
  80. }],
  81. attr_reference : [{
  82. token : "constant.language.escape.reference.attribute-value.xml",
  83. regex : "(?:&#[0-9]+;)|(?:&#x[0-9a-fA-F]+;)|(?:&[a-zA-Z0-9_:\\.-]+;)"
  84. }],
  85. tag : [{
  86. token : ["meta.tag.punctuation.tag-open.xml", "meta.tag.punctuation.end-tag-open.xml", "meta.tag.tag-name.xml"],
  87. regex : "(?:(<)|(</))((?:" + tagRegex + ":)?" + tagRegex + ")",
  88. next: [
  89. {include : "attributes"},
  90. {token : "meta.tag.punctuation.tag-close.xml", regex : "/?>", next : "start"}
  91. ]
  92. }],
  93. tag_whitespace : [
  94. {token : "text.tag-whitespace.xml", regex : "\\s+"}
  95. ],
  96. whitespace : [
  97. {token : "text.whitespace.xml", regex : "\\s+"}
  98. ],
  99. string: [{
  100. token : "string.xml",
  101. regex : "'",
  102. push : [
  103. {token : "string.xml", regex: "'", next: "pop"},
  104. {defaultToken : "string.xml"}
  105. ]
  106. }, {
  107. token : "string.xml",
  108. regex : '"',
  109. push : [
  110. {token : "string.xml", regex: '"', next: "pop"},
  111. {defaultToken : "string.xml"}
  112. ]
  113. }],
  114. attributes: [{
  115. token : "entity.other.attribute-name.xml",
  116. regex : tagRegex
  117. }, {
  118. token : "keyword.operator.attribute-equals.xml",
  119. regex : "="
  120. }, {
  121. include: "tag_whitespace"
  122. }, {
  123. include: "attribute_value"
  124. }],
  125. attribute_value: [{
  126. token : "string.attribute-value.xml",
  127. regex : "'",
  128. push : [
  129. {token : "string.attribute-value.xml", regex: "'", next: "pop"},
  130. {include : "attr_reference"},
  131. {defaultToken : "string.attribute-value.xml"}
  132. ]
  133. }, {
  134. token : "string.attribute-value.xml",
  135. regex : '"',
  136. push : [
  137. {token : "string.attribute-value.xml", regex: '"', next: "pop"},
  138. {include : "attr_reference"},
  139. {defaultToken : "string.attribute-value.xml"}
  140. ]
  141. }]
  142. };
  143. if (this.constructor === XmlHighlightRules)
  144. this.normalizeRules();
  145. };
  146. (function() {
  147. this.embedTagRules = function(HighlightRules, prefix, tag){
  148. this.$rules.tag.unshift({
  149. token : ["meta.tag.punctuation.tag-open.xml", "meta.tag." + tag + ".tag-name.xml"],
  150. regex : "(<)(" + tag + "(?=\\s|>|$))",
  151. next: [
  152. {include : "attributes"},
  153. {token : "meta.tag.punctuation.tag-close.xml", regex : "/?>", next : prefix + "start"}
  154. ]
  155. });
  156. this.$rules[tag + "-end"] = [
  157. {include : "attributes"},
  158. {token : "meta.tag.punctuation.tag-close.xml", regex : "/?>", next: "start",
  159. onMatch : function(value, currentState, stack) {
  160. stack.splice(0);
  161. return this.token;
  162. }}
  163. ];
  164. this.embedRules(HighlightRules, prefix, [{
  165. token: ["meta.tag.punctuation.end-tag-open.xml", "meta.tag." + tag + ".tag-name.xml"],
  166. regex : "(</)(" + tag + "(?=\\s|>|$))",
  167. next: tag + "-end"
  168. }, {
  169. token: "string.cdata.xml",
  170. regex : "<\\!\\[CDATA\\["
  171. }, {
  172. token: "string.cdata.xml",
  173. regex : "\\]\\]>"
  174. }]);
  175. };
  176. }).call(TextHighlightRules.prototype);
  177. oop.inherits(XmlHighlightRules, TextHighlightRules);
  178. exports.XmlHighlightRules = XmlHighlightRules;
  179. });
  180. ace.define("ace/mode/behaviour/xml",[], function(require, exports, module) {
  181. "use strict";
  182. var oop = require("../../lib/oop");
  183. var Behaviour = require("../behaviour").Behaviour;
  184. var TokenIterator = require("../../token_iterator").TokenIterator;
  185. var lang = require("../../lib/lang");
  186. function is(token, type) {
  187. return token && token.type.lastIndexOf(type + ".xml") > -1;
  188. }
  189. var XmlBehaviour = function () {
  190. this.add("string_dquotes", "insertion", function (state, action, editor, session, text) {
  191. if (text == '"' || text == "'") {
  192. var quote = text;
  193. var selected = session.doc.getTextRange(editor.getSelectionRange());
  194. if (selected !== "" && selected !== "'" && selected != '"' && editor.getWrapBehavioursEnabled()) {
  195. return {
  196. text: quote + selected + quote,
  197. selection: false
  198. };
  199. }
  200. var cursor = editor.getCursorPosition();
  201. var line = session.doc.getLine(cursor.row);
  202. var rightChar = line.substring(cursor.column, cursor.column + 1);
  203. var iterator = new TokenIterator(session, cursor.row, cursor.column);
  204. var token = iterator.getCurrentToken();
  205. if (rightChar == quote && (is(token, "attribute-value") || is(token, "string"))) {
  206. return {
  207. text: "",
  208. selection: [1, 1]
  209. };
  210. }
  211. if (!token)
  212. token = iterator.stepBackward();
  213. if (!token)
  214. return;
  215. while (is(token, "tag-whitespace") || is(token, "whitespace")) {
  216. token = iterator.stepBackward();
  217. }
  218. var rightSpace = !rightChar || rightChar.match(/\s/);
  219. if (is(token, "attribute-equals") && (rightSpace || rightChar == '>') || (is(token, "decl-attribute-equals") && (rightSpace || rightChar == '?'))) {
  220. return {
  221. text: quote + quote,
  222. selection: [1, 1]
  223. };
  224. }
  225. }
  226. });
  227. this.add("string_dquotes", "deletion", function(state, action, editor, session, range) {
  228. var selected = session.doc.getTextRange(range);
  229. if (!range.isMultiLine() && (selected == '"' || selected == "'")) {
  230. var line = session.doc.getLine(range.start.row);
  231. var rightChar = line.substring(range.start.column + 1, range.start.column + 2);
  232. if (rightChar == selected) {
  233. range.end.column++;
  234. return range;
  235. }
  236. }
  237. });
  238. this.add("autoclosing", "insertion", function (state, action, editor, session, text) {
  239. if (text == '>') {
  240. var position = editor.getSelectionRange().start;
  241. var iterator = new TokenIterator(session, position.row, position.column);
  242. var token = iterator.getCurrentToken() || iterator.stepBackward();
  243. if (!token || !(is(token, "tag-name") || is(token, "tag-whitespace") || is(token, "attribute-name") || is(token, "attribute-equals") || is(token, "attribute-value")))
  244. return;
  245. if (is(token, "reference.attribute-value"))
  246. return;
  247. if (is(token, "attribute-value")) {
  248. var tokenEndColumn = iterator.getCurrentTokenColumn() + token.value.length;
  249. if (position.column < tokenEndColumn)
  250. return;
  251. if (position.column == tokenEndColumn) {
  252. var nextToken = iterator.stepForward();
  253. if (nextToken && is(nextToken, "attribute-value"))
  254. return;
  255. iterator.stepBackward();
  256. }
  257. }
  258. if (/^\s*>/.test(session.getLine(position.row).slice(position.column)))
  259. return;
  260. while (!is(token, "tag-name")) {
  261. token = iterator.stepBackward();
  262. if (token.value == "<") {
  263. token = iterator.stepForward();
  264. break;
  265. }
  266. }
  267. var tokenRow = iterator.getCurrentTokenRow();
  268. var tokenColumn = iterator.getCurrentTokenColumn();
  269. if (is(iterator.stepBackward(), "end-tag-open"))
  270. return;
  271. var element = token.value;
  272. if (tokenRow == position.row)
  273. element = element.substring(0, position.column - tokenColumn);
  274. if (this.voidElements.hasOwnProperty(element.toLowerCase()))
  275. return;
  276. return {
  277. text: ">" + "</" + element + ">",
  278. selection: [1, 1]
  279. };
  280. }
  281. });
  282. this.add("autoindent", "insertion", function (state, action, editor, session, text) {
  283. if (text == "\n") {
  284. var cursor = editor.getCursorPosition();
  285. var line = session.getLine(cursor.row);
  286. var iterator = new TokenIterator(session, cursor.row, cursor.column);
  287. var token = iterator.getCurrentToken();
  288. if (token && token.type.indexOf("tag-close") !== -1) {
  289. if (token.value == "/>")
  290. return;
  291. while (token && token.type.indexOf("tag-name") === -1) {
  292. token = iterator.stepBackward();
  293. }
  294. if (!token) {
  295. return;
  296. }
  297. var tag = token.value;
  298. var row = iterator.getCurrentTokenRow();
  299. token = iterator.stepBackward();
  300. if (!token || token.type.indexOf("end-tag") !== -1) {
  301. return;
  302. }
  303. if (this.voidElements && !this.voidElements[tag]) {
  304. var nextToken = session.getTokenAt(cursor.row, cursor.column+1);
  305. var line = session.getLine(row);
  306. var nextIndent = this.$getIndent(line);
  307. var indent = nextIndent + session.getTabString();
  308. if (nextToken && nextToken.value === "</") {
  309. return {
  310. text: "\n" + indent + "\n" + nextIndent,
  311. selection: [1, indent.length, 1, indent.length]
  312. };
  313. } else {
  314. return {
  315. text: "\n" + indent
  316. };
  317. }
  318. }
  319. }
  320. }
  321. });
  322. };
  323. oop.inherits(XmlBehaviour, Behaviour);
  324. exports.XmlBehaviour = XmlBehaviour;
  325. });
  326. ace.define("ace/mode/folding/xml",[], function(require, exports, module) {
  327. "use strict";
  328. var oop = require("../../lib/oop");
  329. var lang = require("../../lib/lang");
  330. var Range = require("../../range").Range;
  331. var BaseFoldMode = require("./fold_mode").FoldMode;
  332. var TokenIterator = require("../../token_iterator").TokenIterator;
  333. var FoldMode = exports.FoldMode = function(voidElements, optionalEndTags) {
  334. BaseFoldMode.call(this);
  335. this.voidElements = voidElements || {};
  336. this.optionalEndTags = oop.mixin({}, this.voidElements);
  337. if (optionalEndTags)
  338. oop.mixin(this.optionalEndTags, optionalEndTags);
  339. };
  340. oop.inherits(FoldMode, BaseFoldMode);
  341. var Tag = function() {
  342. this.tagName = "";
  343. this.closing = false;
  344. this.selfClosing = false;
  345. this.start = {row: 0, column: 0};
  346. this.end = {row: 0, column: 0};
  347. };
  348. function is(token, type) {
  349. return token.type.lastIndexOf(type + ".xml") > -1;
  350. }
  351. (function() {
  352. this.getFoldWidget = function(session, foldStyle, row) {
  353. var tag = this._getFirstTagInLine(session, row);
  354. if (!tag)
  355. return this.getCommentFoldWidget(session, row);
  356. if (tag.closing || (!tag.tagName && tag.selfClosing))
  357. return foldStyle == "markbeginend" ? "end" : "";
  358. if (!tag.tagName || tag.selfClosing || this.voidElements.hasOwnProperty(tag.tagName.toLowerCase()))
  359. return "";
  360. if (this._findEndTagInLine(session, row, tag.tagName, tag.end.column))
  361. return "";
  362. return "start";
  363. };
  364. this.getCommentFoldWidget = function(session, row) {
  365. if (/comment/.test(session.getState(row)) && /<!-/.test(session.getLine(row)))
  366. return "start";
  367. return "";
  368. };
  369. this._getFirstTagInLine = function(session, row) {
  370. var tokens = session.getTokens(row);
  371. var tag = new Tag();
  372. for (var i = 0; i < tokens.length; i++) {
  373. var token = tokens[i];
  374. if (is(token, "tag-open")) {
  375. tag.end.column = tag.start.column + token.value.length;
  376. tag.closing = is(token, "end-tag-open");
  377. token = tokens[++i];
  378. if (!token)
  379. return null;
  380. tag.tagName = token.value;
  381. tag.end.column += token.value.length;
  382. for (i++; i < tokens.length; i++) {
  383. token = tokens[i];
  384. tag.end.column += token.value.length;
  385. if (is(token, "tag-close")) {
  386. tag.selfClosing = token.value == '/>';
  387. break;
  388. }
  389. }
  390. return tag;
  391. } else if (is(token, "tag-close")) {
  392. tag.selfClosing = token.value == '/>';
  393. return tag;
  394. }
  395. tag.start.column += token.value.length;
  396. }
  397. return null;
  398. };
  399. this._findEndTagInLine = function(session, row, tagName, startColumn) {
  400. var tokens = session.getTokens(row);
  401. var column = 0;
  402. for (var i = 0; i < tokens.length; i++) {
  403. var token = tokens[i];
  404. column += token.value.length;
  405. if (column < startColumn)
  406. continue;
  407. if (is(token, "end-tag-open")) {
  408. token = tokens[i + 1];
  409. if (token && token.value == tagName)
  410. return true;
  411. }
  412. }
  413. return false;
  414. };
  415. this._readTagForward = function(iterator) {
  416. var token = iterator.getCurrentToken();
  417. if (!token)
  418. return null;
  419. var tag = new Tag();
  420. do {
  421. if (is(token, "tag-open")) {
  422. tag.closing = is(token, "end-tag-open");
  423. tag.start.row = iterator.getCurrentTokenRow();
  424. tag.start.column = iterator.getCurrentTokenColumn();
  425. } else if (is(token, "tag-name")) {
  426. tag.tagName = token.value;
  427. } else if (is(token, "tag-close")) {
  428. tag.selfClosing = token.value == "/>";
  429. tag.end.row = iterator.getCurrentTokenRow();
  430. tag.end.column = iterator.getCurrentTokenColumn() + token.value.length;
  431. iterator.stepForward();
  432. return tag;
  433. }
  434. } while(token = iterator.stepForward());
  435. return null;
  436. };
  437. this._readTagBackward = function(iterator) {
  438. var token = iterator.getCurrentToken();
  439. if (!token)
  440. return null;
  441. var tag = new Tag();
  442. do {
  443. if (is(token, "tag-open")) {
  444. tag.closing = is(token, "end-tag-open");
  445. tag.start.row = iterator.getCurrentTokenRow();
  446. tag.start.column = iterator.getCurrentTokenColumn();
  447. iterator.stepBackward();
  448. return tag;
  449. } else if (is(token, "tag-name")) {
  450. tag.tagName = token.value;
  451. } else if (is(token, "tag-close")) {
  452. tag.selfClosing = token.value == "/>";
  453. tag.end.row = iterator.getCurrentTokenRow();
  454. tag.end.column = iterator.getCurrentTokenColumn() + token.value.length;
  455. }
  456. } while(token = iterator.stepBackward());
  457. return null;
  458. };
  459. this._pop = function(stack, tag) {
  460. while (stack.length) {
  461. var top = stack[stack.length-1];
  462. if (!tag || top.tagName == tag.tagName) {
  463. return stack.pop();
  464. }
  465. else if (this.optionalEndTags.hasOwnProperty(top.tagName)) {
  466. stack.pop();
  467. continue;
  468. } else {
  469. return null;
  470. }
  471. }
  472. };
  473. this.getFoldWidgetRange = function(session, foldStyle, row) {
  474. var firstTag = this._getFirstTagInLine(session, row);
  475. if (!firstTag) {
  476. return this.getCommentFoldWidget(session, row)
  477. && session.getCommentFoldRange(row, session.getLine(row).length);
  478. }
  479. var isBackward = firstTag.closing || firstTag.selfClosing;
  480. var stack = [];
  481. var tag;
  482. if (!isBackward) {
  483. var iterator = new TokenIterator(session, row, firstTag.start.column);
  484. var start = {
  485. row: row,
  486. column: firstTag.start.column + firstTag.tagName.length + 2
  487. };
  488. if (firstTag.start.row == firstTag.end.row)
  489. start.column = firstTag.end.column;
  490. while (tag = this._readTagForward(iterator)) {
  491. if (tag.selfClosing) {
  492. if (!stack.length) {
  493. tag.start.column += tag.tagName.length + 2;
  494. tag.end.column -= 2;
  495. return Range.fromPoints(tag.start, tag.end);
  496. } else
  497. continue;
  498. }
  499. if (tag.closing) {
  500. this._pop(stack, tag);
  501. if (stack.length == 0)
  502. return Range.fromPoints(start, tag.start);
  503. }
  504. else {
  505. stack.push(tag);
  506. }
  507. }
  508. }
  509. else {
  510. var iterator = new TokenIterator(session, row, firstTag.end.column);
  511. var end = {
  512. row: row,
  513. column: firstTag.start.column
  514. };
  515. while (tag = this._readTagBackward(iterator)) {
  516. if (tag.selfClosing) {
  517. if (!stack.length) {
  518. tag.start.column += tag.tagName.length + 2;
  519. tag.end.column -= 2;
  520. return Range.fromPoints(tag.start, tag.end);
  521. } else
  522. continue;
  523. }
  524. if (!tag.closing) {
  525. this._pop(stack, tag);
  526. if (stack.length == 0) {
  527. tag.start.column += tag.tagName.length + 2;
  528. if (tag.start.row == tag.end.row && tag.start.column < tag.end.column)
  529. tag.start.column = tag.end.column;
  530. return Range.fromPoints(tag.start, end);
  531. }
  532. }
  533. else {
  534. stack.push(tag);
  535. }
  536. }
  537. }
  538. };
  539. }).call(FoldMode.prototype);
  540. });
  541. ace.define("ace/mode/xml",[], function(require, exports, module) {
  542. "use strict";
  543. var oop = require("../lib/oop");
  544. var lang = require("../lib/lang");
  545. var TextMode = require("./text").Mode;
  546. var XmlHighlightRules = require("./xml_highlight_rules").XmlHighlightRules;
  547. var XmlBehaviour = require("./behaviour/xml").XmlBehaviour;
  548. var XmlFoldMode = require("./folding/xml").FoldMode;
  549. var WorkerClient = require("../worker/worker_client").WorkerClient;
  550. var Mode = function() {
  551. this.HighlightRules = XmlHighlightRules;
  552. this.$behaviour = new XmlBehaviour();
  553. this.foldingRules = new XmlFoldMode();
  554. };
  555. oop.inherits(Mode, TextMode);
  556. (function() {
  557. this.voidElements = lang.arrayToMap([]);
  558. this.blockComment = {start: "<!--", end: "-->"};
  559. this.createWorker = function(session) {
  560. var worker = new WorkerClient(["ace"], "ace/mode/xml_worker", "Worker");
  561. worker.attachToDocument(session.getDocument());
  562. worker.on("error", function(e) {
  563. session.setAnnotations(e.data);
  564. });
  565. worker.on("terminate", function() {
  566. session.clearAnnotations();
  567. });
  568. return worker;
  569. };
  570. this.$id = "ace/mode/xml";
  571. }).call(Mode.prototype);
  572. exports.Mode = Mode;
  573. });
  574. (function() {
  575. ace.require(["ace/mode/xml"], function(m) {
  576. if (typeof module == "object" && typeof exports == "object" && module) {
  577. module.exports = m;
  578. }
  579. });
  580. })();