From 7df5f9a7c27c06a390908d0da5b86ac94b3037c2 Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Sun, 16 Nov 2025 01:50:15 -0800 Subject: [PATCH 1/2] Formatter: Respect Outlook Conditional HTML Comments --- .../packages/formatter/src/format-printer.ts | 8 +++ .../html/outlook-conditional-comments.test.ts | 51 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 javascript/packages/formatter/test/html/outlook-conditional-comments.test.ts diff --git a/javascript/packages/formatter/src/format-printer.ts b/javascript/packages/formatter/src/format-printer.ts index bf6c0f1cc..aaba6575f 100644 --- a/javascript/packages/formatter/src/format-printer.ts +++ b/javascript/packages/formatter/src/format-printer.ts @@ -1179,6 +1179,14 @@ export class FormatPrinter extends Printer { } }).join("") + const isConditionalComment = /^\[if\s*[^\]]*\]>/.test(inner.trim()) + + if (isConditionalComment) { + this.pushWithIndent(open + inner + close) + + return + } + const hasNewlines = inner.includes('\n') if (hasNewlines) { diff --git a/javascript/packages/formatter/test/html/outlook-conditional-comments.test.ts b/javascript/packages/formatter/test/html/outlook-conditional-comments.test.ts new file mode 100644 index 000000000..ec2487ced --- /dev/null +++ b/javascript/packages/formatter/test/html/outlook-conditional-comments.test.ts @@ -0,0 +1,51 @@ +import { describe, test, expect, beforeAll } from "vitest" +import { Herb } from "@herb-tools/node-wasm" +import { Formatter } from "../../src" +import dedent from "dedent" + +let formatter: Formatter + +describe("Outlook conditional comments", () => { + beforeAll(async () => { + await Herb.load() + + formatter = new Formatter(Herb, { + indentWidth: 2, + maxLineLength: 80 + }) + }) + + test("preserves conditional comment syntax on same line - issue #877", () => { + const source = dedent` + + ` + const result = formatter.format(source) + expect(result).toEqual(source) + }) + + test("handles conditional comment with content", () => { + const source = dedent` + + ` + const result = formatter.format(source) + expect(result).toEqual(source) + }) + + test("handles negated conditional comment", () => { + const source = dedent` + +
Not Outlook
+ + ` + const result = formatter.format(source) + expect(result).toEqual(source) + }) +}) From 6d671525cbd4a9aa3955f7624f925a8320847706 Mon Sep 17 00:00:00 2001 From: Marco Roth Date: Sun, 16 Nov 2025 02:03:07 -0800 Subject: [PATCH 2/2] Simpler check and extra ERB test --- .../packages/formatter/src/format-printer.ts | 4 ++-- .../test/html/outlook-conditional-comments.test.ts | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/javascript/packages/formatter/src/format-printer.ts b/javascript/packages/formatter/src/format-printer.ts index aaba6575f..20f8f4678 100644 --- a/javascript/packages/formatter/src/format-printer.ts +++ b/javascript/packages/formatter/src/format-printer.ts @@ -1179,9 +1179,9 @@ export class FormatPrinter extends Printer { } }).join("") - const isConditionalComment = /^\[if\s*[^\]]*\]>/.test(inner.trim()) + const trimmedInner = inner.trim() - if (isConditionalComment) { + if (trimmedInner.startsWith('[if ') && trimmedInner.endsWith(' { const result = formatter.format(source) expect(result).toEqual(source) }) + + test("handles conditional comment with ERB interpolation", () => { + const source = dedent` + + ` + const result = formatter.format(source) + expect(result).toEqual(source) + }) })