Skip to content

Commit

Permalink
capricorn86#1099@minor: Add support for currentScript.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckhampus committed Sep 27, 2023
1 parent c06f50d commit 1c0174a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/happy-dom/src/nodes/document/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default class Document extends Node implements IDocument {
public readonly _readyStateManager: DocumentReadyStateManager;
public readonly _children: IHTMLCollection<IElement> = new HTMLCollection<IElement>();
public _activeElement: IHTMLElement = null;
public _currentScript: IHTMLScriptElement = null;

// Used as an unique identifier which is updated whenever the DOM gets modified.
public _cacheID = 0;
Expand Down Expand Up @@ -501,6 +502,15 @@ export default class Document extends Node implements IDocument {
return true;
}

/**
* Gets the currently executing script element.
*
* @returns the currently executing script element.
*/
public get currentScript(): IHTMLScriptElement {
return this._currentScript;
}

/**
* Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
*
Expand Down
1 change: 1 addition & 0 deletions packages/happy-dom/src/nodes/document/IDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default interface IDocument extends IParentNode {
readonly hidden: boolean;
readonly links: IHTMLCollection<IHTMLElement>;
readonly referrer: string;
readonly currentScript: IHTMLScriptElement;
cookie: string;
title: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,18 @@ export default class HTMLScriptElement extends HTMLElement implements IHTMLScrip
type === 'application/x-javascript' ||
type.startsWith('text/javascript'))
) {
if (this.ownerDocument.defaultView.happyDOM.settings.disableErrorCapturing) {
this.ownerDocument.defaultView.eval(textContent);
} else {
WindowErrorUtility.captureError(this.ownerDocument.defaultView, () =>
this.ownerDocument.defaultView.eval(textContent)
);
try {
this.ownerDocument['_currentScript'] = this;

if (this.ownerDocument.defaultView.happyDOM.settings.disableErrorCapturing) {
this.ownerDocument.defaultView.eval(textContent);
} else {
WindowErrorUtility.captureError(this.ownerDocument.defaultView, () =>
this.ownerDocument.defaultView.eval(textContent)
);
}
} finally {
this.ownerDocument['_currentScript'] = null;
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions packages/happy-dom/test/nodes/document/Document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1290,4 +1290,20 @@ describe('Document', () => {
}
});
});

describe('currentScript', () => {
it('Returns the currently executing script element.', () => {
expect(document.currentScript).toBe(null);
const script1 = document.createElement('script');
script1.textContent = 'window.test = document.currentScript;';
document.body.appendChild(script1);
expect(window['test']).toBe(script1);
expect(document.currentScript).toBe(null);
const script2 = document.createElement('script');
script2.textContent = 'window.test = document.currentScript;';
document.body.appendChild(script2);
expect(window['test']).toBe(script2);
expect(document.currentScript).toBe(null);
});
});
});

0 comments on commit 1c0174a

Please sign in to comment.