0

I'm trying to edit a Wordpress plugin that uses JavaScript. I want to have HTML in the description field, but currently the JavaScript is outputting text, not translating it to HTML.

For instance, when the description is

Test <p>html</p>

It prints it with the <p>, instead of rendering a new paragraph.

function appendPersonality(quiz, personality, hasTitle, hasImage, hasDescription) {
  var $personality, $title, $description, $image;

  $title = createIf(hasTitle, '<h2>', { 'html': personality.name });

  if (personality.image.file) {
    $image = createIf(hasImage, '<img>', {
      'class': classes('result-image'),
      'src': _getPath(personality.image.file.path),
      'alt': personality.image.alt
    });
  }

  $description = createIf(hasDescription, '<p>', {
    'html': personality.description
  });

  // NOTE (Emil): We only create $personality element if it has at least
  // one child element.
  if (hasTitle || hasImage || hasDescription) {
    $personality = $('<div>', { 'class': classes('personality') });

    $personality.append($title);
    $personality.append($image);
    $personality.append($description);
  }

  quiz.$resultWrapper.append($personality);

  setInlineImageHeight($image, $personality, quiz.$resultWrapper);

  return $personality;
}

Why does 'html': personality.description not render html?

jonbon
  • 1,142
  • 3
  • 12
  • 37
  • Need more details on where `$description` being used afterwards – xhg Apr 09 '17 at 09:54
  • Are we going to guess the structure of object? – hdotluna Apr 09 '17 at 09:56
  • @aahung I added the rest of the function. The full code I'm trying to edit is https://github.com/LumeniaAS/h5p-personality-quiz – jonbon Apr 09 '17 at 09:57
  • @LaraBelle sorry, I thought this was just a quick JavaScript problem...I added the rest of the function, and this is the full code (https://github.com/LumeniaAS/h5p-personality-quiz) – jonbon Apr 09 '17 at 09:58

1 Answers1

0

I took at the codes you posted, it should be fine. But my suggestion is to check the value of personality.description, it might be escaped at some point. you can unescape it using

(The function unescapeHTML is taken from Arun P Johny's answer)

function unescapeHtml(safe) {
    return safe.replace(/&amp;/g, '&')
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>')
        .replace(/&quot;/g, '"')
        .replace(/&#039;/g, "'");
}

...

$description = createIf(hasDescription, '<p>', {
  'html': unescapeHtml(personality.description)
});
Community
  • 1
  • 1
xhg
  • 1,850
  • 2
  • 21
  • 35
  • It might be escaped at some point? Just like, arbitrarily excaped? I don't see anywhere on the github https://github.com/LumeniaAS/h5p-personality-quiz where it is escaped – jonbon Apr 09 '17 at 10:25
  • @jonbon Unless you can provide a full website/webpage or a live demo, as far as I know the repository only contains couple of files. Where did you assign the value `Test

    html

    ` and can you check `params.resultScreen.displayDescription` is still `Test

    html

    `?
    – xhg Apr 09 '17 at 10:29
  • The test value is inputted through the plug-in interface – jonbon Apr 09 '17 at 12:09
  • @jonbon then the "escape" should be conducted before the value being passed to you. – xhg Apr 09 '17 at 12:48