summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/reps/reps/text-node.js
blob: 474e647a9decb07e2a7d36696e372fa10aec11bc (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
/* 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/>. */

"use strict";

// Make this available to both AMD and CJS environments
define(function (require, exports, module) {
  // ReactJS
  const {
    button,
    span,
  } = require("devtools/client/shared/vendor/react-dom-factories");
  const PropTypes = require("devtools/client/shared/vendor/react-prop-types");

  // Reps
  const {
    appendRTLClassNameIfNeeded,
    cropString,
    wrapRender,
  } = require("devtools/client/shared/components/reps/reps/rep-utils");
  const {
    MODE,
  } = require("devtools/client/shared/components/reps/reps/constants");
  const {
    rep: StringRep,
    isLongString,
  } = require("devtools/client/shared/components/reps/reps/string");

  /**
   * Renders DOM #text node.
   */

  TextNode.propTypes = {
    object: PropTypes.object.isRequired,
    mode: PropTypes.oneOf(Object.values(MODE)),
    onDOMNodeMouseOver: PropTypes.func,
    onDOMNodeMouseOut: PropTypes.func,
    onInspectIconClick: PropTypes.func,
    shouldRenderTooltip: PropTypes.bool,
  };

  function TextNode(props) {
    const { object: grip, mode = MODE.SHORT } = props;

    const isInTree = grip.preview && grip.preview.isConnected === true;
    const config = getElementConfig({ ...props, isInTree });
    const inspectIcon = getInspectIcon({ ...props, isInTree });

    if (mode === MODE.TINY || mode === MODE.HEADER) {
      return span(config, getTitle(grip), inspectIcon);
    }

    return span(
      config,
      getTitle(grip),
      " ",
      StringRep({
        className: "nodeValue",
        object: grip.preview.textContent,
      }),
      inspectIcon ? inspectIcon : null
    );
  }

  function getElementConfig(opts) {
    const {
      object,
      isInTree,
      onDOMNodeMouseOver,
      onDOMNodeMouseOut,
      shouldRenderTooltip,
    } = opts;

    const text = getTextContent(object);
    const config = {
      "data-link-actor-id": object.actor,
      "data-link-content-dom-reference": JSON.stringify(
        object.contentDomReference
      ),
      className: appendRTLClassNameIfNeeded(
        "objectBox objectBox-textNode",
        text
      ),
      title: shouldRenderTooltip ? `#text "${text}"` : null,
    };

    if (isInTree) {
      if (onDOMNodeMouseOver) {
        Object.assign(config, {
          onMouseOver: _ => onDOMNodeMouseOver(object),
        });
      }

      if (onDOMNodeMouseOut) {
        Object.assign(config, {
          onMouseOut: _ => onDOMNodeMouseOut(object),
        });
      }
    }

    return config;
  }

  function getTextContent(grip) {
    const text = grip.preview.textContent;
    return cropString(isLongString(text) ? text.initial : text);
  }

  function getInspectIcon(opts) {
    const { object, isInTree, onInspectIconClick } = opts;

    if (!isInTree || !onInspectIconClick) {
      return null;
    }

    return button({
      className: "open-inspector",
      draggable: false,
      // TODO: Localize this with "openNodeInInspector" when Bug 1317038 lands
      title: "Click to select the node in the inspector",
      onClick: e => onInspectIconClick(object, e),
    });
  }

  function getTitle() {
    const title = "#text";
    return span({}, title);
  }

  // Registration
  function supportsObject(grip) {
    return grip?.preview && grip?.class == "Text";
  }

  // Exports from this module
  module.exports = {
    rep: wrapRender(TextNode),
    supportsObject,
  };
});