Skip to content

Commit 597b124

Browse files
committed
Fix review feedback: non-mapping YAML crash, SRI hash, clipboard error
- Guard against null/scalar/array YAML input that would crash Object.keys() - Add SRI integrity hash to js-yaml CDN script tag - Add .catch() handler for clipboard write failures https://claude.ai/code/session_01P4nYNMs3xBReSMSF35TG1g
1 parent 9b1a188 commit 597b124

1 file changed

Lines changed: 34 additions & 9 deletions

File tree

html/yaml-sorter.html

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
rel="icon"
99
href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🛠️</text></svg>"
1010
/>
11-
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"></script>
11+
<script
12+
src="https://cdnjs.cloudflare.com/ajax/libs/js-yaml/4.1.0/js-yaml.min.js"
13+
integrity="sha512-CSBhVREyzHAjAFfBlIBakjoRUKp5h7VSweP0InR/pAJyptH7peuhCsqAI/snV+TwZmXZqoUAZpXAN8x1ouSiQ=="
14+
crossorigin="anonymous"
15+
referrerpolicy="no-referrer"
16+
></script>
1217
<style>
1318
* {
1419
box-sizing: border-box;
@@ -302,6 +307,18 @@ <h1>YAML Sorter</h1>
302307

303308
try {
304309
const parsed = jsyaml.load(raw);
310+
if (
311+
typeof parsed !== "object" ||
312+
parsed === null ||
313+
Array.isArray(parsed)
314+
) {
315+
inputStatus.textContent = "Not a mapping";
316+
inputStatus.className = "status error";
317+
output.value = jsyaml.dump(parsed, { lineWidth: -1 });
318+
outputStatus.textContent = "No keys to sort";
319+
outputStatus.className = "status empty";
320+
return;
321+
}
305322
const keyCount = Object.keys(parsed).length;
306323
inputStatus.textContent = `${keyCount} key${keyCount !== 1 ? "s" : ""}`;
307324
inputStatus.className = "status ok";
@@ -333,14 +350,22 @@ <h1>YAML Sorter</h1>
333350

334351
copyBtn.addEventListener("click", () => {
335352
if (!output.value) return;
336-
navigator.clipboard.writeText(output.value).then(() => {
337-
copyBtn.textContent = "Copied!";
338-
copyBtn.classList.add("copied");
339-
setTimeout(() => {
340-
copyBtn.textContent = "Copy";
341-
copyBtn.classList.remove("copied");
342-
}, 1500);
343-
});
353+
navigator.clipboard.writeText(output.value).then(
354+
() => {
355+
copyBtn.textContent = "Copied!";
356+
copyBtn.classList.add("copied");
357+
setTimeout(() => {
358+
copyBtn.textContent = "Copy";
359+
copyBtn.classList.remove("copied");
360+
}, 1500);
361+
},
362+
() => {
363+
copyBtn.textContent = "Failed!";
364+
setTimeout(() => {
365+
copyBtn.textContent = "Copy";
366+
}, 1500);
367+
},
368+
);
344369
});
345370
</script>
346371

0 commit comments

Comments
 (0)