LaravelTest
142 строки · 4.7 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4/*
5DTD mode
6Ported to CodeMirror by Peter Kroon <plakroon@gmail.com>
7Report bugs/issues here: https://github.com/codemirror/CodeMirror/issues
8GitHub: @peterkroon
9*/
10
11(function(mod) {12if (typeof exports == "object" && typeof module == "object") // CommonJS13mod(require("../../lib/codemirror"));14else if (typeof define == "function" && define.amd) // AMD15define(["../../lib/codemirror"], mod);16else // Plain browser env17mod(CodeMirror);18})(function(CodeMirror) {19"use strict";20
21CodeMirror.defineMode("dtd", function(config) {22var indentUnit = config.indentUnit, type;23function ret(style, tp) {type = tp; return style;}24
25function tokenBase(stream, state) {26var ch = stream.next();27
28if (ch == "<" && stream.eat("!") ) {29if (stream.eatWhile(/[\-]/)) {30state.tokenize = tokenSGMLComment;31return tokenSGMLComment(stream, state);32} else if (stream.eatWhile(/[\w]/)) return ret("keyword", "doindent");33} else if (ch == "<" && stream.eat("?")) { //xml declaration34state.tokenize = inBlock("meta", "?>");35return ret("meta", ch);36} else if (ch == "#" && stream.eatWhile(/[\w]/)) return ret("atom", "tag");37else if (ch == "|") return ret("keyword", "separator");38else if (ch.match(/[\(\)\[\]\-\.,\+\?>]/)) return ret(null, ch);//if(ch === ">") return ret(null, "endtag"); else39else if (ch.match(/[\[\]]/)) return ret("rule", ch);40else if (ch == "\"" || ch == "'") {41state.tokenize = tokenString(ch);42return state.tokenize(stream, state);43} else if (stream.eatWhile(/[a-zA-Z\?\+\d]/)) {44var sc = stream.current();45if( sc.substr(sc.length-1,sc.length).match(/\?|\+/) !== null )stream.backUp(1);46return ret("tag", "tag");47} else if (ch == "%" || ch == "*" ) return ret("number", "number");48else {49stream.eatWhile(/[\w\\\-_%.{,]/);50return ret(null, null);51}52}53
54function tokenSGMLComment(stream, state) {55var dashes = 0, ch;56while ((ch = stream.next()) != null) {57if (dashes >= 2 && ch == ">") {58state.tokenize = tokenBase;59break;60}61dashes = (ch == "-") ? dashes + 1 : 0;62}63return ret("comment", "comment");64}65
66function tokenString(quote) {67return function(stream, state) {68var escaped = false, ch;69while ((ch = stream.next()) != null) {70if (ch == quote && !escaped) {71state.tokenize = tokenBase;72break;73}74escaped = !escaped && ch == "\\";75}76return ret("string", "tag");77};78}79
80function inBlock(style, terminator) {81return function(stream, state) {82while (!stream.eol()) {83if (stream.match(terminator)) {84state.tokenize = tokenBase;85break;86}87stream.next();88}89return style;90};91}92
93return {94startState: function(base) {95return {tokenize: tokenBase,96baseIndent: base || 0,97stack: []};98},99
100token: function(stream, state) {101if (stream.eatSpace()) return null;102var style = state.tokenize(stream, state);103
104var context = state.stack[state.stack.length-1];105if (stream.current() == "[" || type === "doindent" || type == "[") state.stack.push("rule");106else if (type === "endtag") state.stack[state.stack.length-1] = "endtag";107else if (stream.current() == "]" || type == "]" || (type == ">" && context == "rule")) state.stack.pop();108else if (type == "[") state.stack.push("[");109return style;110},111
112indent: function(state, textAfter) {113var n = state.stack.length;114
115if( textAfter.charAt(0) === ']' )n--;116else if(textAfter.substr(textAfter.length-1, textAfter.length) === ">"){117if(textAfter.substr(0,1) === "<") {}118else if( type == "doindent" && textAfter.length > 1 ) {}119else if( type == "doindent")n--;120else if( type == ">" && textAfter.length > 1) {}121else if( type == "tag" && textAfter !== ">") {}122else if( type == "tag" && state.stack[state.stack.length-1] == "rule")n--;123else if( type == "tag")n++;124else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule" && type === ">")n--;125else if( textAfter === ">" && state.stack[state.stack.length-1] == "rule") {}126else if( textAfter.substr(0,1) !== "<" && textAfter.substr(0,1) === ">" )n=n-1;127else if( textAfter === ">") {}128else n=n-1;129//over rule them all130if(type == null || type == "]")n--;131}132
133return state.baseIndent + n * indentUnit;134},135
136electricChars: "]>"137};138});139
140CodeMirror.defineMIME("application/xml-dtd", "dtd");141
142});143