feat: stored HTML keeps link rel and media names are slugged

- the HTML policy keeps rel on a when it holds only nofollow, sponsored
  and ugc tokens (D-83); any other rel value is removed
- classes.MediaFileName slugs uploaded image names (lowercase, Polish
  letters transliterated, hyphens, at most 80 characters, image when
  empty) for the editor upload and POST /media/upload (D-85)
- the editor corpus gains an external link with rel and a decorative
  image, pinned by TestEditorOutputPassesPolicy
This commit is contained in:
Jakub Zych
2026-11-04 10:32:25 +01:00
parent 805bfdb837
commit 391435b4bf
12 changed files with 167 additions and 56 deletions

View File

@@ -32,15 +32,18 @@ function startBlock(editor: Editor): void {
editor.commands.setTextSelection(editor.state.doc.content.size - 1)
}
/** link types text and links it through setLink, then leaves the link. */
function link(editor: Editor, text: string, href: string): void {
/** link types text and links it through setLink (with rel, as the link form writes it), then leaves the link. */
function link(editor: Editor, text: string, href: string, rel?: string): void {
const from = editor.state.selection.from
type(editor, text)
const to = editor.state.selection.from
expect(editor.chain().setTextSelection({ from, to }).setLink({ href }).run(), href).toBe(true)
expect(editor.chain().setTextSelection({ from, to }).setLink(rel ? { href, rel } : { href }).run(), href).toBe(true)
editor.chain().setTextSelection(to).unsetMark('link').run()
}
/** The rel the link form writes (J3, D-83): the three tokens, external links only. */
const REL_VALUE = /^(nofollow|sponsored|ugc)( (nofollow|sponsored|ugc))*$/
function mark(editor: Editor, toggle: () => boolean, text: string): void {
expect(toggle()).toBe(true)
type(editor, text)
@@ -153,6 +156,20 @@ const builders: Record<string, (editor: Editor) => void> = {
expect(editor.commands.setHardBreak()).toBe(true)
type(editor, 'Druga linia')
},
// J3: an external link with the rel tokens of the link form (D-83).
'link-rel': (editor) => {
startBlock(editor)
type(editor, 'Partner: ')
link(editor, 'sklep', 'https://partner.example.test/', 'nofollow sponsored')
type(editor, ', ')
link(editor, 'forum', 'https://forum.example.test/watek', 'ugc')
type(editor, '.')
},
// J4: a decorative image carries alt="" (D-85).
'image-decorative': (editor) => {
startBlock(editor)
expect(editor.commands.setImage({ src: IMAGE_SRC, alt: '' })).toBe(true)
},
}
function build(steps: ((editor: Editor) => void)[]): string {
@@ -184,11 +201,11 @@ describe('editor corpus (D-05)', () => {
expect(readFileSync(CORPUS, 'utf8')).toBe(text)
})
it('holds no iframe, handler, target, rel or style and only policy URLs', () => {
it('holds no iframe, handler, target or style, rel only on external links, and only policy URLs', () => {
expect(entries.length).toBeGreaterThanOrEqual(15)
const doc = document.implementation.createHTMLDocument('')
for (const { name, html } of entries) {
expect(html, name).not.toMatch(/<iframe|target=|rel=| on[a-z]+=|style=/i)
expect(html, name).not.toMatch(/<iframe|target=| on[a-z]+=|style=/i)
expect(html, name).not.toBe('')
expect(html, name).not.toMatch(/<p><\/p>$/)
const host = doc.createElement('div')
@@ -198,7 +215,14 @@ describe('editor corpus (D-05)', () => {
expect(isPolicyUrl(url), `${name}: ${url}`).toBe(true)
}
for (const a of Array.from(host.querySelectorAll('a'))) {
expect(a.getAttributeNames(), name).toEqual(['href'])
const rel = a.getAttribute('rel')
if (rel === null) {
expect(a.getAttributeNames(), name).toEqual(['href'])
continue
}
expect(a.getAttributeNames().sort(), name).toEqual(['href', 'rel'])
expect(rel, name).toMatch(REL_VALUE)
expect(a.getAttribute('href'), name).toMatch(/^https?:\/\//)
}
}
const html = entries.map((e) => e.html).join('\n')
@@ -206,5 +230,7 @@ describe('editor corpus (D-05)', () => {
expect(html).toContain('<figcaption>')
expect(html).toContain('<table>')
expect(html).toContain('start="3"')
expect(html).toContain('rel="nofollow sponsored"')
expect(html).toContain('alt=""')
})
})

View File

@@ -55,8 +55,16 @@
"name": "hard-break",
"html": "<p>Pierwsza linia<br>Druga linia</p>"
},
{
"name": "link-rel",
"html": "<p>Partner: <a rel=\"nofollow sponsored\" href=\"https://partner.example.test/\">sklep</a>, <a rel=\"ugc\" href=\"https://forum.example.test/watek\">forum</a>.</p>"
},
{
"name": "image-decorative",
"html": "<p><img src=\"/storage/app/uploads/public/journal/2026/10/turkey-tail.jpg\" alt=\"\"></p>"
},
{
"name": "combined",
"html": "<figure data-embed=\"youtube\" data-id=\"dQw4w9WgXcQ\"></figure><p><img src=\"/storage/app/uploads/public/journal/2026/10/turkey-tail.jpg\" alt=\"Turkey Tail: czy to &quot;grzyb odporności&quot;?\"></p><figure><img src=\"/storage/app/uploads/public/journal/2026/10/turkey-tail.jpg\" alt=\"Owocniki wrośniaka\"><figcaption>Owocniki wrośniaka różnobarwnego.</figcaption></figure><table><thead><tr><th>Forma</th><th>Dawka</th><th>Uwagi</th></tr></thead><tbody><tr><td>Proszek</td><td>1-2 g</td><td>z posiłkiem</td></tr><tr><td>Kapsułki</td><td>2 x 500 mg</td><td>rano</td></tr></tbody></table><p>Źródła: <a href=\"http://example.test/\">strona</a>, <a href=\"https://example.test/badania?rok=2026#wyniki\">badania</a>, <a href=\"mailto:redakcja@example.test\">redakcja</a> i <a href=\"/blog/beta-glukany\">inny wpis</a>.</p><h2>Nagłówek 2</h2><h3>Nagłówek 3</h3><h4>Nagłówek 4</h4><ul><li><p>Beta-glukany</p></li><li><p>Antyoksydanty</p></li></ul><ol><li><p>Pierwszy krok</p></li><li><p>Drugi krok</p></li></ol><blockquote><p>Grzyby funkcjonalne wspierają organizm.</p></blockquote><ol start=\"3\"><li><p>Trzeci krok</p></li><li><p>Czwarty krok</p></li></ol><p>Tekst <strong>pogrubiony</strong>, <em>kursywa</em>, <s>przekreślony</s>, <u>podkreślony</u> i <code>kod</code>.</p><pre><code>dawka = 2 * 500 // mg</code></pre><p>Przed linią</p><hr><p>Po linii</p><p>Pierwsza linia<br>Druga linia</p>"
"html": "<figure data-embed=\"youtube\" data-id=\"dQw4w9WgXcQ\"></figure><p><img src=\"/storage/app/uploads/public/journal/2026/10/turkey-tail.jpg\" alt=\"Turkey Tail: czy to &quot;grzyb odporności&quot;?\"></p><figure><img src=\"/storage/app/uploads/public/journal/2026/10/turkey-tail.jpg\" alt=\"Owocniki wrośniaka\"><figcaption>Owocniki wrośniaka różnobarwnego.</figcaption></figure><table><thead><tr><th>Forma</th><th>Dawka</th><th>Uwagi</th></tr></thead><tbody><tr><td>Proszek</td><td>1-2 g</td><td>z posiłkiem</td></tr><tr><td>Kapsułki</td><td>2 x 500 mg</td><td>rano</td></tr></tbody></table><p>Źródła: <a href=\"http://example.test/\">strona</a>, <a href=\"https://example.test/badania?rok=2026#wyniki\">badania</a>, <a href=\"mailto:redakcja@example.test\">redakcja</a> i <a href=\"/blog/beta-glukany\">inny wpis</a>.</p><h2>Nagłówek 2</h2><h3>Nagłówek 3</h3><h4>Nagłówek 4</h4><ul><li><p>Beta-glukany</p></li><li><p>Antyoksydanty</p></li></ul><ol><li><p>Pierwszy krok</p></li><li><p>Drugi krok</p></li></ol><blockquote><p>Grzyby funkcjonalne wspierają organizm.</p></blockquote><ol start=\"3\"><li><p>Trzeci krok</p></li><li><p>Czwarty krok</p></li></ol><p>Tekst <strong>pogrubiony</strong>, <em>kursywa</em>, <s>przekreślony</s>, <u>podkreślony</u> i <code>kod</code>.</p><pre><code>dawka = 2 * 500 // mg</code></pre><p>Przed linią</p><hr><p>Po linii</p><p>Pierwsza linia<br>Druga linia</p><p>Partner: <a rel=\"nofollow sponsored\" href=\"https://partner.example.test/\">sklep</a>, <a rel=\"ugc\" href=\"https://forum.example.test/watek\">forum</a>.</p><p><img src=\"/storage/app/uploads/public/journal/2026/10/turkey-tail.jpg\" alt=\"\"></p>"
}
]