Skip to content

Commit

Permalink
Merge branch 'master' into 783-fixes-xmlhttprequest-response-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 authored Feb 24, 2023
2 parents 1bf4473 + ca0d075 commit 80c621b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
27 changes: 27 additions & 0 deletions packages/happy-dom/src/nodes/document/Document.ts
Original file line number Diff line number Diff line change
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
3 changes: 3 additions & 0 deletions packages/happy-dom/src/nodes/document/IDocument.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum VisibilityStateEnum {
hidden = 'hidden',
visible = 'visible',
prerender = 'prerender'
}
export default VisibilityStateEnum;
34 changes: 20 additions & 14 deletions packages/happy-dom/src/nodes/html-element/HTMLElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,15 +383,18 @@ export default class HTMLElement extends Element implements IHTMLElement {

this.ownerDocument['_activeElement'] = null;

for (const eventType of ['blur', 'focusout']) {
const event = new FocusEvent(eventType, {
this.dispatchEvent(
new FocusEvent('blur', {
bubbles: false,
composed: true
})
);
this.dispatchEvent(
new FocusEvent('focusout', {
bubbles: true,
composed: true
});
event._target = this;
event._currentTarget = this;
this.dispatchEvent(event);
}
})
);
}

/**
Expand All @@ -408,15 +411,18 @@ export default class HTMLElement extends Element implements IHTMLElement {

this.ownerDocument['_activeElement'] = this;

for (const eventType of ['focus', 'focusin']) {
const event = new FocusEvent(eventType, {
this.dispatchEvent(
new FocusEvent('focus', {
bubbles: false,
composed: true
})
);
this.dispatchEvent(
new FocusEvent('focusin', {
bubbles: true,
composed: true
});
event._target = this;
event._currentTarget = this;
this.dispatchEvent(event);
}
})
);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/happy-dom/src/nodes/node/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ export default class Node extends EventTarget implements INode {
* @returns "true" if this node contains the other node.
*/
public contains(otherNode: INode): boolean {
if (this === otherNode) {
return true;
}
for (const childNode of this.childNodes) {
if (childNode === otherNode || childNode.contains(otherNode)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ describe('HTMLElement', () => {
element.blur();

expect(triggeredBlurEvent.type).toBe('blur');
expect(triggeredBlurEvent.bubbles).toBe(true);
expect(triggeredBlurEvent.bubbles).toBe(false);
expect(triggeredBlurEvent.composed).toBe(true);
expect(triggeredBlurEvent.target === element).toBe(true);

Expand Down Expand Up @@ -446,7 +446,7 @@ describe('HTMLElement', () => {
element.focus();

expect(triggeredFocusEvent.type).toBe('focus');
expect(triggeredFocusEvent.bubbles).toBe(true);
expect(triggeredFocusEvent.bubbles).toBe(false);
expect(triggeredFocusEvent.composed).toBe(true);
expect(triggeredFocusEvent.target === element).toBe(true);

Expand Down Expand Up @@ -502,7 +502,7 @@ describe('HTMLElement', () => {
element.focus();

expect(triggeredEvent.type).toBe('blur');
expect(triggeredEvent.bubbles).toBe(true);
expect(triggeredEvent.bubbles).toBe(false);
expect(triggeredEvent.composed).toBe(true);
expect(triggeredEvent.target === previousElement).toBe(true);
});
Expand Down

0 comments on commit 80c621b

Please sign in to comment.