Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Conditional Comments using DomDocument

I've seen a thread relating to altering conditional comments, but I can't find whether it's possible to add new conditional comments using the php dom functionality.

I essentially want to be able to add the following into (not the only example, but you get the idea!).

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->

I've looked at DomComment but it seems to add the closing tag for the comment, so I end up with:

<!--[if ! lte IE 6]--><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->
like image 715
Simon Avatar asked Dec 08 '25 12:12

Simon


1 Answers

this:

<?php
$doc = new DOMDocument();
$doc->loadHTML("<html><body><p id='bla'>Test</body></html>");
$bla = $doc->getElementById("bla");
$bla->appendChild(new DOMComment('[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]'));

echo $doc->saveHTML();  //<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]-->

works for me. note that the proper syntax for a conditional comment is

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]-->

not

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->

as you say you want to have it.

like image 140
ax. Avatar answered Dec 11 '25 02:12

ax.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!