JacobHawley's picture
download
raw
85.9 kB
diff --git a/parsing/html/html_test.go b/parsing/html/html_test.go new file mode 100644 index 0000000..e1d275e --- /dev/null +++ b/parsing/html/html_test.go @@ -0,0 +1,2864 @@ +//go:build html + +package html_test + +import ( + "strings" + "testing" + + "github.com/tomwright/dasel/v3/model" + "github.com/tomwright/dasel/v3/parsing" + _ "github.com/tomwright/dasel/v3/parsing/html" +) + +func harnessRead(t *testing.T, input string) *model.Value { + t.Helper() + f := parsing.Format("html") + reader, err := f.NewReader(parsing.DefaultReaderOptions()) + if err != nil { + t.Fatalf("failed to create reader: %v", err) + } + val, err := reader.Read([]byte(input)) + if err != nil { + t.Fatalf("failed to read: %v", err) + } + return val +} + +func TestReadBasicHTML(t *testing.T) { + t.Run("simple html document", func(t *testing.T) { + input := `<html><head><title>Test</title></head><body><p>Hello</p></body></html>` + val := harnessRead(t, input) + if val.Type() != model.TypeMap { + t.Fatalf("expected map, got %s", val.Type()) + } + }) + + t.Run("html with doctype", func(t *testing.T) { + input := `<!DOCTYPE html><html><head><title>Test</title></head><body></body></html>` + val := harnessRead(t, input) + if val.Type() != model.TypeMap { + t.Fatalf("expected map, got %s", val.Type()) + } + }) + + t.Run("fragment without html wrapper", func(t *testing.T) { + input := `<div><p>Content</p></div>` + val := harnessRead(t, input) + if val.Type() != model.TypeMap { + t.Fatalf("expected map, got %s", val.Type()) + } + }) +} + +func TestReadHTMLElements(t *testing.T) { + t.Run("nested elements", func(t *testing.T) { + input := `<html><body><div><span>text</span></div></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("failed to get div: %v", err) + } + span, err := div.GetMapKey("span") + if err != nil { + t.Fatalf("failed to get span: %v", err) + } + text, err := span.StringValue() + if err != nil { + t.Fatalf("failed to get text: %v", err) + } + if text != "text" { + t.Fatalf("expected 'text', got '%s'", text) + } + }) + + t.Run("multiple same-tag siblings become slice", func(t *testing.T) { + input := `<html><body><p>First</p><p>Second</p><p>Third</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + ps, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + if ps.Type() != model.TypeSlice { + t.Fatalf("expected slice for multiple p tags, got %s", ps.Type()) + } + length, err := ps.SliceLen() + if err != nil { + t.Fatalf("failed to get slice length: %v", err) + } + if length != 3 { + t.Fatalf("expected 3 p elements, got %d", length) + } + }) + + t.Run("single child element is not slice", func(t *testing.T) { + input := `<html><body><p>Only one</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + if p.Type() == model.TypeSlice { + t.Fatalf("single p element should not be a slice") + } + }) +} + +func TestReadHTMLAttributes(t *testing.T) { + t.Run("element with id attribute", func(t *testing.T) { + input := `<html><body><div id="main">Content</div></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("failed to get div: %v", err) + } + id, err := div.GetMapKey("-id") + if err != nil { + t.Fatalf("failed to get -id attribute: %v", err) + } + idStr, err := id.StringValue() + if err != nil { + t.Fatalf("failed to get id string: %v", err) + } + if idStr != "main" { + t.Fatalf("expected id 'main', got '%s'", idStr) + } + }) + + t.Run("element with class attribute", func(t *testing.T) { + input := `<html><body><div class="container active">Content</div></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("failed to get div: %v", err) + } + class, err := div.GetMapKey("-class") + if err != nil { + t.Fatalf("failed to get -class attribute: %v", err) + } + classStr, err := class.StringValue() + if err != nil { + t.Fatalf("failed to get class string: %v", err) + } + if classStr != "container active" { + t.Fatalf("expected class 'container active', got '%s'", classStr) + } + }) + + t.Run("element with multiple attributes", func(t *testing.T) { + input := `<html><body><a href="/page" title="Link" target="_blank">Click</a></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + a, err := body.GetMapKey("a") + if err != nil { + t.Fatalf("failed to get a: %v", err) + } + + href, err := a.GetMapKey("-href") + if err != nil { + t.Fatalf("failed to get -href: %v", err) + } + hrefStr, _ := href.StringValue() + if hrefStr != "/page" { + t.Fatalf("expected href '/page', got '%s'", hrefStr) + } + + title, err := a.GetMapKey("-title") + if err != nil { + t.Fatalf("failed to get -title: %v", err) + } + titleStr, _ := title.StringValue() + if titleStr != "Link" { + t.Fatalf("expected title 'Link', got '%s'", titleStr) + } + + target, err := a.GetMapKey("-target") + if err != nil { + t.Fatalf("failed to get -target: %v", err) + } + targetStr, _ := target.StringValue() + if targetStr != "_blank" { + t.Fatalf("expected target '_blank', got '%s'", targetStr) + } + }) + + t.Run("data attributes", func(t *testing.T) { + input := `<html><body><div data-id="123" data-name="test">Content</div></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("failed to get div: %v", err) + } + + dataId, err := div.GetMapKey("-data-id") + if err != nil { + t.Fatalf("failed to get -data-id: %v", err) + } + dataIdStr, _ := dataId.StringValue() + if dataIdStr != "123" { + t.Fatalf("expected data-id '123', got '%s'", dataIdStr) + } + }) +} + +func TestReadHTMLText(t *testing.T) { + t.Run("text content in #text key", func(t *testing.T) { + input := `<html><body><p class="intro">Hello World</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, err := p.GetMapKey("#text") + if err != nil { + t.Fatalf("failed to get #text: %v", err) + } + textStr, err := text.StringValue() + if err != nil { + t.Fatalf("failed to get text string: %v", err) + } + if textStr != "Hello World" { + t.Fatalf("expected 'Hello World', got '%s'", textStr) + } + }) + + t.Run("text-only element becomes string value", func(t *testing.T) { + input := `<html><body><span>Just text</span></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + span, err := body.GetMapKey("span") + if err != nil { + t.Fatalf("failed to get span: %v", err) + } + if span.Type() != model.TypeString { + t.Fatalf("text-only element should be string, got %s", span.Type()) + } + text, _ := span.StringValue() + if text != "Just text" { + t.Fatalf("expected 'Just text', got '%s'", text) + } + }) + + t.Run("whitespace trimmed", func(t *testing.T) { + input := `<html><body><p> + Spaced text + </p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, _ := p.StringValue() + if text != "Spaced text" { + t.Fatalf("expected trimmed text, got '%s'", text) + } + }) +} + +func TestReadHTMLSpecialElements(t *testing.T) { + t.Run("empty elements", func(t *testing.T) { + input := `<html><body><br/><hr/></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + br, err := body.GetMapKey("br") + if err != nil { + t.Fatalf("failed to get br: %v", err) + } + if br.Type() != model.TypeString { + t.Fatalf("empty br should be empty string, got %s", br.Type()) + } + brStr, _ := br.StringValue() + if brStr != "" { + t.Fatalf("void element without attributes must equal empty string, got '%s'", brStr) + } + hr, err := body.GetMapKey("hr") + if err != nil { + t.Fatalf("failed to get hr: %v", err) + } + hrStr, _ := hr.StringValue() + if hrStr != "" { + t.Fatalf("void element without attributes must equal empty string, got '%s'", hrStr) + } + }) + + t.Run("self-closing tags", func(t *testing.T) { + input := `<html><body><img src="test.png" alt="Test"/></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + img, err := body.GetMapKey("img") + if err != nil { + t.Fatalf("failed to get img: %v", err) + } + src, err := img.GetMapKey("-src") + if err != nil { + t.Fatalf("failed to get -src: %v", err) + } + srcStr, _ := src.StringValue() + if srcStr != "test.png" { + t.Fatalf("expected src 'test.png', got '%s'", srcStr) + } + }) + + t.Run("input elements", func(t *testing.T) { + input := `<html><body><input type="text" name="username" value="john"/></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + inp, err := body.GetMapKey("input") + if err != nil { + t.Fatalf("failed to get input: %v", err) + } + typ, err := inp.GetMapKey("-type") + if err != nil { + t.Fatalf("failed to get -type: %v", err) + } + typStr, _ := typ.StringValue() + if typStr != "text" { + t.Fatalf("expected type 'text', got '%s'", typStr) + } + }) +} + +func TestReadHTMLMixedContent(t *testing.T) { + t.Run("text with child elements", func(t *testing.T) { + input := `<html><body><p>Hello <strong>World</strong></p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, err := p.GetMapKey("#text") + if err != nil { + t.Fatalf("failed to get #text: %v", err) + } + textStr, _ := text.StringValue() + if textStr != "Hello" { + t.Fatalf("expected 'Hello', got '%s'", textStr) + } + + strong, err := p.GetMapKey("strong") + if err != nil { + t.Fatalf("failed to get strong: %v", err) + } + strongText, _ := strong.StringValue() + if strongText != "World" { + t.Fatalf("expected 'World', got '%s'", strongText) + } + }) + + t.Run("deeply nested structure", func(t *testing.T) { + input := `<html><body><div><ul><li><a href="#">Link 1</a></li><li><a href="#">Link 2</a></li></ul></div></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("failed to get div: %v", err) + } + ul, err := div.GetMapKey("ul") + if err != nil { + t.Fatalf("failed to get ul: %v", err) + } + li, err := ul.GetMapKey("li") + if err != nil { + t.Fatalf("failed to get li: %v", err) + } + if li.Type() != model.TypeSlice { + t.Fatalf("expected slice for multiple li, got %s", li.Type()) + } + length, _ := li.SliceLen() + if length != 2 { + t.Fatalf("expected 2 li elements, got %d", length) + } + }) +} + +func TestReadHTMLTable(t *testing.T) { + t.Run("basic table structure", func(t *testing.T) { + input := `<html><body><table><tbody><tr><th>Name</th><th>Age</th></tr><tr><td>Alice</td><td>30</td></tr></tbody></table></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + table, err := body.GetMapKey("table") + if err != nil { + t.Fatalf("failed to get table: %v", err) + } + tbody, err := table.GetMapKey("tbody") + if err != nil { + t.Fatalf("failed to get tbody: %v", err) + } + tr, err := tbody.GetMapKey("tr") + if err != nil { + t.Fatalf("failed to get tr: %v", err) + } + if tr.Type() != model.TypeSlice { + t.Fatalf("expected slice for multiple tr, got %s", tr.Type()) + } + length, _ := tr.SliceLen() + if length != 2 { + t.Fatalf("expected 2 tr elements, got %d", length) + } + }) +} + +func TestReadHTMLForm(t *testing.T) { + t.Run("form with inputs", func(t *testing.T) { + input := `<html><body><form action="/submit" method="post"><input type="text" name="user"/><input type="submit" value="Send"/></form></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + form, err := body.GetMapKey("form") + if err != nil { + t.Fatalf("failed to get form: %v", err) + } + action, err := form.GetMapKey("-action") + if err != nil { + t.Fatalf("failed to get -action: %v", err) + } + actionStr, _ := action.StringValue() + if actionStr != "/submit" { + t.Fatalf("expected action '/submit', got '%s'", actionStr) + } + + inputs, err := form.GetMapKey("input") + if err != nil { + t.Fatalf("failed to get input: %v", err) + } + if inputs.Type() != model.TypeSlice { + t.Fatalf("expected slice for multiple inputs, got %s", inputs.Type()) + } + }) +} + +func TestReadHTMLHead(t *testing.T) { + t.Run("title extraction", func(t *testing.T) { + input := `<html><head><title>My Page Title</title></head><body></body></html>` + val := harnessRead(t, input) + + head, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("failed to get head: %v", err) + } + title, err := head.GetMapKey("title") + if err != nil { + t.Fatalf("failed to get title: %v", err) + } + titleStr, _ := title.StringValue() + if titleStr != "My Page Title" { + t.Fatalf("expected 'My Page Title', got '%s'", titleStr) + } + }) + + t.Run("meta tags", func(t *testing.T) { + input := `<html><head><meta charset="utf-8"/><meta name="description" content="A test page"/></head><body></body></html>` + val := harnessRead(t, input) + + head, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("failed to get head: %v", err) + } + meta, err := head.GetMapKey("meta") + if err != nil { + t.Fatalf("failed to get meta: %v", err) + } + if meta.Type() != model.TypeSlice { + t.Fatalf("expected slice for multiple meta, got %s", meta.Type()) + } + }) + + t.Run("link tags", func(t *testing.T) { + input := `<html><head><link rel="stylesheet" href="style.css"/></head><body></body></html>` + val := harnessRead(t, input) + + head, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("failed to get head: %v", err) + } + link, err := head.GetMapKey("link") + if err != nil { + t.Fatalf("failed to get link: %v", err) + } + href, err := link.GetMapKey("-href") + if err != nil { + t.Fatalf("failed to get -href: %v", err) + } + hrefStr, _ := href.StringValue() + if hrefStr != "style.css" { + t.Fatalf("expected 'style.css', got '%s'", hrefStr) + } + }) +} + +func TestReadHTMLScriptStyle(t *testing.T) { + t.Run("script tag content", func(t *testing.T) { + input := `<html><body><script>console.log("hello");</script></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + script, err := body.GetMapKey("script") + if err != nil { + t.Fatalf("failed to get script: %v", err) + } + scriptStr, _ := script.StringValue() + if !strings.Contains(scriptStr, "console.log") { + t.Fatalf("expected script content, got '%s'", scriptStr) + } + }) + + t.Run("style tag content", func(t *testing.T) { + input := `<html><head><style>body { color: red; }</style></head><body></body></html>` + val := harnessRead(t, input) + + head, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("failed to get head: %v", err) + } + style, err := head.GetMapKey("style") + if err != nil { + t.Fatalf("failed to get style: %v", err) + } + styleStr, _ := style.StringValue() + if !strings.Contains(styleStr, "color: red") { + t.Fatalf("expected style content, got '%s'", styleStr) + } + }) + + t.Run("script preserves embedded HTML tags", func(t *testing.T) { + input := `<html><body><script>var x = "<div>test</div>";</script></body></html>` + val := harnessRead(t, input) + + body, _ := val.GetMapKey("body") + script, _ := body.GetMapKey("script") + text, _ := script.StringValue() + if !strings.Contains(text, "<div>test</div>") { + t.Fatalf("script should preserve HTML tags as text, got '%s'", text) + } + }) +} + +func TestReadHTMLMalformed(t *testing.T) { + t.Run("unclosed tags are handled", func(t *testing.T) { + input := `<html><body><p>Unclosed paragraph<div>Another element</div></body></html>` + val := harnessRead(t, input) + if val.Type() != model.TypeMap { + t.Fatalf("should handle unclosed tags, got %s", val.Type()) + } + }) + + t.Run("missing closing tags", func(t *testing.T) { + input := `<html><body><br><hr><img src="test.png"></body></html>` + val := harnessRead(t, input) + if val.Type() != model.TypeMap { + t.Fatalf("should handle void elements, got %s", val.Type()) + } + }) + + t.Run("empty input", func(t *testing.T) { + input := `` + val := harnessRead(t, input) + if val == nil { + t.Fatal("should handle empty input") + } + if val.Type() != model.TypeMap { + t.Fatalf("empty input should return map, got %s", val.Type()) + } + }) + + t.Run("whitespace only input", func(t *testing.T) { + input := ` + + ` + val := harnessRead(t, input) + if val == nil { + t.Fatal("should handle whitespace input") + } + if val.Type() != model.TypeMap { + t.Fatalf("whitespace input should return map, got %s", val.Type()) + } + }) +} + +func TestReadHTMLEdgeCases(t *testing.T) { + t.Run("comments are ignored", func(t *testing.T) { + input := `<html><body><!-- This is a comment --><p>Content</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, _ := p.StringValue() + if text != "Content" { + t.Fatalf("expected 'Content', got '%s'", text) + } + }) + + t.Run("special characters in text", func(t *testing.T) { + input := `<html><body><p>&amp; &lt; &gt; &quot;</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, _ := p.StringValue() + if !strings.Contains(text, "&") { + t.Fatalf("expected decoded ampersand, got '%s'", text) + } + }) + + t.Run("unicode content", func(t *testing.T) { + input := `<html><body><p>Hello 世界 🌍</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, _ := p.StringValue() + if !strings.Contains(text, "世界") { + t.Fatalf("expected unicode content, got '%s'", text) + } + }) + + t.Run("boolean attributes", func(t *testing.T) { + input := `<html><body><input type="checkbox" checked disabled/></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + inp, err := body.GetMapKey("input") + if err != nil { + t.Fatalf("failed to get input: %v", err) + } + checked, err := inp.GetMapKey("-checked") + if err != nil { + t.Fatalf("failed to get -checked: %v", err) + } + checkedStr, _ := checked.StringValue() + if checkedStr != "" { + t.Fatalf("boolean attribute should be empty string, got '%s'", checkedStr) + } + }) +} + +func TestReadHTMLNormalization(t *testing.T) { + t.Run("fragment input gets normalized with head and body", func(t *testing.T) { + input := `<div>content</div>` + val := harnessRead(t, input) + + _, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("normalized document should have head: %v", err) + } + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("normalized document should have body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("body should contain the fragment div: %v", err) + } + text, _ := div.StringValue() + if text != "content" { + t.Fatalf("expected 'content', got '%s'", text) + } + }) + + t.Run("input missing head gets head added", func(t *testing.T) { + input := `<html><body><p>text</p></body></html>` + val := harnessRead(t, input) + + _, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("normalized document should have head even if missing in input: %v", err) + } + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("should have body: %v", err) + } + _, err = body.GetMapKey("p") + if err != nil { + t.Fatalf("body should contain p: %v", err) + } + }) + + t.Run("input missing body gets body added", func(t *testing.T) { + input := `<html><head><title>Test</title></head></html>` + val := harnessRead(t, input) + + head, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("should have head: %v", err) + } + _, err = head.GetMapKey("title") + if err != nil { + t.Fatalf("head should contain title: %v", err) + } + _, err = val.GetMapKey("body") + if err != nil { + t.Fatalf("normalized document should have body even if missing in input: %v", err) + } + }) + + t.Run("bare text gets normalized", func(t *testing.T) { + input := `Just some text` + val := harnessRead(t, input) + + _, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("normalized document should have head: %v", err) + } + _, err = val.GetMapKey("body") + if err != nil { + t.Fatalf("normalized document should have body: %v", err) + } + }) + + t.Run("orphan elements under html placed in body", func(t *testing.T) { + input := `<html><div>orphan</div></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("should have body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("orphan div should be placed in body: %v", err) + } + text, _ := div.StringValue() + if text != "orphan" { + t.Fatalf("expected 'orphan', got '%s'", text) + } + }) +} + +func TestReadHTMLImplicitClosing(t *testing.T) { + t.Run("consecutive p tags create siblings", func(t *testing.T) { + input := `<html><body><p>first<p>second</body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + pSlice, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + length, err := pSlice.SliceLen() + if err != nil { + t.Fatalf("p should be a slice of siblings: %v", err) + } + if length != 2 { + t.Fatalf("expected 2 p elements, got %d", length) + } + first, _ := pSlice.GetSliceIndex(0) + firstStr, _ := first.StringValue() + if firstStr != "first" { + t.Fatalf("expected 'first', got '%s'", firstStr) + } + second, _ := pSlice.GetSliceIndex(1) + secondStr, _ := second.StringValue() + if secondStr != "second" { + t.Fatalf("expected 'second', got '%s'", secondStr) + } + }) + + t.Run("consecutive li tags create siblings", func(t *testing.T) { + input := `<html><body><ul><li>A<li>B<li>C</ul></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + ul, err := body.GetMapKey("ul") + if err != nil { + t.Fatalf("failed to get ul: %v", err) + } + li, err := ul.GetMapKey("li") + if err != nil { + t.Fatalf("failed to get li: %v", err) + } + length, err := li.SliceLen() + if err != nil { + t.Fatalf("li should be a slice: %v", err) + } + if length != 3 { + t.Fatalf("expected 3 li elements, got %d", length) + } + second, _ := li.GetSliceIndex(1) + secondStr, _ := second.StringValue() + if secondStr != "B" { + t.Fatalf("expected 'B', got '%s'", secondStr) + } + }) + + t.Run("div closes open p", func(t *testing.T) { + input := `<html><body><p>Before<div>Inside</div>After</body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + pStr, _ := p.StringValue() + if pStr != "Before" { + t.Fatalf("p should contain only 'Before' (div closed it), got '%s'", pStr) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("div should be sibling of p: %v", err) + } + divStr, _ := div.StringValue() + if divStr != "Inside" { + t.Fatalf("div should contain 'Inside', got '%s'", divStr) + } + }) + + t.Run("ul closes open p", func(t *testing.T) { + input := `<html><body><p>Text<ul><li>Item</ul></body></html>` + val := harnessRead(t, input) + + body, _ := val.GetMapKey("body") + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + pStr, _ := p.StringValue() + if pStr != "Text" { + t.Fatalf("p should contain only 'Text' (ul closed it), got '%s'", pStr) + } + ul, err := body.GetMapKey("ul") + if err != nil { + t.Fatalf("ul should be sibling of p: %v", err) + } + li, _ := ul.GetMapKey("li") + liStr, _ := li.StringValue() + if liStr != "Item" { + t.Fatalf("li should contain 'Item', got '%s'", liStr) + } + }) + + t.Run("table closes open p", func(t *testing.T) { + input := `<html><body><p>Intro<table><tr><td>Cell</td></tr></table></body></html>` + val := harnessRead(t, input) + + body, _ := val.GetMapKey("body") + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + pStr, _ := p.StringValue() + if pStr != "Intro" { + t.Fatalf("p should contain only 'Intro' (table closed it), got '%s'", pStr) + } + table, err := body.GetMapKey("table") + if err != nil { + t.Fatalf("table should be sibling of p: %v", err) + } + if table.Type() != model.TypeMap { + t.Fatalf("table should be map, got %s", table.Type()) + } + }) + + t.Run("consecutive td tags create siblings", func(t *testing.T) { + input := `<html><body><table><tr><td>X<td>Y</tr></table></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + table, err := body.GetMapKey("table") + if err != nil { + t.Fatalf("failed to get table: %v", err) + } + if table.Type() != model.TypeMap { + t.Fatalf("table should be map, got %s", table.Type()) + } + tr, err := table.GetMapKey("tr") + if tr == nil { + tbody, tbodyErr := table.GetMapKey("tbody") + if tbodyErr != nil { + t.Fatalf("failed to get tr or tbody from table: %v / %v", err, tbodyErr) + } + tr, err = tbody.GetMapKey("tr") + if err != nil { + t.Fatalf("failed to get tr from tbody: %v", err) + } + } + td, err := tr.GetMapKey("td") + if err != nil { + t.Fatalf("failed to get td: %v", err) + } + length, err := td.SliceLen() + if err != nil { + t.Fatalf("td should be a slice: %v", err) + } + if length != 2 { + t.Fatalf("expected 2 td elements, got %d", length) + } + }) + + t.Run("h2 closes open p", func(t *testing.T) { + input := `<html><body><p>Text<h2>Heading</h2></body></html>` + val := harnessRead(t, input) + + body, _ := val.GetMapKey("body") + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + pStr, _ := p.StringValue() + if pStr != "Text" { + t.Fatalf("p should contain only 'Text' (h2 closed it), got '%s'", pStr) + } + h2, err := body.GetMapKey("h2") + if err != nil { + t.Fatalf("h2 should be sibling of p: %v", err) + } + h2Str, _ := h2.StringValue() + if h2Str != "Heading" { + t.Fatalf("h2 should contain 'Heading', got '%s'", h2Str) + } + }) + + t.Run("blockquote closes open p", func(t *testing.T) { + input := `<html><body><p>Before<blockquote>Quote</blockquote></body></html>` + val := harnessRead(t, input) + + body, _ := val.GetMapKey("body") + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + pStr, _ := p.StringValue() + if pStr != "Before" { + t.Fatalf("p should contain only 'Before' (blockquote closed it), got '%s'", pStr) + } + bq, err := body.GetMapKey("blockquote") + if err != nil { + t.Fatalf("blockquote should be sibling of p: %v", err) + } + bqStr, _ := bq.StringValue() + if bqStr != "Quote" { + t.Fatalf("blockquote should contain 'Quote', got '%s'", bqStr) + } + }) + + t.Run("tr closes previous tr", func(t *testing.T) { + input := `<html><body><table><tr><td>R1C1<td>R1C2<tr><td>R2C1<td>R2C2</table></body></html>` + val := harnessRead(t, input) + + body, _ := val.GetMapKey("body") + table, _ := body.GetMapKey("table") + tr, err := table.GetMapKey("tr") + if tr == nil || err != nil { + tbody, _ := table.GetMapKey("tbody") + if tbody != nil { + tr, err = tbody.GetMapKey("tr") + } + } + if err != nil { + t.Fatalf("failed to get tr: %v", err) + } + trLen, err := tr.SliceLen() + if err != nil { + t.Fatalf("tr should be a slice: %v", err) + } + if trLen != 2 { + t.Fatalf("expected 2 tr elements, got %d", trLen) + } + firstTr, _ := tr.GetSliceIndex(0) + firstTd, err := firstTr.GetMapKey("td") + if err != nil { + t.Fatalf("first tr should have td: %v", err) + } + firstTdLen, _ := firstTd.SliceLen() + if firstTdLen != 2 { + t.Fatalf("first tr should have 2 td, got %d", firstTdLen) + } + }) +} + +func TestReadHTMLEntityDecoding(t *testing.T) { + t.Run("numeric decimal entities", func(t *testing.T) { + input := `<html><body><p>&#65;&#66;&#67;</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, _ := p.StringValue() + if text != "ABC" { + t.Fatalf("expected 'ABC' from numeric entities, got '%s'", text) + } + }) + + t.Run("numeric hex entities", func(t *testing.T) { + input := `<html><body><p>&#x48;&#x65;&#x6C;&#x6C;&#x6F;</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, _ := p.StringValue() + if text != "Hello" { + t.Fatalf("expected 'Hello' from hex entities, got '%s'", text) + } + }) + + t.Run("mixed named and numeric entities", func(t *testing.T) { + input := `<html><body><p>&lt;&#65;&gt;</p></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + text, _ := p.StringValue() + if text != "<A>" { + t.Fatalf("expected '<A>' from mixed entities, got '%s'", text) + } + }) + + t.Run("entity in attribute value", func(t *testing.T) { + input := `<html><body><a href="page?a=1&amp;b=2">link</a></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + a, err := body.GetMapKey("a") + if err != nil { + t.Fatalf("failed to get a: %v", err) + } + href, err := a.GetMapKey("-href") + if err != nil { + t.Fatalf("failed to get -href: %v", err) + } + hrefStr, _ := href.StringValue() + if hrefStr != "page?a=1&b=2" { + t.Fatalf("expected decoded attribute value, got '%s'", hrefStr) + } + }) +} + +func TestReadHTMLCaseInsensitive(t *testing.T) { + t.Run("uppercase tags normalized to lowercase", func(t *testing.T) { + input := `<HTML><BODY><DIV>content</DIV></BODY></HTML>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("uppercase BODY should be accessible as body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("uppercase DIV should be accessible as div: %v", err) + } + text, _ := div.StringValue() + if text != "content" { + t.Fatalf("expected 'content', got '%s'", text) + } + }) + + t.Run("mixed case tags normalized", func(t *testing.T) { + input := `<Html><Body><Span>text</Span></Body></Html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("mixed case Body should be accessible as body: %v", err) + } + span, err := body.GetMapKey("span") + if err != nil { + t.Fatalf("mixed case Span should be accessible as span: %v", err) + } + text, _ := span.StringValue() + if text != "text" { + t.Fatalf("expected 'text', got '%s'", text) + } + }) + + t.Run("uppercase attributes normalized to lowercase", func(t *testing.T) { + input := `<html><body><div ID="main" CLASS="box">text</div></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("failed to get div: %v", err) + } + id, err := div.GetMapKey("-id") + if err != nil { + t.Fatalf("uppercase ID should be accessible as -id: %v", err) + } + idStr, _ := id.StringValue() + if idStr != "main" { + t.Fatalf("expected 'main', got '%s'", idStr) + } + }) +} + +func harnessWrite(t *testing.T, val *model.Value) string { + t.Helper() + f := parsing.Format("html") + writer, err := f.NewWriter(parsing.DefaultWriterOptions()) + if err != nil { + t.Fatalf("failed to create writer: %v", err) + } + out, err := writer.Write(val) + if err != nil { + t.Fatalf("failed to write: %v", err) + } + return string(out) +} + +func harnessReadStructured(t *testing.T, input string) *model.Value { + t.Helper() + f := parsing.Format("html") + opts := parsing.DefaultReaderOptions() + opts.Ext["html-mode"] = "structured" + reader, err := f.NewReader(opts) + if err != nil { + t.Fatalf("failed to create reader: %v", err) + } + val, err := reader.Read([]byte(input)) + if err != nil { + t.Fatalf("failed to read: %v", err) + } + return val +} + +func TestWriteHTMLBasic(t *testing.T) { + t.Run("write simple element", func(t *testing.T) { + val := model.NewMapValue() + _ = val.SetMapKey("p", model.NewStringValue("Hello")) + out := harnessWrite(t, val) + if !strings.Contains(out, "<p>Hello</p>") { + t.Fatalf("expected <p>Hello</p>, got %s", out) + } + }) + + t.Run("write element with attributes", func(t *testing.T) { + inner := model.NewMapValue() + _ = inner.SetMapKey("-id", model.NewStringValue("main")) + _ = inner.SetMapKey("#text", model.NewStringValue("Content")) + val := model.NewMapValue() + _ = val.SetMapKey("div", inner) + out := harnessWrite(t, val) + if !strings.Contains(out, `id="main"`) { + t.Fatalf("expected id attribute, got %s", out) + } + if !strings.Contains(out, "Content") { + t.Fatalf("expected content text, got %s", out) + } + }) + + t.Run("write nested elements", func(t *testing.T) { + p := model.NewStringValue("Hello") + body := model.NewMapValue() + _ = body.SetMapKey("p", p) + val := model.NewMapValue() + _ = val.SetMapKey("body", body) + out := harnessWrite(t, val) + if !strings.Contains(out, "<body>") { + t.Fatalf("expected body tag, got %s", out) + } + if !strings.Contains(out, "<p>Hello</p>") { + t.Fatalf("expected p tag, got %s", out) + } + }) + + t.Run("write void elements", func(t *testing.T) { + inner := model.NewMapValue() + _ = inner.SetMapKey("-type", model.NewStringValue("text")) + _ = inner.SetMapKey("-name", model.NewStringValue("user")) + val := model.NewMapValue() + _ = val.SetMapKey("input", inner) + out := harnessWrite(t, val) + if !strings.Contains(out, "/>") { + t.Fatalf("expected self-closing tag, got %s", out) + } + if !strings.Contains(out, `type="text"`) { + t.Fatalf("expected type attribute, got %s", out) + } + }) + + t.Run("write slice of elements", func(t *testing.T) { + slice := model.NewSliceValue() + _ = slice.Append(model.NewStringValue("First")) + _ = slice.Append(model.NewStringValue("Second")) + val := model.NewMapValue() + _ = val.SetMapKey("p", slice) + out := harnessWrite(t, val) + if !strings.Contains(out, "<p>First</p>") { + t.Fatalf("expected first p, got %s", out) + } + if !strings.Contains(out, "<p>Second</p>") { + t.Fatalf("expected second p, got %s", out) + } + }) + + t.Run("write escapes special chars in attributes", func(t *testing.T) { + inner := model.NewMapValue() + _ = inner.SetMapKey("-data", model.NewStringValue(`a"b<c>`)) + val := model.NewMapValue() + _ = val.SetMapKey("div", inner) + out := harnessWrite(t, val) + if !strings.Contains(out, "&quot;") { + t.Fatalf("expected escaped quote, got %s", out) + } + if !strings.Contains(out, "&lt;") { + t.Fatalf("expected escaped lt, got %s", out) + } + }) + + t.Run("write escapes special chars in text", func(t *testing.T) { + val := model.NewMapValue() + _ = val.SetMapKey("p", model.NewStringValue("a < b & c > d")) + out := harnessWrite(t, val) + if !strings.Contains(out, "&lt;") { + t.Fatalf("expected escaped lt, got %s", out) + } + if !strings.Contains(out, "&amp;") { + t.Fatalf("expected escaped amp, got %s", out) + } + }) + + t.Run("write empty element", func(t *testing.T) { + val := model.NewMapValue() + _ = val.SetMapKey("div", model.NewStringValue("")) + out := harnessWrite(t, val) + if !strings.Contains(out, "<div></div>") { + t.Fatalf("expected empty div, got %s", out) + } + }) +} + +func TestWriteHTMLCompact(t *testing.T) { + t.Run("compact mode has no indentation", func(t *testing.T) { + f := parsing.Format("html") + opts := parsing.DefaultWriterOptions() + opts.Compact = true + writer, err := f.NewWriter(opts) + if err != nil { + t.Fatalf("failed to create writer: %v", err) + } + body := model.NewMapValue() + _ = body.SetMapKey("p", model.NewStringValue("Hello")) + val := model.NewMapValue() + _ = val.SetMapKey("body", body) + out, err := writer.Write(val) + if err != nil { + t.Fatalf("failed to write: %v", err) + } + output := string(out) + if strings.Contains(output, " <p>") { + t.Fatalf("compact mode should not have indentation, got %s", output) + } + }) + + t.Run("compact nested output has no internal newlines", func(t *testing.T) { + f := parsing.Format("html") + opts := parsing.DefaultWriterOptions() + opts.Compact = true + writer, err := f.NewWriter(opts) + if err != nil { + t.Fatalf("failed to create writer: %v", err) + } + ul := model.NewMapValue() + liSlice := model.NewSliceValue() + _ = liSlice.Append(model.NewStringValue("A")) + _ = liSlice.Append(model.NewStringValue("B")) + _ = liSlice.Append(model.NewStringValue("C")) + _ = ul.SetMapKey("li", liSlice) + body := model.NewMapValue() + _ = body.SetMapKey("ul", ul) + val := model.NewMapValue() + _ = val.SetMapKey("body", body) + out, err := writer.Write(val) + if err != nil { + t.Fatalf("failed to write: %v", err) + } + output := strings.TrimRight(string(out), "\n") + if strings.Contains(output, "\n") { + t.Fatalf("compact output should have no internal newlines, got: %s", string(out)) + } + }) +} + +func TestReadHTMLStructuredMode(t *testing.T) { + t.Run("structured mode returns tag field", func(t *testing.T) { + input := `<html><body><p>Hello</p></body></html>` + val := harnessReadStructured(t, input) + tag, err := val.GetMapKey("tag") + if err != nil { + t.Fatalf("failed to get tag: %v", err) + } + tagStr, _ := tag.StringValue() + if tagStr != "html" { + t.Fatalf("expected tag 'html', got '%s'", tagStr) + } + }) + + t.Run("structured mode returns attrs map", func(t *testing.T) { + input := `<html><body><div id="main" class="container">Hello</div></body></html>` + val := harnessReadStructured(t, input) + children, err := val.GetMapKey("children") + if err != nil { + t.Fatalf("failed to get children: %v", err) + } + length, _ := children.SliceLen() + for i := 0; i < length; i++ { + child, _ := children.GetSliceIndex(i) + tag, _ := child.GetMapKey("tag") + tagStr, _ := tag.StringValue() + if tagStr == "body" { + bodyChildren, _ := child.GetMapKey("children") + divEl, _ := bodyChildren.GetSliceIndex(0) + attrs, err := divEl.GetMapKey("attrs") + if err != nil { + t.Fatalf("failed to get attrs: %v", err) + } + if attrs.Type() != model.TypeMap { + t.Fatalf("expected map for attrs, got %s", attrs.Type()) + } + idVal, err := attrs.GetMapKey("id") + if err != nil { + t.Fatalf("failed to get id attr: %v", err) + } + idStr, _ := idVal.StringValue() + if idStr != "main" { + t.Fatalf("expected id 'main', got '%s'", idStr) + } + classVal, err := attrs.GetMapKey("class") + if err != nil { + t.Fatalf("failed to get class attr: %v", err) + } + classStr, _ := classVal.StringValue() + if classStr != "container" { + t.Fatalf("expected class 'container', got '%s'", classStr) + } + return + } + } + t.Fatal("body element not found in structured output") + }) + + t.Run("structured mode returns children slice", func(t *testing.T) { + input := `<html><head><title>T</title></head><body></body></html>` + val := harnessReadStructured(t, input) + children, err := val.GetMapKey("children") + if err != nil { + t.Fatalf("failed to get children: %v", err) + } + length, _ := children.SliceLen() + if length != 2 { + t.Fatalf("expected 2 children (head, body), got %d", length) + } + }) + + t.Run("structured mode returns text field", func(t *testing.T) { + input := `<html><body><p>Hello World</p></body></html>` + val := harnessReadStructured(t, input) + children, err := val.GetMapKey("children") + if err != nil { + t.Fatalf("failed to get children: %v", err) + } + length, _ := children.SliceLen() + found := false + for i := 0; i < length; i++ { + child, _ := children.GetSliceIndex(i) + tag, _ := child.GetMapKey("tag") + tagStr, _ := tag.StringValue() + if tagStr == "body" { + bodyChildren, _ := child.GetMapKey("children") + pEl, _ := bodyChildren.GetSliceIndex(0) + text, err := pEl.GetMapKey("text") + if err != nil { + t.Fatalf("failed to get text: %v", err) + } + textStr, _ := text.StringValue() + if textStr != "Hello World" { + t.Fatalf("expected 'Hello World', got '%s'", textStr) + } + found = true + break + } + } + if !found { + t.Fatal("body element not found in structured output") + } + }) +} + +func TestHTMLReadWriteReadConsistency(t *testing.T) { + t.Run("read write read produces same structure", func(t *testing.T) { + input := `<html><head><title>Test</title></head><body><div id="main"><p>Hello</p></div></body></html>` + val1 := harnessRead(t, input) + + body1, _ := val1.GetMapKey("body") + div1, _ := body1.GetMapKey("div") + id1, _ := div1.GetMapKey("-id") + id1Str, _ := id1.StringValue() + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body2, err := val2.GetMapKey("body") + if err != nil { + t.Fatalf("round-trip lost body: %v", err) + } + div2, err := body2.GetMapKey("div") + if err != nil { + t.Fatalf("round-trip lost div: %v", err) + } + id2, err := div2.GetMapKey("-id") + if err != nil { + t.Fatalf("round-trip lost id attribute: %v", err) + } + id2Str, _ := id2.StringValue() + if id1Str != id2Str { + t.Fatalf("round-trip changed id from '%s' to '%s'", id1Str, id2Str) + } + }) + + t.Run("round-trip preserves entity encoding", func(t *testing.T) { + input := `<html><body><p>&lt;script&gt;</p></body></html>` + val1 := harnessRead(t, input) + + body, _ := val1.GetMapKey("body") + p, _ := body.GetMapKey("p") + text1, _ := p.StringValue() + if text1 != "<script>" { + t.Fatalf("expected decoded '<script>', got '%s'", text1) + } + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body2, _ := val2.GetMapKey("body") + p2, _ := body2.GetMapKey("p") + text2, _ := p2.StringValue() + if text2 != "<script>" { + t.Fatalf("round-trip changed text from '<script>' to '%s'", text2) + } + }) + + t.Run("round-trip preserves nested structure", func(t *testing.T) { + input := `<html><body><div><span>text</span></div></body></html>` + val1 := harnessRead(t, input) + + body1, _ := val1.GetMapKey("body") + div1, _ := body1.GetMapKey("div") + span1, _ := div1.GetMapKey("span") + text1, _ := span1.StringValue() + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body2, err := val2.GetMapKey("body") + if err != nil { + t.Fatalf("round-trip lost body: %v", err) + } + div2, err := body2.GetMapKey("div") + if err != nil { + t.Fatalf("round-trip lost div: %v", err) + } + span2, err := div2.GetMapKey("span") + if err != nil { + t.Fatalf("round-trip lost span: %v", err) + } + text2, _ := span2.StringValue() + if text1 != text2 { + t.Fatalf("round-trip changed text from '%s' to '%s'", text1, text2) + } + }) +} + +func TestHTMLRawTextEntities(t *testing.T) { + t.Run("script preserves entities unescaped", func(t *testing.T) { + input := `<html><body><script>var x = "&amp;";</script></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + script, err := body.GetMapKey("script") + if err != nil { + t.Fatalf("failed to get script: %v", err) + } + text, _ := script.StringValue() + if !strings.Contains(text, "&amp;") { + t.Fatalf("script content should preserve '&amp;' literally, got '%s'", text) + } + }) + + t.Run("style preserves entities unescaped", func(t *testing.T) { + input := `<html><head><style>.cls { content: "&lt;"; }</style></head><body></body></html>` + val := harnessRead(t, input) + + head, err := val.GetMapKey("head") + if err != nil { + t.Fatalf("failed to get head: %v", err) + } + style, err := head.GetMapKey("style") + if err != nil { + t.Fatalf("failed to get style: %v", err) + } + text, _ := style.StringValue() + if !strings.Contains(text, "&lt;") { + t.Fatalf("style content should preserve '&lt;' literally, got '%s'", text) + } + }) +} + +func TestHTMLComplexImplicitClosing(t *testing.T) { + t.Run("p closed by another p with text", func(t *testing.T) { + input := `<html><body><p>First paragraph<p>Second paragraph</body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("failed to get p: %v", err) + } + pLen, err := p.SliceLen() + if err != nil { + t.Fatalf("p should be slice: %v", err) + } + if pLen != 2 { + t.Fatalf("expected 2 p elements, got %d", pLen) + } + first, _ := p.GetSliceIndex(0) + firstText, _ := first.StringValue() + if firstText != "First paragraph" { + t.Fatalf("first p should contain 'First paragraph', got '%s'", firstText) + } + second, _ := p.GetSliceIndex(1) + secondText, _ := second.StringValue() + if secondText != "Second paragraph" { + t.Fatalf("second p should contain 'Second paragraph', got '%s'", secondText) + } + }) + + t.Run("nested lists with implicit li closing", func(t *testing.T) { + input := `<html><body><ul><li>A<ul><li>A1<li>A2</ul><li>B</ul></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + ul, err := body.GetMapKey("ul") + if err != nil { + t.Fatalf("failed to get ul: %v", err) + } + li, err := ul.GetMapKey("li") + if err != nil { + t.Fatalf("failed to get li: %v", err) + } + liLen, err := li.SliceLen() + if err != nil { + t.Fatalf("li should be slice: %v", err) + } + if liLen != 2 { + t.Fatalf("outer ul should have 2 li (A and B), got %d", liLen) + } + firstLi, _ := li.GetSliceIndex(0) + nestedUl, err := firstLi.GetMapKey("ul") + if err != nil { + t.Fatalf("first li should contain nested ul: %v", err) + } + nestedLi, err := nestedUl.GetMapKey("li") + if err != nil { + t.Fatalf("nested ul should have li: %v", err) + } + nestedLen, _ := nestedLi.SliceLen() + if nestedLen != 2 { + t.Fatalf("nested ul should have 2 li (A1, A2), got %d", nestedLen) + } + }) + + t.Run("definition list implicit closing", func(t *testing.T) { + input := `<html><body><dl><dt>Term1<dd>Def1<dt>Term2<dd>Def2</dl></body></html>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("failed to get body: %v", err) + } + dl, err := body.GetMapKey("dl") + if err != nil { + t.Fatalf("failed to get dl: %v", err) + } + dt, err := dl.GetMapKey("dt") + if err != nil { + t.Fatalf("failed to get dt: %v", err) + } + dtLen, _ := dt.SliceLen() + if dtLen != 2 { + t.Fatalf("expected 2 dt elements, got %d", dtLen) + } + dd, err := dl.GetMapKey("dd") + if err != nil { + t.Fatalf("failed to get dd: %v", err) + } + ddLen, _ := dd.SliceLen() + if ddLen != 2 { + t.Fatalf("expected 2 dd elements, got %d", ddLen) + } + }) +} + +func TestHTMLStructuredModeImplicitClosing(t *testing.T) { + t.Run("structured mode reflects implicit p closing", func(t *testing.T) { + input := `<html><body><p>First<p>Second</body></html>` + val := harnessReadStructured(t, input) + + children, _ := val.GetMapKey("children") + length, _ := children.SliceLen() + var bodyEl *model.Value + for i := 0; i < length; i++ { + child, _ := children.GetSliceIndex(i) + tag, _ := child.GetMapKey("tag") + tagStr, _ := tag.StringValue() + if tagStr == "body" { + bodyEl = child + break + } + } + if bodyEl == nil { + t.Fatal("body not found in structured output") + } + bodyChildren, _ := bodyEl.GetMapKey("children") + bodyLen, _ := bodyChildren.SliceLen() + if bodyLen != 2 { + t.Fatalf("body should have 2 children (two p elements), got %d", bodyLen) + } + firstP, _ := bodyChildren.GetSliceIndex(0) + firstTag, _ := firstP.GetMapKey("tag") + firstTagStr, _ := firstTag.StringValue() + if firstTagStr != "p" { + t.Fatalf("first child should be p, got '%s'", firstTagStr) + } + secondP, _ := bodyChildren.GetSliceIndex(1) + secondTag, _ := secondP.GetMapKey("tag") + secondTagStr, _ := secondTag.StringValue() + if secondTagStr != "p" { + t.Fatalf("second child should be p, got '%s'", secondTagStr) + } + }) +} + +func TestHTMLCombinedBehaviors(t *testing.T) { + t.Run("uppercase tags with entities and implicit closing", func(t *testing.T) { + input := `<HTML><BODY><P>First &amp; &#65;<P>Second</BODY></HTML>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("uppercase BODY should be normalized to body: %v", err) + } + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("uppercase P should be normalized to p: %v", err) + } + pLen, err := p.SliceLen() + if err != nil { + t.Fatalf("p should be slice due to implicit closing: %v", err) + } + if pLen != 2 { + t.Fatalf("expected 2 p elements, got %d", pLen) + } + first, _ := p.GetSliceIndex(0) + firstStr, _ := first.StringValue() + if firstStr != "First & A" { + t.Fatalf("expected 'First & A' with decoded entities, got '%s'", firstStr) + } + }) + + t.Run("attributes with mixed case and numeric entities", func(t *testing.T) { + input := `<html><body><div TITLE="&#60;test&#62;" CLASS="box">content</div></body></html>` + val := harnessRead(t, input) + + body, _ := val.GetMapKey("body") + div, _ := body.GetMapKey("div") + title, err := div.GetMapKey("-title") + if err != nil { + t.Fatalf("uppercase TITLE should be normalized to -title: %v", err) + } + titleStr, _ := title.StringValue() + if titleStr != "<test>" { + t.Fatalf("expected '<test>' from numeric entities, got '%s'", titleStr) + } + class, err := div.GetMapKey("-class") + if err != nil { + t.Fatalf("uppercase CLASS should be normalized to -class: %v", err) + } + classStr, _ := class.StringValue() + if classStr != "box" { + t.Fatalf("expected 'box', got '%s'", classStr) + } + }) +} + +func TestHTMLWriterRawTextRoundTrip(t *testing.T) { + t.Run("script content survives write then read", func(t *testing.T) { + input := `<html><body><script>if (a < b && c > d) { alert("ok"); }</script></body></html>` + val1 := harnessRead(t, input) + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body, err := val2.GetMapKey("body") + if err != nil { + t.Fatalf("round-trip lost body: %v", err) + } + script, err := body.GetMapKey("script") + if err != nil { + t.Fatalf("round-trip lost script: %v", err) + } + text, _ := script.StringValue() + if !strings.Contains(text, "a < b") { + t.Fatalf("script content corrupted in round-trip, got '%s'", text) + } + if !strings.Contains(text, `alert("ok")`) { + t.Fatalf("script content lost quotes in round-trip, got '%s'", text) + } + }) + + t.Run("style content survives write then read", func(t *testing.T) { + input := `<html><head><style>div > p { content: "a&b"; }</style></head><body></body></html>` + val1 := harnessRead(t, input) + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + head, err := val2.GetMapKey("head") + if err != nil { + t.Fatalf("round-trip lost head: %v", err) + } + style, err := head.GetMapKey("style") + if err != nil { + t.Fatalf("round-trip lost style: %v", err) + } + text, _ := style.StringValue() + if !strings.Contains(text, "div > p") { + t.Fatalf("style content corrupted in round-trip, got '%s'", text) + } + }) +} + +func TestHTMLVoidElementCycle(t *testing.T) { + t.Run("img with attrs survives round-trip", func(t *testing.T) { + input := `<html><body><img src="photo.jpg" alt="A photo"/></body></html>` + val1 := harnessRead(t, input) + + output := harnessWrite(t, val1) + if !strings.Contains(output, "/>") { + t.Fatalf("writer should self-close img, got: %s", output) + } + + val2 := harnessRead(t, output) + body, _ := val2.GetMapKey("body") + img, err := body.GetMapKey("img") + if err != nil { + t.Fatalf("round-trip lost img: %v", err) + } + src, err := img.GetMapKey("-src") + if err != nil { + t.Fatalf("round-trip lost src attr: %v", err) + } + srcStr, _ := src.StringValue() + if srcStr != "photo.jpg" { + t.Fatalf("expected 'photo.jpg', got '%s'", srcStr) + } + alt, err := img.GetMapKey("-alt") + if err != nil { + t.Fatalf("round-trip lost alt attr: %v", err) + } + altStr, _ := alt.StringValue() + if altStr != "A photo" { + t.Fatalf("expected 'A photo', got '%s'", altStr) + } + }) + + t.Run("input with boolean attr survives round-trip", func(t *testing.T) { + input := `<html><body><input type="checkbox" checked disabled/></body></html>` + val1 := harnessRead(t, input) + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body, _ := val2.GetMapKey("body") + inp, err := body.GetMapKey("input") + if err != nil { + t.Fatalf("round-trip lost input: %v", err) + } + typ, err := inp.GetMapKey("-type") + if err != nil { + t.Fatalf("round-trip lost type attr: %v", err) + } + typStr, _ := typ.StringValue() + if typStr != "checkbox" { + t.Fatalf("expected 'checkbox', got '%s'", typStr) + } + checked, err := inp.GetMapKey("-checked") + if err != nil { + t.Fatalf("round-trip lost checked attr: %v", err) + } + checkedStr, _ := checked.StringValue() + if checkedStr != "" { + t.Fatalf("boolean attr should stay empty string after round-trip, got '%s'", checkedStr) + } + }) +} + +func TestHTMLNormalizationCycle(t *testing.T) { + t.Run("fragment normalizes and stays normalized after round-trip", func(t *testing.T) { + input := `<p>Just a paragraph</p>` + val1 := harnessRead(t, input) + + _, err := val1.GetMapKey("head") + if err != nil { + t.Fatalf("fragment should normalize to include head: %v", err) + } + body1, err := val1.GetMapKey("body") + if err != nil { + t.Fatalf("fragment should normalize to include body: %v", err) + } + p1, err := body1.GetMapKey("p") + if err != nil { + t.Fatalf("body should contain p: %v", err) + } + text1, _ := p1.StringValue() + if text1 != "Just a paragraph" { + t.Fatalf("expected 'Just a paragraph', got '%s'", text1) + } + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + _, err = val2.GetMapKey("head") + if err != nil { + t.Fatalf("normalized structure lost head after round-trip: %v", err) + } + body2, err := val2.GetMapKey("body") + if err != nil { + t.Fatalf("normalized structure lost body after round-trip: %v", err) + } + p2, err := body2.GetMapKey("p") + if err != nil { + t.Fatalf("round-trip lost p element: %v", err) + } + text2, _ := p2.StringValue() + if text2 != "Just a paragraph" { + t.Fatalf("round-trip changed text from 'Just a paragraph' to '%s'", text2) + } + }) + + t.Run("head-only input gets body after normalization and round-trip", func(t *testing.T) { + input := `<html><head><title>Only Head</title></head></html>` + val1 := harnessRead(t, input) + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + head2, err := val2.GetMapKey("head") + if err != nil { + t.Fatalf("round-trip lost head: %v", err) + } + title, err := head2.GetMapKey("title") + if err != nil { + t.Fatalf("round-trip lost title: %v", err) + } + titleStr, _ := title.StringValue() + if titleStr != "Only Head" { + t.Fatalf("expected 'Only Head', got '%s'", titleStr) + } + _, err = val2.GetMapKey("body") + if err != nil { + t.Fatalf("round-trip should preserve normalized body: %v", err) + } + }) +} + +func TestHTMLCompactModeCycle(t *testing.T) { + t.Run("compact mode void elements with attrs", func(t *testing.T) { + f := parsing.Format("html") + opts := parsing.DefaultWriterOptions() + opts.Compact = true + writer, err := f.NewWriter(opts) + if err != nil { + t.Fatalf("failed to create writer: %v", err) + } + + inner := model.NewMapValue() + _ = inner.SetMapKey("-src", model.NewStringValue("img.png")) + _ = inner.SetMapKey("-alt", model.NewStringValue("test")) + body := model.NewMapValue() + _ = body.SetMapKey("img", inner) + _ = body.SetMapKey("p", model.NewStringValue("text")) + val := model.NewMapValue() + _ = val.SetMapKey("body", body) + + out, err := writer.Write(val) + if err != nil { + t.Fatalf("failed to write: %v", err) + } + output := string(out) + if strings.Contains(output, " ") { + t.Fatalf("compact mode should have no indentation, got: %s", output) + } + if !strings.Contains(output, "/>") { + t.Fatalf("compact mode should still self-close void elements, got: %s", output) + } + if !strings.Contains(output, "<p>text</p>") { + t.Fatalf("compact mode should render p element inline, got: %s", output) + } + }) + + t.Run("compact output can be re-read correctly", func(t *testing.T) { + f := parsing.Format("html") + opts := parsing.DefaultWriterOptions() + opts.Compact = true + writer, err := f.NewWriter(opts) + if err != nil { + t.Fatalf("failed to create writer: %v", err) + } + + input := `<html><body><div id="main"><p>Hello</p><p>World</p></div></body></html>` + val1 := harnessRead(t, input) + + out, err := writer.Write(val1) + if err != nil { + t.Fatalf("failed to write compact: %v", err) + } + + val2 := harnessRead(t, string(out)) + body, err := val2.GetMapKey("body") + if err != nil { + t.Fatalf("compact round-trip lost body: %v", err) + } + div, err := body.GetMapKey("div") + if err != nil { + t.Fatalf("compact round-trip lost div: %v", err) + } + id, err := div.GetMapKey("-id") + if err != nil { + t.Fatalf("compact round-trip lost id attr: %v", err) + } + idStr, _ := id.StringValue() + if idStr != "main" { + t.Fatalf("expected 'main', got '%s'", idStr) + } + p, err := div.GetMapKey("p") + if err != nil { + t.Fatalf("compact round-trip lost p: %v", err) + } + pLen, _ := p.SliceLen() + if pLen != 2 { + t.Fatalf("compact round-trip should preserve 2 p siblings, got %d", pLen) + } + }) +} + +func TestHTMLImplicitClosingCycle(t *testing.T) { + t.Run("implicit p closing with entities survives round-trip", func(t *testing.T) { + input := `<html><body><p>First &amp; foremost<p>Second &lt;item&gt;</body></html>` + val1 := harnessRead(t, input) + + body1, _ := val1.GetMapKey("body") + p1, _ := body1.GetMapKey("p") + p1Len, _ := p1.SliceLen() + if p1Len != 2 { + t.Fatalf("expected 2 p elements from implicit closing, got %d", p1Len) + } + first1, _ := p1.GetSliceIndex(0) + firstText1, _ := first1.StringValue() + if firstText1 != "First & foremost" { + t.Fatalf("expected decoded 'First & foremost', got '%s'", firstText1) + } + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body2, _ := val2.GetMapKey("body") + p2, err := body2.GetMapKey("p") + if err != nil { + t.Fatalf("round-trip lost p elements: %v", err) + } + p2Len, _ := p2.SliceLen() + if p2Len != 2 { + t.Fatalf("round-trip should preserve 2 p siblings, got %d", p2Len) + } + first2, _ := p2.GetSliceIndex(0) + firstText2, _ := first2.StringValue() + if firstText2 != "First & foremost" { + t.Fatalf("round-trip corrupted first p text, got '%s'", firstText2) + } + second2, _ := p2.GetSliceIndex(1) + secondText2, _ := second2.StringValue() + if secondText2 != "Second <item>" { + t.Fatalf("round-trip corrupted second p text, got '%s'", secondText2) + } + }) + + t.Run("implicit li closing in nested list survives round-trip", func(t *testing.T) { + input := `<html><body><ul><li>A<li>B<li>C</ul></body></html>` + val1 := harnessRead(t, input) + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body, _ := val2.GetMapKey("body") + ul, err := body.GetMapKey("ul") + if err != nil { + t.Fatalf("round-trip lost ul: %v", err) + } + li, err := ul.GetMapKey("li") + if err != nil { + t.Fatalf("round-trip lost li: %v", err) + } + liLen, _ := li.SliceLen() + if liLen != 3 { + t.Fatalf("round-trip should preserve 3 li siblings, got %d", liLen) + } + second, _ := li.GetSliceIndex(1) + secondStr, _ := second.StringValue() + if secondStr != "B" { + t.Fatalf("expected 'B', got '%s'", secondStr) + } + }) +} + +func TestHTMLStructuredModeDeepTree(t *testing.T) { + t.Run("structured mode with attrs text and children", func(t *testing.T) { + input := `<html><body><div id="wrap"><p class="intro">Hello</p><span>World</span></div></body></html>` + val := harnessReadStructured(t, input) + + tag, _ := val.GetMapKey("tag") + tagStr, _ := tag.StringValue() + if tagStr != "html" { + t.Fatalf("root tag should be html, got '%s'", tagStr) + } + + children, _ := val.GetMapKey("children") + cLen, _ := children.SliceLen() + + var bodyEl *model.Value + for i := 0; i < cLen; i++ { + child, _ := children.GetSliceIndex(i) + ct, _ := child.GetMapKey("tag") + cts, _ := ct.StringValue() + if cts == "body" { + bodyEl = child + break + } + } + if bodyEl == nil { + t.Fatal("body not found in structured output") + } + + bodyChildren, _ := bodyEl.GetMapKey("children") + bcLen, _ := bodyChildren.SliceLen() + if bcLen != 1 { + t.Fatalf("body should have 1 child (div), got %d", bcLen) + } + + divEl, _ := bodyChildren.GetSliceIndex(0) + divTag, _ := divEl.GetMapKey("tag") + divTagStr, _ := divTag.StringValue() + if divTagStr != "div" { + t.Fatalf("expected div tag, got '%s'", divTagStr) + } + + divAttrs, _ := divEl.GetMapKey("attrs") + idVal, err := divAttrs.GetMapKey("id") + if err != nil { + t.Fatalf("structured mode should preserve id attr: %v", err) + } + idStr, _ := idVal.StringValue() + if idStr != "wrap" { + t.Fatalf("expected id 'wrap', got '%s'", idStr) + } + + divChildren, _ := divEl.GetMapKey("children") + dcLen, _ := divChildren.SliceLen() + if dcLen != 2 { + t.Fatalf("div should have 2 children (p and span), got %d", dcLen) + } + + pEl, _ := divChildren.GetSliceIndex(0) + pTag, _ := pEl.GetMapKey("tag") + pTagStr, _ := pTag.StringValue() + if pTagStr != "p" { + t.Fatalf("first child should be p, got '%s'", pTagStr) + } + pAttrs, _ := pEl.GetMapKey("attrs") + classVal, err := pAttrs.GetMapKey("class") + if err != nil { + t.Fatalf("structured mode should preserve class attr: %v", err) + } + classStr, _ := classVal.StringValue() + if classStr != "intro" { + t.Fatalf("expected class 'intro', got '%s'", classStr) + } + pText, _ := pEl.GetMapKey("text") + pTextStr, _ := pText.StringValue() + if pTextStr != "Hello" { + t.Fatalf("expected text 'Hello', got '%s'", pTextStr) + } + + spanEl, _ := divChildren.GetSliceIndex(1) + spanTag, _ := spanEl.GetMapKey("tag") + spanTagStr, _ := spanTag.StringValue() + if spanTagStr != "span" { + t.Fatalf("second child should be span, got '%s'", spanTagStr) + } + spanText, _ := spanEl.GetMapKey("text") + spanTextStr, _ := spanText.StringValue() + if spanTextStr != "World" { + t.Fatalf("expected text 'World', got '%s'", spanTextStr) + } + }) + + t.Run("structured mode void element has empty children", func(t *testing.T) { + input := `<html><body><br/></body></html>` + val := harnessReadStructured(t, input) + + children, _ := val.GetMapKey("children") + cLen, _ := children.SliceLen() + var bodyEl *model.Value + for i := 0; i < cLen; i++ { + child, _ := children.GetSliceIndex(i) + ct, _ := child.GetMapKey("tag") + cts, _ := ct.StringValue() + if cts == "body" { + bodyEl = child + break + } + } + if bodyEl == nil { + t.Fatal("body not found") + } + bodyChildren, _ := bodyEl.GetMapKey("children") + bcLen, _ := bodyChildren.SliceLen() + if bcLen != 1 { + t.Fatalf("body should have 1 child (br), got %d", bcLen) + } + brEl, _ := bodyChildren.GetSliceIndex(0) + brTag, _ := brEl.GetMapKey("tag") + brTagStr, _ := brTag.StringValue() + if brTagStr != "br" { + t.Fatalf("expected br tag, got '%s'", brTagStr) + } + brChildren, _ := brEl.GetMapKey("children") + brCLen, _ := brChildren.SliceLen() + if brCLen != 0 { + t.Fatalf("void element should have 0 children in structured mode, got %d", brCLen) + } + }) +} + +func TestHTMLWriterEntityEscaping(t *testing.T) { + t.Run("writer uses named entities for text and attrs", func(t *testing.T) { + inner := model.NewMapValue() + _ = inner.SetMapKey("-title", model.NewStringValue(`He said "hello" & <goodbye>`)) + _ = inner.SetMapKey("#text", model.NewStringValue("A < B & C > D")) + val := model.NewMapValue() + _ = val.SetMapKey("p", inner) + out := harnessWrite(t, val) + + if !strings.Contains(out, "&amp;") { + t.Fatalf("writer should escape & in text with &amp;, got: %s", out) + } + if !strings.Contains(out, "&lt;") { + t.Fatalf("writer should escape < in text with &lt;, got: %s", out) + } + if !strings.Contains(out, "&gt;") { + t.Fatalf("writer should escape > in text with &gt;, got: %s", out) + } + if !strings.Contains(out, "&quot;") { + t.Fatalf("writer should escape \" in attrs with &quot;, got: %s", out) + } + }) + + t.Run("entity escaping round-trips correctly", func(t *testing.T) { + inner := model.NewMapValue() + _ = inner.SetMapKey("-data", model.NewStringValue(`x"y<z>&w`)) + _ = inner.SetMapKey("#text", model.NewStringValue("1 < 2 & 3 > 0")) + val := model.NewMapValue() + _ = val.SetMapKey("div", inner) + + out := harnessWrite(t, val) + val2 := harnessRead(t, out) + + div, _ := val2.GetMapKey("body") + if div == nil { + div = val2 + } + var target *model.Value + d, err := div.GetMapKey("div") + if err == nil { + target = d + } else { + target = div + } + + dataAttr, err := target.GetMapKey("-data") + if err != nil { + t.Fatalf("round-trip lost data attr: %v", err) + } + dataStr, _ := dataAttr.StringValue() + if dataStr != `x"y<z>&w` { + t.Fatalf("round-trip corrupted attr value, expected 'x\"y<z>&w', got '%s'", dataStr) + } + + textVal, err := target.GetMapKey("#text") + if err != nil { + t.Fatalf("round-trip lost text: %v", err) + } + textStr, _ := textVal.StringValue() + if textStr != "1 < 2 & 3 > 0" { + t.Fatalf("round-trip corrupted text, got '%s'", textStr) + } + }) +} + +func TestHTMLCombinedComplexScenarios(t *testing.T) { + t.Run("uppercase implicit closing with entities round-trip structured", func(t *testing.T) { + input := `<HTML><BODY><P>First &amp; &#65;<P>Second &lt;end&gt;</BODY></HTML>` + val := harnessReadStructured(t, input) + + tag, _ := val.GetMapKey("tag") + tagStr, _ := tag.StringValue() + if tagStr != "html" { + t.Fatalf("structured root should be html, got '%s'", tagStr) + } + + children, _ := val.GetMapKey("children") + cLen, _ := children.SliceLen() + var bodyEl *model.Value + for i := 0; i < cLen; i++ { + child, _ := children.GetSliceIndex(i) + ct, _ := child.GetMapKey("tag") + cts, _ := ct.StringValue() + if cts == "body" { + bodyEl = child + break + } + } + if bodyEl == nil { + t.Fatal("body not found in structured mode") + } + + bodyChildren, _ := bodyEl.GetMapKey("children") + bcLen, _ := bodyChildren.SliceLen() + if bcLen != 2 { + t.Fatalf("body should have 2 children (two p elements), got %d", bcLen) + } + + p1, _ := bodyChildren.GetSliceIndex(0) + p1Tag, _ := p1.GetMapKey("tag") + p1TagStr, _ := p1Tag.StringValue() + if p1TagStr != "p" { + t.Fatalf("first child should be lowercase p, got '%s'", p1TagStr) + } + p1Text, _ := p1.GetMapKey("text") + p1TextStr, _ := p1Text.StringValue() + if p1TextStr != "First & A" { + t.Fatalf("expected 'First & A' with decoded entities, got '%s'", p1TextStr) + } + + p2, _ := bodyChildren.GetSliceIndex(1) + p2Text, _ := p2.GetMapKey("text") + p2TextStr, _ := p2Text.StringValue() + if p2TextStr != "Second <end>" { + t.Fatalf("expected 'Second <end>' with decoded entities, got '%s'", p2TextStr) + } + }) + + t.Run("mixed content with attrs and siblings through full pipeline", func(t *testing.T) { + input := `<html><body><div class="list"><p>Para 1</p><p>Para 2</p><img src="icon.png"/></div></body></html>` + val1 := harnessRead(t, input) + + body1, _ := val1.GetMapKey("body") + div1, _ := body1.GetMapKey("div") + class1, _ := div1.GetMapKey("-class") + classStr1, _ := class1.StringValue() + if classStr1 != "list" { + t.Fatalf("expected class 'list', got '%s'", classStr1) + } + p1, _ := div1.GetMapKey("p") + p1Len, _ := p1.SliceLen() + if p1Len != 2 { + t.Fatalf("expected 2 p elements, got %d", p1Len) + } + img1, _ := div1.GetMapKey("img") + imgSrc1, _ := img1.GetMapKey("-src") + imgSrcStr1, _ := imgSrc1.StringValue() + if imgSrcStr1 != "icon.png" { + t.Fatalf("expected src 'icon.png', got '%s'", imgSrcStr1) + } + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body2, _ := val2.GetMapKey("body") + div2, err := body2.GetMapKey("div") + if err != nil { + t.Fatalf("round-trip lost div: %v", err) + } + class2, _ := div2.GetMapKey("-class") + classStr2, _ := class2.StringValue() + if classStr2 != "list" { + t.Fatalf("round-trip changed class from 'list' to '%s'", classStr2) + } + p2, err := div2.GetMapKey("p") + if err != nil { + t.Fatalf("round-trip lost p elements: %v", err) + } + p2Len, _ := p2.SliceLen() + if p2Len != 2 { + t.Fatalf("round-trip should preserve 2 p siblings, got %d", p2Len) + } + img2, err := div2.GetMapKey("img") + if err != nil { + t.Fatalf("round-trip lost img: %v", err) + } + imgSrc2, _ := img2.GetMapKey("-src") + imgSrcStr2, _ := imgSrc2.StringValue() + if imgSrcStr2 != "icon.png" { + t.Fatalf("round-trip changed img src to '%s'", imgSrcStr2) + } + }) + + t.Run("definition list with entities through structured mode", func(t *testing.T) { + input := `<html><body><dl><dt>Term &amp; Def<dd>Def &lt;1&gt;<dt>Term &#50;<dd>Def &#51;</dl></body></html>` + val := harnessReadStructured(t, input) + + children, _ := val.GetMapKey("children") + cLen, _ := children.SliceLen() + var bodyEl *model.Value + for i := 0; i < cLen; i++ { + child, _ := children.GetSliceIndex(i) + ct, _ := child.GetMapKey("tag") + cts, _ := ct.StringValue() + if cts == "body" { + bodyEl = child + break + } + } + if bodyEl == nil { + t.Fatal("body not found") + } + + bodyChildren, _ := bodyEl.GetMapKey("children") + bcLen, _ := bodyChildren.SliceLen() + if bcLen != 1 { + t.Fatalf("body should have 1 child (dl), got %d", bcLen) + } + dlEl, _ := bodyChildren.GetSliceIndex(0) + dlTag, _ := dlEl.GetMapKey("tag") + dlTagStr, _ := dlTag.StringValue() + if dlTagStr != "dl" { + t.Fatalf("expected dl tag, got '%s'", dlTagStr) + } + dlChildren, _ := dlEl.GetMapKey("children") + dlCLen, _ := dlChildren.SliceLen() + if dlCLen != 4 { + t.Fatalf("dl should have 4 children (dt,dd,dt,dd), got %d", dlCLen) + } + + dt1, _ := dlChildren.GetSliceIndex(0) + dt1Tag, _ := dt1.GetMapKey("tag") + dt1TagStr, _ := dt1Tag.StringValue() + if dt1TagStr != "dt" { + t.Fatalf("first child should be dt, got '%s'", dt1TagStr) + } + dt1Text, _ := dt1.GetMapKey("text") + dt1TextStr, _ := dt1Text.StringValue() + if dt1TextStr != "Term & Def" { + t.Fatalf("expected 'Term & Def', got '%s'", dt1TextStr) + } + + dd1, _ := dlChildren.GetSliceIndex(1) + dd1Text, _ := dd1.GetMapKey("text") + dd1TextStr, _ := dd1Text.StringValue() + if dd1TextStr != "Def <1>" { + t.Fatalf("expected 'Def <1>', got '%s'", dd1TextStr) + } + + dt2, _ := dlChildren.GetSliceIndex(2) + dt2Text, _ := dt2.GetMapKey("text") + dt2TextStr, _ := dt2Text.StringValue() + if dt2TextStr != "Term 2" { + t.Fatalf("expected 'Term 2' from &#50;, got '%s'", dt2TextStr) + } + }) +} + +func TestHTMLTableCycle(t *testing.T) { + t.Run("multi-row table with implicit closing survives round-trip", func(t *testing.T) { + input := `<html><body><table><tr><td>R1C1<td>R1C2<tr><td>R2C1<td>R2C2</table></body></html>` + val1 := harnessRead(t, input) + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body, err := val2.GetMapKey("body") + if err != nil { + t.Fatalf("round-trip lost body: %v", err) + } + table, err := body.GetMapKey("table") + if err != nil { + t.Fatalf("round-trip lost table: %v", err) + } + tr, _ := table.GetMapKey("tr") + if tr == nil { + tbody, _ := table.GetMapKey("tbody") + if tbody != nil { + tr, _ = tbody.GetMapKey("tr") + } + } + if tr == nil { + t.Fatal("round-trip lost tr elements") + } + trLen, _ := tr.SliceLen() + if trLen != 2 { + t.Fatalf("round-trip should preserve 2 tr rows, got %d", trLen) + } + firstTr, _ := tr.GetSliceIndex(0) + firstTd, _ := firstTr.GetMapKey("td") + firstTdLen, _ := firstTd.SliceLen() + if firstTdLen != 2 { + t.Fatalf("first tr should have 2 td after round-trip, got %d", firstTdLen) + } + secondTr, _ := tr.GetSliceIndex(1) + secondTd, _ := secondTr.GetMapKey("td") + secondTdLen, _ := secondTd.SliceLen() + if secondTdLen != 2 { + t.Fatalf("second tr should have 2 td after round-trip, got %d", secondTdLen) + } + r2c1, _ := secondTd.GetSliceIndex(0) + r2c1Str, _ := r2c1.StringValue() + if r2c1Str != "R2C1" { + t.Fatalf("expected 'R2C1', got '%s'", r2c1Str) + } + }) +} + +func TestHTMLOrphanNormalizationCycle(t *testing.T) { + t.Run("orphan content normalizes and round-trips", func(t *testing.T) { + input := `<html><p>orphan paragraph</p></html>` + val1 := harnessRead(t, input) + + body1, err := val1.GetMapKey("body") + if err != nil { + t.Fatalf("orphan should be placed in body: %v", err) + } + p1, err := body1.GetMapKey("p") + if err != nil { + t.Fatalf("body should contain orphan p: %v", err) + } + text1, _ := p1.StringValue() + if text1 != "orphan paragraph" { + t.Fatalf("expected 'orphan paragraph', got '%s'", text1) + } + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body2, _ := val2.GetMapKey("body") + p2, err := body2.GetMapKey("p") + if err != nil { + t.Fatalf("round-trip lost orphan p: %v", err) + } + text2, _ := p2.StringValue() + if text2 != "orphan paragraph" { + t.Fatalf("round-trip changed text to '%s'", text2) + } + }) +} + +func TestHTMLBlockLevelClosingCycle(t *testing.T) { + t.Run("h2 closing p with entities round-trips", func(t *testing.T) { + input := `<html><body><p>Intro &amp; more<h2>Title</h2><p>After</p></body></html>` + val1 := harnessRead(t, input) + + body1, _ := val1.GetMapKey("body") + p1, _ := body1.GetMapKey("p") + p1Len, _ := p1.SliceLen() + if p1Len != 2 { + t.Fatalf("should have 2 p elements (h2 closed first p), got %d", p1Len) + } + firstP1, _ := p1.GetSliceIndex(0) + firstText1, _ := firstP1.StringValue() + if firstText1 != "Intro & more" { + t.Fatalf("first p should be 'Intro & more', got '%s'", firstText1) + } + + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body2, _ := val2.GetMapKey("body") + p2, err := body2.GetMapKey("p") + if err != nil { + t.Fatalf("round-trip lost p: %v", err) + } + p2Len, _ := p2.SliceLen() + if p2Len != 2 { + t.Fatalf("round-trip should preserve 2 p elements, got %d", p2Len) + } + h2, err := body2.GetMapKey("h2") + if err != nil { + t.Fatalf("round-trip lost h2: %v", err) + } + h2Text, _ := h2.StringValue() + if h2Text != "Title" { + t.Fatalf("h2 text should be 'Title', got '%s'", h2Text) + } + }) +} + +func TestHTMLWriterVoidSelfClose(t *testing.T) { + t.Run("void element from empty string self-closes", func(t *testing.T) { + val := model.NewMapValue() + _ = val.SetMapKey("br", model.NewStringValue("")) + out := harnessWrite(t, val) + if strings.Contains(out, "</br>") { + t.Fatalf("writer should self-close br, not use </br>, got: %s", out) + } + if !strings.Contains(out, "<br/>") { + t.Fatalf("writer should output <br/>, got: %s", out) + } + }) + + t.Run("void element with attrs self-closes", func(t *testing.T) { + inner := model.NewMapValue() + _ = inner.SetMapKey("-src", model.NewStringValue("img.png")) + _ = inner.SetMapKey("-alt", model.NewStringValue("test")) + val := model.NewMapValue() + _ = val.SetMapKey("img", inner) + out := harnessWrite(t, val) + if strings.Contains(out, "</img>") { + t.Fatalf("writer should self-close img, not use </img>, got: %s", out) + } + if !strings.Contains(out, "src=\"img.png\"") { + t.Fatalf("expected src attr in output, got: %s", out) + } + }) +} + +func TestHTMLCompactCycleStrict(t *testing.T) { + t.Run("compact round-trip preserves structure with no internal newlines", func(t *testing.T) { + f := parsing.Format("html") + opts := parsing.DefaultWriterOptions() + opts.Compact = true + writer, err := f.NewWriter(opts) + if err != nil { + t.Fatalf("failed to create writer: %v", err) + } + + input := `<html><body><div><p>A</p><p>B</p><span>C</span></div></body></html>` + val1 := harnessRead(t, input) + + out, err := writer.Write(val1) + if err != nil { + t.Fatalf("failed to write compact: %v", err) + } + output := strings.TrimRight(string(out), "\n") + if strings.Contains(output, "\n") { + t.Fatalf("compact output should have no internal newlines, got: %s", string(out)) + } + + val2 := harnessRead(t, string(out)) + body2, _ := val2.GetMapKey("body") + div2, err := body2.GetMapKey("div") + if err != nil { + t.Fatalf("compact round-trip lost div: %v", err) + } + p2, err := div2.GetMapKey("p") + if err != nil { + t.Fatalf("compact round-trip lost p: %v", err) + } + p2Len, _ := p2.SliceLen() + if p2Len != 2 { + t.Fatalf("compact round-trip should preserve 2 p siblings, got %d", p2Len) + } + span2, err := div2.GetMapKey("span") + if err != nil { + t.Fatalf("compact round-trip lost span: %v", err) + } + spanStr, _ := span2.StringValue() + if spanStr != "C" { + t.Fatalf("expected 'C', got '%s'", spanStr) + } + }) +} + +func TestHTMLHardenedPipeline(t *testing.T) { + t.Run("combined features with case normalization entities implicit-close and raw text", func(t *testing.T) { + input := `<HTML><BODY><P>Intro &amp; &#65;<H2 CLASS="title">Heading</H2><TABLE><TR><TD>R1<TD>R2<TR><TD>R3<TD>R4</TABLE><SCRIPT>var x = "<p>not a tag</p>";</SCRIPT></BODY></HTML>` + val := harnessRead(t, input) + + body, err := val.GetMapKey("body") + if err != nil { + t.Fatalf("body missing: %v", err) + } + + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("p missing (h2 should have closed it as sibling): %v", err) + } + pStr, _ := p.StringValue() + if pStr != "Intro & A" { + t.Fatalf("p text should be 'Intro & A' (entities decoded), got '%s'", pStr) + } + + h2, err := body.GetMapKey("h2") + if err != nil { + t.Fatalf("h2 missing: %v", err) + } + h2Class, err := h2.GetMapKey("-class") + if err != nil { + t.Fatalf("h2 should have class attr (case-normalized): %v", err) + } + h2ClassStr, _ := h2Class.StringValue() + if h2ClassStr != "title" { + t.Fatalf("expected class 'title', got '%s'", h2ClassStr) + } + h2Text, _ := h2.GetMapKey("#text") + h2TextStr, _ := h2Text.StringValue() + if h2TextStr != "Heading" { + t.Fatalf("expected h2 text 'Heading', got '%s'", h2TextStr) + } + + table, err := body.GetMapKey("table") + if err != nil { + t.Fatalf("table missing: %v", err) + } + tr, _ := table.GetMapKey("tr") + if tr == nil { + tbody, _ := table.GetMapKey("tbody") + if tbody != nil { + tr, _ = tbody.GetMapKey("tr") + } + } + if tr == nil { + t.Fatal("tr missing from table") + } + trLen, _ := tr.SliceLen() + if trLen != 2 { + t.Fatalf("expected 2 tr (implicit closing), got %d", trLen) + } + + script, err := body.GetMapKey("script") + if err != nil { + t.Fatalf("script missing: %v", err) + } + scriptStr, _ := script.StringValue() + if !strings.Contains(scriptStr, "<p>not a tag</p>") { + t.Fatalf("script should preserve HTML tags as raw text, got '%s'", scriptStr) + } + }) + + t.Run("hardened pipeline round-trip", func(t *testing.T) { + input := `<HTML><BODY><P>Intro &amp; &#65;<H2 CLASS="title">Heading</H2><TABLE><TR><TD>R1<TD>R2<TR><TD>R3<TD>R4</TABLE><SCRIPT>var x = "<p>not a tag</p>";</SCRIPT></BODY></HTML>` + val1 := harnessRead(t, input) + output := harnessWrite(t, val1) + val2 := harnessRead(t, output) + + body, _ := val2.GetMapKey("body") + p, err := body.GetMapKey("p") + if err != nil { + t.Fatalf("round-trip lost p: %v", err) + } + pStr, _ := p.StringValue() + if pStr != "Intro & A" { + t.Fatalf("round-trip corrupted p text, got '%s'", pStr) + } + + h2, err := body.GetMapKey("h2") + if err != nil { + t.Fatalf("round-trip lost h2: %v", err) + } + h2Class, _ := h2.GetMapKey("-class") + h2ClassStr, _ := h2Class.StringValue() + if h2ClassStr != "title" { + t.Fatalf("round-trip lost h2 class, got '%s'", h2ClassStr) + } + + table, _ := body.GetMapKey("table") + tr, _ := table.GetMapKey("tr") + if tr == nil { + tbody, _ := table.GetMapKey("tbody") + if tbody != nil { + tr, _ = tbody.GetMapKey("tr") + } + } + trLen, _ := tr.SliceLen() + if trLen != 2 { + t.Fatalf("round-trip should preserve 2 tr, got %d", trLen) + } + + script, err := body.GetMapKey("script") + if err != nil { + t.Fatalf("round-trip lost script: %v", err) + } + scriptStr, _ := script.StringValue() + if !strings.Contains(scriptStr, "<p>not a tag</p>") { + t.Fatalf("round-trip corrupted script content, got '%s'", scriptStr) + } + }) +} + +func TestFormatRegistration(t *testing.T) { + t.Run("html format is registered", func(t *testing.T) { + readers := parsing.RegisteredReaders() + found := false + for _, r := range readers { + if string(r) == "html" { + found = true + break + } + } + if !found { + t.Fatal("html format should be registered as reader") + } + }) + + t.Run("html format is registered as writer", func(t *testing.T) { + writers := parsing.RegisteredWriters() + found := false + for _, w := range writers { + if string(w) == "html" { + found = true + break + } + } + if !found { + t.Fatal("html format should be registered as writer") + } + }) +} diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..13e77ff --- /dev/null +++ b/test.sh @@ -0,0 +1,16 @@ +#!/bin/bash +set -e +cd "$(dirname "$0")" + +case "$1" in + base) + go test -v ./... + ;; + new) + go test -v -tags=html -run '^Test(Read|Write|Format|HTML)' ./parsing/html/ + ;; + *) + echo "Usage: ./test.sh {base|new}" + exit 1 + ;; +esac
diff --git a/parsing/html/html_test.go b/parsing/html/html_test.go
new file mode 100644
index 0000000..e1d275e
--- /dev/null
+++ b/parsing/html/html_test.go
@@ -0,0 +1,2864 @@
+//go:build html
+
+package html_test
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/tomwright/dasel/v3/model"
+ "github.com/tomwright/dasel/v3/parsing"
+ _ "github.com/tomwright/dasel/v3/parsing/html"
+)
+
+func harnessRead(t *testing.T, input string) *model.Value {
+ t.Helper()
+ f := parsing.Format("html")
+ reader, err := f.NewReader(parsing.DefaultReaderOptions())
+ if err != nil {
+ t.Fatalf("failed to create reader: %v", err)
+ }
+ val, err := reader.Read([]byte(input))
+ if err != nil {
+ t.Fatalf("failed to read: %v", err)
+ }
+ return val
+}
+
+func TestReadBasicHTML(t *testing.T) {
+ t.Run("simple html document", func(t *testing.T) {
+ input := `<html><head><title>Test</title></head><body><p>Hello</p></body></html>`
+ val := harnessRead(t, input)
+ if val.Type() != model.TypeMap {
+ t.Fatalf("expected map, got %s", val.Type())
+ }
+ })
+
+ t.Run("html with doctype", func(t *testing.T) {
+ input := `<!DOCTYPE html><html><head><title>Test</title></head><body></body></html>`
+ val := harnessRead(t, input)
+ if val.Type() != model.TypeMap {
+ t.Fatalf("expected map, got %s", val.Type())
+ }
+ })
+
+ t.Run("fragment without html wrapper", func(t *testing.T) {
+ input := `<div><p>Content</p></div>`
+ val := harnessRead(t, input)
+ if val.Type() != model.TypeMap {
+ t.Fatalf("expected map, got %s", val.Type())
+ }
+ })
+}
+
+func TestReadHTMLElements(t *testing.T) {
+ t.Run("nested elements", func(t *testing.T) {
+ input := `<html><body><div><span>text</span></div></body></html>`
+ val := harnessRead(t, input)
+
+ body, err := val.GetMapKey("body")
+ if err != nil {
+ t.Fatalf("failed to get body: %v", err)
+ }
+ div, err := body.GetMapKey("div")
+ if err != nil {
+ t.Fatalf("failed to get div: %v", err)
+ }
+ span, err := div.GetMapKey("span")
+ if err != nil {
+ t.Fatalf("failed to get span: %v", err)
+ }
+ text, err := span.StringValue()
+ if err != nil {
+ t.Fatalf("failed to get text: %v", err)
+ }
+ if text != "text" {
+ t.Fatalf("expected 'text', got '%s'", text)
+ }
+ })
+
+ t.Run("multiple same-tag siblings become slice", func(t *testing.T) {
+ input := `<html><body><p>First</p><p>Second</p><p>Third</p></body></html>`
+ val := harnessRead(t, input)
+
+ body, err := val.GetMapKey("body")
+ if err != nil {
+ t.Fatalf("failed to get body: %v", err)
+ }
+ ps, err := body.GetMapKey("p")
+ if err != nil {
+ t.Fatalf("failed to get p: %v", err)
+ }
+ if ps.Type() != model.TypeSlice {
+ t.Fatalf("expected slice for multiple p tags, got %s", ps.Type())
+ }

Xet Storage Details

Size:
85.9 kB
·
Xet hash:
179fca1164acf11773b3d7f0f24566994ddc87a5a495935eccc8b7176d74f78e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.