From 5077570d3ae3304463cfc1b75a5122b9957902f1 Mon Sep 17 00:00:00 2001 From: Enes Kartal <43950508+EnesKartal@users.noreply.github.com> Date: Thu, 19 Nov 2026 16:26:53 +0300 Subject: [PATCH] fix(angular): avoid NG0503 during SSR hydration serialization --- .../src/components/markdown-node.component.ts | 25 ++-- .../comark-angular/test/ssr-hydration.test.ts | 137 ++++++++++++++++++ test/bundle.test.ts | 2 +- 3 files changed, 151 insertions(+), 13 deletions(-) create mode 100644 packages/comark-angular/test/ssr-hydration.test.ts diff --git a/packages/comark-angular/src/components/markdown-node.component.ts b/packages/comark-angular/src/components/markdown-node.component.ts index 3c67a736..b27007c2 100644 --- a/packages/comark-angular/src/components/markdown-node.component.ts +++ b/packages/comark-angular/src/components/markdown-node.component.ts @@ -3,6 +3,7 @@ import { ComponentRef, ChangeDetectionStrategy, ViewContainerRef, + ViewChild, ElementRef, Renderer2, OnChanges, @@ -106,7 +107,8 @@ function prepareNode( selector: 'comark-markdown-node', standalone: true, changeDetection: ChangeDetectionStrategy.OnPush, - template: '', + template: '', + host: { ngSkipHydration: 'true' }, }) export class MarkdownNode implements OnChanges { /** The Comark AST node to render */ @@ -124,7 +126,8 @@ export class MarkdownNode implements OnChanges { /** Change the tag or props of element nodes just before they render */ readonly transformNode = input() - private vcr = inject(ViewContainerRef) + @ViewChild('container', { read: ViewContainerRef, static: true }) + private vcr!: ViewContainerRef private renderer = inject(Renderer2) private elementRef = inject(ElementRef) private injector = inject(Injector) @@ -137,8 +140,14 @@ export class MarkdownNode implements OnChanges { this.vcr.clear() const hostEl = this.elementRef.nativeElement as HTMLElement - while (hostEl.firstChild) { - hostEl.removeChild(hostEl.firstChild) + const anchor = this.vcr.element.nativeElement as Node + let child = hostEl.firstChild + while (child) { + const next = child.nextSibling + if (child !== anchor) { + hostEl.removeChild(child) + } + child = next } if (this.node() === undefined || this.node() === null) return @@ -325,14 +334,6 @@ export class MarkdownNode implements OnChanges { } this.vcr.insert(componentRef.hostView) - - // The container's anchor stays where this node was first attached, so when this node - // was moved into a parent element, place the component right after this node's host. - const hostEl = this.elementRef.nativeElement as HTMLElement - const hostParent = this.renderer.parentNode(hostEl) - if (hostParent) { - this.renderer.insertBefore(hostParent, componentRef.location.nativeElement, this.renderer.nextSibling(hostEl)) - } componentRef.changeDetectorRef.detectChanges() } diff --git a/packages/comark-angular/test/ssr-hydration.test.ts b/packages/comark-angular/test/ssr-hydration.test.ts new file mode 100644 index 00000000..60dec3dc --- /dev/null +++ b/packages/comark-angular/test/ssr-hydration.test.ts @@ -0,0 +1,137 @@ +import '@angular/compiler' +import { describe, expect, it } from 'vitest' +import { Component, input, provideZonelessChangeDetection } from '@angular/core' +import { TestBed } from '@angular/core/testing' +import { bootstrapApplication, provideClientHydration } from '@angular/platform-browser' +import { renderApplication } from '@angular/platform-server' +import { parseMarkdown, type MarkdownDocument as MarkdownDocumentType } from 'comark' +import { Markdown } from '../src/components/markdown.component.ts' +import { MarkdownDocument } from '../src/components/markdown-document.component.ts' +import { MarkdownNode } from '../src/components/markdown-node.component.ts' + +@Component({ + selector: 'markdown-code', + standalone: true, + template: `
+ +
`, +}) +class ProsePre { + readonly language = input('') + readonly code = input('') +} + +@Component({ + selector: 'custom-card', + standalone: true, + template: `
`, +}) +class CustomCard {} + +async function renderWithHydration( + template: string, + document: MarkdownDocumentType, + components: Record +): Promise { + class App { + document = document + components = components + } + Component({ + selector: 'app-root', + standalone: true, + imports: [Markdown, MarkdownDocument, MarkdownNode], + template, + })(App) + + return renderApplication( + (context) => + bootstrapApplication(App, { providers: [provideZonelessChangeDetection(), provideClientHydration()] }, context), + { document: '' } + ) +} + +describe('MarkdownNode view container anchor', () => { + it('preserves the internal view container anchor across re-renders', async () => { + await TestBed.configureTestingModule({ + imports: [MarkdownNode], + }).compileComponents() + + const fixture = TestBed.createComponent(MarkdownNode) + const hostEl = fixture.nativeElement as HTMLElement + + fixture.componentRef.setInput('components', { ProsePre }) + fixture.componentRef.setInput('node', ['p', {}, 'hello']) + fixture.detectChanges() + expect(hostEl.querySelector('p')?.textContent).toBe('hello') + + fixture.componentRef.setInput('node', ['pre', { language: 'ts' }, ['code', {}, 'const a = 1']]) + fixture.detectChanges() + expect(hostEl.querySelector('p')).toBeNull() + expect(hostEl.querySelector('markdown-code')?.textContent).toContain('const a = 1') + + fixture.componentRef.setInput('node', 'plain text') + fixture.detectChanges() + expect(hostEl.querySelector('markdown-code')).toBeNull() + expect(hostEl.textContent).toBe('plain text') + + fixture.componentRef.setInput('node', ['pre', { language: 'js' }, ['code', {}, 'const b = 2']]) + fixture.detectChanges() + expect(hostEl.querySelector('markdown-code')?.textContent).toContain('const b = 2') + }) +}) + +describe('SSR hydration serialization', () => { + it('renders a fenced code block through a custom ProsePre component with hydration enabled', async () => { + const doc = await parseMarkdown('```ts\nconst x = 1\n```') + const html = await renderWithHydration( + '', + doc, + { ProsePre } + ) + + expect(html).toContain('ngskiphydration="true"') + expect(html).toContain(' { + const doc = await parseMarkdown('```ts\nconst y = 2\n```') + const html = await renderWithHydration('', doc, { + ProsePre, + }) + + expect(html).toContain(' { + const doc = await parseMarkdown('::custom-card\n```ts\nconst z = 3\n```\n::') + const html = await renderWithHydration( + '', + doc, + { CustomCard, ProsePre } + ) + + expect(html).toContain(' { + const doc = await parseMarkdown('```ts\nconst solo = 4\n```') + const html = await renderWithHydration( + '', + doc, + { ProsePre } + ) + + expect(html).toContain(' { expect(report).toMatchInlineSnapshot(` { - "@comark/angular": "70.1k (63 files)", + "@comark/angular": "70.4k (63 files)", "@comark/ansi": "42.1k (109 files)", "@comark/html": "17.7k (69 files)", "@comark/nuxt": "11.0k (69 files)",