Source code

Revision control

Line Code
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->


<bindings id="textBindings"
   xmlns="http://www.mozilla.org/xbl"
   xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
   xmlns:html="http://www.w3.org/1999/xhtml">

  <binding id="label-control">
    <content>
      <children/><html:span anonid="accessKeyParens"></html:span>
    </content>
    <implementation>
      <constructor>
        <![CDATA[
          this.formatAccessKey(true);
        ]]>
      </constructor>

      <method name="formatAccessKey">
        <parameter name="firstTime"/>
        <body>
          <![CDATA[
            var control = this.labeledControlElement;
            if (!control) {
              var bindingParent = document.getBindingParent(this);
              if ("accessKey" in bindingParent) {
                control = bindingParent; // For controls that make the <label> an anon child
              }
            }
            if (control) {
              control.labelElement = this;
              var controlAccessKey = control.getAttribute("accesskey");
              if (controlAccessKey) {
                this.setAttribute("accesskey", controlAccessKey);
              }
            }

            var accessKey = this.accessKey;
            // No need to remove existing formatting the first time.
            if (firstTime && !accessKey)
              return;

            if (this.mInsertSeparator === undefined) {
              try {
                var prefs = Cc["@mozilla.org/preferences-service;1"].
                                       getService(Ci.nsIPrefBranch);
                this.mUnderlineAccesskey = (prefs.getIntPref("ui.key.menuAccessKey") != 0);

                const nsIPrefLocalizedString =
                  Ci.nsIPrefLocalizedString;

                const prefNameInsertSeparator =
                  "intl.menuitems.insertseparatorbeforeaccesskeys";
                const prefNameAlwaysAppendAccessKey =
                  "intl.menuitems.alwaysappendaccesskeys";

                var val = prefs.getComplexValue(prefNameInsertSeparator,
                                                nsIPrefLocalizedString).data;
                this.mInsertSeparator = (val == "true");

                val = prefs.getComplexValue(prefNameAlwaysAppendAccessKey,
                                            nsIPrefLocalizedString).data;
                this.mAlwaysAppendAccessKey = (val == "true");
              } catch (e) {
                this.mInsertSeparator = true;
              }
            }

            if (!this.mUnderlineAccesskey)
              return;

            var afterLabel = document.getAnonymousElementByAttribute(this, "anonid", "accessKeyParens");
            afterLabel.textContent = "";

            var oldAccessKey = this.getElementsByAttribute("class", "accesskey").item(0);
            if (oldAccessKey) { // Clear old accesskey
              this.mergeElement(oldAccessKey);
            }

            var oldHiddenSpan =
              this.getElementsByAttribute("class", "hiddenColon").item(0);
            if (oldHiddenSpan) {
              this.mergeElement(oldHiddenSpan);
            }

            var labelText = this.textContent;
            if (!accessKey || !labelText || !control) {
              return;
            }
            var accessKeyIndex = -1;
            if (!this.mAlwaysAppendAccessKey) {
              accessKeyIndex = labelText.indexOf(accessKey);
              if (accessKeyIndex < 0) { // Try again in upper case
                accessKeyIndex =
                  labelText.toUpperCase().indexOf(accessKey.toUpperCase());
              }
            } else if (labelText.endsWith(`(${accessKey.toUpperCase()})`)) {
              accessKeyIndex = labelText.length - (1 + accessKey.length); // = index of accessKey.
            }

            const HTML_NS = "http://www.w3.org/1999/xhtml";
            var span = document.createElementNS(HTML_NS, "span");
            span.className = "accesskey";

            // Note that if you change the following code, see the comment of
            // nsTextBoxFrame::UpdateAccessTitle.

            // If accesskey is not in string, append in parentheses
            if (accessKeyIndex < 0) {
              // If end is colon, we should insert before colon.
              // i.e., "label:" -> "label(X):"
              var colonHidden = false;
              if (/:$/.test(labelText)) {
                labelText = labelText.slice(0, -1);
                var hiddenSpan = document.createElementNS(HTML_NS, "span");
                hiddenSpan.className = "hiddenColon";
                hiddenSpan.style.display = "none";
                // Hide the last colon by using span element.
                // I.e., label<span style="display:none;">:</span>
                this.wrapChar(hiddenSpan, labelText.length);
                colonHidden = true;
              }
              // If end is space(U+20),
              // we should not add space before parentheses.
              var endIsSpace = false;
              if (/ $/.test(labelText)) {
                endIsSpace = true;
              }
              if (this.mInsertSeparator && !endIsSpace)
                afterLabel.textContent = " (";
              else
                afterLabel.textContent = "(";
              span.textContent = accessKey.toUpperCase();
              afterLabel.appendChild(span);
              if (!colonHidden)
                afterLabel.appendChild(document.createTextNode(")"));
              else
                afterLabel.appendChild(document.createTextNode("):"));
              return;
            }
            this.wrapChar(span, accessKeyIndex);
          ]]>
        </body>
      </method>

      <method name="wrapChar">
        <parameter name="element"/>
        <parameter name="index"/>
        <body>
          <![CDATA[
             var treeWalker = document.createTreeWalker(this,
                                                        NodeFilter.SHOW_TEXT,
                                                        null);
             var node = treeWalker.nextNode();
             while (index >= node.length) {
               index -= node.length;
               node = treeWalker.nextNode();
             }
             if (index) {
               node = node.splitText(index);
             }
             node.parentNode.insertBefore(element, node);
             if (node.length > 1) {
               node.splitText(1);
             }
             element.appendChild(node);
          ]]>
        </body>
      </method>

      <method name="mergeElement">
        <parameter name="element"/>
        <body>
          <![CDATA[
            if (element.previousSibling instanceof Text) {
              element.previousSibling.appendData(element.textContent);
            } else {
              element.parentNode.insertBefore(element.firstChild, element);
            }
            element.remove();
          ]]>
        </body>
      </method>

      <field name="mUnderlineAccesskey">
        !/Mac/.test(navigator.platform)
      </field>
      <field name="mInsertSeparator"/>
      <field name="mAlwaysAppendAccessKey">false</field>

      <property name="accessKey">
        <getter>
          <![CDATA[
            var accessKey = this.getAttribute("accesskey");
            return accessKey ? accessKey[0] : null;
          ]]>
        </getter>
        <setter>
          <![CDATA[
            // If this label already has an accesskey attribute store it here as well
            if (this.hasAttribute("accesskey")) {
              this.setAttribute("accesskey", val);
            }
            var control = this.labeledControlElement;
            if (control) {
              control.setAttribute("accesskey", val);
            }
            this.formatAccessKey(false);
            return val;
          ]]>
        </setter>
      </property>

      <property name="labeledControlElement" readonly="true"
                onget="var control = this.control; return control ? document.getElementById(control) : null;" />

      <property name="control" onget="return this.getAttribute('control');">
        <setter>
          <![CDATA[
            var control = this.labeledControlElement;
            if (control) {
              control.labelElement = null; // No longer pointed to be this label
            }
            this.setAttribute("control", val);
            this.formatAccessKey(false);
            return val;
          ]]>
        </setter>
      </property>
    </implementation>

    <handlers>
      <handler event="click"><![CDATA[
        if (this.disabled) {
          return;
        }
        var controlElement = this.labeledControlElement;
        if (!controlElement) {
          return;
        }
        controlElement.focus();
        const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

        if (controlElement.namespaceURI != XUL_NS) {
          return;
        }
        if (controlElement.localName == "checkbox") {
          controlElement.checked = !controlElement.checked;
        } else if (controlElement.localName == "radio") {
          controlElement.control.selectedItem = controlElement;
        }
      ]]></handler>
    </handlers>
  </binding>

  <binding id="text-link">
    <implementation>
      <property name="href" onget="return this.getAttribute('href');"
                            onset="this.setAttribute('href', val); return val;" />
      <method name="open">
        <parameter name="aEvent"/>
        <body>
        <![CDATA[
          var href = this.href;
          if (!href || this.disabled || aEvent.defaultPrevented)
            return;

          var uri = null;
          try {
            const nsISSM = Ci.nsIScriptSecurityManager;
            const secMan =
                     Cc["@mozilla.org/scriptsecuritymanager;1"]
                       .getService(nsISSM);

            const ioService =
                     Cc["@mozilla.org/network/io-service;1"]
                       .getService(Ci.nsIIOService);

            uri = ioService.newURI(href);

            let principal;
            if (this.getAttribute("useoriginprincipal") == "true") {
              principal = this.nodePrincipal;
            } else {
              principal = secMan.createNullPrincipal({});
            }
            try {
              secMan.checkLoadURIWithPrincipal(principal, uri,
                                               nsISSM.DISALLOW_INHERIT_PRINCIPAL);
            } catch (ex) {
              var msg = "Error: Cannot open a " + uri.scheme + ": link using \
                         the text-link binding.";
              Cu.reportError(msg);
              return;
            }

            const cID = "@mozilla.org/uriloader/external-protocol-service;1";
            const nsIEPS = Ci.nsIExternalProtocolService;
            var protocolSvc = Cc[cID].getService(nsIEPS);

            // if the scheme is not an exposed protocol, then opening this link
            // should be deferred to the system's external protocol handler
            if (!protocolSvc.isExposedProtocol(uri.scheme)) {
              protocolSvc.loadURI(uri);
              aEvent.preventDefault();
              return;
            }

          } catch (ex) {
            Cu.reportError(ex);
          }

          aEvent.preventDefault();
          href = uri ? uri.spec : href;

          // Try handing off the link to the host application, e.g. for
          // opening it in a tabbed browser.
          var linkHandled = Cc["@mozilla.org/supports-PRBool;1"]
                              .createInstance(Ci.nsISupportsPRBool);
          linkHandled.data = false;
          let {shiftKey, ctrlKey, metaKey, altKey, button} = aEvent;
          let data = {shiftKey, ctrlKey, metaKey, altKey, button, href};
          Cc["@mozilla.org/observer-service;1"]
            .getService(Ci.nsIObserverService)
            .notifyObservers(linkHandled, "handle-xul-text-link", JSON.stringify(data));
          if (linkHandled.data)
            return;

          // otherwise, fall back to opening the anchor directly
          var win = window;
          if (window.isChromeWindow) {
            while (win.opener && !win.opener.closed)
              win = win.opener;
          }
          win.open(href);
        ]]>
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="click" phase="capturing" button="0" action="this.open(event)"/>
      <handler event="click" phase="capturing" button="1" action="this.open(event)"/>
      <handler event="keypress" preventdefault="true" keycode="VK_RETURN" action="this.click()" />
    </handlers>
  </binding>

</bindings>