Skip to content

Commit

Permalink
🐛[no-signing] Transfer attributes on body element (#33311)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcordry committed Mar 17, 2021
1 parent f6c1dde commit e9373cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/utils/dom-tranform-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@ export class DomTransformStream {
this.shouldTransfer_ = true;
this.targetBodyResolver_(targetBody);

this.headPromise_.then(() => {
const attrs = this.detachedBody_.attributes;
for (let i = 0; i < attrs.length; i++) {
const {name, value} = attrs[i];
targetBody.setAttribute(name, value);
}
});

this.transferBodyChunk_();

return this.bodyTransferPromise_;
Expand Down
22 changes: 22 additions & 0 deletions test/unit/utils/test-dom-transform-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ describes.fakeWin('DomTransformStream', {amp: true}, (env) => {
expect(body.querySelector('child-two')).to.exist;
});

it('should transfer <body> attributes to target body element', async () => {
const {body} = win.document;
detachedDoc.write(`
<!doctype html>
<html ⚡>
<head>
<script async src="https://cdn.ampproject.org/v0.js"></script>
</head>
<body marginwidth="0" marginheight="0" class="amp-cats" style="opacity: 1;">
<child-one></child-one>
<child-two></child-two>
`);
transformer.onChunk(detachedDoc);
transformer.transferBody(body /* targetBody */);
await flush();

expect(body.getAttribute('marginwidth')).to.equal('0');
expect(body.getAttribute('marginheight')).to.equal('0');
expect(body.getAttribute('style')).to.equal('opacity: 1;');
expect(body).to.have.class('amp-cats');
});

it('should keep transferring new chunks after call', async () => {
const {body} = win.document;
detachedDoc.write(`
Expand Down

0 comments on commit e9373cc

Please sign in to comment.