mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-08 16:51:24 +00:00
transform: Add missing test case in livereloadinject
* Test for both </body> and </BODY> * This also cosmetically changes the behaviour, as the case of the end body tag is kept.
This commit is contained in:
parent
96e990456b
commit
dd1db334ac
2 changed files with 16 additions and 5 deletions
|
@ -15,15 +15,20 @@ package transform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LiveReloadInject(ct contentTransformer) {
|
func LiveReloadInject(ct contentTransformer) {
|
||||||
match := []byte("</body>")
|
endBodyTag := "</body>"
|
||||||
replace := []byte(`<script data-no-instant>document.write('<script src="/livereload.js?mindelay=10"></' + 'script>')</script></body>`)
|
match := []byte(endBodyTag)
|
||||||
|
replaceTemplate := `<script data-no-instant>document.write('<script src="/livereload.js?mindelay=10"></' + 'script>')</script>%s`
|
||||||
|
replace := []byte(fmt.Sprintf(replaceTemplate, endBodyTag))
|
||||||
|
|
||||||
newcontent := bytes.Replace(ct.Content(), match, replace, -1)
|
newcontent := bytes.Replace(ct.Content(), match, replace, -1)
|
||||||
if len(newcontent) == len(ct.Content()) {
|
if len(newcontent) == len(ct.Content()) {
|
||||||
match := []byte("</BODY>")
|
endBodyTag = "</BODY>"
|
||||||
|
replace := []byte(fmt.Sprintf(replaceTemplate, endBodyTag))
|
||||||
|
match := []byte(endBodyTag)
|
||||||
newcontent = bytes.Replace(ct.Content(), match, replace, -1)
|
newcontent = bytes.Replace(ct.Content(), match, replace, -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,18 +15,24 @@ package transform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"github.com/spf13/hugo/helpers"
|
"github.com/spf13/hugo/helpers"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLiveReloadInject(t *testing.T) {
|
func TestLiveReloadInject(t *testing.T) {
|
||||||
|
doTestLiveReloadInject(t, "</body>")
|
||||||
|
doTestLiveReloadInject(t, "</BODY>")
|
||||||
|
}
|
||||||
|
|
||||||
|
func doTestLiveReloadInject(t *testing.T, bodyEndTag string) {
|
||||||
out := new(bytes.Buffer)
|
out := new(bytes.Buffer)
|
||||||
in := helpers.StringToReader("</body>")
|
in := helpers.StringToReader(bodyEndTag)
|
||||||
|
|
||||||
tr := NewChain(LiveReloadInject)
|
tr := NewChain(LiveReloadInject)
|
||||||
tr.Apply(out, in, []byte("path"))
|
tr.Apply(out, in, []byte("path"))
|
||||||
|
|
||||||
expected := `<script data-no-instant>document.write('<script src="/livereload.js?mindelay=10"></' + 'script>')</script></body>`
|
expected := fmt.Sprintf(`<script data-no-instant>document.write('<script src="/livereload.js?mindelay=10"></' + 'script>')</script>%s`, bodyEndTag)
|
||||||
if string(out.Bytes()) != expected {
|
if string(out.Bytes()) != expected {
|
||||||
t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
|
t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue