diff --git a/packages/happy-dom/src/html-serializer/HTMLSerializer.ts b/packages/happy-dom/src/html-serializer/HTMLSerializer.ts index 58b90d4dd..61bd1cb83 100644 --- a/packages/happy-dom/src/html-serializer/HTMLSerializer.ts +++ b/packages/happy-dom/src/html-serializer/HTMLSerializer.ts @@ -15,7 +15,7 @@ import HTMLElementConfigContentModelEnum from '../config/HTMLElementConfigConten * Serializes a node into HTML. */ export default class HTMLSerializer { - public [PropertySymbol.options]: { + private options: { serializableShadowRoots: boolean; shadowRoots: ShadowRoot[] | null; allShadowRoots: boolean; @@ -25,6 +25,34 @@ export default class HTMLSerializer { allShadowRoots: false }; + /** + * Constructor. + * + * @param [options] Options. + * @param [options.serializableShadowRoots] If shadow roots should be serialized. + * @param [options.shadowRoots] Shadow roots to serialize. + * @param [options.allShadowRoots] If all shadow roots should be serialized. + */ + constructor(options?: { + serializableShadowRoots?: boolean; + shadowRoots?: ShadowRoot[] | null; + allShadowRoots?: boolean; + }) { + if (options) { + if (options.serializableShadowRoots) { + this.options.serializableShadowRoots = options.serializableShadowRoots; + } + + if (options.shadowRoots) { + this.options.shadowRoots = options.shadowRoots; + } + + if (options.allShadowRoots) { + this.options.allShadowRoots = options.allShadowRoots; + } + } + } + /** * Renders an element as HTML. * @@ -32,8 +60,6 @@ export default class HTMLSerializer { * @returns Result. */ public serializeToString(root: Node): string { - const options = this[PropertySymbol.options]; - switch (root[PropertySymbol.nodeType]) { case NodeTypeEnum.elementNode: const element = root; @@ -49,9 +75,10 @@ export default class HTMLSerializer { // TODO: Should we include closed shadow roots? We are currently only including open shadow roots. if ( element.shadowRoot && - (options.allShadowRoots || - (options.serializableShadowRoots && element.shadowRoot[PropertySymbol.serializable]) || - options.shadowRoots?.includes(element.shadowRoot)) + (this.options.allShadowRoots || + (this.options.serializableShadowRoots && + element.shadowRoot[PropertySymbol.serializable]) || + this.options.shadowRoots?.includes(element.shadowRoot)) ) { innerHTML += `