Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Serialize iframe document URLs #405

Merged
merged 3 commits into from
Jul 7, 2021
Merged

Conversation

wwilsman
Copy link
Contributor

@wwilsman wwilsman commented Jul 6, 2021

What is this?

As per #396, when a same-origin frame is serialized, all of its content becomes relative to the frame parent, which can then result in invalid resource URLs. Like the issue suggests, we can transform the URLs within the frame document to be fully qualified URLs based on the frame document's URL.

During the recursive DOM serialization, we can provide a custom transform function to be applied to iframe documents after cloning but before the function returns a string. In this transformation we can then find all relevant elements to transform their respective URLs.

According to HTML 4/5 spec the href, src, srcset, poster, and background attributes are attributes that may contain URIs (and that we care about transforming). Most attributes' property counterparts are already resolved, however that is not always the case (such as background). The srcset property is also not resolved, and requires parsing to properly replace URLs within the list of image candidates.

In addition to those attributes, CSS within style elements or inline style attributes may also contain URLs using the url() CSS function and therefore are also transformed.

In adding a test, Karma complained about assets being missing. I found a workaround by setting a proxy option to a semi-valid URL so it doesn't proxy and doesn't log. If logs crop up in the future, we probably want to host a single fake asset to silence them.

Fixes #396

@wwilsman wwilsman added the 🐛 bug Something isn't working label Jul 6, 2021
@wwilsman wwilsman requested a review from Robdel12 July 6, 2021 22:19
@wwilsman wwilsman enabled auto-merge (squash) July 7, 2021 00:42
Copy link
Contributor

@Robdel12 Robdel12 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏁

@@ -22,6 +72,7 @@ export default function serializeFrames(dom, clone, { enableJavaScript }) {

// recersively serialize contents
let serialized = serializeDOM({
domTransformation: transformRelativeUrls,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wwilsman wwilsman merged commit efa916a into master Jul 7, 2021
@wwilsman wwilsman deleted the ww/serialize-iframe-urls branch July 7, 2021 15:19
samarsault pushed a commit that referenced this pull request Mar 3, 2023
Bumps [@percy/sdk-utils](https://github.com/percy/cli/tree/HEAD/packages/sdk-utils) from 1.0.0-beta.65 to 1.0.0-beta.67.
- [Release notes](https://github.com/percy/cli/releases)
- [Commits](https://github.com/percy/cli/commits/v1.0.0-beta.67/packages/sdk-utils)

---
updated-dependencies:
- dependency-name: "@percy/sdk-utils"
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix serialized iframe relative URLs
2 participants