Mercurial
comparison .cms/lib/codemirror/mode/htmlmixed/htmlmixed.js @ 0:78edf6b517a0 draft
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Fri, 11 Oct 2024 22:40:23 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78edf6b517a0 |
---|---|
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | |
2 // Distributed under an MIT license: https://codemirror.net/5/LICENSE | |
3 | |
4 (function(mod) { | |
5 if (typeof exports == "object" && typeof module == "object") // CommonJS | |
6 mod(require("../../lib/codemirror"), require("../xml/xml"), require("../javascript/javascript"), require("../css/css")); | |
7 else if (typeof define == "function" && define.amd) // AMD | |
8 define(["../../lib/codemirror", "../xml/xml", "../javascript/javascript", "../css/css"], mod); | |
9 else // Plain browser env | |
10 mod(CodeMirror); | |
11 })(function(CodeMirror) { | |
12 "use strict"; | |
13 | |
14 var defaultTags = { | |
15 script: [ | |
16 ["lang", /(javascript|babel)/i, "javascript"], | |
17 ["type", /^(?:text|application)\/(?:x-)?(?:java|ecma)script$|^module$|^$/i, "javascript"], | |
18 ["type", /./, "text/plain"], | |
19 [null, null, "javascript"] | |
20 ], | |
21 style: [ | |
22 ["lang", /^css$/i, "css"], | |
23 ["type", /^(text\/)?(x-)?(stylesheet|css)$/i, "css"], | |
24 ["type", /./, "text/plain"], | |
25 [null, null, "css"] | |
26 ] | |
27 }; | |
28 | |
29 function maybeBackup(stream, pat, style) { | |
30 var cur = stream.current(), close = cur.search(pat); | |
31 if (close > -1) { | |
32 stream.backUp(cur.length - close); | |
33 } else if (cur.match(/<\/?$/)) { | |
34 stream.backUp(cur.length); | |
35 if (!stream.match(pat, false)) stream.match(cur); | |
36 } | |
37 return style; | |
38 } | |
39 | |
40 var attrRegexpCache = {}; | |
41 function getAttrRegexp(attr) { | |
42 var regexp = attrRegexpCache[attr]; | |
43 if (regexp) return regexp; | |
44 return attrRegexpCache[attr] = new RegExp("\\s+" + attr + "\\s*=\\s*('|\")?([^'\"]+)('|\")?\\s*"); | |
45 } | |
46 | |
47 function getAttrValue(text, attr) { | |
48 var match = text.match(getAttrRegexp(attr)) | |
49 return match ? /^\s*(.*?)\s*$/.exec(match[2])[1] : "" | |
50 } | |
51 | |
52 function getTagRegexp(tagName, anchored) { | |
53 return new RegExp((anchored ? "^" : "") + "<\/\\s*" + tagName + "\\s*>", "i"); | |
54 } | |
55 | |
56 function addTags(from, to) { | |
57 for (var tag in from) { | |
58 var dest = to[tag] || (to[tag] = []); | |
59 var source = from[tag]; | |
60 for (var i = source.length - 1; i >= 0; i--) | |
61 dest.unshift(source[i]) | |
62 } | |
63 } | |
64 | |
65 function findMatchingMode(tagInfo, tagText) { | |
66 for (var i = 0; i < tagInfo.length; i++) { | |
67 var spec = tagInfo[i]; | |
68 if (!spec[0] || spec[1].test(getAttrValue(tagText, spec[0]))) return spec[2]; | |
69 } | |
70 } | |
71 | |
72 CodeMirror.defineMode("htmlmixed", function (config, parserConfig) { | |
73 var htmlMode = CodeMirror.getMode(config, { | |
74 name: "xml", | |
75 htmlMode: true, | |
76 multilineTagIndentFactor: parserConfig.multilineTagIndentFactor, | |
77 multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag, | |
78 allowMissingTagName: parserConfig.allowMissingTagName, | |
79 }); | |
80 | |
81 var tags = {}; | |
82 var configTags = parserConfig && parserConfig.tags, configScript = parserConfig && parserConfig.scriptTypes; | |
83 addTags(defaultTags, tags); | |
84 if (configTags) addTags(configTags, tags); | |
85 if (configScript) for (var i = configScript.length - 1; i >= 0; i--) | |
86 tags.script.unshift(["type", configScript[i].matches, configScript[i].mode]) | |
87 | |
88 function html(stream, state) { | |
89 var style = htmlMode.token(stream, state.htmlState), tag = /\btag\b/.test(style), tagName | |
90 if (tag && !/[<>\s\/]/.test(stream.current()) && | |
91 (tagName = state.htmlState.tagName && state.htmlState.tagName.toLowerCase()) && | |
92 tags.hasOwnProperty(tagName)) { | |
93 state.inTag = tagName + " " | |
94 } else if (state.inTag && tag && />$/.test(stream.current())) { | |
95 var inTag = /^([\S]+) (.*)/.exec(state.inTag) | |
96 state.inTag = null | |
97 var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]], inTag[2]) | |
98 var mode = CodeMirror.getMode(config, modeSpec) | |
99 var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[1], false); | |
100 state.token = function (stream, state) { | |
101 if (stream.match(endTagA, false)) { | |
102 state.token = html; | |
103 state.localState = state.localMode = null; | |
104 return null; | |
105 } | |
106 return maybeBackup(stream, endTag, state.localMode.token(stream, state.localState)); | |
107 }; | |
108 state.localMode = mode; | |
109 state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, "", "")); | |
110 } else if (state.inTag) { | |
111 state.inTag += stream.current() | |
112 if (stream.eol()) state.inTag += " " | |
113 } | |
114 return style; | |
115 }; | |
116 | |
117 return { | |
118 startState: function () { | |
119 var state = CodeMirror.startState(htmlMode); | |
120 return {token: html, inTag: null, localMode: null, localState: null, htmlState: state}; | |
121 }, | |
122 | |
123 copyState: function (state) { | |
124 var local; | |
125 if (state.localState) { | |
126 local = CodeMirror.copyState(state.localMode, state.localState); | |
127 } | |
128 return {token: state.token, inTag: state.inTag, | |
129 localMode: state.localMode, localState: local, | |
130 htmlState: CodeMirror.copyState(htmlMode, state.htmlState)}; | |
131 }, | |
132 | |
133 token: function (stream, state) { | |
134 return state.token(stream, state); | |
135 }, | |
136 | |
137 indent: function (state, textAfter, line) { | |
138 if (!state.localMode || /^\s*<\//.test(textAfter)) | |
139 return htmlMode.indent(state.htmlState, textAfter, line); | |
140 else if (state.localMode.indent) | |
141 return state.localMode.indent(state.localState, textAfter, line); | |
142 else | |
143 return CodeMirror.Pass; | |
144 }, | |
145 | |
146 innerMode: function (state) { | |
147 return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode}; | |
148 } | |
149 }; | |
150 }, "xml", "javascript", "css"); | |
151 | |
152 CodeMirror.defineMIME("text/html", "htmlmixed"); | |
153 }); |