-
-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
d3566f4
commit ad3872d
Showing
92 changed files
with
6,945 additions
and
2,011 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
81 changes: 81 additions & 0 deletions
81
packages/happy-dom/src/custom-element/CustomElementReactionStack.ts
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,81 @@ | ||
import HTMLElement from '../nodes/html-element/HTMLElement.js'; | ||
import BrowserWindow from '../window/BrowserWindow.js'; | ||
import * as PropertySymbol from '../PropertySymbol.js'; | ||
import WindowBrowserContext from '../window/WindowBrowserContext.js'; | ||
|
||
/** | ||
* Custom element reaction stack. | ||
* | ||
* @see https://html.spec.whatwg.org/multipage/custom-elements.html#custom-element-reactions-stack | ||
*/ | ||
export default class CustomElementReactionStack { | ||
private window: BrowserWindow; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param window Window. | ||
*/ | ||
constructor(window: BrowserWindow) { | ||
this.window = window; | ||
} | ||
|
||
/** | ||
* Enqueues a custom element reaction. | ||
* | ||
* @see https://html.spec.whatwg.org/multipage/custom-elements.html#enqueue-a-custom-element-callback-reaction | ||
* @see https://html.spec.whatwg.org/multipage/custom-elements.html#enqueue-an-element-on-the-appropriate-element-queue | ||
* @param element Element. | ||
* @param callbackName Callback name. | ||
* @param [args] Arguments. | ||
*/ | ||
public enqueueReaction(element: HTMLElement, callbackName: string, args?: any[]): void { | ||
// If a polyfill is used, [PropertySymbol.registry] may be undefined | ||
const definition = this.window.customElements[PropertySymbol.registry]?.get(element.localName); | ||
|
||
if (!definition) { | ||
return; | ||
} | ||
|
||
// According to the spec, we should use a queue for each element and then invoke the reactions in the order they were enqueued asynchronously. | ||
// However, the browser seem to always invoke the reactions synchronously. | ||
// TODO: Can we find an example where the reactions are invoked asynchronously? In that case we should use a queue for those cases. | ||
|
||
switch (callbackName) { | ||
case 'connectedCallback': | ||
if (definition.livecycleCallbacks.connectedCallback) { | ||
const returnValue = definition.livecycleCallbacks.connectedCallback.call(element); | ||
|
||
/** | ||
* It is common to import dependencies in the connectedCallback() method of web components. | ||
* As Happy DOM doesn't have support for dynamic imports yet, this is a temporary solution to wait for imports in connectedCallback(). | ||
* | ||
* @see https://github.com/capricorn86/happy-dom/issues/1442 | ||
*/ | ||
if (returnValue instanceof Promise) { | ||
const asyncTaskManager = new WindowBrowserContext(this.window).getAsyncTaskManager(); | ||
if (asyncTaskManager) { | ||
const taskID = asyncTaskManager.startTask(); | ||
returnValue | ||
.then(() => asyncTaskManager.endTask(taskID)) | ||
.catch(() => asyncTaskManager.endTask(taskID)); | ||
} | ||
} | ||
} | ||
break; | ||
case 'disconnectedCallback': | ||
if (definition.livecycleCallbacks.disconnectedCallback) { | ||
definition.livecycleCallbacks.disconnectedCallback.call(element); | ||
} | ||
break; | ||
case 'attributeChangedCallback': | ||
if ( | ||
definition.livecycleCallbacks.attributeChangedCallback && | ||
definition.observedAttributes.has(args[0]) | ||
) { | ||
definition.livecycleCallbacks.attributeChangedCallback.apply(element, args); | ||
} | ||
break; | ||
} | ||
} | ||
} |
Oops, something went wrong.