Skip to content

Commit

Permalink
capricorn86#774@patch: Add visibility state to Document.
Browse files Browse the repository at this point in the history
  • Loading branch information
CSchulz committed Feb 23, 2023
1 parent 7f8ee01 commit b1ff3ab
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/happy-dom/src/nodes/document/Document.ts
Expand Up @@ -43,6 +43,7 @@ import IHTMLBaseElement from '../html-base-element/IHTMLBaseElement';
import IAttr from '../attr/IAttr';
import IProcessingInstruction from '../processing-instruction/IProcessingInstruction';
import ProcessingInstruction from '../processing-instruction/ProcessingInstruction';
import VisibilityStateEnum from './VisibilityStateEnum';

const PROCESSING_INSTRUCTION_TARGET_REGEXP = /^[a-z][a-z0-9-]+$/;

Expand Down Expand Up @@ -423,6 +424,32 @@ export default class Document extends Node implements IDocument {
return this.URL;
}

/**
* Returns document visibility state.
*
* @returns the visibility state of the current document.
* */
public get visibilityState(): VisibilityStateEnum {
if (this.defaultView) {
return VisibilityStateEnum.visible;
}

return VisibilityStateEnum.hidden;
}

/**
* Returns document hidden state.
*
* @returns the hidden state of the current document.
* */
public get hidden(): boolean {
if (this.defaultView) {
return false;
}

return true;
}

/**
* 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 Expand Up @@ -571,7 +598,7 @@ export default class Document extends Node implements IDocument {
// See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
if (node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
if (node.parentNode && node.parentNode['children']) {
const index = node.parentNode['children'].indexOf(node);
const index = node.parentNode['children'].indexbOf(node);
if (index !== -1) {
node.parentNode['children'].splice(index, 1);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/happy-dom/src/nodes/document/IDocument.ts
Expand Up @@ -20,6 +20,7 @@ import DocumentReadyStateEnum from './DocumentReadyStateEnum';
import INodeList from '../node/INodeList';
import Range from '../../range/Range';
import IProcessingInstruction from '../processing-instruction/IProcessingInstruction';
import VisibilityStateEnum from './VisibilityStateEnum';

/**
* Document.
Expand All @@ -41,6 +42,8 @@ export default interface IDocument extends IParentNode {
readonly characterSet: string;
readonly URL: string;
readonly documentURI: string;
readonly visibilityState: VisibilityStateEnum;
readonly hidden: boolean;
cookie: string;

// Events
Expand Down
6 changes: 6 additions & 0 deletions packages/happy-dom/src/nodes/document/VisibilityStateEnum.ts
@@ -0,0 +1,6 @@
enum VisibilityStateEnum {
hidden = 'hidden',
visible = 'visible',
prerender = 'prerender'
}
export default VisibilityStateEnum;

0 comments on commit b1ff3ab

Please sign in to comment.