summaryrefslogtreecommitdiffstats
path: root/toolkit/components/tooltiptext/TooltipTextProvider.sys.mjs
blob: 96a3e8dd5f65a809df202ba100d8bed7d059001b (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
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
/* 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/. */

export function TooltipTextProvider() {}

function getFileInputTitleText(tipElement) {
  let files = tipElement.files;
  let bundle = Services.strings.createBundle(
    "chrome://global/locale/layout/HtmlForm.properties"
  );
  if (!files.length) {
    return bundle.GetStringFromName(
      tipElement.multiple ? "NoFilesSelected" : "NoFileSelected"
    );
  }
  let titleText = files[0].name;
  // For UX and performance (jank) reasons we cap the number of
  // files that we list in the tooltip to 20 plus a "and xxx more"
  // line, or to 21 if exactly 21 files were picked.
  const TRUNCATED_FILE_COUNT = 20;
  let count = Math.min(files.length, TRUNCATED_FILE_COUNT);
  for (let i = 1; i < count; ++i) {
    titleText += "\n" + files[i].name;
  }
  if (files.length == TRUNCATED_FILE_COUNT + 1) {
    titleText += "\n" + files[TRUNCATED_FILE_COUNT].name;
  } else if (files.length > TRUNCATED_FILE_COUNT + 1) {
    const l10n = new Localization(["toolkit/global/htmlForm.ftl"], true);
    const andXMoreStr = l10n.formatValueSync("input-file-and-more-files", {
      fileCount: files.length - TRUNCATED_FILE_COUNT,
    });
    titleText += "\n" + andXMoreStr;
  }
  return titleText;
}

TooltipTextProvider.prototype = {
  getNodeText(tipElement, textOut, directionOut) {
    // Don't show the tooltip if the tooltip node is a document or browser.
    // Caller should ensure the node is in (composed) document.
    if (
      !tipElement ||
      !tipElement.ownerDocument ||
      tipElement.localName == "browser"
    ) {
      return false;
    }

    var defView = tipElement.ownerGlobal;
    // XXX Work around bug 350679:
    // "Tooltips can be fired in documents with no view".
    if (!defView) {
      return false;
    }

    const XLinkNS = "http://www.w3.org/1999/xlink";
    const XUL_NS =
      "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

    var titleText = null;
    var XLinkTitleText = null;
    var lookingForSVGTitle = true;
    var direction = tipElement.ownerDocument.dir;

    for (; tipElement; tipElement = tipElement.flattenedTreeParentNode) {
      if (tipElement.nodeType != defView.Node.ELEMENT_NODE) {
        continue;
      }
      if (tipElement.namespaceURI == XUL_NS) {
        lookingForSVGTitle = false;
        // NOTE: getAttribute behaves differently for XUL so we can't rely on
        // it returning null, see bug 232598.
        titleText = tipElement.hasAttribute("tooltiptext")
          ? tipElement.getAttribute("tooltiptext")
          : null;
      } else if (!defView.SVGElement.isInstance(tipElement)) {
        lookingForSVGTitle = false;
        titleText = tipElement.getAttribute("title");
      }

      if (
        (defView.HTMLAnchorElement.isInstance(tipElement) ||
          defView.HTMLAreaElement.isInstance(tipElement) ||
          defView.HTMLLinkElement.isInstance(tipElement) ||
          defView.SVGAElement.isInstance(tipElement)) &&
        tipElement.href
      ) {
        XLinkTitleText = tipElement.getAttributeNS(XLinkNS, "title");
      }

      // If the element is invalid per HTML5 Forms specifications and has no title,
      // show the constraint validation error message.
      if (
        titleText == null &&
        (defView.HTMLInputElement.isInstance(tipElement) ||
          defView.HTMLTextAreaElement.isInstance(tipElement) ||
          defView.HTMLSelectElement.isInstance(tipElement) ||
          defView.HTMLButtonElement.isInstance(tipElement)) &&
        !tipElement.form?.noValidate
      ) {
        // If the element is barred from constraint validation or valid,
        // the validation message will be the empty string.
        titleText = tipElement.validationMessage || null;
      }

      // If the element is an <input type='file'> without a title, we should show
      // the current file selection.
      if (
        titleText == null &&
        defView.HTMLInputElement.isInstance(tipElement) &&
        tipElement.type == "file"
      ) {
        try {
          titleText = getFileInputTitleText(tipElement);
        } catch (ex) {}
      }

      if (
        lookingForSVGTitle &&
        tipElement.parentNode.nodeType != defView.Node.DOCUMENT_NODE
      ) {
        for (let childNode of tipElement.childNodes) {
          if (defView.SVGTitleElement.isInstance(childNode)) {
            titleText = childNode.textContent;
            break;
          }
        }
      }

      // Check texts against null so that title="" can be used to undefine a
      // title on a child element.
      if (titleText != null || XLinkTitleText != null) {
        break;
      }
    }

    return [titleText, XLinkTitleText].some(function (t) {
      if (t && /\S/.test(t)) {
        // Make CRLF and CR render one line break each.
        textOut.value = t.replace(/\r\n?/g, "\n");

        if (tipElement) {
          direction = defView
            .getComputedStyle(tipElement)
            .getPropertyValue("direction");
        }

        directionOut.value = direction;
        return true;
      }

      return false;
    });
  },

  classID: Components.ID("{f376627f-0bbc-47b8-887e-fc92574cc91f}"),
  QueryInterface: ChromeUtils.generateQI(["nsITooltipTextProvider"]),
};