Skip to content

Commit e04d75e

Browse files
committed
Add copy functionality for SHA256 checksum and optimize display
1 parent 60d1102 commit e04d75e

6 files changed

Lines changed: 44 additions & 31 deletions

File tree

include/layout.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,18 @@ function site_footer(array $config = []): void
504504
require __DIR__ . "/footer.inc";
505505
}
506506

507+
function checksum_html(string $checksum, string $algorithm = 'sha256'): string
508+
{
509+
$checksum = htmlspecialchars($checksum, ENT_QUOTES, 'UTF-8');
510+
$algorithm = htmlspecialchars($algorithm, ENT_QUOTES, 'UTF-8');
511+
512+
if ($algorithm !== 'sha256') {
513+
return '<span class="' . $algorithm . 'sum">' . $checksum . '</span>';
514+
}
515+
516+
return '<span class="sha256-row"><span class="sha256">' . $checksum . '</span><button type="button" class="sha256-copy" data-copy-text="' . $checksum . '" aria-label="Copy sha256 checksum">Copy</button></span>';
517+
}
518+
507519
function get_nav_items(): array {
508520
return [
509521
new NavItem(

include/version.inc

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ function show_source_releases()
143143
echo '<span class="md5sum">', $rel['md5'], '</span>';
144144
}
145145
if (isset($rel['sha256'])) {
146-
echo '<span class="sha256-row">';
147-
echo '<span class="sha256">', $rel['sha256'], '</span>';
148-
echo '<button type="button" class="sha256-copy" data-copy-text="', $rel['sha256'], '" aria-label="Copy sha256 checksum">Copy</button>';
149-
echo '</span>';
146+
echo checksum_html($rel['sha256']);
150147
}
151148
?>
152149
<?php if (isset($rel['note']) && $rel['note']): ?>
@@ -168,20 +165,5 @@ function show_source_releases()
168165
</div>
169166
<?php endforeach; ?>
170167
<?php endforeach; /* major releases loop end */ ?>
171-
<script>
172-
document.addEventListener('click', function (event) {
173-
var button = event.target.closest('.sha256-copy');
174-
if (!button || !navigator.clipboard) {
175-
return console.error('Clipboard API not available');
176-
}
177-
178-
navigator.clipboard.writeText(button.dataset.copyText).then(function () {
179-
button.textContent = 'Copied';
180-
setTimeout(function () {
181-
button.textContent = 'Copy';
182-
}, 3000);
183-
});
184-
});
185-
</script>
186168
<?php
187169
}

js/common.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,18 @@ function applyTheme(theme) {
879879
}
880880

881881
applyTheme(savedTheme)
882+
883+
const downloads = document.querySelector('.downloads');
884+
downloads?.addEventListener('click', function (event) {
885+
var button = event.target.closest('.sha256-copy');
886+
if (!button || !navigator.clipboard) {
887+
return;
888+
}
889+
890+
navigator.clipboard.writeText(button.dataset.copyText).then(function () {
891+
button.textContent = 'Copied';
892+
setTimeout(function () {
893+
button.textContent = 'Copy';
894+
}, 1500);
895+
});
896+
});

pre-release-builds.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@
9797
<a href="<?php echo $file_info['path'] ?>"><?php echo "php-{$info['version']}.tar.{$file_type}"; ?></a>
9898
<span class="releasedate"><?php echo date('d M Y', strtotime($info['date'])); ?></span>
9999
<?php foreach ($QA_CHECKSUM_TYPES as $algo): ?>
100-
<span class="<?php echo $algo; ?>">
101100
<?php if (isset($file_info[$algo]) && strlen($file_info[$algo])) : ?>
102-
<?php echo $file_info[$algo]; ?>
101+
<?php echo checksum_html($file_info[$algo], $algo); ?>
103102
<?php else: ?>
104103
<em><small>No checksum value available</small></em>)&nbsp;
105104
<?php endif; ?>
@@ -189,7 +188,7 @@
189188
}
190189

191190
if ($includeSha && $sha !== '') {
192-
$parts[] = '<span class="sha256">' . htmlspecialchars($sha, ENT_QUOTES, 'UTF-8') . '</span>';
191+
$parts[] = checksum_html($sha);
193192
}
194193

195194
return '<li>' . implode(' ', $parts) . '</li>';
@@ -311,4 +310,3 @@
311310

312311
<?php
313312
site_footer(['sidebar' => $SIDEBAR_DATA]);
314-

releases/index.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ function mk_rel(int $major,
222222
$linebreak = '';
223223
foreach (['md5', 'sha256'] as $cs) {
224224
if (isset($src[$cs])) {
225-
echo $linebreak;
226-
echo "<span class=\"{$cs}sum\">{$cs}: {$src[$cs]}</span>\n";
225+
echo $linebreak, checksum_html($src[$cs], $cs), "\n";
227226
$linebreak = "<br/>";
228227
}
229228
}

styles/theme-base.css

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -789,23 +789,28 @@ fieldset {
789789
overflow: hidden;
790790
text-overflow: ellipsis;
791791
}
792-
.content-box .md5sum:before {
792+
.content-box .md5sum:before,
793+
.downloads .md5sum:before {
793794
content: "md5: ";
794795
font-family: var(--font-family-sans-serif);
795796
}
796-
.content-box .sha256:before {
797+
.content-box .sha256:before,
798+
.downloads .sha256:before {
797799
content: "sha256: ";
798800
font-family: var(--font-family-sans-serif);
799801
}
800-
.content-box .sha256-row {
802+
.content-box .sha256-row,
803+
.downloads .sha256-row {
801804
display: flex;
802805
align-items: center;
803806
gap: 0.5rem;
804807
}
805-
.content-box .sha256-row .sha256 {
808+
.content-box .sha256-row .sha256,
809+
.downloads .sha256-row .sha256 {
806810
min-width: 0;
807811
}
808-
.content-box .sha256-copy {
812+
.content-box .sha256-copy,
813+
.downloads .sha256-copy {
809814
border: 1px solid var(--dark-blue-color);
810815
border-radius: 30px;
811816
background-color: var(--dark-blue-color);
@@ -819,7 +824,9 @@ fieldset {
819824
white-space: nowrap;
820825
}
821826
.content-box .sha256-copy:hover,
822-
.content-box .sha256-copy:focus {
827+
.content-box .sha256-copy:focus,
828+
.downloads .sha256-copy:hover,
829+
.downloads .sha256-copy:focus {
823830
border-color: var(--dark-magenta-color);
824831
background-color: var(--dark-magenta-color);
825832
}

0 commit comments

Comments
 (0)