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
Bug - Morph text nodes. #2524
Merged
Merged
Bug - Morph text nodes. #2524
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,23 +6,23 @@ function breakpoint(message) { | |
if (! debug) return | ||
|
||
message && logger(message.replace('\n', '\\n')) | ||
|
||
return new Promise(resolve => resolveStep = () => resolve()) | ||
} | ||
|
||
export async function morph(from, toHtml, options) { | ||
assignOptions(options) | ||
|
||
let toEl = createElement(toHtml) | ||
|
||
// If there is no x-data on the element we're morphing, | ||
// let's seed it with the outer Alpine scope on the page. | ||
if (window.Alpine && ! from._x_dataStack) { | ||
toEl._x_dataStack = window.Alpine.closestDataStack(from) | ||
|
||
toEl._x_dataStack && window.Alpine.clone(from, toEl) | ||
} | ||
|
||
await breakpoint() | ||
|
||
patch(from, toEl) | ||
|
@@ -57,7 +57,7 @@ function assignOptions(options = {}) { | |
adding = options.adding || noop | ||
added = options.added || noop | ||
key = options.key || defaultGetKey | ||
lookahead = options.lookahead || true | ||
lookahead = options.lookahead || true | ||
debug = options.debug || false | ||
} | ||
|
||
|
@@ -71,12 +71,12 @@ async function patch(from, to) { | |
// don't see a way to enable it currently: | ||
// | ||
// if (from.isEqualNode(to)) return | ||
|
||
if (differentElementNamesTypesOrKeys(from, to)) { | ||
let result = patchElement(from, to) | ||
|
||
await breakpoint('Swap elements') | ||
|
||
return result | ||
} | ||
|
||
|
@@ -89,7 +89,7 @@ async function patch(from, to) { | |
if (textOrComment(to)) { | ||
await patchNodeValue(from, to) | ||
updated(from, to) | ||
|
||
return | ||
} | ||
|
||
|
@@ -152,7 +152,7 @@ async function patchAttributes(from, to) { | |
|
||
if (! to.hasAttribute(name)) { | ||
from.removeAttribute(name) | ||
|
||
await breakpoint('Remove attribute') | ||
} | ||
} | ||
|
@@ -212,7 +212,7 @@ async function patchChildren(from, to) { | |
currentFrom = addNodeBefore(currentTo, currentFrom) | ||
|
||
domKey = getKey(currentFrom) | ||
|
||
await breakpoint('Move element (lookahead)') | ||
} | ||
} | ||
|
@@ -222,8 +222,8 @@ async function patchChildren(from, to) { | |
domKeyHoldovers[domKey] = currentFrom | ||
currentFrom = addNodeBefore(currentTo, currentFrom) | ||
domKeyHoldovers[domKey].remove() | ||
currentFrom = dom(currentFrom).nodes.next() | ||
currentTo = dom(currentTo).nodes.next() | ||
currentFrom = dom(currentFrom).nodes().next() | ||
currentTo = dom(currentTo).nodes().next() | ||
|
||
await breakpoint('No "to" key') | ||
|
||
|
@@ -233,7 +233,7 @@ async function patchChildren(from, to) { | |
if (toKey && ! domKey) { | ||
if (domKeyDomNodeMap[toKey]) { | ||
currentFrom = dom(currentFrom).replace(domKeyDomNodeMap[toKey]) | ||
|
||
await breakpoint('No "from" key') | ||
} | ||
} | ||
|
@@ -244,17 +244,17 @@ async function patchChildren(from, to) { | |
|
||
if (domKeyNode) { | ||
currentFrom = dom(currentFrom).replace(domKeyNode) | ||
|
||
await breakpoint('Move "from" key') | ||
} else { | ||
domKeyHoldovers[domKey] = currentFrom | ||
currentFrom = addNodeBefore(currentTo, currentFrom) | ||
domKeyHoldovers[domKey].remove() | ||
currentFrom = dom(currentFrom).next() | ||
currentTo = dom(currentTo).next() | ||
|
||
await breakpoint('I dont even know what this does') | ||
|
||
continue | ||
} | ||
} | ||
|
@@ -263,8 +263,8 @@ async function patchChildren(from, to) { | |
// Patch elements | ||
await patch(currentFrom, currentTo) | ||
|
||
currentTo = currentTo && dom(currentTo).next() | ||
currentFrom = currentFrom && dom(currentFrom).next() | ||
currentTo = currentTo && dom(currentTo).nodes().next() | ||
currentFrom = currentFrom && dom(currentFrom).nodes().next() | ||
Comment on lines
+266
to
+267
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relevant change. When getting the next element for the next loop iteration, we want to use nodes() so we don’t skip text nodes. |
||
} | ||
|
||
// Cleanup extra froms | ||
|
@@ -367,7 +367,7 @@ class DomManager { | |
this.traversals = { | ||
'first': 'firstChild', | ||
'next': 'nextSibling', | ||
'parent': 'parentNode', | ||
'parent': 'parentNode', | ||
}; return this | ||
} | ||
|
||
|
@@ -386,7 +386,7 @@ class DomManager { | |
replace(replacement) { | ||
this.el[this.traversals['parent']].replaceChild(replacement, this.el); return replacement | ||
} | ||
|
||
append(appendee) { | ||
this.el.appendChild(appendee); return appendee | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not relevant to this bug but I believe it's a typo as nodes should be a function call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, should be yeah