Skip to main content

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 or class attribute with empty content (e.g. style="") is considered the same as an absent style/class attribute.
  • inline style declarations are parsed with an actual CSS parser: ordering, whitespace and trailing semicolons do not matter (Python 3.5+ only)
  • 0px is considered equal to 0 in inline CSS.

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+.

Metadata

Release files for HTMLCompare 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for HTMLCompare 0.3.2
File Size Uploaded Provenance Actions
HTMLCompare-0.3.2.tar.gz 8.6 kB Without provenance Details for HTMLCompare-0.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for HTMLCompare 0.3.2
File Interpreter ABI Platform Actions
HTMLCompare-0.3.2-py2.py3-none-any.whl Python 2, Python 3 none any Details for HTMLCompare-0.3.2-py2.py3-none-any.whl

Total release size: 20.0 kB

Release files / HTMLCompare-0.3.2.tar.gz

Download URL HTMLCompare-0.3.2.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6cd2fc939367ead8f33c3f3939f33dfb1eb073c7c486fa348125689176b97f06
BLAKE2b-256 checksum
How to use checksums
fdc2afc180f71f8b51ed3d954f45ce26ada0c1bb18a3f0f85386c8355e7a2c73
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.3

Release files / HTMLCompare-0.3.2-py2.py3-none-any.whl

Download URL HTMLCompare-0.3.2-py2.py3-none-any.whl
Size 11.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
def6b62ffb29dc76f3e7cc8f82538127990fd4b8025921fac6e3542e8ca023bf
BLAKE2b-256 checksum
How to use checksums
4b90251243d39c5f8c3c0b46ed489992b53d987cd7261b847347ad768c32c932
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.3

Security for HTMLCompare 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page