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

earwig / mwparserfromhell / 6080278913

05 Sep 2023 04:44AM UTC coverage: 99.136% (+0.04%) from 99.098%
6080278913

push

github

earwig
Fix pickling SmartLists (fixes #289)

23 of 23 new or added lines in 4 files covered. (100.0%)

2982 of 3008 relevant lines covered (99.14%)

6.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
7✔
23
from ...utils import parse_anything
7✔
24

25
__all__ = ["Attribute"]
7✔
26

27

28
class Attribute(StringMixIn):
7✔
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__(
7✔
37
        self,
38
        name,
39
        value=None,
40
        quotes='"',
41
        pad_first=" ",
42
        pad_before_eq="",
43
        pad_after_eq="",
44
    ):
45
        super().__init__()
7✔
46
        self.name = name
7✔
47
        self._quotes = None
7✔
48
        self.value = value
7✔
49
        self.quotes = quotes
7✔
50
        self.pad_first = pad_first
7✔
51
        self.pad_before_eq = pad_before_eq
7✔
52
        self.pad_after_eq = pad_after_eq
7✔
53

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

155
    @pad_after_eq.setter
7✔
156
    def pad_after_eq(self, value):
6✔
157
        self._set_padding("_pad_after_eq", value)
7✔
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