Skip to content

details bind:open should count as [open] for styling #3281

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

Closed
Conduitry opened this issue Jul 23, 2019 · 2 comments · Fixed by #3335
Closed

details bind:open should count as [open] for styling #3281

Conduitry opened this issue Jul 23, 2019 · 2 comments · Fixed by #3335
Labels

Comments

@Conduitry
Copy link
Member

<script>
	let open = false;
</script>

<details bind:open>Hello</details>

<style>
	details[open] {
		color: red;
	}
</style>

details[open] is getting removed because the compiler doesn't think that <details bind:open> indicates an open attribute that might be present.

@Conduitry
Copy link
Member Author

I had assumed there was some precedent for handling this with other bindings, but thinking about it more, I'm not sure what bindings those would be. We might just need a weird one-off check for this when determining whether an element can match a selector.

@Conduitry
Copy link
Member Author

Conduitry commented Aug 2, 2019

diff --git a/src/compiler/compile/css/Selector.ts b/src/compiler/compile/css/Selector.ts
index ef54f789..e70b2986 100644
--- a/src/compiler/compile/css/Selector.ts
+++ b/src/compiler/compile/css/Selector.ts
@@ -219,6 +219,8 @@ function attribute_matches(node: Node, name: string, expected_value: string, ope
 	const spread = node.attributes.find(attr => attr.type === 'Spread');
 	if (spread) return true;
 
+	if (name === 'open' && node.bindings.some((binding: Node) => binding.name === 'open')) return true;
+
 	const attr = node.attributes.find((attr: Node) => attr.name === name);
 	if (!attr) return false;
 	if (attr.is_true) return operator === null;

This seems to do it. I don't know whether there is a more elegant way. It might make sense to always also search node.bindings, and if we find a binding, assume that it's always possible for this selector to match this element. I.e.:

diff --git a/src/compiler/compile/css/Selector.ts b/src/compiler/compile/css/Selector.ts
index ef54f789..0d4ea82b 100644
--- a/src/compiler/compile/css/Selector.ts
+++ b/src/compiler/compile/css/Selector.ts
@@ -219,6 +219,8 @@ function attribute_matches(node: Node, name: string, expected_value: string, ope
 	const spread = node.attributes.find(attr => attr.type === 'Spread');
 	if (spread) return true;
 
+	if (node.bindings.some((binding: Node) => binding.name === name)) return true;
+
 	const attr = node.attributes.find((attr: Node) => attr.name === name);
 	if (!attr) return false;
 	if (attr.is_true) return operator === null;

Conduitry added a commit to Conduitry/sveltejs_svelte that referenced this issue Aug 2, 2019
Rich-Harris added a commit that referenced this issue Aug 3, 2019
use bindings when matching attribute selector against element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant