diff --git a/apps/web/public/r/forme/utils.json b/apps/web/public/r/forme/utils.json index 52931d1..88c4705 100644 --- a/apps/web/public/r/forme/utils.json +++ b/apps/web/public/r/forme/utils.json @@ -25,7 +25,7 @@ }, { "path": "registry/lib/pdf-renderer.ts", - "content": "import {\n DOCUMENT,\n ElementRef,\n RendererFactory2,\n createComponent,\n createEnvironmentInjector,\n inject,\n platformCore,\n} from \"@angular/core\";\nimport type {\n EnvironmentInjector,\n Provider,\n Renderer2,\n Type,\n} from \"@angular/core\";\n\n/** An element in the tree an Angular component renders into. */\nexport class PdfElement {\n readonly name: string;\n readonly namespace: string | null;\n parent: PdfElement | null = null;\n readonly children: PdfNode[] = [];\n readonly attributes: Record = {};\n /** The primitive directive on this element. Elements without one are transparent. */\n primitive: PdfPrimitive | null = null;\n\n constructor(name: string, namespace: string | null) {\n this.name = name;\n this.namespace = namespace;\n }\n\n /** Angular reads hydration markers through this in apps with client hydration. */\n getAttribute(name: string): string | null {\n return this.attributes[name] ?? null;\n }\n}\n\nexport class PdfTextNode {\n parent: PdfElement | null = null;\n\n value: string;\n\n constructor(value: string) {\n this.value = value;\n }\n}\n\n/** Angular's anchor for control flow and projection. Serializers skip it. */\nexport class PdfComment {\n parent: PdfElement | null = null;\n}\n\nexport type PdfNode = PdfElement | PdfTextNode | PdfComment;\n\n/** Base class for the directives that map to engine nodes. */\nexport abstract class PdfPrimitive {\n readonly element: PdfElement =\n inject>(ElementRef).nativeElement;\n\n constructor() {\n this.element.primitive = this;\n }\n}\n\nconst detach = (node: PdfNode) => {\n if (node.parent) {\n node.parent.children.splice(node.parent.children.indexOf(node), 1);\n node.parent = null;\n }\n};\n\n// oxlint-disable-next-line no-empty-function -- Renderer2 hooks the PDF tree ignores\nconst noop = () => {};\n\nconst renderer: Renderer2 = {\n addClass: noop,\n appendChild(parent: PdfElement, child: PdfNode) {\n detach(child);\n child.parent = parent;\n parent.children.push(child);\n },\n createComment: () => new PdfComment(),\n createElement: (name: string, namespace?: string | null) =>\n new PdfElement(name, namespace ?? null),\n createText: (value: string) => new PdfTextNode(value),\n data: {},\n destroy: noop,\n destroyNode: null,\n insertBefore(parent: PdfElement, child: PdfNode, ref: PdfNode | null) {\n detach(child);\n child.parent = parent;\n const index = ref ? parent.children.indexOf(ref) : -1;\n parent.children.splice(\n index === -1 ? parent.children.length : index,\n 0,\n child\n );\n },\n listen: () => noop,\n nextSibling(node: PdfNode) {\n const siblings = node.parent?.children;\n return siblings?.[siblings.indexOf(node) + 1] ?? null;\n },\n parentNode: (node: PdfNode) => node.parent,\n removeAttribute(element: PdfElement, name: string) {\n Reflect.deleteProperty(element.attributes, name);\n },\n removeChild(_parent: PdfElement | null, child: PdfNode) {\n detach(child);\n },\n removeClass: noop,\n removeStyle: noop,\n selectRootElement: (node: PdfElement) => node,\n setAttribute(element: PdfElement, name: string, value: string) {\n element.attributes[name] = value;\n },\n setProperty: noop,\n setStyle: noop,\n setValue(node: PdfTextNode, value: string) {\n node.value = value;\n },\n};\n\nconst rendererFactory: RendererFactory2 = {\n createRenderer: () => renderer,\n};\n\nexport interface RenderOptions {\n /** Values for the component's inputs. */\n inputs?: Partial>;\n providers?: Provider[];\n}\n\n/** Renders a component once and reads its element tree before tearing it down. */\nexport const renderTree = (\n component: Type,\n { inputs = {}, providers = [] }: RenderOptions,\n read: (root: PdfElement) => R\n): R => {\n const environmentInjector = createEnvironmentInjector(\n [\n { provide: RendererFactory2, useValue: rendererFactory },\n // Angular looks up a style host on creation. PDF components have no styles.\n { provide: DOCUMENT, useValue: { head: null } },\n ...providers,\n ],\n platformCore().injector as EnvironmentInjector\n );\n const ref = createComponent(component, { environmentInjector });\n try {\n for (const [name, value] of Object.entries(inputs)) {\n ref.setInput(name, value);\n }\n ref.changeDetectorRef.detectChanges();\n return read(ref.location.nativeElement as PdfElement);\n } finally {\n ref.destroy();\n environmentInjector.destroy();\n }\n};\n\n/** Elements without a primitive pass their children through, like component hosts. */\nexport const contentOf = (element: PdfElement): (PdfElement | PdfTextNode)[] =>\n element.children.flatMap((child) => {\n if (child instanceof PdfComment) {\n return [];\n }\n if (child instanceof PdfElement && !child.primitive && !child.namespace) {\n return contentOf(child);\n }\n return [child];\n });\n\n/** All text under a node, in document order. */\nexport const textOf = (node: PdfNode): string => {\n if (node instanceof PdfTextNode) {\n return node.value;\n }\n if (node instanceof PdfElement) {\n return node.children.map(textOf).join(\"\");\n }\n return \"\";\n};\n", + "content": "import {\n DOCUMENT,\n ElementRef,\n RendererFactory2,\n createComponent,\n createEnvironmentInjector,\n getPlatform,\n inject,\n platformCore,\n} from \"@angular/core\";\nimport type {\n EnvironmentInjector,\n Provider,\n Renderer2,\n Type,\n} from \"@angular/core\";\n\n/** An element in the tree an Angular component renders into. */\nexport class PdfElement {\n readonly name: string;\n readonly namespace: string | null;\n parent: PdfElement | null = null;\n readonly children: PdfNode[] = [];\n readonly attributes: Record = {};\n /** The primitive directive on this element. Elements without one are transparent. */\n primitive: PdfPrimitive | null = null;\n\n constructor(name: string, namespace: string | null) {\n this.name = name;\n this.namespace = namespace;\n }\n\n /** Angular reads hydration markers through this in apps with client hydration. */\n getAttribute(name: string): string | null {\n return this.attributes[name] ?? null;\n }\n}\n\nexport class PdfTextNode {\n parent: PdfElement | null = null;\n\n value: string;\n\n constructor(value: string) {\n this.value = value;\n }\n}\n\n/** Angular's anchor for control flow and projection. Serializers skip it. */\nexport class PdfComment {\n parent: PdfElement | null = null;\n}\n\nexport type PdfNode = PdfElement | PdfTextNode | PdfComment;\n\n/** Base class for the directives that map to engine nodes. */\nexport abstract class PdfPrimitive {\n readonly element: PdfElement =\n inject>(ElementRef).nativeElement;\n\n constructor() {\n this.element.primitive = this;\n }\n}\n\nconst detach = (node: PdfNode) => {\n if (node.parent) {\n node.parent.children.splice(node.parent.children.indexOf(node), 1);\n node.parent = null;\n }\n};\n\n// oxlint-disable-next-line no-empty-function -- Renderer2 hooks the PDF tree ignores\nconst noop = () => {};\n\nconst renderer: Renderer2 = {\n addClass: noop,\n appendChild(parent: PdfElement, child: PdfNode) {\n detach(child);\n child.parent = parent;\n parent.children.push(child);\n },\n createComment: () => new PdfComment(),\n createElement: (name: string, namespace?: string | null) =>\n new PdfElement(name, namespace ?? null),\n createText: (value: string) => new PdfTextNode(value),\n data: {},\n destroy: noop,\n destroyNode: null,\n insertBefore(parent: PdfElement, child: PdfNode, ref: PdfNode | null) {\n detach(child);\n child.parent = parent;\n const index = ref ? parent.children.indexOf(ref) : -1;\n parent.children.splice(\n index === -1 ? parent.children.length : index,\n 0,\n child\n );\n },\n listen: () => noop,\n nextSibling(node: PdfNode) {\n const siblings = node.parent?.children;\n return siblings?.[siblings.indexOf(node) + 1] ?? null;\n },\n parentNode: (node: PdfNode) => node.parent,\n removeAttribute(element: PdfElement, name: string) {\n Reflect.deleteProperty(element.attributes, name);\n },\n removeChild(_parent: PdfElement | null, child: PdfNode) {\n detach(child);\n },\n removeClass: noop,\n removeStyle: noop,\n selectRootElement: (node: PdfElement) => node,\n setAttribute(element: PdfElement, name: string, value: string) {\n element.attributes[name] = value;\n },\n setProperty: noop,\n setStyle: noop,\n setValue(node: PdfTextNode, value: string) {\n node.value = value;\n },\n};\n\nconst rendererFactory: RendererFactory2 = {\n createRenderer: () => renderer,\n};\n\nexport interface RenderOptions {\n /** Values for the component's inputs. */\n inputs?: Partial>;\n providers?: Provider[];\n}\n\n/** Renders a component once and reads its element tree before tearing it down. */\nexport const renderTree = (\n component: Type,\n { inputs = {}, providers = [] }: RenderOptions,\n read: (root: PdfElement) => R\n): R => {\n const environmentInjector = createEnvironmentInjector(\n [\n { provide: RendererFactory2, useValue: rendererFactory },\n // Angular looks up a style host on creation. PDF components have no styles.\n { provide: DOCUMENT, useValue: { head: null } },\n ...providers,\n ],\n // bootstrapApplication's platform lacks the marker platformCore() asserts in dev mode.\n (getPlatform() ?? platformCore()).injector as EnvironmentInjector\n );\n const ref = createComponent(component, { environmentInjector });\n try {\n for (const [name, value] of Object.entries(inputs)) {\n ref.setInput(name, value);\n }\n ref.changeDetectorRef.detectChanges();\n return read(ref.location.nativeElement as PdfElement);\n } finally {\n ref.destroy();\n environmentInjector.destroy();\n }\n};\n\n/** Elements without a primitive pass their children through, like component hosts. */\nexport const contentOf = (element: PdfElement): (PdfElement | PdfTextNode)[] =>\n element.children.flatMap((child) => {\n if (child instanceof PdfComment) {\n return [];\n }\n if (child instanceof PdfElement && !child.primitive && !child.namespace) {\n return contentOf(child);\n }\n return [child];\n });\n\n/** All text under a node, in document order. */\nexport const textOf = (node: PdfNode): string => {\n if (node instanceof PdfTextNode) {\n return node.value;\n }\n if (node instanceof PdfElement) {\n return node.children.map(textOf).join(\"\");\n }\n return \"\";\n};\n", "type": "registry:lib", "target": "lib/pdf-renderer.ts" }, diff --git a/apps/web/public/r/takumi/utils.json b/apps/web/public/r/takumi/utils.json index 189f7d0..aaf888b 100644 --- a/apps/web/public/r/takumi/utils.json +++ b/apps/web/public/r/takumi/utils.json @@ -25,7 +25,7 @@ }, { "path": "registry/lib/pdf-renderer.ts", - "content": "import {\n DOCUMENT,\n ElementRef,\n RendererFactory2,\n createComponent,\n createEnvironmentInjector,\n inject,\n platformCore,\n} from \"@angular/core\";\nimport type {\n EnvironmentInjector,\n Provider,\n Renderer2,\n Type,\n} from \"@angular/core\";\n\n/** An element in the tree an Angular component renders into. */\nexport class PdfElement {\n readonly name: string;\n readonly namespace: string | null;\n parent: PdfElement | null = null;\n readonly children: PdfNode[] = [];\n readonly attributes: Record = {};\n /** The primitive directive on this element. Elements without one are transparent. */\n primitive: PdfPrimitive | null = null;\n\n constructor(name: string, namespace: string | null) {\n this.name = name;\n this.namespace = namespace;\n }\n\n /** Angular reads hydration markers through this in apps with client hydration. */\n getAttribute(name: string): string | null {\n return this.attributes[name] ?? null;\n }\n}\n\nexport class PdfTextNode {\n parent: PdfElement | null = null;\n\n value: string;\n\n constructor(value: string) {\n this.value = value;\n }\n}\n\n/** Angular's anchor for control flow and projection. Serializers skip it. */\nexport class PdfComment {\n parent: PdfElement | null = null;\n}\n\nexport type PdfNode = PdfElement | PdfTextNode | PdfComment;\n\n/** Base class for the directives that map to engine nodes. */\nexport abstract class PdfPrimitive {\n readonly element: PdfElement =\n inject>(ElementRef).nativeElement;\n\n constructor() {\n this.element.primitive = this;\n }\n}\n\nconst detach = (node: PdfNode) => {\n if (node.parent) {\n node.parent.children.splice(node.parent.children.indexOf(node), 1);\n node.parent = null;\n }\n};\n\n// oxlint-disable-next-line no-empty-function -- Renderer2 hooks the PDF tree ignores\nconst noop = () => {};\n\nconst renderer: Renderer2 = {\n addClass: noop,\n appendChild(parent: PdfElement, child: PdfNode) {\n detach(child);\n child.parent = parent;\n parent.children.push(child);\n },\n createComment: () => new PdfComment(),\n createElement: (name: string, namespace?: string | null) =>\n new PdfElement(name, namespace ?? null),\n createText: (value: string) => new PdfTextNode(value),\n data: {},\n destroy: noop,\n destroyNode: null,\n insertBefore(parent: PdfElement, child: PdfNode, ref: PdfNode | null) {\n detach(child);\n child.parent = parent;\n const index = ref ? parent.children.indexOf(ref) : -1;\n parent.children.splice(\n index === -1 ? parent.children.length : index,\n 0,\n child\n );\n },\n listen: () => noop,\n nextSibling(node: PdfNode) {\n const siblings = node.parent?.children;\n return siblings?.[siblings.indexOf(node) + 1] ?? null;\n },\n parentNode: (node: PdfNode) => node.parent,\n removeAttribute(element: PdfElement, name: string) {\n Reflect.deleteProperty(element.attributes, name);\n },\n removeChild(_parent: PdfElement | null, child: PdfNode) {\n detach(child);\n },\n removeClass: noop,\n removeStyle: noop,\n selectRootElement: (node: PdfElement) => node,\n setAttribute(element: PdfElement, name: string, value: string) {\n element.attributes[name] = value;\n },\n setProperty: noop,\n setStyle: noop,\n setValue(node: PdfTextNode, value: string) {\n node.value = value;\n },\n};\n\nconst rendererFactory: RendererFactory2 = {\n createRenderer: () => renderer,\n};\n\nexport interface RenderOptions {\n /** Values for the component's inputs. */\n inputs?: Partial>;\n providers?: Provider[];\n}\n\n/** Renders a component once and reads its element tree before tearing it down. */\nexport const renderTree = (\n component: Type,\n { inputs = {}, providers = [] }: RenderOptions,\n read: (root: PdfElement) => R\n): R => {\n const environmentInjector = createEnvironmentInjector(\n [\n { provide: RendererFactory2, useValue: rendererFactory },\n // Angular looks up a style host on creation. PDF components have no styles.\n { provide: DOCUMENT, useValue: { head: null } },\n ...providers,\n ],\n platformCore().injector as EnvironmentInjector\n );\n const ref = createComponent(component, { environmentInjector });\n try {\n for (const [name, value] of Object.entries(inputs)) {\n ref.setInput(name, value);\n }\n ref.changeDetectorRef.detectChanges();\n return read(ref.location.nativeElement as PdfElement);\n } finally {\n ref.destroy();\n environmentInjector.destroy();\n }\n};\n\n/** Elements without a primitive pass their children through, like component hosts. */\nexport const contentOf = (element: PdfElement): (PdfElement | PdfTextNode)[] =>\n element.children.flatMap((child) => {\n if (child instanceof PdfComment) {\n return [];\n }\n if (child instanceof PdfElement && !child.primitive && !child.namespace) {\n return contentOf(child);\n }\n return [child];\n });\n\n/** All text under a node, in document order. */\nexport const textOf = (node: PdfNode): string => {\n if (node instanceof PdfTextNode) {\n return node.value;\n }\n if (node instanceof PdfElement) {\n return node.children.map(textOf).join(\"\");\n }\n return \"\";\n};\n", + "content": "import {\n DOCUMENT,\n ElementRef,\n RendererFactory2,\n createComponent,\n createEnvironmentInjector,\n getPlatform,\n inject,\n platformCore,\n} from \"@angular/core\";\nimport type {\n EnvironmentInjector,\n Provider,\n Renderer2,\n Type,\n} from \"@angular/core\";\n\n/** An element in the tree an Angular component renders into. */\nexport class PdfElement {\n readonly name: string;\n readonly namespace: string | null;\n parent: PdfElement | null = null;\n readonly children: PdfNode[] = [];\n readonly attributes: Record = {};\n /** The primitive directive on this element. Elements without one are transparent. */\n primitive: PdfPrimitive | null = null;\n\n constructor(name: string, namespace: string | null) {\n this.name = name;\n this.namespace = namespace;\n }\n\n /** Angular reads hydration markers through this in apps with client hydration. */\n getAttribute(name: string): string | null {\n return this.attributes[name] ?? null;\n }\n}\n\nexport class PdfTextNode {\n parent: PdfElement | null = null;\n\n value: string;\n\n constructor(value: string) {\n this.value = value;\n }\n}\n\n/** Angular's anchor for control flow and projection. Serializers skip it. */\nexport class PdfComment {\n parent: PdfElement | null = null;\n}\n\nexport type PdfNode = PdfElement | PdfTextNode | PdfComment;\n\n/** Base class for the directives that map to engine nodes. */\nexport abstract class PdfPrimitive {\n readonly element: PdfElement =\n inject>(ElementRef).nativeElement;\n\n constructor() {\n this.element.primitive = this;\n }\n}\n\nconst detach = (node: PdfNode) => {\n if (node.parent) {\n node.parent.children.splice(node.parent.children.indexOf(node), 1);\n node.parent = null;\n }\n};\n\n// oxlint-disable-next-line no-empty-function -- Renderer2 hooks the PDF tree ignores\nconst noop = () => {};\n\nconst renderer: Renderer2 = {\n addClass: noop,\n appendChild(parent: PdfElement, child: PdfNode) {\n detach(child);\n child.parent = parent;\n parent.children.push(child);\n },\n createComment: () => new PdfComment(),\n createElement: (name: string, namespace?: string | null) =>\n new PdfElement(name, namespace ?? null),\n createText: (value: string) => new PdfTextNode(value),\n data: {},\n destroy: noop,\n destroyNode: null,\n insertBefore(parent: PdfElement, child: PdfNode, ref: PdfNode | null) {\n detach(child);\n child.parent = parent;\n const index = ref ? parent.children.indexOf(ref) : -1;\n parent.children.splice(\n index === -1 ? parent.children.length : index,\n 0,\n child\n );\n },\n listen: () => noop,\n nextSibling(node: PdfNode) {\n const siblings = node.parent?.children;\n return siblings?.[siblings.indexOf(node) + 1] ?? null;\n },\n parentNode: (node: PdfNode) => node.parent,\n removeAttribute(element: PdfElement, name: string) {\n Reflect.deleteProperty(element.attributes, name);\n },\n removeChild(_parent: PdfElement | null, child: PdfNode) {\n detach(child);\n },\n removeClass: noop,\n removeStyle: noop,\n selectRootElement: (node: PdfElement) => node,\n setAttribute(element: PdfElement, name: string, value: string) {\n element.attributes[name] = value;\n },\n setProperty: noop,\n setStyle: noop,\n setValue(node: PdfTextNode, value: string) {\n node.value = value;\n },\n};\n\nconst rendererFactory: RendererFactory2 = {\n createRenderer: () => renderer,\n};\n\nexport interface RenderOptions {\n /** Values for the component's inputs. */\n inputs?: Partial>;\n providers?: Provider[];\n}\n\n/** Renders a component once and reads its element tree before tearing it down. */\nexport const renderTree = (\n component: Type,\n { inputs = {}, providers = [] }: RenderOptions,\n read: (root: PdfElement) => R\n): R => {\n const environmentInjector = createEnvironmentInjector(\n [\n { provide: RendererFactory2, useValue: rendererFactory },\n // Angular looks up a style host on creation. PDF components have no styles.\n { provide: DOCUMENT, useValue: { head: null } },\n ...providers,\n ],\n // bootstrapApplication's platform lacks the marker platformCore() asserts in dev mode.\n (getPlatform() ?? platformCore()).injector as EnvironmentInjector\n );\n const ref = createComponent(component, { environmentInjector });\n try {\n for (const [name, value] of Object.entries(inputs)) {\n ref.setInput(name, value);\n }\n ref.changeDetectorRef.detectChanges();\n return read(ref.location.nativeElement as PdfElement);\n } finally {\n ref.destroy();\n environmentInjector.destroy();\n }\n};\n\n/** Elements without a primitive pass their children through, like component hosts. */\nexport const contentOf = (element: PdfElement): (PdfElement | PdfTextNode)[] =>\n element.children.flatMap((child) => {\n if (child instanceof PdfComment) {\n return [];\n }\n if (child instanceof PdfElement && !child.primitive && !child.namespace) {\n return contentOf(child);\n }\n return [child];\n });\n\n/** All text under a node, in document order. */\nexport const textOf = (node: PdfNode): string => {\n if (node instanceof PdfTextNode) {\n return node.value;\n }\n if (node instanceof PdfElement) {\n return node.children.map(textOf).join(\"\");\n }\n return \"\";\n};\n", "type": "registry:lib", "target": "lib/pdf-renderer.ts" }, diff --git a/apps/web/registry/lib/pdf-renderer.ts b/apps/web/registry/lib/pdf-renderer.ts index bd6a197..8e2cc19 100644 --- a/apps/web/registry/lib/pdf-renderer.ts +++ b/apps/web/registry/lib/pdf-renderer.ts @@ -4,6 +4,7 @@ import { RendererFactory2, createComponent, createEnvironmentInjector, + getPlatform, inject, platformCore, } from "@angular/core"; @@ -144,7 +145,8 @@ export const renderTree = ( { provide: DOCUMENT, useValue: { head: null } }, ...providers, ], - platformCore().injector as EnvironmentInjector + // bootstrapApplication's platform lacks the marker platformCore() asserts in dev mode. + (getPlatform() ?? platformCore()).injector as EnvironmentInjector ); const ref = createComponent(component, { environmentInjector }); try { diff --git a/apps/web/tests/behavior.test.ts b/apps/web/tests/behavior.test.ts index f0d2dde..588045c 100644 --- a/apps/web/tests/behavior.test.ts +++ b/apps/web/tests/behavior.test.ts @@ -1,5 +1,9 @@ // Behavior the original demos never exercise, so the parity suite can't see it. -import { Component } from "@angular/core"; +import { + Component, + destroyPlatform, + ɵcreateOrReusePlatformInjector as createOrReusePlatformInjector, +} from "@angular/core"; import { describe, expect, it } from "vitest"; import { PdfDataTable } from "@/registry/bases/forme/components/data-table/data-table"; @@ -12,6 +16,7 @@ import { FormePage, } from "@/registry/bases/forme/lib/pdf-primitives"; import { renderFormeDocument } from "@/registry/bases/forme/lib/render"; +import { renderTree, textOf } from "@/registry/lib/pdf-renderer"; import { modernTheme } from "@/registry/themes/modern"; const texts = (node: unknown): string[] => { @@ -107,3 +112,18 @@ describe("forme components", () => { ); }); }); + +describe(renderTree, () => { + it("renders in an app started with bootstrapApplication", () => { + @Component({ template: "Hallo" }) + class Hello {} + // bootstrapApplication creates its platform this way. + destroyPlatform(); + createOrReusePlatformInjector([]); + try { + expect(renderTree(Hello, {}, textOf)).toBe("Hallo"); + } finally { + destroyPlatform(); + } + }); +});