justhtml 0.12.0__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 justhtml might be problematic. Click here for more details.

@@ -0,0 +1,93 @@
1
+ from __future__ import annotations
2
+
3
+ import enum
4
+ from typing import TYPE_CHECKING
5
+
6
+ from .constants import (
7
+ HTML4_PUBLIC_PREFIXES,
8
+ LIMITED_QUIRKY_PUBLIC_PREFIXES,
9
+ QUIRKY_PUBLIC_MATCHES,
10
+ QUIRKY_PUBLIC_PREFIXES,
11
+ QUIRKY_SYSTEM_MATCHES,
12
+ )
13
+
14
+ if TYPE_CHECKING:
15
+ from .tokens import Doctype
16
+
17
+
18
+ class InsertionMode(enum.IntEnum):
19
+ INITIAL = 0
20
+ BEFORE_HTML = 1
21
+ BEFORE_HEAD = 2
22
+ IN_HEAD = 3
23
+ IN_HEAD_NOSCRIPT = 4
24
+ AFTER_HEAD = 5
25
+ TEXT = 6
26
+ IN_BODY = 7
27
+ AFTER_BODY = 8
28
+ AFTER_AFTER_BODY = 9
29
+ IN_TABLE = 10
30
+ IN_TABLE_TEXT = 11
31
+ IN_CAPTION = 12
32
+ IN_COLUMN_GROUP = 13
33
+ IN_TABLE_BODY = 14
34
+ IN_ROW = 15
35
+ IN_CELL = 16
36
+ IN_FRAMESET = 17
37
+ AFTER_FRAMESET = 18
38
+ AFTER_AFTER_FRAMESET = 19
39
+ IN_SELECT = 20
40
+ IN_TEMPLATE = 21
41
+
42
+
43
+ def is_all_whitespace(text: str) -> bool:
44
+ return text.strip("\t\n\f\r ") == ""
45
+
46
+
47
+ def contains_prefix(haystack: tuple[str, ...], needle: str) -> bool:
48
+ return any(needle.startswith(prefix) for prefix in haystack)
49
+
50
+
51
+ def doctype_error_and_quirks(doctype: Doctype, iframe_srcdoc: bool = False) -> tuple[bool, str]:
52
+ name = doctype.name.lower() if doctype.name else None
53
+ public_id = doctype.public_id
54
+ system_id = doctype.system_id
55
+
56
+ acceptable: tuple[tuple[str | None, str | None, str | None], ...] = (
57
+ ("html", None, None),
58
+ ("html", None, "about:legacy-compat"),
59
+ ("html", "-//W3C//DTD HTML 4.0//EN", None),
60
+ ("html", "-//W3C//DTD HTML 4.0//EN", "http://www.w3.org/TR/REC-html40/strict.dtd"),
61
+ ("html", "-//W3C//DTD HTML 4.01//EN", None),
62
+ ("html", "-//W3C//DTD HTML 4.01//EN", "http://www.w3.org/TR/html4/strict.dtd"),
63
+ ("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"),
64
+ ("html", "-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"),
65
+ )
66
+
67
+ key = (name, public_id, system_id)
68
+ parse_error = key not in acceptable
69
+
70
+ public_lower = public_id.lower() if public_id else None
71
+ system_lower = system_id.lower() if system_id else None
72
+
73
+ quirks_mode: str
74
+ if doctype.force_quirks:
75
+ quirks_mode = "quirks"
76
+ elif iframe_srcdoc:
77
+ quirks_mode = "no-quirks"
78
+ elif name != "html":
79
+ quirks_mode = "quirks"
80
+ elif public_lower in QUIRKY_PUBLIC_MATCHES:
81
+ quirks_mode = "quirks"
82
+ elif system_lower in QUIRKY_SYSTEM_MATCHES:
83
+ quirks_mode = "quirks"
84
+ elif public_lower and contains_prefix(QUIRKY_PUBLIC_PREFIXES, public_lower):
85
+ quirks_mode = "quirks"
86
+ elif public_lower and contains_prefix(LIMITED_QUIRKY_PUBLIC_PREFIXES, public_lower):
87
+ quirks_mode = "limited-quirks"
88
+ elif public_lower and contains_prefix(HTML4_PUBLIC_PREFIXES, public_lower):
89
+ quirks_mode = "quirks" if system_lower is None else "limited-quirks"
90
+ else:
91
+ quirks_mode = "no-quirks"
92
+
93
+ return parse_error, quirks_mode
@@ -0,0 +1,164 @@
1
+ Metadata-Version: 2.4
2
+ Name: justhtml
3
+ Version: 0.12.0
4
+ Summary: A pure Python HTML5 parser that just works.
5
+ Project-URL: Homepage, https://github.com/emilstenstrom/justhtml
6
+ Project-URL: Issues, https://github.com/emilstenstrom/justhtml/issues
7
+ Author-email: Emil Stenström <emil@emilstenstrom.se>
8
+ License-File: LICENSE
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Programming Language :: Python :: 3
12
+ Requires-Python: >=3.10
13
+ Provides-Extra: benchmark
14
+ Requires-Dist: beautifulsoup4; extra == 'benchmark'
15
+ Requires-Dist: html5-parser; extra == 'benchmark'
16
+ Requires-Dist: html5lib; extra == 'benchmark'
17
+ Requires-Dist: lxml; extra == 'benchmark'
18
+ Requires-Dist: psutil; extra == 'benchmark'
19
+ Requires-Dist: selectolax; extra == 'benchmark'
20
+ Requires-Dist: zstandard; extra == 'benchmark'
21
+ Provides-Extra: dev
22
+ Requires-Dist: build; extra == 'dev'
23
+ Requires-Dist: coverage; extra == 'dev'
24
+ Requires-Dist: mypy>=1.0; (platform_python_implementation != 'PyPy') and extra == 'dev'
25
+ Requires-Dist: pre-commit; extra == 'dev'
26
+ Requires-Dist: ruff==0.14.7; extra == 'dev'
27
+ Requires-Dist: twine; extra == 'dev'
28
+ Description-Content-Type: text/markdown
29
+
30
+ # JustHTML
31
+
32
+ A pure Python HTML5 parser that just works. No C extensions to compile. No system dependencies to install. No complex API to learn.
33
+
34
+ **[📖 Read the full documentation here](docs/index.md)**
35
+
36
+ ## Why use JustHTML?
37
+
38
+ ### 1. Just... Correct ✅
39
+ It implements the official WHATWG HTML5 specification exactly. If a browser can parse it, JustHTML can parse it. It handles all the complex error-handling rules that browsers use.
40
+
41
+ - **Verified Compliance**: Passes all 9k+ tests in the official [html5lib-tests](https://github.com/html5lib/html5lib-tests) suite (used by browser vendors).
42
+ - **100% Coverage**: Every line and branch of code is covered by integration tests.
43
+ - **Fuzz Tested**: Has parsed 6 million randomized broken HTML documents to ensure it never crashes or hangs (see benchmarks/fuzz.py).
44
+ - **Living Standard**: It tracks the living standard, not a snapshot from 2012.
45
+
46
+ ### 2. Just... Python 🐍
47
+ JustHTML has **zero dependencies**. It's pure Python.
48
+
49
+ - **Just Install**: No C extensions to compile, no system libraries (like libxml2) required. Works on PyPy, WASM (Pyodide) (yes, it's in the test matrix), and anywhere Python runs.
50
+ - **No dependency upgrade hassle**: Some libraries depend on a large set of libraries, all which require upgrades to avoid security issues.
51
+ - **Debuggable**: It's just Python code. You can step through it with a debugger to understand exactly how your HTML is being parsed.
52
+ - **Returns plain python objects**: Other parsers return lxml or etree trees which means you have another API to learn. JustHTML returns a set of nested objects you can iterate over. Simple.
53
+
54
+ ### 3. Just... Query 🔍
55
+ Find elements with CSS selectors. Just one method to learn - `query()` - and it uses CSS syntax you already know.
56
+
57
+ ```python
58
+ doc.query("div.container > p.intro") # Familiar CSS syntax
59
+ doc.query("#main, .sidebar") # Selector groups
60
+ doc.query("li:nth-child(2n+1)") # Pseudo-classes
61
+ ```
62
+
63
+ ### 4. Just... Fast Enough ⚡
64
+
65
+ If you need to parse terabytes of data, use a C or Rust parser (like `html5ever`). They are 10x-20x faster.
66
+
67
+ But for most use cases, JustHTML is **fast enough**. It parses the Wikipedia homepage in ~0.1s. It is the fastest pure-Python HTML5 parser available, outperforming `html5lib` and `BeautifulSoup`.
68
+
69
+ ## Comparison to other parsers
70
+
71
+ | Parser | HTML5 Compliance | Pure Python? | Speed | Query API | Notes |
72
+ |--------|:----------------:|:------------:|-------|-----------|-------|
73
+ | **JustHTML** | ✅ **100%** | ✅ Yes | ⚡ Fast | ✅ CSS selectors | It just works. Correct, easy to install, and fast enough. |
74
+ | `html5lib` | 🟡 88% | ✅ Yes | 🐢 Slow | ❌ None | The reference implementation. Very correct but quite slow. |
75
+ | `html5_parser` | 🟡 84% | ❌ No | 🚀 Very Fast | 🟡 XPath (lxml) | C-based (Gumbo). Fast and mostly correct. |
76
+ | `selectolax` | 🟡 68% | ❌ No | 🚀 Very Fast | ✅ CSS selectors | C-based (Lexbor). Very fast but less compliant. |
77
+ | `BeautifulSoup` | 🔴 4% | ✅ Yes | 🐢 Slow | 🟡 Custom API | Wrapper around `html.parser`. Not spec compliant. |
78
+ | `html.parser` | 🔴 4% | ✅ Yes | ⚡ Fast | ❌ None | Standard library. Chokes on malformed HTML. |
79
+ | `lxml` | 🔴 1% | ❌ No | 🚀 Very Fast | 🟡 XPath | C-based (libxml2). Fast but not HTML5 compliant. |
80
+
81
+ *Compliance scores from running the [html5lib-tests](https://github.com/html5lib/html5lib-tests) suite (1,743 tree-construction tests). See `benchmarks/correctness.py`.*
82
+
83
+ ## Installation
84
+
85
+ Requires Python 3.10 or later.
86
+
87
+ ```bash
88
+ pip install justhtml
89
+ ```
90
+
91
+ ## Quick Example
92
+
93
+ ```python
94
+ from justhtml import JustHTML
95
+
96
+ doc = JustHTML("<html><body><p class='intro'>Hello!</p></body></html>")
97
+
98
+ # Query with CSS selectors
99
+ for p in doc.query("p.intro"):
100
+ print(p.name) # "p"
101
+ print(p.attrs) # {"class": "intro"}
102
+ print(p.to_html()) # <p class="intro">Hello!</p>
103
+ ```
104
+
105
+ See the **[Quickstart Guide](docs/quickstart.md)** for more examples including tree traversal, streaming, and strict mode.
106
+
107
+ ## Command Line
108
+
109
+ If you installed JustHTML (for example with `pip install justhtml` or `pip install -e .`), you can use the `justhtml` command.
110
+ If you don't have it available, use the equivalent `python -m justhtml ...` form instead.
111
+
112
+ ```bash
113
+ # Pretty-print an HTML file
114
+ justhtml index.html
115
+
116
+ # Parse from stdin
117
+ curl -s https://example.com | justhtml -
118
+
119
+ # Select nodes and output text
120
+ justhtml index.html --selector "main p" --format text
121
+
122
+ # Select nodes and output Markdown (subset of GFM)
123
+ justhtml index.html --selector "article" --format markdown
124
+
125
+ # Select nodes and output HTML
126
+ justhtml index.html --selector "a" --format html
127
+ ```
128
+
129
+ ```bash
130
+ # Example: extract Markdown from GitHub README HTML
131
+ curl -s https://github.com/EmilStenstrom/justhtml/ | justhtml - --selector '.markdown-body' --format markdown | head -n 15
132
+ ```
133
+
134
+ Output:
135
+
136
+ ```text
137
+ # JustHTML
138
+
139
+ [](#justhtml)
140
+
141
+ A pure Python HTML5 parser that just works. No C extensions to compile. No system dependencies to install. No complex API to learn.
142
+
143
+ **[📖 Read the full documentation here](/EmilStenstrom/justhtml/blob/main/docs/index.md)**
144
+
145
+ ## Why use JustHTML?
146
+
147
+ [](#why-use-justhtml)
148
+
149
+ ### 1. Just... Correct ✅
150
+
151
+ [](#1-just-correct-)
152
+ ```
153
+
154
+ ## Contributing
155
+
156
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.
157
+
158
+ ## Acknowledgments
159
+
160
+ JustHTML started as a Python port of [html5ever](https://github.com/servo/html5ever), the HTML5 parser from Mozilla's Servo browser engine. While the codebase has since evolved significantly, html5ever's clean architecture and spec-compliant approach were invaluable as a starting point. Thank you to the Servo team for their excellent work.
161
+
162
+ ## License
163
+
164
+ MIT. Free to use both for commercial and non-commercial use.
@@ -0,0 +1,23 @@
1
+ justhtml/__init__.py,sha256=rsc4X1uTsJziqKtZxWQsIqwuC5DI0cvfYw5q_FtEOCo,375
2
+ justhtml/__main__.py,sha256=o-ur6qdyt8EmYDKjCP3waHjuvmD38j6mpAx-CJZtAXs,3876
3
+ justhtml/constants.py,sha256=-UATvXXQ7ueFWxJHW79c2eMmMWaSKoqwwcNIGesTAj0,11603
4
+ justhtml/context.py,sha256=Ac4mV-a3ZgJILQbstFu-EB6bRA5oYlSkHqpTxMlMfk0,293
5
+ justhtml/encoding.py,sha256=9mscoXtBb57zehG_BxzN6aTTJHaNfywk5gwxrnH92K8,11310
6
+ justhtml/entities.py,sha256=qLijZDS2n6Cc8UqbVZYNv2XzKVahysFBlAbyHkerD7c,9808
7
+ justhtml/errors.py,sha256=kt8uA9pH43PEiL2ccrV6zJMXK5iZvbtDtIhIWIzS8FU,10004
8
+ justhtml/node.py,sha256=NEDQzD7VBuch-BIZixNIUyZNCHydnThF_eSwXeA5OuA,19056
9
+ justhtml/parser.py,sha256=S_mbXd3YcNGJSZYR_34wkI1mW6SZNQw5d0_LOtjHCRM,4628
10
+ justhtml/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ justhtml/selector.py,sha256=_L9XmJTVJVuAZF12B_nex9iytR4a_xlkIePwUVVvHUk,32884
12
+ justhtml/serialize.py,sha256=Cezl3KVy9RT4sPFr6QJmtWiVjwAvwZSuBoRUP-E3yKM,8606
13
+ justhtml/stream.py,sha256=n8pKtVAivG0VerCWEcXSEBwzj8Tm1ltEAL7F46RGUVM,3431
14
+ justhtml/tokenizer.py,sha256=auDP8svHMlB0PxohVcq3VxhN3WjXUs2A8ckHN78SjKc,100623
15
+ justhtml/tokens.py,sha256=WNGpQ6nHQECY7J8jBvs3XAP2Lxy_xExxRMlqBn_MexI,6836
16
+ justhtml/treebuilder.py,sha256=Mjhyp3Ral5ACCGDi4GL-clHeg65TUrQdoiW2ZunZqNY,55606
17
+ justhtml/treebuilder_modes.py,sha256=VVAbHWbLq1DP3KxNYQa2m2DjJ5jEVxBufV7eSTE_gfA,94040
18
+ justhtml/treebuilder_utils.py,sha256=LjK9tg9sNYR-sJdXKemJCzzzgh6lQW1KBqyvhpWtaoQ,2912
19
+ justhtml-0.12.0.dist-info/METADATA,sha256=8Ex7MpHIhbJaGJvyOvIkEvMBS48K4z4zTlm7_nRTPkk,6922
20
+ justhtml-0.12.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
21
+ justhtml-0.12.0.dist-info/entry_points.txt,sha256=UN06mPn7J0cBM1dqyf245FvmU9mF3ivgplSr5ppdp6g,52
22
+ justhtml-0.12.0.dist-info/licenses/LICENSE,sha256=jM1KAJ1VQZAo7SCGVK1jtVj11zgIc5_BxZAUhXq01V8,1072
23
+ justhtml-0.12.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.28.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ justhtml = justhtml.__main__:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Emil Stenström
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.