happy-dom
Version:
Happy DOM is a JavaScript implementation of a web browser without its graphical user interface. It includes many web standards from WHATWG DOM and HTML.
108 lines (99 loc) • 2.82 kB
text/typescript
import IVirtualConsoleLogEntry from './IVirtualConsoleLogEntry.js';
import VirtualConsoleLogLevelEnum from './enums/VirtualConsoleLogLevelEnum.js';
import Event from '../event/Event.js';
import VirtualConsoleLogEntryStringifier from './utilities/VirtualConsoleLogEntryStringifier.js';
import IVirtualConsolePrinter from './IVirtualConsolePrinter.js';
/**
* Virtual console printer.
*/
export default class VirtualConsolePrinter implements IVirtualConsolePrinter {
print: Array<(event: Event) => void>;
clear: Array<(event: Event) => void>;
} = { print: [], clear: [] };
/**
* Writes to the output.
*
* @param logEntry Log entry.
*/
public print(logEntry: IVirtualConsoleLogEntry): void {
this.
this.dispatchEvent(new Event('print'));
}
/**
* Clears the output.
*/
public clear(): void {
this.
this.dispatchEvent(new Event('clear'));
}
/**
* Adds an event listener.
*
* @param eventType Event type ("print" or "clear").
* @param listener Listener.
*/
public addEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void {
if (!this.
throw new Error(`Event type "${eventType}" is not supported.`);
}
this.
}
/**
* Removes an event listener.
*
* @param eventType Event type ("print" or "clear").
* @param listener Listener.
*/
public removeEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void {
if (!this.
throw new Error(`Event type "${eventType}" is not supported.`);
}
const index = this.
if (index !== -1) {
this.
}
}
/**
* Dispatches an event.
*
* @param event Event.
*/
public dispatchEvent(event: Event): void {
if (!this.
throw new Error(`Event type "${event.type}" is not supported.`);
}
for (const listener of this.
listener(event);
}
}
/**
* Reads the buffer.
*
* @returns Console log entries.
*/
public read(): IVirtualConsoleLogEntry[] {
const logEntries = this.
this.
return logEntries;
}
/**
* Returns the buffer as a string.
*
* @param [logLevel] Log level.
* @returns Buffer as a string of concatenated log entries.
*/
public readAsString(
logLevel: VirtualConsoleLogLevelEnum = VirtualConsoleLogLevelEnum.log
): string {
const logEntries = this.read();
let output = '';
for (const logEntry of logEntries) {
if (logEntry.level >= logLevel) {
output += VirtualConsoleLogEntryStringifier.toString(logEntry);
}
}
return output;
}
}