summaryrefslogtreecommitdiffstats
path: root/devtools/client/netmonitor/src/components/previews/HtmlPreview.js
blob: ab8c24fac02be9b3ba59d219d531d680ddcdcef1 (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
/* 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";

const {
  Component,
} = require("resource://devtools/client/shared/vendor/react.js");
const dom = require("resource://devtools/client/shared/vendor/react-dom-factories.js");
const PropTypes = require("resource://devtools/client/shared/vendor/react-prop-types.js");

/*
 * Response preview component
 * Display HTML content within a sandbox enabled iframe
 */
class HTMLPreview extends Component {
  static get propTypes() {
    return {
      responseContent: PropTypes.object.isRequired,
    };
  }

  componentDidMount() {
    const { container } = this.refs;
    const iframe = container.ownerDocument.createXULElement("iframe");
    this.iframe = iframe;
    iframe.setAttribute("type", "content");
    iframe.setAttribute("remote", "true");

    // For some reason, when we try to select some text,
    // a drag of the whole page is initiated.
    // Workaround this by canceling any start of drag.
    iframe.addEventListener("dragstart", e => e.preventDefault(), {
      capture: true,
    });

    // Bug 1800916 allow interaction with the preview page until
    // we find a way to prevent navigation without preventing copy paste from it.
    //
    // iframe.addEventListener("mousedown", e => e.preventDefault(), {
    //   capture: true,
    // });
    container.appendChild(iframe);

    // browsingContext attribute is only available after the iframe
    // is attached to the DOM Tree.
    iframe.browsingContext.allowJavascript = false;

    this.#updatePreview();
  }

  componentDidUpdate() {
    this.#updatePreview();
  }

  componentWillUnmount() {
    this.iframe.remove();
  }

  #updatePreview() {
    const { responseContent } = this.props;
    const htmlBody = responseContent ? responseContent.content.text : "";
    this.iframe.setAttribute(
      "src",
      "data:text/html;charset=UTF-8," + encodeURIComponent(htmlBody)
    );
  }

  render() {
    return dom.div({ className: "html-preview", ref: "container" });
  }
}

module.exports = HTMLPreview;