forgejo/modules/markup/markdown/transform_html.go
Beowulf d436de90b1 Add to html button in markdown type="button" (#10520)
This is for preventing that a markdown button is recognized as button for submission in a html form.

Buttons can't be stripped from the markdown due to: https://codeberg.org/forgejo/forgejo/pulls/7670#issuecomment-4086608

There is no issue with buttons if they always have `type="button"`, so this should be fine.

This is a "follow-up" to !7670.

Fixes #7656

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10520
Reviewed-by: Otto <otto@codeberg.org>
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Beowulf <beowulf@beocode.eu>
Co-committed-by: Beowulf <beowulf@beocode.eu>
2025-12-21 05:21:27 +01:00

28 lines
751 B
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package markdown
import (
"strings"
"forgejo.org/modules/markup"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
)
func (g *ASTTransformer) addTypeToButton(v *ast.RawHTML, segment string) {
segment = strings.TrimPrefix(segment, "<button")
newTag := ast.NewString([]byte(`<button type="button"` + segment))
newTag.SetCode(true)
v.Parent().ReplaceChild(v.Parent(), v, newTag)
}
func (g *ASTTransformer) transformRawHTML(_ *markup.RenderContext, v *ast.RawHTML, reader text.Reader) {
segment := string(v.Segments.Value(reader.Source()))
if strings.HasPrefix(segment, "<button") {
g.addTypeToButton(v, segment)
}
}