// Copyright 2018 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. package urlreplacers import ( "path/filepath" "testing" bp "github.com/gohugoio/hugo/bufferpool" "github.com/gohugoio/hugo/helpers" "github.com/gohugoio/hugo/transform" ) const ( h5JsContentDoubleQuote = "
content foobar. Follow up
" h5JsContentSingleQuote = "
content foobar. Follow up
" h5JsContentAbsURL = "
content foobar. Follow up
" h5JsContentAbsURLSchemaless = "
content foobar. Follow up
" correctOutputSrcHrefSq = "
content foobar. Follow up
" h5XMLContentAbsURL = "<p><a href="/foobar">foobar</a></p> <p>A video: <iframe src='/foo'></iframe></p>" correctOutputSrcHrefInXML = "<p><a href="http://base/foobar">foobar</a></p> <p>A video: <iframe src='http://base/foo'></iframe></p>" h5XMLContentGuarded = "<p><a href="//foobar">foobar</a></p> <p>A video: <iframe src='//foo'></iframe></p>" ) const ( // additional sanity tests for replacements testing replace1 = "No replacements." replace2 = "ᚠᛇᚻ ᛒᛦᚦ ᚠᚱᚩᚠᚢᚱ\nᚠᛁᚱᚪ ᚷᛖᚻᚹᛦᛚᚳᚢᛗ" replace3 = `End of file: src="/` 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' Schemaless. normal. Post.` replaceSchemalessHTMLCorrect = `Pre. src='//schemaless' src='http://base/normal' Schemaless. normal. Post.` replaceSchemalessXML = `Pre. src='//schemaless' src='/normal' Schemaless. normal. Post.` replaceSchemalessXMLCorrect = `Pre. src='//schemaless' src='http://base/normal' Schemaless. normal. Post.` ) const ( // srcset= srcsetBasic = `Pre. text` srcsetBasicCorrect = `Pre. text` srcsetSingleQuote = `Pre. text POST.` srcsetSingleQuoteCorrect = `Pre. text POST.` srcsetXMLBasic = `Pre. "text"` srcsetXMLBasicCorrect = `Pre. "text"` srcsetXMLSingleQuote = `Pre. "text"` srcsetXMLSingleQuoteCorrect = `Pre. "text"` srcsetVariations = `Pre. Missing start quote: text src='/img/foo.jpg'> FOO. schemaless: schemaless2: text src='http://base/img/foo.jpg'> FOO. schemaless: schemaless2: ASDF`, expected: `Link: ASDF`, }, { content: `Link: ASDF`, expected: `Link: ASDF`, }, }) } func TestRelativeURL(t *testing.T) { tr := transform.New(NewAbsURLTransformer(helpers.GetDottedRelativePath(filepath.FromSlash("/post/sub/")))) applyWithPath(t.Errorf, tr, relurlTests) } func TestAbsURLSrcSet(t *testing.T) { tr := transform.New(NewAbsURLTransformer(testBaseURL)) apply(t.Errorf, tr, srcsetTests) } func TestAbsXMLURLSrcSet(t *testing.T) { tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) apply(t.Errorf, tr, srcsetXMLTests) } func BenchmarkXMLAbsURL(b *testing.B) { tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) b.ResetTimer() for i := 0; i < b.N; i++ { apply(b.Errorf, tr, xmlAbsURLBenchTests) } } func TestXMLAbsURL(t *testing.T) { tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) apply(t.Errorf, tr, xmlAbsURLTests) } func apply(ef errorf, tr transform.Chain, tests []test) { applyWithPath(ef, tr, tests) } func applyWithPath(ef errorf, tr transform.Chain, tests []test) { out := bp.GetBuffer() defer bp.PutBuffer(out) in := bp.GetBuffer() defer bp.PutBuffer(in) for _, test := range tests { var err error in.WriteString(test.content) err = tr.Apply(out, in) if err != nil { ef("Unexpected error: %s", err) } if test.expected != out.String() { ef("Expected:\n%s\nGot:\n%s", test.expected, out.String()) } out.Reset() in.Reset() } } type test struct { content string expected string } type errorf func(string, ...any)