|
2 | 2 |
|
3 | 3 | from typing import TypeVar, cast, overload |
4 | 4 |
|
5 | | -from htmltools import Tag, TagAttrs, TagAttrValue, TagChild, div |
| 5 | +from htmltools import TagAttrs, TagAttrValue, TagChild, div |
6 | 6 |
|
7 | 7 | TagChildT = TypeVar("TagChildT", bound=TagChild) |
8 | 8 |
|
@@ -60,104 +60,3 @@ def trinary(x: bool | str | None) -> None | str: |
60 | 60 | return "true" |
61 | 61 | else: |
62 | 62 | return "false" |
63 | | - |
64 | | - |
65 | | -TagT = TypeVar("TagT", bound="Tag") |
66 | | - |
67 | | -# Tag.add_class(x: str) -> Self[Tag]: |
68 | | -# cls = self.attrs.get("class") |
69 | | -# if cls: |
70 | | -# x = cls + " " + x |
71 | | -# self.attrs["class"] = x |
72 | | -# return self |
73 | | - |
74 | | - |
75 | | -# Do not export |
76 | | -# "Prepend" version of `tag.add_class(class_)` |
77 | | -def tag_prepend_class(tag: TagT, *class_: str | None) -> TagT: |
78 | | - classes = ( |
79 | | - *class_, |
80 | | - tag.attrs.get("class"), |
81 | | - ) |
82 | | - classes = [c for c in classes if c is not None] |
83 | | - if len(classes) == 0: |
84 | | - return tag |
85 | | - tag.attrs["class"] = " ".join(classes) |
86 | | - return tag |
87 | | - |
88 | | - |
89 | | -def tag_remove_class(tag: TagT, *class_: str | None) -> TagT: |
90 | | - """ |
91 | | - Remove a class value from the HTML class attribute. |
92 | | -
|
93 | | - Parameters |
94 | | - ---------- |
95 | | - *class_ |
96 | | - The class name to remove. |
97 | | -
|
98 | | - Returns |
99 | | - ------- |
100 | | - : |
101 | | - The modified tag. |
102 | | - """ |
103 | | - cls = tag.attrs.get("class") |
104 | | - # If no class values to remove from, quit |
105 | | - if not cls: |
106 | | - return tag |
107 | | - |
108 | | - # Remove any `None` values |
109 | | - set_to_remove = {c for c in class_ if c is not None} |
110 | | - |
111 | | - # If no classes to remove, quit |
112 | | - if len(set_to_remove) == 0: |
113 | | - return tag |
114 | | - |
115 | | - # Get new set of classes |
116 | | - # Order matters, otherwise we could use `set()` subtraction: `set(cls.split(" ")) - set(class_)` |
117 | | - new_cls: list[str] = [] |
118 | | - for cls_val in cls.split(" "): |
119 | | - if cls_val not in set_to_remove: |
120 | | - new_cls.append(cls_val) |
121 | | - |
122 | | - # If no classes left, remove the attribute |
123 | | - if len(new_cls) == 0: |
124 | | - # If here, `attrs.class` must exist |
125 | | - tag.attrs.pop("class") |
126 | | - return tag |
127 | | - |
128 | | - # Otherwise, set the new class |
129 | | - tag.attrs["class"] = " ".join(new_cls) |
130 | | - return tag |
131 | | - |
132 | | - |
133 | | -def tag_add_style( |
134 | | - tag: TagT, |
135 | | - *style: str | None, |
136 | | -) -> TagT: |
137 | | - """ |
138 | | - Add a style value(s) to the HTML style attribute. |
139 | | -
|
140 | | - Parameters |
141 | | - ---------- |
142 | | - *style |
143 | | - CSS properties and values already properly formatted. Each should already contain trailing semicolons. |
144 | | -
|
145 | | - See Also |
146 | | - -------- |
147 | | - ~htmltools.css |
148 | | -
|
149 | | - Returns |
150 | | - ------- |
151 | | - : |
152 | | - The modified tag. |
153 | | - """ |
154 | | - styles = ( |
155 | | - tag.attrs.get("style"), |
156 | | - *style, |
157 | | - ) |
158 | | - non_none_style_tuple = (s for s in styles if s is not None) |
159 | | - style_str = "".join(non_none_style_tuple) |
160 | | - |
161 | | - if style_str: |
162 | | - tag.attrs["style"] = style_str |
163 | | - return tag |
0 commit comments