Skip to content

Commit 062991c

Browse files
committed
breaking(core dom create_from_string): Change create_from_string to be able to create multiple siblings from a string.
Returns now a DocumentFragment instead of a single DOM node. This method wasn't used in Patternslib or any of the core addons. If you used it and it breaks your code, let me know.
1 parent 661b74c commit 062991c

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

src/core/dom.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,9 @@ const is_visible = (el) => {
132132
};
133133

134134
const create_from_string = (string) => {
135-
// Create a DOM element from a string.
136-
const div = document.createElement("div");
137-
div.innerHTML = string.trim();
138-
return div.firstChild;
135+
// Create a DOM nodes from a string.
136+
// See: https://davidwalsh.name/convert-html-stings-dom-nodes
137+
return document.createRange().createContextualFragment(string.trim());
139138
};
140139

141140
const dom = {

src/core/dom.test.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,26 +434,52 @@ describe("core.dom tests", () => {
434434

435435
describe("create_from_string", () => {
436436
it("Creates a DOM element from a string", (done) => {
437-
let res = dom.create_from_string(`
437+
const res = dom.create_from_string(`
438438
<section id="section1">
439439
<span class='yo'>does work.</span>
440440
</section>`);
441441

442-
expect(res.getAttribute("id")).toEqual("section1");
443-
expect(res.querySelector("span.yo").textContent).toEqual("does work.");
442+
// The returned result is like a NodeList
443+
expect(res.firstChild.getAttribute("id")).toEqual("section1");
444+
expect(res.firstChild.querySelector("span.yo").textContent).toEqual(
445+
"does work."
446+
);
447+
448+
done();
449+
});
444450

445-
res = dom.create_from_string(`
451+
it("Creates multiple siblings from a string", (done) => {
452+
const res = dom.create_from_string(`
446453
<section id="section1"></section>
447454
<section id="section2"></section>
448455
`);
449-
// Section 2 is not returned.
450-
expect(res.getAttribute("id")).toEqual("section1");
456+
// Multiple sibplings are also returned.
457+
const sections = res.querySelectorAll("section");
458+
expect(sections[0].getAttribute("id")).toEqual("section1");
459+
expect(sections[1].getAttribute("id")).toEqual("section2");
460+
461+
done();
462+
});
463+
464+
it("Can append multiple siblings to another DOM node", (done) => {
465+
const res = dom.create_from_string(`
466+
<section id="section1"></section>
467+
<section id="section2"></section>
468+
`);
469+
const el = document.createElement("div");
470+
// Multiple siblings can be appended to another element
471+
el.append(res);
472+
expect(el.querySelectorAll("section").length).toBe(2);
473+
474+
done();
475+
});
451476

452-
// TD elements or others which can not be direct children of a
453-
// <div> are not yet supported.
477+
it("It cannot create out-of-context elements like a <td> without a <table> 😔", (done) => {
478+
// TD elements or others which need to be defined in the context of a <table>
479+
// are not yet supported.
454480
// Also see: https://stackoverflow.com/a/494348/1337474
455-
res = dom.create_from_string(`<td></td>`);
456-
expect(res).toBeFalsy();
481+
const res = dom.create_from_string(`<td></td>`);
482+
expect(res.firstChild).toBe(null);
457483

458484
done();
459485
});

0 commit comments

Comments
 (0)