Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions __tests__/options/trusted-type-policy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import htmlToDOM from 'html-dom-parser';
Comment thread
remarkablemark marked this conversation as resolved.

import parse from '../../src';

vi.mock('html-dom-parser', () => ({
default: vi.fn(() => []),
}));

describe('trustedTypePolicy option', () => {
it('passes trustedTypePolicy to html-dom-parser', () => {
const trustedTypePolicy = {
createHTML: vi.fn((input: string) => input),
};

parse('<div>test</div>', { trustedTypePolicy });

expect(htmlToDOM).toHaveBeenCalledWith(
'<div>test</div>',
expect.objectContaining({
lowerCaseAttributeNames: false,
trustedTypePolicy,
}),
);
});
});
8 changes: 7 additions & 1 deletion examples/webpack/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ import parse from 'html-react-parser';

const root = createRoot(document.getElementById('root'));

root.render(parse('<h1>HTMLReactParser loaded with Webpack</h1>'));
let trustedHtml = (window.trustedTypes && window.trustedTypes.createPolicy)
? window.trustedTypes.createPolicy('csp-react-html', {createHTML: function(s) { return s; }})
: null;

root.render(parse('<h1>HTMLReactParser loaded with Webpack</h1>',{
trustedTypePolicy : trustedHtml
}));
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export default function HTMLReactParser(
return [];
}

return domToReact(
htmlToDOM(html, options?.htmlparser2 ?? domParserOptions),
options,
);
const htmlToDOMOptions = {
...(options?.htmlparser2 ?? domParserOptions),
trustedTypePolicy: options?.trustedTypePolicy,
};

return domToReact(htmlToDOM(html, htmlToDOMOptions), options);
}
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { DomHandlerOptions } from 'domhandler';
import type { DOMNode } from 'html-dom-parser';
import type { DOMNode, TrustedTypePolicy } from 'html-dom-parser';

Check failure on line 2 in src/types.ts

View workflow job for this annotation

GitHub Actions / unit

Module '"html-dom-parser"' has no exported member 'TrustedTypePolicy'. Did you mean to use 'import TrustedTypePolicy from "html-dom-parser"' instead?

Check failure on line 2 in src/types.ts

View workflow job for this annotation

GitHub Actions / build

Module '"html-dom-parser"' has no exported member 'TrustedTypePolicy'. Did you mean to use 'import TrustedTypePolicy from "html-dom-parser"' instead?

Check failure on line 2 in src/types.ts

View workflow job for this annotation

GitHub Actions / lint

Module '"html-dom-parser"' has no exported member 'TrustedTypePolicy'. Did you mean to use 'import TrustedTypePolicy from "html-dom-parser"' instead?
import type { ParserOptions } from 'htmlparser2';
import type { JSX, ReactNode } from 'react';

export interface HTMLReactParserOptions {
htmlparser2?: ParserOptions & DomHandlerOptions;
trustedTypePolicy?: TrustedTypePolicy;

library?: {
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
Loading