Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/epubview/highlight_words.js
blob: ffa5e9af3624b2cc0d290faf2ecefab23c8e28de (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
    var parentElement;
    var actualChild;
    var actualWord;
    var words;
    var originalNode = null;
    var modifiedNode = null;

    function trim(s) { 
      s =  ( s || '' ).replace( /^\s+|\s+$/g, '' ); 
      return s.replace(/[\n\r\t]/g,' ');
    }


    function init() {
        parentElement = document.getElementsByTagName("body")[0];
        actualChild = new Array();
        actualWord = 0;
        actualChild.push(0);
    }

    function highLightNextWordInt() {
        var nodeList = parentElement.childNodes;
        ini_posi = actualChild[actualChild.length - 1];
        for (var i=ini_posi; i < nodeList.length; i++) {
            var node = nodeList[i];            
            if ((node.nodeName == "#text") && (trim(node.nodeValue) != '')) {
                node_text =  trim(node.nodeValue);
                words = node_text.split(" ");
                if (actualWord < words.length) {
                    originalNode = document.createTextNode(node.nodeValue);
                    
                    prev_text = '';
                    for (var p1 = 0; p1 < actualWord; p1++) {
                        prev_text = prev_text + words[p1] + " ";
                    }
                    var textNode1 = document.createTextNode(prev_text);
                    var textNode2 = document.createTextNode(words[actualWord]+" ");
                    post_text = '';
                    for (var p2 = actualWord + 1; p2 < words.length; p2++) {
                        post_text = post_text + words[p2] + " ";
                    }
                    var textNode3 = document.createTextNode(post_text);
                    var newParagraph = document.createElement('p');
                    var boldNode = document.createElement('b');
                    boldNode.appendChild(textNode2);
                    newParagraph.appendChild(textNode1);    
                    newParagraph.appendChild(boldNode);    
                    newParagraph.appendChild(textNode3);    

                    parentElement.insertBefore(newParagraph, node);
                    parentElement.removeChild(node);
                    modifiedNode = newParagraph;

                    actualWord = actualWord + 1;
                    if (actualWord >=  words.length) {
                        actualChild.pop();
                        actualChild[actualChild.length - 1] = actualChild[actualChild.length - 1] + 2;
                        actualWord = 0;
                        parentElement = parentElement.parentNode;
                    }
                }    
                throw "exit"; 
            } else {
                if (node.childNodes.length > 0) {
                    parentElement = node;
                    actualChild.push(0);
                    actualWord = 0;
                    highLightNextWordInt();
                    actualChild.pop();
                }
            }
        }
        return;
    }


    function highLightNextWord() {
        if (typeof parentElement  == "undefined") {
            init();
        }
        if (originalNode != null) {
            modifiedNode.parentNode.insertBefore(originalNode, modifiedNode);
            modifiedNode.parentNode.removeChild(modifiedNode);
        } 
        try {
            highLightNextWordInt();
        } catch(er) {
        }
    }