Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap unsafe component invocation with ensure-safe-component #22

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ember-prismic-dom/src/components/prismic/element.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{~#if this.isCustom~}}
{{~#component this.componentName node=@node~}}
{{~#component (ensure-safe-component this.componentName) node=@node~}}
<Prismic::Children
@componentNames={{@componentNames}}
@node={{@node}}
Expand Down
1 change: 1 addition & 0 deletions test-app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
// test files
files: ['tests/**/*-test.{js,ts}'],
extends: ['plugin:qunit/recommended'],
rules: { 'ember/no-empty-glimmer-component-classes': 'off' },
},
],
};
1 change: 0 additions & 1 deletion test-app/app/components/group-list-item.hbs

This file was deleted.

1 change: 0 additions & 1 deletion test-app/app/components/hyperlink.hbs

This file was deleted.

1 change: 0 additions & 1 deletion test-app/app/components/list-item.hbs

This file was deleted.

2 changes: 1 addition & 1 deletion test-app/config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = async function () {
},
},
embroiderSafe(),
embroiderOptimized({ allowedToFail: true }),
embroiderOptimized(),
],
};
};
52 changes: 49 additions & 3 deletions test-app/tests/integration/components/prismic/dom-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { render } from '@ember/test-helpers';
import { setupRenderingTest } from 'ember-qunit';
import { module, test } from 'qunit';
import { setComponentTemplate } from '@ember/component';
import Component from '@glimmer/component';

import { hbs } from 'ember-cli-htmlbars';

Expand All @@ -18,6 +20,14 @@ module('Integration | Component | prismic/dom', function (hooks) {

module('custom elements', function () {
test('hyperlink', async function (assert) {
class Hyperlink extends Component {}
setComponentTemplate(
hbs`<a href={{@node.element.data.url}}>{{yield}}</a>`,
Hyperlink
);

this.hyperlink = Hyperlink;

this.nodes = [
{
type: 'paragraph',
Expand All @@ -34,32 +44,68 @@ module('Integration | Component | prismic/dom', function (hooks) {
];

await render(
hbs`<Prismic::Dom @nodes={{this.nodes}} @hyperlink='hyperlink'/>`
hbs`<Prismic::Dom @nodes={{this.nodes}} @hyperlink={{this.hyperlink}} />`
);
assert.strictEqual(
cleanHtml(this),
'<div><p>A <a href="https://example.org">link</a> to somewhere</p></div>'
);
});

test('handle passing a custom component as a string', async function (assert) {
class SuperCustom extends Component {}
setComponentTemplate(hbs`<mark>{{yield}}</mark>`, SuperCustom);

this.nodes = [
{
type: 'paragraph',
text: 'A fancy component',
spans: [
{
start: 2,
end: 7,
type: 'super-custom',
},
],
},
];
this.owner.register('component:super-custom', SuperCustom);

await render(
hbs`<Prismic::Dom @nodes={{this.nodes}} @super-custom="super-custom" />`
);

assert.strictEqual(
cleanHtml(this),
'<div><p>A <mark>fancy</mark> component</p></div>'
);
});

test('list', async function (assert) {
class GroupListItem extends Component {}
class ListItem extends Component {}

setComponentTemplate(hbs`<ul>{{~yield~}}elephant</ul>`, GroupListItem);
setComponentTemplate(hbs`<li>{{~yield}} bananna</li>`, ListItem);

this.nodes = [
{ type: 'list-item', text: 'one', spans: [] },
{ type: 'list-item', text: 'two', spans: [] },
];

this.groupListItem = GroupListItem;
this.listItem = '';

await render(
hbs`<Prismic::Dom @nodes={{this.nodes}} @group-list-item='group-list-item' @list-item={{this.listItem}}/>`
hbs`<Prismic::Dom @nodes={{this.nodes}} @group-list-item={{this.groupListItem}} @list-item={{this.listItem}}/>`
);

assert.strictEqual(
cleanHtml(this),
'<div><ul><li>one</li><li>two</li>elephant</ul></div>'
);

this.set('listItem', 'list-item');
this.set('listItem', ListItem);

assert.strictEqual(
cleanHtml(this),
Expand Down