]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Make the HTML collector parsing more robust
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Mon, 6 Feb 2023 16:29:12 +0000 (17:29 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 7 Feb 2023 08:11:02 +0000 (09:11 +0100)
Most notably better handling self-closing elements

Closes #10698

publisher/htmlElementsCollector.go
publisher/htmlElementsCollector_test.go

index 91e1237a94b8ac02175637c94f69b89dbe4d008f..c3b88c4cc141b95b1fca8e9d34722fabb97db6b0 100644 (file)
@@ -294,9 +294,10 @@ func htmlLexElementStart(w *htmlElementsCollectorWriter) htmlCollectorStateFunc
                }
 
                tagName := w.buff.Bytes()[1:]
+               isSelfClosing := tagName[len(tagName)-1] == '/'
 
                switch {
-               case skipInnerElementRe.Match(tagName):
+               case !isSelfClosing && skipInnerElementRe.Match(tagName):
                        // pre, script etc. We collect classes etc. on the surrounding
                        // element, but skip the inner content.
                        w.backup()
@@ -432,10 +433,18 @@ func parseStartTag(s string) string {
        })
 
        if spaceIndex == -1 {
-               return s[1 : len(s)-1]
+               s = s[1 : len(s)-1]
+       } else {
+               s = s[1:spaceIndex]
        }
 
-       return s[1:spaceIndex]
+       if s[len(s)-1] == '/' {
+               // Self closing.
+               s = s[:len(s)-1]
+       }
+
+       return s
+
 }
 
 // isClosedByTag reports whether b ends with a closing tag for tagName.
@@ -487,7 +496,7 @@ LOOP:
                }
        }
 
-       if state != 2 {
+       if state != 2 || lo >= hi {
                return false
        }
 
index 11590e0a3e3730757ef2c1d5bbafdfed9b604287..f9c9424cbacce194561baf037746e42a4c03e5d2 100644 (file)
@@ -110,6 +110,9 @@ func TestClassCollector(t *testing.T) {
                {"DOCTYPE should beskipped", `<!DOCTYPE html>`, f("", "", "")},
                {"Comments should be skipped", `<!-- example comment -->`, f("", "", "")},
                {"Comments with elements before and after", `<div></div><!-- example comment --><span><span>`, f("div span", "", "")},
+               {"Self closing tag", `<div><hr/></div>`, f("div hr", "", "")},
+               // svg with self closing style tag.
+               {"SVG with self closing style tag", `<svg><style/><g><path class="foo"/></g></svg>`, f("g path style svg", "foo", "")},
                // Issue #8530
                {"Comment with single quote", `<!-- Hero Area Image d'accueil --><i class="foo">`, f("i", "foo", "")},
                {"Uppercase tags", `<DIV></DIV>`, f("div", "", "")},
@@ -174,6 +177,7 @@ func TestEndsWithTag(t *testing.T) {
                {"match space", "foo<  / div>", "div", true},
                {"match space 2", "foo<  / div   \n>", "div", true},
                {"match case", "foo</DIV>", "div", true},
+               {"self closing", `</defs><g><g><path fill="#010101" d=asdf"/>`, "div", false},
        } {
                c.Run(test.name, func(c *qt.C) {
                        got := isClosedByTag([]byte(test.s), []byte(test.tagName))