Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7cafdc

Browse files
DEV: Deprecate api.decoratePluginOutlet (discourse#24145)
This API is not used by any known themes/plugins, and is problematic for a few reasons - It doesn't work on modern plugin connectors which have no wrapper element - Making modifications to Ember-rendered DOM elements can lead to catastrophic and surprising errors - It doesn't re-run when arguments to a plugin outlet change This commit adds the deprecation notice, and refactors the tests so that they do not rely on any real core plugin outlets
1 parent 351cbab commit b7cafdc

File tree

3 files changed

+73
-68
lines changed

3 files changed

+73
-68
lines changed

app/assets/javascripts/discourse/app/lib/plugin-api.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,9 @@ class PluginApi {
16101610
addDocumentTitleCounter(counterFunction) {
16111611
addPluginDocumentTitleCounter(counterFunction);
16121612
}
1613+
16131614
/**
1615+
*
16141616
* Used for decorating the rendered HTML content of a plugin-outlet after it's been rendered
16151617
*
16161618
* `callback` will be called when it is time to decorate it.
@@ -1628,8 +1630,14 @@ class PluginApi {
16281630
* );
16291631
* ```
16301632
*
1633+
* @deprecated because modifying an Ember-rendered DOM tree can lead to very unexpected errors. Use CSS or plugin outlet connectors instead
1634+
*
16311635
**/
16321636
decoratePluginOutlet(outletName, callback, opts) {
1637+
deprecated(
1638+
"decoratePluginOutlet is deprecated because modifying an Ember-rendered DOM tree can lead to very unexpected errors. Use CSS or plugin outlet connectors instead",
1639+
{ id: "discourse.decorate-plugin-outlet" }
1640+
);
16331641
addPluginOutletDecorator(outletName, callback, opts || {});
16341642
}
16351643

app/assets/javascripts/discourse/tests/acceptance/plugin-outlet-decorator-test.js

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { hash } from "@ember/helper";
2+
import { render } from "@ember/test-helpers";
3+
import { hbs } from "ember-cli-htmlbars";
4+
import { module, test } from "qunit";
5+
import PluginOutlet from "discourse/components/plugin-outlet";
6+
import { withPluginApi } from "discourse/lib/plugin-api";
7+
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
8+
import { query } from "discourse/tests/helpers/qunit-helpers";
9+
import { withSilencedDeprecations } from "discourse-common/lib/deprecated";
10+
import { registerTemporaryModule } from "../../helpers/temporary-module-helper";
11+
12+
const PREFIX = "discourse/plugins/some-plugin/templates/connectors";
13+
14+
module("Plugin Outlet - Decorator", function (hooks) {
15+
setupRenderingTest(hooks);
16+
17+
hooks.beforeEach(() => {
18+
registerTemporaryModule(`${PREFIX}/my-outlet-name/foo`, hbs`FOO`);
19+
registerTemporaryModule(`${PREFIX}/my-outlet-name/bar`, hbs`BAR`);
20+
21+
withPluginApi("0.8.38", (api) => {
22+
withSilencedDeprecations("discourse.decorate-plugin-outlet", () => {
23+
api.decoratePluginOutlet(
24+
"my-outlet-name",
25+
(elem, args) => {
26+
if (elem.classList.contains("foo")) {
27+
elem.style.backgroundColor = "yellow";
28+
elem.classList.toggle("has-value", !!args.value);
29+
}
30+
},
31+
{ id: "yellow-decorator" }
32+
);
33+
});
34+
});
35+
});
36+
37+
test("Calls the plugin callback with the rendered outlet", async function (assert) {
38+
await render(<template>
39+
<PluginOutlet @connectorTagName="div" @name="my-outlet-name" />
40+
</template>);
41+
42+
const fooConnector = query(".my-outlet-name-outlet.foo");
43+
const barConnector = query(".my-outlet-name-outlet.bar");
44+
45+
assert.dom(fooConnector).exists();
46+
assert.strictEqual(fooConnector.style.backgroundColor, "yellow");
47+
assert.strictEqual(barConnector.style.backgroundColor, "");
48+
49+
await render(<template>
50+
<PluginOutlet
51+
@connectorTagName="div"
52+
@name="my-outlet-name"
53+
@outletArgs={{hash value=true}}
54+
/>
55+
</template>);
56+
57+
assert.dom(".my-outlet-name-outlet.foo").hasClass("has-value");
58+
59+
await render(<template>
60+
<PluginOutlet @connectorTagName="div" @name="my-outlet-name" />
61+
</template>);
62+
63+
assert.dom(".my-outlet-name-outlet.foo").doesNotHaveClass("has-value");
64+
});
65+
});

0 commit comments

Comments
 (0)