mirror of
https://github.com/discourse/discourse.git
synced 2024-11-29 12:24:38 +08:00
DEV: Convert IframedHtml to gjs (#24836)
* DEV: Convert IframedHtml to gjs * disable lint * didUpdate * no className
This commit is contained in:
parent
cfd72fa65c
commit
b6819fd8c1
|
@ -0,0 +1,25 @@
|
||||||
|
import Component from "@glimmer/component";
|
||||||
|
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
|
||||||
|
import didUpdate from "@ember/render-modifiers/modifiers/did-update";
|
||||||
|
import { bind } from "discourse-common/utils/decorators";
|
||||||
|
|
||||||
|
export default class IframedHtml extends Component {
|
||||||
|
@bind
|
||||||
|
writeHtml(element) {
|
||||||
|
const iframeDoc = element.contentWindow.document;
|
||||||
|
iframeDoc.open("text/html", "replace");
|
||||||
|
iframeDoc.write(this.args.html);
|
||||||
|
iframeDoc.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
<template>
|
||||||
|
{{! template-lint-disable require-iframe-title }}
|
||||||
|
<iframe
|
||||||
|
{{didInsert this.writeHtml}}
|
||||||
|
{{didUpdate this.witeHtml @html}}
|
||||||
|
sandbox="allow-same-origin"
|
||||||
|
class={{if @html "iframed-html"}}
|
||||||
|
...attributes
|
||||||
|
></iframe>
|
||||||
|
</template>
|
||||||
|
}
|
|
@ -1,17 +0,0 @@
|
||||||
import Component from "@ember/component";
|
|
||||||
|
|
||||||
export default Component.extend({
|
|
||||||
tagName: "iframe",
|
|
||||||
html: null,
|
|
||||||
classNameBindings: ["html:iframed-html"],
|
|
||||||
sandbox: "allow-same-origin",
|
|
||||||
attributeBindings: ["sandbox:sandbox"],
|
|
||||||
|
|
||||||
didRender() {
|
|
||||||
this._super(...arguments);
|
|
||||||
const iframeDoc = this.element.contentWindow.document;
|
|
||||||
iframeDoc.open("text/html", "replace");
|
|
||||||
iframeDoc.write(this.html);
|
|
||||||
iframeDoc.close();
|
|
||||||
},
|
|
||||||
});
|
|
|
@ -2,7 +2,6 @@ import { render } from "@ember/test-helpers";
|
||||||
import { hbs } from "ember-cli-htmlbars";
|
import { hbs } from "ember-cli-htmlbars";
|
||||||
import { module, test } from "qunit";
|
import { module, test } from "qunit";
|
||||||
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
||||||
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
|
|
||||||
|
|
||||||
module("Integration | Component | iframed-html", function (hooks) {
|
module("Integration | Component | iframed-html", function (hooks) {
|
||||||
setupRenderingTest(hooks);
|
setupRenderingTest(hooks);
|
||||||
|
@ -12,16 +11,13 @@ module("Integration | Component | iframed-html", function (hooks) {
|
||||||
hbs`<IframedHtml @html="<h1 id='find-me'>hello</h1>" class="this-is-an-iframe" />`
|
hbs`<IframedHtml @html="<h1 id='find-me'>hello</h1>" class="this-is-an-iframe" />`
|
||||||
);
|
);
|
||||||
|
|
||||||
const iframe = queryAll("iframe.this-is-an-iframe");
|
assert
|
||||||
assert.strictEqual(iframe.length, 1, "inserts an iframe");
|
.dom("iframe.this-is-an-iframe")
|
||||||
|
.exists({ count: 1 }, "inserts an iframe");
|
||||||
assert.ok(
|
|
||||||
iframe[0].classList.contains("this-is-an-iframe"),
|
|
||||||
"Adds className to the iframes classList"
|
|
||||||
);
|
|
||||||
|
|
||||||
|
const iframe = document.querySelector(".iframed-html");
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
iframe[0].contentWindow.document.body.querySelectorAll("#find-me").length,
|
iframe.contentWindow.document.body.querySelectorAll("#find-me").length,
|
||||||
1,
|
1,
|
||||||
"inserts the passed in html into the iframe"
|
"inserts the passed in html into the iframe"
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user