From 8bc01413b68dfbe0c8d1a77c08c5d10ea54d7758 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Mon, 10 Feb 2025 14:35:03 +0800 Subject: [PATCH 1/4] Add darkmode support for difflib's comparison pages --- Doc/whatsnew/3.14.rst | 8 ++++++++ Lib/difflib.py | 13 +++++++++++-- Lib/test/test_difflib_expect.html | 11 ++++++++++- .../2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst | 3 +++ 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 23b1f5f2e0c67c..6181d2db2be6ee 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -410,6 +410,14 @@ decimal :meth:`Decimal.from_number() `. (Contributed by Serhiy Storchaka in :gh:`121798`.) +difflib +------- + +* Comparison pages with highlighted changes generated by the + :class:`difflib.HtmlDiff` class allow browsers to apply its default dark + mode. + (Contributed by Jiahao Li in :gh:`129939`.) + dis --- diff --git a/Lib/difflib.py b/Lib/difflib.py index 7f595b6c72e641..56634f6a26d636 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -1632,13 +1632,22 @@ def _line_pair_iterator(): """ _styles = """ + :root {color-scheme: light dark} table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} - .diff_add {background-color:#aaffaa} + .diff_add {background-color:palegreen} .diff_chg {background-color:#ffff77} - .diff_sub {background-color:#ffaaaa}""" + .diff_sub {background-color:#ffaaaa} + + @media (prefers-color-scheme: dark) { + .diff_header {background-color:#666} + .diff_next {background-color:#444} + .diff_add {background-color:darkgreen} + .diff_chg {background-color:goldenrod} + .diff_sub {background-color:darkred} + }""" _table_template = """ diff --git a/Misc/NEWS.d/next/Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst b/Misc/NEWS.d/next/Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst new file mode 100644 index 00000000000000..8b08ac9b6ce7e7 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst @@ -0,0 +1,3 @@ +Comparison pages with highlighted changes generated by the +:class:`difflib.HtmlDiff` class allow browsers to apply its default dark +mode. From ff66981a2d6289e04d592bd3d58fe7ed1b816025 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Mon, 10 Feb 2025 14:54:21 +0800 Subject: [PATCH 2/4] whatsnew --- Doc/whatsnew/3.14.rst | 3 +-- .../Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 6181d2db2be6ee..63f56189412794 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -414,8 +414,7 @@ difflib ------- * Comparison pages with highlighted changes generated by the - :class:`difflib.HtmlDiff` class allow browsers to apply its default dark - mode. + :class:`difflib.HtmlDiff` class now support dark mode. (Contributed by Jiahao Li in :gh:`129939`.) dis diff --git a/Misc/NEWS.d/next/Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst b/Misc/NEWS.d/next/Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst index 8b08ac9b6ce7e7..5014f5bc3c7f3c 100644 --- a/Misc/NEWS.d/next/Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst +++ b/Misc/NEWS.d/next/Library/2025-02-10-14-34-34.gh-issue-129939.B08L4e.rst @@ -1,3 +1,2 @@ Comparison pages with highlighted changes generated by the -:class:`difflib.HtmlDiff` class allow browsers to apply its default dark -mode. +:class:`difflib.HtmlDiff` class now support dark mode. From 3cfd78df9a658ead79f8a10cea78f89fb485a6a9 Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Mon, 10 Feb 2025 20:24:44 +0800 Subject: [PATCH 3/4] WCAG AA --- Lib/difflib.py | 16 +++++++--------- Lib/test/test_difflib_expect.html | 16 +++++++--------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/Lib/difflib.py b/Lib/difflib.py index 56634f6a26d636..b53051c0566057 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -1612,15 +1612,13 @@ def _line_pair_iterator(): _file_template = """ - - - + + - - + + + Comparison page @@ -1643,9 +1641,9 @@ def _line_pair_iterator(): @media (prefers-color-scheme: dark) { .diff_header {background-color:#666} - .diff_next {background-color:#444} + .diff_next {background-color:#393939} .diff_add {background-color:darkgreen} - .diff_chg {background-color:goldenrod} + .diff_chg {background-color:#847415} .diff_sub {background-color:darkred} }""" diff --git a/Lib/test/test_difflib_expect.html b/Lib/test/test_difflib_expect.html index 06b62f5a9a984f..b943341b2fb637 100644 --- a/Lib/test/test_difflib_expect.html +++ b/Lib/test/test_difflib_expect.html @@ -1,13 +1,11 @@ - - - + + - - + + + Comparison page From e4180bc0717174ada000adf2382d70024ce59a1f Mon Sep 17 00:00:00 2001 From: Wulian233 <1055917385@qq.com> Date: Tue, 11 Feb 2025 09:40:53 +0800 Subject: [PATCH 4/4] only colour --- Lib/difflib.py | 12 +++++++----- Lib/test/test_difflib_expect.html | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Lib/difflib.py b/Lib/difflib.py index b53051c0566057..c124afdd039559 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -1612,13 +1612,15 @@ def _line_pair_iterator(): _file_template = """ - - + + + - - - Comparison page + + diff --git a/Lib/test/test_difflib_expect.html b/Lib/test/test_difflib_expect.html index b943341b2fb637..6c2ee49837e689 100644 --- a/Lib/test/test_difflib_expect.html +++ b/Lib/test/test_difflib_expect.html @@ -1,11 +1,13 @@ - - + + + - - - Comparison page + +