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

earwig / mwparserfromhell / 15949141982

28 Jun 2025 11:18PM UTC coverage: 98.886% (-0.3%) from 99.204%
15949141982

push

github

earwig
Fix a failing test

3106 of 3141 relevant lines covered (98.89%)

9.85 hits per line

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

98.98
/src/mwparserfromhell/nodes/extras/attribute.py
1
# Copyright (C) 2012-2025 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
from __future__ import annotations
10✔
22

23
from typing import TYPE_CHECKING, Any
10✔
24

25
from ...string_mixin import StringMixIn
10✔
26
from ...utils import parse_anything
10✔
27

28
if TYPE_CHECKING:
10✔
29
    from ...wikicode import Wikicode
×
30

31
__all__ = ["Attribute"]
10✔
32

33

34
class Attribute(StringMixIn):
10✔
35
    """Represents an attribute of an HTML tag.
36

37
    This is used by :class:`.Tag` objects. For example, the tag
38
    ``<ref name="foo">`` contains an Attribute whose name is ``"name"`` and
39
    whose value is ``"foo"``.
40
    """
41

42
    def __init__(
10✔
43
        self,
44
        name: Any,
45
        value: Any = None,
46
        quotes: str | None = '"',
47
        pad_first: str = " ",
48
        pad_before_eq: str = "",
49
        pad_after_eq: str = "",
50
    ):
51
        super().__init__()
10✔
52

53
        self._pad_first: str
10✔
54
        self._pad_before_eq: str
10✔
55
        self._pad_after_eq: str
10✔
56

57
        self.name = name
10✔
58
        self._quotes: str | None = None
10✔
59
        self.value = value
10✔
60
        self.quotes = quotes
10✔
61
        self.pad_first = pad_first
10✔
62
        self.pad_before_eq = pad_before_eq
10✔
63
        self.pad_after_eq = pad_after_eq
10✔
64

65
    def __str__(self) -> str:
10✔
66
        result = self.pad_first + str(self.name) + self.pad_before_eq
10✔
67
        if self.value is not None:
10✔
68
            result += "=" + self.pad_after_eq
10✔
69
            if self.quotes:
10✔
70
                return result + self.quotes + str(self.value) + self.quotes
10✔
71
            return result + str(self.value)
10✔
72
        return result
10✔
73

74
    @staticmethod
10✔
75
    def _value_needs_quotes(value: Wikicode | None) -> str | None:
10✔
76
        """Return valid quotes for the given value, or None if unneeded."""
77
        if not value:
10✔
78
            return None
10✔
79
        val = "".join(str(node) for node in value.filter_text(recursive=False))
10✔
80
        if not any(char.isspace() for char in val):
10✔
81
            return None
10✔
82
        if "'" in val and '"' not in val:
10✔
83
            return '"'
10✔
84
        if '"' in val and "'" not in val:
10✔
85
            return "'"
10✔
86
        return "\"'"  # Either acceptable, " preferred over '
10✔
87

88
    def _set_padding(self, attr: str, value: str) -> None:
10✔
89
        """Setter for the value of a padding attribute."""
90
        if not value:
10✔
91
            setattr(self, attr, "")
10✔
92
        else:
93
            value = str(value)
10✔
94
            if not value.isspace():
10✔
95
                raise ValueError("padding must be entirely whitespace")
10✔
96
            setattr(self, attr, value)
10✔
97

98
    @staticmethod
10✔
99
    def coerce_quotes(quotes: Any) -> str | None:
10✔
100
        """Coerce a quote type into an acceptable value, or raise an error."""
101
        coerced_quotes = str(quotes) if quotes else None
10✔
102
        if coerced_quotes not in [None, '"', "'"]:
10✔
103
            raise ValueError(f"{quotes!r} is not a valid quote type")
10✔
104
        return coerced_quotes
10✔
105

106
    @property
10✔
107
    def name(self) -> Wikicode:
10✔
108
        """The name of the attribute as a :class:`.Wikicode` object."""
109
        return self._name
10✔
110

111
    @name.setter
10✔
112
    def name(self, value: Any) -> None:
10✔
113
        self._name = parse_anything(value)
10✔
114

115
    @property
10✔
116
    def value(self) -> Wikicode | None:
10✔
117
        """The value of the attribute as a :class:`.Wikicode` object."""
118
        return self._value
10✔
119

120
    @value.setter
10✔
121
    def value(self, newval: Any) -> None:
10✔
122
        if newval is None:
10✔
123
            self._value = None
10✔
124
        else:
125
            code = parse_anything(newval)
10✔
126
            quotes = self._value_needs_quotes(code)
10✔
127
            if quotes and (not self.quotes or self.quotes not in quotes):
10✔
128
                self._quotes = quotes[0]
10✔
129
            self._value = code
10✔
130

131
    @property
10✔
132
    def quotes(self) -> str | None:
10✔
133
        """How to enclose the attribute value. ``"``, ``'``, or ``None``."""
134
        return self._quotes
10✔
135

136
    @quotes.setter
10✔
137
    def quotes(self, value: Any) -> None:
10✔
138
        value = self.coerce_quotes(value)
10✔
139
        if not value and self._value_needs_quotes(self.value):
10✔
140
            raise ValueError("attribute value requires quotes")
10✔
141
        self._quotes = value
10✔
142

143
    @property
10✔
144
    def pad_first(self) -> str:
10✔
145
        """Spacing to insert right before the attribute."""
146
        return self._pad_first
10✔
147

148
    @pad_first.setter
10✔
149
    def pad_first(self, value: str) -> None:
10✔
150
        self._set_padding("_pad_first", value)
10✔
151

152
    @property
10✔
153
    def pad_before_eq(self) -> str:
10✔
154
        """Spacing to insert right before the equal sign."""
155
        return self._pad_before_eq
10✔
156

157
    @pad_before_eq.setter
10✔
158
    def pad_before_eq(self, value: str) -> None:
10✔
159
        self._set_padding("_pad_before_eq", value)
10✔
160

161
    @property
10✔
162
    def pad_after_eq(self) -> str:
10✔
163
        """Spacing to insert right after the equal sign."""
164
        return self._pad_after_eq
10✔
165

166
    @pad_after_eq.setter
10✔
167
    def pad_after_eq(self, value: str) -> None:
10✔
168
        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

© 2026 Coveralls, Inc