library to compare HTML while ignoring non-functional differences
Project description
htmlcompare
A Python library to ensure two HTML documents are "equal". Currently the functionality is very limited but the idea is that the library should ignore differences automatically when these are not relevant for HTML semantics (e.g. <img style="">
is the same as <img>
, style="color: black; font-weight: bold"
is equal to style="font-weight:bold;color:black;"
).
Usage
import htmlcompare
diff = htmlcompare.compare('<div>', '<p>')
is_same = bool(diff)
To ease testing the library provides some helpers
from htmlcompare import assert_different_html, assert_same_html
assert_different_html('<br>', '<p>')
assert_same_html('<div />', '<div></div>')
Implemented Features
- ignores whitespace between HTML tags
<div />
is treated like<div></div>
- ordering of HTML attributes does not matter:
<div class="…" style="…" />
is treated equal to<div style="…" class="…" />
- HTML comments are ignored (yes, also conditional comments unfortunately)
- ordering of CSS classes inside
class
attribute does not matter:<div class="foo bar" />
is the same as<div class="bar foo" />
. - a
style
orclass
attribute with empty content (e.g.style=""
) is considered the same as an absentstyle
/class
attribute. - inline style declarations are parsed with an actual CSS parser: ordering, whitespace and trailing semicolons do not matter (Python 3.5+ only)
Limitations / Plans
Only basic CSS support. Declarations in style
attributes are parsed with tinycss2 (Python 3.5+) so ordering of declarations and extra whitespace should not matter. tinycss2
does not support Python 2 and 3.4 so the only help here is to strip trailing ;
s in style
attributes. Contents of <style>
tags are completely ignored for now (even with tinycss2
).
No validation of conditional comments. Not sure which library I can use here but at some point I'll likely need this as well.
JavaScript - for obvious reasons it will be impossible to implement perfect JS comparison but it might be possible to run some kind of "beautifier" to take care of insignificant stylistic changes. However I don't need this feature so this is unlikely to get implemented (unless contributed by someone else).
Custom hooks could help adapting the comparison to your specific needs. However I don't know which API would be best so this will wait until there are real-world use cases.
Better API: The current API is very minimal and implements just what I needed right now. I hope to improve the API once I use this project in more complex scenarios.
Other projects
xmldiff is a well established project to compare two XML documents. However it seems as if the code does not contain knowledge about specific HTML semantics (e.g. CSS, empty attributes, insignificant attribute order).
Misc
The code is licensed under the MIT license. It supports Python 2.7 and Python 3.4+ though some features are only available for Python 3.5+.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file HTMLCompare-0.3.1.tar.gz
.
File metadata
- Download URL: HTMLCompare-0.3.1.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.3 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3a1750d0e00d6e615d0093f29842fa9a18b5857ebb580f0774774e9a7d05850 |
|
MD5 | 53e35f9957e4ee6e98c30ced708ca399 |
|
BLAKE2b-256 | 9af2d3e697e3ec28299ae5c6c905dac4705493fcdcbf3357e5f475d952b87ac5 |
File details
Details for the file HTMLCompare-0.3.1-py2.py3-none-any.whl
.
File metadata
- Download URL: HTMLCompare-0.3.1-py2.py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.1.3 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54e1de87e0fbd7b1dc16d24ee222f1ddde928ce6696f1ea62307ef1b0fe4d68c |
|
MD5 | 10d5b79ffd72c6ff0d53ac7a9e71157d |
|
BLAKE2b-256 | 4945dce26992f4f1f6d957503d801059684bf1420c35388a726520bfb7328c7a |