File: srcsetparse.py

package info (click to toggle)
linkchecker 10.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,132 kB
  • sloc: python: 13,154; makefile: 134; sh: 71; xml: 36; sql: 20; javascript: 19; php: 2
file content (91 lines) | stat: -rw-r--r-- 3,067 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (C) 2022 Stefan Fisk
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
srcset attribute parser
"""


_TAB = '\u0009'
_LF = '\u000A'
_FF = '\u000C'
_CR = '\u000D'
_SPACE = '\u0020'
_COMMA = '\u002C'
_LEFT_PARENTHESIS = '\u0028'
_RIGHT_PARENTHESIS = '\u0029'


_WHITESPACE = {_TAB,  _LF,  _FF,  _CR,  _SPACE}
_WHITESPACE_OR_COMMA = _WHITESPACE | {_COMMA}


def parse_srcset(input):
    """
    Parse HTML srcset

    Based on WhatWG HTML standard § 4.8.4.3.10 Parsing a srcset attribute,
    but does not parse or validate descriptors.

    https://html.spec.whatwg.org/multipage/images.html#parse-a-srcset-attribute
    """

    input_end = len(input)
    position = 0
    urls = []

    while position < input_end:
        # 4. Splitting loop: Collect a sequence of code points that are ASCII
        # whitespace or U+002C COMMA characters from input given position.
        while position < input_end and input[position] in _WHITESPACE_OR_COMMA:
            position += 1

        # 5. If position is past the end of input, return candidates.
        if position >= input_end:
            return urls

        # 6. Collect a sequence of code points that are not ASCII
        # whitespace from input given position, and let that be url.
        url_start = position
        while position < input_end and input[position] not in _WHITESPACE:
            position += 1
        url_end = position

        # 8, If url ends with U+002C (,), then:
        if input[url_end - 1] == _COMMA:
            # Remove all trailing U+002C COMMA characters from url.
            while url_end > url_start and input[url_end - 1] == _COMMA:
                url_end -= 1
        else:
            # This is a shortened version of 1–4 that simply skips the
            # descriptors
            while position < input_end:
                if input[position] == _LEFT_PARENTHESIS:
                    # Skip until first closing parenthesis
                    while (position < input_end and input[position] !=
                            _RIGHT_PARENTHESIS):
                        position += 1
                elif input[position] == _COMMA:
                    break

                position += 1

        # 9-15 is parsing and validation of the descriptors, which we ignore

        # If we found an URL
        if url_end > url_start:
            urls.append(input[url_start:url_end])

    return urls