tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GripMessageBody.js (3135B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 // React
      8 const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.mjs");
      9 const {
     10  MESSAGE_TYPE,
     11  JSTERM_COMMANDS,
     12 } = require("resource://devtools/client/webconsole/constants.js");
     13 const { cleanupStyle } = ChromeUtils.importESModule(
     14  "resource://devtools/client/shared/components/reps/reps/rep-utils.mjs",
     15  { global: "current" }
     16 );
     17 const {
     18  getObjectInspector,
     19 } = require("resource://devtools/client/webconsole/utils/object-inspector.js");
     20 const actions = require("resource://devtools/client/webconsole/actions/index.js");
     21 
     22 loader.lazyGetter(this, "objectInspector", function () {
     23  return require("resource://devtools/client/shared/components/object-inspector/index.js");
     24 });
     25 
     26 loader.lazyGetter(this, "MODE", function () {
     27  return ChromeUtils.importESModule(
     28    "resource://devtools/client/shared/components/reps/index.mjs",
     29    { global: "current" }
     30  ).MODE;
     31 });
     32 
     33 GripMessageBody.displayName = "GripMessageBody";
     34 
     35 GripMessageBody.propTypes = {
     36  grip: PropTypes.oneOfType([
     37    PropTypes.string,
     38    PropTypes.number,
     39    PropTypes.object,
     40  ]).isRequired,
     41  serviceContainer: PropTypes.shape({
     42    createElement: PropTypes.func.isRequired,
     43    onViewSourceInDebugger: PropTypes.func.isRequired,
     44  }),
     45  userProvidedStyle: PropTypes.string,
     46  useQuotes: PropTypes.bool,
     47  escapeWhitespace: PropTypes.bool,
     48  type: PropTypes.string,
     49  helperType: PropTypes.string,
     50  maybeScrollToBottom: PropTypes.func,
     51  setExpanded: PropTypes.func,
     52 };
     53 
     54 GripMessageBody.defaultProps = {
     55  mode: MODE.LONG,
     56 };
     57 
     58 function GripMessageBody(props) {
     59  const {
     60    grip,
     61    userProvidedStyle,
     62    serviceContainer,
     63    useQuotes,
     64    escapeWhitespace,
     65    mode = MODE.LONG,
     66    dispatch,
     67    maybeScrollToBottom,
     68    setExpanded,
     69    customFormat = false,
     70  } = props;
     71 
     72  let styleObject;
     73  if (userProvidedStyle && userProvidedStyle !== "") {
     74    styleObject = cleanupStyle(
     75      userProvidedStyle,
     76      serviceContainer.createElement
     77    );
     78  }
     79 
     80  const objectInspectorProps = {
     81    autoExpandDepth: shouldAutoExpandObjectInspector(props) ? 1 : 0,
     82    mode,
     83    maybeScrollToBottom,
     84    setExpanded,
     85    customFormat,
     86    onCmdCtrlClick: node => {
     87      const front = objectInspector.utils.node.getFront(node);
     88      if (front) {
     89        dispatch(actions.showObjectInSidebar(front));
     90      }
     91    },
     92  };
     93 
     94  if (
     95    typeof grip === "string" ||
     96    (grip && grip.type === "longString") ||
     97    (grip?.getGrip && grip.getGrip().type === "longString")
     98  ) {
     99    Object.assign(objectInspectorProps, {
    100      useQuotes,
    101      transformEmptyString: true,
    102      escapeWhitespace,
    103      style: styleObject,
    104    });
    105  }
    106 
    107  return getObjectInspector(grip, serviceContainer, objectInspectorProps);
    108 }
    109 
    110 function shouldAutoExpandObjectInspector(props) {
    111  const { helperType, type } = props;
    112 
    113  return type === MESSAGE_TYPE.DIR || helperType === JSTERM_COMMANDS.INSPECT;
    114 }
    115 
    116 module.exports = GripMessageBody;