mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #4109 from sphinx-doc/fix/highlight
fix: #4108 - Search word highlight breaks SVG
This commit is contained in:
commit
0116cef451
1
CHANGES
1
CHANGES
@ -23,6 +23,7 @@ Bugs fixed
|
|||||||
* #3987: Changing theme from alabaster causes HTML build to fail
|
* #3987: Changing theme from alabaster causes HTML build to fail
|
||||||
* #4096: C++, don't crash when using the wrong role type. Thanks to mitya57.
|
* #4096: C++, don't crash when using the wrong role type. Thanks to mitya57.
|
||||||
* #4070, #4111: crashes when the warning message contains format strings (again)
|
* #4070, #4111: crashes when the warning message contains format strings (again)
|
||||||
|
* #4108: Search word highlighting breaks SVG images
|
||||||
|
|
||||||
Testing
|
Testing
|
||||||
--------
|
--------
|
||||||
|
@ -445,10 +445,14 @@ dd {
|
|||||||
margin-left: 30px;
|
margin-left: 30px;
|
||||||
}
|
}
|
||||||
|
|
||||||
dt:target, .highlighted {
|
dt:target, span.highlighted {
|
||||||
background-color: #fbe54e;
|
background-color: #fbe54e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rect.highlighted {
|
||||||
|
fill: #fbe54e;
|
||||||
|
}
|
||||||
|
|
||||||
dl.glossary dt {
|
dl.glossary dt {
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
font-size: 1.1em;
|
font-size: 1.1em;
|
||||||
|
@ -45,7 +45,7 @@ jQuery.urlencode = encodeURIComponent;
|
|||||||
* it will always return arrays of strings for the value parts.
|
* it will always return arrays of strings for the value parts.
|
||||||
*/
|
*/
|
||||||
jQuery.getQueryParameters = function(s) {
|
jQuery.getQueryParameters = function(s) {
|
||||||
if (typeof s == 'undefined')
|
if (typeof s === 'undefined')
|
||||||
s = document.location.search;
|
s = document.location.search;
|
||||||
var parts = s.substr(s.indexOf('?') + 1).split('&');
|
var parts = s.substr(s.indexOf('?') + 1).split('&');
|
||||||
var result = {};
|
var result = {};
|
||||||
@ -66,29 +66,53 @@ jQuery.getQueryParameters = function(s) {
|
|||||||
* span elements with the given class name.
|
* span elements with the given class name.
|
||||||
*/
|
*/
|
||||||
jQuery.fn.highlightText = function(text, className) {
|
jQuery.fn.highlightText = function(text, className) {
|
||||||
function highlight(node) {
|
function highlight(node, addItems) {
|
||||||
if (node.nodeType == 3) {
|
if (node.nodeType === 3) {
|
||||||
var val = node.nodeValue;
|
var val = node.nodeValue;
|
||||||
var pos = val.toLowerCase().indexOf(text);
|
var pos = val.toLowerCase().indexOf(text);
|
||||||
if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) {
|
if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) {
|
||||||
var span = document.createElement("span");
|
var span;
|
||||||
span.className = className;
|
var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg");
|
||||||
|
if (isInSVG) {
|
||||||
|
span = document.createElementNS("http://www.w3.org/2000/svg", "tspan");
|
||||||
|
} else {
|
||||||
|
span = document.createElement("span");
|
||||||
|
span.className = className;
|
||||||
|
}
|
||||||
span.appendChild(document.createTextNode(val.substr(pos, text.length)));
|
span.appendChild(document.createTextNode(val.substr(pos, text.length)));
|
||||||
node.parentNode.insertBefore(span, node.parentNode.insertBefore(
|
node.parentNode.insertBefore(span, node.parentNode.insertBefore(
|
||||||
document.createTextNode(val.substr(pos + text.length)),
|
document.createTextNode(val.substr(pos + text.length)),
|
||||||
node.nextSibling));
|
node.nextSibling));
|
||||||
node.nodeValue = val.substr(0, pos);
|
node.nodeValue = val.substr(0, pos);
|
||||||
|
if (isInSVG) {
|
||||||
|
var bbox = span.getBBox();
|
||||||
|
var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
|
||||||
|
rect.x.baseVal.value = bbox.x;
|
||||||
|
rect.y.baseVal.value = bbox.y;
|
||||||
|
rect.width.baseVal.value = bbox.width;
|
||||||
|
rect.height.baseVal.value = bbox.height;
|
||||||
|
rect.setAttribute('class', className);
|
||||||
|
var parentOfText = node.parentNode.parentNode;
|
||||||
|
addItems.push({
|
||||||
|
"parent": node.parentNode,
|
||||||
|
"target": rect});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (!jQuery(node).is("button, select, textarea")) {
|
else if (!jQuery(node).is("button, select, textarea")) {
|
||||||
jQuery.each(node.childNodes, function() {
|
jQuery.each(node.childNodes, function() {
|
||||||
highlight(this);
|
highlight(this, addItems);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this.each(function() {
|
var addItems = [];
|
||||||
highlight(this);
|
var result = this.each(function() {
|
||||||
|
highlight(this, addItems);
|
||||||
});
|
});
|
||||||
|
for (var i = 0; i < addItems.length; ++i) {
|
||||||
|
jQuery(addItems[i].parent).before(addItems[i].target);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -133,21 +157,21 @@ var Documentation = {
|
|||||||
* i18n support
|
* i18n support
|
||||||
*/
|
*/
|
||||||
TRANSLATIONS : {},
|
TRANSLATIONS : {},
|
||||||
PLURAL_EXPR : function(n) { return n == 1 ? 0 : 1; },
|
PLURAL_EXPR : function(n) { return n === 1 ? 0 : 1; },
|
||||||
LOCALE : 'unknown',
|
LOCALE : 'unknown',
|
||||||
|
|
||||||
// gettext and ngettext don't access this so that the functions
|
// gettext and ngettext don't access this so that the functions
|
||||||
// can safely bound to a different name (_ = Documentation.gettext)
|
// can safely bound to a different name (_ = Documentation.gettext)
|
||||||
gettext : function(string) {
|
gettext : function(string) {
|
||||||
var translated = Documentation.TRANSLATIONS[string];
|
var translated = Documentation.TRANSLATIONS[string];
|
||||||
if (typeof translated == 'undefined')
|
if (typeof translated === 'undefined')
|
||||||
return string;
|
return string;
|
||||||
return (typeof translated == 'string') ? translated : translated[0];
|
return (typeof translated === 'string') ? translated : translated[0];
|
||||||
},
|
},
|
||||||
|
|
||||||
ngettext : function(singular, plural, n) {
|
ngettext : function(singular, plural, n) {
|
||||||
var translated = Documentation.TRANSLATIONS[singular];
|
var translated = Documentation.TRANSLATIONS[singular];
|
||||||
if (typeof translated == 'undefined')
|
if (typeof translated === 'undefined')
|
||||||
return (n == 1) ? singular : plural;
|
return (n == 1) ? singular : plural;
|
||||||
return translated[Documentation.PLURALEXPR(n)];
|
return translated[Documentation.PLURALEXPR(n)];
|
||||||
},
|
},
|
||||||
@ -218,7 +242,7 @@ var Documentation = {
|
|||||||
var src = $(this).attr('src');
|
var src = $(this).attr('src');
|
||||||
var idnum = $(this).attr('id').substr(7);
|
var idnum = $(this).attr('id').substr(7);
|
||||||
$('tr.cg-' + idnum).toggle();
|
$('tr.cg-' + idnum).toggle();
|
||||||
if (src.substr(-9) == 'minus.png')
|
if (src.substr(-9) === 'minus.png')
|
||||||
$(this).attr('src', src.substr(0, src.length-9) + 'plus.png');
|
$(this).attr('src', src.substr(0, src.length-9) + 'plus.png');
|
||||||
else
|
else
|
||||||
$(this).attr('src', src.substr(0, src.length-8) + 'minus.png');
|
$(this).attr('src', src.substr(0, src.length-8) + 'minus.png');
|
||||||
@ -250,7 +274,7 @@ var Documentation = {
|
|||||||
var path = document.location.pathname;
|
var path = document.location.pathname;
|
||||||
var parts = path.split(/\//);
|
var parts = path.split(/\//);
|
||||||
$.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() {
|
$.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() {
|
||||||
if (this == '..')
|
if (this === '..')
|
||||||
parts.pop();
|
parts.pop();
|
||||||
});
|
});
|
||||||
var url = parts.join('/');
|
var url = parts.join('/');
|
||||||
|
Loading…
Reference in New Issue
Block a user