[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/vendor/codemirror/addon/edit/ -> matchbrackets.js (source)

   1  // CodeMirror, copyright (c) by Marijn Haverbeke and others
   2  // Distributed under an MIT license: https://codemirror.net/5/LICENSE
   3  
   4  (function(mod) {
   5    if (typeof exports == "object" && typeof module == "object") // CommonJS
   6      mod(require("../../lib/codemirror"));
   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    var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
  13      (document.documentMode == null || document.documentMode < 8);
  14  
  15    var Pos = CodeMirror.Pos;
  16  
  17    var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<", "<": ">>", ">": "<<"};
  18  
  19    function bracketRegex(config) {
  20      return config && config.bracketRegex || /[(){}[\]]/
  21    }
  22  
  23    function findMatchingBracket(cm, where, config) {
  24      var line = cm.getLineHandle(where.line), pos = where.ch - 1;
  25      var afterCursor = config && config.afterCursor
  26      if (afterCursor == null)
  27        afterCursor = /(^| )cm-fat-cursor($| )/.test(cm.getWrapperElement().className)
  28      var re = bracketRegex(config)
  29  
  30      // A cursor is defined as between two characters, but in in vim command mode
  31      // (i.e. not insert mode), the cursor is visually represented as a
  32      // highlighted box on top of the 2nd character. Otherwise, we allow matches
  33      // from before or after the cursor.
  34      var match = (!afterCursor && pos >= 0 && re.test(line.text.charAt(pos)) && matching[line.text.charAt(pos)]) ||
  35          re.test(line.text.charAt(pos + 1)) && matching[line.text.charAt(++pos)];
  36      if (!match) return null;
  37      var dir = match.charAt(1) == ">" ? 1 : -1;
  38      if (config && config.strict && (dir > 0) != (pos == where.ch)) return null;
  39      var style = cm.getTokenTypeAt(Pos(where.line, pos + 1));
  40  
  41      var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style, config);
  42      if (found == null) return null;
  43      return {from: Pos(where.line, pos), to: found && found.pos,
  44              match: found && found.ch == match.charAt(0), forward: dir > 0};
  45    }
  46  
  47    // bracketRegex is used to specify which type of bracket to scan
  48    // should be a regexp, e.g. /[[\]]/
  49    //
  50    // Note: If "where" is on an open bracket, then this bracket is ignored.
  51    //
  52    // Returns false when no bracket was found, null when it reached
  53    // maxScanLines and gave up
  54    function scanForBracket(cm, where, dir, style, config) {
  55      var maxScanLen = (config && config.maxScanLineLength) || 10000;
  56      var maxScanLines = (config && config.maxScanLines) || 1000;
  57  
  58      var stack = [];
  59      var re = bracketRegex(config)
  60      var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
  61                            : Math.max(cm.firstLine() - 1, where.line - maxScanLines);
  62      for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {
  63        var line = cm.getLine(lineNo);
  64        if (!line) continue;
  65        var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;
  66        if (line.length > maxScanLen) continue;
  67        if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);
  68        for (; pos != end; pos += dir) {
  69          var ch = line.charAt(pos);
  70          if (re.test(ch) && (style === undefined ||
  71                              (cm.getTokenTypeAt(Pos(lineNo, pos + 1)) || "") == (style || ""))) {
  72            var match = matching[ch];
  73            if (match && (match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
  74            else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
  75            else stack.pop();
  76          }
  77        }
  78      }
  79      return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
  80    }
  81  
  82    function matchBrackets(cm, autoclear, config) {
  83      // Disable brace matching in long lines, since it'll cause hugely slow updates
  84      var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000,
  85        highlightNonMatching = config && config.highlightNonMatching;
  86      var marks = [], ranges = cm.listSelections();
  87      for (var i = 0; i < ranges.length; i++) {
  88        var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config);
  89        if (match && (match.match || highlightNonMatching !== false) && cm.getLine(match.from.line).length <= maxHighlightLen) {
  90          var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
  91          marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
  92          if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen)
  93            marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
  94        }
  95      }
  96  
  97      if (marks.length) {
  98        // Kludge to work around the IE bug from issue #1193, where text
  99        // input stops going to the textarea whenever this fires.
 100        if (ie_lt8 && cm.state.focused) cm.focus();
 101  
 102        var clear = function() {
 103          cm.operation(function() {
 104            for (var i = 0; i < marks.length; i++) marks[i].clear();
 105          });
 106        };
 107        if (autoclear) setTimeout(clear, 800);
 108        else return clear;
 109      }
 110    }
 111  
 112    function doMatchBrackets(cm) {
 113      cm.operation(function() {
 114        if (cm.state.matchBrackets.currentlyHighlighted) {
 115          cm.state.matchBrackets.currentlyHighlighted();
 116          cm.state.matchBrackets.currentlyHighlighted = null;
 117        }
 118        cm.state.matchBrackets.currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
 119      });
 120    }
 121  
 122    function clearHighlighted(cm) {
 123      if (cm.state.matchBrackets && cm.state.matchBrackets.currentlyHighlighted) {
 124        cm.state.matchBrackets.currentlyHighlighted();
 125        cm.state.matchBrackets.currentlyHighlighted = null;
 126      }
 127    }
 128  
 129    CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
 130      if (old && old != CodeMirror.Init) {
 131        cm.off("cursorActivity", doMatchBrackets);
 132        cm.off("focus", doMatchBrackets)
 133        cm.off("blur", clearHighlighted)
 134        clearHighlighted(cm);
 135      }
 136      if (val) {
 137        cm.state.matchBrackets = typeof val == "object" ? val : {};
 138        cm.on("cursorActivity", doMatchBrackets);
 139        cm.on("focus", doMatchBrackets)
 140        cm.on("blur", clearHighlighted)
 141      }
 142    });
 143  
 144    CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
 145    CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){
 146      // Backwards-compatibility kludge
 147      if (oldConfig || typeof config == "boolean") {
 148        if (!oldConfig) {
 149          config = config ? {strict: true} : null
 150        } else {
 151          oldConfig.strict = config
 152          config = oldConfig
 153        }
 154      }
 155      return findMatchingBracket(this, pos, config)
 156    });
 157    CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
 158      return scanForBracket(this, pos, dir, style, config);
 159    });
 160  });


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer