question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

entityToHTML doesn't work for element like "img"

See original GitHub issue

Assuming 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

3reactions
benbriggscommented, Dec 7, 2017

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!

2reactions
JohnMaguircommented, Dec 6, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found