Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/tmp/*
!tmp/.gitkeep
.idea
/src/*.d.ts
156 changes: 156 additions & 0 deletions dist/idiomorph-ext.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
export type ConfigHead = {
style?: "merge" | "append" | "morph" | "none";
block?: boolean;
ignore?: boolean;
shouldPreserve?: (arg0: Element) => boolean;
shouldReAppend?: (arg0: Element) => boolean;
shouldRemove?: (arg0: Element) => boolean;
afterHeadMorphed?: (arg0: Element, arg1: {
added: Node[];
kept: Element[];
removed: Element[];
}) => void;
};
export type ConfigCallbacks = {
beforeNodeAdded?: (arg0: Node) => boolean;
afterNodeAdded?: (arg0: Node) => void;
beforeNodeMorphed?: (arg0: Element, arg1: Node) => boolean;
afterNodeMorphed?: (arg0: Element, arg1: Node) => void;
beforeNodeRemoved?: (arg0: Element) => boolean;
afterNodeRemoved?: (arg0: Element) => void;
beforeAttributeUpdated?: (arg0: string, arg1: Element, arg2: "update" | "remove") => boolean;
};
export type Config = {
morphStyle?: "outerHTML" | "innerHTML";
ignoreActive?: boolean;
ignoreActiveValue?: boolean;
restoreFocus?: boolean;
callbacks?: ConfigCallbacks;
head?: ConfigHead;
};
export type NoOp = () => void;
export type ConfigHeadInternal = {
style: "merge" | "append" | "morph" | "none";
block?: boolean;
ignore?: boolean;
shouldPreserve: ((arg0: Element) => boolean) | NoOp;
shouldReAppend: ((arg0: Element) => boolean) | NoOp;
shouldRemove: ((arg0: Element) => boolean) | NoOp;
afterHeadMorphed: ((arg0: Element, arg1: {
added: Node[];
kept: Element[];
removed: Element[];
}) => void) | NoOp;
};
export type ConfigCallbacksInternal = {
beforeNodeAdded: ((arg0: Node) => boolean) | NoOp;
afterNodeAdded: ((arg0: Node) => void) | NoOp;
beforeNodeMorphed: ((arg0: Node, arg1: Node) => boolean) | NoOp;
afterNodeMorphed: ((arg0: Node, arg1: Node) => void) | NoOp;
beforeNodeRemoved: ((arg0: Node) => boolean) | NoOp;
afterNodeRemoved: ((arg0: Node) => void) | NoOp;
beforeAttributeUpdated: ((arg0: string, arg1: Element, arg2: "update" | "remove") => boolean) | NoOp;
};
export type ConfigInternal = {
morphStyle: "outerHTML" | "innerHTML";
ignoreActive?: boolean;
ignoreActiveValue?: boolean;
restoreFocus?: boolean;
callbacks: ConfigCallbacksInternal;
head: ConfigHeadInternal;
};
export type IdSets = {
persistentIds: Set<string>;
idMap: Map<Node, Set<string>>;
};
export type Morph = (oldNode: Element | Document, newContent: Element | Node | HTMLCollection | Node[] | string | null, config?: Config) => Promise<Node[]> | Node[];
/**
* @typedef {object} ConfigHead
*
* @property {'merge' | 'append' | 'morph' | 'none'} [style]
* @property {boolean} [block]
* @property {boolean} [ignore]
* @property {function(Element): boolean} [shouldPreserve]
* @property {function(Element): boolean} [shouldReAppend]
* @property {function(Element): boolean} [shouldRemove]
* @property {function(Element, {added: Node[], kept: Element[], removed: Element[]}): void} [afterHeadMorphed]
*/
/**
* @typedef {object} ConfigCallbacks
*
* @property {function(Node): boolean} [beforeNodeAdded]
* @property {function(Node): void} [afterNodeAdded]
* @property {function(Element, Node): boolean} [beforeNodeMorphed]
* @property {function(Element, Node): void} [afterNodeMorphed]
* @property {function(Element): boolean} [beforeNodeRemoved]
* @property {function(Element): void} [afterNodeRemoved]
* @property {function(string, Element, "update" | "remove"): boolean} [beforeAttributeUpdated]
*/
/**
* @typedef {object} Config
*
* @property {'outerHTML' | 'innerHTML'} [morphStyle]
* @property {boolean} [ignoreActive]
* @property {boolean} [ignoreActiveValue]
* @property {boolean} [restoreFocus]
* @property {ConfigCallbacks} [callbacks]
* @property {ConfigHead} [head]
*/
/**
* @callback NoOp
*
* @returns {void}
*/
/**
* @typedef {object} ConfigHeadInternal
*
* @property {'merge' | 'append' | 'morph' | 'none'} style
* @property {boolean} [block]
* @property {boolean} [ignore]
* @property {(function(Element): boolean) | NoOp} shouldPreserve
* @property {(function(Element): boolean) | NoOp} shouldReAppend
* @property {(function(Element): boolean) | NoOp} shouldRemove
* @property {(function(Element, {added: Node[], kept: Element[], removed: Element[]}): void) | NoOp} afterHeadMorphed
*/
/**
* @typedef {object} ConfigCallbacksInternal
*
* @property {(function(Node): boolean) | NoOp} beforeNodeAdded
* @property {(function(Node): void) | NoOp} afterNodeAdded
* @property {(function(Node, Node): boolean) | NoOp} beforeNodeMorphed
* @property {(function(Node, Node): void) | NoOp} afterNodeMorphed
* @property {(function(Node): boolean) | NoOp} beforeNodeRemoved
* @property {(function(Node): void) | NoOp} afterNodeRemoved
* @property {(function(string, Element, "update" | "remove"): boolean) | NoOp} beforeAttributeUpdated
*/
/**
* @typedef {object} ConfigInternal
*
* @property {'outerHTML' | 'innerHTML'} morphStyle
* @property {boolean} [ignoreActive]
* @property {boolean} [ignoreActiveValue]
* @property {boolean} [restoreFocus]
* @property {ConfigCallbacksInternal} callbacks
* @property {ConfigHeadInternal} head
*/
/**
* @typedef {Object} IdSets
* @property {Set<string>} persistentIds
* @property {Map<Node, Set<string>>} idMap
*/
/**
* @callback Morph
*
* @param {Element | Document} oldNode
* @param {Element | Node | HTMLCollection | Node[] | string | null} newContent
* @param {Config} [config]
* @returns {Promise<Node[]> | Node[]}
*/
/**
*
* @type {{defaults: ConfigInternal, morph: Morph}}
*/
export var Idiomorph: {
defaults: ConfigInternal;
morph: Morph;
};
21 changes: 14 additions & 7 deletions dist/idiomorph-ext.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import htmx from "htmx.org";
*/

/**
* @typedef {function} NoOp
* @callback NoOp
*
* @returns {void}
*/
Expand Down Expand Up @@ -83,12 +83,12 @@ import htmx from "htmx.org";
*/

/**
* @typedef {Function} Morph
* @callback Morph
*
* @param {Element | Document} oldNode
* @param {Element | Node | HTMLCollection | Node[] | string | null} newContent
* @param {Config} [config]
* @returns {undefined | Node[]}
* @returns {Promise<Node[]> | Node[]}
*/

// base IIFE to define idiomorph
Expand Down Expand Up @@ -236,7 +236,13 @@ var Idiomorph = (function () {
activeElement?.focus();
}
if (activeElement && !activeElement.selectionEnd && selectionEnd) {
activeElement.setSelectionRange(selectionStart, selectionEnd);
try {
activeElement.setSelectionRange(selectionStart, selectionEnd);
} catch {
// the element may not support setSelectionRange: it's no longer an
// input/textarea after the morph, or it's an input type (number,
// email, date, ...) that doesn't support text selection
}
}

return results;
Expand Down Expand Up @@ -678,12 +684,13 @@ var Idiomorph = (function () {
const oldAttributes = oldElt.attributes;
const newAttributes = newElt.attributes;
for (const newAttribute of newAttributes) {
if (ignoreAttribute(newAttribute.name, oldElt, "update", ctx)) {
if (oldElt.getAttribute(newAttribute.name) === newAttribute.value) {
continue;
}
if (oldElt.getAttribute(newAttribute.name) !== newAttribute.value) {
oldElt.setAttribute(newAttribute.name, newAttribute.value);
if (ignoreAttribute(newAttribute.name, oldElt, "update", ctx)) {
continue;
}
oldElt.setAttribute(newAttribute.name, newAttribute.value);
}
// iterate backwards to avoid skipping over items when a delete occurs
for (let i = oldAttributes.length - 1; 0 <= i; i--) {
Expand Down
21 changes: 14 additions & 7 deletions dist/idiomorph-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/

/**
* @typedef {function} NoOp
* @callback NoOp
*
* @returns {void}
*/
Expand Down Expand Up @@ -81,12 +81,12 @@
*/

/**
* @typedef {Function} Morph
* @callback Morph
*
* @param {Element | Document} oldNode
* @param {Element | Node | HTMLCollection | Node[] | string | null} newContent
* @param {Config} [config]
* @returns {undefined | Node[]}
* @returns {Promise<Node[]> | Node[]}
*/

// base IIFE to define idiomorph
Expand Down Expand Up @@ -234,7 +234,13 @@ var Idiomorph = (function () {
activeElement?.focus();
}
if (activeElement && !activeElement.selectionEnd && selectionEnd) {
activeElement.setSelectionRange(selectionStart, selectionEnd);
try {
activeElement.setSelectionRange(selectionStart, selectionEnd);
} catch {
// the element may not support setSelectionRange: it's no longer an
// input/textarea after the morph, or it's an input type (number,
// email, date, ...) that doesn't support text selection
}
}

return results;
Expand Down Expand Up @@ -676,12 +682,13 @@ var Idiomorph = (function () {
const oldAttributes = oldElt.attributes;
const newAttributes = newElt.attributes;
for (const newAttribute of newAttributes) {
if (ignoreAttribute(newAttribute.name, oldElt, "update", ctx)) {
if (oldElt.getAttribute(newAttribute.name) === newAttribute.value) {
continue;
}
if (oldElt.getAttribute(newAttribute.name) !== newAttribute.value) {
oldElt.setAttribute(newAttribute.name, newAttribute.value);
if (ignoreAttribute(newAttribute.name, oldElt, "update", ctx)) {
continue;
}
oldElt.setAttribute(newAttribute.name, newAttribute.value);
}
// iterate backwards to avoid skipping over items when a delete occurs
for (let i = oldAttributes.length - 1; 0 <= i; i--) {
Expand Down
2 changes: 1 addition & 1 deletion dist/idiomorph-ext.min.js

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions dist/idiomorph.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ define(() => {
*/

/**
* @typedef {function} NoOp
* @callback NoOp
*
* @returns {void}
*/
Expand Down Expand Up @@ -83,12 +83,12 @@ define(() => {
*/

/**
* @typedef {Function} Morph
* @callback Morph
*
* @param {Element | Document} oldNode
* @param {Element | Node | HTMLCollection | Node[] | string | null} newContent
* @param {Config} [config]
* @returns {undefined | Node[]}
* @returns {Promise<Node[]> | Node[]}
*/

// base IIFE to define idiomorph
Expand Down Expand Up @@ -236,7 +236,13 @@ var Idiomorph = (function () {
activeElement?.focus();
}
if (activeElement && !activeElement.selectionEnd && selectionEnd) {
activeElement.setSelectionRange(selectionStart, selectionEnd);
try {
activeElement.setSelectionRange(selectionStart, selectionEnd);
} catch {
// the element may not support setSelectionRange: it's no longer an
// input/textarea after the morph, or it's an input type (number,
// email, date, ...) that doesn't support text selection
}
}

return results;
Expand Down Expand Up @@ -678,12 +684,13 @@ var Idiomorph = (function () {
const oldAttributes = oldElt.attributes;
const newAttributes = newElt.attributes;
for (const newAttribute of newAttributes) {
if (ignoreAttribute(newAttribute.name, oldElt, "update", ctx)) {
if (oldElt.getAttribute(newAttribute.name) === newAttribute.value) {
continue;
}
if (oldElt.getAttribute(newAttribute.name) !== newAttribute.value) {
oldElt.setAttribute(newAttribute.name, newAttribute.value);
if (ignoreAttribute(newAttribute.name, oldElt, "update", ctx)) {
continue;
}
oldElt.setAttribute(newAttribute.name, newAttribute.value);
}
// iterate backwards to avoid skipping over items when a delete occurs
for (let i = oldAttributes.length - 1; 0 <= i; i--) {
Expand Down
21 changes: 14 additions & 7 deletions dist/idiomorph.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*/

/**
* @typedef {function} NoOp
* @callback NoOp
*
* @returns {void}
*/
Expand Down Expand Up @@ -81,12 +81,12 @@
*/

/**
* @typedef {Function} Morph
* @callback Morph
*
* @param {Element | Document} oldNode
* @param {Element | Node | HTMLCollection | Node[] | string | null} newContent
* @param {Config} [config]
* @returns {undefined | Node[]}
* @returns {Promise<Node[]> | Node[]}
*/

// base IIFE to define idiomorph
Expand Down Expand Up @@ -234,7 +234,13 @@ var Idiomorph = (function () {
activeElement?.focus();
}
if (activeElement && !activeElement.selectionEnd && selectionEnd) {
activeElement.setSelectionRange(selectionStart, selectionEnd);
try {
activeElement.setSelectionRange(selectionStart, selectionEnd);
} catch {
// the element may not support setSelectionRange: it's no longer an
// input/textarea after the morph, or it's an input type (number,
// email, date, ...) that doesn't support text selection
}
}

return results;
Expand Down Expand Up @@ -676,12 +682,13 @@ var Idiomorph = (function () {
const oldAttributes = oldElt.attributes;
const newAttributes = newElt.attributes;
for (const newAttribute of newAttributes) {
if (ignoreAttribute(newAttribute.name, oldElt, "update", ctx)) {
if (oldElt.getAttribute(newAttribute.name) === newAttribute.value) {
continue;
}
if (oldElt.getAttribute(newAttribute.name) !== newAttribute.value) {
oldElt.setAttribute(newAttribute.name, newAttribute.value);
if (ignoreAttribute(newAttribute.name, oldElt, "update", ctx)) {
continue;
}
oldElt.setAttribute(newAttribute.name, newAttribute.value);
}
// iterate backwards to avoid skipping over items when a delete occurs
for (let i = oldAttributes.length - 1; 0 <= i; i--) {
Expand Down
Loading