entityToHTML doesn't work for element like "img"
See original GitHub issueAssuming I have such an entityToHTML
entityToHTML: (entity, originalText) => {
if(entity.type === "IMAGE") {
return (
<img src={entity.data.src} style={{width: "100%"}}/>
)
}
}
It goes to the https://github.com/HubSpot/draft-convert/blob/master/src/blockEntities.js#L34 to get tag element length:
const entityHTML = getEntityHTML(entity, originalText);
const converted = [...getElementHTML(entityHTML, originalText)
|| originalText];
const prefixLength = getElementTagLength(entityHTML, 'start');
const suffixLength = getElementTagLength(entityHTML, 'end');
The getElementTagLength (https://github.com/HubSpot/draft-convert/blob/master/src/util/getElementTagLength.js#L6) use splitReactElement:
const getElementTagLength = (element, type = 'start') => {
if (React.isValidElement(element)) {
const length = splitReactElement(element)[type].length;
const child = React.Children.toArray(element.props.children)[0];
return length + (child && React.isValidElement(child)
? getElementTagLength(child, type)
: 0
);
}
......
However the splitReactElement than treat the as “void element” (https://github.com/HubSpot/draft-convert/blob/master/src/util/splitReactElement.js#L25) , where it directly returns a String, rather than
{start: <sth>, end: <sth>}
:
if (VOID_TAGS.indexOf(element.type) !== -1) {
return ReactDOMServer.renderToStaticMarkup(element);
}
Then the splitReactElement(element)[type].length
report an error.
Isn’t it an issue, or am I missing something?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:5
Top Results From Across the Web
Is it not possible to use HTML entities in a title attribute?
It is an <img> element (well actually it's an image together with an image map containing a single circular <area> element, but I...
Read more >HTML elements reference - HTML: HyperText Markup Language
This page lists all the HTML elements, which are created using tags. ... grouping of related content, such as images or form fields....
Read more >HTML Elements - W3Schools
An HTML element is defined by a start tag, some content, and an end tag. ... Note: Some HTML elements have no content...
Read more >Developing HTML Emails for Gmail: 14 Tips for Coding
Use an HTML entity that Gmail doesn't recognize ... If you don't want certain phone numbers, emails, or URLs in your email to...
Read more >htmlspecialchars - Manual - PHP
(PHP 4, PHP 5, PHP 7, PHP 8). htmlspecialchars — Convert special characters to HTML entities ... ini_set('default_charset', $charset); // doesn't work.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
just opened #111 with a fix that seems to work - it’d be great to get verification from you guys trying it out locally. sorry for the wait!
A quick workaround for this is to use a string instead of a React DOM element. So instead of the React.DOM img element use
"<img src='...' />"
within the entityToHTML method. Not sure why this works but was a quick fix for me until the actual issue is fixed.