LaravelTest
184 строки · 6.5 Кб
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"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14var Pos = CodeMirror.Pos;15function cmp(a, b) { return a.line - b.line || a.ch - b.ch; }16
17var nameStartChar = "A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";18var nameChar = nameStartChar + "\-\:\.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040";19var xmlTagStart = new RegExp("<(/?)([" + nameStartChar + "][" + nameChar + "]*)", "g");20
21function Iter(cm, line, ch, range) {22this.line = line; this.ch = ch;23this.cm = cm; this.text = cm.getLine(line);24this.min = range ? Math.max(range.from, cm.firstLine()) : cm.firstLine();25this.max = range ? Math.min(range.to - 1, cm.lastLine()) : cm.lastLine();26}27
28function tagAt(iter, ch) {29var type = iter.cm.getTokenTypeAt(Pos(iter.line, ch));30return type && /\btag\b/.test(type);31}32
33function nextLine(iter) {34if (iter.line >= iter.max) return;35iter.ch = 0;36iter.text = iter.cm.getLine(++iter.line);37return true;38}39function prevLine(iter) {40if (iter.line <= iter.min) return;41iter.text = iter.cm.getLine(--iter.line);42iter.ch = iter.text.length;43return true;44}45
46function toTagEnd(iter) {47for (;;) {48var gt = iter.text.indexOf(">", iter.ch);49if (gt == -1) { if (nextLine(iter)) continue; else return; }50if (!tagAt(iter, gt + 1)) { iter.ch = gt + 1; continue; }51var lastSlash = iter.text.lastIndexOf("/", gt);52var selfClose = lastSlash > -1 && !/\S/.test(iter.text.slice(lastSlash + 1, gt));53iter.ch = gt + 1;54return selfClose ? "selfClose" : "regular";55}56}57function toTagStart(iter) {58for (;;) {59var lt = iter.ch ? iter.text.lastIndexOf("<", iter.ch - 1) : -1;60if (lt == -1) { if (prevLine(iter)) continue; else return; }61if (!tagAt(iter, lt + 1)) { iter.ch = lt; continue; }62xmlTagStart.lastIndex = lt;63iter.ch = lt;64var match = xmlTagStart.exec(iter.text);65if (match && match.index == lt) return match;66}67}68
69function toNextTag(iter) {70for (;;) {71xmlTagStart.lastIndex = iter.ch;72var found = xmlTagStart.exec(iter.text);73if (!found) { if (nextLine(iter)) continue; else return; }74if (!tagAt(iter, found.index + 1)) { iter.ch = found.index + 1; continue; }75iter.ch = found.index + found[0].length;76return found;77}78}79function toPrevTag(iter) {80for (;;) {81var gt = iter.ch ? iter.text.lastIndexOf(">", iter.ch - 1) : -1;82if (gt == -1) { if (prevLine(iter)) continue; else return; }83if (!tagAt(iter, gt + 1)) { iter.ch = gt; continue; }84var lastSlash = iter.text.lastIndexOf("/", gt);85var selfClose = lastSlash > -1 && !/\S/.test(iter.text.slice(lastSlash + 1, gt));86iter.ch = gt + 1;87return selfClose ? "selfClose" : "regular";88}89}90
91function findMatchingClose(iter, tag) {92var stack = [];93for (;;) {94var next = toNextTag(iter), end, startLine = iter.line, startCh = iter.ch - (next ? next[0].length : 0);95if (!next || !(end = toTagEnd(iter))) return;96if (end == "selfClose") continue;97if (next[1]) { // closing tag98for (var i = stack.length - 1; i >= 0; --i) if (stack[i] == next[2]) {99stack.length = i;100break;101}102if (i < 0 && (!tag || tag == next[2])) return {103tag: next[2],104from: Pos(startLine, startCh),105to: Pos(iter.line, iter.ch)106};107} else { // opening tag108stack.push(next[2]);109}110}111}112function findMatchingOpen(iter, tag) {113var stack = [];114for (;;) {115var prev = toPrevTag(iter);116if (!prev) return;117if (prev == "selfClose") { toTagStart(iter); continue; }118var endLine = iter.line, endCh = iter.ch;119var start = toTagStart(iter);120if (!start) return;121if (start[1]) { // closing tag122stack.push(start[2]);123} else { // opening tag124for (var i = stack.length - 1; i >= 0; --i) if (stack[i] == start[2]) {125stack.length = i;126break;127}128if (i < 0 && (!tag || tag == start[2])) return {129tag: start[2],130from: Pos(iter.line, iter.ch),131to: Pos(endLine, endCh)132};133}134}135}136
137CodeMirror.registerHelper("fold", "xml", function(cm, start) {138var iter = new Iter(cm, start.line, 0);139for (;;) {140var openTag = toNextTag(iter)141if (!openTag || iter.line != start.line) return142var end = toTagEnd(iter)143if (!end) return144if (!openTag[1] && end != "selfClose") {145var startPos = Pos(iter.line, iter.ch);146var endPos = findMatchingClose(iter, openTag[2]);147return endPos && cmp(endPos.from, startPos) > 0 ? {from: startPos, to: endPos.from} : null148}149}150});151CodeMirror.findMatchingTag = function(cm, pos, range) {152var iter = new Iter(cm, pos.line, pos.ch, range);153if (iter.text.indexOf(">") == -1 && iter.text.indexOf("<") == -1) return;154var end = toTagEnd(iter), to = end && Pos(iter.line, iter.ch);155var start = end && toTagStart(iter);156if (!end || !start || cmp(iter, pos) > 0) return;157var here = {from: Pos(iter.line, iter.ch), to: to, tag: start[2]};158if (end == "selfClose") return {open: here, close: null, at: "open"};159
160if (start[1]) { // closing tag161return {open: findMatchingOpen(iter, start[2]), close: here, at: "close"};162} else { // opening tag163iter = new Iter(cm, to.line, to.ch, range);164return {open: here, close: findMatchingClose(iter, start[2]), at: "open"};165}166};167
168CodeMirror.findEnclosingTag = function(cm, pos, range, tag) {169var iter = new Iter(cm, pos.line, pos.ch, range);170for (;;) {171var open = findMatchingOpen(iter, tag);172if (!open) break;173var forward = new Iter(cm, pos.line, pos.ch, range);174var close = findMatchingClose(forward, open.tag);175if (close) return {open: open, close: close};176}177};178
179// Used by addon/edit/closetag.js180CodeMirror.scanForClosingTag = function(cm, pos, name, end) {181var iter = new Iter(cm, pos.line, pos.ch, end ? {from: 0, to: end} : null);182return findMatchingClose(iter, name);183};184});185