From 8b9af3263a5f7dde37b56b17e43e55db1fddeeae Mon Sep 17 00:00:00 2001 From: Simon Pieters Date: Fri, 21 Feb 2014 11:58:57 +0100 Subject: [PATCH] Parse srcset attribute more like original srcset. Fixes #97 --- index.src.html | 207 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 181 insertions(+), 26 deletions(-) diff --git a/index.src.html b/index.src.html index def2b9b8..2094f555 100644 --- a/index.src.html +++ b/index.src.html @@ -292,6 +292,16 @@

specification. The following terms are defined by the [[!HTML]] specification: + skip whitespace, + collect a sequence of characters, + space character, + split a string on spaces, + valid non-negative integer, + rules for parsing non-negative integers, + valid floating-point number, + rules for parsing floating-point number values, + valid non-empty URL + and valid media query. @@ -482,48 +492,193 @@

Parsing a srcset Attribute

When asked to parse a srcset attribute from an element, - parse the value of the element's srcset attribute with the following grammar: + parse the value of the element's srcset attribute as follows: -
-		<image-source-list> = <>#
-		<image-source> = <> [ <> | <> ]?
-	
+
    +
  1. + Let input be the value passed to this + algorithm. - The above grammar must be interpreted per the grammar definition in [[!CSS3VAL]]. - For the purposes of the above grammar, - the <url> production is simply any sequence of non-whitespace characters - that does not end in a comma. - The <source-width> production is a dimension with a unit of ''w''. - All other terminal productions are defined as per CSS. +
  2. + Let position be a pointer into + input, initially pointing at the start of the + string. - If the value does not parse successfully according to the above grammar, - return an empty source set. +
  3. + Let raw candidates be an initially empty + ordered list of URLs with associated unparsed + descriptors. The order of entries in the list is the + order in which entries are added to the list. - Otherwise, - let source set initially be an empty source set. - For each <> parsed, - do the following: +
  4. + Splitting loop: Skip whitespace. + +
  5. + Collect a sequence of characters that are not + space characters, and let that be url. -
    1. - Let source be a fresh image source. + If url ends with a U+002C COMMA character + (,), remove that character from url and let + descriptors be the empty string. Otherwise, + follow these substeps: + +
        +
      1. + If url is empty, then jump to + the step labeled descriptor + parser. + +
      2. + Collect a sequence of characters + that are not U+002C COMMA characters + (,), and let that be + descriptors. +
      + +
    2. + Add url to raw candidates, + associated with descriptors. + +
    3. + If position is past the end of + input, then jump to the step labeled + descriptor parser.
    4. - Set source’s URL to the parsed <>. + Advance position to the next character in + input (skipping past the U+002C COMMA + character (,) separating this candidate from the next).
    5. - If a <> was parsed, - set source’s resolution descriptor to the <>’s value. + Return to the step labeled splitting loop.
    6. - If a <> was parsed, - set source’s width descriptor to the <>’s value. + Descriptor parser: Let + candidates be an initially empty source + set. The order of entries in the list is the order + in which entries are added to the list. + +
    7. + For each entry in raw candidates with URL + url associated with the unparsed descriptors + unparsed descriptors, in the order they were + originally added to the list, run these substeps: + +
        +
      1. + Let descriptor list be the + result of + splitting + unparsed descriptors on + spaces. + +
      2. + Let error be no. + +
      3. + Let width be + absent. + +
      4. + Let density be + absent. + +
      5. + For each token in descriptor + list, run the appropriate set of + steps from the following list: + +
        +
        If the token consists of a + valid non-negative + integer followed by a U+0077 + LATIN SMALL LETTER W character + +
        +
          +
        1. + If width and density are not both absent, then + let error be yes. + +
        2. + Apply the rules for parsing non-negative integers to the token. Let + width be the result. +
        + +
        If the token consists of a + valid floating-point + number followed by a U+0078 + LATIN SMALL LETTER X character + +
        +
          +
        1. + If width and density are not both absent, then + let error be yes. + +
        2. + Apply the rules for parsing floating-point number values to the token. Let + density be the result. +
        +
        + +
      6. + If error is still + no, then add a new image + source to candidates + whose URL is url, associated + with a width width if not + absent and a pixel density + density if not + absent. +
    8. - Append source to source set. + Return candidates.
    - Then return source set. + An image candidate string consists of the following + components, in order: + +
      +
    1. + Zero or more space characters. + +
    2. + A valid non-empty URL that does not end with a + U+002C COMMA character (,), referencing a + non-interactive, optionally animated, image resource + that is neither paged nor scripted. + +
    3. + Zero or more space characters. + +
    4. + Zero or one of the following: + +
        +
      • + A width descriptor, + consisting of: a space character, + a valid non-negative integer + representing the width + descriptor value, and a U+0077 + LATIN SMALL LETTER W character. + +
      • + A pixel density descriptor, + consisting of: a space character, + a valid floating-point number + giving a number greater than zero + representing the pixel density + descriptor value, and a U+0078 + LATIN SMALL LETTER X character. +
      + +
    5. + Zero or more space characters. +

    Parsing a sizes Attribute