summaryrefslogtreecommitdiffstats
path: root/web/server/h2o/libh2o/misc/oktavia/templates/jsdoc3/static/scripts/oktavia-jquery-highlight.js
blob: 519e8876c91f28ef0f05ed44f6f7b18f13c1ddda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
 * @fileOverview
 * A UI script helper that provides search word highlight.
 * Almost all code came from Sphinx
 * @author Yoshiki Shibukawa, yoshiki@shibu.jp
 */

(function ($)
{
    /**
     * small helper function to urldecode strings
     */
    function urldecode(x)
    {
        return decodeURIComponent(x).replace(/\+/g, ' ');
    }

    /**
     * This function returns the parsed url parameters of the
     * current request. Multiple values per key are supported,
     * it will always return arrays of strings for the value parts.
     */
    function getQueryParameters(s)
    {
        if (typeof s == 'undefined')
        s = document.location.search;
        var parts = s.substr(s.indexOf('?') + 1).split('&');
        var result = {};
        for (var i = 0; i < parts.length; i++)
        {
            var tmp = parts[i].split('=', 2);
            var key = urldecode(tmp[0]);
            var value = urldecode(tmp[1]);
            if (key in result)
            {
                result[key].push(value);
            }
            else
            {
                result[key] = [value];
            }
        }
        return result;
    }

    /**
     * highlight a given string on a jquery object by wrapping it in
     * span elements with the given class name.
     */
    jQuery.fn.highlightText = function(text, className)
    {
        function highlight(node)
        {
            if (node.nodeType == 3)
            {
                var val = node.nodeValue;
                var pos = val.toLowerCase().indexOf(text);
                if (pos >= 0 && !jQuery(node.parentNode).hasClass(className))
                {
                    var span = document.createElement("span");
                    span.className = className;
                    span.appendChild(document.createTextNode(val.substr(pos, text.length)));
                    node.parentNode.insertBefore(span, node.parentNode.insertBefore(
                        document.createTextNode(val.substr(pos + text.length)),
                        node.nextSibling));
                    node.nodeValue = val.substr(0, pos);
                }
            }
            else if (!jQuery(node).is("button, select, textarea"))
            {
                jQuery.each(node.childNodes, function() {
                    highlight(this);
                });
            }
        }
        return this.each(function() {
            highlight(this);
        });
    };

    /**
     * highlight the search words provided in the url in the text
     */
    function highlightSearchWords(selector)
    {
        var params = getQueryParameters();
        var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : [];
        if (terms.length)
        {
            var body = $(selector);
            window.setTimeout(function()
            {
                $.each(terms, function()
                {
                    body.highlightText(this.toLowerCase(), 'highlighted');
                });
            }, 10);
        }
    }

    jQuery(document).ready(function () {
        highlightSearchWords('body');
    });
})(jQuery);