Skip to content

Commit c2dafb7

Browse files
simplify softMatch more and tweak docs.
1 parent 52be2bd commit c2dafb7

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

src/idiomorph.js

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,8 @@ var Idiomorph = (function () {
382382
}
383383

384384
/**
385-
* =============================================================================
386-
* Scans forward from the insertionPoint in the old parent looking for a potential id match
387-
* for the newChild.
388-
* =============================================================================
385+
* Scans forward from the insertionPoint in the old parent looking for a potential id match
386+
* for the newChild.
389387
* @param {Node} newChild
390388
* @param {Node | null} insertionPoint
391389
* @param {Node | null} endPoint
@@ -410,12 +408,8 @@ var Idiomorph = (function () {
410408
}
411409

412410
/**
413-
* =============================================================================
414-
* Scans forward from the insertionPoint in the old parent looking for a potential soft match
415-
* for the newChild. We stop if we find a potential soft match for the new child OR
416-
* if we find a potential id match in the old parents children OR if we find two
417-
* potential soft matches for the next two pieces of new content
418-
* =============================================================================
411+
* Scans forward from the insertionPoint in the old parent looking for a potential soft match
412+
* for the newChild.
419413
* @param {Node} newChild
420414
* @param {Node | null} insertionPoint
421415
* @param {Node | null} endPoint
@@ -427,18 +421,15 @@ var Idiomorph = (function () {
427421
let nextSibling = newChild.nextSibling;
428422

429423
while (potentialSoftMatch && potentialSoftMatch != endPoint) {
430-
if (hasPersistentIdNodes(ctx, potentialSoftMatch)) {
431-
// the current potential soft match has a potential id set match with the remaining new
432-
// content so bail out of looking
433-
return null;
434-
}
435-
436-
if (isSoftMatch(potentialSoftMatch, newChild)) {
437-
return potentialSoftMatch;
424+
// the current potential soft match has a id set match with the remaining new
425+
// content so leave this one for the future
426+
if (!hasPersistentIdNodes(ctx, potentialSoftMatch)) {
427+
if (isSoftMatch(potentialSoftMatch, newChild)) {
428+
return potentialSoftMatch;
429+
}
438430
}
439431
potentialSoftMatch = potentialSoftMatch.nextSibling;
440432
}
441-
442433
return null;
443434
}
444435

@@ -503,7 +494,7 @@ var Idiomorph = (function () {
503494

504495
/**
505496
* Moves an element before another element within the same parent.
506-
* Uses the proposed `moveBefore` API if available, otherwise falls back to `insertBefore`.
497+
* Uses the proposed `moveBefore` API if available (and working), otherwise falls back to `insertBefore`.
507498
* This is essentialy a forward-compat wrapper.
508499
*
509500
* @param {Element} parentNode - The parent node containing the after element.
@@ -628,7 +619,7 @@ var Idiomorph = (function () {
628619
}
629620

630621
/**
631-
* syncs a given node with another node, copying over all attributes and
622+
* syncs the oldNode to the newNode, copying over all attributes and
632623
* inner element state from the newNode to the oldNode
633624
*
634625
* @param {Node} oldNode the node to copy attributes & state to
@@ -746,8 +737,8 @@ var Idiomorph = (function () {
746737
}
747738

748739
/**
749-
* @param {Element} oldElement element to sync the value to
750-
* @param {Element} newElement element to sync the value from
740+
* @param {Element} oldElement element to write the value to
741+
* @param {Element} newElement element to read the value from
751742
* @param {string} attributeName the attribute name
752743
* @param {MorphContext} ctx the merge context
753744
*/
@@ -859,17 +850,11 @@ var Idiomorph = (function () {
859850
* @returns {Promise<void>[]}
860851
*/
861852
function handleHeadElement(oldHead, newHead, ctx) {
862-
/** @type {Node[]} */
863853
let added = [];
864-
/** @type {Element[]} */
865854
let removed = [];
866-
/** @type {Element[]} */
867855
let preserved = [];
868-
/** @type {Element[]} */
869856
let nodesToAppend = [];
870857

871-
let headMergeStyle = ctx.head.style;
872-
873858
// put all new head elements into a Map, by their outerHTML
874859
let srcToNewHeadNodes = new Map();
875860
for (const newHeadChild of newHead.children) {
@@ -893,7 +878,7 @@ var Idiomorph = (function () {
893878
preserved.push(currentHeadElt);
894879
}
895880
} else {
896-
if (headMergeStyle === "append") {
881+
if (ctx.head.style === "append") {
897882
// we are appending and this existing element is not new content
898883
// so if and only if it is marked for re-append do we do anything
899884
if (isReAppended) {
@@ -999,9 +984,6 @@ var Idiomorph = (function () {
999984
* @returns {ConfigInternal}
1000985
*/
1001986
function mergeDefaults(config) {
1002-
/**
1003-
* @type {ConfigInternal}
1004-
*/
1005987
let finalConfig = Object.assign({}, defaults);
1006988

1007989
// copy top level stuff into final config
@@ -1020,6 +1002,9 @@ var Idiomorph = (function () {
10201002
return finalConfig;
10211003
}
10221004

1005+
/**
1006+
* @returns {HTMLDivElement}
1007+
*/
10231008
function createPantry() {
10241009
const pantry = document.createElement("div");
10251010
pantry.hidden = true;
@@ -1113,10 +1098,8 @@ var Idiomorph = (function () {
11131098
persistentIds.add(id);
11141099
}
11151100
}
1116-
/**
1117-
*
1118-
* @type {Map<Node, Set<string>>}
1119-
*/
1101+
1102+
/** @type {Map<Node, Set<string>>} */
11201103
let idMap = new Map();
11211104
populateIdMapForNode(
11221105
oldContent.parentElement,

0 commit comments

Comments
 (0)