Home > Web Front-end > CSS Tutorial > Why are CSS rules with invalid selectors discarded instead of just removing the invalid part?

Why are CSS rules with invalid selectors discarded instead of just removing the invalid part?

Barbara Streisand
Release: 2024-11-10 18:59:03
Original
384 people have browsed it

Why are CSS rules with invalid selectors discarded instead of just removing the invalid part?

Why Invalid CSS Selectors Result in Dropped Rules

Issue:
The CSS Selectors Level 3 specification mandates that rules containing invalid selectors be discarded. Why is this necessary instead of simply removing the unrecognized selector and keeping the rule set?

Rationale:

According to the CSS2.1 specification:

Even though the rest of the selector may look reasonable in CSS 2.1, the whole statement should be ignored if there is an error anywhere in the selector, because it's not known if the comma may acquire other meanings in future updates of CSS.

The CSS Selectors Level 4 specification introduces functional pseudo-classes that accept selector groups as arguments, such as :not(). This could lead to ambiguities in parsing, making it difficult for implementations to accurately handle invalid selectors without potential layout disruptions.

Additionally:

  • Parsing errors can occur at various points, making it uncertain where to end the invalid selector, selector list, or declaration block.
  • The spec makes no distinction between unrecognized and malformed selectors, simplifying error handling.
  • Some layout engines behave differently, such as WebKit ignoring unrecognized selectors with prefixes and applying its own.
  • Changing this behavior could break existing sites that rely on it for selector hacks and filtering.

Therefore, disregarding rules with invalid selectors maintains consistency in error handling, ensures forward compatibility, and prevents potential layout issues.

The above is the detailed content of Why are CSS rules with invalid selectors discarded instead of just removing the invalid part?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template