Skip to content

Modernizing difflib.HtmlDiff for HTML Output #134580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Wulian233 opened this issue May 23, 2025 · 0 comments
Closed

Modernizing difflib.HtmlDiff for HTML Output #134580

Wulian233 opened this issue May 23, 2025 · 0 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@Wulian233
Copy link
Contributor

Wulian233 commented May 23, 2025

Feature or enhancement

Proposal:

Previously, I added dark mode support to the HTML export feature of difflib, and during that process, I uncovered several issues.

By opening the browser's developer tools (F12) and inspecting the generated HTML, you can observe a number of warnings, such as:

Image

I initially addressed these warnings in the same pull request that added dark mode support. However, we later decided it would be better to split these changes into a separate PR—each PR should serve a single purpose. As a result, the warning fixes were rolled back from that submission. (see #129940 (comment))

Recently, I resumed working on this issue. I discovered that a large portion of these browser warnings stem from the legacy nature of the generated HTML. For example, the first line of the html still uses:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

And elements like:

<meta http-equiv="Content-Type"
      content="text/html; charset=%(charset)s" />

These are artifacts from the HTML4 era—a specification that was finalized more than two decades ago. While HTML5 remains backward-compatible with this syntax, I believe updating to modern HTML5 conventions is the right move. In 2025, there's no need to worry about browsers lacking HTML5 support.

For instance, HTML5 removes the need for lengthy DTD declarations (see here), simplifying the doctype to just <!DOCTYPE html>. This also makes the resulting HTML cleaner and easier to maintain.

With that in mind, I’m proposing to modernize difflib.HtmlDiff, not just updating the HTML structure to HTML5, but also refining the CSS for improved styling.

Take the current layout, for example: it’s not easy to distinguish line numbers from content maybe it's not very user-friendly.

Image

In the revised version, the diff content has been slightly enlarged for better readability. Line numbers are bolded and given more horizontal space, preventing them from blending into the content and causing visual confusion.

Image

Additionally, the legend section has been visually enhanced—now more intuitive and aesthetically pleasing. And, of course, all browser warnings have been eliminated!

Before: Image
After: Image

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@Wulian233 Wulian233 added the type-feature A feature request or enhancement label May 23, 2025
@hugovk hugovk added the stdlib Python modules in the Lib dir label May 23, 2025
hugovk added a commit that referenced this issue May 28, 2025
@hugovk hugovk closed this as completed May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants