Skip to content

Commit

Permalink
UX fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jennspencer committed May 9, 2024
1 parent 3ca6ecc commit e447442
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
9 changes: 6 additions & 3 deletions components/HTMLBlock/index.tsx
@@ -1,5 +1,5 @@
import React, { useEffect } from 'react';
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import { renderToStaticMarkup } from 'react-dom/server';

const MATCH_SCRIPT_TAGS = /<script\b[^>]*>([\s\S]*?)<\/script *>\n?/gim;

Expand All @@ -15,7 +15,10 @@ const extractScripts = (html: string = ''): [string, () => void] => {

const HTMLBlock = props => {
const { children, runScripts, safeMode = false } = props;
const html = renderToString(<>{children}</>);
let html = children;

if (typeof html !== 'string') html = renderToStaticMarkup(html);

const [cleanedHtml, exec] = extractScripts(html);

useEffect(() => {

Check failure on line 24 in components/HTMLBlock/index.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 16)

__tests__/components/HTMLBlock.test.jsx

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem. ❯ resolveDispatcher node_modules/react/cjs/react.development.js:1465:13 ❯ Proxy.useEffect node_modules/react/cjs/react.development.js:1508:20 ❯ Module.HTMLBlock [as default] components/HTMLBlock/index.tsx:24:3 ❯ __tests__/components/HTMLBlock.test.jsx:10:19

Check failure on line 24 in components/HTMLBlock/index.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 17)

__tests__/components/HTMLBlock.test.jsx

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem. ❯ resolveDispatcher node_modules/react/cjs/react.development.js:1476:13 ❯ Proxy.useEffect node_modules/react/cjs/react.development.js:1519:20 ❯ Module.HTMLBlock [as default] components/HTMLBlock/index.tsx:24:3 ❯ __tests__/components/HTMLBlock.test.jsx:10:19

Check failure on line 24 in components/HTMLBlock/index.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 18)

__tests__/components/HTMLBlock.test.jsx

TypeError: Cannot read properties of null (reading 'useEffect') ❯ Proxy.useEffect node_modules/react/cjs/react.development.js:1634:21 ❯ Module.HTMLBlock [as default] components/HTMLBlock/index.tsx:24:3 ❯ __tests__/components/HTMLBlock.test.jsx:10:19
Expand All @@ -25,7 +28,7 @@ const HTMLBlock = props => {
if (safeMode) {
return (
<pre className="html-unsafe">
<code dangerouslySetInnerHTML={{ __html: renderToStaticMarkup(cleanedHtml) }} />
<code>{cleanedHtml}</code>
</pre>
);
}
Expand Down
71 changes: 71 additions & 0 deletions docs/html-tests.md
@@ -0,0 +1,71 @@
---
title: 'HTML Blocks'
---

## JSX (with no HTML attributes)

<HTMLBlock runScripts={true}>
<script>console.log(true, 0);</script>
<h3>Header 0</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div>I am actually a div!</div>
</HTMLBlock>


## JSX

<HTMLBlock>
<script>console.log(true, 1);</script>
<h3>Header 1</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style={{ paddingLeft: "20px", color: "blue" }}>
Behold, I am blue and indented!
</div>
</HTMLBlock>


## HTML as a prop

<HTMLBlock children='<script>console.log(true, 2);</script><h3>Header 2</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style="padding-left: 20px; color: blue">Behold, I am blue and indented!</div>' />


## HTML in template literal

<HTMLBlock>
{`
<script>console.log(true, 3);</script>
<h3>Header 3</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style="padding-left: 20px; color: blue">
Behold, I am blue and indented!
</div>
`}
</HTMLBlock>


## JSX in safe mode

<HTMLBlock safeMode={true}>
<script>console.log(true, 4);</script>
<h3>Header 4</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style={{ paddingLeft: "20px", color: "blue" }}>
Behold, I am blue and indented!
</div>
</HTMLBlock>


## HTML in safe mode

<HTMLBlock safeMode={true}>
{`
<script>console.log(true, 5);</script>
<h3>Header 5</h3>
<p>Paragraph with <em>italics</em> and <strong>bold stuff</strong>.</p>
<div style="padding-left: 20px; color: blue">
Behold, I am blue and indented!
</div>
`}
</HTMLBlock>
23 changes: 19 additions & 4 deletions docs/sanitizing-tests.md
@@ -1,7 +1,22 @@

## Sanitizing `style` tags

<style>
* {
background-color: olive;
}
</style>


## Sanitizing `style` attributes

<p style="background-color: salmon">fish content</p>


## Sanitizing html blocks

<HTMLBlock>
<h2>Header</h2>
<p>hello there</p>
</HTMLBlock>
[block:html]
{
"html": "<style>* { border: 3px solid magenta; }</style>"
}
[/block]
3 changes: 3 additions & 0 deletions example/docs.ts
Expand Up @@ -15,6 +15,8 @@ import gettingStarted from '../docs/getting-started.md';
// @ts-ignore
import headings from '../docs/headings.md';
// @ts-ignore
import htmlBlocks from '../docs/html-tests.md';
// @ts-ignore
import images from '../docs/images.md';
// @ts-ignore
import lists from '../docs/lists.md';
Expand All @@ -41,6 +43,7 @@ const fixtures = Object.entries({
features,
gettingStarted,
headings,
htmlBlocks,
images,
lists,
sanitizingTests,
Expand Down

0 comments on commit e447442

Please sign in to comment.