_sphinx_javascript_frameworks_compat.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * _sphinx_javascript_frameworks_compat.js
  3. * ~~~~~~~~~~
  4. *
  5. * Compatability shim for jQuery and underscores.js.
  6. *
  7. * WILL BE REMOVED IN Sphinx 6.0
  8. * xref RemovedInSphinx60Warning
  9. *
  10. */
  11. /**
  12. * select a different prefix for underscore
  13. */
  14. $u = _.noConflict();
  15. /**
  16. * small helper function to urldecode strings
  17. *
  18. * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL
  19. */
  20. jQuery.urldecode = function(x) {
  21. if (!x) {
  22. return x
  23. }
  24. return decodeURIComponent(x.replace(/\+/g, ' '));
  25. };
  26. /**
  27. * small helper function to urlencode strings
  28. */
  29. jQuery.urlencode = encodeURIComponent;
  30. /**
  31. * This function returns the parsed url parameters of the
  32. * current request. Multiple values per key are supported,
  33. * it will always return arrays of strings for the value parts.
  34. */
  35. jQuery.getQueryParameters = function(s) {
  36. if (typeof s === 'undefined')
  37. s = document.location.search;
  38. var parts = s.substr(s.indexOf('?') + 1).split('&');
  39. var result = {};
  40. for (var i = 0; i < parts.length; i++) {
  41. var tmp = parts[i].split('=', 2);
  42. var key = jQuery.urldecode(tmp[0]);
  43. var value = jQuery.urldecode(tmp[1]);
  44. if (key in result)
  45. result[key].push(value);
  46. else
  47. result[key] = [value];
  48. }
  49. return result;
  50. };
  51. /**
  52. * highlight a given string on a jquery object by wrapping it in
  53. * span elements with the given class name.
  54. */
  55. jQuery.fn.highlightText = function(text, className) {
  56. function highlight(node, addItems) {
  57. if (node.nodeType === 3) {
  58. var val = node.nodeValue;
  59. var pos = val.toLowerCase().indexOf(text);
  60. if (pos >= 0 &&
  61. !jQuery(node.parentNode).hasClass(className) &&
  62. !jQuery(node.parentNode).hasClass("nohighlight")) {
  63. var span;
  64. var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg");
  65. if (isInSVG) {
  66. span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
  67. } else {
  68. span = document.createElement("span");
  69. span.className = className;
  70. }
  71. span.appendChild(document.createTextNode(val.substr(pos, text.length)));
  72. node.parentNode.insertBefore(span, node.parentNode.insertBefore(
  73. document.createTextNode(val.substr(pos + text.length)),
  74. node.nextSibling));
  75. node.nodeValue = val.substr(0, pos);
  76. if (isInSVG) {
  77. var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
  78. var bbox = node.parentElement.getBBox();
  79. rect.x.baseVal.value = bbox.x;
  80. rect.y.baseVal.value = bbox.y;
  81. rect.width.baseVal.value = bbox.width;
  82. rect.height.baseVal.value = bbox.height;
  83. rect.setAttribute('class', className);
  84. addItems.push({
  85. "parent": node.parentNode,
  86. "target": rect});
  87. }
  88. }
  89. }
  90. else if (!jQuery(node).is("button, select, textarea")) {
  91. jQuery.each(node.childNodes, function() {
  92. highlight(this, addItems);
  93. });
  94. }
  95. }
  96. var addItems = [];
  97. var result = this.each(function() {
  98. highlight(this, addItems);
  99. });
  100. for (var i = 0; i < addItems.length; ++i) {
  101. jQuery(addItems[i].parent).before(addItems[i].target);
  102. }
  103. return result;
  104. };
  105. /*
  106. * backward compatibility for jQuery.browser
  107. * This will be supported until firefox bug is fixed.
  108. */
  109. if (!jQuery.browser) {
  110. jQuery.uaMatch = function(ua) {
  111. ua = ua.toLowerCase();
  112. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  113. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  114. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  115. /(msie) ([\w.]+)/.exec(ua) ||
  116. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  117. [];
  118. return {
  119. browser: match[ 1 ] || "",
  120. version: match[ 2 ] || "0"
  121. };
  122. };
  123. jQuery.browser = {};
  124. jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true;
  125. }