Skip to content

Conversation

@alexsch01
Copy link
Contributor

@alexsch01 alexsch01 commented Dec 3, 2025

Additional details

Instead of trying to fit the injected cypress script into the head element, it's less error-prone to put it as a top level <script> tag before the HTML

  • one exception is the DOCTYPE, which needs to be the first line of text, so we are keeping insertAfter for that case

Original HTML

<!DOCTYPE html>
<html>
<!-- <head>
<title>Test</title> 
</head> -->
</html>

Before PR

<!DOCTYPE html>
<html>
<!-- <head> <script type='text/javascript'> CYPRESS_INJECTION(); </script>
<title>Test</title> 
</head> -->
</html>

After PR

<!DOCTYPE html>
<script type='text/javascript'> CYPRESS_INJECTION(); </script>
<html>
<!-- <head>
<title>Test</title> 
</head> -->
</html>

Steps to test

How has the user experience changed?

PR Tasks


Note

Injects Cypress HTML after <!DOCTYPE> when present, or at top-level otherwise, handling commented/XML-prefixed HTML; adds unit tests and updates changelog.

  • Proxy/Rewriter (packages/proxy/lib/http/util/rewriter.ts)
    • Replace head/body/html insertion with simpler strategy: insert after <!DOCTYPE> if found, otherwise prepend injection at top-level via new htmlHelper().
    • Broaden doctype detection regex to handle leading comments and XML declarations.
    • Preserve security stripping; refactor to use helper and remove unused insertion paths.
  • Tests (packages/proxy/test/unit/http/util/rewriter.spec.ts)
    • Add unit tests covering DOCTYPE, commented DOCTYPE, XML declaration, and mixed-comment scenarios.
  • Changelog (cli/CHANGELOG.md)
    • Note bugfix for commented HTML breaking DOM injection.

Written by Cursor Bugbot for commit 63fb635. This will update automatically on new commits. Configure here.

@cypress-app-bot
Copy link
Collaborator

@alexsch01 alexsch01 marked this pull request as draft December 5, 2025 05:52
@alexsch01 alexsch01 marked this pull request as ready for review December 5, 2025 12:04
@alexsch01
Copy link
Contributor Author

@jennifer-shehane @AtofStryker @mschile this is kinda a big change, let me know what you think of it
The regex isn't perfect, but I don't want a commented out DOCTYPE to be matched since that would cause the same issue this PR is fixing

@jennifer-shehane
Copy link
Member

@alexsch01 Going to run the tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

commented HTML code prevents packages/proxy/lib/http/util/rewriter.ts from working properly

3 participants