xml.js 4.71 KB
CodeMirror.defineMode("xml",function(t,e){function n(t,e){function n(n){return e.tokenize=n,n(t,e)}var o=t.next();if("<"==o){if(t.eat("!"))return t.eat("[")?t.match("CDATA[")?n(a("atom","]]>")):null:t.match("--")?n(a("comment","-->")):t.match("DOCTYPE",!0,!0)?(t.eatWhile(/[\w\._\-]/),n(i(1))):null;if(t.eat("?"))return t.eatWhile(/[\w\._\-]/),e.tokenize=a("meta","?>"),"meta";w=t.eat("/")?"closeTag":"openTag",t.eatSpace(),b="";for(var l;l=t.eat(/[^\s\u00a0=<>\"\'\/?]/);)b+=l;return e.tokenize=r,"tag"}if("&"==o){var u;return u=t.eat("#")?t.eat("x")?t.eatWhile(/[a-fA-F\d]/)&&t.eat(";"):t.eatWhile(/[\d]/)&&t.eat(";"):t.eatWhile(/[\w\.\-:]/)&&t.eat(";"),u?"atom":"error"}return t.eatWhile(/[^&<]/),null}function r(t,e){var r=t.next();return">"==r||"/"==r&&t.eat(">")?(e.tokenize=n,w=">"==r?"endTag":"selfcloseTag","tag"):"="==r?(w="equals",null):/[\'\"]/.test(r)?(e.tokenize=o(r),e.tokenize(t,e)):(t.eatWhile(/[^\s\u00a0=<>\"\'\/?]/),"word")}function o(t){return function(e,n){for(;!e.eol();)if(e.next()==t){n.tokenize=r;break}return"string"}}function a(t,e){return function(r,o){for(;!r.eol();){if(r.match(e)){o.tokenize=n;break}r.next()}return t}}function i(t){return function(e,r){for(var o;null!=(o=e.next());){if("<"==o)return r.tokenize=i(t+1),r.tokenize(e,r);if(">"==o){if(1==t){r.tokenize=n;break}return r.tokenize=i(t-1),r.tokenize(e,r)}}return"meta"}}function l(){for(var t=arguments.length-1;t>=0;t--)v.cc.push(arguments[t])}function u(){return l.apply(null,arguments),!0}function c(t,e){var n=z.doNotIndent.hasOwnProperty(t)||v.context&&v.context.noIndent;v.context={prev:v.context,tagName:t,indent:v.indented,startOfLine:e,noIndent:n}}function d(){v.context&&(v.context=v.context.prev)}function f(t){if("openTag"==t)return v.tagName=b,u(g,s(v.startOfLine));if("closeTag"==t){var e=!1;return v.context?v.context.tagName!=b&&(z.implicitlyClosed.hasOwnProperty(v.context.tagName.toLowerCase())&&d(),e=!v.context||v.context.tagName!=b):e=!0,e&&(C="error"),u(m(e))}return u()}function s(t){return function(e){return"selfcloseTag"==e||"endTag"==e&&z.autoSelfClosers.hasOwnProperty(v.tagName.toLowerCase())?(p(v.tagName.toLowerCase()),u()):"endTag"==e?(p(v.tagName.toLowerCase()),c(v.tagName,t),u()):u()}}function m(t){return function(e){return t&&(C="error"),"endTag"==e?(d(),u()):(C="error",u(arguments.callee))}}function p(t){for(var e;;){if(!v.context)return;if(e=v.context.tagName.toLowerCase(),!z.contextGrabbers.hasOwnProperty(e)||!z.contextGrabbers[e].hasOwnProperty(t))return;d()}}function g(t){return"word"==t?(C="attribute",u(h,g)):"endTag"==t||"selfcloseTag"==t?l():(C="error",u(g))}function h(t){return"equals"==t?u(x,g):(z.allowMissing||(C="error"),"endTag"==t||"selfcloseTag"==t?l():u())}function x(t){return"string"==t?u(k):"word"==t&&z.allowUnquoted?(C="string",u()):(C="error","endTag"==t||"selfCloseTag"==t?l():u())}function k(t){return"string"==t?u(k):l()}var b,w,v,C,T=t.indentUnit,z=e.htmlMode?{autoSelfClosers:{area:!0,base:!0,br:!0,col:!0,command:!0,embed:!0,frame:!0,hr:!0,img:!0,input:!0,keygen:!0,link:!0,meta:!0,param:!0,source:!0,track:!0,wbr:!0},implicitlyClosed:{dd:!0,li:!0,optgroup:!0,option:!0,p:!0,rp:!0,rt:!0,tbody:!0,td:!0,tfoot:!0,th:!0,tr:!0},contextGrabbers:{dd:{dd:!0,dt:!0},dt:{dd:!0,dt:!0},li:{li:!0},option:{option:!0,optgroup:!0},optgroup:{optgroup:!0},p:{address:!0,article:!0,aside:!0,blockquote:!0,dir:!0,div:!0,dl:!0,fieldset:!0,footer:!0,form:!0,h1:!0,h2:!0,h3:!0,h4:!0,h5:!0,h6:!0,header:!0,hgroup:!0,hr:!0,menu:!0,nav:!0,ol:!0,p:!0,pre:!0,section:!0,table:!0,ul:!0},rp:{rp:!0,rt:!0},rt:{rp:!0,rt:!0},tbody:{tbody:!0,tfoot:!0},td:{td:!0,th:!0},tfoot:{tbody:!0},th:{td:!0,th:!0},thead:{tbody:!0,tfoot:!0},tr:{tr:!0}},doNotIndent:{pre:!0},allowUnquoted:!0,allowMissing:!1}:{autoSelfClosers:{},implicitlyClosed:{},contextGrabbers:{},doNotIndent:{},allowUnquoted:!1,allowMissing:!1},y=e.alignCDATA;return{startState:function(){return{tokenize:n,cc:[],indented:0,startOfLine:!0,tagName:null,context:null}},token:function(t,e){if(t.sol()&&(e.startOfLine=!0,e.indented=t.indentation()),t.eatSpace())return null;C=w=b=null;var n=e.tokenize(t,e);if(e.type=w,(n||w)&&"comment"!=n)for(v=e;;){var r=e.cc.pop()||f;if(r(w||n))break}return e.startOfLine=!1,C||n},indent:function(t,e,o){var a=t.context;if(t.tokenize!=r&&t.tokenize!=n||a&&a.noIndent)return o?o.match(/^(\s*)/)[0].length:0;if(y&&/<!\[CDATA\[/.test(e))return 0;for(a&&/^<\//.test(e)&&(a=a.prev);a&&!a.startOfLine;)a=a.prev;return a?a.indent+T:0},compareStates:function(t,e){if(t.indented!=e.indented||t.tokenize!=e.tokenize)return!1;for(var n=t.context,r=e.context;;n=n.prev,r=r.prev){if(!n||!r)return n==r;if(n.tagName!=r.tagName)return!1}},electricChars:"/"}}),CodeMirror.defineMIME("application/xml","xml"),CodeMirror.mimeModes.hasOwnProperty("text/html")||CodeMirror.defineMIME("text/html",{name:"xml",htmlMode:!0});