HTML color attributes microsyntax

Superseded — Last Update 16 February 2009

Editor
Simon Pieters <simonp@opera.com>

Status of this document

This document has been superseded by HTML5. See the rules for parsing a legacy color value definition and the rendering section.

Table of contents

  1. 1 Common infrastructure
    1. 1.1 Terminology
    2. 1.2 Conformance requirements
    3. 1.3 Case-sensitivity
  2. 2 Color attributes
  3. Acknowledgements

1 Common infrastructure

1.1 Terminology

Unless otherwise stated, all elements defined or mentioned in this specification are in the http://www.w3.org/1999/xhtml namespace, and all attributes defined or mentioned in this specification have no namespace (they are in the per-element partition).

1.2 Conformance requirements

RFC2119

1.3 Case-sensitivity

Comparing two strings in a ASCII case-insensitive manner means comparing them exactly, codepoint for codepoint, except that the characters in the range U+0041 .. U+005A (i.e. LATIN CAPITAL LETTER A to LATIN CAPITAL LETTER Z) and the corresponding characters in the range U+0061 .. U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z) are considered to also match.

2 Color attributes

The following attributes are color attributes:

When a user agent is to extract the color of a color attribute, it must use the rules for parsing color strings, using the attribute's value as the string being parsed. If the algorithm returned an error, the user agent must act as if the attribute was absent. Otherwise, the user agent must process the string returned by the algorithm as a <color> as defined in the CSS Color Module Level 3 specification. [CSS3COLOR]

The rules for parsing color strings are given in the following algorithm. When invoked, the steps must be followed in the order given, aborting at the first step that returns a value. This algorithm will either return a string or an error.

Browsers act on UTF-16 code units here — not Unicode codepoints. Though HTML5 should probably address this throughout.

  1. Let input be the string being parsed.

  2. If input is the empty string, return an error.

  3. If input matches one of the color keywords (in an ASCII case-insensitive manner), then return input.

  4. If input's length is 4 and the first character is a U+0023 NUMBER SIGN ("#") and the remaining characters are in the ranges U+0030 DIGIT ZERO .. U+0039 DIGIT NINE, U+0061 LATIN SMALL LETTER A .. U+0066 LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER A .. U+0046 LATIN CAPITAL LETTER F, then return input.

  5. If input's length is greater than 128, truncate input after the 128'th character.

  6. If the first character in input is a U+0023 NUMBER SIGN ("#"), remove it from input.

  7. Replace every character in input that is not in the ranges U+0030 DIGIT ZERO .. U+0039 DIGIT NINE, U+0061 LATIN SMALL LETTER A .. U+0066 LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER A .. U+0046 LATIN CAPITAL LETTER F with a U+0030 DIGIT ZERO character.

  8. If input is the empty string, append a U+0030 DIGIT ZERO character.

  9. While input's length is not a multiple of 3, append a U+0030 DIGIT ZERO character to input.

  10. Let segments be an array with the length 3.

  11. Split input into three strings with equal length and append them to segments.

  12. If the first item's length in segments is greater than 8, then truncate each item in segments so that the trailing 8 characters remain.

  13. If the first item's length in segments is 1, prepend a U+0030 DIGIT ZERO character to each item in segments. Otherwise, follow these substeps:

    1. While the first item's length in segments is greater than 2 and the first character in each item in segments is a U+0030 DIGIT ZERO character, then loop through each item in segments and remove the first character.

    2. If the first item's length in segments is greater than 2, truncate each item in segments after the second character.

  14. Return the concatenation of the U+0023 NUMBER SIGN character ("#") and each item in segments.

The color keywords used by the algorithm above are those in the following list:

Those are the HTML4 color keywords, SVG color keywords, CSS2 UI Colors, and 'transparent' from [CSS3COLOR].

Acknowledgements

Thanks to Andy Lyttle and Lachlan Hunt for their useful comments.