Skip to content

Commit

Permalink
Copy optional crossorigin attribute to <link rel=preload>
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 363521960
  • Loading branch information
Googler authored and twifkak committed Mar 18, 2021
1 parent bae8246 commit 684426c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ func extractPreloads(dom *amphtml.DOM) []*rpb.Metadata_Preload {
switch current.DataAtom {
case atom.Script:
if src, ok := htmlnode.GetAttributeVal(current, "", "src"); ok {
preloads = append(preloads, &rpb.Metadata_Preload{Url: src, As: "script"})
preload := &rpb.Metadata_Preload{Url: src, As: "script"}
if crossorigin, ok := htmlnode.GetAttributeVal(current, "", "crossorigin"); ok && crossorigin != "" {
preload.Attributes = append(preload.Attributes, &rpb.Metadata_Preload_Attribute{Key: "crossorigin", Val: crossorigin})
}
preloads = append(preloads, preload)
}
case atom.Link:
if rel, ok := htmlnode.GetAttributeVal(current, "", "rel"); ok {
Expand Down
5 changes: 5 additions & 0 deletions transformer/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func TestPreloads(t *testing.T) {
"<html ⚡><head><script></script></head><body></body></html>",
[]*rpb.Metadata_Preload{},
},
{
"<html ⚡><script src=foo crossorigin=anonymous>",
"<html ⚡><head><script crossorigin=anonymous src=foo></script></head><body></body></html>",
[]*rpb.Metadata_Preload{{Url: "foo", As: "script", Attributes: []*rpb.Metadata_Preload_Attribute{{Key: "crossorigin", Val: "anonymous"}}}},
},
{
"<html ⚡><script src=foo>",
"<html ⚡><head><script src=foo></script></head><body></body></html>",
Expand Down

0 comments on commit 684426c

Please sign in to comment.