-
Notifications
You must be signed in to change notification settings - Fork 25
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
Support document fragments #41
Comments
Seems easy enough to do your own |
In every library, some people won't need everything. Some already might not need |
FWIW, I've adapted your own chai.use(function (_chai, utils) {
const {flag} = utils;
const container = (contents) => {
const dummyContainer = document.createElement('div');
dummyContainer.append(contents);
return dummyContainer;
};
_chai.Assertion.addMethod('fragmentHtml', function (html) {
const frag = flag(this, 'object'),
actual = container(flag(this, 'object')).innerHTML;
if (flag(this, 'contains')) {
this.assert(
actual.includes(html),
'expected #{act} to contain HTML #{exp}',
'expected #{act} not to contain HTML #{exp}',
html,
actual
);
} else {
this.assert(
actual === html,
'expected ' + String(frag) +
' to have HTML #{exp}, but the HTML was #{act}',
'expected ' + String(frag) + ' not to have HTML #{exp}',
html,
actual
);
}
});
}); |
So to understand your use case, you have a string of HTML, and you want to check that it's parsed correctly? |
I have I have an i18n (internationalization) library, intl-dom which returns a string by default if there is no injecting of DOM elements/nodes, while returning a fragment otherwise. As an example, the i18n function my app produces can accept DOM objects for safe injection into locale strings (without allowing the locale to include arbitrary HTML) like this: const result = _('translationWithLineBreak', {
lb: $('<br>')[0]
}); with the locale file: {
"translationWithLineBreak": "here is a translation with a{lb}line break"
} And the resulting DOM fragment (represented here as a string): here is a translation with a<br/>line break I don't need or want to return this encapsulated in a Sometimes, however, a project may use our i18n API but not have any DOM to inject. In such cases, we just return a Javascript string rather than a DOM fragment (unless the user opts to always get a fragment). We return different types by default because:
E.g., in my templating tool, jamilih, one can embed fragments or strings: jml('section', [
'some text: ',
_('translationWithLineBreak', {
lb: $('<br>')[0]
}),
['div', [
'and some text in an element
]]
]) ...giving this DOM: <section>some text: here is a translation with a<br/>line break<div>and some text in an element</div></section>
So with the above, whether a string or fragment is returned, one can always do: desiredParentContainer.append(result); TL;DR: Even if your project doesn't allow checking of strings in the same manner as fragments, it would be nice to explicitly be able to check document fragment contents out of the box since fragments have long been a part of the DOM, and as per the above, they continue to have their uses. |
What might be nice is to have |
That'd work! |
Hi,
I'm wondering if you'd be open to supporting checks of the HTML (or text) on document fragments?
I'm currently using this awkward hack:
Thanks!
The text was updated successfully, but these errors were encountered: