wpsync 1.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of wpsync might be problematic. Click here for more details.

wpsync/markup/dom.py ADDED
@@ -0,0 +1,313 @@
1
+ """A small HTML tree for converting and comparing post content.
2
+
3
+ Markup is parsed with html5lib, which follows the HTML parsing rules browsers
4
+ use, so the tree is what a reader's browser builds.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ import re
10
+ import xml.etree.ElementTree as ET
11
+ from dataclasses import dataclass, field
12
+
13
+ import html5lib
14
+
15
+ VOID_TAGS = frozenset(
16
+ "area base br col embed hr img input link meta param source track wbr".split()
17
+ )
18
+ RAW_TEXT_TAGS = frozenset({"script", "style"})
19
+ PRESERVED_WHITESPACE_TAGS = frozenset({"pre", "textarea", "script", "style"})
20
+ BLOCK_TAGS = frozenset(
21
+ """address article aside blockquote caption col colgroup dd details dialog div
22
+ dl dt fieldset figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr
23
+ li main menu nav ol p pre section summary table tbody td tfoot th thead tr
24
+ ul""".split()
25
+ )
26
+ """Elements browsers display as blocks, so whitespace around their edges is hidden."""
27
+ TAG_ALIASES = {"b": "strong", "i": "em"}
28
+ EDGE_SPACE_TAGS = frozenset({"strong", "em"})
29
+ EMPTY_FORMATTING_TAGS = frozenset({"strong", "em", "b", "i", "span"})
30
+ WHITESPACE = re.compile(r"[ \t\n\r\f]+")
31
+ HTML_SPACE = " \t\n\r\f"
32
+ LAYOUT_STYLE = re.compile(r"(?:^|;)\s*(?:white-space|display)\s*:", re.IGNORECASE)
33
+ NAMESPACE_PREFIXES = {
34
+ "http://www.w3.org/1999/xlink": "xlink",
35
+ "http://www.w3.org/XML/1998/namespace": "xml",
36
+ "http://www.w3.org/2000/xmlns/": "xmlns",
37
+ }
38
+
39
+
40
+ @dataclass
41
+ class Element:
42
+ tag: str
43
+ attrs: dict[str, str] = field(default_factory=dict)
44
+ children: list[Node] = field(default_factory=list)
45
+
46
+
47
+ @dataclass(frozen=True)
48
+ class Comment:
49
+ text: str
50
+
51
+
52
+ Node = Element | Comment | str
53
+
54
+ _PARSER = html5lib.HTMLParser(namespaceHTMLElements=False)
55
+
56
+
57
+ def _local_name(name: str) -> str:
58
+ if not name.startswith("{"):
59
+ return name
60
+ namespace, local = name[1:].split("}", 1)
61
+ prefix = NAMESPACE_PREFIXES.get(namespace)
62
+ return f"{prefix}:{local}" if prefix else local
63
+
64
+
65
+ def _append_node(nodes: list[Node], node: Node) -> None:
66
+ if isinstance(node, str) and nodes and isinstance(nodes[-1], str):
67
+ nodes[-1] += node
68
+ elif node != "":
69
+ nodes.append(node)
70
+
71
+
72
+ def _children(element: ET.Element) -> list[Node]:
73
+ nodes: list[Node] = []
74
+ _append_node(nodes, element.text or "")
75
+ for child in element:
76
+ if child.tag is ET.Comment:
77
+ nodes.append(Comment(child.text or ""))
78
+ else:
79
+ attrs = {_local_name(name): value for name, value in child.attrib.items()}
80
+ nodes.append(Element(_local_name(child.tag), attrs, _children(child)))
81
+ _append_node(nodes, child.tail or "")
82
+ return nodes
83
+
84
+
85
+ def parse_html(markup: str) -> list[Node]:
86
+ return _children(_PARSER.parseFragment(markup))
87
+
88
+
89
+ def _escape_text(text: str) -> str:
90
+ return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
91
+
92
+
93
+ def _escape_attribute(value: str) -> str:
94
+ return (
95
+ value.replace("&", "&amp;")
96
+ .replace('"', "&quot;")
97
+ .replace("<", "&lt;")
98
+ .replace("\n", "&#10;")
99
+ )
100
+
101
+
102
+ def start_tag(element: Element) -> str:
103
+ attributes = "".join(
104
+ f' {name}="{_escape_attribute(value)}"' for name, value in element.attrs.items()
105
+ )
106
+ return f"<{element.tag}{attributes}>"
107
+
108
+
109
+ def preserves_whitespace(element: Element) -> bool:
110
+ """Whether whitespace in and around the element may be displayed as written."""
111
+ return element.tag in PRESERVED_WHITESPACE_TAGS or bool(
112
+ LAYOUT_STYLE.search(element.attrs.get("style", ""))
113
+ )
114
+
115
+
116
+ def serialize(nodes: list[Node], *, preserve_whitespace: bool = False) -> str:
117
+ """Serialize without line breaks outside elements that preserve whitespace."""
118
+ out = []
119
+ for node in nodes:
120
+ if isinstance(node, str):
121
+ text = node if preserve_whitespace else node.replace("\n", " ")
122
+ out.append(_escape_text(text))
123
+ elif isinstance(node, Comment):
124
+ if node.text.strip(HTML_SPACE):
125
+ out.append(f"<!--{node.text}-->")
126
+ elif node.tag in VOID_TAGS:
127
+ out.append(start_tag(node)[:-1] + " />")
128
+ else:
129
+ preserve = preserve_whitespace or preserves_whitespace(node)
130
+ if node.tag in RAW_TEXT_TAGS:
131
+ inner = "".join(
132
+ child for child in node.children if isinstance(child, str)
133
+ )
134
+ else:
135
+ inner = serialize(node.children, preserve_whitespace=preserve)
136
+ out.append(f"{start_tag(node)}{inner}</{node.tag}>")
137
+ return "".join(out)
138
+
139
+
140
+ def to_html(nodes: list[Node]) -> str:
141
+ return serialize(trim_block_whitespace(nodes))
142
+
143
+
144
+ def is_block(node: Node) -> bool:
145
+ return (
146
+ isinstance(node, Element)
147
+ and node.tag in BLOCK_TAGS
148
+ and not LAYOUT_STYLE.search(node.attrs.get("style", ""))
149
+ )
150
+
151
+
152
+ def _breaks_line(node: Node | None) -> bool:
153
+ """Whitespace next to a block or a line break is not displayed."""
154
+ return is_block(node) or (isinstance(node, Element) and node.tag == "br")
155
+
156
+
157
+ def trim_block_whitespace(
158
+ nodes: list[Node], *, block_context: bool = True
159
+ ) -> list[Node]:
160
+ """Drop whitespace that cannot affect display.
161
+
162
+ Browsers do not display whitespace around block elements or at the start
163
+ and end of a line, including inside inline elements at those positions.
164
+ """
165
+ result: list[Node] = []
166
+ for node in nodes:
167
+ if isinstance(node, Element) and not preserves_whitespace(node):
168
+ children = trim_block_whitespace(
169
+ node.children, block_context=is_block(node)
170
+ )
171
+ node = Element(node.tag, node.attrs, children)
172
+ result.append(node)
173
+ result = _strip_after_breaks(result, block_context, leading=True)
174
+ result.reverse()
175
+ result = _strip_after_breaks(result, block_context, leading=False)
176
+ result.reverse()
177
+ return [node for node in result if not _empty_formatting(node)]
178
+
179
+
180
+ def _empty_formatting(node: Node) -> bool:
181
+ """Plain bold, italic, or span with nothing inside displays nothing."""
182
+ return (
183
+ isinstance(node, Element)
184
+ and node.tag in EMPTY_FORMATTING_TAGS
185
+ and not node.attrs
186
+ and not node.children
187
+ )
188
+
189
+
190
+ def _strip_after_breaks(
191
+ nodes: list[Node], block_context: bool, *, leading: bool
192
+ ) -> list[Node]:
193
+ kept: list[Node] = []
194
+ for node in nodes:
195
+ previous = kept[-1] if kept else None
196
+ if (previous is None and block_context) or _breaks_line(previous):
197
+ node = _strip_edge(node, leading=leading)
198
+ if node != "":
199
+ kept.append(node)
200
+ return kept
201
+
202
+
203
+ def _strip_edge(node: Node, *, leading: bool) -> Node:
204
+ if isinstance(node, str):
205
+ return node.lstrip(HTML_SPACE) if leading else node.rstrip(HTML_SPACE)
206
+ if (
207
+ not isinstance(node, Element)
208
+ or is_block(node)
209
+ or preserves_whitespace(node)
210
+ or not node.children
211
+ ):
212
+ return node
213
+ children = list(node.children)
214
+ index = 0 if leading else -1
215
+ children[index] = _strip_edge(children[index], leading=leading)
216
+ return Element(node.tag, node.attrs, [child for child in children if child != ""])
217
+
218
+
219
+ Canonical = tuple
220
+
221
+
222
+ def _canonical_attrs(attrs: dict[str, str], ignored_classes: frozenset[str]) -> tuple:
223
+ items = []
224
+ for name, value in attrs.items():
225
+ if name == "class":
226
+ classes = sorted(set(value.split()) - ignored_classes)
227
+ if not classes:
228
+ continue
229
+ value = " ".join(classes)
230
+ items.append((name, value))
231
+ return tuple(sorted(items))
232
+
233
+
234
+ def _canonical_children(
235
+ nodes: list[Node], ignored_classes: frozenset[str], preserve: bool
236
+ ) -> list[Canonical]:
237
+ out: list[Canonical] = []
238
+ for node in nodes:
239
+ if isinstance(node, str):
240
+ _append_text(out, node if preserve else WHITESPACE.sub(" ", node))
241
+ elif isinstance(node, Comment):
242
+ # Comments are never displayed; only their words matter.
243
+ text = WHITESPACE.sub(" ", node.text).strip()
244
+ if text:
245
+ out.append(("comment", text))
246
+ else:
247
+ tag = TAG_ALIASES.get(node.tag, node.tag)
248
+ attrs = _canonical_attrs(node.attrs, ignored_classes)
249
+ inner = _canonical_children(
250
+ node.children, ignored_classes, preserve or preserves_whitespace(node)
251
+ )
252
+ if tag == "pre":
253
+ inner = _trim_trailing_newline(inner)
254
+ if tag == "span" and not attrs:
255
+ for child in inner:
256
+ if child[0] == "text":
257
+ _append_text(out, child[1])
258
+ else:
259
+ out.append(child)
260
+ elif tag in EDGE_SPACE_TAGS and not attrs and not preserve:
261
+ lead, inner, trail = _edge_spaces(inner)
262
+ _append_text(out, lead)
263
+ out.append(("element", tag, attrs, tuple(inner)))
264
+ _append_text(out, trail)
265
+ else:
266
+ out.append(("element", tag, attrs, tuple(inner)))
267
+ return out
268
+
269
+
270
+ def _append_text(out: list[Canonical], text: str) -> None:
271
+ if not text:
272
+ return
273
+ if out and out[-1][0] == "text":
274
+ out[-1] = ("text", out[-1][1] + text)
275
+ else:
276
+ out.append(("text", text))
277
+
278
+
279
+ def _edge_spaces(inner: list[Canonical]) -> tuple[str, list[Canonical], str]:
280
+ """Spaces at the edges of plain bold or italic text display as if outside it."""
281
+ inner = list(inner)
282
+ lead = trail = ""
283
+ if inner and inner[0][0] == "text" and inner[0][1].startswith(" "):
284
+ lead = " "
285
+ inner[0] = ("text", inner[0][1].lstrip(" "))
286
+ if inner and inner[-1][0] == "text" and inner[-1][1].endswith(" "):
287
+ trail = " "
288
+ inner[-1] = ("text", inner[-1][1].rstrip(" "))
289
+ return lead, [node for node in inner if node != ("text", "")], trail
290
+
291
+
292
+ def _trim_trailing_newline(children: list[Canonical]) -> list[Canonical]:
293
+ """A single line break at the very end of `<pre>` is not displayed."""
294
+ if not children:
295
+ return children
296
+ children = list(children)
297
+ last = children[-1]
298
+ if last[0] == "text":
299
+ text = last[1].removesuffix("\n")
300
+ children[-1] = ("text", text)
301
+ return [child for child in children if child != ("text", "")]
302
+ if last[0] == "element":
303
+ children[-1] = (*last[:3], tuple(_trim_trailing_newline(list(last[3]))))
304
+ return children
305
+
306
+
307
+ def canonical(
308
+ nodes: list[Node], ignored_classes: frozenset[str] = frozenset()
309
+ ) -> Canonical:
310
+ """A comparable form that is equal when two trees display the same."""
311
+ return tuple(
312
+ _canonical_children(trim_block_whitespace(nodes), ignored_classes, False)
313
+ )