5 <title>CodeMirror: Automatically derive odd wrapping behavior for your browser</title>
6 <link rel="stylesheet" href="../doc/docs.css">
9 <h1>CodeMirror: odd wrapping shim</h1>
11 <p>This is a hack to automatically derive
12 a <code>spanAffectsWrapping</code> regexp for a browser. See the
13 comments above that variable
14 in <a href="../lib/codemirror.js"><code>lib/codemirror.js</code></a>
15 for some more details.</p>
17 <div style="white-space: pre-wrap; width: 50px;" id="area"></div>
18 <pre id="output"></pre>
21 var a = document.getElementById("area"), bad = Object.create(null);
22 var chars = "a~`!@#$%^&*()-_=+}{[]\|'\"/?.>,<:;", l = chars.length;
23 for (var x = 0; x < l; ++x) for (var y = 0; y < l; ++y) {
24 var s1 = "foooo" + chars.charAt(x), s2 = chars.charAt(y) + "br";
25 a.appendChild(document.createTextNode(s1 + s2));
26 var h1 = a.offsetHeight;
28 a.appendChild(document.createElement("span")).appendChild(document.createTextNode(s1));
29 a.appendChild(document.createElement("span")).appendChild(document.createTextNode(s2));
30 if (a.offsetHeight != h1)
31 bad[chars.charAt(x)] = (bad[chars.charAt(x)] || "") + chars.charAt(y);
36 function toREElt(str) {
39 if (str.length > chars.length * .6) {
42 for (var i = 0; i < l; ++i) if (str.indexOf(chars.charAt(i)) == -1) newStr += chars.charAt(i);
45 str = str.replace(/[\-\.\]\"\'\\\/\^a]/g, function(orig) { return orig == "a" ? "\\w" : "\\" + orig; });
46 return "[" + (invert ? "^" : "") + str + "]";
47 } else if (str == "a") {
49 } else if (/[?$*()+{}[\]\.|/\'\"]/.test(str)) {
59 for (var left in bad) break;
60 if (left == null) break;
61 var right = bad[left];
63 for (var other in bad) if (bad[other] == right) {
67 newRE += (newRE ? "|" : "") + toREElt(left) + toREElt(right);
70 document.getElementById("output").appendChild(document.createTextNode("Your regexp is: " + (newRE || "^$")));