2015-12-10 22:19:38 +00:00
|
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2013-10-01 21:26:21 +00:00
|
|
|
|
package transform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2015-03-17 23:36:48 +00:00
|
|
|
|
"github.com/spf13/hugo/helpers"
|
2016-02-06 17:17:48 +00:00
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-15 22:11:39 +00:00
|
|
|
|
"path/filepath"
|
2015-02-12 11:17:59 +00:00
|
|
|
|
"strings"
|
2013-10-01 21:26:21 +00:00
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-23 20:55:14 +00:00
|
|
|
|
const (
|
|
|
|
|
h5JsContentDoubleQuote = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"/foobar\">Follow up</a></article></body></html>"
|
|
|
|
|
h5JsContentSingleQuote = "<!DOCTYPE html><html><head><script src='foobar.js'></script><script src='/barfoo.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='foobar'>foobar</a>. <a href='/foobar'>Follow up</a></article></body></html>"
|
|
|
|
|
h5JsContentAbsURL = "<!DOCTYPE html><html><head><script src=\"http://user@host:10234/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>"
|
|
|
|
|
h5JsContentAbsURLSchemaless = "<!DOCTYPE html><html><head><script src=\"//host/foobar.js\"></script><script src='//host2/barfoo.js'></head><body><nav><h1>title</h1></nav><article>content <a href=\"//host/foobar\">foobar</a>. <a href='//host2/foobar'>Follow up</a></article></body></html>"
|
|
|
|
|
corectOutputSrcHrefDq = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"http://base/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"http://base/foobar\">Follow up</a></article></body></html>"
|
|
|
|
|
corectOutputSrcHrefSq = "<!DOCTYPE html><html><head><script src='foobar.js'></script><script src='http://base/barfoo.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='foobar'>foobar</a>. <a href='http://base/foobar'>Follow up</a></article></body></html>"
|
|
|
|
|
|
|
|
|
|
h5XMLXontentAbsURL = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="/foobar">foobar</a></p> <p>A video: <iframe src='/foo'></iframe></p></content></entry></feed>"
|
|
|
|
|
correctOutputSrcHrefInXML = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="http://base/foobar">foobar</a></p> <p>A video: <iframe src='http://base/foo'></iframe></p></content></entry></feed>"
|
|
|
|
|
h5XMLContentGuarded = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="//foobar">foobar</a></p> <p>A video: <iframe src='//foo'></iframe></p></content></entry></feed>"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// additional sanity tests for replacements testing
|
|
|
|
|
replace1 = "No replacements."
|
|
|
|
|
replace2 = "ᚠᛇᚻ ᛒᛦᚦ ᚠᚱᚩᚠᚢᚱ\nᚠᛁᚱᚪ ᚷᛖᚻᚹᛦᛚᚳᚢᛗ"
|
|
|
|
|
replace3 = `End of file: src="/`
|
|
|
|
|
replace4 = `End of file: srcset="/`
|
|
|
|
|
replace5 = `Srcsett with no closing quote: srcset="/img/small.jpg do be do be do.`
|
|
|
|
|
|
|
|
|
|
// Issue: 816, schemaless links combined with others
|
|
|
|
|
replaceSchemalessHTML = `Pre. src='//schemaless' src='/normal' <a href="//schemaless">Schemaless</a>. <a href="/normal">normal</a>. Post.`
|
|
|
|
|
replaceSchemalessHTMLCorrect = `Pre. src='//schemaless' src='http://base/normal' <a href="//schemaless">Schemaless</a>. <a href="http://base/normal">normal</a>. Post.`
|
|
|
|
|
replaceSchemalessXML = `Pre. src='//schemaless' src='/normal' <a href='//schemaless'>Schemaless</a>. <a href='/normal'>normal</a>. Post.`
|
|
|
|
|
replaceSchemalessXMLCorrect = `Pre. src='//schemaless' src='http://base/normal' <a href='//schemaless'>Schemaless</a>. <a href='http://base/normal'>normal</a>. Post.`
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// srcset=
|
|
|
|
|
srcsetBasic = `Pre. <img srcset="/img/small.jpg 200w, /img/medium.jpg 300w, /img/big.jpg 700w" alt="text" src="/img/foo.jpg">`
|
|
|
|
|
srcsetBasicCorrect = `Pre. <img srcset="http://base/img/small.jpg 200w, http://base/img/medium.jpg 300w, http://base/img/big.jpg 700w" alt="text" src="http://base/img/foo.jpg">`
|
|
|
|
|
srcsetSingleQuote = `Pre. <img srcset='/img/small.jpg 200w, /img/big.jpg 700w' alt="text" src="/img/foo.jpg"> POST.`
|
|
|
|
|
srcsetSingleQuoteCorrect = `Pre. <img srcset='http://base/img/small.jpg 200w, http://base/img/big.jpg 700w' alt="text" src="http://base/img/foo.jpg"> POST.`
|
|
|
|
|
srcsetXMLBasic = `Pre. <img srcset="/img/small.jpg 200w, /img/big.jpg 700w" alt="text" src="/img/foo.jpg">`
|
|
|
|
|
srcsetXMLBasicCorrect = `Pre. <img srcset="http://base/img/small.jpg 200w, http://base/img/big.jpg 700w" alt="text" src="http://base/img/foo.jpg">`
|
|
|
|
|
srcsetXMLSingleQuote = `Pre. <img srcset="/img/small.jpg 200w, /img/big.jpg 700w" alt="text" src="/img/foo.jpg">`
|
|
|
|
|
srcsetXMLSingleQuoteCorrect = `Pre. <img srcset="http://base/img/small.jpg 200w, http://base/img/big.jpg 700w" alt="text" src="http://base/img/foo.jpg">`
|
|
|
|
|
srcsetVariations = `Pre.
|
2015-05-03 18:09:36 +00:00
|
|
|
|
Missing start quote: <img srcset=/img/small.jpg 200w, /img/big.jpg 700w" alt="text"> src='/img/foo.jpg'> FOO.
|
2015-05-03 17:54:17 +00:00
|
|
|
|
<img srcset='/img.jpg'>
|
|
|
|
|
schemaless: <img srcset='//img.jpg' src='//basic.jpg'>
|
|
|
|
|
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST
|
|
|
|
|
`
|
2016-03-23 20:55:14 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
srcsetVariationsCorrect = `Pre.
|
2015-05-03 18:09:36 +00:00
|
|
|
|
Missing start quote: <img srcset=/img/small.jpg 200w, /img/big.jpg 700w" alt="text"> src='http://base/img/foo.jpg'> FOO.
|
2015-05-03 17:54:17 +00:00
|
|
|
|
<img srcset='http://base/img.jpg'>
|
|
|
|
|
schemaless: <img srcset='//img.jpg' src='//basic.jpg'>
|
|
|
|
|
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST
|
|
|
|
|
`
|
2016-03-23 20:55:14 +00:00
|
|
|
|
srcsetXMLVariations = `Pre.
|
2015-05-03 17:54:17 +00:00
|
|
|
|
Missing start quote: <img srcset=/img/small.jpg 200w /img/big.jpg 700w" alt="text"> src='/img/foo.jpg'> FOO.
|
|
|
|
|
<img srcset='/img.jpg'>
|
|
|
|
|
schemaless: <img srcset='//img.jpg' src='//basic.jpg'>
|
|
|
|
|
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST
|
|
|
|
|
`
|
2016-03-23 20:55:14 +00:00
|
|
|
|
srcsetXMLVariationsCorrect = `Pre.
|
2015-05-03 17:54:17 +00:00
|
|
|
|
Missing start quote: <img srcset=/img/small.jpg 200w /img/big.jpg 700w" alt="text"> src='http://base/img/foo.jpg'> FOO.
|
|
|
|
|
<img srcset='http://base/img.jpg'>
|
|
|
|
|
schemaless: <img srcset='//img.jpg' src='//basic.jpg'>
|
|
|
|
|
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST
|
|
|
|
|
`
|
2015-02-17 03:33:44 +00:00
|
|
|
|
|
2016-03-23 20:55:14 +00:00
|
|
|
|
relPathVariations = `PRE. a href="/img/small.jpg" POST.`
|
|
|
|
|
relPathVariationsCorrect = `PRE. a href="../../img/small.jpg" POST.`
|
2015-05-15 22:11:39 +00:00
|
|
|
|
|
2016-03-23 20:55:14 +00:00
|
|
|
|
testBaseURL = "http://base/"
|
|
|
|
|
)
|
2015-05-15 22:11:39 +00:00
|
|
|
|
|
2016-03-23 20:55:14 +00:00
|
|
|
|
var (
|
|
|
|
|
absURLlBenchTests = []test{
|
|
|
|
|
{h5JsContentDoubleQuote, corectOutputSrcHrefDq},
|
|
|
|
|
{h5JsContentSingleQuote, corectOutputSrcHrefSq},
|
|
|
|
|
{h5JsContentAbsURL, h5JsContentAbsURL},
|
|
|
|
|
{h5JsContentAbsURLSchemaless, h5JsContentAbsURLSchemaless},
|
|
|
|
|
}
|
2013-10-01 21:26:21 +00:00
|
|
|
|
|
2016-03-23 20:55:14 +00:00
|
|
|
|
xmlAbsURLBenchTests = []test{
|
|
|
|
|
{h5XMLXontentAbsURL, correctOutputSrcHrefInXML},
|
|
|
|
|
{h5XMLContentGuarded, h5XMLContentGuarded},
|
|
|
|
|
}
|
2014-12-18 19:59:39 +00:00
|
|
|
|
|
2016-03-23 20:55:14 +00:00
|
|
|
|
sanityTests = []test{{replace1, replace1}, {replace2, replace2}, {replace3, replace3}, {replace3, replace3}, {replace5, replace5}}
|
|
|
|
|
extraTestsHTML = []test{{replaceSchemalessHTML, replaceSchemalessHTMLCorrect}}
|
|
|
|
|
absURLTests = append(absURLlBenchTests, append(sanityTests, extraTestsHTML...)...)
|
|
|
|
|
extraTestsXML = []test{{replaceSchemalessXML, replaceSchemalessXMLCorrect}}
|
|
|
|
|
xmlAbsURLTests = append(xmlAbsURLBenchTests, append(sanityTests, extraTestsXML...)...)
|
|
|
|
|
srcsetTests = []test{{srcsetBasic, srcsetBasicCorrect}, {srcsetSingleQuote, srcsetSingleQuoteCorrect}, {srcsetVariations, srcsetVariationsCorrect}}
|
|
|
|
|
srcsetXMLTests = []test{
|
|
|
|
|
{srcsetXMLBasic, srcsetXMLBasicCorrect},
|
|
|
|
|
{srcsetXMLSingleQuote, srcsetXMLSingleQuoteCorrect},
|
|
|
|
|
{srcsetXMLVariations, srcsetXMLVariationsCorrect}}
|
|
|
|
|
|
|
|
|
|
relurlTests = []test{{relPathVariations, relPathVariationsCorrect}}
|
|
|
|
|
)
|
2015-05-15 22:11:39 +00:00
|
|
|
|
|
2013-11-05 22:28:06 +00:00
|
|
|
|
func TestChainZeroTransformers(t *testing.T) {
|
|
|
|
|
tr := NewChain()
|
|
|
|
|
in := new(bytes.Buffer)
|
|
|
|
|
out := new(bytes.Buffer)
|
2015-05-15 22:11:39 +00:00
|
|
|
|
if err := tr.Apply(in, out, []byte("")); err != nil {
|
2013-11-05 22:28:06 +00:00
|
|
|
|
t.Errorf("A zero transformer chain returned an error.")
|
|
|
|
|
}
|
2013-10-30 03:24:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-17 23:36:48 +00:00
|
|
|
|
func TestChaingMultipleTransformers(t *testing.T) {
|
2015-03-19 00:55:49 +00:00
|
|
|
|
f1 := func(ct contentTransformer) {
|
|
|
|
|
ct.Write(bytes.Replace(ct.Content(), []byte("f1"), []byte("f1r"), -1))
|
2015-03-17 23:36:48 +00:00
|
|
|
|
}
|
2015-03-19 00:55:49 +00:00
|
|
|
|
f2 := func(ct contentTransformer) {
|
|
|
|
|
ct.Write(bytes.Replace(ct.Content(), []byte("f2"), []byte("f2r"), -1))
|
2015-03-17 23:36:48 +00:00
|
|
|
|
}
|
2015-03-19 00:55:49 +00:00
|
|
|
|
f3 := func(ct contentTransformer) {
|
|
|
|
|
ct.Write(bytes.Replace(ct.Content(), []byte("f3"), []byte("f3r"), -1))
|
2015-03-17 23:36:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-19 00:55:49 +00:00
|
|
|
|
f4 := func(ct contentTransformer) {
|
|
|
|
|
ct.Write(bytes.Replace(ct.Content(), []byte("f4"), []byte("f4r"), -1))
|
2015-03-17 23:36:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tr := NewChain(f1, f2, f3, f4)
|
|
|
|
|
|
|
|
|
|
out := new(bytes.Buffer)
|
2015-05-15 22:11:39 +00:00
|
|
|
|
if err := tr.Apply(out, helpers.StringToReader("Test: f4 f3 f1 f2 f1 The End."), []byte("")); err != nil {
|
2015-03-17 23:36:48 +00:00
|
|
|
|
t.Errorf("Multi transformer chain returned an error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected := "Test: f4r f3r f1r f2r f1r The End."
|
|
|
|
|
|
|
|
|
|
if string(out.Bytes()) != expected {
|
|
|
|
|
t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
|
func BenchmarkAbsURL(b *testing.B) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
tr := NewChain(AbsURL)
|
2013-11-01 05:14:11 +00:00
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2013-10-30 03:24:29 +00:00
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-03-23 19:03:13 +00:00
|
|
|
|
apply(b.Errorf, tr, absURLlBenchTests)
|
2015-02-12 11:17:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-03 18:57:47 +00:00
|
|
|
|
func BenchmarkAbsURLSrcset(b *testing.B) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
tr := NewChain(AbsURL)
|
2015-05-03 18:57:47 +00:00
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-03-23 19:03:13 +00:00
|
|
|
|
apply(b.Errorf, tr, srcsetTests)
|
2015-05-03 18:57:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkXMLAbsURLSrcset(b *testing.B) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
tr := NewChain(AbsURLInXML)
|
2015-05-03 18:57:47 +00:00
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-03-23 19:03:13 +00:00
|
|
|
|
apply(b.Errorf, tr, srcsetXMLTests)
|
2015-05-03 18:57:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
|
func TestAbsURL(t *testing.T) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
tr := NewChain(AbsURL)
|
2015-02-12 11:17:59 +00:00
|
|
|
|
|
2016-03-23 19:03:13 +00:00
|
|
|
|
apply(t.Errorf, tr, absURLTests)
|
2015-02-12 11:17:59 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-15 22:11:39 +00:00
|
|
|
|
func TestRelativeURL(t *testing.T) {
|
|
|
|
|
tr := NewChain(AbsURL)
|
|
|
|
|
|
2016-03-23 19:03:13 +00:00
|
|
|
|
applyWithPath(t.Errorf, tr, relurlTests, helpers.GetDottedRelativePath(filepath.FromSlash("/post/sub/")))
|
2015-05-15 22:11:39 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-03 17:54:17 +00:00
|
|
|
|
func TestAbsURLSrcSet(t *testing.T) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
tr := NewChain(AbsURL)
|
2015-05-03 17:54:17 +00:00
|
|
|
|
|
2016-03-23 19:03:13 +00:00
|
|
|
|
apply(t.Errorf, tr, srcsetTests)
|
2015-05-03 17:54:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAbsXMLURLSrcSet(t *testing.T) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
tr := NewChain(AbsURLInXML)
|
2015-05-03 17:54:17 +00:00
|
|
|
|
|
2016-03-23 19:03:13 +00:00
|
|
|
|
apply(t.Errorf, tr, srcsetXMLTests)
|
2015-05-03 17:54:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
|
func BenchmarkXMLAbsURL(b *testing.B) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
tr := NewChain(AbsURLInXML)
|
2015-02-12 11:17:59 +00:00
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-03-23 19:03:13 +00:00
|
|
|
|
apply(b.Errorf, tr, xmlAbsURLBenchTests)
|
2013-10-30 03:24:29 +00:00
|
|
|
|
}
|
2013-10-01 21:26:21 +00:00
|
|
|
|
}
|
2014-12-18 19:59:39 +00:00
|
|
|
|
|
2015-03-11 17:34:57 +00:00
|
|
|
|
func TestXMLAbsURL(t *testing.T) {
|
2015-05-15 22:11:39 +00:00
|
|
|
|
tr := NewChain(AbsURLInXML)
|
2016-03-23 19:03:13 +00:00
|
|
|
|
apply(t.Errorf, tr, xmlAbsURLTests)
|
2014-12-18 19:59:39 +00:00
|
|
|
|
}
|
2015-02-12 11:17:59 +00:00
|
|
|
|
|
2016-02-06 17:17:48 +00:00
|
|
|
|
func TestNewEmptyTransforms(t *testing.T) {
|
|
|
|
|
transforms := NewEmptyTransforms()
|
|
|
|
|
assert.Equal(t, 20, cap(transforms))
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 11:17:59 +00:00
|
|
|
|
type errorf func(string, ...interface{})
|
|
|
|
|
|
2015-05-15 22:11:39 +00:00
|
|
|
|
func applyWithPath(ef errorf, tr chain, tests []test, path string) {
|
2015-02-12 11:17:59 +00:00
|
|
|
|
for _, test := range tests {
|
|
|
|
|
out := new(bytes.Buffer)
|
2015-05-15 22:11:39 +00:00
|
|
|
|
var err error
|
|
|
|
|
err = tr.Apply(out, strings.NewReader(test.content), []byte(path))
|
2015-02-12 11:17:59 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
ef("Unexpected error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
if test.expected != string(out.Bytes()) {
|
|
|
|
|
ef("Expected:\n%s\nGot:\n%s", test.expected, string(out.Bytes()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-15 22:11:39 +00:00
|
|
|
|
func apply(ef errorf, tr chain, tests []test) {
|
|
|
|
|
applyWithPath(ef, tr, tests, testBaseURL)
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 11:17:59 +00:00
|
|
|
|
type test struct {
|
|
|
|
|
content string
|
|
|
|
|
expected string
|
|
|
|
|
}
|