-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from chialab/svelte5
Svelte5
- Loading branch information
Showing
7 changed files
with
2,299 additions
and
2,029 deletions.
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@chialab/quantum': minor | ||
--- | ||
|
||
Extend `CharacterData` prototype. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { getParentRealm } from './Realm.js'; | ||
import { defineProperty, getOwnPropertyDescriptors } from './utils.js'; | ||
|
||
/** | ||
* Extends the CharacterData prototype with realm aware methods. | ||
* @param {typeof CharacterData} CharacterData The CharacterData constructor to extend. | ||
*/ | ||
export function extendCharacterData(CharacterData) { | ||
const CharacterDataPrototype = CharacterData.prototype; | ||
const { remove, previousElementSibling, nextElementSibling, after, before, replaceWith } = | ||
getOwnPropertyDescriptors(CharacterDataPrototype); | ||
|
||
defineProperty(CharacterDataPrototype, 'remove', { | ||
/** | ||
* @this {Element} | ||
*/ | ||
value() { | ||
const parentRealm = getParentRealm(this, true); | ||
if (!parentRealm) { | ||
return /** @type {import('./utils.js').ValueDescriptor} */ (remove).value.call(this); | ||
} | ||
return parentRealm.remove(this); | ||
}, | ||
}); | ||
|
||
defineProperty(CharacterDataPrototype, 'previousElementSibling', { | ||
/** | ||
* @this {Element} | ||
*/ | ||
get() { | ||
const parentRealm = getParentRealm(this); | ||
if (!parentRealm) { | ||
return ( | ||
/** @type {import('./utils.js').GetterDescriptor} */ (previousElementSibling).get.call(this) ?? null | ||
); | ||
} | ||
|
||
let sibling = parentRealm.getPreviousSibling(this); | ||
while (sibling) { | ||
if (sibling.nodeType === 1) { | ||
return sibling; | ||
} | ||
sibling = parentRealm.getPreviousSibling(sibling); | ||
} | ||
return null; | ||
}, | ||
set: previousElementSibling.set, | ||
}); | ||
|
||
defineProperty(CharacterDataPrototype, 'nextElementSibling', { | ||
/** | ||
* @this {Element} | ||
*/ | ||
get() { | ||
const parentRealm = getParentRealm(this); | ||
if (!parentRealm) { | ||
return /** @type {import('./utils.js').GetterDescriptor} */ (nextElementSibling).get.call(this) ?? null; | ||
} | ||
|
||
let sibling = parentRealm.getNextSibling(this); | ||
while (sibling) { | ||
if (sibling.nodeType === 1) { | ||
return sibling; | ||
} | ||
sibling = parentRealm.getNextSibling(sibling); | ||
} | ||
return null; | ||
}, | ||
set: nextElementSibling.set, | ||
}); | ||
defineProperty(CharacterDataPrototype, 'after', { | ||
/** | ||
* @this {Element} | ||
* @param {(ChildNode | string)[]} nodes | ||
*/ | ||
value(...nodes) { | ||
const parentRealm = getParentRealm(this, true); | ||
if (!parentRealm) { | ||
return /** @type {import('./utils.js').ValueDescriptor} */ (after).value.apply(this, nodes); | ||
} | ||
const sibling = parentRealm.getNextSibling(this); | ||
if (sibling) { | ||
return parentRealm.insertBefore(sibling, ...nodes); | ||
} | ||
return parentRealm.append(...nodes); | ||
}, | ||
}); | ||
|
||
defineProperty(CharacterDataPrototype, 'before', { | ||
/** | ||
* @this {Element} | ||
* @param {(ChildNode | string)[]} nodes | ||
*/ | ||
value(...nodes) { | ||
const parentRealm = getParentRealm(this, true); | ||
if (!parentRealm) { | ||
return /** @type {import('./utils.js').ValueDescriptor} */ (before).value.apply(this, nodes); | ||
} | ||
return parentRealm.insertBefore(this, ...nodes); | ||
}, | ||
}); | ||
|
||
defineProperty(CharacterDataPrototype, 'replaceWith', { | ||
/** | ||
* @this {Element} | ||
* @param {(ChildNode | string)[]} nodes | ||
*/ | ||
value(...nodes) { | ||
const parentRealm = getParentRealm(this, true); | ||
if (!parentRealm) { | ||
return /** @type {import('./utils.js').ValueDescriptor} */ (replaceWith).value.apply(this, nodes); | ||
} | ||
return parentRealm.replaceWith(this, ...nodes); | ||
}, | ||
}); | ||
} |
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
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
Oops, something went wrong.