3

I have been getting this error when trying to parse HTML with a script tag using PHP's DOMDocument class.

Warning: DOMDocument::loadHTML(): Unexpected end tag : style in Entity, line: 71 in /var/www/signage-dev/dynaml/includes/page.php on line 95

I have even tried CDATA tags but it has resulted in the same error

My script tag is

<script type="text/javascript">
//<![CDATA[
document.write('<style type="text/css"> .printBTN { display: inline; } </style>');
//]]>   
</script>

This snippet is supposed to show a print button when JavaScript is enabled. I don't believe this is the most effective way of doing this (the previous developers created this), but even so, I wish to find a solution to this problem since I may run into this problem again in the future and it may not be as easy to fix as this is.

daxvena
  • 1,140
  • 3
  • 14
  • 30
  • I dont think its the script/cdata thats the issue. Are you sure all your style tags are opened/closed properly? I made basic example because i was curious and got no parse errors. – prodigitalson Apr 27 '12 at 20:32
  • Does the warning disappear if the CDATA section is removed, maybe the real error is being obscured by the style tag? You should also check if the warning affects the results, maybe the parser is just pointing out that the tag is suspicious but parses it correctly. – mmarinero Apr 27 '12 at 20:45
  • @prodigitalson Yes, I do have proper opening and closing tags. When I remove the content in the script tag, it runs fine but I no longer have a print button – daxvena Apr 27 '12 at 21:52
  • @mariom No I had the warning originally without the CDATA sections. I just included it in this because I thought the CDATA tags would stop the parsing in the script tag, but it does not, and noted from my comment above, that is were the error is coming from. Also, I don't have problems when the script tag is at the bottom of the page, just the warning, but when I bring it above the body or head, the body and head does not get parsed. – daxvena Apr 27 '12 at 21:54

1 Answers1

2

This question seems to have the solution Why split the tag when writing it with document.write()?

I guess php DOM is more strict than most browsers. The solution I have seen sometimes is the one posted in a comment below the linked answer, escaping it with <\/script>, or in this case <\/style>, but I thought it was only necessary with script tags.

Community
  • 1
  • 1
mmarinero
  • 365
  • 1
  • 4
  • Hmm maybe thats why it worked for me in a test... i had the `script` in the `body` and use the HTML5 doctype. – prodigitalson Apr 27 '12 at 23:30
  • 1
    @prodigitalson It also works without the character escape when placed in the body, regardless of the doctype, but not in head. That's some pretty strange behavior. – daxvena Apr 28 '12 at 04:54