Skip to content

Commit 57aa46a

Browse files
authored
Merge pull request #3351 from sveltejs/disable-validation
disable validation with magic comments
2 parents 50f8c85 + 8170d4f commit 57aa46a

File tree

14 files changed

+203
-0
lines changed

14 files changed

+203
-0
lines changed

site/content/docs/02-template-syntax.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ Text can also contain JavaScript expressions:
111111
```
112112

113113

114+
### Comments
115+
116+
---
117+
118+
You can use HTML comments inside components.
119+
120+
```html
121+
<!-- this is a comment! -->
122+
<h1>Hello world</h1>
123+
```
124+
125+
---
126+
127+
Comments beginning with `svelte-ignore` disable warnings for the next block of markup. Usually these are accessibility warnings; make sure that you're disabling them for a good reason.
128+
129+
```html
130+
<!-- svelte-ignore a11y-autofocus -->
131+
<input bind:value={name} autofocus>
132+
```
133+
134+
114135
### {#if ...}
115136

116137
```sv

src/compiler/compile/Component.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import get_object from './utils/get_object';
2323
import unwrap_parens from './utils/unwrap_parens';
2424
import Slot from './nodes/Slot';
2525
import { Node as ESTreeNode } from 'estree';
26+
import add_to_set from './utils/add_to_set';
2627

2728
interface ComponentOptions {
2829
namespace?: string;
@@ -70,6 +71,8 @@ function remove_node(code: MagicString, start: number, end: number, body: Node,
7071
export default class Component {
7172
stats: Stats;
7273
warnings: Warning[];
74+
ignores: Set<string>;
75+
ignore_stack: Set<string>[] = [];
7376

7477
ast: Ast;
7578
source: string;
@@ -442,6 +445,10 @@ export default class Component {
442445
message: string;
443446
}
444447
) {
448+
if (this.ignores && this.ignores.has(warning.code)) {
449+
return;
450+
}
451+
445452
if (!this.locator) {
446453
this.locator = getLocator(this.source, { offsetLine: 1 });
447454
}
@@ -1253,6 +1260,17 @@ export default class Component {
12531260
message
12541261
});
12551262
}
1263+
1264+
push_ignores(ignores) {
1265+
this.ignores = new Set(this.ignores || []);
1266+
add_to_set(this.ignores, ignores);
1267+
this.ignore_stack.push(this.ignores);
1268+
}
1269+
1270+
pop_ignores() {
1271+
this.ignore_stack.pop();
1272+
this.ignores = this.ignore_stack[this.ignore_stack.length - 1];
1273+
}
12561274
}
12571275

12581276
function process_component_options(component: Component, nodes) {

src/compiler/compile/nodes/Comment.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import Node from './shared/Node';
22

3+
const pattern = /^\s*svelte-ignore\s+([\s\S]+)\s*$/m;
4+
35
export default class Comment extends Node {
46
type: 'Comment';
57
data: string;
8+
ignores: string[];
69

710
constructor(component, parent, scope, info) {
811
super(component, parent, scope, info);
912
this.data = info.data;
13+
14+
const match = pattern.exec(this.data);
15+
this.ignores = match ? match[1].split(/[^\S]/).map(x => x.trim()).filter(Boolean) : [];
1016
}
1117
}

src/compiler/compile/nodes/shared/map_children.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,20 @@ function get_constructor(type) {
4242

4343
export default function map_children(component, parent, scope, children: Node[]) {
4444
let last = null;
45+
let ignores = [];
46+
4547
return children.map(child => {
4648
const constructor = get_constructor(child.type);
49+
50+
const use_ignores = child.type !== 'Text' && child.type !== 'Comment' && ignores.length;
51+
52+
if (use_ignores) component.push_ignores(ignores);
4753
const node = new constructor(component, parent, scope, child);
54+
if (use_ignores) component.pop_ignores(), ignores = [];
55+
56+
if (node.type === 'Comment' && node.ignores.length) {
57+
ignores.push(...node.ignores);
58+
}
4859

4960
if (last) last.next = node;
5061
node.prev = last;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!-- svelte-ignore a11y-missing-attribute -->
2+
<div>
3+
<img src="this-is-fine.jpg">
4+
<marquee>but this is still discouraged</marquee>
5+
</div>
6+
7+
<img src="potato.jpg">
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"code": "a11y-distracting-elements",
4+
"end": {
5+
"character": 131,
6+
"column": 49,
7+
"line": 4
8+
},
9+
"message": "A11y: Avoid <marquee> elements",
10+
"pos": 83,
11+
"start": {
12+
"character": 83,
13+
"column": 1,
14+
"line": 4
15+
}
16+
},
17+
{
18+
"code": "a11y-missing-attribute",
19+
"end": {
20+
"character": 162,
21+
"column": 22,
22+
"line": 7
23+
},
24+
"message": "A11y: <img> element should have an alt attribute",
25+
"pos": 140,
26+
"start": {
27+
"character": 140,
28+
"column": 0,
29+
"line": 7
30+
}
31+
}
32+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!-- svelte-ignore a11y-structure a11y-missing-attribute -->
2+
<div>
3+
<figure>
4+
<img src="potato.jpg">
5+
<marquee>
6+
<figcaption>potato</figcaption>
7+
</marquee>
8+
</figure>
9+
10+
<!-- svelte-ignore a11y-distracting-elements -->
11+
<figure>
12+
<img src="potato.jpg">
13+
<marquee>
14+
<figcaption>potato</figcaption>
15+
</marquee>
16+
</figure>
17+
</div>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"code": "a11y-distracting-elements",
4+
"end": {
5+
"character": 161,
6+
"column": 12,
7+
"line": 7
8+
},
9+
"message": "A11y: Avoid <marquee> elements",
10+
"pos": 104,
11+
"start": {
12+
"character": 104,
13+
"column": 2,
14+
"line": 5
15+
}
16+
}
17+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- svelte-ignore a11y-missing-attribute
2+
a11y-distracting-elements -->
3+
<div>
4+
<img src="this-is-fine.jpg">
5+
<marquee>but this is still discouraged</marquee>
6+
</div>
7+
8+
<img src="potato.jpg">
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"code": "a11y-missing-attribute",
4+
"end": {
5+
"character": 207,
6+
"column": 22,
7+
"line": 8
8+
},
9+
"message": "A11y: <img> element should have an alt attribute",
10+
"pos": 185,
11+
"start": {
12+
"character": 185,
13+
"column": 0,
14+
"line": 8
15+
}
16+
}
17+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- svelte-ignore a11y-missing-attribute -->
2+
<!-- svelte-ignore a11y-distracting-elements -->
3+
<div>
4+
<img src="this-is-fine.jpg">
5+
<marquee>but this is still discouraged</marquee>
6+
</div>
7+
8+
<img src="potato.jpg">
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"code": "a11y-missing-attribute",
4+
"end": {
5+
"character": 211,
6+
"column": 22,
7+
"line": 8
8+
},
9+
"message": "A11y: <img> element should have an alt attribute",
10+
"pos": 189,
11+
"start": {
12+
"character": 189,
13+
"column": 0,
14+
"line": 8
15+
}
16+
}
17+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!-- svelte-ignore a11y-missing-attribute a11y-distracting-elements -->
2+
<div>
3+
<img src="this-is-fine.jpg">
4+
<marquee>but this is still discouraged</marquee>
5+
</div>
6+
7+
<img src="potato.jpg">
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{
3+
"code": "a11y-missing-attribute",
4+
"end": {
5+
"character": 188,
6+
"column": 22,
7+
"line": 7
8+
},
9+
"message": "A11y: <img> element should have an alt attribute",
10+
"pos": 166,
11+
"start": {
12+
"character": 166,
13+
"column": 0,
14+
"line": 7
15+
}
16+
}
17+
]

0 commit comments

Comments
 (0)