mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-20 20:00:33 +03:00
fix(react): rendering self-closed tags and tags without content (#74)
This commit is contained in:
committed by
GitHub
parent
9c71bb5f26
commit
5a7211db91
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-use-before-define */
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import core from '@bbob/core';
|
import core from '@bbob/core';
|
||||||
import * as html from '@bbob/html';
|
import * as html from '@bbob/html';
|
||||||
@@ -10,12 +11,13 @@ const toAST = (source, plugins, options) => core(plugins)
|
|||||||
render: (input) => html.render(input, { stripTags: true }),
|
render: (input) => html.render(input, { stripTags: true }),
|
||||||
}).tree;
|
}).tree;
|
||||||
|
|
||||||
|
const isContentEmpty = (content) => (!content || content.length === 0);
|
||||||
|
|
||||||
function tagToReactElement(node, index) {
|
function tagToReactElement(node, index) {
|
||||||
return React.createElement(
|
return React.createElement(
|
||||||
node.tag,
|
node.tag,
|
||||||
{ ...node.attrs, key: index },
|
{ ...node.attrs, key: index },
|
||||||
// eslint-disable-next-line no-use-before-define
|
isContentEmpty(node.content) ? null : renderToReactNodes(node.content),
|
||||||
node.content ? renderToReactNodes(node.content) : null,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -43,6 +43,12 @@ describe('@bbob/react', () => {
|
|||||||
expect(html).toBe('<span><a href="https://en.wikipedia.org">https://en.wikipedia.org</a></span>')
|
expect(html).toBe('<span><a href="https://en.wikipedia.org">https://en.wikipedia.org</a></span>')
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('[b]Testing[/b][hr]', () => {
|
||||||
|
const html = renderBBCode('[b]Testing[/b][hr]');
|
||||||
|
|
||||||
|
expect(html).toBe('<span><span style="font-weight:bold;">Testing</span><hr/></span>')
|
||||||
|
});
|
||||||
|
|
||||||
describe('options.onlyAllowTags', () => {
|
describe('options.onlyAllowTags', () => {
|
||||||
test('render "[Super Feature] and [i]super[/i]" when only [i] allowed', () => {
|
test('render "[Super Feature] and [i]super[/i]" when only [i] allowed', () => {
|
||||||
const html = renderBBCode('[Super Feature] and [i]super[/i]', { onlyAllowTags: ['i'] });
|
const html = renderBBCode('[Super Feature] and [i]super[/i]', { onlyAllowTags: ['i'] });
|
||||||
|
|||||||
Reference in New Issue
Block a user