mode-xml.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. define("ace/mode/xml_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_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. define("ace/mode/behaviour/xml",["require","exports","module","ace/lib/oop","ace/mode/behaviour","ace/token_iterator","ace/lib/lang"], 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.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 firstChar = token.value.charAt(0);
  249. if (firstChar == '"' || firstChar == "'") {
  250. var lastChar = token.value.charAt(token.value.length - 1);
  251. var tokenEnd = iterator.getCurrentTokenColumn() + token.value.length;
  252. if (tokenEnd > position.column || tokenEnd == position.column && firstChar != lastChar)
  253. return;
  254. }
  255. }
  256. while (!is(token, "tag-name")) {
  257. token = iterator.stepBackward();
  258. if (token.value == "<") {
  259. token = iterator.stepForward();
  260. break;
  261. }
  262. }
  263. var tokenRow = iterator.getCurrentTokenRow();
  264. var tokenColumn = iterator.getCurrentTokenColumn();
  265. if (is(iterator.stepBackward(), "end-tag-open"))
  266. return;
  267. var element = token.value;
  268. if (tokenRow == position.row)
  269. element = element.substring(0, position.column - tokenColumn);
  270. if (this.voidElements.hasOwnProperty(element.toLowerCase()))
  271. return;
  272. return {
  273. text: ">" + "</" + element + ">",
  274. selection: [1, 1]
  275. };
  276. }
  277. });
  278. this.add("autoindent", "insertion", function (state, action, editor, session, text) {
  279. if (text == "\n") {
  280. var cursor = editor.getCursorPosition();
  281. var line = session.getLine(cursor.row);
  282. var iterator = new TokenIterator(session, cursor.row, cursor.column);
  283. var token = iterator.getCurrentToken();
  284. if (token && token.type.indexOf("tag-close") !== -1) {
  285. if (token.value == "/>")
  286. return;
  287. while (token && token.type.indexOf("tag-name") === -1) {
  288. token = iterator.stepBackward();
  289. }
  290. if (!token) {
  291. return;
  292. }
  293. var tag = token.value;
  294. var row = iterator.getCurrentTokenRow();
  295. token = iterator.stepBackward();
  296. if (!token || token.type.indexOf("end-tag") !== -1) {
  297. return;
  298. }
  299. if (this.voidElements && !this.voidElements[tag]) {
  300. var nextToken = session.getTokenAt(cursor.row, cursor.column+1);
  301. var line = session.getLine(row);
  302. var nextIndent = this.$getIndent(line);
  303. var indent = nextIndent + session.getTabString();
  304. if (nextToken && nextToken.value === "</") {
  305. return {
  306. text: "\n" + indent + "\n" + nextIndent,
  307. selection: [1, indent.length, 1, indent.length]
  308. };
  309. } else {
  310. return {
  311. text: "\n" + indent
  312. };
  313. }
  314. }
  315. }
  316. }
  317. });
  318. };
  319. oop.inherits(XmlBehaviour, Behaviour);
  320. exports.XmlBehaviour = XmlBehaviour;
  321. });
  322. define("ace/mode/folding/xml",["require","exports","module","ace/lib/oop","ace/lib/lang","ace/range","ace/mode/folding/fold_mode","ace/token_iterator"], function(require, exports, module) {
  323. "use strict";
  324. var oop = require("../../lib/oop");
  325. var lang = require("../../lib/lang");
  326. var Range = require("../../range").Range;
  327. var BaseFoldMode = require("./fold_mode").FoldMode;
  328. var TokenIterator = require("../../token_iterator").TokenIterator;
  329. var FoldMode = exports.FoldMode = function(voidElements, optionalEndTags) {
  330. BaseFoldMode.call(this);
  331. this.voidElements = voidElements || {};
  332. this.optionalEndTags = oop.mixin({}, this.voidElements);
  333. if (optionalEndTags)
  334. oop.mixin(this.optionalEndTags, optionalEndTags);
  335. };
  336. oop.inherits(FoldMode, BaseFoldMode);
  337. var Tag = function() {
  338. this.tagName = "";
  339. this.closing = false;
  340. this.selfClosing = false;
  341. this.start = {row: 0, column: 0};
  342. this.end = {row: 0, column: 0};
  343. };
  344. function is(token, type) {
  345. return token.type.lastIndexOf(type + ".xml") > -1;
  346. }
  347. (function() {
  348. this.getFoldWidget = function(session, foldStyle, row) {
  349. var tag = this._getFirstTagInLine(session, row);
  350. if (!tag)
  351. return this.getCommentFoldWidget(session, row);
  352. if (tag.closing || (!tag.tagName && tag.selfClosing))
  353. return foldStyle == "markbeginend" ? "end" : "";
  354. if (!tag.tagName || tag.selfClosing || this.voidElements.hasOwnProperty(tag.tagName.toLowerCase()))
  355. return "";
  356. if (this._findEndTagInLine(session, row, tag.tagName, tag.end.column))
  357. return "";
  358. return "start";
  359. };
  360. this.getCommentFoldWidget = function(session, row) {
  361. if (/comment/.test(session.getState(row)) && /<!-/.test(session.getLine(row)))
  362. return "start";
  363. return "";
  364. }
  365. this._getFirstTagInLine = function(session, row) {
  366. var tokens = session.getTokens(row);
  367. var tag = new Tag();
  368. for (var i = 0; i < tokens.length; i++) {
  369. var token = tokens[i];
  370. if (is(token, "tag-open")) {
  371. tag.end.column = tag.start.column + token.value.length;
  372. tag.closing = is(token, "end-tag-open");
  373. token = tokens[++i];
  374. if (!token)
  375. return null;
  376. tag.tagName = token.value;
  377. tag.end.column += token.value.length;
  378. for (i++; i < tokens.length; i++) {
  379. token = tokens[i];
  380. tag.end.column += token.value.length;
  381. if (is(token, "tag-close")) {
  382. tag.selfClosing = token.value == '/>';
  383. break;
  384. }
  385. }
  386. return tag;
  387. } else if (is(token, "tag-close")) {
  388. tag.selfClosing = token.value == '/>';
  389. return tag;
  390. }
  391. tag.start.column += token.value.length;
  392. }
  393. return null;
  394. };
  395. this._findEndTagInLine = function(session, row, tagName, startColumn) {
  396. var tokens = session.getTokens(row);
  397. var column = 0;
  398. for (var i = 0; i < tokens.length; i++) {
  399. var token = tokens[i];
  400. column += token.value.length;
  401. if (column < startColumn)
  402. continue;
  403. if (is(token, "end-tag-open")) {
  404. token = tokens[i + 1];
  405. if (token && token.value == tagName)
  406. return true;
  407. }
  408. }
  409. return false;
  410. };
  411. this._readTagForward = function(iterator) {
  412. var token = iterator.getCurrentToken();
  413. if (!token)
  414. return null;
  415. var tag = new Tag();
  416. do {
  417. if (is(token, "tag-open")) {
  418. tag.closing = is(token, "end-tag-open");
  419. tag.start.row = iterator.getCurrentTokenRow();
  420. tag.start.column = iterator.getCurrentTokenColumn();
  421. } else if (is(token, "tag-name")) {
  422. tag.tagName = token.value;
  423. } else if (is(token, "tag-close")) {
  424. tag.selfClosing = token.value == "/>";
  425. tag.end.row = iterator.getCurrentTokenRow();
  426. tag.end.column = iterator.getCurrentTokenColumn() + token.value.length;
  427. iterator.stepForward();
  428. return tag;
  429. }
  430. } while(token = iterator.stepForward());
  431. return null;
  432. };
  433. this._readTagBackward = function(iterator) {
  434. var token = iterator.getCurrentToken();
  435. if (!token)
  436. return null;
  437. var tag = new Tag();
  438. do {
  439. if (is(token, "tag-open")) {
  440. tag.closing = is(token, "end-tag-open");
  441. tag.start.row = iterator.getCurrentTokenRow();
  442. tag.start.column = iterator.getCurrentTokenColumn();
  443. iterator.stepBackward();
  444. return tag;
  445. } else if (is(token, "tag-name")) {
  446. tag.tagName = token.value;
  447. } else if (is(token, "tag-close")) {
  448. tag.selfClosing = token.value == "/>";
  449. tag.end.row = iterator.getCurrentTokenRow();
  450. tag.end.column = iterator.getCurrentTokenColumn() + token.value.length;
  451. }
  452. } while(token = iterator.stepBackward());
  453. return null;
  454. };
  455. this._pop = function(stack, tag) {
  456. while (stack.length) {
  457. var top = stack[stack.length-1];
  458. if (!tag || top.tagName == tag.tagName) {
  459. return stack.pop();
  460. }
  461. else if (this.optionalEndTags.hasOwnProperty(top.tagName)) {
  462. stack.pop();
  463. continue;
  464. } else {
  465. return null;
  466. }
  467. }
  468. };
  469. this.getFoldWidgetRange = function(session, foldStyle, row) {
  470. var firstTag = this._getFirstTagInLine(session, row);
  471. if (!firstTag) {
  472. return this.getCommentFoldWidget(session, row)
  473. && session.getCommentFoldRange(row, session.getLine(row).length);
  474. }
  475. var isBackward = firstTag.closing || firstTag.selfClosing;
  476. var stack = [];
  477. var tag;
  478. if (!isBackward) {
  479. var iterator = new TokenIterator(session, row, firstTag.start.column);
  480. var start = {
  481. row: row,
  482. column: firstTag.start.column + firstTag.tagName.length + 2
  483. };
  484. if (firstTag.start.row == firstTag.end.row)
  485. start.column = firstTag.end.column;
  486. while (tag = this._readTagForward(iterator)) {
  487. if (tag.selfClosing) {
  488. if (!stack.length) {
  489. tag.start.column += tag.tagName.length + 2;
  490. tag.end.column -= 2;
  491. return Range.fromPoints(tag.start, tag.end);
  492. } else
  493. continue;
  494. }
  495. if (tag.closing) {
  496. this._pop(stack, tag);
  497. if (stack.length == 0)
  498. return Range.fromPoints(start, tag.start);
  499. }
  500. else {
  501. stack.push(tag);
  502. }
  503. }
  504. }
  505. else {
  506. var iterator = new TokenIterator(session, row, firstTag.end.column);
  507. var end = {
  508. row: row,
  509. column: firstTag.start.column
  510. };
  511. while (tag = this._readTagBackward(iterator)) {
  512. if (tag.selfClosing) {
  513. if (!stack.length) {
  514. tag.start.column += tag.tagName.length + 2;
  515. tag.end.column -= 2;
  516. return Range.fromPoints(tag.start, tag.end);
  517. } else
  518. continue;
  519. }
  520. if (!tag.closing) {
  521. this._pop(stack, tag);
  522. if (stack.length == 0) {
  523. tag.start.column += tag.tagName.length + 2;
  524. if (tag.start.row == tag.end.row && tag.start.column < tag.end.column)
  525. tag.start.column = tag.end.column;
  526. return Range.fromPoints(tag.start, end);
  527. }
  528. }
  529. else {
  530. stack.push(tag);
  531. }
  532. }
  533. }
  534. };
  535. }).call(FoldMode.prototype);
  536. });
  537. define("ace/mode/xml",["require","exports","module","ace/lib/oop","ace/lib/lang","ace/mode/text","ace/mode/xml_highlight_rules","ace/mode/behaviour/xml","ace/mode/folding/xml","ace/worker/worker_client"], function(require, exports, module) {
  538. "use strict";
  539. var oop = require("../lib/oop");
  540. var lang = require("../lib/lang");
  541. var TextMode = require("./text").Mode;
  542. var XmlHighlightRules = require("./xml_highlight_rules").XmlHighlightRules;
  543. var XmlBehaviour = require("./behaviour/xml").XmlBehaviour;
  544. var XmlFoldMode = require("./folding/xml").FoldMode;
  545. var WorkerClient = require("../worker/worker_client").WorkerClient;
  546. var Mode = function() {
  547. this.HighlightRules = XmlHighlightRules;
  548. this.$behaviour = new XmlBehaviour();
  549. this.foldingRules = new XmlFoldMode();
  550. };
  551. oop.inherits(Mode, TextMode);
  552. (function() {
  553. this.voidElements = lang.arrayToMap([]);
  554. this.blockComment = {start: "<!--", end: "-->"};
  555. this.createWorker = function(session) {
  556. var worker = new WorkerClient(["ace"], "ace/mode/xml_worker", "Worker");
  557. worker.attachToDocument(session.getDocument());
  558. worker.on("error", function(e) {
  559. session.setAnnotations(e.data);
  560. });
  561. worker.on("terminate", function() {
  562. session.clearAnnotations();
  563. });
  564. return worker;
  565. };
  566. this.$id = "ace/mode/xml";
  567. }).call(Mode.prototype);
  568. exports.Mode = Mode;
  569. });