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

a11y: warn about href="javascript:void(0)" #4733

Merged
merged 3 commits into from
Apr 28, 2020

Conversation

kjj6198
Copy link
Contributor

@kjj6198 kjj6198 commented Apr 27, 2020

Summary

href="javascript:void(0)" is not recommended for a11y and maybe it would be great if svelte can warn about it.

Note

Besides, would it be great to warn about something like:

<a href={null}></a>
<a href={undefined}></a>
<a href={`javascript:void(0)`}></a>

as well? If so I'm willing to help or have an issue about this.

Tests

  • Run the tests tests with npm test or yarn test)

@@ -428,7 +428,7 @@ export default class Element extends Node {
if (attribute) {
const value = attribute.get_static_value();

if (value === '' || value === '#') {
if (value === '' || value === '#' || /^\W*?javascript/.test(value)) {
Copy link
Member

Choose a reason for hiding this comment

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

The ? here after the * doesn't do anything - and we should be testing for javascript:, not just javascript. There's nothing wrong with linking to paths that happen to begin with javascript.

@@ -428,7 +428,7 @@ export default class Element extends Node {
if (attribute) {
const value = attribute.get_static_value();

if (value === '' || value === '#' || /^\W*?javascript/.test(value)) {
if (value === '' || value === '#' || /^\W*javascript:/.test(value)) {
Copy link
Contributor

@bwbroersma bwbroersma Apr 28, 2020

Choose a reason for hiding this comment

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

A case insensitive check should be done, since URI schemes are case insensitive RFC 2396:

/^\W*javascript:/i.test(value)

Copy link
Contributor Author

@kjj6198 kjj6198 Apr 28, 2020

Choose a reason for hiding this comment

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

thanks for the information!

@Conduitry Conduitry merged commit 56d1af4 into sveltejs:master Apr 28, 2020
@kjj6198 kjj6198 deleted the warn-about-invalid-href branch April 29, 2020 00:02
@Conduitry
Copy link
Member

This has been released in 3.22.0 - thanks!

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.

3 participants