shithub: hugo

Download patch

ref: dd1db334ac15f3bdb67b40973668d1d7b9ca7f08
parent: 96e990456b65c7744819000f67f226c34d487d99
author: Bjørn Erik Pedersen <[email protected]>
date: Sat Feb 6 13:28:26 EST 2016

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.

--- a/transform/livereloadinject.go
+++ b/transform/livereloadinject.go
@@ -15,15 +15,20 @@
 
 import (
 	"bytes"
+	"fmt"
 )
 
 func LiveReloadInject(ct contentTransformer) {
-	match := []byte("</body>")
-	replace := []byte(`<script data-no-instant>document.write('<script src="/livereload.js?mindelay=10"></' + 'script>')</script></body>`)
+	endBodyTag := "</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)
 	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)
 	}
 
--- a/transform/livereloadinject_test.go
+++ b/transform/livereloadinject_test.go
@@ -15,18 +15,24 @@
 
 import (
 	"bytes"
+	"fmt"
 	"github.com/spf13/hugo/helpers"
 	"testing"
 )
 
 func TestLiveReloadInject(t *testing.T) {
+	doTestLiveReloadInject(t, "</body>")
+	doTestLiveReloadInject(t, "</BODY>")
+}
+
+func doTestLiveReloadInject(t *testing.T, bodyEndTag string) {
 	out := new(bytes.Buffer)
-	in := helpers.StringToReader("</body>")
+	in := helpers.StringToReader(bodyEndTag)
 
 	tr := NewChain(LiveReloadInject)
 	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 {
 		t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
 	}