diff --git a/transform/livereloadinject/livereloadinject.go b/transform/livereloadinject/livereloadinject.go index a29a64ebb..43c03348d 100644 --- a/transform/livereloadinject/livereloadinject.go +++ b/transform/livereloadinject/livereloadinject.go @@ -14,10 +14,10 @@ package livereloadinject import ( - "bytes" "fmt" "html" "net/url" + "regexp" "strings" "github.com/gohugoio/hugo/common/loggers" @@ -25,42 +25,27 @@ import ( "github.com/gohugoio/hugo/transform" ) -const warnMessage = `"head" or "body" tag is required in html to append livereload script. ` + - "As a fallback, Hugo injects it somewhere but it might not work properly." - -var warnScript = fmt.Sprintf(``, warnMessage) - -type tag struct { - markup []byte - appendScript bool - warnRequired bool +var ignoredSyntax = regexp.MustCompile(`(?s)^(?:\s+||<\?.*?\?>)*`) +var tagsBeforeHead = []*regexp.Regexp{ + regexp.MustCompile(`(?is)^]*>`), + regexp.MustCompile(`(?is)^]*)?>`), + regexp.MustCompile(`(?is)^]*)?>`), } -var tags = []tag{ - {markup: []byte("")}, - {markup: []byte("")}, - {markup: []byte("`, html.EscapeString(src))) - i := idx - if match.appendScript { - i += bytes.Index(b[i:], []byte(">")) + 1 - } - - if match.warnRequired { - script = append(script, []byte(warnScript)...) - } - - c = append(c[:i], append(script, c[i:]...)...) + c = append(c[:idx], append(script, c[idx:]...)...) if _, err := ft.To().Write(c); err != nil { loggers.Log().Warnf("Failed to inject LiveReload script:", err) diff --git a/transform/livereloadinject/livereloadinject_test.go b/transform/livereloadinject/livereloadinject_test.go index cba2d3834..dc8740208 100644 --- a/transform/livereloadinject/livereloadinject_test.go +++ b/transform/livereloadinject/livereloadinject_test.go @@ -43,32 +43,80 @@ func TestLiveReloadInject(t *testing.T) { return out.String() } - c.Run("Head lower", func(c *qt.C) { - c.Assert(apply("foo"), qt.Equals, ""+expectBase+"foo") + c.Run("Inject after head tag", func(c *qt.C) { + c.Assert(apply("after"), qt.Equals, ""+expectBase+"after") }) - c.Run("Head upper", func(c *qt.C) { - c.Assert(apply("foo"), qt.Equals, ""+expectBase+"foo") + c.Run("Inject after head tag when doctype and html omitted", func(c *qt.C) { + c.Assert(apply("after"), qt.Equals, ""+expectBase+"after") }) - c.Run("Body lower", func(c *qt.C) { - c.Assert(apply("foo"), qt.Equals, "foo"+expectBase+"") + c.Run("Inject after html when head omitted", func(c *qt.C) { + c.Assert(apply("after"), qt.Equals, ""+expectBase+"after") }) - c.Run("Body upper", func(c *qt.C) { - c.Assert(apply("foo"), qt.Equals, "foo"+expectBase+"") + c.Run("Inject after doctype when head and html omitted", func(c *qt.C) { + c.Assert(apply("after"), qt.Equals, ""+expectBase+"after") }) - c.Run("Html upper", func(c *qt.C) { - c.Assert(apply("foo"), qt.Equals, ""+expectBase+warnScript+"foo") + c.Run("Inject before other elements if all else omitted", func(c *qt.C) { + c.Assert(apply("after"), qt.Equals, expectBase+"after") }) - c.Run("Html upper with attr", func(c *qt.C) { - c.Assert(apply(`foo`), qt.Equals, ``+expectBase+warnScript+"foo") + c.Run("Inject before text content if all else omitted", func(c *qt.C) { + c.Assert(apply("after"), qt.Equals, expectBase+"after") }) - c.Run("No match", func(c *qt.C) { - c.Assert(apply("

No match

"), qt.Equals, "

No match

"+expectBase+warnScript) + c.Run("Inject after HeAd tag MiXed CaSe", func(c *qt.C) { + c.Assert(apply("AfTer"), qt.Equals, ""+expectBase+"AfTer") + }) + + c.Run("Inject after HtMl tag MiXed CaSe", func(c *qt.C) { + c.Assert(apply("AfTer"), qt.Equals, ""+expectBase+"AfTer") + }) + + c.Run("Inject after doctype mixed case", func(c *qt.C) { + c.Assert(apply("AfTer"), qt.Equals, ""+expectBase+"AfTer") + }) + + c.Run("Inject after html tag with attributes", func(c *qt.C) { + c.Assert(apply(`after`), qt.Equals, ``+expectBase+"after") + }) + + c.Run("Inject after html tag with newline", func(c *qt.C) { + c.Assert(apply("after"), qt.Equals, ""+expectBase+"after") + }) + + c.Run("Skip comments and whitespace", func(c *qt.C) { + c.Assert( + apply(" \n after"), + qt.Equals, + " \n "+expectBase+"after", + ) + }) + + c.Run("Do not search inside comment", func(c *qt.C) { + c.Assert(apply(""), qt.Equals, ""+expectBase) + }) + + c.Run("Do not search inside scripts", func(c *qt.C) { + c.Assert(apply(""), qt.Equals, ""+expectBase+"") + }) + + c.Run("Do not search inside templates", func(c *qt.C) { + c.Assert(apply(""), qt.Not(qt.Equals), "") + }) + + c.Run("Search from the start of the input", func(c *qt.C) { + c.Assert(apply("after"), qt.Equals, ""+expectBase+"after") + }) + + c.Run("Do not mistake header for head", func(c *qt.C) { + c.Assert(apply("
"), qt.Equals, ""+expectBase+"
") + }) + + c.Run("Do not mistake custom elements for head", func(c *qt.C) { + c.Assert(apply(""), qt.Equals, ""+expectBase+"") }) }