@@ -4,11 +4,34 @@ import (
44 "bytes"
55 "io"
66 "regexp"
7+ "strings"
78
89 "github.com/osteele/gojekyll/utils"
910 "golang.org/x/net/html"
1011)
1112
13+ // HTML void elements that are self-closing and never have an end tag.
14+ // See https://html.spec.whatwg.org/multipage/syntax.html#void-elements
15+ var htmlVoidElements = map [string ]bool {
16+ "area" : true , "base" : true , "br" : true , "col" : true ,
17+ "embed" : true , "hr" : true , "img" : true , "input" : true ,
18+ "link" : true , "meta" : true , "param" : true , "source" : true ,
19+ "track" : true , "wbr" : true ,
20+ }
21+
22+ // isVoidElement returns true if the raw token bytes represent a void element.
23+ func isVoidElement (raw []byte ) bool {
24+ s := strings .TrimLeft (string (raw ), "< " )
25+ // Extract tag name (up to space, /, or >)
26+ for i , c := range s {
27+ if c == ' ' || c == '/' || c == '>' || c == '\t' || c == '\n' {
28+ s = s [:i ]
29+ break
30+ }
31+ }
32+ return htmlVoidElements [strings .ToLower (s )]
33+ }
34+
1235var markdownAttrRE = regexp .MustCompile (`\s*markdown\s*=[^\s>]*\s*` )
1336
1437// Used inside markdown=1.
@@ -107,12 +130,12 @@ loop:
107130 if err == io .EOF {
108131 return utils .WrapError (err ,
109132 "unexpected EOF while processing markdown attribute. " +
110- "Common causes: unclosed HTML tags (use <br/> instead of <br>) , " +
133+ "Common causes: unclosed HTML tags, " +
111134 "or mismatched opening/closing tags" )
112135 }
113136 return err
114137 case html .StartTagToken :
115- if ! notATagRE .Match (z .Raw ()) {
138+ if ! notATagRE .Match (z .Raw ()) && ! isVoidElement ( z . Raw ()) {
116139 depth ++
117140 }
118141 case html .EndTagToken :
@@ -179,12 +202,12 @@ loop:
179202 if err == io .EOF {
180203 return utils .WrapError (err ,
181204 "unexpected EOF while processing markdown=\" 0\" attribute. " +
182- "Common causes: unclosed HTML tags (use <br/> instead of <br>) , " +
205+ "Common causes: unclosed HTML tags, " +
183206 "or mismatched opening/closing tags" )
184207 }
185208 return err
186209 case html .StartTagToken :
187- if ! notATagRE .Match (z .Raw ()) {
210+ if ! notATagRE .Match (z .Raw ()) && ! isVoidElement ( z . Raw ()) {
188211 depth ++
189212 }
190213 case html .EndTagToken :
0 commit comments