openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

spanaffectswrapping_shim.html (3039B)


      1 <!doctype html>
      2 
      3 <title>CodeMirror: Automatically derive odd wrapping behavior for your browser</title>
      4 <meta charset="utf-8"/>
      5 <link rel=stylesheet href="../doc/docs.css">
      6 
      7 <div id=nav>
      8   <a href="http://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
      9 
     10   <ul>
     11     <li><a href="../index.html">Home</a>
     12     <li><a href="../doc/manual.html">Manual</a>
     13     <li><a href="https://github.com/codemirror/codemirror">Code</a>
     14   </ul>
     15   <ul>
     16     <li><a class=active href="#">Automatically derive odd wrapping behavior for your browser</a>
     17   </ul>
     18 </div>
     19 
     20 <article>
     21 <h2>Automatically derive odd wrapping behavior for your browser</h2>
     22 
     23 
     24     <p>This is a hack to automatically derive
     25     a <code>spanAffectsWrapping</code> regexp for a browser. See the
     26     comments above that variable
     27     in <a href="../lib/codemirror.js"><code>lib/codemirror.js</code></a>
     28     for some more details.</p>
     29 
     30     <div style="white-space: pre-wrap; width: 50px;" id="area"></div>
     31     <pre id="output"></pre>
     32 
     33     <script id="script">
     34       var a = document.getElementById("area"), bad = Object.create(null);
     35       var chars = "a~`!@#$%^&*()-_=+}{[]\\|'\"/?.>,<:;", l = chars.length;
     36       for (var x = 0; x < l; ++x) for (var y = 0; y < l; ++y) {
     37         var s1 = "foooo" + chars.charAt(x), s2 = chars.charAt(y) + "br";
     38         a.appendChild(document.createTextNode(s1 + s2));
     39         var h1 = a.offsetHeight;
     40         a.innerHTML = "";
     41         a.appendChild(document.createElement("span")).appendChild(document.createTextNode(s1));
     42         a.appendChild(document.createElement("span")).appendChild(document.createTextNode(s2));
     43         if (a.offsetHeight != h1)
     44           bad[chars.charAt(x)] = (bad[chars.charAt(x)] || "") + chars.charAt(y);
     45         a.innerHTML = "";
     46       }
     47 
     48       var re = "";
     49       function toREElt(str) {
     50         if (str.length > 1) {
     51           var invert = false;
     52           if (str.length > chars.length * .6) {
     53             invert = true;
     54             var newStr = "";
     55             for (var i = 0; i < l; ++i) if (str.indexOf(chars.charAt(i)) == -1) newStr += chars.charAt(i);
     56             str = newStr;
     57           }
     58           str = str.replace(/[\-\.\]\"\'\\\/\^a]/g, function(orig) { return orig == "a" ? "\\w" : "\\" + orig; });
     59           return "[" + (invert ? "^" : "") + str + "]";
     60         } else if (str == "a") {
     61           return "\\w";
     62         } else if (/[?$*()+{}[\]\.|/\'\"]/.test(str)) {
     63           return "\\" + str;
     64         } else {
     65           return str;
     66         }
     67       }
     68 
     69       var newRE = "";
     70       for (;;) {
     71         var left = null;
     72         for (var left in bad) break;
     73         if (left == null) break;
     74         var right = bad[left];
     75         delete bad[left];
     76         for (var other in bad) if (bad[other] == right) {
     77           left += other;
     78           delete bad[other];
     79         }
     80         newRE += (newRE ? "|" : "") + toREElt(left) + toREElt(right);
     81       }
     82 
     83       document.getElementById("output").appendChild(document.createTextNode("Your regexp is: " + (newRE || "^$")));
     84     </script>
     85   </article>