You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.0 KiB

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