w3c / w3c/csswg-drafts

[css-syntax] Which trailing whitespace characters are allowed after escaped hex digits?

Open
#5,835 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

css-syntax-3
Dominant language
Bikeshed
Stars
4.9k
Forks
816
Avg merge
2d 18h
Merged PRs (30d)
24

Description

Hi everyone! I'm implementing a CSS parser, and I found some strange edge case in the "consume escaped code point" routine which I haven't seen documented anywhere on the Internet. So I'm asking for clarification, and this could serve as documentation for future implementors.

Here is the relevant part of the specification:

https://www.w3.org/TR/css-syntax-3/#consume-escaped-code-point

When consuming an escaped code point, when this code point is expressed as hex digits, then the specification says that a trailing whitespace should also be consumed. The rationale is well explained in this article: this makes it possible to express the string foo@bar as foo\A9 bar. Otherwise, if we write foo\A9bar, the b and a characters would also be considered hex digits.

However, it wasn't completely clear to me which whitespace characters should be considered here:

  • Should we just consume a trailing U+0020 SPACE?
  • Or should we also consume a trailing U+000A LINE FEED ("newline") or trailing U+0009 CHARACTER TABULATION ("tab")?

The specification as written seems to imply the latter, that is, a trailing newline or trailing tab should also be consumed. However, in the context of consuming a string token, the presence of a newline is normally considered a parse error, and a <bad-string-token> should be returned. So it seemed strange to me to allow the presence of a newline when it appears after escaped hex digits.

So I checked in Chrome and Firefox, and both agree with what the specification seems to imply: a trailing newline after escaped hex digits in a string is valid. But otherwise a newline in a string is a parse error.

For example, in the following HTML, we only see "World" rendered (because "Hello Beautiful " contains a newline, so it's a bad string, so it's not rendered):

<!DOCTYPE html>
<html>
<head>
<style>
p:before {
    content: "Hello
 Beautiful ";
}
</style>
</head>
<body>
<p>World</p>
</body>
</html>

But if we escape the letter o (U+006F):

    content: "Hell\6f
 Beautiful ";

Then the "Hello Beautiful " string is now valid despite containing a newline, and the whole "Hello Beautiful World" is rendered.

Since browsers and the specification are consistent, the answer to the question in the title is clear: all three whitespace characters (space, newline, and tab) are allowed as trailing character after escaped hex digits.

However, this behavior is surprising to me. Is it intentional? Would it be useful to add a clarification note in the specification?

Thank you!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the CSS Syntax 3 sections on “consume escaped code point” and “consume string token,” then compare their whitespace and parse-error rules with the browser behavior described here. Done means the Working Group has resolved whether the behavior is intentional and, if needed, clarified the specification text or added a note.

Written by the indexing model from the issue text.

Assessment

Tech stack
css
Domain
documentation, web-dev
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.