From 034f12af3bf6c31338538dc1b291537869459ed9 Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Tue, 30 Jul 2024 15:59:49 +0200 Subject: [PATCH] Add SVG element SVG is now part of HTML 5 and the SVG element a valid top-level element. The other SVG element, however, do not stand on their own. https://html.spec.whatwg.org/dev/embedded-content-other.html#svg-0 Note: This does not match up with the definition of CommonMark HTML blocks. CommonMark does not recognize the SVG element as a HTML block. In this respect, however, it would seem that CommonMark is lagging behind. https://spec.commonmark.org/0.31.2/#html-blocks --- parser/block.go | 1 + parser/block_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 parser/block_test.go diff --git a/parser/block.go b/parser/block.go index 3ef5334..f6e9da0 100644 --- a/parser/block.go +++ b/parser/block.go @@ -76,6 +76,7 @@ var ( "output": {}, "progress": {}, "section": {}, + "svg": {}, "video": {}, } ) diff --git a/parser/block_test.go b/parser/block_test.go new file mode 100644 index 0000000..4e0a808 --- /dev/null +++ b/parser/block_test.go @@ -0,0 +1,53 @@ +package parser + +import ( + "bytes" + "testing" + + "github.com/gomarkdown/markdown/ast" +) + +// Inside HTML, no Markdown is parsed. +func TestHtmlP(t *testing.T) { + input := "

*not emph*

\n" + p := NewWithExtensions(CommonExtensions) + doc := p.Parse([]byte(input)) + var buf bytes.Buffer + ast.Print(&buf, doc) + got := buf.String() + exp := "HTMLBlock '

*not emph*

'\n" + if got != exp { + t.Errorf("\nInput [%#v]\nExpected[%#v]\nGot [%#v]\n", + input, exp, got) + } +} + +// Inside SVG, so the RECT element is passed through. +func TestSVG(t *testing.T) { + input := " *no emph* \n" + p := NewWithExtensions(CommonExtensions) + doc := p.Parse([]byte(input)) + var buf bytes.Buffer + ast.Print(&buf, doc) + got := buf.String() + exp := "HTMLBlock ' *no emph* '\n" + if got != exp { + t.Errorf("\nInput [%#v]\nExpected[%#v]\nGot [%#v]\n", + input, exp, got) + } +} + +// The RECT element on its own is nothing special, so Markdown is parsed. +func TestRect(t *testing.T) { + input := " *emph* \n" + p := NewWithExtensions(CommonExtensions) + doc := p.Parse([]byte(input)) + var buf bytes.Buffer + ast.Print(&buf, doc) + got := buf.String() + exp := "Paragraph\n Text\n HTMLSpan ''\n Text\n Emph\n Text 'emph'\n Text\n HTMLSpan ''\n" + if got != exp { + t.Errorf("\nInput [%#v]\nExpected[%#v]\nGot [%#v]\n", + input, exp, got) + } +}