~ubuntu-branches/ubuntu/lucid/scrapbook/lucid

« back to all changes in this revision

Viewing changes to chrome/scrapbook.jar!/content/scrapbook/highlighter.js

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2009-11-08 13:04:00 UTC
  • mfrom: (1.1.5 upstream) (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20091108130400-qjinjbtq655eflpz
Tags: 1.3.5-1ubuntu1
* Merge from debian testing, Ubuntu remaining changes:
  - debian/control:
    + Set binary package name to scrapbook.
    + Add a transitional package (iceweasel-scrapbook -> scrapbook),
      and a Conflicts to scrapbook against older iceweasel-scrapbook
      installations.
    + Packages that Depend/Recommend/Suggest firefox must alternatively
      Depend/Recommend/Suggest abrowser.
    + Adjust the short description, replace IceWeasel occurrences with
      Firefox.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
var sbHighlighter = {
 
2
 
 
3
        nodePositionInRange : {
 
4
                SINGLE  : 0,
 
5
                START   : 1,
 
6
                MIDDLE  : 2,
 
7
                END             : 3
 
8
        },
 
9
 
 
10
        get PRESET_STYLES()
 
11
        {
 
12
                return [
 
13
                        "",
 
14
                        "background-color: #FFFF99; color: #000000; border: thin dashed #FFCC00;",
 
15
                        "border-bottom: medium solid #33FF33;",
 
16
                        "background-color: #CCFFFF; color: #000000; border: thin solid #0099FF;",
 
17
                        "background-color: #FFFF00; color: #000000;",
 
18
                        "border: medium double #993399;",
 
19
                        "background-color: #EE3311; color: #FFFFFF; font-weight: bold;",
 
20
                        "color: #FF0000; text-decoration: line-through;",
 
21
                        "border-bottom: thin solid #3366FF;",
 
22
                ];
 
23
        },
 
24
 
 
25
        updatePopup : function()
 
26
        {
 
27
                var idx = document.getElementById("ScrapBookHighlighter").getAttribute("color");
 
28
                if (idx < 1 || idx > 4)
 
29
                        idx = 4;
 
30
                document.getElementById("ScrapBookHighlighter" + idx).setAttribute("checked", "true");
 
31
                for ( idx = 4; idx > 0; idx-- )
 
32
                {
 
33
                        var cssText = sbCommonUtils.copyUnicharPref("scrapbook.highlighter.style." + idx, this.PRESET_STYLES[idx]);
 
34
                        this.decorateElement(document.getElementById("ScrapBookHighlighter" + idx), cssText);
 
35
                }
 
36
        },
 
37
 
 
38
        decorateElement : function(aElement, aCssText)
 
39
        {
 
40
                if (aElement.localName == "menuitem")
 
41
                        aElement = document.getAnonymousElementByAttribute(aElement, "class", "menu-iconic-text");
 
42
                aElement.style.cssText = aCssText;
 
43
        },
 
44
 
 
45
        set : function(aWindow, aSelection, aNodeName, aAttributes)
 
46
        {
 
47
                for ( var r = 0; r < aSelection.rangeCount; ++r ) {
 
48
                        var range = aSelection.getRangeAt( r ); 
 
49
                        var doc   = aWindow.document;
 
50
 
 
51
                        var startC      = range.startContainer;
 
52
                        var endC        = range.endContainer;
 
53
                        var sOffset     = range.startOffset;
 
54
                        var eOffset     = range.endOffset;
 
55
 
 
56
                        var sameNode = ( startC == endC );
 
57
 
 
58
                        if ( aNodeName == "a" && !sameNode )
 
59
                        {
 
60
                                alert("ScrapBook ERROR: Can't attach link across tags."); return;
 
61
                        }
 
62
 
 
63
                        if ( ! sameNode || ! this._isTextNode( startC ) ) { 
 
64
 
 
65
                                var nodeWalker 
 
66
                                        = doc.createTreeWalker(
 
67
                                                        range.commonAncestorContainer,
 
68
                                                        NodeFilter.SHOW_TEXT,
 
69
                                                        this._acceptNode,
 
70
                                                        false
 
71
                                          );
 
72
 
 
73
                                nodeWalker.currentNode = startC; 
 
74
 
 
75
                                for ( var txtNode = nodeWalker.nextNode(); 
 
76
                                          txtNode && txtNode != endC; 
 
77
                                          txtNode = nodeWalker.nextNode() 
 
78
                                        ) {
 
79
 
 
80
                                        nodeWalker.currentNode 
 
81
                                                = this._wrapTextNodeWithSpan(
 
82
                                                                doc,
 
83
                                                                txtNode,
 
84
                                                                this._createNode( 
 
85
                                                                        aWindow, 
 
86
                                                                        aNodeName, 
 
87
                                                                        aAttributes, 
 
88
                                                                        this.nodePositionInRange.MIDDLE 
 
89
                                                                )
 
90
                                                ); 
 
91
                                }
 
92
                        }
 
93
 
 
94
                        if ( this._isTextNode( endC ) ) 
 
95
                                endC.splitText( eOffset );
 
96
                        
 
97
                        if ( ! sameNode) 
 
98
                                this._wrapTextNodeWithSpan(
 
99
                                                doc,
 
100
                                                endC,
 
101
                                                this._createNode( 
 
102
                                                        aWindow, 
 
103
                                                        aNodeName, 
 
104
                                                        aAttributes,
 
105
                                                        this.nodePositionInRange.END
 
106
                                                )
 
107
                                ); 
 
108
 
 
109
                        if ( this._isTextNode( startC ) ) { 
 
110
                                var secondHalf = startC.splitText( sOffset );
 
111
                                if ( sameNode ) {
 
112
                                        this._wrapTextNodeWithSpan(
 
113
                                                        doc,
 
114
                                                        secondHalf,
 
115
                                                        this._createNode( 
 
116
                                                                aWindow, 
 
117
                                                                aNodeName, 
 
118
                                                                aAttributes,
 
119
                                                                this.nodePositionInRange.SINGLE
 
120
                                                        )
 
121
                                        );
 
122
                                }
 
123
                                else {
 
124
                                        this._wrapTextNodeWithSpan(
 
125
                                                        doc,
 
126
                                                        secondHalf,
 
127
                                                        this._createNode( 
 
128
                                                                aWindow, 
 
129
                                                                aNodeName, 
 
130
                                                                aAttributes,
 
131
                                                                this.nodePositionInRange.START
 
132
                                                        )
 
133
                                        );
 
134
                                }
 
135
                        } 
 
136
 
 
137
                        range.collapse( true ); 
 
138
 
 
139
                }
 
140
 
 
141
        },
 
142
 
 
143
        _isTextNode : function( aNode ) 
 
144
        { 
 
145
                return aNode.nodeType == aNode.TEXT_NODE; 
 
146
        },
 
147
 
 
148
        _acceptNode : function( aNode ) 
 
149
        {
 
150
                if ( aNode.nodeType == aNode.TEXT_NODE 
 
151
                         && ! ( /[^\t\n\r ]/.test( aNode.nodeValue ) ) 
 
152
                   )
 
153
                        return NodeFilter.FILTER_REJECT;
 
154
 
 
155
                return NodeFilter.FILTER_ACCEPT;
 
156
        },
 
157
 
 
158
        _createNode : function( aWindow, aNodeName, aAttributes, aNodePosInRange )
 
159
        {
 
160
                var newNode = aWindow.document.createElement( aNodeName );
 
161
                for ( var attr in aAttributes )
 
162
                {
 
163
                        newNode.setAttribute( attr, aAttributes[attr] );
 
164
                }
 
165
                return newNode;
 
166
        },
 
167
 
 
168
        _wrapTextNodeWithSpan : function( aDoc, aTextNode, aSpanNode ) 
 
169
        {
 
170
                var clonedTextNode = aTextNode.cloneNode( false );
 
171
                var nodeParent   = aTextNode.parentNode;        
 
172
 
 
173
                aSpanNode.appendChild( clonedTextNode );
 
174
                nodeParent.replaceChild( aSpanNode, aTextNode );
 
175
 
 
176
                return clonedTextNode;
 
177
        },
 
178
 
 
179
};
 
180