From ee8405980cbd21ac176ac34ed511b50e8baae51e Mon Sep 17 00:00:00 2001 From: Miguel Teixeira Date: Tue, 4 Aug 2026 21:57:16 +0100 Subject: [PATCH] =?UTF-8?q?fix(perf):=20article=20TBT=207416ms=20=E2=80=94?= =?UTF-8?q?=20strip=20scripts/styles/iframes/event-handlers,=20lazy-load?= =?UTF-8?q?=20images,=20content-visibility?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BIT-28145: newsproject.bitreport.pt article template Total Blocking Time was 7416ms (Lighthouse Performance 60) vs home 73ms (Performance 97). Root cause: WordPress oEmbed HTML injected via set:html rendered

World

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Hello

World

'); +}); + +test('strips script tags with attributes', () => { + const input = '

Text

More

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

More

'); +}); + +test('strips script tags with type attribute', () => { + const input = '

Text

More

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

More

'); +}); + +test('strips multiple script tags', () => { + const input = '

A

B

'; + const result = sanitize_article_html(input); + assert.equal(result, '

A

B

'); +}); + +test('strips script with multiline content', () => { + const input = '

Before

After

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Before

After

'); +}); + +test('strips script with nested angle brackets (inline JSON)', () => { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

'); +}); + +test('strips style tags from article content', () => { + const input = '

Text

More

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

More

'); +}); + +test('strips style tags with attributes', () => { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

'); +}); + +test('strips link tags from article content', () => { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

'); +}); + +test('strips multiple link tags', () => { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

'); +}); + +test('strips inline event handlers', () => { + const input = '
Text
'; + const result = sanitize_article_html(input); + assert.ok(!result.includes('onload'), `Expected no onload in: ${result}`); + assert.ok(!result.includes('onclick'), `Expected no onclick in: ${result}`); + assert.ok(result.includes('Text'), `Expected text preserved in: ${result}`); +}); + +test('strips inline event handlers with single quotes', () => { + const input = ""; + const result = sanitize_article_html(input); + assert.ok(!result.includes('onerror'), `Expected no onerror in: ${result}`); +}); + +test('strips inline event handlers with unquoted values', () => { + const input = '
Text
'; + const result = sanitize_article_html(input); + assert.ok(!result.includes('onmouseover'), `Expected no onmouseover in: ${result}`); +}); + +test('replaces iframes with placeholders', () => { + const input = ''; + const result = sanitize_article_html(input); + assert.ok(result.includes('embed-placeholder'), `Expected placeholder div in: ${result}`); + assert.ok(result.includes('data-embed-src'), `Expected data-embed-src in: ${result}`); + assert.ok(!result.includes(' { + const input = ''; + const result = sanitize_article_html(input); + assert.ok(result.includes('aria-label="Video player"'), `Expected aria-label in: ${result}`); + assert.ok(result.includes('Video player'), `Expected title in button span in: ${result}`); +}); + +test('placeholder uses aspect-ratio from width/height', () => { + const input = ''; + const result = sanitize_article_html(input); + assert.ok(result.includes('aspect-ratio:560/315'), `Expected aspect-ratio in: ${result}`); +}); + +test('handles WordPress Twitter embed blockquote', () => { + const input = ''; + const result = sanitize_article_html(input); + assert.ok(!result.includes(' { + const input = ''; + const result = sanitize_article_html(input); + assert.ok(!result.includes(' { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.ok(!result.includes('Text

'), `Expected paragraph preserved in: ${result}`); + assert.ok(result.includes('embed-placeholder'), `Expected placeholder in: ${result}`); +}); + +test('handles case-insensitive script tags', () => { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

'); +}); + +test('handles empty content', () => { + const result = sanitize_article_html(''); + assert.equal(result, ''); +}); + +test('handles content with no scripts, styles, links, iframes, or event handlers', () => { + const input = '

Hello world

Heading

  • Item
'; + const result = sanitize_article_html(input); + assert.equal(result, input); +}); + +test('strips style tags with multiline CSS', () => { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

'); +}); + +test('strips link with multiple attributes', () => { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.equal(result, '

Text

'); +}); + +test('handles iframe with no src gracefully', () => { + const input = '

Text

'; + const result = sanitize_article_html(input); + assert.ok(result.includes('

Text

'), `Expected paragraph preserved in: ${result}`); +}); + +test('adds loading=lazy and decoding=async to images without those attributes', () => { + const input = 'Photo'; + const result = sanitize_article_html(input); + assert.ok(result.includes('loading="lazy"'), `Expected loading="lazy" in: ${result}`); + assert.ok(result.includes('decoding="async"'), `Expected decoding="async" in: ${result}`); + assert.ok(result.includes('alt="Photo"'), `Expected alt preserved in: ${result}`); +}); + +test('does not add loading=lazy to images that already have it', () => { + const input = 'Photo'; + const result = sanitize_article_html(input); + assert.ok(result.includes('loading="eager"'), `Expected loading="eager" preserved in: ${result}`); + assert.ok(!result.includes('loading="lazy"'), `Did not expect loading="lazy" in: ${result}`); +}); + +test('does not add decoding to images that already have it', () => { + const input = 'Photo'; + const result = sanitize_article_html(input); + assert.ok(result.includes('decoding="sync"'), `Expected decoding="sync" preserved in: ${result}`); + assert.ok(!result.includes('decoding="async"'), `Did not expect decoding="async" in: ${result}`); +}); \ No newline at end of file diff --git a/bridge/src/lib/sanitize-html.ts b/bridge/src/lib/sanitize-html.ts new file mode 100644 index 0000000..a73d3d1 --- /dev/null +++ b/bridge/src/lib/sanitize-html.ts @@ -0,0 +1,102 @@ +/** + * Sanitize WordPress article HTML to reduce client-side main-thread blocking. + * + * Primary targets: + * -

World

'; + const result = sanitizeArticleHtml(input); + assert.equal(result, '

Hello

World

'); + }); + + it('strips script tags with src attribute', () => { + const input = '

Text

More

'; + const result = sanitizeArticleHtml(input); + assert.equal(result, '

Text

More

'); + }); + + it('strips style tags', () => { + const input = '

Text

'; + const result = sanitizeArticleHtml(input); + assert.equal(result, '

Text

'); + }); + + it('strips link tags', () => { + const input = '

Text

'; + const result = sanitizeArticleHtml(input); + assert.equal(result, '

Text

'); + }); + + it('strips inline event handlers', () => { + const input = '
Text
'; + const result = sanitizeArticleHtml(input); + assert.ok(!result.includes('onload')); + assert.ok(!result.includes('onclick')); + assert.ok(result.includes('Text')); + }); + + it('replaces iframes with click-to-load placeholders', () => { + const input = ''; + const result = sanitizeArticleHtml(input); + assert.ok(result.includes('embed-placeholder')); + assert.ok(result.includes('data-embed-src')); + assert.ok(!result.includes(' { + const input = ''; + const result = sanitizeArticleHtml(input); + assert.ok(result.includes('aria-label="Video player"')); + assert.ok(result.includes('Video player')); + }); + + it('preserves non-script, non-iframe, non-style content unchanged', () => { + const input = '

Hello

Quote
'; + assert.equal(sanitizeArticleHtml(input), input); + }); + + it('handles WordPress Twitter embed', () => { + const input = ''; + const result = sanitizeArticleHtml(input); + assert.ok(!result.includes(' { + const input = '

Text

'; + const result = sanitizeArticleHtml(input); + assert.ok(!result.includes('Text

')); + }); + + it('adds loading=lazy and decoding=async to images without those attributes', () => { + const input = 'Photo'; + const result = sanitizeArticleHtml(input); + assert.ok(result.includes('loading="lazy"')); + assert.ok(result.includes('decoding="async"')); + assert.ok(result.includes('alt="Photo"')); + }); + + it('does not add loading=lazy to images that already have it', () => { + const input = 'Photo'; + const result = sanitizeArticleHtml(input); + assert.ok(result.includes('loading="eager"')); + assert.ok(!result.includes('loading="lazy"')); + }); +}); \ No newline at end of file diff --git a/frontend/src/lib/sanitize-html.ts b/frontend/src/lib/sanitize-html.ts new file mode 100644 index 0000000..266ce50 --- /dev/null +++ b/frontend/src/lib/sanitize-html.ts @@ -0,0 +1,94 @@ +/** + * Sanitize WordPress article HTML to reduce client-side Total Blocking Time. + * + * WordPress oEmbed embeds (Twitter, Instagram, YouTube, etc.) inject +