Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
a11y: warn about href="javascript:void(0)"
  • Loading branch information
kjj6198 committed Apr 27, 2020
commit f10de519e23419fd939e5f009807e2a42793a59e
2 changes: 1 addition & 1 deletion src/compiler/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

component.warn(attribute, {
code: `a11y-invalid-attribute`,
message: `A11y: '${value}' is not a valid ${attribute.name} attribute`
Expand Down
3 changes: 2 additions & 1 deletion test/validator/samples/a11y-anchor-is-valid/input.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<a>not actually a link</a>
<a href=''>invalid</a>
<a href='#'>invalid</a>
<a href='#'>invalid</a>
<a href="javascript:void(0)">invalid</a>
15 changes: 15 additions & 0 deletions test/validator/samples/a11y-anchor-is-valid/warnings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,20 @@
"character": 61
},
"pos": 53
},
{
"code": "a11y-invalid-attribute",
"message": "A11y: 'javascript:void(0)' is not a valid href attribute",
"start": {
"line": 4,
"column": 3,
"character": 77
},
"end": {
"line": 4,
"column": 28,
"character": 102
},
"pos": 77
}
]