xml_highlight_rules.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 xmlUtil = require("./xml_util");
  34. var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
  35. var XmlHighlightRules = function(normalize) {
  36. this.$rules = {
  37. start : [
  38. {token : "punctuation.string.begin", regex : "<\\!\\[CDATA\\[", next : "cdata"},
  39. {
  40. token : ["punctuation.instruction.begin", "keyword.instruction"],
  41. regex : "(<\\?)(xml)(?=[\\s])", next : "xml_declaration"
  42. },
  43. {
  44. token : ["punctuation.instruction.begin", "keyword.instruction"],
  45. regex : "(<\\?)([-_a-zA-Z0-9]+)", next : "instruction"
  46. },
  47. {token : "comment", regex : "<\\!--", next : "comment"},
  48. {
  49. token : ["punctuation.doctype.begin", "meta.tag.doctype"],
  50. regex : "(<\\!)(DOCTYPE)(?=[\\s])", next : "doctype"
  51. },
  52. {include : "tag"},
  53. {include : "reference"}
  54. ],
  55. xml_declaration : [
  56. {include : "attributes"},
  57. {include : "instruction"}
  58. ],
  59. instruction : [
  60. {token : "punctuation.instruction.end", regex : "\\?>", next : "start"}
  61. ],
  62. doctype : [
  63. {include : "space"},
  64. {include : "string"},
  65. {token : "punctuation.doctype.end", regex : ">", next : "start"},
  66. {token : "xml-pe", regex : "[-_a-zA-Z0-9:]+"},
  67. {token : "punctuation.begin", regex : "\\[", push : "declarations"}
  68. ],
  69. declarations : [{
  70. token : "text",
  71. regex : "\\s+"
  72. }, {
  73. token: "punctuation.end",
  74. regex: "]",
  75. next: "pop"
  76. }, {
  77. token : ["punctuation.begin", "keyword"],
  78. regex : "(<\\!)([-_a-zA-Z0-9]+)",
  79. push : [{
  80. token : "text",
  81. regex : "\\s+"
  82. },
  83. {
  84. token : "punctuation.end",
  85. regex : ">",
  86. next : "pop"
  87. },
  88. {include : "string"}]
  89. }],
  90. cdata : [
  91. {token : "string.end", regex : "\\]\\]>", next : "start"},
  92. {token : "text", regex : "\\s+"},
  93. {token : "text", regex : "(?:[^\\]]|\\](?!\\]>))+"}
  94. ],
  95. comment : [
  96. {token : "comment", regex : "-->", next : "start"},
  97. {defaultToken : "comment"}
  98. ],
  99. tag : [{
  100. token : ["meta.tag.punctuation.begin", "meta.tag.name"],
  101. regex : "(<)((?:[-_a-zA-Z0-9]+:)?[-_a-zA-Z0-9]+)",
  102. next: [
  103. {include : "attributes"},
  104. {token : "meta.tag.punctuation.end", regex : "/?>", next : "start"}
  105. ]
  106. }, {
  107. token : ["meta.tag.punctuation.begin", "meta.tag.name"],
  108. regex : "(</)((?:[-_a-zA-Z0-9]+:)?[-_a-zA-Z0-9]+)",
  109. next: [
  110. {include : "space"},
  111. {token : "meta.tag.punctuation.end", regex : ">", next : "start"}
  112. ]
  113. }],
  114. space : [
  115. {token : "text", regex : "\\s+"}
  116. ],
  117. reference : [{
  118. token : "constant.language.escape",
  119. regex : "(?:&#[0-9]+;)|(?:&#x[0-9a-fA-F]+;)|(?:&[a-zA-Z0-9_:\\.-]+;)"
  120. }, {
  121. token : "invalid.illegal", regex : "&"
  122. }],
  123. string: [{
  124. token : "string",
  125. regex : "'",
  126. push : "qstring_inner"
  127. }, {
  128. token : "string",
  129. regex : '"',
  130. push : "qqstring_inner"
  131. }],
  132. qstring_inner: [
  133. {token : "string", regex: "'", next: "pop"},
  134. {include : "reference"},
  135. {defaultToken : "string"}
  136. ],
  137. qqstring_inner: [
  138. {token : "string", regex: '"', next: "pop"},
  139. {include : "reference"},
  140. {defaultToken : "string"}
  141. ],
  142. attributes: [{
  143. token : "entity.other.attribute-name",
  144. regex : "(?:[-_a-zA-Z0-9]+:)?[-_a-zA-Z0-9]+"
  145. }, {
  146. token : "keyword.operator.separator",
  147. regex : "="
  148. }, {
  149. include : "space"
  150. }, {
  151. include : "string"
  152. }]
  153. };
  154. if (this.constructor === XmlHighlightRules)
  155. this.normalizeRules();
  156. };
  157. (function() {
  158. this.embedTagRules = function(HighlightRules, prefix, tag){
  159. this.$rules.tag.unshift({
  160. token : ["meta.tag.punctuation.begin", "meta.tag.name." + tag],
  161. regex : "(<)(" + tag + ")",
  162. next: [
  163. {include : "space"},
  164. {include : "attributes"},
  165. {token : "meta.tag.punctuation.end", regex : "/?>", next : prefix + "start"}
  166. ]
  167. });
  168. this.$rules[tag + "-end"] = [
  169. {include : "space"},
  170. {token : "meta.tag.punctuation.end", regex : ">", next: "start",
  171. onMatch : function(value, currentState, stack) {
  172. stack.splice(0);
  173. return this.token;
  174. }}
  175. ]
  176. this.embedRules(HighlightRules, prefix, [{
  177. token: ["meta.tag.punctuation.begin", "meta.tag.name." + tag],
  178. regex : "(</)(" + tag + ")",
  179. next: tag + "-end"
  180. }, {
  181. token: "string.begin",
  182. regex : "<\\!\\[CDATA\\["
  183. }, {
  184. token: "string.end",
  185. regex : "\\]\\]>"
  186. }]);
  187. };
  188. }).call(TextHighlightRules.prototype);
  189. oop.inherits(XmlHighlightRules, TextHighlightRules);
  190. exports.XmlHighlightRules = XmlHighlightRules;
  191. });