[wp-trac] [WordPress Trac] #54179: Raw JS is prematurely ended with a </script> tag when HTML tags are present within JS strings.

WordPress Trac noreply at wordpress.org
Fri Sep 24 13:42:03 UTC 2021


#54179: Raw JS is prematurely ended with a </script> tag when HTML tags are present
within JS strings.
----------------------------+-----------------------------
 Reporter:  joshuapeck9898  |      Owner:  (none)
     Type:  defect (bug)    |     Status:  new
 Priority:  normal          |  Milestone:  Awaiting Review
Component:  General         |    Version:
 Severity:  normal          |   Keywords:
  Focuses:  javascript      |
----------------------------+-----------------------------
 We are using a custom JS file that is reaching out to a web service,
 building a string from the data, and setting it as the innerHTML to a DOM
 Element already on the page. For some reason, HTML tags in this string
 were causing our script to get cut short, and whatever remained would
 instead by interpreted as HTML. A JavaScript snippet such as the
 following:


 {{{
 myElement.innerHTML = ‘<div><p>My Content</p></div>’;
 }}}


 would cause a </script> tag to be inserted between the </p> and </div>
 tags, cutting the JS short, leaving the rest to be read as HTML. The fix
 was to replace every tag in these strings with a chain of concatenations:


 {{{
 myElement.innerHTML = ‘<’ + ‘div>’ + ‘<’ + ‘p>My Content’ + ‘<’ + ‘/p>’ +
 ‘<’ + ‘/div>’;
 }}}


 I don’t know what was causing this, but this workaround prevented whatever
 was processing our JavaScript from recognizing and interpreting the HTML
 tags.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54179>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list