/* * SPDX-FileCopyrightText: 2020 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ import { renderToStaticMarkup } from 'react-dom/server' import { convertHtmlToReact, ParserOptions } from './convertHtmlToReact.js' import { convertNodeToReactElement } from './convertNodeToReactElement.js' import { Document, isTag, isText } from 'domhandler' import { NodeToReactElementTransformer } from './NodeToReactElementTransformer.js' import React, { ReactElement } from 'react' import { describe, expect, it } from '@jest/globals' const expectSameHtml = function (html: string, options: ParserOptions = {}) { const actual = renderToStaticMarkup(
test
', 'test
') }) it('should convert boolean attribute values', () => { expectOtherHtml('', '') expectOtherHtml('', '') expectOtherHtml('', '') }) ;[ ['CONTENTEDITABLE', 'contentEditable'], ['LABEL', 'label'], ['iTemREF', 'itemRef'] ].forEach(([attr, prop]) => { it(`should convert attribute ${attr} to prop ${prop}`, () => { const nodes = convertHtmlToReact(``, {}) expect(nodes).toHaveLength(1) expect((nodes[0] as ReactElement).props).toHaveProperty(prop) }) }) it('should decode html entities by default', () => { expectOtherHtml('!', '!') }) it('should not decode html entities when the option is disabled', () => { expectOtherHtml('!', '!', { decodeEntities: false }) }) describe('transform function', () => { it('should use the response when it is not undefined', () => { expectOtherHtml('testtransformed
transformed
', { transform(node, index) { returntransformed
} }) }) it('should not render elements and children when returning null', () => { expectOtherHtml('testinner testbold child
', 'test
', { transform(node) { if (isTag(node) && node.type === 'tag' && node.name === 'span') { return null } } }) }) it('should allow modifying nodes', () => { expectOtherHtml('test link', 'test link', { transform(node, index) { if (isTag(node)) { node.attribs.href = '/changed' } return convertNodeToReactElement(node, index) } }) }) it('should allow passing the transform function down to children', () => { const transform: NodeToReactElementTransformer = (node, index) => { if (isTag(node)) { if (node.name === 'ul') { node.attribs.class = 'test' return convertNodeToReactElement(node, index, transform) } } else if (isText(node)) { return node.data.replace(/list/, 'changed') } else { return null } } expectOtherHtml( '