_sphinx_javascript_frameworks_compat.js 4.2 KB

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