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

fix(ui): escape html for console log view #4724

Merged
merged 7 commits into from Dec 18, 2023

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Dec 11, 2023

Description

Closes #1279, #2765, #3934

I'm not sure if this naive idea is supposed to work (or already attempted before) but this is just to start investigation and I will test it further.

At least this filter.toHtml(escapeHtml(...)) pattern seems to be already used in report view:

htmlError = `<b>${error.nameStr || error.name}</b>: ${filter.toHtml(escapeHtml(error.message))}`

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented Dec 11, 2023

Deploy Preview for fastidious-cascaron-4ded94 ready!

Name Link
🔨 Latest commit 64ae9d2
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/657fa43b3ebab10008031eb8
😎 Deploy Preview https://deploy-preview-4724--fastidious-cascaron-4ded94.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@hi-ogawa hi-ogawa marked this pull request as ready for review December 11, 2023 06:44
Comment on lines +1 to +8
export function escapeHtml(unsafe: string) {
return unsafe
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;')
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It might be worth noting that the similar code is also used for junit xml.

function escapeXML(value: any): string {
return removeInvalidXMLCharacters(
String(value)
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;'),
true,
)
}

Is it preferred to move this into @vitest/utils and share everywhere?

examples/react-testing-lib/src/components/input.test.tsx Outdated Show resolved Hide resolved
Comment on lines +6 to +33
// https://github.com/vitest-dev/vitest/issues/2765
it('regexp', () => {
console.log(/(?<char>\w)/)
})

// https://github.com/vitest-dev/vitest/issues/3934
it('html-raw', async () => {
console.log(`
<form>
<label for="email">Email Address</label>
<input name="email" />
<button>Submit</button>
</form>
`);
})

// https://github.com/vitest-dev/vitest/issues/1279
it('html-pretty', () => {
const div = document.createElement("div");
div.innerHTML = `
<form>
<label for="email">Email Address</label>
<input name="email" />
<button>Submit</button>
</form>
`.replaceAll(/\n */gm, ""); // strip new liens
console.log(prettyDOM(div))
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I add e2e tests 64ae9d2. Asserting dom format was difficult, so the actual testing is limited to only checking /(?<char>\w) string.

@sheremet-va
Copy link
Member

Thank you! Let's try this out.

@sheremet-va sheremet-va merged commit e0dde6a into vitest-dev:main Dec 18, 2023
19 checks passed
@hi-ogawa hi-ogawa deleted the fix-ui-log-escape-html branch December 19, 2023 23:17
@hi-ogawa
Copy link
Contributor Author

I just realized Github didn't close these two issues since my comment didn't use "Closes" properly.
Would you close them manually?

Closes #2765
Closes #3934

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.

vitest UI doesn't log html correctly in the "console log" tab.
2 participants