@@ -26,7 +26,7 @@ import (
26
26
"github.com/kylelemons/godebug/diff"
27
27
)
28
28
29
- func transformAndOutput (input string ) (string , error ) {
29
+ func transformAndOutput (input string , version int64 ) (string , error ) {
30
30
inputDoc , err := html .Parse (strings .NewReader (input ))
31
31
if err != nil {
32
32
return "" , err
@@ -42,6 +42,7 @@ func transformAndOutput(input string) (string, error) {
42
42
DOM : inputDOM ,
43
43
BaseURL : baseURL ,
44
44
DocumentURL : documentURL ,
45
+ Version : version ,
45
46
}
46
47
transformers .PreloadImage (context )
47
48
var output strings.Builder
@@ -320,10 +321,32 @@ var testcaseDataHero = []struct {
320
321
},
321
322
}
322
323
324
+ var testLazyLoadImg = []struct {
325
+ testcaseName string
326
+ input string
327
+ expected string
328
+ }{
329
+ {
330
+ "data-hero leftover" ,
331
+ `<html><head></head><body><amp-img data-hero width="500" height="400" src="https://example.com/foo.png"></amp-img><amp-img width="500" height="400" src="https://example.com/bar.png"></amp-img></body></html>` ,
332
+ `<html><head><link rel="preload" as="image" href="https://example.com/foo.png"/></head><body><amp-img data-hero="" width="500" height="400" src="https://example.com/foo.png" i-amphtml-ssr=""><img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://example.com/foo.png"/></amp-img><amp-img width="500" height="400" src="https://example.com/bar.png" i-amphtml-ssr=""><img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://example.com/bar.png" loading="lazy"/></amp-img></body></html>` ,
333
+ },
334
+ {
335
+ "inferred-size leftover" ,
336
+ `<html><head></head><body><amp-img width="500" height="400" src="https://example.com/foo.png"></amp-img><amp-img width="100" height="100" src="https://example.com/bar.png"></amp-img></body></html>` ,
337
+ `<html><head><link rel="preload" as="image" href="https://example.com/foo.png"/></head><body><amp-img width="500" height="400" src="https://example.com/foo.png" i-amphtml-ssr=""><img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://example.com/foo.png"/></amp-img><amp-img width="100" height="100" src="https://example.com/bar.png" i-amphtml-ssr=""><img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://example.com/bar.png" loading="lazy"/></amp-img></body></html>` ,
338
+ },
339
+ {
340
+ "no transformed images" ,
341
+ `<html><head></head><body><amp-img width="100" height="100" src="https://example.com/foo.png"></amp-img></body></html>` ,
342
+ `<html><head></head><body><amp-img width="100" height="100" src="https://example.com/foo.png" i-amphtml-ssr=""><img class="i-amphtml-fill-content i-amphtml-replaced-content" decoding="async" src="https://example.com/foo.png" loading="lazy"/></amp-img></body></html>` ,
343
+ },
344
+ }
345
+
323
346
func TestInferSizeCases (t * testing.T ) {
324
347
for _ , tt := range testcaseInferSize {
325
348
t .Run (tt .testcaseName , func (t * testing.T ) {
326
- output , err := transformAndOutput (strings .TrimSpace (tt .input ))
349
+ output , err := transformAndOutput (strings .TrimSpace (tt .input ), 0 )
327
350
if err != nil {
328
351
t .Fatalf ("Unexpected error %q" , err )
329
352
}
@@ -337,7 +360,21 @@ func TestInferSizeCases(t *testing.T) {
337
360
func TestDataHeroCases (t * testing.T ) {
338
361
for _ , tt := range testcaseDataHero {
339
362
t .Run (tt .testcaseName , func (t * testing.T ) {
340
- output , err := transformAndOutput (strings .TrimSpace (tt .input ))
363
+ output , err := transformAndOutput (strings .TrimSpace (tt .input ), 0 )
364
+ if err != nil {
365
+ t .Fatalf ("Unexpected error %q" , err )
366
+ }
367
+ if diff := diff .Diff (strings .TrimSpace (tt .expected ), output ); diff != "" {
368
+ t .Errorf ("PreloadImage transformer produced unexpected output:\n %s" , diff )
369
+ }
370
+ })
371
+ }
372
+ }
373
+
374
+ func TestLazyLoadCases (t * testing.T ) {
375
+ for _ , tt := range testLazyLoadImg {
376
+ t .Run (tt .testcaseName , func (t * testing.T ) {
377
+ output , err := transformAndOutput (strings .TrimSpace (tt .input ), 5 )
341
378
if err != nil {
342
379
t .Fatalf ("Unexpected error %q" , err )
343
380
}
0 commit comments