mirror of
https://github.com/gohugoio/hugo.git
synced 2024-05-11 05:54:58 +00:00
transform/livereloadinject: Inject livereload script right after head if possible
We used to insert the livereload script right before the closing body. This dord not work when combined with tools such as Turbolinks. This commit changes it So we try to inject the script as early as possible. Fixes #6821
This commit is contained in:
@@ -15,27 +15,45 @@ package livereloadinject
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
qt "github.com/frankban/quicktest"
|
||||
"github.com/gohugoio/hugo/transform"
|
||||
)
|
||||
|
||||
func TestLiveReloadInject(t *testing.T) {
|
||||
doTestLiveReloadInject(t, "</body>")
|
||||
doTestLiveReloadInject(t, "</BODY>")
|
||||
}
|
||||
c := qt.New(t)
|
||||
|
||||
func doTestLiveReloadInject(t *testing.T, bodyEndTag string) {
|
||||
out := new(bytes.Buffer)
|
||||
in := strings.NewReader(bodyEndTag)
|
||||
expectBase := `<script data-no-instant>document.write('<script src="/livereload.js?port=1313&mindelay=10&v=2"></' + 'script>')</script>`
|
||||
apply := func(s string) string {
|
||||
out := new(bytes.Buffer)
|
||||
in := strings.NewReader(s)
|
||||
|
||||
tr := transform.New(New(1313))
|
||||
tr.Apply(out, in)
|
||||
tr := transform.New(New(1313))
|
||||
tr.Apply(out, in)
|
||||
|
||||
expected := fmt.Sprintf(`<script data-no-instant>document.write('<script src="/livereload.js?port=1313&mindelay=10&v=2"></' + 'script>')</script>%s`, bodyEndTag)
|
||||
if out.String() != expected {
|
||||
t.Errorf("Expected %s got %s", expected, out.String())
|
||||
return out.String()
|
||||
}
|
||||
|
||||
c.Run("Head lower", func(c *qt.C) {
|
||||
c.Assert(apply("<html><head>foo"), qt.Equals, "<html><head>"+expectBase+"foo")
|
||||
})
|
||||
|
||||
c.Run("Head upper", func(c *qt.C) {
|
||||
c.Assert(apply("<html><HEAD>foo"), qt.Equals, "<html><HEAD>"+expectBase+"foo")
|
||||
})
|
||||
|
||||
c.Run("Body lower", func(c *qt.C) {
|
||||
c.Assert(apply("foo</body>"), qt.Equals, "foo"+expectBase+"</body>")
|
||||
})
|
||||
|
||||
c.Run("Body upper", func(c *qt.C) {
|
||||
c.Assert(apply("foo</BODY>"), qt.Equals, "foo"+expectBase+"</BODY>")
|
||||
})
|
||||
|
||||
c.Run("No match", func(c *qt.C) {
|
||||
c.Assert(apply("<h1>No match</h1>"), qt.Equals, "<h1>No match</h1>")
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user