From bc7ac096cb01c18c680a661543f32582b4e7bcea Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 6 Aug 2020 19:21:56 -0700 Subject: [PATCH] Add support for link imagesrcset and imagesizes Adds validator support for the two new `` attributes, and adds srcset URL rewriting for imagesrcset. Fixes https://github.com/ampproject/amphtml/issues/29302 PiperOrigin-RevId: 325359140 --- transformer/transformers/absoluteurl.go | 35 ++++++++++-------- transformer/transformers/absoluteurl_test.go | 8 ++++ transformer/transformers/urlrewrite.go | 39 +++++++++++++++----- transformer/transformers/urlrewrite_test.go | 36 ++++++++++++++++++ 4 files changed, 93 insertions(+), 25 deletions(-) diff --git a/transformer/transformers/absoluteurl.go b/transformer/transformers/absoluteurl.go index 62e3c06ed..50a01fed8 100644 --- a/transformer/transformers/absoluteurl.go +++ b/transformer/transformers/absoluteurl.go @@ -62,6 +62,8 @@ var /* const */ imgTagAttrs = []string{"longdesc"} // * action-xhr // * Any tag with attribute: // * longdesc +// * Any tag with attribute: +// * imagesrcset // // URLs in stylesheets and srcsets are handled by the ExternalUrlRewrite // transformer. @@ -146,8 +148,13 @@ func AbsoluteURL(e *Context) error { amphtml.ToAbsoluteURL(documentURL, e.BaseURL, href.Val)) } } - if _, ok := htmlnode.FindAttribute(n, "", "srcset"); ok { - rewriteSrcsetURLs(n, documentURL, e.BaseURL) + if srcset, ok := htmlnode.FindAttribute(n, "", "srcset"); ok { + htmlnode.SetAttribute(n, "", "srcset", rewriteSrcsetURLs(documentURL, e.BaseURL, srcset.Val)) + } + if n.DataAtom == atom.Link { + if srcset, ok := htmlnode.FindAttribute(n, "", "imagesrcset"); ok { + htmlnode.SetAttribute(n, "", "imagesrcset", rewriteSrcsetURLs(documentURL, e.BaseURL, srcset.Val)) + } } } return nil @@ -165,18 +172,16 @@ func rewriteAbsoluteURLs(n *html.Node, documentURL string, baseURL *url.URL, } } -func rewriteSrcsetURLs(n *html.Node, documentURL string, baseURL *url.URL) { - if v, ok := htmlnode.GetAttributeVal(n, "", "srcset"); ok { - normalized, offsets := amphtml.ParseSrcset(v) - var sb strings.Builder - var pos int - for _, element := range offsets { - sb.WriteString(normalized[pos:element.Start]) - sb.WriteString(amphtml.ToAbsoluteURL( - documentURL, baseURL, normalized[element.Start:element.End])) - pos = element.End - } - sb.WriteString(normalized[pos:]) - htmlnode.SetAttribute(n, "", "srcset", sb.String()) +func rewriteSrcsetURLs(documentURL string, baseURL *url.URL, srcset string) string { + normalized, offsets := amphtml.ParseSrcset(srcset) + var sb strings.Builder + var pos int + for _, element := range offsets { + sb.WriteString(normalized[pos:element.Start]) + sb.WriteString(amphtml.ToAbsoluteURL( + documentURL, baseURL, normalized[element.Start:element.End])) + pos = element.End } + sb.WriteString(normalized[pos:]) + return sb.String() } diff --git a/transformer/transformers/absoluteurl_test.go b/transformer/transformers/absoluteurl_test.go index 58fee6caf..60cfd357d 100644 --- a/transformer/transformers/absoluteurl_test.go +++ b/transformer/transformers/absoluteurl_test.go @@ -173,6 +173,14 @@ func TestAbsoluteURLTansformer(t *testing.T) { baseURL: "http://www.example.com", documentURL: fooURL, }, + { + desc: "imagesrcset rewritten", + input: "", + expected: "", + baseURL: "https://example.com/", + documentURL: barURL, + }, } for _, tc := range tcs { rawInput := tt.Concat( diff --git a/transformer/transformers/urlrewrite.go b/transformer/transformers/urlrewrite.go index 51be1160b..cb996e38f 100644 --- a/transformer/transformers/urlrewrite.go +++ b/transformer/transformers/urlrewrite.go @@ -64,6 +64,8 @@ type nodeMap map[string]*html.Node // * // * / which are SVG-specific images. // * +// * +// * // * // * // * a background image given in the