summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/components/reps/element-node.js
blob: 6315fb5b1bbd47c13cd7a7ff76e5f0e88bca3ced (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* 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 React = require("devtools/client/shared/vendor/react");
  const { isGrip } = require("./rep-utils");

  // Utils
  const nodeConstants = require("devtools/shared/dom-node-constants");

  // Shortcuts
  const { span } = React.DOM;

  /**
   * Renders DOM element node.
   */
  const ElementNode = React.createClass({
    displayName: "ElementNode",

    propTypes: {
      object: React.PropTypes.object.isRequired,
      mode: React.PropTypes.string,
    },

    getElements: function (grip, mode) {
      let {attributes, nodeName} = grip.preview;
      const nodeNameElement = span({
        className: "tag-name theme-fg-color3"
      }, nodeName);

      if (mode === "tiny") {
        let elements = [nodeNameElement];
        if (attributes.id) {
          elements.push(
            span({className: "attr-name theme-fg-color2"}, `#${attributes.id}`));
        }
        if (attributes.class) {
          elements.push(
            span({className: "attr-name theme-fg-color2"},
              attributes.class
                .replace(/(^\s+)|(\s+$)/g, "")
                .split(" ")
                .map(cls => `.${cls}`)
                .join("")
            )
          );
        }
        return elements;
      }
      let attributeElements = Object.keys(attributes)
        .sort(function getIdAndClassFirst(a1, a2) {
          if ([a1, a2].includes("id")) {
            return 3 * (a1 === "id" ? -1 : 1);
          }
          if ([a1, a2].includes("class")) {
            return 2 * (a1 === "class" ? -1 : 1);
          }

          // `id` and `class` excepted,
          // we want to keep the same order that in `attributes`.
          return 0;
        })
        .reduce((arr, name, i, keys) => {
          let value = attributes[name];
          let attribute = span({},
            span({className: "attr-name theme-fg-color2"}, `${name}`),
            `="`,
            span({className: "attr-value theme-fg-color6"}, `${value}`),
            `"`
          );

          return arr.concat([" ", attribute]);
        }, []);

      return [
        "<",
        nodeNameElement,
        ...attributeElements,
        ">",
      ];
    },

    render: function () {
      let {object, mode} = this.props;
      let elements = this.getElements(object, mode);
      const baseElement = span({className: "objectBox"}, ...elements);

      if (this.props.objectLink) {
        return this.props.objectLink({object}, baseElement);
      }
      return baseElement;
    },
  });

  // Registration
  function supportsObject(object, type) {
    if (!isGrip(object)) {
      return false;
    }
    return object.preview && object.preview.nodeType === nodeConstants.ELEMENT_NODE;
  }

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