import dedent from "dedent" import { describe, test, expect, beforeAll } from "vitest" import { Herb } from "@herb-tools/node-wasm" import { IdentityPrinter } from "@herb-tools/printer" import { Minifier, minify } from "../src/index.js" describe("Minifier", () => { let minifier: Minifier beforeAll(async () => { minifier = new Minifier(Herb) await minifier.initialize() }) describe("top-level minify function", () => { test("minify", () => { const template = `
Hello
` const parseResult = Herb.parse(template) expect(minify(parseResult.value)).toBe(`
Hello
`) expect(IdentityPrinter.print(parseResult.value)).toBe(template) }) }) describe("basic minification", () => { test("removes whitespace between tags", () => { const template = `
Hello
` const result = minifier.minifyString(template) expect(result).toBe(`
Hello
`) }) test("collapses multiple spaces to single space in text", () => { const template = `
Hello World
` const result = minifier.minifyString(template) expect(result).toBe(`
Hello World
`) }) test("removes newlines and indentation", () => { const template = dedent`

Hello World

This is a test

` const result = minifier.minifyString(template) expect(result).toBe(`

Hello World

This is a test

`) }) test("handles nested elements", () => { const template = dedent`
` const result = minifier.minifyString(template) expect(result).toBe(`
`) }) }) describe("whitespace between elements", () => { test("keeps the space between text and a following inline element", () => { expect(minifier.minifyString(`
Hello world
`)).toBe(`
Hello world
`) }) test("keeps the space between an inline element and following text", () => { expect(minifier.minifyString(`
Hello world
`)).toBe(`
Hello world
`) }) test("keeps the space between two inline elements", () => { expect(minifier.minifyString(`

a b

`)).toBe(`

a b

`) }) test("collapses a run of whitespace between two inline elements to one space", () => { expect(minifier.minifyString(`

a \n b

`)).toBe(`

a b

`) }) test("keeps the spaces around an inline element inside a sentence", () => { expect(minifier.minifyString(`

one two three

`)).toBe(`

one two three

`) }) test("drops the whitespace between two block elements", () => { expect(minifier.minifyString(`

a

b

`)).toBe(`

a

b

`) }) test("drops the whitespace between text and a following block element", () => { expect(minifier.minifyString(`
Hello

world

`)).toBe(`
Hello

world

`) }) test("drops the leading and trailing whitespace of a block", () => { expect(minifier.minifyString(`
Hello
`)).toBe(`
Hello
`) }) test("keeps the space between text and a void inline element", () => { expect(minifier.minifyString(`
Hello world
`)).toBe(`
Hello world
`) }) }) describe("preserve whitespace in special tags", () => { test("preserves whitespace in
 tags", () => {
      const template = dedent`
        
            Line 1
              Line 2
                Line 3
          
` const result = minifier.minifyString(template) expect(result).toBe(dedent`
            Line 1
              Line 2
                Line 3
          
`) }) test("collapses whitespace in tags, which render with `white-space: normal`", () => { const template = dedent`
const x = 1 const y = 2
` const result = minifier.minifyString(template) expect(result).toBe(`
const x = 1 const y = 2
`) }) test("preserves whitespace in ` const result = minifier.minifyString(template) expect(result).toBe(dedent`
`) }) test("preserves whitespace in ` const result = minifier.minifyString(template) expect(result).toBe(dedent`
`) }) test("preserves whitespace in ` const result = minifier.minifyString(template) expect(result).toBe(dedent`
`) }) test("handles nested preserve-whitespace tags", () => { const template = dedent`
            
              function test() {
                return true
              }
            
          
` const result = minifier.minifyString(template) expect(result).toBe(dedent`
            
              function test() {
                return true
              }
            
          
`) }) }) describe("attributes", () => { test("preserves attributes", () => { const template = `
Content
` const result = minifier.minifyString(template) expect(result).toBe(`
Content
`) }) test("drops the space before a self-closing tag when the last value is quoted", () => { const template = `
` const result = minifier.minifyString(template) expect(result).toBe(`
`) }) test("keeps the space before a self-closing tag when the last value is unquoted", () => { const template = `
` const result = minifier.minifyString(template) expect(result).toBe(`
`) }) }) describe("ERB support", () => { test("handles ERB output tags", () => { const template = dedent`
<%= user.name %>
` const result = minifier.minifyString(template) expect(result).toBe(`
<%=user.name%>
`) }) test("handles ERB conditionals", () => { const template = dedent`
<% if admin? %> Admin <% else %> User <% end %>
` const result = minifier.minifyString(template) expect(result).toBe(`
<%if admin?%>Admin<%else%> User<%end%>
`) }) test("handles ERB output", () => { const template = dedent`
Hello <%= world %>
` const result = minifier.minifyString(template) expect(result).toBe(`
Hello <%=world%>
`) }) test("handles ERB output-2", () => { const template = dedent`
Hello <%= world %>
` const result = minifier.minifyString(template) expect(result).toBe(`
Hello <%=world%>
`) }) test("handles ERB output-2", () => { const template = dedent`
Hello <%= world %> !
` const result = minifier.minifyString(template) expect(result).toBe(`
Hello <%=world%> !
`) }) }) describe("HTML attributes", () => { test("class", () => { const template = `
` const result = minifier.minifyString(template) expect(result).toBe(`
`) }) test("keeps the spaces around an ERB conditional in a class list", () => { const template = `
x
` expect(minifier.minifyString(template)).toBe(`
x
`) }) test("class with erb", () => { const template = `
` const result = minifier.minifyString(template) expect(result).toBe(`
`) }) test("non-class", () => { const template = `` const result = minifier.minifyString(template) expect(result).toBe(``) }) test("with newlines", () => { const template = dedent`
Hello <%= world %> !
` const result = minifier.minifyString(template) expect(result).toBe(`
Hello <%=world%> !
`) }) test("with spaces around =", () => { const template = dedent`
` const result = minifier.minifyString(template) expect(result).toBe(`
`) }) test("attribute wrapped in if", () => { const template = dedent`
class=" one <%= two %> three " <% end %> disabled > Hello <%= world %> !
` const result = minifier.minifyString(template) expect(result).toBe(`
class="one <%=two%> three"<%end%> disabled>Hello <%=world%> !
`) }) test("two attributes wrapped in if", () => { const template = dedent`
class=" one <%= two %> three " id="one" <% end %> disabled > Hello <%= world %> !
` const result = minifier.minifyString(template) expect(result).toBe(`
class="one <%=two%> three" id="one"<%end%> disabled>Hello <%=world%> !
`) }) }) describe("comments", () => { test("removes HTML comments", () => { expect(minifier.minifyString(`

x

`)).toBe(`

x

`) }) test("removes ERB comments", () => { expect(minifier.minifyString(`
<%# a comment %>

x

`)).toBe(`

x

`) }) test("removes the whitespace a removed comment leaves behind", () => { const template = dedent`

x

` expect(minifier.minifyString(template)).toBe(`

x

`) }) test("collapses the whitespace around a comment between inline elements to one space", () => { const template = `

a b

` expect(minifier.minifyString(template)).toBe(`

a b

`) }) test("keeps downlevel-revealed conditional comments", () => { const template = `

x

` expect(minifier.minifyString(template)).toBe(`

x

`) }) test("keeps Herb directives, which reach the minifier as ERB comments", () => { const template = `
<%# herb:state count = 0 %>

x

` expect(minifier.minifyString(template)).toBe(`
<%# herb:state count = 0 %>

x

`) }) test("removes comments inside inline elements", () => { expect(minifier.minifyString(`ab`)).toBe(`ab`) }) }) describe("Ruby that newlines hold together", () => { test("keeps a newline after a tag whose content ends in a Ruby comment", () => { const template = dedent` <% # one # two %> <%= foo( bar: 1 ) %> ` const result = minifier.minifyString(template) expect(Herb.parse(result).failed).toBe(false) expect(result).toBe(dedent` <% # one # two %> <%= foo( bar: 1 ) %> `) }) test("keeps a newline after a branch tag whose content ends in a Ruby comment", () => { const template = dedent` <% if admin? %>

Admin

<% else # everyone else %>

User

<% end %> ` const result = minifier.minifyString(template) expect(Herb.parse(result).failed).toBe(false) expect(result).toBe(`<%if admin?%>

Admin

<%else # everyone else%>\n

User

<%end%>`) }) test("keeps a newline after a block tag whose content ends in a Ruby comment", () => { const template = dedent` <% talks.each do |talk| # newest first %>

<%= talk %>

<% end %> ` const result = minifier.minifyString(template) expect(Herb.parse(result).failed).toBe(false) }) test("does not trim multi-line ERB content, which would break a heredoc", () => { const template = dedent`

<%= wrap(<<~TEXT) hello TEXT %>

` const result = minifier.minifyString(template) expect(Herb.parse(result).failed).toBe(false) expect(result).toContain("TEXT\n ") }) test("does not treat a hash inside a string as a Ruby comment", () => { const template = dedent` <% anchor = "#" + slug %> <%= link_to( "x", anchor ) %> ` const result = minifier.minifyString(template) expect(Herb.parse(result).failed).toBe(false) expect(result).toBe(`<%anchor = "#" + slug%><%= link_to(\n "x", anchor\n) %>`) }) }) describe("Ruby that looks like a comment", () => { test("does not treat interpolation inside a string as a Ruby comment", () => { const template = `<%= "#{count} #{label}" %>, <%= other %>:` expect(minifier.minifyString(template)).toBe(`<%="#{count} #{label}"%>, <%=other%>:`) }) test("does not treat interpolation inside a percent literal as a Ruby comment", () => { const template = `

<%= %{#{ points }}.html_safe %>\n

` expect(minifier.minifyString(template)).toBe(`

<%= %{#{ points }}.html_safe %>

`) }) test("does not fuse a trailing dash into an ERB trim marker", () => { const template = `

<% title %-Search results- %>

` const result = minifier.minifyString(template) expect(Herb.parse(result).failed).toBe(false) expect(result).toBe(`

<% title %-Search results- %>

`) }) test("keeps a doctype whole after a tag that ends in a Ruby comment", () => { const template = `<% # OVERRIDE: a note %>\n\nx` expect(minifier.minifyString(template)).toBe(`<% # OVERRIDE: a note %>\nx`) }) }) describe("declarations", () => { test("keeps the whitespace inside an XML declaration", () => { const template = `\nx` expect(minifier.minifyString(template)).toBe(`x`) }) test("keeps the whitespace inside a doctype", () => { const template = `\nx` expect(minifier.minifyString(template)).toBe(`x`) }) }) describe("error handling", () => { test("returns original template on parse failure", () => { const template = `
{ test("minifies using instance method", () => { const template = dedent`

Hello World

` const result = minifier.minifyString(template) expect(result).toBe(`

Hello World

`) }) }) })