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] error when using combinator incorrectly #7650

Merged
merged 2 commits into from Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/compile/compiler_errors.ts
Expand Up @@ -234,6 +234,10 @@ export default {
code: 'css-invalid-global-selector',
message: ':global(...) must contain a single selector'
},
css_invalid_selector: (selector: string) => ({
code: 'css-invalid-selector',
message: `Invalid selector "${selector}"`
}),
duplicate_animation: {
code: 'duplicate-animation',
message: "An element can only have one 'animate' directive"
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/compile/css/Selector.ts
Expand Up @@ -145,6 +145,7 @@ export default class Selector {
}

this.validate_global_with_multiple_selectors(component);
this.validate_invalid_combinator_without_selector(component);
}

validate_global_with_multiple_selectors(component: Component) {
Expand All @@ -163,6 +164,17 @@ export default class Selector {
}
}
}
validate_invalid_combinator_without_selector(component: Component) {
for (let i = 0; i < this.blocks.length; i++) {
const block = this.blocks[i];
if (block.combinator && block.selectors.length === 0) {
component.error(this.node, compiler_errors.css_invalid_selector(component.source.slice(this.node.start, this.node.end)));
}
tanhauhau marked this conversation as resolved.
Show resolved Hide resolved
if (!block.combinator && block.selectors.length === 0) {
component.error(this.node, compiler_errors.css_invalid_selector(component.source.slice(this.node.start, this.node.end)));
}
}
}

get_amount_class_specificity_increased() {
let count = 0;
Expand Down
@@ -0,0 +1,9 @@
[
{
"code": "css-invalid-selector",
"message": "Invalid selector \"> span\"",
"start": { "line": 10, "column": 1, "character": 88 },
"end": { "line": 10, "column": 7, "character": 94 },
"pos": 88
}
]
@@ -0,0 +1,13 @@
<p><span /></p>

<style>
p > span {
color: red;
}
:has(> span) {
color: red;
}
> span {
color: red;
}
</style>
@@ -0,0 +1,9 @@
[
{
"code": "css-invalid-selector",
"message": "Invalid selector \"+ p\"",
"start": { "line": 8, "column": 1, "character": 68 },
"end": { "line": 8, "column": 4, "character": 71 },
"pos": 68
}
]
@@ -0,0 +1,11 @@
<p><span /></p>
<p><span /></p>

<style>
p + p {
color: red;
}
+ p {
color: red;
}
</style>
@@ -0,0 +1,9 @@
[
{
"code": "css-invalid-selector",
"message": "Invalid selector \"> span\"",
"start": { "line": 5, "column": 2, "character": 44 },
"end": { "line": 5, "column": 8, "character": 50 },
"pos": 44
}
]
@@ -0,0 +1,9 @@
<p><span /></p>

<style>
@media screen {
> span {
color: red;
}
}
</style>
@@ -0,0 +1,9 @@
[
{
"code": "css-invalid-selector",
"message": "Invalid selector \"p >\"",
"start": { "line": 4, "column": 1, "character": 26 },
"end": { "line": 4, "column": 4, "character": 29 },
"pos": 26
}
]
@@ -0,0 +1,7 @@
<p><span /></p>

<style>
p > {
color: red;
}
</style>