From c0224357fe29fa7ccca04b34fb35ce8412f92c62 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 23 May 2025 20:02:25 +0800 Subject: [PATCH 1/4] Improved the styling of HTML diff pages generated by the `difflib.HtmlDiff` --- Doc/whatsnew/3.15.rst | 7 +++ Lib/difflib.py | 50 +++++++++++++------ Lib/test/test_difflib.py | 6 +-- Lib/test/test_difflib_expect.html | 48 +++++++++++++----- ...-05-23-20-01-52.gh-issue-134580.xnaJ70.rst | 3 ++ 5 files changed, 84 insertions(+), 30 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-05-23-20-01-52.gh-issue-134580.xnaJ70.rst diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst index bf186c191b04d1..2d4f7898e79b0c 100644 --- a/Doc/whatsnew/3.15.rst +++ b/Doc/whatsnew/3.15.rst @@ -89,6 +89,13 @@ New modules Improved modules ================ +difflib +------- + +* Improved the styling of HTML diff pages generated by the :class:`difflib.HtmlDiff` + class, and migrated the output to the HTML5 standard. + (Contributed by Jiahao Li in :gh:`134580`.) + ssl --- diff --git a/Lib/difflib.py b/Lib/difflib.py index f1f4e62514a7bd..dd28c26c5a97c1 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -1615,16 +1615,13 @@ def _line_pair_iterator(): _file_template = """ - - - - + + - - - @@ -1636,13 +1633,36 @@ def _line_pair_iterator(): _styles = """ :root {color-scheme: light dark} - table.diff {font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; border:medium} - .diff_header {background-color:#e0e0e0} - td.diff_header {text-align:right} - .diff_next {background-color:#c0c0c0} + table.diff { + font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; + border:medium; + } + .diff_header { + background-color:#e0e0e0; + font-weight:bold; + } + td.diff_header { + text-align:right; + padding:4px 8px; + } + .diff_next { + background-color:#c0c0c0; + padding:4px 0; + } .diff_add {background-color:palegreen} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa} + table.diff[summary="Legends"] { + margin-top: 20px; + border: 1px solid #ccc; + } + table.diff[summary="Legends"] th { + background-color: #e0e0e0; + padding: 4px 8px; + } + table.diff[summary="Legends"] td { + padding: 4px 8px; + } @media (prefers-color-scheme: dark) { .diff_header {background-color:#666} @@ -1650,6 +1670,8 @@ def _line_pair_iterator(): .diff_add {background-color:darkgreen} .diff_chg {background-color:#847415} .diff_sub {background-color:darkred} + table.diff[summary="Legends"] {border-color:#555} + table.diff[summary="Legends"] th{background-color:#666} }""" _table_template = """ @@ -1692,7 +1714,7 @@ class HtmlDiff(object): make_table -- generates HTML for a single side by side table make_file -- generates complete HTML file with a single side by side table - See tools/scripts/diff.py for an example usage of this class. + See Doc/includes/diff.py for an example usage of this class. """ _file_template = _file_template diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py index 9e217249be7332..6ac584a08d1e86 100644 --- a/Lib/test/test_difflib.py +++ b/Lib/test/test_difflib.py @@ -255,21 +255,21 @@ def test_make_file_default_charset(self): html_diff = difflib.HtmlDiff() output = html_diff.make_file(patch914575_from1.splitlines(), patch914575_to1.splitlines()) - self.assertIn('content="text/html; charset=utf-8"', output) + self.assertIn('charset="utf-8"', output) def test_make_file_iso88591_charset(self): html_diff = difflib.HtmlDiff() output = html_diff.make_file(patch914575_from1.splitlines(), patch914575_to1.splitlines(), charset='iso-8859-1') - self.assertIn('content="text/html; charset=iso-8859-1"', output) + self.assertIn('charset="iso-8859-1"', output) def test_make_file_usascii_charset_with_nonascii_input(self): html_diff = difflib.HtmlDiff() output = html_diff.make_file(patch914575_nonascii_from1.splitlines(), patch914575_nonascii_to1.splitlines(), charset='us-ascii') - self.assertIn('content="text/html; charset=us-ascii"', output) + self.assertIn('charset="us-ascii"', output) self.assertIn('ımplıcıt', output) class TestDiffer(unittest.TestCase): diff --git a/Lib/test/test_difflib_expect.html b/Lib/test/test_difflib_expect.html index 9f33a9e9c9cf58..5bc8e0dcd437f5 100644 --- a/Lib/test/test_difflib_expect.html +++ b/Lib/test/test_difflib_expect.html @@ -1,22 +1,42 @@ - - - - + + - - - diff --git a/Misc/NEWS.d/next/Library/2025-05-23-20-01-52.gh-issue-134580.xnaJ70.rst b/Misc/NEWS.d/next/Library/2025-05-23-20-01-52.gh-issue-134580.xnaJ70.rst new file mode 100644 index 00000000000000..979d310d3cee58 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-23-20-01-52.gh-issue-134580.xnaJ70.rst @@ -0,0 +1,3 @@ +Improved the styling of HTML diff pages generated by the +:class:`difflib.HtmlDiff` class, and migrated the output to the HTML5 +standard. From 1288aa16bdbb6c418153463ee16cc79255097cc7 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Fri, 23 May 2025 21:10:13 +0800 Subject: [PATCH 2/4] vertical padding removed --- Lib/difflib.py | 14 +++++++------- Lib/test/test_difflib_expect.html | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Lib/difflib.py b/Lib/difflib.py index dd28c26c5a97c1..c8b9d889b0e3cf 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -1635,19 +1635,19 @@ def _line_pair_iterator(): :root {color-scheme: light dark} table.diff { font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; - border:medium; + border: medium; } .diff_header { - background-color:#e0e0e0; - font-weight:bold; + background-color: #e0e0e0; + font-weight: bold; } td.diff_header { - text-align:right; - padding:4px 8px; + text-align: right; + padding: 0 8px; } .diff_next { - background-color:#c0c0c0; - padding:4px 0; + background-color: #c0c0c0; + padding: 4px 0; } .diff_add {background-color:palegreen} .diff_chg {background-color:#ffff77} diff --git a/Lib/test/test_difflib_expect.html b/Lib/test/test_difflib_expect.html index 5bc8e0dcd437f5..30521539c41061 100644 --- a/Lib/test/test_difflib_expect.html +++ b/Lib/test/test_difflib_expect.html @@ -9,19 +9,19 @@ :root {color-scheme: light dark} table.diff { font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace; - border:medium; + border: medium; } .diff_header { - background-color:#e0e0e0; - font-weight:bold; + background-color: #e0e0e0; + font-weight: bold; } td.diff_header { - text-align:right; - padding:4px 8px; + text-align: right; + padding: 0 8px; } .diff_next { - background-color:#c0c0c0; - padding:4px 0; + background-color: #c0c0c0; + padding: 4px 0; } .diff_add {background-color:palegreen} .diff_chg {background-color:#ffff77} From 66cdcb56b278870067e7d6b000358839df626c1b Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Tue, 27 May 2025 17:23:35 +0300 Subject: [PATCH 3/4] Casing --- Lib/difflib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/difflib.py b/Lib/difflib.py index c8b9d889b0e3cf..18801a9b19eb9d 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -1620,7 +1620,7 @@ def _line_pair_iterator(): - Diff Comparison + Diff comparison From 102a544525c7613ffe5d3ece5d1d90195f1bdf4c Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Wed, 28 May 2025 06:23:55 +0800 Subject: [PATCH 4/4] casting --- Lib/test/test_difflib_expect.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_difflib_expect.html b/Lib/test/test_difflib_expect.html index 30521539c41061..2346a6f9f8dddf 100644 --- a/Lib/test/test_difflib_expect.html +++ b/Lib/test/test_difflib_expect.html @@ -4,7 +4,7 @@ - Diff Comparison + Diff comparison