-
Notifications
You must be signed in to change notification settings - Fork 189
fix: coalesce duplicate class attributes in CodeBlock and Heading #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package html | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCoalesceClassAttrs(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| in []string | ||
| want []string | ||
| }{ | ||
| { | ||
| name: "no class attrs", | ||
| in: []string{`id="foo"`}, | ||
| want: []string{`id="foo"`}, | ||
| }, | ||
| { | ||
| name: "single class attr unchanged", | ||
| in: []string{`class="language-go"`}, | ||
| want: []string{`class="language-go"`}, | ||
| }, | ||
| { | ||
| name: "two class attrs merged", | ||
| in: []string{`class="language-yml"`, `class="my-class"`}, | ||
| want: []string{`class="language-yml my-class"`}, | ||
| }, | ||
| { | ||
| name: "class attrs with other attrs preserved", | ||
| in: []string{`class="language-go"`, `id="code1"`, `class="highlight"`}, | ||
| want: []string{`class="language-go highlight"`, `id="code1"`}, | ||
| }, | ||
| { | ||
| name: "empty input", | ||
| in: nil, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "no attrs at all", | ||
| in: []string{}, | ||
| want: []string{}, | ||
| }, | ||
| { | ||
| name: "empty class value stripped", | ||
| in: []string{`class=""`, `class="my-class"`}, | ||
| want: []string{`class="my-class"`}, | ||
| }, | ||
| { | ||
| name: "multi-word class values preserved", | ||
| in: []string{`class="foo bar"`, `class="baz"`}, | ||
| want: []string{`class="foo bar baz"`}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := coalesceClassAttrs(tt.in) | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Errorf("coalesceClassAttrs(%v) = %v, want %v", tt.in, got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,40 @@ func TestTagParagraphCode(t *testing.T) { | |
| doTestsParam(t, tests, params) | ||
| } | ||
|
|
||
| // TestCodeBlockClassCoalescing verifies that when a code block has both | ||
| // a language annotation and a custom class attribute, they are merged into | ||
| // a single class attribute. See https://github.com/gomarkdown/markdown/issues/209. | ||
| func TestCodeBlockClassCoalescing(t *testing.T) { | ||
| input := "``` yml\ntext: something\n```\n" | ||
|
|
||
| p := parser.NewWithExtensions(parser.FencedCode) | ||
| doc := p.Parse([]byte(input)) | ||
|
|
||
| // Walk the AST and add a custom class to the CodeBlock node. | ||
| ast.WalkFunc(doc, func(node ast.Node, entering bool) ast.WalkStatus { | ||
| if cb, ok := node.(*ast.CodeBlock); ok { | ||
| cb.Attribute = &ast.Attribute{ | ||
| Classes: [][]byte{[]byte("my-class")}, | ||
| } | ||
| } | ||
| return ast.GoToNext | ||
|
Comment on lines
+83
to
+89
|
||
| }) | ||
|
|
||
| renderer := html.NewRenderer(html.RendererOptions{}) | ||
| got := string(Render(doc, renderer)) | ||
|
|
||
| // Before the fix, this produced two separate class= attributes: | ||
| // <code class="language-yml" class="my-class"> | ||
| // After the fix, they should be coalesced: | ||
| // <code class="language-yml my-class"> | ||
| expected := `<pre><code class="language-yml my-class">text: something | ||
| </code></pre> | ||
| ` | ||
| if got != expected { | ||
| t.Errorf("CodeBlock class coalescing failed.\nExpected:\n%s\nGot:\n%s", expected, got) | ||
| } | ||
| } | ||
|
|
||
| func TestRenderNodeHookLinkAttrs(t *testing.T) { | ||
| tests := []string{ | ||
| `[Click Me](gopher://foo.bar "Click Me")`, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.