fix: utils IsValidHTML, uses golang net html parse (#28)
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import "regexp"
|
import (
|
||||||
|
"strings"
|
||||||
|
"golang.org/x/net/html"
|
||||||
|
)
|
||||||
|
|
||||||
func Filter[A any](arr []A, f func(A) bool) []A {
|
func Filter[A any](arr []A, f func(A) bool) []A {
|
||||||
var res []A
|
var res []A
|
||||||
@@ -14,19 +17,7 @@ func Filter[A any](arr []A, f func(A) bool) []A {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func IsValidHTML(input string) bool {
|
func IsValidHTML(input string) bool {
|
||||||
// Check for <!DOCTYPE> declaration (case-insensitive)
|
r := strings.NewReader(input)
|
||||||
doctypeRegex := regexp.MustCompile(`(?i)<!DOCTYPE\s+html>`)
|
_, err := html.Parse(r)
|
||||||
if !doctypeRegex.MatchString(input) {
|
return err == nil
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for <html> and </html> tags (case-insensitive)
|
|
||||||
htmlTagRegex := regexp.MustCompile(`(?i)<html[\s\S]*?>[\s\S]*?</html>`)
|
|
||||||
if !htmlTagRegex.MatchString(input) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for <body> and </body> tags (case-insensitive)
|
|
||||||
bodyTagRegex := regexp.MustCompile(`(?i)<body[\s\S]*?>[\s\S]*?</body>`)
|
|
||||||
return bodyTagRegex.MatchString(input)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user