Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle self-closing HTML inside parseable HTML #103

Merged
merged 1 commit into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 16 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,16 @@ function attributeValueToJSXPropValue(key, value) {
return value;
}

function isCoalesceableHTML(html) {
// ignore block-level elements
// ignore self-closing or non-content-bearing elements
return html.match(BLOCK_ELEMENT_REGEX) || html.match(SELF_CLOSING_ELEMENT_REGEX) ? false : true;
}

function coalesceInlineHTML(ast) {
function coalescer(node, index, siblings) {
if (node.type === 'html') {
// ignore block-level elements
if (BLOCK_ELEMENT_REGEX.test(node.value)) {
return;
}

// ignore self-closing or non-content-bearing elements
if (SELF_CLOSING_ELEMENT_REGEX.test(node.value)) {
if (!isCoalesceableHTML(node.value)) {
return;
}

Expand All @@ -347,7 +347,8 @@ function coalesceInlineHTML(ast) {

// where's the end tag?
while (end === undefined && i < siblings.length) {
if (siblings[i].type !== 'html') {
if ( siblings[i].type !== 'html'
|| (siblings[i].type === 'html' && !isCoalesceableHTML(siblings[i].value))) {
i += 1;
continue;
}
Expand Down Expand Up @@ -398,7 +399,7 @@ function coalesceInlineHTML(ast) {
}
};

return ast.children.forEach(coalescer);
ast.children.forEach(coalescer);
}

export function compiler(markdown, {overrides = {}} = {}) {
Expand Down Expand Up @@ -543,6 +544,11 @@ export function compiler(markdown, {overrides = {}} = {}) {

let props = {key, ...ast.props};

if (Array.isArray(ast.children) && ast.children.length === 1 && ast.children[0].type === 'html') {
props.dangerouslySetInnerHTML = {__html: ast.children[0].value};
ast.children = null;
}

const override = overrides[htmlNodeType];
if (override) {
if (override.component) {
Expand Down Expand Up @@ -607,8 +613,7 @@ export function compiler(markdown, {overrides = {}} = {}) {
let jsx = astToJSX(remarkAST);

// discard the root <div> node if there is only one valid initial child
// generally this is a paragraph
if (jsx.props.children.length === 1) {
if (jsx.props.children && jsx.props.children.length === 1) {
jsx = jsx.props.children[0];
}

Expand Down
9 changes: 9 additions & 0 deletions index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,15 @@ describe('markdown-to-jsx', () => {
expect($element.children[0].children[0].tagName).toBe('STRONG');
expect($element.children[0].children[0].textContent).toBe('code');
});

it('handles self-closing html inside parsable html (regression)', () => {
const element = render(compiler('<a href="https://opencollective.com/react-dropzone/sponsor/0/website" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsor/0/avatar.svg"></a>'));
const $element = dom(element);

expect($element.tagName).toBe('P');
expect($element.children[0].tagName).toBe('A');
expect($element.children[0].children[0].tagName).toBe('IMG');
});
});

describe('horizontal rules', () => {
Expand Down