Home > Article > Web Front-end > Why Does CSS Discard Entire Rules with Invalid Selectors?
Invalid CSS Selector Error Handling: Rationale
The CSS Selectors Level 3 specification dictates that entire rules containing invalid selectors should be discarded. This decision stems from the challenges of parsing errors within selectors, ensuring consistent error handling, and maintaining forward compatibility.
Challenges of Parsing Errors
Parsing errors in CSS selectors can involve:
By discarding rules with invalid selectors, implementations avoid the complexity of determining the validity of partial selectors or complex selector groups.
Consistency and Compatibility
The spec ensures consistent error handling across all implementations. Invalid selectors are treated as a complete parsing failure, regardless of the recognition of individual parts. Forward compatibility is also considered, as future specifications may introduce new selector syntax that could introduce further parsing challenges.
Example: Handling ":last-child"
In the example rule involving ":last-child," the spec considers this selector invalid since it is unrecognized, even if browsers can generally identify pseudo-classes. Maintaining consistency and avoiding potential layout disruptions takes precedence.
Rationale for Discarding the Rule
Ultimately, discarding invalid rules provides the most straightforward and predictable error handling:
Alternative Viewpoints and Exceptions
While discarding invalid rules is generally the recommended approach, some discussions in mailing lists have suggested revisions to the specification. However, the current behavior remains unchanged due to potential web compatibility concerns, as sites rely on the existing behavior for CSS hacks.
The above is the detailed content of Why Does CSS Discard Entire Rules with Invalid Selectors?. For more information, please follow other related articles on the PHP Chinese website!