├── .gitignore ├── src ├── css │ ├── monokai.css │ └── 00-codemirror.css ├── mode │ ├── active-line.js │ ├── htmlmixed.js │ ├── xml.js │ ├── css.js │ └── javascript.js └── index.html └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | -------------------------------------------------------------------------------- /src/css/monokai.css: -------------------------------------------------------------------------------- 1 | /* Based on Sublime Text's Monokai theme */ 2 | 3 | .cm-s-monokai.CodeMirror { background: #272822; color: #f8f8f2; } 4 | .cm-s-monokai div.CodeMirror-selected { background: #49483E; } 5 | .cm-s-monokai .CodeMirror-line::selection, .cm-s-monokai .CodeMirror-line > span::selection, .cm-s-monokai .CodeMirror-line > span > span::selection { background: rgba(73, 72, 62, .99); } 6 | .cm-s-monokai .CodeMirror-line::-moz-selection, .cm-s-monokai .CodeMirror-line > span::-moz-selection, .cm-s-monokai .CodeMirror-line > span > span::-moz-selection { background: rgba(73, 72, 62, .99); } 7 | .cm-s-monokai .CodeMirror-gutters { background: #272822; border-right: 0px; } 8 | .cm-s-monokai .CodeMirror-guttermarker { color: white; } 9 | .cm-s-monokai .CodeMirror-guttermarker-subtle { color: #d0d0d0; } 10 | .cm-s-monokai .CodeMirror-linenumber { color: #d0d0d0; } 11 | .cm-s-monokai .CodeMirror-cursor { border-left: 1px solid #f8f8f0; } 12 | 13 | .cm-s-monokai span.cm-comment { color: #75715e; } 14 | .cm-s-monokai span.cm-atom { color: #ae81ff; } 15 | .cm-s-monokai span.cm-number { color: #ae81ff; } 16 | 17 | .cm-s-monokai span.cm-comment.cm-attribute { color: #97b757; } 18 | .cm-s-monokai span.cm-comment.cm-def { color: #bc9262; } 19 | .cm-s-monokai span.cm-comment.cm-tag { color: #bc6283; } 20 | .cm-s-monokai span.cm-comment.cm-type { color: #5998a6; } 21 | 22 | .cm-s-monokai span.cm-property, .cm-s-monokai span.cm-attribute { color: #a6e22e; } 23 | .cm-s-monokai span.cm-keyword { color: #f92672; } 24 | .cm-s-monokai span.cm-builtin { color: #66d9ef; } 25 | .cm-s-monokai span.cm-string { color: #e6db74; } 26 | 27 | .cm-s-monokai span.cm-variable { color: #f8f8f2; } 28 | .cm-s-monokai span.cm-variable-2 { color: #9effff; } 29 | .cm-s-monokai span.cm-variable-3, .cm-s-monokai span.cm-type { color: #66d9ef; } 30 | .cm-s-monokai span.cm-def { color: #fd971f; } 31 | .cm-s-monokai span.cm-bracket { color: #f8f8f2; } 32 | .cm-s-monokai span.cm-tag { color: #f92672; } 33 | .cm-s-monokai span.cm-header { color: #ae81ff; } 34 | .cm-s-monokai span.cm-link { color: #ae81ff; } 35 | .cm-s-monokai span.cm-error { background: #f92672; color: #f8f8f0; } 36 | 37 | .cm-s-monokai .CodeMirror-activeline-background { background: #373831; } 38 | .cm-s-monokai .CodeMirror-matchingbracket { 39 | text-decoration: underline; 40 | color: white !important; 41 | } 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## About 2 | 3 | A small and lighting fast in-browser HTML editor with syntax highlighting and instant preview for code you type. It is a great tool for HTML prototyping since you do not have to save the file everytime you want to make the changes. On top of that it's only single HTML file and works offline too. 4 | 5 | You can think this app as a diet version of CodePen.io or JsFiddle but you can run it locally. It has two main windows -- left is used for code editor and the right one is used for HTML preview. For syntax highlighting it uses an awesome CodeMirror library. 6 | 7 | HTML Editor support following features. 8 | 9 | - Open file from local disk 10 | - Save file to local disk 11 | - Syntax highlighting 12 | - Word Wrap 13 | - Instant preview window 14 | - Keyboard shortcuts 15 | - Responsive design for mobile 16 | - Works offline 17 | 18 | Demo are available at [https://rioastamal.net/html-editor/](https://rioastamal.net/html-editor/) 19 | 20 | ![Demo HTML Editor](https://s3.amazonaws.com/rioastamal-assets/html-editor/html-editor-demo.gif) 21 | 22 | ## Run 23 | 24 | To run the HTML editor just open file `src/index.html` using your favorite web browser and you're ready to go. 25 | 26 | ## Build 27 | 28 | The build process will generate single HTML file of the editor. It may useful if you want to host or upload the editor to your own server. The build script only uses Bash so it should be easy to run. 29 | 30 | ``` 31 | $ bash build.sh 32 | Build file build/index.html complete. 33 | ``` 34 | 35 | You can try to open file `build/index.html` using web browser. 36 | 37 | ## Changelog 38 | 39 | ### v1.5 40 | 41 | * Bug fixes: Decoding value of `?code=` query string parameter 42 | 43 | ### v1.4 44 | 45 | * New feature: Auto populate contents from `?code=` query string parameter 46 | 47 | ### v1.3 48 | 49 | * Add option to manually trigger preview 50 | 51 | ### v1.2 52 | 53 | * Hide or show for code editor window 54 | * Prompt a file name on SaveAs feature 55 | * Confirm to user before closing or reloading the page 56 | * Add meta tags to the page 57 | * Add keyboard shortcuts 58 | 59 | ### v1.1 60 | 61 | * Bug fixes: Undefined function name removeObjectURL() on saveAsFile() function. 62 | 63 | ### v1.0 64 | 65 | * Initial release of HTML Editor 66 | 67 | ## Author 68 | 69 | This application is written by Rio Astamal \ 70 | 71 | ## License 72 | 73 | This application is open source licensed under [MIT license](http://opensource.org/licenses/MIT). 74 | -------------------------------------------------------------------------------- /src/mode/active-line.js: -------------------------------------------------------------------------------- 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 | // Distributed under an MIT license: https://codemirror.net/LICENSE 3 | 4 | (function(mod) { 5 | if (typeof exports == "object" && typeof module == "object") // CommonJS 6 | mod(require("../../lib/codemirror")); 7 | else if (typeof define == "function" && define.amd) // AMD 8 | define(["../../lib/codemirror"], mod); 9 | else // Plain browser env 10 | mod(CodeMirror); 11 | })(function(CodeMirror) { 12 | "use strict"; 13 | var WRAP_CLASS = "CodeMirror-activeline"; 14 | var BACK_CLASS = "CodeMirror-activeline-background"; 15 | var GUTT_CLASS = "CodeMirror-activeline-gutter"; 16 | 17 | CodeMirror.defineOption("styleActiveLine", false, function(cm, val, old) { 18 | var prev = old == CodeMirror.Init ? false : old; 19 | if (val == prev) return 20 | if (prev) { 21 | cm.off("beforeSelectionChange", selectionChange); 22 | clearActiveLines(cm); 23 | delete cm.state.activeLines; 24 | } 25 | if (val) { 26 | cm.state.activeLines = []; 27 | updateActiveLines(cm, cm.listSelections()); 28 | cm.on("beforeSelectionChange", selectionChange); 29 | } 30 | }); 31 | 32 | function clearActiveLines(cm) { 33 | for (var i = 0; i < cm.state.activeLines.length; i++) { 34 | cm.removeLineClass(cm.state.activeLines[i], "wrap", WRAP_CLASS); 35 | cm.removeLineClass(cm.state.activeLines[i], "background", BACK_CLASS); 36 | cm.removeLineClass(cm.state.activeLines[i], "gutter", GUTT_CLASS); 37 | } 38 | } 39 | 40 | function sameArray(a, b) { 41 | if (a.length != b.length) return false; 42 | for (var i = 0; i < a.length; i++) 43 | if (a[i] != b[i]) return false; 44 | return true; 45 | } 46 | 47 | function updateActiveLines(cm, ranges) { 48 | var active = []; 49 | for (var i = 0; i < ranges.length; i++) { 50 | var range = ranges[i]; 51 | var option = cm.getOption("styleActiveLine"); 52 | if (typeof option == "object" && option.nonEmpty ? range.anchor.line != range.head.line : !range.empty()) 53 | continue 54 | var line = cm.getLineHandleVisualStart(range.head.line); 55 | if (active[active.length - 1] != line) active.push(line); 56 | } 57 | if (sameArray(cm.state.activeLines, active)) return; 58 | cm.operation(function() { 59 | clearActiveLines(cm); 60 | for (var i = 0; i < active.length; i++) { 61 | cm.addLineClass(active[i], "wrap", WRAP_CLASS); 62 | cm.addLineClass(active[i], "background", BACK_CLASS); 63 | cm.addLineClass(active[i], "gutter", GUTT_CLASS); 64 | } 65 | cm.state.activeLines = active; 66 | }); 67 | } 68 | 69 | function selectionChange(cm, sel) { 70 | updateActiveLines(cm, sel.ranges); 71 | } 72 | }); 73 | -------------------------------------------------------------------------------- /src/mode/htmlmixed.js: -------------------------------------------------------------------------------- 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 | // Distributed under an MIT license: https://codemirror.net/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 | }); 79 | 80 | var tags = {}; 81 | var configTags = parserConfig && parserConfig.tags, configScript = parserConfig && parserConfig.scriptTypes; 82 | addTags(defaultTags, tags); 83 | if (configTags) addTags(configTags, tags); 84 | if (configScript) for (var i = configScript.length - 1; i >= 0; i--) 85 | tags.script.unshift(["type", configScript[i].matches, configScript[i].mode]) 86 | 87 | function html(stream, state) { 88 | var style = htmlMode.token(stream, state.htmlState), tag = /\btag\b/.test(style), tagName 89 | if (tag && !/[<>\s\/]/.test(stream.current()) && 90 | (tagName = state.htmlState.tagName && state.htmlState.tagName.toLowerCase()) && 91 | tags.hasOwnProperty(tagName)) { 92 | state.inTag = tagName + " " 93 | } else if (state.inTag && tag && />$/.test(stream.current())) { 94 | var inTag = /^([\S]+) (.*)/.exec(state.inTag) 95 | state.inTag = null 96 | var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]], inTag[2]) 97 | var mode = CodeMirror.getMode(config, modeSpec) 98 | var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[1], false); 99 | state.token = function (stream, state) { 100 | if (stream.match(endTagA, false)) { 101 | state.token = html; 102 | state.localState = state.localMode = null; 103 | return null; 104 | } 105 | return maybeBackup(stream, endTag, state.localMode.token(stream, state.localState)); 106 | }; 107 | state.localMode = mode; 108 | state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, "", "")); 109 | } else if (state.inTag) { 110 | state.inTag += stream.current() 111 | if (stream.eol()) state.inTag += " " 112 | } 113 | return style; 114 | }; 115 | 116 | return { 117 | startState: function () { 118 | var state = CodeMirror.startState(htmlMode); 119 | return {token: html, inTag: null, localMode: null, localState: null, htmlState: state}; 120 | }, 121 | 122 | copyState: function (state) { 123 | var local; 124 | if (state.localState) { 125 | local = CodeMirror.copyState(state.localMode, state.localState); 126 | } 127 | return {token: state.token, inTag: state.inTag, 128 | localMode: state.localMode, localState: local, 129 | htmlState: CodeMirror.copyState(htmlMode, state.htmlState)}; 130 | }, 131 | 132 | token: function (stream, state) { 133 | return state.token(stream, state); 134 | }, 135 | 136 | indent: function (state, textAfter, line) { 137 | if (!state.localMode || /^\s*<\//.test(textAfter)) 138 | return htmlMode.indent(state.htmlState, textAfter, line); 139 | else if (state.localMode.indent) 140 | return state.localMode.indent(state.localState, textAfter, line); 141 | else 142 | return CodeMirror.Pass; 143 | }, 144 | 145 | innerMode: function (state) { 146 | return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode}; 147 | } 148 | }; 149 | }, "xml", "javascript", "css"); 150 | 151 | CodeMirror.defineMIME("text/html", "htmlmixed"); 152 | }); 153 | -------------------------------------------------------------------------------- /src/css/00-codemirror.css: -------------------------------------------------------------------------------- 1 | /* BASICS */ 2 | 3 | .CodeMirror { 4 | /* Set height, width, borders, and global font properties here */ 5 | font-family: monospace; 6 | height: 300px; 7 | color: black; 8 | direction: ltr; 9 | } 10 | 11 | /* PADDING */ 12 | 13 | .CodeMirror-lines { 14 | padding: 4px 0; /* Vertical padding around content */ 15 | } 16 | .CodeMirror pre { 17 | padding: 0 4px; /* Horizontal padding of content */ 18 | } 19 | 20 | .CodeMirror-scrollbar-filler, .CodeMirror-gutter-filler { 21 | background-color: white; /* The little square between H and V scrollbars */ 22 | } 23 | 24 | /* GUTTER */ 25 | 26 | .CodeMirror-gutters { 27 | border-right: 1px solid #ddd; 28 | background-color: #f7f7f7; 29 | white-space: nowrap; 30 | } 31 | .CodeMirror-linenumbers {} 32 | .CodeMirror-linenumber { 33 | padding: 0 3px 0 5px; 34 | min-width: 20px; 35 | text-align: right; 36 | color: #999; 37 | white-space: nowrap; 38 | } 39 | 40 | .CodeMirror-guttermarker { color: black; } 41 | .CodeMirror-guttermarker-subtle { color: #999; } 42 | 43 | /* CURSOR */ 44 | 45 | .CodeMirror-cursor { 46 | border-left: 1px solid black; 47 | border-right: none; 48 | width: 0; 49 | } 50 | /* Shown when moving in bi-directional text */ 51 | .CodeMirror div.CodeMirror-secondarycursor { 52 | border-left: 1px solid silver; 53 | } 54 | .cm-fat-cursor .CodeMirror-cursor { 55 | width: auto; 56 | border: 0 !important; 57 | background: #7e7; 58 | } 59 | .cm-fat-cursor div.CodeMirror-cursors { 60 | z-index: 1; 61 | } 62 | .cm-fat-cursor-mark { 63 | background-color: rgba(20, 255, 20, 0.5); 64 | -webkit-animation: blink 1.06s steps(1) infinite; 65 | -moz-animation: blink 1.06s steps(1) infinite; 66 | animation: blink 1.06s steps(1) infinite; 67 | } 68 | .cm-animate-fat-cursor { 69 | width: auto; 70 | border: 0; 71 | -webkit-animation: blink 1.06s steps(1) infinite; 72 | -moz-animation: blink 1.06s steps(1) infinite; 73 | animation: blink 1.06s steps(1) infinite; 74 | background-color: #7e7; 75 | } 76 | @-moz-keyframes blink { 77 | 0% {} 78 | 50% { background-color: transparent; } 79 | 100% {} 80 | } 81 | @-webkit-keyframes blink { 82 | 0% {} 83 | 50% { background-color: transparent; } 84 | 100% {} 85 | } 86 | @keyframes blink { 87 | 0% {} 88 | 50% { background-color: transparent; } 89 | 100% {} 90 | } 91 | 92 | /* Can style cursor different in overwrite (non-insert) mode */ 93 | .CodeMirror-overwrite .CodeMirror-cursor {} 94 | 95 | .cm-tab { display: inline-block; text-decoration: inherit; } 96 | 97 | .CodeMirror-rulers { 98 | position: absolute; 99 | left: 0; right: 0; top: -50px; bottom: -20px; 100 | overflow: hidden; 101 | } 102 | .CodeMirror-ruler { 103 | border-left: 1px solid #ccc; 104 | top: 0; bottom: 0; 105 | position: absolute; 106 | } 107 | 108 | /* DEFAULT THEME */ 109 | 110 | .cm-s-default .cm-header {color: blue;} 111 | .cm-s-default .cm-quote {color: #090;} 112 | .cm-negative {color: #d44;} 113 | .cm-positive {color: #292;} 114 | .cm-header, .cm-strong {font-weight: bold;} 115 | .cm-em {font-style: italic;} 116 | .cm-link {text-decoration: underline;} 117 | .cm-strikethrough {text-decoration: line-through;} 118 | 119 | .cm-s-default .cm-keyword {color: #708;} 120 | .cm-s-default .cm-atom {color: #219;} 121 | .cm-s-default .cm-number {color: #164;} 122 | .cm-s-default .cm-def {color: #00f;} 123 | .cm-s-default .cm-variable, 124 | .cm-s-default .cm-punctuation, 125 | .cm-s-default .cm-property, 126 | .cm-s-default .cm-operator {} 127 | .cm-s-default .cm-variable-2 {color: #05a;} 128 | .cm-s-default .cm-variable-3, .cm-s-default .cm-type {color: #085;} 129 | .cm-s-default .cm-comment {color: #a50;} 130 | .cm-s-default .cm-string {color: #a11;} 131 | .cm-s-default .cm-string-2 {color: #f50;} 132 | .cm-s-default .cm-meta {color: #555;} 133 | .cm-s-default .cm-qualifier {color: #555;} 134 | .cm-s-default .cm-builtin {color: #30a;} 135 | .cm-s-default .cm-bracket {color: #997;} 136 | .cm-s-default .cm-tag {color: #170;} 137 | .cm-s-default .cm-attribute {color: #00c;} 138 | .cm-s-default .cm-hr {color: #999;} 139 | .cm-s-default .cm-link {color: #00c;} 140 | 141 | .cm-s-default .cm-error {color: #f00;} 142 | .cm-invalidchar {color: #f00;} 143 | 144 | .CodeMirror-composing { border-bottom: 2px solid; } 145 | 146 | /* Default styles for common addons */ 147 | 148 | div.CodeMirror span.CodeMirror-matchingbracket {color: #0b0;} 149 | div.CodeMirror span.CodeMirror-nonmatchingbracket {color: #a22;} 150 | .CodeMirror-matchingtag { background: rgba(255, 150, 0, .3); } 151 | .CodeMirror-activeline-background {background: #e8f2ff;} 152 | 153 | /* STOP */ 154 | 155 | /* The rest of this file contains styles related to the mechanics of 156 | the editor. You probably shouldn't touch them. */ 157 | 158 | .CodeMirror { 159 | position: relative; 160 | overflow: hidden; 161 | background: white; 162 | } 163 | 164 | .CodeMirror-scroll { 165 | overflow: scroll !important; /* Things will break if this is overridden */ 166 | /* 30px is the magic margin used to hide the element's real scrollbars */ 167 | /* See overflow: hidden in .CodeMirror */ 168 | margin-bottom: -30px; margin-right: -30px; 169 | padding-bottom: 30px; 170 | height: 100%; 171 | outline: none; /* Prevent dragging from highlighting the element */ 172 | position: relative; 173 | } 174 | .CodeMirror-sizer { 175 | position: relative; 176 | border-right: 30px solid transparent; 177 | } 178 | 179 | /* The fake, visible scrollbars. Used to force redraw during scrolling 180 | before actual scrolling happens, thus preventing shaking and 181 | flickering artifacts. */ 182 | .CodeMirror-vscrollbar, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-gutter-filler { 183 | position: absolute; 184 | z-index: 6; 185 | display: none; 186 | } 187 | .CodeMirror-vscrollbar { 188 | right: 0; top: 0; 189 | overflow-x: hidden; 190 | overflow-y: scroll; 191 | } 192 | .CodeMirror-hscrollbar { 193 | bottom: 0; left: 0; 194 | overflow-y: hidden; 195 | overflow-x: scroll; 196 | } 197 | .CodeMirror-scrollbar-filler { 198 | right: 0; bottom: 0; 199 | } 200 | .CodeMirror-gutter-filler { 201 | left: 0; bottom: 0; 202 | } 203 | 204 | .CodeMirror-gutters { 205 | position: absolute; left: 0; top: 0; 206 | min-height: 100%; 207 | z-index: 3; 208 | } 209 | .CodeMirror-gutter { 210 | white-space: normal; 211 | height: 100%; 212 | display: inline-block; 213 | vertical-align: top; 214 | margin-bottom: -30px; 215 | } 216 | .CodeMirror-gutter-wrapper { 217 | position: absolute; 218 | z-index: 4; 219 | background: none !important; 220 | border: none !important; 221 | } 222 | .CodeMirror-gutter-background { 223 | position: absolute; 224 | top: 0; bottom: 0; 225 | z-index: 4; 226 | } 227 | .CodeMirror-gutter-elt { 228 | position: absolute; 229 | cursor: default; 230 | z-index: 4; 231 | } 232 | .CodeMirror-gutter-wrapper ::selection { background-color: transparent } 233 | .CodeMirror-gutter-wrapper ::-moz-selection { background-color: transparent } 234 | 235 | .CodeMirror-lines { 236 | cursor: text; 237 | min-height: 1px; /* prevents collapsing before first draw */ 238 | } 239 | .CodeMirror pre { 240 | /* Reset some styles that the rest of the page might have set */ 241 | -moz-border-radius: 0; -webkit-border-radius: 0; border-radius: 0; 242 | border-width: 0; 243 | background: transparent; 244 | font-family: inherit; 245 | font-size: inherit; 246 | margin: 0; 247 | white-space: pre; 248 | word-wrap: normal; 249 | line-height: inherit; 250 | color: inherit; 251 | z-index: 2; 252 | position: relative; 253 | overflow: visible; 254 | -webkit-tap-highlight-color: transparent; 255 | -webkit-font-variant-ligatures: contextual; 256 | font-variant-ligatures: contextual; 257 | } 258 | .CodeMirror-wrap pre { 259 | word-wrap: break-word; 260 | white-space: pre-wrap; 261 | word-break: normal; 262 | } 263 | 264 | .CodeMirror-linebackground { 265 | position: absolute; 266 | left: 0; right: 0; top: 0; bottom: 0; 267 | z-index: 0; 268 | } 269 | 270 | .CodeMirror-linewidget { 271 | position: relative; 272 | z-index: 2; 273 | padding: 0.1px; /* Force widget margins to stay inside of the container */ 274 | } 275 | 276 | .CodeMirror-widget {} 277 | 278 | .CodeMirror-rtl pre { direction: rtl; } 279 | 280 | .CodeMirror-code { 281 | outline: none; 282 | } 283 | 284 | /* Force content-box sizing for the elements where we expect it */ 285 | .CodeMirror-scroll, 286 | .CodeMirror-sizer, 287 | .CodeMirror-gutter, 288 | .CodeMirror-gutters, 289 | .CodeMirror-linenumber { 290 | -moz-box-sizing: content-box; 291 | box-sizing: content-box; 292 | } 293 | 294 | .CodeMirror-measure { 295 | position: absolute; 296 | width: 100%; 297 | height: 0; 298 | overflow: hidden; 299 | visibility: hidden; 300 | } 301 | 302 | .CodeMirror-cursor { 303 | position: absolute; 304 | pointer-events: none; 305 | } 306 | .CodeMirror-measure pre { position: static; } 307 | 308 | div.CodeMirror-cursors { 309 | visibility: hidden; 310 | position: relative; 311 | z-index: 3; 312 | } 313 | div.CodeMirror-dragcursors { 314 | visibility: visible; 315 | } 316 | 317 | .CodeMirror-focused div.CodeMirror-cursors { 318 | visibility: visible; 319 | } 320 | 321 | .CodeMirror-selected { background: #d9d9d9; } 322 | .CodeMirror-focused .CodeMirror-selected { background: #d7d4f0; } 323 | .CodeMirror-crosshair { cursor: crosshair; } 324 | .CodeMirror-line::selection, .CodeMirror-line > span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; } 325 | .CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; } 326 | 327 | .cm-searching { 328 | background-color: #ffa; 329 | background-color: rgba(255, 255, 0, .4); 330 | } 331 | 332 | /* Used to force a border model for a node */ 333 | .cm-force-border { padding-right: .1px; } 334 | 335 | @media print { 336 | /* Hide the cursor when printing */ 337 | .CodeMirror div.CodeMirror-cursors { 338 | visibility: hidden; 339 | } 340 | } 341 | 342 | /* See issue #2901 */ 343 | .cm-tab-wrap-hack:after { content: ''; } 344 | 345 | /* Help users use markselection to safely style text background */ 346 | span.CodeMirror-selectedtext { background: none; } 347 | -------------------------------------------------------------------------------- /src/mode/xml.js: -------------------------------------------------------------------------------- 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 | // Distributed under an MIT license: https://codemirror.net/LICENSE 3 | 4 | (function(mod) { 5 | if (typeof exports == "object" && typeof module == "object") // CommonJS 6 | mod(require("../../lib/codemirror")); 7 | else if (typeof define == "function" && define.amd) // AMD 8 | define(["../../lib/codemirror"], mod); 9 | else // Plain browser env 10 | mod(CodeMirror); 11 | })(function(CodeMirror) { 12 | "use strict"; 13 | 14 | var htmlConfig = { 15 | autoSelfClosers: {'area': true, 'base': true, 'br': true, 'col': true, 'command': true, 16 | 'embed': true, 'frame': true, 'hr': true, 'img': true, 'input': true, 17 | 'keygen': true, 'link': true, 'meta': true, 'param': true, 'source': true, 18 | 'track': true, 'wbr': true, 'menuitem': true}, 19 | implicitlyClosed: {'dd': true, 'li': true, 'optgroup': true, 'option': true, 'p': true, 20 | 'rp': true, 'rt': true, 'tbody': true, 'td': true, 'tfoot': true, 21 | 'th': true, 'tr': true}, 22 | contextGrabbers: { 23 | 'dd': {'dd': true, 'dt': true}, 24 | 'dt': {'dd': true, 'dt': true}, 25 | 'li': {'li': true}, 26 | 'option': {'option': true, 'optgroup': true}, 27 | 'optgroup': {'optgroup': true}, 28 | 'p': {'address': true, 'article': true, 'aside': true, 'blockquote': true, 'dir': true, 29 | 'div': true, 'dl': true, 'fieldset': true, 'footer': true, 'form': true, 30 | 'h1': true, 'h2': true, 'h3': true, 'h4': true, 'h5': true, 'h6': true, 31 | 'header': true, 'hgroup': true, 'hr': true, 'menu': true, 'nav': true, 'ol': true, 32 | 'p': true, 'pre': true, 'section': true, 'table': true, 'ul': true}, 33 | 'rp': {'rp': true, 'rt': true}, 34 | 'rt': {'rp': true, 'rt': true}, 35 | 'tbody': {'tbody': true, 'tfoot': true}, 36 | 'td': {'td': true, 'th': true}, 37 | 'tfoot': {'tbody': true}, 38 | 'th': {'td': true, 'th': true}, 39 | 'thead': {'tbody': true, 'tfoot': true}, 40 | 'tr': {'tr': true} 41 | }, 42 | doNotIndent: {"pre": true}, 43 | allowUnquoted: true, 44 | allowMissing: true, 45 | caseFold: true 46 | } 47 | 48 | var xmlConfig = { 49 | autoSelfClosers: {}, 50 | implicitlyClosed: {}, 51 | contextGrabbers: {}, 52 | doNotIndent: {}, 53 | allowUnquoted: false, 54 | allowMissing: false, 55 | allowMissingTagName: false, 56 | caseFold: false 57 | } 58 | 59 | CodeMirror.defineMode("xml", function(editorConf, config_) { 60 | var indentUnit = editorConf.indentUnit 61 | var config = {} 62 | var defaults = config_.htmlMode ? htmlConfig : xmlConfig 63 | for (var prop in defaults) config[prop] = defaults[prop] 64 | for (var prop in config_) config[prop] = config_[prop] 65 | 66 | // Return variables for tokenizers 67 | var type, setStyle; 68 | 69 | function inText(stream, state) { 70 | function chain(parser) { 71 | state.tokenize = parser; 72 | return parser(stream, state); 73 | } 74 | 75 | var ch = stream.next(); 76 | if (ch == "<") { 77 | if (stream.eat("!")) { 78 | if (stream.eat("[")) { 79 | if (stream.match("CDATA[")) return chain(inBlock("atom", "]]>")); 80 | else return null; 81 | } else if (stream.match("--")) { 82 | return chain(inBlock("comment", "-->")); 83 | } else if (stream.match("DOCTYPE", true, true)) { 84 | stream.eatWhile(/[\w\._\-]/); 85 | return chain(doctype(1)); 86 | } else { 87 | return null; 88 | } 89 | } else if (stream.eat("?")) { 90 | stream.eatWhile(/[\w\._\-]/); 91 | state.tokenize = inBlock("meta", "?>"); 92 | return "meta"; 93 | } else { 94 | type = stream.eat("/") ? "closeTag" : "openTag"; 95 | state.tokenize = inTag; 96 | return "tag bracket"; 97 | } 98 | } else if (ch == "&") { 99 | var ok; 100 | if (stream.eat("#")) { 101 | if (stream.eat("x")) { 102 | ok = stream.eatWhile(/[a-fA-F\d]/) && stream.eat(";"); 103 | } else { 104 | ok = stream.eatWhile(/[\d]/) && stream.eat(";"); 105 | } 106 | } else { 107 | ok = stream.eatWhile(/[\w\.\-:]/) && stream.eat(";"); 108 | } 109 | return ok ? "atom" : "error"; 110 | } else { 111 | stream.eatWhile(/[^&<]/); 112 | return null; 113 | } 114 | } 115 | inText.isInText = true; 116 | 117 | function inTag(stream, state) { 118 | var ch = stream.next(); 119 | if (ch == ">" || (ch == "/" && stream.eat(">"))) { 120 | state.tokenize = inText; 121 | type = ch == ">" ? "endTag" : "selfcloseTag"; 122 | return "tag bracket"; 123 | } else if (ch == "=") { 124 | type = "equals"; 125 | return null; 126 | } else if (ch == "<") { 127 | state.tokenize = inText; 128 | state.state = baseState; 129 | state.tagName = state.tagStart = null; 130 | var next = state.tokenize(stream, state); 131 | return next ? next + " tag error" : "tag error"; 132 | } else if (/[\'\"]/.test(ch)) { 133 | state.tokenize = inAttribute(ch); 134 | state.stringStartCol = stream.column(); 135 | return state.tokenize(stream, state); 136 | } else { 137 | stream.match(/^[^\s\u00a0=<>\"\']*[^\s\u00a0=<>\"\'\/]/); 138 | return "word"; 139 | } 140 | } 141 | 142 | function inAttribute(quote) { 143 | var closure = function(stream, state) { 144 | while (!stream.eol()) { 145 | if (stream.next() == quote) { 146 | state.tokenize = inTag; 147 | break; 148 | } 149 | } 150 | return "string"; 151 | }; 152 | closure.isInAttribute = true; 153 | return closure; 154 | } 155 | 156 | function inBlock(style, terminator) { 157 | return function(stream, state) { 158 | while (!stream.eol()) { 159 | if (stream.match(terminator)) { 160 | state.tokenize = inText; 161 | break; 162 | } 163 | stream.next(); 164 | } 165 | return style; 166 | } 167 | } 168 | 169 | function doctype(depth) { 170 | return function(stream, state) { 171 | var ch; 172 | while ((ch = stream.next()) != null) { 173 | if (ch == "<") { 174 | state.tokenize = doctype(depth + 1); 175 | return state.tokenize(stream, state); 176 | } else if (ch == ">") { 177 | if (depth == 1) { 178 | state.tokenize = inText; 179 | break; 180 | } else { 181 | state.tokenize = doctype(depth - 1); 182 | return state.tokenize(stream, state); 183 | } 184 | } 185 | } 186 | return "meta"; 187 | }; 188 | } 189 | 190 | function Context(state, tagName, startOfLine) { 191 | this.prev = state.context; 192 | this.tagName = tagName; 193 | this.indent = state.indented; 194 | this.startOfLine = startOfLine; 195 | if (config.doNotIndent.hasOwnProperty(tagName) || (state.context && state.context.noIndent)) 196 | this.noIndent = true; 197 | } 198 | function popContext(state) { 199 | if (state.context) state.context = state.context.prev; 200 | } 201 | function maybePopContext(state, nextTagName) { 202 | var parentTagName; 203 | while (true) { 204 | if (!state.context) { 205 | return; 206 | } 207 | parentTagName = state.context.tagName; 208 | if (!config.contextGrabbers.hasOwnProperty(parentTagName) || 209 | !config.contextGrabbers[parentTagName].hasOwnProperty(nextTagName)) { 210 | return; 211 | } 212 | popContext(state); 213 | } 214 | } 215 | 216 | function baseState(type, stream, state) { 217 | if (type == "openTag") { 218 | state.tagStart = stream.column(); 219 | return tagNameState; 220 | } else if (type == "closeTag") { 221 | return closeTagNameState; 222 | } else { 223 | return baseState; 224 | } 225 | } 226 | function tagNameState(type, stream, state) { 227 | if (type == "word") { 228 | state.tagName = stream.current(); 229 | setStyle = "tag"; 230 | return attrState; 231 | } else if (config.allowMissingTagName && type == "endTag") { 232 | setStyle = "tag bracket"; 233 | return attrState(type, stream, state); 234 | } else { 235 | setStyle = "error"; 236 | return tagNameState; 237 | } 238 | } 239 | function closeTagNameState(type, stream, state) { 240 | if (type == "word") { 241 | var tagName = stream.current(); 242 | if (state.context && state.context.tagName != tagName && 243 | config.implicitlyClosed.hasOwnProperty(state.context.tagName)) 244 | popContext(state); 245 | if ((state.context && state.context.tagName == tagName) || config.matchClosing === false) { 246 | setStyle = "tag"; 247 | return closeState; 248 | } else { 249 | setStyle = "tag error"; 250 | return closeStateErr; 251 | } 252 | } else if (config.allowMissingTagName && type == "endTag") { 253 | setStyle = "tag bracket"; 254 | return closeState(type, stream, state); 255 | } else { 256 | setStyle = "error"; 257 | return closeStateErr; 258 | } 259 | } 260 | 261 | function closeState(type, _stream, state) { 262 | if (type != "endTag") { 263 | setStyle = "error"; 264 | return closeState; 265 | } 266 | popContext(state); 267 | return baseState; 268 | } 269 | function closeStateErr(type, stream, state) { 270 | setStyle = "error"; 271 | return closeState(type, stream, state); 272 | } 273 | 274 | function attrState(type, _stream, state) { 275 | if (type == "word") { 276 | setStyle = "attribute"; 277 | return attrEqState; 278 | } else if (type == "endTag" || type == "selfcloseTag") { 279 | var tagName = state.tagName, tagStart = state.tagStart; 280 | state.tagName = state.tagStart = null; 281 | if (type == "selfcloseTag" || 282 | config.autoSelfClosers.hasOwnProperty(tagName)) { 283 | maybePopContext(state, tagName); 284 | } else { 285 | maybePopContext(state, tagName); 286 | state.context = new Context(state, tagName, tagStart == state.indented); 287 | } 288 | return baseState; 289 | } 290 | setStyle = "error"; 291 | return attrState; 292 | } 293 | function attrEqState(type, stream, state) { 294 | if (type == "equals") return attrValueState; 295 | if (!config.allowMissing) setStyle = "error"; 296 | return attrState(type, stream, state); 297 | } 298 | function attrValueState(type, stream, state) { 299 | if (type == "string") return attrContinuedState; 300 | if (type == "word" && config.allowUnquoted) {setStyle = "string"; return attrState;} 301 | setStyle = "error"; 302 | return attrState(type, stream, state); 303 | } 304 | function attrContinuedState(type, stream, state) { 305 | if (type == "string") return attrContinuedState; 306 | return attrState(type, stream, state); 307 | } 308 | 309 | return { 310 | startState: function(baseIndent) { 311 | var state = {tokenize: inText, 312 | state: baseState, 313 | indented: baseIndent || 0, 314 | tagName: null, tagStart: null, 315 | context: null} 316 | if (baseIndent != null) state.baseIndent = baseIndent 317 | return state 318 | }, 319 | 320 | token: function(stream, state) { 321 | if (!state.tagName && stream.sol()) 322 | state.indented = stream.indentation(); 323 | 324 | if (stream.eatSpace()) return null; 325 | type = null; 326 | var style = state.tokenize(stream, state); 327 | if ((style || type) && style != "comment") { 328 | setStyle = null; 329 | state.state = state.state(type || style, stream, state); 330 | if (setStyle) 331 | style = setStyle == "error" ? style + " error" : setStyle; 332 | } 333 | return style; 334 | }, 335 | 336 | indent: function(state, textAfter, fullLine) { 337 | var context = state.context; 338 | // Indent multi-line strings (e.g. css). 339 | if (state.tokenize.isInAttribute) { 340 | if (state.tagStart == state.indented) 341 | return state.stringStartCol + 1; 342 | else 343 | return state.indented + indentUnit; 344 | } 345 | if (context && context.noIndent) return CodeMirror.Pass; 346 | if (state.tokenize != inTag && state.tokenize != inText) 347 | return fullLine ? fullLine.match(/^(\s*)/)[0].length : 0; 348 | // Indent the starts of attribute names. 349 | if (state.tagName) { 350 | if (config.multilineTagIndentPastTag !== false) 351 | return state.tagStart + state.tagName.length + 2; 352 | else 353 | return state.tagStart + indentUnit * (config.multilineTagIndentFactor || 1); 354 | } 355 | if (config.alignCDATA && /$/, 384 | blockCommentStart: "", 386 | 387 | configuration: config.htmlMode ? "html" : "xml", 388 | helperType: config.htmlMode ? "html" : "xml", 389 | 390 | skipAttribute: function(state) { 391 | if (state.state == attrValueState) 392 | state.state = attrState 393 | } 394 | }; 395 | }); 396 | 397 | CodeMirror.defineMIME("text/xml", "xml"); 398 | CodeMirror.defineMIME("application/xml", "xml"); 399 | if (!CodeMirror.mimeModes.hasOwnProperty("text/html")) 400 | CodeMirror.defineMIME("text/html", {name: "xml", htmlMode: true}); 401 | 402 | }); 403 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {{OG_TITLE}} 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 174 | 175 | 176 | 177 | 178 | 185 | 186 | 187 | 222 | 223 | 224 |
225 |

HTML Editor

226 | 227 |
228 | 229 | 230 |
231 |

Preview

232 | 233 |
234 | 235 | 236 | 237 | 494 | 495 | -------------------------------------------------------------------------------- /src/mode/css.js: -------------------------------------------------------------------------------- 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 | // Distributed under an MIT license: https://codemirror.net/LICENSE 3 | 4 | (function(mod) { 5 | if (typeof exports == "object" && typeof module == "object") // CommonJS 6 | mod(require("../../lib/codemirror")); 7 | else if (typeof define == "function" && define.amd) // AMD 8 | define(["../../lib/codemirror"], mod); 9 | else // Plain browser env 10 | mod(CodeMirror); 11 | })(function(CodeMirror) { 12 | "use strict"; 13 | 14 | CodeMirror.defineMode("css", function(config, parserConfig) { 15 | var inline = parserConfig.inline 16 | if (!parserConfig.propertyKeywords) parserConfig = CodeMirror.resolveMode("text/css"); 17 | 18 | var indentUnit = config.indentUnit, 19 | tokenHooks = parserConfig.tokenHooks, 20 | documentTypes = parserConfig.documentTypes || {}, 21 | mediaTypes = parserConfig.mediaTypes || {}, 22 | mediaFeatures = parserConfig.mediaFeatures || {}, 23 | mediaValueKeywords = parserConfig.mediaValueKeywords || {}, 24 | propertyKeywords = parserConfig.propertyKeywords || {}, 25 | nonStandardPropertyKeywords = parserConfig.nonStandardPropertyKeywords || {}, 26 | fontProperties = parserConfig.fontProperties || {}, 27 | counterDescriptors = parserConfig.counterDescriptors || {}, 28 | colorKeywords = parserConfig.colorKeywords || {}, 29 | valueKeywords = parserConfig.valueKeywords || {}, 30 | allowNested = parserConfig.allowNested, 31 | lineComment = parserConfig.lineComment, 32 | supportsAtComponent = parserConfig.supportsAtComponent === true; 33 | 34 | var type, override; 35 | function ret(style, tp) { type = tp; return style; } 36 | 37 | // Tokenizers 38 | 39 | function tokenBase(stream, state) { 40 | var ch = stream.next(); 41 | if (tokenHooks[ch]) { 42 | var result = tokenHooks[ch](stream, state); 43 | if (result !== false) return result; 44 | } 45 | if (ch == "@") { 46 | stream.eatWhile(/[\w\\\-]/); 47 | return ret("def", stream.current()); 48 | } else if (ch == "=" || (ch == "~" || ch == "|") && stream.eat("=")) { 49 | return ret(null, "compare"); 50 | } else if (ch == "\"" || ch == "'") { 51 | state.tokenize = tokenString(ch); 52 | return state.tokenize(stream, state); 53 | } else if (ch == "#") { 54 | stream.eatWhile(/[\w\\\-]/); 55 | return ret("atom", "hash"); 56 | } else if (ch == "!") { 57 | stream.match(/^\s*\w*/); 58 | return ret("keyword", "important"); 59 | } else if (/\d/.test(ch) || ch == "." && stream.eat(/\d/)) { 60 | stream.eatWhile(/[\w.%]/); 61 | return ret("number", "unit"); 62 | } else if (ch === "-") { 63 | if (/[\d.]/.test(stream.peek())) { 64 | stream.eatWhile(/[\w.%]/); 65 | return ret("number", "unit"); 66 | } else if (stream.match(/^-[\w\\\-]*/)) { 67 | stream.eatWhile(/[\w\\\-]/); 68 | if (stream.match(/^\s*:/, false)) 69 | return ret("variable-2", "variable-definition"); 70 | return ret("variable-2", "variable"); 71 | } else if (stream.match(/^\w+-/)) { 72 | return ret("meta", "meta"); 73 | } 74 | } else if (/[,+>*\/]/.test(ch)) { 75 | return ret(null, "select-op"); 76 | } else if (ch == "." && stream.match(/^-?[_a-z][_a-z0-9-]*/i)) { 77 | return ret("qualifier", "qualifier"); 78 | } else if (/[:;{}\[\]\(\)]/.test(ch)) { 79 | return ret(null, ch); 80 | } else if (stream.match(/[\w-.]+(?=\()/)) { 81 | if (/^(url(-prefix)?|domain|regexp)$/.test(stream.current().toLowerCase())) { 82 | state.tokenize = tokenParenthesized; 83 | } 84 | return ret("variable callee", "variable"); 85 | } else if (/[\w\\\-]/.test(ch)) { 86 | stream.eatWhile(/[\w\\\-]/); 87 | return ret("property", "word"); 88 | } else { 89 | return ret(null, null); 90 | } 91 | } 92 | 93 | function tokenString(quote) { 94 | return function(stream, state) { 95 | var escaped = false, ch; 96 | while ((ch = stream.next()) != null) { 97 | if (ch == quote && !escaped) { 98 | if (quote == ")") stream.backUp(1); 99 | break; 100 | } 101 | escaped = !escaped && ch == "\\"; 102 | } 103 | if (ch == quote || !escaped && quote != ")") state.tokenize = null; 104 | return ret("string", "string"); 105 | }; 106 | } 107 | 108 | function tokenParenthesized(stream, state) { 109 | stream.next(); // Must be '(' 110 | if (!stream.match(/\s*[\"\')]/, false)) 111 | state.tokenize = tokenString(")"); 112 | else 113 | state.tokenize = null; 114 | return ret(null, "("); 115 | } 116 | 117 | // Context management 118 | 119 | function Context(type, indent, prev) { 120 | this.type = type; 121 | this.indent = indent; 122 | this.prev = prev; 123 | } 124 | 125 | function pushContext(state, stream, type, indent) { 126 | state.context = new Context(type, stream.indentation() + (indent === false ? 0 : indentUnit), state.context); 127 | return type; 128 | } 129 | 130 | function popContext(state) { 131 | if (state.context.prev) 132 | state.context = state.context.prev; 133 | return state.context.type; 134 | } 135 | 136 | function pass(type, stream, state) { 137 | return states[state.context.type](type, stream, state); 138 | } 139 | function popAndPass(type, stream, state, n) { 140 | for (var i = n || 1; i > 0; i--) 141 | state.context = state.context.prev; 142 | return pass(type, stream, state); 143 | } 144 | 145 | // Parser 146 | 147 | function wordAsValue(stream) { 148 | var word = stream.current().toLowerCase(); 149 | if (valueKeywords.hasOwnProperty(word)) 150 | override = "atom"; 151 | else if (colorKeywords.hasOwnProperty(word)) 152 | override = "keyword"; 153 | else 154 | override = "variable"; 155 | } 156 | 157 | var states = {}; 158 | 159 | states.top = function(type, stream, state) { 160 | if (type == "{") { 161 | return pushContext(state, stream, "block"); 162 | } else if (type == "}" && state.context.prev) { 163 | return popContext(state); 164 | } else if (supportsAtComponent && /@component/i.test(type)) { 165 | return pushContext(state, stream, "atComponentBlock"); 166 | } else if (/^@(-moz-)?document$/i.test(type)) { 167 | return pushContext(state, stream, "documentTypes"); 168 | } else if (/^@(media|supports|(-moz-)?document|import)$/i.test(type)) { 169 | return pushContext(state, stream, "atBlock"); 170 | } else if (/^@(font-face|counter-style)/i.test(type)) { 171 | state.stateArg = type; 172 | return "restricted_atBlock_before"; 173 | } else if (/^@(-(moz|ms|o|webkit)-)?keyframes$/i.test(type)) { 174 | return "keyframes"; 175 | } else if (type && type.charAt(0) == "@") { 176 | return pushContext(state, stream, "at"); 177 | } else if (type == "hash") { 178 | override = "builtin"; 179 | } else if (type == "word") { 180 | override = "tag"; 181 | } else if (type == "variable-definition") { 182 | return "maybeprop"; 183 | } else if (type == "interpolation") { 184 | return pushContext(state, stream, "interpolation"); 185 | } else if (type == ":") { 186 | return "pseudo"; 187 | } else if (allowNested && type == "(") { 188 | return pushContext(state, stream, "parens"); 189 | } 190 | return state.context.type; 191 | }; 192 | 193 | states.block = function(type, stream, state) { 194 | if (type == "word") { 195 | var word = stream.current().toLowerCase(); 196 | if (propertyKeywords.hasOwnProperty(word)) { 197 | override = "property"; 198 | return "maybeprop"; 199 | } else if (nonStandardPropertyKeywords.hasOwnProperty(word)) { 200 | override = "string-2"; 201 | return "maybeprop"; 202 | } else if (allowNested) { 203 | override = stream.match(/^\s*:(?:\s|$)/, false) ? "property" : "tag"; 204 | return "block"; 205 | } else { 206 | override += " error"; 207 | return "maybeprop"; 208 | } 209 | } else if (type == "meta") { 210 | return "block"; 211 | } else if (!allowNested && (type == "hash" || type == "qualifier")) { 212 | override = "error"; 213 | return "block"; 214 | } else { 215 | return states.top(type, stream, state); 216 | } 217 | }; 218 | 219 | states.maybeprop = function(type, stream, state) { 220 | if (type == ":") return pushContext(state, stream, "prop"); 221 | return pass(type, stream, state); 222 | }; 223 | 224 | states.prop = function(type, stream, state) { 225 | if (type == ";") return popContext(state); 226 | if (type == "{" && allowNested) return pushContext(state, stream, "propBlock"); 227 | if (type == "}" || type == "{") return popAndPass(type, stream, state); 228 | if (type == "(") return pushContext(state, stream, "parens"); 229 | 230 | if (type == "hash" && !/^#([0-9a-fA-f]{3,4}|[0-9a-fA-f]{6}|[0-9a-fA-f]{8})$/.test(stream.current())) { 231 | override += " error"; 232 | } else if (type == "word") { 233 | wordAsValue(stream); 234 | } else if (type == "interpolation") { 235 | return pushContext(state, stream, "interpolation"); 236 | } 237 | return "prop"; 238 | }; 239 | 240 | states.propBlock = function(type, _stream, state) { 241 | if (type == "}") return popContext(state); 242 | if (type == "word") { override = "property"; return "maybeprop"; } 243 | return state.context.type; 244 | }; 245 | 246 | states.parens = function(type, stream, state) { 247 | if (type == "{" || type == "}") return popAndPass(type, stream, state); 248 | if (type == ")") return popContext(state); 249 | if (type == "(") return pushContext(state, stream, "parens"); 250 | if (type == "interpolation") return pushContext(state, stream, "interpolation"); 251 | if (type == "word") wordAsValue(stream); 252 | return "parens"; 253 | }; 254 | 255 | states.pseudo = function(type, stream, state) { 256 | if (type == "meta") return "pseudo"; 257 | 258 | if (type == "word") { 259 | override = "variable-3"; 260 | return state.context.type; 261 | } 262 | return pass(type, stream, state); 263 | }; 264 | 265 | states.documentTypes = function(type, stream, state) { 266 | if (type == "word" && documentTypes.hasOwnProperty(stream.current())) { 267 | override = "tag"; 268 | return state.context.type; 269 | } else { 270 | return states.atBlock(type, stream, state); 271 | } 272 | }; 273 | 274 | states.atBlock = function(type, stream, state) { 275 | if (type == "(") return pushContext(state, stream, "atBlock_parens"); 276 | if (type == "}" || type == ";") return popAndPass(type, stream, state); 277 | if (type == "{") return popContext(state) && pushContext(state, stream, allowNested ? "block" : "top"); 278 | 279 | if (type == "interpolation") return pushContext(state, stream, "interpolation"); 280 | 281 | if (type == "word") { 282 | var word = stream.current().toLowerCase(); 283 | if (word == "only" || word == "not" || word == "and" || word == "or") 284 | override = "keyword"; 285 | else if (mediaTypes.hasOwnProperty(word)) 286 | override = "attribute"; 287 | else if (mediaFeatures.hasOwnProperty(word)) 288 | override = "property"; 289 | else if (mediaValueKeywords.hasOwnProperty(word)) 290 | override = "keyword"; 291 | else if (propertyKeywords.hasOwnProperty(word)) 292 | override = "property"; 293 | else if (nonStandardPropertyKeywords.hasOwnProperty(word)) 294 | override = "string-2"; 295 | else if (valueKeywords.hasOwnProperty(word)) 296 | override = "atom"; 297 | else if (colorKeywords.hasOwnProperty(word)) 298 | override = "keyword"; 299 | else 300 | override = "error"; 301 | } 302 | return state.context.type; 303 | }; 304 | 305 | states.atComponentBlock = function(type, stream, state) { 306 | if (type == "}") 307 | return popAndPass(type, stream, state); 308 | if (type == "{") 309 | return popContext(state) && pushContext(state, stream, allowNested ? "block" : "top", false); 310 | if (type == "word") 311 | override = "error"; 312 | return state.context.type; 313 | }; 314 | 315 | states.atBlock_parens = function(type, stream, state) { 316 | if (type == ")") return popContext(state); 317 | if (type == "{" || type == "}") return popAndPass(type, stream, state, 2); 318 | return states.atBlock(type, stream, state); 319 | }; 320 | 321 | states.restricted_atBlock_before = function(type, stream, state) { 322 | if (type == "{") 323 | return pushContext(state, stream, "restricted_atBlock"); 324 | if (type == "word" && state.stateArg == "@counter-style") { 325 | override = "variable"; 326 | return "restricted_atBlock_before"; 327 | } 328 | return pass(type, stream, state); 329 | }; 330 | 331 | states.restricted_atBlock = function(type, stream, state) { 332 | if (type == "}") { 333 | state.stateArg = null; 334 | return popContext(state); 335 | } 336 | if (type == "word") { 337 | if ((state.stateArg == "@font-face" && !fontProperties.hasOwnProperty(stream.current().toLowerCase())) || 338 | (state.stateArg == "@counter-style" && !counterDescriptors.hasOwnProperty(stream.current().toLowerCase()))) 339 | override = "error"; 340 | else 341 | override = "property"; 342 | return "maybeprop"; 343 | } 344 | return "restricted_atBlock"; 345 | }; 346 | 347 | states.keyframes = function(type, stream, state) { 348 | if (type == "word") { override = "variable"; return "keyframes"; } 349 | if (type == "{") return pushContext(state, stream, "top"); 350 | return pass(type, stream, state); 351 | }; 352 | 353 | states.at = function(type, stream, state) { 354 | if (type == ";") return popContext(state); 355 | if (type == "{" || type == "}") return popAndPass(type, stream, state); 356 | if (type == "word") override = "tag"; 357 | else if (type == "hash") override = "builtin"; 358 | return "at"; 359 | }; 360 | 361 | states.interpolation = function(type, stream, state) { 362 | if (type == "}") return popContext(state); 363 | if (type == "{" || type == ";") return popAndPass(type, stream, state); 364 | if (type == "word") override = "variable"; 365 | else if (type != "variable" && type != "(" && type != ")") override = "error"; 366 | return "interpolation"; 367 | }; 368 | 369 | return { 370 | startState: function(base) { 371 | return {tokenize: null, 372 | state: inline ? "block" : "top", 373 | stateArg: null, 374 | context: new Context(inline ? "block" : "top", base || 0, null)}; 375 | }, 376 | 377 | token: function(stream, state) { 378 | if (!state.tokenize && stream.eatSpace()) return null; 379 | var style = (state.tokenize || tokenBase)(stream, state); 380 | if (style && typeof style == "object") { 381 | type = style[1]; 382 | style = style[0]; 383 | } 384 | override = style; 385 | if (type != "comment") 386 | state.state = states[state.state](type, stream, state); 387 | return override; 388 | }, 389 | 390 | indent: function(state, textAfter) { 391 | var cx = state.context, ch = textAfter && textAfter.charAt(0); 392 | var indent = cx.indent; 393 | if (cx.type == "prop" && (ch == "}" || ch == ")")) cx = cx.prev; 394 | if (cx.prev) { 395 | if (ch == "}" && (cx.type == "block" || cx.type == "top" || 396 | cx.type == "interpolation" || cx.type == "restricted_atBlock")) { 397 | // Resume indentation from parent context. 398 | cx = cx.prev; 399 | indent = cx.indent; 400 | } else if (ch == ")" && (cx.type == "parens" || cx.type == "atBlock_parens") || 401 | ch == "{" && (cx.type == "at" || cx.type == "atBlock")) { 402 | // Dedent relative to current context. 403 | indent = Math.max(0, cx.indent - indentUnit); 404 | } 405 | } 406 | return indent; 407 | }, 408 | 409 | electricChars: "}", 410 | blockCommentStart: "/*", 411 | blockCommentEnd: "*/", 412 | blockCommentContinue: " * ", 413 | lineComment: lineComment, 414 | fold: "brace" 415 | }; 416 | }); 417 | 418 | function keySet(array) { 419 | var keys = {}; 420 | for (var i = 0; i < array.length; ++i) { 421 | keys[array[i].toLowerCase()] = true; 422 | } 423 | return keys; 424 | } 425 | 426 | var documentTypes_ = [ 427 | "domain", "regexp", "url", "url-prefix" 428 | ], documentTypes = keySet(documentTypes_); 429 | 430 | var mediaTypes_ = [ 431 | "all", "aural", "braille", "handheld", "print", "projection", "screen", 432 | "tty", "tv", "embossed" 433 | ], mediaTypes = keySet(mediaTypes_); 434 | 435 | var mediaFeatures_ = [ 436 | "width", "min-width", "max-width", "height", "min-height", "max-height", 437 | "device-width", "min-device-width", "max-device-width", "device-height", 438 | "min-device-height", "max-device-height", "aspect-ratio", 439 | "min-aspect-ratio", "max-aspect-ratio", "device-aspect-ratio", 440 | "min-device-aspect-ratio", "max-device-aspect-ratio", "color", "min-color", 441 | "max-color", "color-index", "min-color-index", "max-color-index", 442 | "monochrome", "min-monochrome", "max-monochrome", "resolution", 443 | "min-resolution", "max-resolution", "scan", "grid", "orientation", 444 | "device-pixel-ratio", "min-device-pixel-ratio", "max-device-pixel-ratio", 445 | "pointer", "any-pointer", "hover", "any-hover" 446 | ], mediaFeatures = keySet(mediaFeatures_); 447 | 448 | var mediaValueKeywords_ = [ 449 | "landscape", "portrait", "none", "coarse", "fine", "on-demand", "hover", 450 | "interlace", "progressive" 451 | ], mediaValueKeywords = keySet(mediaValueKeywords_); 452 | 453 | var propertyKeywords_ = [ 454 | "align-content", "align-items", "align-self", "alignment-adjust", 455 | "alignment-baseline", "anchor-point", "animation", "animation-delay", 456 | "animation-direction", "animation-duration", "animation-fill-mode", 457 | "animation-iteration-count", "animation-name", "animation-play-state", 458 | "animation-timing-function", "appearance", "azimuth", "backface-visibility", 459 | "background", "background-attachment", "background-blend-mode", "background-clip", 460 | "background-color", "background-image", "background-origin", "background-position", 461 | "background-repeat", "background-size", "baseline-shift", "binding", 462 | "bleed", "bookmark-label", "bookmark-level", "bookmark-state", 463 | "bookmark-target", "border", "border-bottom", "border-bottom-color", 464 | "border-bottom-left-radius", "border-bottom-right-radius", 465 | "border-bottom-style", "border-bottom-width", "border-collapse", 466 | "border-color", "border-image", "border-image-outset", 467 | "border-image-repeat", "border-image-slice", "border-image-source", 468 | "border-image-width", "border-left", "border-left-color", 469 | "border-left-style", "border-left-width", "border-radius", "border-right", 470 | "border-right-color", "border-right-style", "border-right-width", 471 | "border-spacing", "border-style", "border-top", "border-top-color", 472 | "border-top-left-radius", "border-top-right-radius", "border-top-style", 473 | "border-top-width", "border-width", "bottom", "box-decoration-break", 474 | "box-shadow", "box-sizing", "break-after", "break-before", "break-inside", 475 | "caption-side", "caret-color", "clear", "clip", "color", "color-profile", "column-count", 476 | "column-fill", "column-gap", "column-rule", "column-rule-color", 477 | "column-rule-style", "column-rule-width", "column-span", "column-width", 478 | "columns", "content", "counter-increment", "counter-reset", "crop", "cue", 479 | "cue-after", "cue-before", "cursor", "direction", "display", 480 | "dominant-baseline", "drop-initial-after-adjust", 481 | "drop-initial-after-align", "drop-initial-before-adjust", 482 | "drop-initial-before-align", "drop-initial-size", "drop-initial-value", 483 | "elevation", "empty-cells", "fit", "fit-position", "flex", "flex-basis", 484 | "flex-direction", "flex-flow", "flex-grow", "flex-shrink", "flex-wrap", 485 | "float", "float-offset", "flow-from", "flow-into", "font", "font-feature-settings", 486 | "font-family", "font-kerning", "font-language-override", "font-size", "font-size-adjust", 487 | "font-stretch", "font-style", "font-synthesis", "font-variant", 488 | "font-variant-alternates", "font-variant-caps", "font-variant-east-asian", 489 | "font-variant-ligatures", "font-variant-numeric", "font-variant-position", 490 | "font-weight", "grid", "grid-area", "grid-auto-columns", "grid-auto-flow", 491 | "grid-auto-rows", "grid-column", "grid-column-end", "grid-column-gap", 492 | "grid-column-start", "grid-gap", "grid-row", "grid-row-end", "grid-row-gap", 493 | "grid-row-start", "grid-template", "grid-template-areas", "grid-template-columns", 494 | "grid-template-rows", "hanging-punctuation", "height", "hyphens", 495 | "icon", "image-orientation", "image-rendering", "image-resolution", 496 | "inline-box-align", "justify-content", "justify-items", "justify-self", "left", "letter-spacing", 497 | "line-break", "line-height", "line-stacking", "line-stacking-ruby", 498 | "line-stacking-shift", "line-stacking-strategy", "list-style", 499 | "list-style-image", "list-style-position", "list-style-type", "margin", 500 | "margin-bottom", "margin-left", "margin-right", "margin-top", 501 | "marks", "marquee-direction", "marquee-loop", 502 | "marquee-play-count", "marquee-speed", "marquee-style", "max-height", 503 | "max-width", "min-height", "min-width", "mix-blend-mode", "move-to", "nav-down", "nav-index", 504 | "nav-left", "nav-right", "nav-up", "object-fit", "object-position", 505 | "opacity", "order", "orphans", "outline", 506 | "outline-color", "outline-offset", "outline-style", "outline-width", 507 | "overflow", "overflow-style", "overflow-wrap", "overflow-x", "overflow-y", 508 | "padding", "padding-bottom", "padding-left", "padding-right", "padding-top", 509 | "page", "page-break-after", "page-break-before", "page-break-inside", 510 | "page-policy", "pause", "pause-after", "pause-before", "perspective", 511 | "perspective-origin", "pitch", "pitch-range", "place-content", "place-items", "place-self", "play-during", "position", 512 | "presentation-level", "punctuation-trim", "quotes", "region-break-after", 513 | "region-break-before", "region-break-inside", "region-fragment", 514 | "rendering-intent", "resize", "rest", "rest-after", "rest-before", "richness", 515 | "right", "rotation", "rotation-point", "ruby-align", "ruby-overhang", 516 | "ruby-position", "ruby-span", "shape-image-threshold", "shape-inside", "shape-margin", 517 | "shape-outside", "size", "speak", "speak-as", "speak-header", 518 | "speak-numeral", "speak-punctuation", "speech-rate", "stress", "string-set", 519 | "tab-size", "table-layout", "target", "target-name", "target-new", 520 | "target-position", "text-align", "text-align-last", "text-decoration", 521 | "text-decoration-color", "text-decoration-line", "text-decoration-skip", 522 | "text-decoration-style", "text-emphasis", "text-emphasis-color", 523 | "text-emphasis-position", "text-emphasis-style", "text-height", 524 | "text-indent", "text-justify", "text-outline", "text-overflow", "text-shadow", 525 | "text-size-adjust", "text-space-collapse", "text-transform", "text-underline-position", 526 | "text-wrap", "top", "transform", "transform-origin", "transform-style", 527 | "transition", "transition-delay", "transition-duration", 528 | "transition-property", "transition-timing-function", "unicode-bidi", 529 | "user-select", "vertical-align", "visibility", "voice-balance", "voice-duration", 530 | "voice-family", "voice-pitch", "voice-range", "voice-rate", "voice-stress", 531 | "voice-volume", "volume", "white-space", "widows", "width", "will-change", "word-break", 532 | "word-spacing", "word-wrap", "z-index", 533 | // SVG-specific 534 | "clip-path", "clip-rule", "mask", "enable-background", "filter", "flood-color", 535 | "flood-opacity", "lighting-color", "stop-color", "stop-opacity", "pointer-events", 536 | "color-interpolation", "color-interpolation-filters", 537 | "color-rendering", "fill", "fill-opacity", "fill-rule", "image-rendering", 538 | "marker", "marker-end", "marker-mid", "marker-start", "shape-rendering", "stroke", 539 | "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", 540 | "stroke-miterlimit", "stroke-opacity", "stroke-width", "text-rendering", 541 | "baseline-shift", "dominant-baseline", "glyph-orientation-horizontal", 542 | "glyph-orientation-vertical", "text-anchor", "writing-mode" 543 | ], propertyKeywords = keySet(propertyKeywords_); 544 | 545 | var nonStandardPropertyKeywords_ = [ 546 | "scrollbar-arrow-color", "scrollbar-base-color", "scrollbar-dark-shadow-color", 547 | "scrollbar-face-color", "scrollbar-highlight-color", "scrollbar-shadow-color", 548 | "scrollbar-3d-light-color", "scrollbar-track-color", "shape-inside", 549 | "searchfield-cancel-button", "searchfield-decoration", "searchfield-results-button", 550 | "searchfield-results-decoration", "zoom" 551 | ], nonStandardPropertyKeywords = keySet(nonStandardPropertyKeywords_); 552 | 553 | var fontProperties_ = [ 554 | "font-family", "src", "unicode-range", "font-variant", "font-feature-settings", 555 | "font-stretch", "font-weight", "font-style" 556 | ], fontProperties = keySet(fontProperties_); 557 | 558 | var counterDescriptors_ = [ 559 | "additive-symbols", "fallback", "negative", "pad", "prefix", "range", 560 | "speak-as", "suffix", "symbols", "system" 561 | ], counterDescriptors = keySet(counterDescriptors_); 562 | 563 | var colorKeywords_ = [ 564 | "aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", 565 | "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown", 566 | "burlywood", "cadetblue", "chartreuse", "chocolate", "coral", "cornflowerblue", 567 | "cornsilk", "crimson", "cyan", "darkblue", "darkcyan", "darkgoldenrod", 568 | "darkgray", "darkgreen", "darkkhaki", "darkmagenta", "darkolivegreen", 569 | "darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", 570 | "darkslateblue", "darkslategray", "darkturquoise", "darkviolet", 571 | "deeppink", "deepskyblue", "dimgray", "dodgerblue", "firebrick", 572 | "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite", 573 | "gold", "goldenrod", "gray", "grey", "green", "greenyellow", "honeydew", 574 | "hotpink", "indianred", "indigo", "ivory", "khaki", "lavender", 575 | "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral", 576 | "lightcyan", "lightgoldenrodyellow", "lightgray", "lightgreen", "lightpink", 577 | "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray", 578 | "lightsteelblue", "lightyellow", "lime", "limegreen", "linen", "magenta", 579 | "maroon", "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", 580 | "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", 581 | "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin", 582 | "navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange", "orangered", 583 | "orchid", "palegoldenrod", "palegreen", "paleturquoise", "palevioletred", 584 | "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", 585 | "purple", "rebeccapurple", "red", "rosybrown", "royalblue", "saddlebrown", 586 | "salmon", "sandybrown", "seagreen", "seashell", "sienna", "silver", "skyblue", 587 | "slateblue", "slategray", "snow", "springgreen", "steelblue", "tan", 588 | "teal", "thistle", "tomato", "turquoise", "violet", "wheat", "white", 589 | "whitesmoke", "yellow", "yellowgreen" 590 | ], colorKeywords = keySet(colorKeywords_); 591 | 592 | var valueKeywords_ = [ 593 | "above", "absolute", "activeborder", "additive", "activecaption", "afar", 594 | "after-white-space", "ahead", "alias", "all", "all-scroll", "alphabetic", "alternate", 595 | "always", "amharic", "amharic-abegede", "antialiased", "appworkspace", 596 | "arabic-indic", "armenian", "asterisks", "attr", "auto", "auto-flow", "avoid", "avoid-column", "avoid-page", 597 | "avoid-region", "background", "backwards", "baseline", "below", "bidi-override", "binary", 598 | "bengali", "blink", "block", "block-axis", "bold", "bolder", "border", "border-box", 599 | "both", "bottom", "break", "break-all", "break-word", "bullets", "button", "button-bevel", 600 | "buttonface", "buttonhighlight", "buttonshadow", "buttontext", "calc", "cambodian", 601 | "capitalize", "caps-lock-indicator", "caption", "captiontext", "caret", 602 | "cell", "center", "checkbox", "circle", "cjk-decimal", "cjk-earthly-branch", 603 | "cjk-heavenly-stem", "cjk-ideographic", "clear", "clip", "close-quote", 604 | "col-resize", "collapse", "color", "color-burn", "color-dodge", "column", "column-reverse", 605 | "compact", "condensed", "contain", "content", "contents", 606 | "content-box", "context-menu", "continuous", "copy", "counter", "counters", "cover", "crop", 607 | "cross", "crosshair", "currentcolor", "cursive", "cyclic", "darken", "dashed", "decimal", 608 | "decimal-leading-zero", "default", "default-button", "dense", "destination-atop", 609 | "destination-in", "destination-out", "destination-over", "devanagari", "difference", 610 | "disc", "discard", "disclosure-closed", "disclosure-open", "document", 611 | "dot-dash", "dot-dot-dash", 612 | "dotted", "double", "down", "e-resize", "ease", "ease-in", "ease-in-out", "ease-out", 613 | "element", "ellipse", "ellipsis", "embed", "end", "ethiopic", "ethiopic-abegede", 614 | "ethiopic-abegede-am-et", "ethiopic-abegede-gez", "ethiopic-abegede-ti-er", 615 | "ethiopic-abegede-ti-et", "ethiopic-halehame-aa-er", 616 | "ethiopic-halehame-aa-et", "ethiopic-halehame-am-et", 617 | "ethiopic-halehame-gez", "ethiopic-halehame-om-et", 618 | "ethiopic-halehame-sid-et", "ethiopic-halehame-so-et", 619 | "ethiopic-halehame-ti-er", "ethiopic-halehame-ti-et", "ethiopic-halehame-tig", 620 | "ethiopic-numeric", "ew-resize", "exclusion", "expanded", "extends", "extra-condensed", 621 | "extra-expanded", "fantasy", "fast", "fill", "fixed", "flat", "flex", "flex-end", "flex-start", "footnotes", 622 | "forwards", "from", "geometricPrecision", "georgian", "graytext", "grid", "groove", 623 | "gujarati", "gurmukhi", "hand", "hangul", "hangul-consonant", "hard-light", "hebrew", 624 | "help", "hidden", "hide", "higher", "highlight", "highlighttext", 625 | "hiragana", "hiragana-iroha", "horizontal", "hsl", "hsla", "hue", "icon", "ignore", 626 | "inactiveborder", "inactivecaption", "inactivecaptiontext", "infinite", 627 | "infobackground", "infotext", "inherit", "initial", "inline", "inline-axis", 628 | "inline-block", "inline-flex", "inline-grid", "inline-table", "inset", "inside", "intrinsic", "invert", 629 | "italic", "japanese-formal", "japanese-informal", "justify", "kannada", 630 | "katakana", "katakana-iroha", "keep-all", "khmer", 631 | "korean-hangul-formal", "korean-hanja-formal", "korean-hanja-informal", 632 | "landscape", "lao", "large", "larger", "left", "level", "lighter", "lighten", 633 | "line-through", "linear", "linear-gradient", "lines", "list-item", "listbox", "listitem", 634 | "local", "logical", "loud", "lower", "lower-alpha", "lower-armenian", 635 | "lower-greek", "lower-hexadecimal", "lower-latin", "lower-norwegian", 636 | "lower-roman", "lowercase", "ltr", "luminosity", "malayalam", "match", "matrix", "matrix3d", 637 | "media-controls-background", "media-current-time-display", 638 | "media-fullscreen-button", "media-mute-button", "media-play-button", 639 | "media-return-to-realtime-button", "media-rewind-button", 640 | "media-seek-back-button", "media-seek-forward-button", "media-slider", 641 | "media-sliderthumb", "media-time-remaining-display", "media-volume-slider", 642 | "media-volume-slider-container", "media-volume-sliderthumb", "medium", 643 | "menu", "menulist", "menulist-button", "menulist-text", 644 | "menulist-textfield", "menutext", "message-box", "middle", "min-intrinsic", 645 | "mix", "mongolian", "monospace", "move", "multiple", "multiply", "myanmar", "n-resize", 646 | "narrower", "ne-resize", "nesw-resize", "no-close-quote", "no-drop", 647 | "no-open-quote", "no-repeat", "none", "normal", "not-allowed", "nowrap", 648 | "ns-resize", "numbers", "numeric", "nw-resize", "nwse-resize", "oblique", "octal", "opacity", "open-quote", 649 | "optimizeLegibility", "optimizeSpeed", "oriya", "oromo", "outset", 650 | "outside", "outside-shape", "overlay", "overline", "padding", "padding-box", 651 | "painted", "page", "paused", "persian", "perspective", "plus-darker", "plus-lighter", 652 | "pointer", "polygon", "portrait", "pre", "pre-line", "pre-wrap", "preserve-3d", 653 | "progress", "push-button", "radial-gradient", "radio", "read-only", 654 | "read-write", "read-write-plaintext-only", "rectangle", "region", 655 | "relative", "repeat", "repeating-linear-gradient", 656 | "repeating-radial-gradient", "repeat-x", "repeat-y", "reset", "reverse", 657 | "rgb", "rgba", "ridge", "right", "rotate", "rotate3d", "rotateX", "rotateY", 658 | "rotateZ", "round", "row", "row-resize", "row-reverse", "rtl", "run-in", "running", 659 | "s-resize", "sans-serif", "saturation", "scale", "scale3d", "scaleX", "scaleY", "scaleZ", "screen", 660 | "scroll", "scrollbar", "scroll-position", "se-resize", "searchfield", 661 | "searchfield-cancel-button", "searchfield-decoration", 662 | "searchfield-results-button", "searchfield-results-decoration", "self-start", "self-end", 663 | "semi-condensed", "semi-expanded", "separate", "serif", "show", "sidama", 664 | "simp-chinese-formal", "simp-chinese-informal", "single", 665 | "skew", "skewX", "skewY", "skip-white-space", "slide", "slider-horizontal", 666 | "slider-vertical", "sliderthumb-horizontal", "sliderthumb-vertical", "slow", 667 | "small", "small-caps", "small-caption", "smaller", "soft-light", "solid", "somali", 668 | "source-atop", "source-in", "source-out", "source-over", "space", "space-around", "space-between", "space-evenly", "spell-out", "square", 669 | "square-button", "start", "static", "status-bar", "stretch", "stroke", "sub", 670 | "subpixel-antialiased", "super", "sw-resize", "symbolic", "symbols", "system-ui", "table", 671 | "table-caption", "table-cell", "table-column", "table-column-group", 672 | "table-footer-group", "table-header-group", "table-row", "table-row-group", 673 | "tamil", 674 | "telugu", "text", "text-bottom", "text-top", "textarea", "textfield", "thai", 675 | "thick", "thin", "threeddarkshadow", "threedface", "threedhighlight", 676 | "threedlightshadow", "threedshadow", "tibetan", "tigre", "tigrinya-er", 677 | "tigrinya-er-abegede", "tigrinya-et", "tigrinya-et-abegede", "to", "top", 678 | "trad-chinese-formal", "trad-chinese-informal", "transform", 679 | "translate", "translate3d", "translateX", "translateY", "translateZ", 680 | "transparent", "ultra-condensed", "ultra-expanded", "underline", "unset", "up", 681 | "upper-alpha", "upper-armenian", "upper-greek", "upper-hexadecimal", 682 | "upper-latin", "upper-norwegian", "upper-roman", "uppercase", "urdu", "url", 683 | "var", "vertical", "vertical-text", "visible", "visibleFill", "visiblePainted", 684 | "visibleStroke", "visual", "w-resize", "wait", "wave", "wider", 685 | "window", "windowframe", "windowtext", "words", "wrap", "wrap-reverse", "x-large", "x-small", "xor", 686 | "xx-large", "xx-small" 687 | ], valueKeywords = keySet(valueKeywords_); 688 | 689 | var allWords = documentTypes_.concat(mediaTypes_).concat(mediaFeatures_).concat(mediaValueKeywords_) 690 | .concat(propertyKeywords_).concat(nonStandardPropertyKeywords_).concat(colorKeywords_) 691 | .concat(valueKeywords_); 692 | CodeMirror.registerHelper("hintWords", "css", allWords); 693 | 694 | function tokenCComment(stream, state) { 695 | var maybeEnd = false, ch; 696 | while ((ch = stream.next()) != null) { 697 | if (maybeEnd && ch == "/") { 698 | state.tokenize = null; 699 | break; 700 | } 701 | maybeEnd = (ch == "*"); 702 | } 703 | return ["comment", "comment"]; 704 | } 705 | 706 | CodeMirror.defineMIME("text/css", { 707 | documentTypes: documentTypes, 708 | mediaTypes: mediaTypes, 709 | mediaFeatures: mediaFeatures, 710 | mediaValueKeywords: mediaValueKeywords, 711 | propertyKeywords: propertyKeywords, 712 | nonStandardPropertyKeywords: nonStandardPropertyKeywords, 713 | fontProperties: fontProperties, 714 | counterDescriptors: counterDescriptors, 715 | colorKeywords: colorKeywords, 716 | valueKeywords: valueKeywords, 717 | tokenHooks: { 718 | "/": function(stream, state) { 719 | if (!stream.eat("*")) return false; 720 | state.tokenize = tokenCComment; 721 | return tokenCComment(stream, state); 722 | } 723 | }, 724 | name: "css" 725 | }); 726 | 727 | CodeMirror.defineMIME("text/x-scss", { 728 | mediaTypes: mediaTypes, 729 | mediaFeatures: mediaFeatures, 730 | mediaValueKeywords: mediaValueKeywords, 731 | propertyKeywords: propertyKeywords, 732 | nonStandardPropertyKeywords: nonStandardPropertyKeywords, 733 | colorKeywords: colorKeywords, 734 | valueKeywords: valueKeywords, 735 | fontProperties: fontProperties, 736 | allowNested: true, 737 | lineComment: "//", 738 | tokenHooks: { 739 | "/": function(stream, state) { 740 | if (stream.eat("/")) { 741 | stream.skipToEnd(); 742 | return ["comment", "comment"]; 743 | } else if (stream.eat("*")) { 744 | state.tokenize = tokenCComment; 745 | return tokenCComment(stream, state); 746 | } else { 747 | return ["operator", "operator"]; 748 | } 749 | }, 750 | ":": function(stream) { 751 | if (stream.match(/\s*\{/, false)) 752 | return [null, null] 753 | return false; 754 | }, 755 | "$": function(stream) { 756 | stream.match(/^[\w-]+/); 757 | if (stream.match(/^\s*:/, false)) 758 | return ["variable-2", "variable-definition"]; 759 | return ["variable-2", "variable"]; 760 | }, 761 | "#": function(stream) { 762 | if (!stream.eat("{")) return false; 763 | return [null, "interpolation"]; 764 | } 765 | }, 766 | name: "css", 767 | helperType: "scss" 768 | }); 769 | 770 | CodeMirror.defineMIME("text/x-less", { 771 | mediaTypes: mediaTypes, 772 | mediaFeatures: mediaFeatures, 773 | mediaValueKeywords: mediaValueKeywords, 774 | propertyKeywords: propertyKeywords, 775 | nonStandardPropertyKeywords: nonStandardPropertyKeywords, 776 | colorKeywords: colorKeywords, 777 | valueKeywords: valueKeywords, 778 | fontProperties: fontProperties, 779 | allowNested: true, 780 | lineComment: "//", 781 | tokenHooks: { 782 | "/": function(stream, state) { 783 | if (stream.eat("/")) { 784 | stream.skipToEnd(); 785 | return ["comment", "comment"]; 786 | } else if (stream.eat("*")) { 787 | state.tokenize = tokenCComment; 788 | return tokenCComment(stream, state); 789 | } else { 790 | return ["operator", "operator"]; 791 | } 792 | }, 793 | "@": function(stream) { 794 | if (stream.eat("{")) return [null, "interpolation"]; 795 | if (stream.match(/^(charset|document|font-face|import|(-(moz|ms|o|webkit)-)?keyframes|media|namespace|page|supports)\b/i, false)) return false; 796 | stream.eatWhile(/[\w\\\-]/); 797 | if (stream.match(/^\s*:/, false)) 798 | return ["variable-2", "variable-definition"]; 799 | return ["variable-2", "variable"]; 800 | }, 801 | "&": function() { 802 | return ["atom", "atom"]; 803 | } 804 | }, 805 | name: "css", 806 | helperType: "less" 807 | }); 808 | 809 | CodeMirror.defineMIME("text/x-gss", { 810 | documentTypes: documentTypes, 811 | mediaTypes: mediaTypes, 812 | mediaFeatures: mediaFeatures, 813 | propertyKeywords: propertyKeywords, 814 | nonStandardPropertyKeywords: nonStandardPropertyKeywords, 815 | fontProperties: fontProperties, 816 | counterDescriptors: counterDescriptors, 817 | colorKeywords: colorKeywords, 818 | valueKeywords: valueKeywords, 819 | supportsAtComponent: true, 820 | tokenHooks: { 821 | "/": function(stream, state) { 822 | if (!stream.eat("*")) return false; 823 | state.tokenize = tokenCComment; 824 | return tokenCComment(stream, state); 825 | } 826 | }, 827 | name: "css", 828 | helperType: "gss" 829 | }); 830 | 831 | }); 832 | -------------------------------------------------------------------------------- /src/mode/javascript.js: -------------------------------------------------------------------------------- 1 | // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 | // Distributed under an MIT license: https://codemirror.net/LICENSE 3 | 4 | (function(mod) { 5 | if (typeof exports == "object" && typeof module == "object") // CommonJS 6 | mod(require("../../lib/codemirror")); 7 | else if (typeof define == "function" && define.amd) // AMD 8 | define(["../../lib/codemirror"], mod); 9 | else // Plain browser env 10 | mod(CodeMirror); 11 | })(function(CodeMirror) { 12 | "use strict"; 13 | 14 | CodeMirror.defineMode("javascript", function(config, parserConfig) { 15 | var indentUnit = config.indentUnit; 16 | var statementIndent = parserConfig.statementIndent; 17 | var jsonldMode = parserConfig.jsonld; 18 | var jsonMode = parserConfig.json || jsonldMode; 19 | var isTS = parserConfig.typescript; 20 | var wordRE = parserConfig.wordCharacters || /[\w$\xa1-\uffff]/; 21 | 22 | // Tokenizer 23 | 24 | var keywords = function(){ 25 | function kw(type) {return {type: type, style: "keyword"};} 26 | var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c"), D = kw("keyword d"); 27 | var operator = kw("operator"), atom = {type: "atom", style: "atom"}; 28 | 29 | return { 30 | "if": kw("if"), "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B, 31 | "return": D, "break": D, "continue": D, "new": kw("new"), "delete": C, "void": C, "throw": C, 32 | "debugger": kw("debugger"), "var": kw("var"), "const": kw("var"), "let": kw("var"), 33 | "function": kw("function"), "catch": kw("catch"), 34 | "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"), 35 | "in": operator, "typeof": operator, "instanceof": operator, 36 | "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity": atom, 37 | "this": kw("this"), "class": kw("class"), "super": kw("atom"), 38 | "yield": C, "export": kw("export"), "import": kw("import"), "extends": C, 39 | "await": C 40 | }; 41 | }(); 42 | 43 | var isOperatorChar = /[+\-*&%=<>!?|~^@]/; 44 | var isJsonldKeyword = /^@(context|id|value|language|type|container|list|set|reverse|index|base|vocab|graph)"/; 45 | 46 | function readRegexp(stream) { 47 | var escaped = false, next, inSet = false; 48 | while ((next = stream.next()) != null) { 49 | if (!escaped) { 50 | if (next == "/" && !inSet) return; 51 | if (next == "[") inSet = true; 52 | else if (inSet && next == "]") inSet = false; 53 | } 54 | escaped = !escaped && next == "\\"; 55 | } 56 | } 57 | 58 | // Used as scratch variables to communicate multiple values without 59 | // consing up tons of objects. 60 | var type, content; 61 | function ret(tp, style, cont) { 62 | type = tp; content = cont; 63 | return style; 64 | } 65 | function tokenBase(stream, state) { 66 | var ch = stream.next(); 67 | if (ch == '"' || ch == "'") { 68 | state.tokenize = tokenString(ch); 69 | return state.tokenize(stream, state); 70 | } else if (ch == "." && stream.match(/^\d+(?:[eE][+\-]?\d+)?/)) { 71 | return ret("number", "number"); 72 | } else if (ch == "." && stream.match("..")) { 73 | return ret("spread", "meta"); 74 | } else if (/[\[\]{}\(\),;\:\.]/.test(ch)) { 75 | return ret(ch); 76 | } else if (ch == "=" && stream.eat(">")) { 77 | return ret("=>", "operator"); 78 | } else if (ch == "0" && stream.match(/^(?:x[\da-f]+|o[0-7]+|b[01]+)n?/i)) { 79 | return ret("number", "number"); 80 | } else if (/\d/.test(ch)) { 81 | stream.match(/^\d*(?:n|(?:\.\d*)?(?:[eE][+\-]?\d+)?)?/); 82 | return ret("number", "number"); 83 | } else if (ch == "/") { 84 | if (stream.eat("*")) { 85 | state.tokenize = tokenComment; 86 | return tokenComment(stream, state); 87 | } else if (stream.eat("/")) { 88 | stream.skipToEnd(); 89 | return ret("comment", "comment"); 90 | } else if (expressionAllowed(stream, state, 1)) { 91 | readRegexp(stream); 92 | stream.match(/^\b(([gimyus])(?![gimyus]*\2))+\b/); 93 | return ret("regexp", "string-2"); 94 | } else { 95 | stream.eat("="); 96 | return ret("operator", "operator", stream.current()); 97 | } 98 | } else if (ch == "`") { 99 | state.tokenize = tokenQuasi; 100 | return tokenQuasi(stream, state); 101 | } else if (ch == "#") { 102 | stream.skipToEnd(); 103 | return ret("error", "error"); 104 | } else if (isOperatorChar.test(ch)) { 105 | if (ch != ">" || !state.lexical || state.lexical.type != ">") { 106 | if (stream.eat("=")) { 107 | if (ch == "!" || ch == "=") stream.eat("=") 108 | } else if (/[<>*+\-]/.test(ch)) { 109 | stream.eat(ch) 110 | if (ch == ">") stream.eat(ch) 111 | } 112 | } 113 | return ret("operator", "operator", stream.current()); 114 | } else if (wordRE.test(ch)) { 115 | stream.eatWhile(wordRE); 116 | var word = stream.current() 117 | if (state.lastType != ".") { 118 | if (keywords.propertyIsEnumerable(word)) { 119 | var kw = keywords[word] 120 | return ret(kw.type, kw.style, word) 121 | } 122 | if (word == "async" && stream.match(/^(\s|\/\*.*?\*\/)*[\[\(\w]/, false)) 123 | return ret("async", "keyword", word) 124 | } 125 | return ret("variable", "variable", word) 126 | } 127 | } 128 | 129 | function tokenString(quote) { 130 | return function(stream, state) { 131 | var escaped = false, next; 132 | if (jsonldMode && stream.peek() == "@" && stream.match(isJsonldKeyword)){ 133 | state.tokenize = tokenBase; 134 | return ret("jsonld-keyword", "meta"); 135 | } 136 | while ((next = stream.next()) != null) { 137 | if (next == quote && !escaped) break; 138 | escaped = !escaped && next == "\\"; 139 | } 140 | if (!escaped) state.tokenize = tokenBase; 141 | return ret("string", "string"); 142 | }; 143 | } 144 | 145 | function tokenComment(stream, state) { 146 | var maybeEnd = false, ch; 147 | while (ch = stream.next()) { 148 | if (ch == "/" && maybeEnd) { 149 | state.tokenize = tokenBase; 150 | break; 151 | } 152 | maybeEnd = (ch == "*"); 153 | } 154 | return ret("comment", "comment"); 155 | } 156 | 157 | function tokenQuasi(stream, state) { 158 | var escaped = false, next; 159 | while ((next = stream.next()) != null) { 160 | if (!escaped && (next == "`" || next == "$" && stream.eat("{"))) { 161 | state.tokenize = tokenBase; 162 | break; 163 | } 164 | escaped = !escaped && next == "\\"; 165 | } 166 | return ret("quasi", "string-2", stream.current()); 167 | } 168 | 169 | var brackets = "([{}])"; 170 | // This is a crude lookahead trick to try and notice that we're 171 | // parsing the argument patterns for a fat-arrow function before we 172 | // actually hit the arrow token. It only works if the arrow is on 173 | // the same line as the arguments and there's no strange noise 174 | // (comments) in between. Fallback is to only notice when we hit the 175 | // arrow, and not declare the arguments as locals for the arrow 176 | // body. 177 | function findFatArrow(stream, state) { 178 | if (state.fatArrowAt) state.fatArrowAt = null; 179 | var arrow = stream.string.indexOf("=>", stream.start); 180 | if (arrow < 0) return; 181 | 182 | if (isTS) { // Try to skip TypeScript return type declarations after the arguments 183 | var m = /:\s*(?:\w+(?:<[^>]*>|\[\])?|\{[^}]*\})\s*$/.exec(stream.string.slice(stream.start, arrow)) 184 | if (m) arrow = m.index 185 | } 186 | 187 | var depth = 0, sawSomething = false; 188 | for (var pos = arrow - 1; pos >= 0; --pos) { 189 | var ch = stream.string.charAt(pos); 190 | var bracket = brackets.indexOf(ch); 191 | if (bracket >= 0 && bracket < 3) { 192 | if (!depth) { ++pos; break; } 193 | if (--depth == 0) { if (ch == "(") sawSomething = true; break; } 194 | } else if (bracket >= 3 && bracket < 6) { 195 | ++depth; 196 | } else if (wordRE.test(ch)) { 197 | sawSomething = true; 198 | } else if (/["'\/]/.test(ch)) { 199 | return; 200 | } else if (sawSomething && !depth) { 201 | ++pos; 202 | break; 203 | } 204 | } 205 | if (sawSomething && !depth) state.fatArrowAt = pos; 206 | } 207 | 208 | // Parser 209 | 210 | var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp": true, "this": true, "jsonld-keyword": true}; 211 | 212 | function JSLexical(indented, column, type, align, prev, info) { 213 | this.indented = indented; 214 | this.column = column; 215 | this.type = type; 216 | this.prev = prev; 217 | this.info = info; 218 | if (align != null) this.align = align; 219 | } 220 | 221 | function inScope(state, varname) { 222 | for (var v = state.localVars; v; v = v.next) 223 | if (v.name == varname) return true; 224 | for (var cx = state.context; cx; cx = cx.prev) { 225 | for (var v = cx.vars; v; v = v.next) 226 | if (v.name == varname) return true; 227 | } 228 | } 229 | 230 | function parseJS(state, style, type, content, stream) { 231 | var cc = state.cc; 232 | // Communicate our context to the combinators. 233 | // (Less wasteful than consing up a hundred closures on every call.) 234 | cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc; cx.style = style; 235 | 236 | if (!state.lexical.hasOwnProperty("align")) 237 | state.lexical.align = true; 238 | 239 | while(true) { 240 | var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement; 241 | if (combinator(type, content)) { 242 | while(cc.length && cc[cc.length - 1].lex) 243 | cc.pop()(); 244 | if (cx.marked) return cx.marked; 245 | if (type == "variable" && inScope(state, content)) return "variable-2"; 246 | return style; 247 | } 248 | } 249 | } 250 | 251 | // Combinator utils 252 | 253 | var cx = {state: null, column: null, marked: null, cc: null}; 254 | function pass() { 255 | for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]); 256 | } 257 | function cont() { 258 | pass.apply(null, arguments); 259 | return true; 260 | } 261 | function inList(name, list) { 262 | for (var v = list; v; v = v.next) if (v.name == name) return true 263 | return false; 264 | } 265 | function register(varname) { 266 | var state = cx.state; 267 | cx.marked = "def"; 268 | if (state.context) { 269 | if (state.lexical.info == "var" && state.context && state.context.block) { 270 | // FIXME function decls are also not block scoped 271 | var newContext = registerVarScoped(varname, state.context) 272 | if (newContext != null) { 273 | state.context = newContext 274 | return 275 | } 276 | } else if (!inList(varname, state.localVars)) { 277 | state.localVars = new Var(varname, state.localVars) 278 | return 279 | } 280 | } 281 | // Fall through means this is global 282 | if (parserConfig.globalVars && !inList(varname, state.globalVars)) 283 | state.globalVars = new Var(varname, state.globalVars) 284 | } 285 | function registerVarScoped(varname, context) { 286 | if (!context) { 287 | return null 288 | } else if (context.block) { 289 | var inner = registerVarScoped(varname, context.prev) 290 | if (!inner) return null 291 | if (inner == context.prev) return context 292 | return new Context(inner, context.vars, true) 293 | } else if (inList(varname, context.vars)) { 294 | return context 295 | } else { 296 | return new Context(context.prev, new Var(varname, context.vars), false) 297 | } 298 | } 299 | 300 | function isModifier(name) { 301 | return name == "public" || name == "private" || name == "protected" || name == "abstract" || name == "readonly" 302 | } 303 | 304 | // Combinators 305 | 306 | function Context(prev, vars, block) { this.prev = prev; this.vars = vars; this.block = block } 307 | function Var(name, next) { this.name = name; this.next = next } 308 | 309 | var defaultVars = new Var("this", new Var("arguments", null)) 310 | function pushcontext() { 311 | cx.state.context = new Context(cx.state.context, cx.state.localVars, false) 312 | cx.state.localVars = defaultVars 313 | } 314 | function pushblockcontext() { 315 | cx.state.context = new Context(cx.state.context, cx.state.localVars, true) 316 | cx.state.localVars = null 317 | } 318 | function popcontext() { 319 | cx.state.localVars = cx.state.context.vars 320 | cx.state.context = cx.state.context.prev 321 | } 322 | popcontext.lex = true 323 | function pushlex(type, info) { 324 | var result = function() { 325 | var state = cx.state, indent = state.indented; 326 | if (state.lexical.type == "stat") indent = state.lexical.indented; 327 | else for (var outer = state.lexical; outer && outer.type == ")" && outer.align; outer = outer.prev) 328 | indent = outer.indented; 329 | state.lexical = new JSLexical(indent, cx.stream.column(), type, null, state.lexical, info); 330 | }; 331 | result.lex = true; 332 | return result; 333 | } 334 | function poplex() { 335 | var state = cx.state; 336 | if (state.lexical.prev) { 337 | if (state.lexical.type == ")") 338 | state.indented = state.lexical.indented; 339 | state.lexical = state.lexical.prev; 340 | } 341 | } 342 | poplex.lex = true; 343 | 344 | function expect(wanted) { 345 | function exp(type) { 346 | if (type == wanted) return cont(); 347 | else if (wanted == ";" || type == "}" || type == ")" || type == "]") return pass(); 348 | else return cont(exp); 349 | }; 350 | return exp; 351 | } 352 | 353 | function statement(type, value) { 354 | if (type == "var") return cont(pushlex("vardef", value), vardef, expect(";"), poplex); 355 | if (type == "keyword a") return cont(pushlex("form"), parenExpr, statement, poplex); 356 | if (type == "keyword b") return cont(pushlex("form"), statement, poplex); 357 | if (type == "keyword d") return cx.stream.match(/^\s*$/, false) ? cont() : cont(pushlex("stat"), maybeexpression, expect(";"), poplex); 358 | if (type == "debugger") return cont(expect(";")); 359 | if (type == "{") return cont(pushlex("}"), pushblockcontext, block, poplex, popcontext); 360 | if (type == ";") return cont(); 361 | if (type == "if") { 362 | if (cx.state.lexical.info == "else" && cx.state.cc[cx.state.cc.length - 1] == poplex) 363 | cx.state.cc.pop()(); 364 | return cont(pushlex("form"), parenExpr, statement, poplex, maybeelse); 365 | } 366 | if (type == "function") return cont(functiondef); 367 | if (type == "for") return cont(pushlex("form"), forspec, statement, poplex); 368 | if (type == "class" || (isTS && value == "interface")) { 369 | cx.marked = "keyword" 370 | return cont(pushlex("form", type == "class" ? type : value), className, poplex) 371 | } 372 | if (type == "variable") { 373 | if (isTS && value == "declare") { 374 | cx.marked = "keyword" 375 | return cont(statement) 376 | } else if (isTS && (value == "module" || value == "enum" || value == "type") && cx.stream.match(/^\s*\w/, false)) { 377 | cx.marked = "keyword" 378 | if (value == "enum") return cont(enumdef); 379 | else if (value == "type") return cont(typename, expect("operator"), typeexpr, expect(";")); 380 | else return cont(pushlex("form"), pattern, expect("{"), pushlex("}"), block, poplex, poplex) 381 | } else if (isTS && value == "namespace") { 382 | cx.marked = "keyword" 383 | return cont(pushlex("form"), expression, statement, poplex) 384 | } else if (isTS && value == "abstract") { 385 | cx.marked = "keyword" 386 | return cont(statement) 387 | } else { 388 | return cont(pushlex("stat"), maybelabel); 389 | } 390 | } 391 | if (type == "switch") return cont(pushlex("form"), parenExpr, expect("{"), pushlex("}", "switch"), pushblockcontext, 392 | block, poplex, poplex, popcontext); 393 | if (type == "case") return cont(expression, expect(":")); 394 | if (type == "default") return cont(expect(":")); 395 | if (type == "catch") return cont(pushlex("form"), pushcontext, maybeCatchBinding, statement, poplex, popcontext); 396 | if (type == "export") return cont(pushlex("stat"), afterExport, poplex); 397 | if (type == "import") return cont(pushlex("stat"), afterImport, poplex); 398 | if (type == "async") return cont(statement) 399 | if (value == "@") return cont(expression, statement) 400 | return pass(pushlex("stat"), expression, expect(";"), poplex); 401 | } 402 | function maybeCatchBinding(type) { 403 | if (type == "(") return cont(funarg, expect(")")) 404 | } 405 | function expression(type, value) { 406 | return expressionInner(type, value, false); 407 | } 408 | function expressionNoComma(type, value) { 409 | return expressionInner(type, value, true); 410 | } 411 | function parenExpr(type) { 412 | if (type != "(") return pass() 413 | return cont(pushlex(")"), expression, expect(")"), poplex) 414 | } 415 | function expressionInner(type, value, noComma) { 416 | if (cx.state.fatArrowAt == cx.stream.start) { 417 | var body = noComma ? arrowBodyNoComma : arrowBody; 418 | if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, expect("=>"), body, popcontext); 419 | else if (type == "variable") return pass(pushcontext, pattern, expect("=>"), body, popcontext); 420 | } 421 | 422 | var maybeop = noComma ? maybeoperatorNoComma : maybeoperatorComma; 423 | if (atomicTypes.hasOwnProperty(type)) return cont(maybeop); 424 | if (type == "function") return cont(functiondef, maybeop); 425 | if (type == "class" || (isTS && value == "interface")) { cx.marked = "keyword"; return cont(pushlex("form"), classExpression, poplex); } 426 | if (type == "keyword c" || type == "async") return cont(noComma ? expressionNoComma : expression); 427 | if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeop); 428 | if (type == "operator" || type == "spread") return cont(noComma ? expressionNoComma : expression); 429 | if (type == "[") return cont(pushlex("]"), arrayLiteral, poplex, maybeop); 430 | if (type == "{") return contCommasep(objprop, "}", null, maybeop); 431 | if (type == "quasi") return pass(quasi, maybeop); 432 | if (type == "new") return cont(maybeTarget(noComma)); 433 | if (type == "import") return cont(expression); 434 | return cont(); 435 | } 436 | function maybeexpression(type) { 437 | if (type.match(/[;\}\)\],]/)) return pass(); 438 | return pass(expression); 439 | } 440 | 441 | function maybeoperatorComma(type, value) { 442 | if (type == ",") return cont(expression); 443 | return maybeoperatorNoComma(type, value, false); 444 | } 445 | function maybeoperatorNoComma(type, value, noComma) { 446 | var me = noComma == false ? maybeoperatorComma : maybeoperatorNoComma; 447 | var expr = noComma == false ? expression : expressionNoComma; 448 | if (type == "=>") return cont(pushcontext, noComma ? arrowBodyNoComma : arrowBody, popcontext); 449 | if (type == "operator") { 450 | if (/\+\+|--/.test(value) || isTS && value == "!") return cont(me); 451 | if (isTS && value == "<" && cx.stream.match(/^([^>]|<.*?>)*>\s*\(/, false)) 452 | return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, me); 453 | if (value == "?") return cont(expression, expect(":"), expr); 454 | return cont(expr); 455 | } 456 | if (type == "quasi") { return pass(quasi, me); } 457 | if (type == ";") return; 458 | if (type == "(") return contCommasep(expressionNoComma, ")", "call", me); 459 | if (type == ".") return cont(property, me); 460 | if (type == "[") return cont(pushlex("]"), maybeexpression, expect("]"), poplex, me); 461 | if (isTS && value == "as") { cx.marked = "keyword"; return cont(typeexpr, me) } 462 | if (type == "regexp") { 463 | cx.state.lastType = cx.marked = "operator" 464 | cx.stream.backUp(cx.stream.pos - cx.stream.start - 1) 465 | return cont(expr) 466 | } 467 | } 468 | function quasi(type, value) { 469 | if (type != "quasi") return pass(); 470 | if (value.slice(value.length - 2) != "${") return cont(quasi); 471 | return cont(expression, continueQuasi); 472 | } 473 | function continueQuasi(type) { 474 | if (type == "}") { 475 | cx.marked = "string-2"; 476 | cx.state.tokenize = tokenQuasi; 477 | return cont(quasi); 478 | } 479 | } 480 | function arrowBody(type) { 481 | findFatArrow(cx.stream, cx.state); 482 | return pass(type == "{" ? statement : expression); 483 | } 484 | function arrowBodyNoComma(type) { 485 | findFatArrow(cx.stream, cx.state); 486 | return pass(type == "{" ? statement : expressionNoComma); 487 | } 488 | function maybeTarget(noComma) { 489 | return function(type) { 490 | if (type == ".") return cont(noComma ? targetNoComma : target); 491 | else if (type == "variable" && isTS) return cont(maybeTypeArgs, noComma ? maybeoperatorNoComma : maybeoperatorComma) 492 | else return pass(noComma ? expressionNoComma : expression); 493 | }; 494 | } 495 | function target(_, value) { 496 | if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorComma); } 497 | } 498 | function targetNoComma(_, value) { 499 | if (value == "target") { cx.marked = "keyword"; return cont(maybeoperatorNoComma); } 500 | } 501 | function maybelabel(type) { 502 | if (type == ":") return cont(poplex, statement); 503 | return pass(maybeoperatorComma, expect(";"), poplex); 504 | } 505 | function property(type) { 506 | if (type == "variable") {cx.marked = "property"; return cont();} 507 | } 508 | function objprop(type, value) { 509 | if (type == "async") { 510 | cx.marked = "property"; 511 | return cont(objprop); 512 | } else if (type == "variable" || cx.style == "keyword") { 513 | cx.marked = "property"; 514 | if (value == "get" || value == "set") return cont(getterSetter); 515 | var m // Work around fat-arrow-detection complication for detecting typescript typed arrow params 516 | if (isTS && cx.state.fatArrowAt == cx.stream.start && (m = cx.stream.match(/^\s*:\s*/, false))) 517 | cx.state.fatArrowAt = cx.stream.pos + m[0].length 518 | return cont(afterprop); 519 | } else if (type == "number" || type == "string") { 520 | cx.marked = jsonldMode ? "property" : (cx.style + " property"); 521 | return cont(afterprop); 522 | } else if (type == "jsonld-keyword") { 523 | return cont(afterprop); 524 | } else if (isTS && isModifier(value)) { 525 | cx.marked = "keyword" 526 | return cont(objprop) 527 | } else if (type == "[") { 528 | return cont(expression, maybetype, expect("]"), afterprop); 529 | } else if (type == "spread") { 530 | return cont(expressionNoComma, afterprop); 531 | } else if (value == "*") { 532 | cx.marked = "keyword"; 533 | return cont(objprop); 534 | } else if (type == ":") { 535 | return pass(afterprop) 536 | } 537 | } 538 | function getterSetter(type) { 539 | if (type != "variable") return pass(afterprop); 540 | cx.marked = "property"; 541 | return cont(functiondef); 542 | } 543 | function afterprop(type) { 544 | if (type == ":") return cont(expressionNoComma); 545 | if (type == "(") return pass(functiondef); 546 | } 547 | function commasep(what, end, sep) { 548 | function proceed(type, value) { 549 | if (sep ? sep.indexOf(type) > -1 : type == ",") { 550 | var lex = cx.state.lexical; 551 | if (lex.info == "call") lex.pos = (lex.pos || 0) + 1; 552 | return cont(function(type, value) { 553 | if (type == end || value == end) return pass() 554 | return pass(what) 555 | }, proceed); 556 | } 557 | if (type == end || value == end) return cont(); 558 | if (sep && sep.indexOf(";") > -1) return pass(what) 559 | return cont(expect(end)); 560 | } 561 | return function(type, value) { 562 | if (type == end || value == end) return cont(); 563 | return pass(what, proceed); 564 | }; 565 | } 566 | function contCommasep(what, end, info) { 567 | for (var i = 3; i < arguments.length; i++) 568 | cx.cc.push(arguments[i]); 569 | return cont(pushlex(end, info), commasep(what, end), poplex); 570 | } 571 | function block(type) { 572 | if (type == "}") return cont(); 573 | return pass(statement, block); 574 | } 575 | function maybetype(type, value) { 576 | if (isTS) { 577 | if (type == ":" || value == "in") return cont(typeexpr); 578 | if (value == "?") return cont(maybetype); 579 | } 580 | } 581 | function mayberettype(type) { 582 | if (isTS && type == ":") { 583 | if (cx.stream.match(/^\s*\w+\s+is\b/, false)) return cont(expression, isKW, typeexpr) 584 | else return cont(typeexpr) 585 | } 586 | } 587 | function isKW(_, value) { 588 | if (value == "is") { 589 | cx.marked = "keyword" 590 | return cont() 591 | } 592 | } 593 | function typeexpr(type, value) { 594 | if (value == "keyof" || value == "typeof" || value == "infer") { 595 | cx.marked = "keyword" 596 | return cont(value == "typeof" ? expressionNoComma : typeexpr) 597 | } 598 | if (type == "variable" || value == "void") { 599 | cx.marked = "type" 600 | return cont(afterType) 601 | } 602 | if (type == "string" || type == "number" || type == "atom") return cont(afterType); 603 | if (type == "[") return cont(pushlex("]"), commasep(typeexpr, "]", ","), poplex, afterType) 604 | if (type == "{") return cont(pushlex("}"), commasep(typeprop, "}", ",;"), poplex, afterType) 605 | if (type == "(") return cont(commasep(typearg, ")"), maybeReturnType, afterType) 606 | if (type == "<") return cont(commasep(typeexpr, ">"), typeexpr) 607 | } 608 | function maybeReturnType(type) { 609 | if (type == "=>") return cont(typeexpr) 610 | } 611 | function typeprop(type, value) { 612 | if (type == "variable" || cx.style == "keyword") { 613 | cx.marked = "property" 614 | return cont(typeprop) 615 | } else if (value == "?" || type == "number" || type == "string") { 616 | return cont(typeprop) 617 | } else if (type == ":") { 618 | return cont(typeexpr) 619 | } else if (type == "[") { 620 | return cont(expect("variable"), maybetype, expect("]"), typeprop) 621 | } else if (type == "(") { 622 | return pass(functiondecl, typeprop) 623 | } 624 | } 625 | function typearg(type, value) { 626 | if (type == "variable" && cx.stream.match(/^\s*[?:]/, false) || value == "?") return cont(typearg) 627 | if (type == ":") return cont(typeexpr) 628 | if (type == "spread") return cont(typearg) 629 | return pass(typeexpr) 630 | } 631 | function afterType(type, value) { 632 | if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType) 633 | if (value == "|" || type == "." || value == "&") return cont(typeexpr) 634 | if (type == "[") return cont(typeexpr, expect("]"), afterType) 635 | if (value == "extends" || value == "implements") { cx.marked = "keyword"; return cont(typeexpr) } 636 | if (value == "?") return cont(typeexpr, expect(":"), typeexpr) 637 | } 638 | function maybeTypeArgs(_, value) { 639 | if (value == "<") return cont(pushlex(">"), commasep(typeexpr, ">"), poplex, afterType) 640 | } 641 | function typeparam() { 642 | return pass(typeexpr, maybeTypeDefault) 643 | } 644 | function maybeTypeDefault(_, value) { 645 | if (value == "=") return cont(typeexpr) 646 | } 647 | function vardef(_, value) { 648 | if (value == "enum") {cx.marked = "keyword"; return cont(enumdef)} 649 | return pass(pattern, maybetype, maybeAssign, vardefCont); 650 | } 651 | function pattern(type, value) { 652 | if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(pattern) } 653 | if (type == "variable") { register(value); return cont(); } 654 | if (type == "spread") return cont(pattern); 655 | if (type == "[") return contCommasep(eltpattern, "]"); 656 | if (type == "{") return contCommasep(proppattern, "}"); 657 | } 658 | function proppattern(type, value) { 659 | if (type == "variable" && !cx.stream.match(/^\s*:/, false)) { 660 | register(value); 661 | return cont(maybeAssign); 662 | } 663 | if (type == "variable") cx.marked = "property"; 664 | if (type == "spread") return cont(pattern); 665 | if (type == "}") return pass(); 666 | if (type == "[") return cont(expression, expect(']'), expect(':'), proppattern); 667 | return cont(expect(":"), pattern, maybeAssign); 668 | } 669 | function eltpattern() { 670 | return pass(pattern, maybeAssign) 671 | } 672 | function maybeAssign(_type, value) { 673 | if (value == "=") return cont(expressionNoComma); 674 | } 675 | function vardefCont(type) { 676 | if (type == ",") return cont(vardef); 677 | } 678 | function maybeelse(type, value) { 679 | if (type == "keyword b" && value == "else") return cont(pushlex("form", "else"), statement, poplex); 680 | } 681 | function forspec(type, value) { 682 | if (value == "await") return cont(forspec); 683 | if (type == "(") return cont(pushlex(")"), forspec1, expect(")"), poplex); 684 | } 685 | function forspec1(type) { 686 | if (type == "var") return cont(vardef, expect(";"), forspec2); 687 | if (type == ";") return cont(forspec2); 688 | if (type == "variable") return cont(formaybeinof); 689 | return pass(expression, expect(";"), forspec2); 690 | } 691 | function formaybeinof(_type, value) { 692 | if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); } 693 | return cont(maybeoperatorComma, forspec2); 694 | } 695 | function forspec2(type, value) { 696 | if (type == ";") return cont(forspec3); 697 | if (value == "in" || value == "of") { cx.marked = "keyword"; return cont(expression); } 698 | return pass(expression, expect(";"), forspec3); 699 | } 700 | function forspec3(type) { 701 | if (type != ")") cont(expression); 702 | } 703 | function functiondef(type, value) { 704 | if (value == "*") {cx.marked = "keyword"; return cont(functiondef);} 705 | if (type == "variable") {register(value); return cont(functiondef);} 706 | if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, statement, popcontext); 707 | if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondef) 708 | } 709 | function functiondecl(type, value) { 710 | if (value == "*") {cx.marked = "keyword"; return cont(functiondecl);} 711 | if (type == "variable") {register(value); return cont(functiondecl);} 712 | if (type == "(") return cont(pushcontext, pushlex(")"), commasep(funarg, ")"), poplex, mayberettype, popcontext); 713 | if (isTS && value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, functiondecl) 714 | } 715 | function typename(type, value) { 716 | if (type == "keyword" || type == "variable") { 717 | cx.marked = "type" 718 | return cont(typename) 719 | } else if (value == "<") { 720 | return cont(pushlex(">"), commasep(typeparam, ">"), poplex) 721 | } 722 | } 723 | function funarg(type, value) { 724 | if (value == "@") cont(expression, funarg) 725 | if (type == "spread") return cont(funarg); 726 | if (isTS && isModifier(value)) { cx.marked = "keyword"; return cont(funarg); } 727 | return pass(pattern, maybetype, maybeAssign); 728 | } 729 | function classExpression(type, value) { 730 | // Class expressions may have an optional name. 731 | if (type == "variable") return className(type, value); 732 | return classNameAfter(type, value); 733 | } 734 | function className(type, value) { 735 | if (type == "variable") {register(value); return cont(classNameAfter);} 736 | } 737 | function classNameAfter(type, value) { 738 | if (value == "<") return cont(pushlex(">"), commasep(typeparam, ">"), poplex, classNameAfter) 739 | if (value == "extends" || value == "implements" || (isTS && type == ",")) { 740 | if (value == "implements") cx.marked = "keyword"; 741 | return cont(isTS ? typeexpr : expression, classNameAfter); 742 | } 743 | if (type == "{") return cont(pushlex("}"), classBody, poplex); 744 | } 745 | function classBody(type, value) { 746 | if (type == "async" || 747 | (type == "variable" && 748 | (value == "static" || value == "get" || value == "set" || (isTS && isModifier(value))) && 749 | cx.stream.match(/^\s+[\w$\xa1-\uffff]/, false))) { 750 | cx.marked = "keyword"; 751 | return cont(classBody); 752 | } 753 | if (type == "variable" || cx.style == "keyword") { 754 | cx.marked = "property"; 755 | return cont(isTS ? classfield : functiondef, classBody); 756 | } 757 | if (type == "number" || type == "string") return cont(isTS ? classfield : functiondef, classBody); 758 | if (type == "[") 759 | return cont(expression, maybetype, expect("]"), isTS ? classfield : functiondef, classBody) 760 | if (value == "*") { 761 | cx.marked = "keyword"; 762 | return cont(classBody); 763 | } 764 | if (isTS && type == "(") return pass(functiondecl, classBody) 765 | if (type == ";" || type == ",") return cont(classBody); 766 | if (type == "}") return cont(); 767 | if (value == "@") return cont(expression, classBody) 768 | } 769 | function classfield(type, value) { 770 | if (value == "?") return cont(classfield) 771 | if (type == ":") return cont(typeexpr, maybeAssign) 772 | if (value == "=") return cont(expressionNoComma) 773 | var context = cx.state.lexical.prev, isInterface = context && context.info == "interface" 774 | return pass(isInterface ? functiondecl : functiondef) 775 | } 776 | function afterExport(type, value) { 777 | if (value == "*") { cx.marked = "keyword"; return cont(maybeFrom, expect(";")); } 778 | if (value == "default") { cx.marked = "keyword"; return cont(expression, expect(";")); } 779 | if (type == "{") return cont(commasep(exportField, "}"), maybeFrom, expect(";")); 780 | return pass(statement); 781 | } 782 | function exportField(type, value) { 783 | if (value == "as") { cx.marked = "keyword"; return cont(expect("variable")); } 784 | if (type == "variable") return pass(expressionNoComma, exportField); 785 | } 786 | function afterImport(type) { 787 | if (type == "string") return cont(); 788 | if (type == "(") return pass(expression); 789 | return pass(importSpec, maybeMoreImports, maybeFrom); 790 | } 791 | function importSpec(type, value) { 792 | if (type == "{") return contCommasep(importSpec, "}"); 793 | if (type == "variable") register(value); 794 | if (value == "*") cx.marked = "keyword"; 795 | return cont(maybeAs); 796 | } 797 | function maybeMoreImports(type) { 798 | if (type == ",") return cont(importSpec, maybeMoreImports) 799 | } 800 | function maybeAs(_type, value) { 801 | if (value == "as") { cx.marked = "keyword"; return cont(importSpec); } 802 | } 803 | function maybeFrom(_type, value) { 804 | if (value == "from") { cx.marked = "keyword"; return cont(expression); } 805 | } 806 | function arrayLiteral(type) { 807 | if (type == "]") return cont(); 808 | return pass(commasep(expressionNoComma, "]")); 809 | } 810 | function enumdef() { 811 | return pass(pushlex("form"), pattern, expect("{"), pushlex("}"), commasep(enummember, "}"), poplex, poplex) 812 | } 813 | function enummember() { 814 | return pass(pattern, maybeAssign); 815 | } 816 | 817 | function isContinuedStatement(state, textAfter) { 818 | return state.lastType == "operator" || state.lastType == "," || 819 | isOperatorChar.test(textAfter.charAt(0)) || 820 | /[,.]/.test(textAfter.charAt(0)); 821 | } 822 | 823 | function expressionAllowed(stream, state, backUp) { 824 | return state.tokenize == tokenBase && 825 | /^(?:operator|sof|keyword [bcd]|case|new|export|default|spread|[\[{}\(,;:]|=>)$/.test(state.lastType) || 826 | (state.lastType == "quasi" && /\{\s*$/.test(stream.string.slice(0, stream.pos - (backUp || 0)))) 827 | } 828 | 829 | // Interface 830 | 831 | return { 832 | startState: function(basecolumn) { 833 | var state = { 834 | tokenize: tokenBase, 835 | lastType: "sof", 836 | cc: [], 837 | lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false), 838 | localVars: parserConfig.localVars, 839 | context: parserConfig.localVars && new Context(null, null, false), 840 | indented: basecolumn || 0 841 | }; 842 | if (parserConfig.globalVars && typeof parserConfig.globalVars == "object") 843 | state.globalVars = parserConfig.globalVars; 844 | return state; 845 | }, 846 | 847 | token: function(stream, state) { 848 | if (stream.sol()) { 849 | if (!state.lexical.hasOwnProperty("align")) 850 | state.lexical.align = false; 851 | state.indented = stream.indentation(); 852 | findFatArrow(stream, state); 853 | } 854 | if (state.tokenize != tokenComment && stream.eatSpace()) return null; 855 | var style = state.tokenize(stream, state); 856 | if (type == "comment") return style; 857 | state.lastType = type == "operator" && (content == "++" || content == "--") ? "incdec" : type; 858 | return parseJS(state, style, type, content, stream); 859 | }, 860 | 861 | indent: function(state, textAfter) { 862 | if (state.tokenize == tokenComment) return CodeMirror.Pass; 863 | if (state.tokenize != tokenBase) return 0; 864 | var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical, top 865 | // Kludge to prevent 'maybelse' from blocking lexical scope pops 866 | if (!/^\s*else\b/.test(textAfter)) for (var i = state.cc.length - 1; i >= 0; --i) { 867 | var c = state.cc[i]; 868 | if (c == poplex) lexical = lexical.prev; 869 | else if (c != maybeelse) break; 870 | } 871 | while ((lexical.type == "stat" || lexical.type == "form") && 872 | (firstChar == "}" || ((top = state.cc[state.cc.length - 1]) && 873 | (top == maybeoperatorComma || top == maybeoperatorNoComma) && 874 | !/^[,\.=+\-*:?[\(]/.test(textAfter)))) 875 | lexical = lexical.prev; 876 | if (statementIndent && lexical.type == ")" && lexical.prev.type == "stat") 877 | lexical = lexical.prev; 878 | var type = lexical.type, closing = firstChar == type; 879 | 880 | if (type == "vardef") return lexical.indented + (state.lastType == "operator" || state.lastType == "," ? lexical.info.length + 1 : 0); 881 | else if (type == "form" && firstChar == "{") return lexical.indented; 882 | else if (type == "form") return lexical.indented + indentUnit; 883 | else if (type == "stat") 884 | return lexical.indented + (isContinuedStatement(state, textAfter) ? statementIndent || indentUnit : 0); 885 | else if (lexical.info == "switch" && !closing && parserConfig.doubleIndentSwitch != false) 886 | return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2 * indentUnit); 887 | else if (lexical.align) return lexical.column + (closing ? 0 : 1); 888 | else return lexical.indented + (closing ? 0 : indentUnit); 889 | }, 890 | 891 | electricInput: /^\s*(?:case .*?:|default:|\{|\})$/, 892 | blockCommentStart: jsonMode ? null : "/*", 893 | blockCommentEnd: jsonMode ? null : "*/", 894 | blockCommentContinue: jsonMode ? null : " * ", 895 | lineComment: jsonMode ? null : "//", 896 | fold: "brace", 897 | closeBrackets: "()[]{}''\"\"``", 898 | 899 | helperType: jsonMode ? "json" : "javascript", 900 | jsonldMode: jsonldMode, 901 | jsonMode: jsonMode, 902 | 903 | expressionAllowed: expressionAllowed, 904 | 905 | skipExpression: function(state) { 906 | var top = state.cc[state.cc.length - 1] 907 | if (top == expression || top == expressionNoComma) state.cc.pop() 908 | } 909 | }; 910 | }); 911 | 912 | CodeMirror.registerHelper("wordChars", "javascript", /[\w$]/); 913 | 914 | CodeMirror.defineMIME("text/javascript", "javascript"); 915 | CodeMirror.defineMIME("text/ecmascript", "javascript"); 916 | CodeMirror.defineMIME("application/javascript", "javascript"); 917 | CodeMirror.defineMIME("application/x-javascript", "javascript"); 918 | CodeMirror.defineMIME("application/ecmascript", "javascript"); 919 | CodeMirror.defineMIME("application/json", {name: "javascript", json: true}); 920 | CodeMirror.defineMIME("application/x-json", {name: "javascript", json: true}); 921 | CodeMirror.defineMIME("application/ld+json", {name: "javascript", jsonld: true}); 922 | CodeMirror.defineMIME("text/typescript", { name: "javascript", typescript: true }); 923 | CodeMirror.defineMIME("application/typescript", { name: "javascript", typescript: true }); 924 | 925 | }); 926 | --------------------------------------------------------------------------------