import { describe, test, expect, beforeAll } from "vitest" import { Herb } from "@herb-tools/node-wasm" import { Formatter } from "../../src" import { createExpectFormattedToMatch } from "../helpers" import dedent from "dedent" let formatter: Formatter let expectFormattedToMatch: ReturnType describe("@herb-tools/formatter - inline elements", () => { beforeAll(async () => { await Herb.load() formatter = new Formatter(Herb, { indentWidth: 2, maxLineLength: 80 }) expectFormattedToMatch = createExpectFormattedToMatch(formatter) }) test("preserves inline elements in text flow (issue #251)", () => { expectFormattedToMatch(dedent`

Emphasis

`) }) test("preserves strong elements inline", () => { expectFormattedToMatch(dedent`

This is important text.

`) }) test("preserves span elements inline", () => { expectFormattedToMatch(dedent`
Some inline content
`) }) test("preserves links with attributes inline", () => { expectFormattedToMatch(dedent`

A link in text

`) }) test("preserves multiple inline elements", () => { expectFormattedToMatch(dedent`

This has emphasis and strong text.

`) }) test("preserves nested inline elements", () => { expectFormattedToMatch(dedent`

This is very important text.

`) }) test("preserves code elements inline", () => { expectFormattedToMatch(dedent`

Use the foo() function.

`) }) test("preserves abbr elements inline", () => { const source = dedent`

The WHO was founded in 1948.

` const result = formatter.format(source) expect(result).toEqual(dedent`

The WHO was founded in 1948.

`) }) test("preserves del and ins elements inline", () => { expectFormattedToMatch(dedent`

The price is $50 $40.

`) }) test("correctly handles block elements in text flow", () => { const source = dedent`
Text before
block element
text after
` const result = formatter.format(source) expect(result).toEqual(dedent`
Text before
block element
text after
`) }) test("preserves inline elements with multiple attributes", () => { expectFormattedToMatch(dedent`

Visit our page today.

`) }) test("preserves inline elements when content starts on same line", () => { expectFormattedToMatch(`

Emphasis

`) }) test("normalizes malformed closing tag placement", () => { const source = dedent`

Emphasis

` const result = formatter.format(source) expect(result).toEqual(dedent`

Emphasis

`) }) test("normalizes malformed closing tag placement", () => { const source = dedent`

Emphasis

` const result = formatter.format(source) expect(result).toEqual(dedent`

Emphasis

`) }) test("normalizes malformed closing tag placement", () => { const source = dedent`

Test

` const result = formatter.format(source) expect(result).toEqual(dedent`

Test

`) }) test("normalizes malformed closing tag placement", () => { const source = dedent`

Test

` const result = formatter.format(source) expect(result).toEqual(dedent`

Test

`) }) test("normalizes malformed closing tag placement", () => { const source = dedent`

Test

` const result = formatter.format(source) expect(result).toEqual(dedent`

Test

`) }) test("Element with ERB interpolation in text content", () => { expectFormattedToMatch(dedent`

Posts (<%= @posts.count %>)

`) }) test("block element with inline child on separate line preserves format (issue #1181)", () => { expectFormattedToMatch(dedent`
Hello
`) }) test("block element with inline child on same line stays inline (issue #1181)", () => { expectFormattedToMatch(`
Hello
`) }) test("block element with ERB output child on separate line preserves format (issue #1181)", () => { expectFormattedToMatch(dedent`
<%= f.input :password, hint: false %>
`) }) test("block element with ERB output child on same line stays inline (issue #1181)", () => { expectFormattedToMatch(dedent`
<%= f.input :password, hint: false %>
`) }) test("block element with ERB execution child on separate line preserves format (issue #1181)", () => { expectFormattedToMatch(dedent`
<% f.input :password, hint: false %>
`) }) test("block element with ERB execution child on same line stays inline (issue #1181)", () => { expectFormattedToMatch(dedent`
<% f.input :password, hint: false %>
`) }) test("block element with ERB child on separate line preserves format with maxLineLength 100 (issue #1181)", () => { const wideFormatter = new Formatter(Herb, { indentWidth: 2, maxLineLength: 100 }) const source = dedent`
<% t("registration.policies.edit.title") %>
` expect(wideFormatter.format(source)).toEqual(source) }) test("block element with ERB child on same line stays inline with maxLineLength 100 (issue #1181)", () => { const wideFormatter = new Formatter(Herb, { indentWidth: 2, maxLineLength: 100 }) const source = dedent`
<% t("registration.policies.edit.title") %>
` expect(wideFormatter.format(source)).toEqual(source) }) test("block element with p child on separate line preserves format with maxLineLength 100 (issue #1181)", () => { const wideFormatter = new Formatter(Herb, { indentWidth: 2, maxLineLength: 100 }) const source = dedent`

Test

` expect(wideFormatter.format(source)).toEqual(source) }) test("preserves inline elements with ERB in text flow (issue #1181)", () => { expectFormattedToMatch(dedent`
I <%= verb %> these <%= type %> tags.

It's <%= adjective %>!

`) }) test("expands inline elements with ERB and block child to multiline (issue #1181)", () => { const source = dedent`
I <%= verb %> these <%= type %> tags.

It's <%= adjective %>!

` const result = formatter.format(source) expect(result).toEqual(dedent`
I <%= verb %> these <%= type %> tags.

It's <%= adjective %>!

`) }) test("preserves ERB directly in text flow (issue #1181)", () => { expectFormattedToMatch(dedent`
I <%= verb %> these <%= type %> tags.

It's <%= adjective %>!

`) }) test("handles adjacent ERB without spaces in text flow (issue #1181)", () => { expectFormattedToMatch(dedent`
I<%= verb %>these<%= type %>tags.

Hel

lo

`) }) test("preserves inline elements in text flow (issue #1181)", () => { expectFormattedToMatch(dedent`
I like these inline tags.

It's true!

`) }) test("separates adjacent block elements onto new lines (issue #1181)", () => { const source = dedent`

Hel

lo

` const result = formatter.format(source) expect(result).toEqual(dedent`

Hel

lo

`) }) test("separates adjacent li elements with space onto new lines (issue #1181)", () => { const source = dedent`
  • a
  • b
  • ` const result = formatter.format(source) expect(result).toEqual(dedent`
  • a
  • b
  • `) }) test("separates adjacent li elements without space onto new lines (issue #1181)", () => { const source = dedent`
  • a
  • b
  • ` const result = formatter.format(source) expect(result).toEqual(dedent`
  • a
  • b
  • `) }) test("block element with ERB children on separate lines preserves format", () => { expectFormattedToMatch(dedent`
    <%= f.input :password, hint: false %> <%= f.input :password_confirmation %>
    `) }) test("adjacent ERB tags on same line as block element expand to multiline", () => { const source = dedent`
    <%= f.input :password, hint: false %><%= f.input :password_confirmation %>
    ` const result = formatter.format(source) expect(result).toEqual(dedent`
    <%= f.input :password, hint: false %><%= f.input :password_confirmation %>
    `) }) test("spaced ERB tags on same line as block element expand to multiline", () => { const source = dedent`
    <%= f.input :password, hint: false %> <%= f.input :password_confirmation %>
    ` const result = formatter.format(source) expect(result).toEqual(dedent`
    <%= f.input :password, hint: false %> <%= f.input :password_confirmation %>
    `) }) test("spaced ERB tags on separate line from block element preserves format", () => { expectFormattedToMatch(dedent`
    <%= f.input :password, hint: false %> <%= f.input :password_confirmation %>
    `) }) test("preserves content of element with whitespace-pre-line class (issue #1095)", () => { expectFormattedToMatch(dedent`
    <%= option[:value] %>
    `) }) test("preserves content of element with whitespace-pre-line class and multiline body (issue #1095)", () => { expectFormattedToMatch(dedent`
    <%= option[:value] %>
    `) }) test("preserves content of element with whitespace-pre class", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content of element with whitespace-pre-wrap class", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content of element with whitespace-break-spaces class", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content of element with inline style white-space: pre", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content of element with inline style white-space: pre-line", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content of element with inline style white-space:pre (no space)", () => { expectFormattedToMatch(` some spaced text `) }) test("does not treat whitespace-normal class as content preserving", () => { expectFormattedToMatch(`some text`) }) test("does not treat whitespace-nowrap class as content preserving", () => { expectFormattedToMatch(`some text`) }) test("does not treat inline style white-space: normal as content preserving", () => { expectFormattedToMatch(`some text`) }) test("does not treat inline style white-space: nowrap as content preserving", () => { expectFormattedToMatch(`some text`) }) test("preserves content with Tailwind responsive prefix md:whitespace-pre-line", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content with Tailwind state prefix hover:whitespace-pre", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content with chained Tailwind prefix lg:hover:whitespace-pre-wrap", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content with inline style white-space: pre !important", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content with white-space among other style properties", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves content of block element with whitespace-preserving class", () => { expectFormattedToMatch(dedent`
    some spaced text with newlines
    `) }) test("preserves content when whitespace-preserving class is among many classes", () => { expectFormattedToMatch(` some spaced text `) }) test("preserves nested elements inside whitespace-preserving parent", () => { expectFormattedToMatch(dedent`
    some text more text
    `) }) test("preserves content with white-space as last style property without trailing semicolon", () => { expectFormattedToMatch(` some spaced text `) expectFormattedToMatch(` some spaced text `) expectFormattedToMatch(` some spaced text `) }) test("preserves ERB interpolation in attributes", () => { expectFormattedToMatch(dedent`
    ">
    `) }) })