Skip to content

Commit

Permalink
fixup! Improve DocumentFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
thoughtsunificator committed Nov 14, 2024
1 parent fec65e8 commit 1d05d9c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/binding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ test("Binding remove placeholder documentFragment", (t) => {
Core.run(MyModel5, { binding, parentNode: t.context.document.body })
binding.run({ tagName: "button" }, { binding: new MyBinding3({ observable: t.context.observable }), parentNode: binding.identifier.test })
t.is(t.context.document.body.innerHTML, '<div id="test"><button></button></div>')
console.log(binding.identifier.test)
const b2 = new MyBinding3({ observable: t.context.observable })
binding.run({ tagName: "button" }, { identifier: "test3", binding: b2, parentNode: binding.identifier.test })
t.is(t.context.document.body.innerHTML, '<div id="test"><button></button><button></button></div>')
t.is(binding.identifier.test3.tagName, "BUTTON")
binding.remove()
t.is(t.context.document.body.innerHTML, "")
})
Expand Down
13 changes: 10 additions & 3 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,19 @@ class Core {
node.fragmentChildren = [...node.children]
}
const isPlaceholderDocumentFragment = parentNode.nodeType === node.ownerDocument.defaultView.Node.COMMENT_NODE
if(isPlaceholderDocumentFragment || method === Core.METHOD.REPLACE_NODE) {
parentNode.replaceWith(node)
if(isPlaceholderDocumentFragment) {
if(parentNode.placeholderNode) {
parentNode.placeholderNode.after(node)
} else {
parentNode.replaceWith(node)
}
parentNode.placeholderNode = node
} else if (method === Core.METHOD.APPEND_CHILD) {
parentNode.appendChild(node)
} else if (method === Core.METHOD.INSERT_BEFORE) {
parentNode.parentNode.insertBefore(node, parentNode)
} else if(method === Core.METHOD.REPLACE_NODE) {
parentNode.replaceWith(node)
} else if (method === Core.METHOD.WRAP_NODE) {
node.appendChild(parentNode.cloneNode(true))
parentNode.replaceWith(node)
Expand Down Expand Up @@ -97,7 +104,7 @@ class Core {
if(children.length >= 1) {
node = parentNode.ownerDocument.createDocumentFragment()
} else {
node = parentNode.ownerDocument.createComment("This is a comment automatically generated by domodel. It serves as placeholder for DocumentFragment with no children.")
node = parentNode.ownerDocument.createComment("")
}
}
Object.keys(model).filter(property => Core.PROPERTIES.includes(property) === false).forEach(function(property) {
Expand Down

0 comments on commit 1d05d9c

Please sign in to comment.