LaravelTest
459 строк · 11.3 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"), require("../css/css"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror", "../css/css"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14CodeMirror.defineMode("sass", function(config) {15var cssMode = CodeMirror.mimeModes["text/css"];16var propertyKeywords = cssMode.propertyKeywords || {},17colorKeywords = cssMode.colorKeywords || {},18valueKeywords = cssMode.valueKeywords || {},19fontProperties = cssMode.fontProperties || {};20
21function tokenRegexp(words) {22return new RegExp("^" + words.join("|"));23}24
25var keywords = ["true", "false", "null", "auto"];26var keywordsRegexp = new RegExp("^" + keywords.join("|"));27
28var operators = ["\\(", "\\)", "=", ">", "<", "==", ">=", "<=", "\\+", "-",29"\\!=", "/", "\\*", "%", "and", "or", "not", ";","\\{","\\}",":"];30var opRegexp = tokenRegexp(operators);31
32var pseudoElementsRegexp = /^::?[a-zA-Z_][\w\-]*/;33
34var word;35
36function isEndLine(stream) {37return !stream.peek() || stream.match(/\s+$/, false);38}39
40function urlTokens(stream, state) {41var ch = stream.peek();42
43if (ch === ")") {44stream.next();45state.tokenizer = tokenBase;46return "operator";47} else if (ch === "(") {48stream.next();49stream.eatSpace();50
51return "operator";52} else if (ch === "'" || ch === '"') {53state.tokenizer = buildStringTokenizer(stream.next());54return "string";55} else {56state.tokenizer = buildStringTokenizer(")", false);57return "string";58}59}60function comment(indentation, multiLine) {61return function(stream, state) {62if (stream.sol() && stream.indentation() <= indentation) {63state.tokenizer = tokenBase;64return tokenBase(stream, state);65}66
67if (multiLine && stream.skipTo("*/")) {68stream.next();69stream.next();70state.tokenizer = tokenBase;71} else {72stream.skipToEnd();73}74
75return "comment";76};77}78
79function buildStringTokenizer(quote, greedy) {80if (greedy == null) { greedy = true; }81
82function stringTokenizer(stream, state) {83var nextChar = stream.next();84var peekChar = stream.peek();85var previousChar = stream.string.charAt(stream.pos-2);86
87var endingString = ((nextChar !== "\\" && peekChar === quote) || (nextChar === quote && previousChar !== "\\"));88
89if (endingString) {90if (nextChar !== quote && greedy) { stream.next(); }91if (isEndLine(stream)) {92state.cursorHalf = 0;93}94state.tokenizer = tokenBase;95return "string";96} else if (nextChar === "#" && peekChar === "{") {97state.tokenizer = buildInterpolationTokenizer(stringTokenizer);98stream.next();99return "operator";100} else {101return "string";102}103}104
105return stringTokenizer;106}107
108function buildInterpolationTokenizer(currentTokenizer) {109return function(stream, state) {110if (stream.peek() === "}") {111stream.next();112state.tokenizer = currentTokenizer;113return "operator";114} else {115return tokenBase(stream, state);116}117};118}119
120function indent(state) {121if (state.indentCount == 0) {122state.indentCount++;123var lastScopeOffset = state.scopes[0].offset;124var currentOffset = lastScopeOffset + config.indentUnit;125state.scopes.unshift({ offset:currentOffset });126}127}128
129function dedent(state) {130if (state.scopes.length == 1) return;131
132state.scopes.shift();133}134
135function tokenBase(stream, state) {136var ch = stream.peek();137
138// Comment139if (stream.match("/*")) {140state.tokenizer = comment(stream.indentation(), true);141return state.tokenizer(stream, state);142}143if (stream.match("//")) {144state.tokenizer = comment(stream.indentation(), false);145return state.tokenizer(stream, state);146}147
148// Interpolation149if (stream.match("#{")) {150state.tokenizer = buildInterpolationTokenizer(tokenBase);151return "operator";152}153
154// Strings155if (ch === '"' || ch === "'") {156stream.next();157state.tokenizer = buildStringTokenizer(ch);158return "string";159}160
161if(!state.cursorHalf){// state.cursorHalf === 0162// first half i.e. before : for key-value pairs163// including selectors164
165if (ch === "-") {166if (stream.match(/^-\w+-/)) {167return "meta";168}169}170
171if (ch === ".") {172stream.next();173if (stream.match(/^[\w-]+/)) {174indent(state);175return "qualifier";176} else if (stream.peek() === "#") {177indent(state);178return "tag";179}180}181
182if (ch === "#") {183stream.next();184// ID selectors185if (stream.match(/^[\w-]+/)) {186indent(state);187return "builtin";188}189if (stream.peek() === "#") {190indent(state);191return "tag";192}193}194
195// Variables196if (ch === "$") {197stream.next();198stream.eatWhile(/[\w-]/);199return "variable-2";200}201
202// Numbers203if (stream.match(/^-?[0-9\.]+/))204return "number";205
206// Units207if (stream.match(/^(px|em|in)\b/))208return "unit";209
210if (stream.match(keywordsRegexp))211return "keyword";212
213if (stream.match(/^url/) && stream.peek() === "(") {214state.tokenizer = urlTokens;215return "atom";216}217
218if (ch === "=") {219// Match shortcut mixin definition220if (stream.match(/^=[\w-]+/)) {221indent(state);222return "meta";223}224}225
226if (ch === "+") {227// Match shortcut mixin definition228if (stream.match(/^\+[\w-]+/)){229return "variable-3";230}231}232
233if(ch === "@"){234if(stream.match('@extend')){235if(!stream.match(/\s*[\w]/))236dedent(state);237}238}239
240
241// Indent Directives242if (stream.match(/^@(else if|if|media|else|for|each|while|mixin|function)/)) {243indent(state);244return "def";245}246
247// Other Directives248if (ch === "@") {249stream.next();250stream.eatWhile(/[\w-]/);251return "def";252}253
254if (stream.eatWhile(/[\w-]/)){255if(stream.match(/ *: *[\w-\+\$#!\("']/,false)){256word = stream.current().toLowerCase();257var prop = state.prevProp + "-" + word;258if (propertyKeywords.hasOwnProperty(prop)) {259return "property";260} else if (propertyKeywords.hasOwnProperty(word)) {261state.prevProp = word;262return "property";263} else if (fontProperties.hasOwnProperty(word)) {264return "property";265}266return "tag";267}268else if(stream.match(/ *:/,false)){269indent(state);270state.cursorHalf = 1;271state.prevProp = stream.current().toLowerCase();272return "property";273}274else if(stream.match(/ *,/,false)){275return "tag";276}277else{278indent(state);279return "tag";280}281}282
283if(ch === ":"){284if (stream.match(pseudoElementsRegexp)){ // could be a pseudo-element285return "variable-3";286}287stream.next();288state.cursorHalf=1;289return "operator";290}291
292} // cursorHalf===0 ends here293else{294
295if (ch === "#") {296stream.next();297// Hex numbers298if (stream.match(/[0-9a-fA-F]{6}|[0-9a-fA-F]{3}/)){299if (isEndLine(stream)) {300state.cursorHalf = 0;301}302return "number";303}304}305
306// Numbers307if (stream.match(/^-?[0-9\.]+/)){308if (isEndLine(stream)) {309state.cursorHalf = 0;310}311return "number";312}313
314// Units315if (stream.match(/^(px|em|in)\b/)){316if (isEndLine(stream)) {317state.cursorHalf = 0;318}319return "unit";320}321
322if (stream.match(keywordsRegexp)){323if (isEndLine(stream)) {324state.cursorHalf = 0;325}326return "keyword";327}328
329if (stream.match(/^url/) && stream.peek() === "(") {330state.tokenizer = urlTokens;331if (isEndLine(stream)) {332state.cursorHalf = 0;333}334return "atom";335}336
337// Variables338if (ch === "$") {339stream.next();340stream.eatWhile(/[\w-]/);341if (isEndLine(stream)) {342state.cursorHalf = 0;343}344return "variable-2";345}346
347// bang character for !important, !default, etc.348if (ch === "!") {349stream.next();350state.cursorHalf = 0;351return stream.match(/^[\w]+/) ? "keyword": "operator";352}353
354if (stream.match(opRegexp)){355if (isEndLine(stream)) {356state.cursorHalf = 0;357}358return "operator";359}360
361// attributes362if (stream.eatWhile(/[\w-]/)) {363if (isEndLine(stream)) {364state.cursorHalf = 0;365}366word = stream.current().toLowerCase();367if (valueKeywords.hasOwnProperty(word)) {368return "atom";369} else if (colorKeywords.hasOwnProperty(word)) {370return "keyword";371} else if (propertyKeywords.hasOwnProperty(word)) {372state.prevProp = stream.current().toLowerCase();373return "property";374} else {375return "tag";376}377}378
379//stream.eatSpace();380if (isEndLine(stream)) {381state.cursorHalf = 0;382return null;383}384
385} // else ends here386
387if (stream.match(opRegexp))388return "operator";389
390// If we haven't returned by now, we move 1 character391// and return an error392stream.next();393return null;394}395
396function tokenLexer(stream, state) {397if (stream.sol()) state.indentCount = 0;398var style = state.tokenizer(stream, state);399var current = stream.current();400
401if (current === "@return" || current === "}"){402dedent(state);403}404
405if (style !== null) {406var startOfToken = stream.pos - current.length;407
408var withCurrentIndent = startOfToken + (config.indentUnit * state.indentCount);409
410var newScopes = [];411
412for (var i = 0; i < state.scopes.length; i++) {413var scope = state.scopes[i];414
415if (scope.offset <= withCurrentIndent)416newScopes.push(scope);417}418
419state.scopes = newScopes;420}421
422
423return style;424}425
426return {427startState: function() {428return {429tokenizer: tokenBase,430scopes: [{offset: 0, type: "sass"}],431indentCount: 0,432cursorHalf: 0, // cursor half tells us if cursor lies after (1)433// or before (0) colon (well... more or less)434definedVars: [],435definedMixins: []436};437},438token: function(stream, state) {439var style = tokenLexer(stream, state);440
441state.lastToken = { style: style, content: stream.current() };442
443return style;444},445
446indent: function(state) {447return state.scopes[0].offset;448},449
450blockCommentStart: "/*",451blockCommentEnd: "*/",452lineComment: "//",453fold: "indent"454};455}, "css");456
457CodeMirror.defineMIME("text/x-sass", "sass");458
459});460