• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

earwig / mwparserfromhell / 10014163542

19 Jul 2024 08:49PM CUT coverage: 99.201% (-0.002%) from 99.203%
10014163542

Pull #326

github

web-flow
Merge 8c23031f1 into 4e73af2fa
Pull Request #326: Make fallthrough explicit in tok_parse.c

2979 of 3003 relevant lines covered (99.2%)

9.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/mwparserfromhell/nodes/extras/attribute.py
1
# Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
2
#
3
# Permission is hereby granted, free of charge, to any person obtaining a copy
4
# of this software and associated documentation files (the "Software"), to deal
5
# in the Software without restriction, including without limitation the rights
6
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
# copies of the Software, and to permit persons to whom the Software is
8
# furnished to do so, subject to the following conditions:
9
#
10
# The above copyright notice and this permission notice shall be included in
11
# all copies or substantial portions of the Software.
12
#
13
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
# SOFTWARE.
20

21

22
from ...string_mixin import StringMixIn
10✔
23
from ...utils import parse_anything
10✔
24

25
__all__ = ["Attribute"]
10✔
26

27

28
class Attribute(StringMixIn):
10✔
29
    """Represents an attribute of an HTML tag.
30

31
    This is used by :class:`.Tag` objects. For example, the tag
32
    ``<ref name="foo">`` contains an Attribute whose name is ``"name"`` and
33
    whose value is ``"foo"``.
34
    """
35

36
    def __init__(
10✔
37
        self,
38
        name,
39
        value=None,
40
        quotes='"',
41
        pad_first=" ",
42
        pad_before_eq="",
43
        pad_after_eq="",
44
    ):
45
        super().__init__()
10✔
46
        self.name = name
10✔
47
        self._quotes = None
10✔
48
        self.value = value
10✔
49
        self.quotes = quotes
10✔
50
        self.pad_first = pad_first
10✔
51
        self.pad_before_eq = pad_before_eq
10✔
52
        self.pad_after_eq = pad_after_eq
10✔
53

54
    def __str__(self):
10✔
55
        result = self.pad_first + str(self.name) + self.pad_before_eq
10✔
56
        if self.value is not None:
10✔
57
            result += "=" + self.pad_after_eq
10✔
58
            if self.quotes:
10✔
59
                return result + self.quotes + str(self.value) + self.quotes
10✔
60
            return result + str(self.value)
10✔
61
        return result
10✔
62

63
    @staticmethod
10✔
64
    def _value_needs_quotes(val):
10✔
65
        """Return valid quotes for the given value, or None if unneeded."""
66
        if not val:
10✔
67
            return None
10✔
68
        val = "".join(str(node) for node in val.filter_text(recursive=False))
10✔
69
        if not any(char.isspace() for char in val):
10✔
70
            return None
10✔
71
        if "'" in val and '"' not in val:
10✔
72
            return '"'
10✔
73
        if '"' in val and "'" not in val:
10✔
74
            return "'"
10✔
75
        return "\"'"  # Either acceptable, " preferred over '
10✔
76

77
    def _set_padding(self, attr, value):
10✔
78
        """Setter for the value of a padding attribute."""
79
        if not value:
10✔
80
            setattr(self, attr, "")
10✔
81
        else:
82
            value = str(value)
10✔
83
            if not value.isspace():
10✔
84
                raise ValueError("padding must be entirely whitespace")
10✔
85
            setattr(self, attr, value)
10✔
86

87
    @staticmethod
10✔
88
    def coerce_quotes(quotes):
10✔
89
        """Coerce a quote type into an acceptable value, or raise an error."""
90
        orig, quotes = quotes, str(quotes) if quotes else None
10✔
91
        if quotes not in [None, '"', "'"]:
10✔
92
            raise ValueError("{!r} is not a valid quote type".format(orig))
10✔
93
        return quotes
10✔
94

95
    @property
10✔
96
    def name(self):
10✔
97
        """The name of the attribute as a :class:`.Wikicode` object."""
98
        return self._name
10✔
99

100
    @property
10✔
101
    def value(self):
10✔
102
        """The value of the attribute as a :class:`.Wikicode` object."""
103
        return self._value
10✔
104

105
    @property
10✔
106
    def quotes(self):
10✔
107
        """How to enclose the attribute value. ``"``, ``'``, or ``None``."""
108
        return self._quotes
10✔
109

110
    @property
10✔
111
    def pad_first(self):
10✔
112
        """Spacing to insert right before the attribute."""
113
        return self._pad_first
10✔
114

115
    @property
10✔
116
    def pad_before_eq(self):
10✔
117
        """Spacing to insert right before the equal sign."""
118
        return self._pad_before_eq
10✔
119

120
    @property
10✔
121
    def pad_after_eq(self):
10✔
122
        """Spacing to insert right after the equal sign."""
123
        return self._pad_after_eq
10✔
124

125
    @name.setter
10✔
126
    def name(self, value):
10✔
127
        self._name = parse_anything(value)
10✔
128

129
    @value.setter
10✔
130
    def value(self, newval):
10✔
131
        if newval is None:
10✔
132
            self._value = None
10✔
133
        else:
134
            code = parse_anything(newval)
10✔
135
            quotes = self._value_needs_quotes(code)
10✔
136
            if quotes and (not self.quotes or self.quotes not in quotes):
10✔
137
                self._quotes = quotes[0]
10✔
138
            self._value = code
10✔
139

140
    @quotes.setter
10✔
141
    def quotes(self, value):
10✔
142
        value = self.coerce_quotes(value)
10✔
143
        if not value and self._value_needs_quotes(self.value):
10✔
144
            raise ValueError("attribute value requires quotes")
10✔
145
        self._quotes = value
10✔
146

147
    @pad_first.setter
10✔
148
    def pad_first(self, value):
10✔
149
        self._set_padding("_pad_first", value)
10✔
150

151
    @pad_before_eq.setter
10✔
152
    def pad_before_eq(self, value):
10✔
153
        self._set_padding("_pad_before_eq", value)
10✔
154

155
    @pad_after_eq.setter
10✔
156
    def pad_after_eq(self, value):
10✔
157
        self._set_padding("_pad_after_eq", value)
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc