diff --git a/react-app/package-lock.json b/react-app/package-lock.json
index 62919bb7f..03e1ae8ef 100644
--- a/react-app/package-lock.json
+++ b/react-app/package-lock.json
@@ -8,6 +8,7 @@
"name": "angular2-hn-react",
"version": "0.0.0",
"dependencies": {
+ "dompurify": "^3.4.17",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"react-router-dom": "^7.9.0"
@@ -1394,9 +1395,6 @@
"arm"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1418,9 +1416,6 @@
"arm"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1442,9 +1437,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1466,9 +1458,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1490,9 +1479,6 @@
"x64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1514,9 +1500,6 @@
"x64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1685,9 +1668,6 @@
"arm"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1702,9 +1682,6 @@
"arm"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1719,9 +1696,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1736,9 +1710,6 @@
"arm64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1753,9 +1724,6 @@
"loong64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1770,9 +1738,6 @@
"loong64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1787,9 +1752,6 @@
"ppc64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1804,9 +1766,6 @@
"ppc64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1821,9 +1780,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1838,9 +1794,6 @@
"riscv64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1855,9 +1808,6 @@
"s390x"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1872,9 +1822,6 @@
"x64"
],
"dev": true,
- "libc": [
- "glibc"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -1889,9 +1836,6 @@
"x64"
],
"dev": true,
- "libc": [
- "musl"
- ],
"license": "MIT",
"optional": true,
"os": [
@@ -2184,6 +2128,13 @@
"@types/react": "^19.3.0"
}
},
+ "node_modules/@types/trusted-types": {
+ "version": "2.0.7",
+ "resolved": "https://registry.npmjs.org/@types/trusted-types/-/trusted-types-2.0.7.tgz",
+ "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==",
+ "license": "MIT",
+ "optional": true
+ },
"node_modules/@typescript-eslint/eslint-plugin": {
"version": "8.72.2",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.72.2.tgz",
@@ -3067,6 +3018,15 @@
"license": "MIT",
"peer": true
},
+ "node_modules/dompurify": {
+ "version": "3.4.17",
+ "resolved": "https://registry.npmjs.org/dompurify/-/dompurify-3.4.17.tgz",
+ "integrity": "sha512-B/pSlYVHQ58kqISD/yDjuJ1yfzI34m1gF4lAod2bwSRhiN4K5Cl3H8Af7hqB2PmTL911tdEudnnXn5FnhRoz0w==",
+ "license": "(MPL-2.0 OR Apache-2.0)",
+ "optionalDependencies": {
+ "@types/trusted-types": "^2.0.7"
+ }
+ },
"node_modules/electron-to-chromium": {
"version": "1.5.481",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.481.tgz",
diff --git a/react-app/package.json b/react-app/package.json
index f4269a831..7eef234ff 100644
--- a/react-app/package.json
+++ b/react-app/package.json
@@ -13,6 +13,7 @@
"test:watch": "vitest"
},
"dependencies": {
+ "dompurify": "^3.4.17",
"react": "^19.2.0",
"react-dom": "^19.2.0",
"react-router-dom": "^7.9.0"
diff --git a/react-app/src/components/Comment/Comment.scss b/react-app/src/components/Comment/Comment.scss
new file mode 100644
index 000000000..43597ab23
--- /dev/null
+++ b/react-app/src/components/Comment/Comment.scss
@@ -0,0 +1,86 @@
+@use "../../styles/media" as *;
+
+.comment {
+& {
+ a {
+ font-weight: bold;
+ text-decoration: none;
+ &:hover {
+ text-decoration: underline;
+ }
+ }
+}
+
+.meta {
+ font-size: 13px;
+ color: #696969;
+ font-weight: bold;
+ letter-spacing: 0.5px;
+ margin-bottom: 8px;
+ a {
+ text-decoration: none;
+
+ &:hover {
+ text-decoration: underline;
+ }
+ }
+ .time {
+ padding-left: 5px;
+ }
+}
+
+@media #{$mobile-only} {
+ .meta {
+ font-size: 14px;
+ margin-bottom: 10px;
+ .time {
+ padding: 0;
+ float: right;
+ }
+ }
+}
+
+.meta-collapse {
+ margin-bottom: 20px;
+}
+
+.deleted-meta {
+ font-size: 12px;
+ font-weight: bold;
+ letter-spacing: 0.5px;
+ margin: 30px 0;
+ a {
+ text-decoration: none;
+ }
+}
+
+.collapse {
+ font-size: 13px;
+ letter-spacing: 2px;
+ cursor: pointer;
+}
+
+.comment-tree {
+ margin-left: 24px;
+}
+
+@media #{$tablet-only} {
+ .comment-tree {
+ margin-left: 8px;
+ }
+}
+
+.comment-text {
+ font-size: 15px;
+ margin-top: 0;
+ margin-bottom: 20px;
+ word-wrap: break-word;
+ line-height: 1.5em;
+}
+
+.subtree {
+ margin-left: 0;
+ padding: 0;
+ list-style-type: none;
+}
+}
diff --git a/react-app/src/components/Comment/Comment.tsx b/react-app/src/components/Comment/Comment.tsx
new file mode 100644
index 000000000..8681ddcd1
--- /dev/null
+++ b/react-app/src/components/Comment/Comment.tsx
@@ -0,0 +1,44 @@
+import { useState } from 'react';
+import { Link } from 'react-router-dom';
+import type { Comment as CommentModel } from '../../models';
+import { buttonProps } from '../../utils/activateOnKey';
+import { sanitizedHtml } from '../../utils/sanitize';
+import './Comment.scss';
+
+export function Comment({ comment }: { comment: CommentModel }) {
+ const [collapsed, setCollapsed] = useState(false);
+
+ if (comment.deleted) {
+ return (
+
+
+ [deleted] | Comment Deleted
+
+
+ );
+ }
+
+ return (
+
+
+ setCollapsed((c) => !c))}>
+ [{collapsed ? '+' : '-'}]
+ {' '}
+ {comment.user}
+ {comment.time_ago}
+
+
+
+
+
+ {comment.comments.map((sub) => (
+ -
+
+
+ ))}
+
+
+
+
+ );
+}
diff --git a/react-app/src/pages/ItemDetailsPage.scss b/react-app/src/pages/ItemDetailsPage.scss
new file mode 100644
index 000000000..173199c8a
--- /dev/null
+++ b/react-app/src/pages/ItemDetailsPage.scss
@@ -0,0 +1,141 @@
+@use "../styles/media" as *;
+
+.item-page {
+.item {
+ box-sizing: border-box;
+ padding: 10px 40px 0 40px;
+ z-index: 0;
+}
+
+@media #{$tablet-only} {
+ .item {
+ padding: 10px 20px 0 40px;
+ }
+}
+
+@media #{$mobile-only} {
+ .item {
+ box-sizing: border-box;
+ padding: 110px 15px 0 15px;
+ }
+}
+
+.head-margin {
+ margin-bottom: 15px;
+}
+
+p {
+ margin: 2px 0;
+}
+
+.subject {
+ word-wrap: break-word;
+ margin-top: 20px;
+}
+
+a {
+ cursor: pointer;
+ text-decoration: none;
+}
+
+@media #{$mobile-only} {
+ .laptop {
+ display: none;
+ }
+}
+
+@media #{$laptop-only} {
+ .mobile {
+ display: none;
+ }
+}
+
+.title {
+ font-size: 16px;
+ font-family: Verdana, Geneva, sans-serif;
+}
+
+.title-block {
+ text-align: center;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ overflow: hidden;
+ margin: 0 75px;
+}
+
+@media #{$mobile-only} {
+ .title {
+ font-size: 15px;
+ }
+ .back-button {
+ position: absolute;
+ top: 52%;
+ width: 0.6rem;
+ height: 0.6rem;
+ background: transparent;
+ box-shadow: 0 0 0 lightgray;
+ transition: all 200ms ease;
+ left: 4%;
+ transform: translate3d(0, -50%, 0) rotate(-135deg);
+ }
+}
+
+.subtext {
+ font-size: 12px;
+ font-weight: bold;
+ letter-spacing: 0.5px;
+}
+
+.domain {
+ letter-spacing: 0.5px;
+}
+
+.subtext a {
+ &:hover {
+ text-decoration: underline;
+ }
+}
+
+.item-details {
+ padding: 10px;
+}
+
+.item-header {
+ padding-bottom: 10px;
+}
+
+@media #{$mobile-only} {
+ .item-header {
+ padding: 10px 0 10px 0;
+ position: fixed;
+ width: 100%;
+ left: 0;
+ top: 62px;
+ }
+}
+
+.pollResults {
+ margin-bottom: 1em;
+}
+
+.pollContent {
+ * {
+ padding-bottom: 0;
+ margin-bottom: -1em;
+ margin-top: 1em;
+ }
+ .pollBar {
+ height: 10px;
+ margin-bottom: 1em;
+ }
+}
+
+ul {
+ list-style-type: none;
+ padding: 10px 0;
+}
+
+li {
+ display: list-item;
+}
+}
diff --git a/react-app/src/pages/ItemDetailsPage.test.tsx b/react-app/src/pages/ItemDetailsPage.test.tsx
new file mode 100644
index 000000000..3c68983ca
--- /dev/null
+++ b/react-app/src/pages/ItemDetailsPage.test.tsx
@@ -0,0 +1,177 @@
+import { render, screen, within } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { createMemoryRouter, RouterProvider } from 'react-router-dom';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import type { Comment, Story } from '../models';
+import { routes } from '../routes';
+import { SettingsProvider } from '../settings';
+
+function comment(id: number, overrides: Partial = {}): Comment {
+ return {
+ id,
+ level: 0,
+ user: `user${id}`,
+ time: 0,
+ time_ago: '2 hours ago',
+ content: `Comment ${id}
`,
+ deleted: false,
+ comments: [],
+ ...overrides,
+ };
+}
+
+function story(overrides: Partial = {}): Story {
+ return {
+ id: 1,
+ title: 'A story',
+ points: 42,
+ user: 'pg',
+ time: 0,
+ time_ago: '3 hours ago',
+ type: 'link',
+ url: 'https://example.com/a',
+ domain: 'example.com',
+ content: '',
+ comments: [],
+ comments_count: 0,
+ ...overrides,
+ };
+}
+
+function stubApi(routesByUrl: Record) {
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async (url: string) =>
+ url in routesByUrl ? Response.json(routesByUrl[url]) : new Response('nope', { status: 500 })
+ )
+ );
+}
+
+function renderAt(path: string) {
+ const router = createMemoryRouter(routes, { initialEntries: ['/news/1', path], initialIndex: 1 });
+ render(
+
+
+
+ );
+ return router;
+}
+
+const ITEM = 'https://node-hnapi.herokuapp.com/item/1';
+
+afterEach(() => vi.unstubAllGlobals());
+
+describe('ItemDetailsPage', () => {
+ it('renders the story header and nested comments', async () => {
+ stubApi({
+ [ITEM]: story({
+ comments_count: 3,
+ comments: [comment(10, { comments: [comment(11, { level: 1 })] }), comment(12, { deleted: true })],
+ }),
+ });
+ renderAt('/item/1');
+ const page = await screen.findByTestId('item-page');
+ expect((await within(page).findAllByRole('link', { name: 'A story' }))[0]).toHaveAttribute(
+ 'href',
+ 'https://example.com/a'
+ );
+ expect(page).toHaveTextContent('42 points by');
+ expect(page).toHaveTextContent('3 comments');
+ expect(screen.getByText('Comment 10')).toBeInTheDocument();
+ expect(screen.getByText('Comment 11')).toBeInTheDocument();
+ expect(screen.getByRole('link', { name: 'user11' })).toHaveAttribute('href', '/user/user11');
+ expect(page).toHaveTextContent('Comment Deleted');
+ });
+
+ it('collapses and expands a comment subtree', async () => {
+ stubApi({ [ITEM]: story({ comments: [comment(10, { comments: [comment(11)] })] }) });
+ renderAt('/item/1');
+ const toggle = (await screen.findAllByRole('button', { name: '[-]' }))[0];
+ await userEvent.click(toggle);
+ expect(toggle).toHaveTextContent('[+]');
+ expect(screen.getByText('Comment 10')).not.toBeVisible();
+ expect(screen.getByText('Comment 11')).not.toBeVisible();
+ await userEvent.click(toggle);
+ expect(screen.getByText('Comment 11')).toBeVisible();
+ });
+
+ it('renders poll results with proportional bars', async () => {
+ stubApi({
+ [ITEM]: story({ type: 'poll', url: 'item?id=1', poll: [{}, {}] as Story['poll'] }),
+ 'https://node-hnapi.herokuapp.com/item/2': { points: 30, content: 'Yes
' },
+ 'https://node-hnapi.herokuapp.com/item/3': { points: 10, content: 'No
' },
+ });
+ renderAt('/item/1');
+ expect(await screen.findByText('Yes')).toBeInTheDocument();
+ expect(screen.getByText('30 points')).toBeInTheDocument();
+ const bars = document.querySelectorAll('.pollBar');
+ expect([...bars].map((b) => b.style.width)).toEqual(['75%', '25%']);
+ });
+
+ it('renders a failed poll option as zero points', async () => {
+ stubApi({
+ [ITEM]: story({ type: 'poll', url: 'item?id=1', poll: [{}, {}] as Story['poll'] }),
+ 'https://node-hnapi.herokuapp.com/item/2': { points: 5, content: 'Yes
' },
+ });
+ renderAt('/item/1');
+ expect(await screen.findByText('Yes')).toBeInTheDocument();
+ expect(screen.getByText('0 points')).toBeInTheDocument();
+ const bars = document.querySelectorAll('.pollBar');
+ expect([...bars].map((b) => b.style.width)).toEqual(['100%', '0%']);
+ });
+
+ it('toggles a comment from the keyboard', async () => {
+ stubApi({ [ITEM]: story({ comments: [comment(10)] }) });
+ renderAt('/item/1');
+ const toggle = await screen.findByRole('button', { name: '[-]' });
+ toggle.focus();
+ await userEvent.keyboard('{Enter}');
+ expect(toggle).toHaveTextContent('[+]');
+ await userEvent.keyboard(' ');
+ expect(toggle).toHaveTextContent('[-]');
+ });
+
+ it('sanitizes HTML from the API', async () => {
+ stubApi({
+ [ITEM]: story({
+ content: 'Body
',
+ comments: [comment(10, { content: 'bad' })],
+ }),
+ });
+ renderAt('/item/1');
+ await screen.findByText('Body');
+ const page = screen.getByTestId('item-page');
+ expect(page.innerHTML).not.toContain('onerror');
+ expect(page.innerHTML).not.toContain('',
+ },
+ });
+ renderAt('/item/1');
+ await screen.findByText('Option');
+ const page = screen.getByTestId('item-page');
+ expect(page.innerHTML).not.toContain('onerror');
+ expect(page.innerHTML).not.toContain('