-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optionally filter out XML comments #183
Comments
Something like this code, perhaps? $dom = new DOMDocument;
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//comment()') as $comment) {
$comment->parentNode->removeChild($comment);
} |
This will work, but we could do with a better syntax: class CommentStrip implements \Transphporm\Module {
public function load(\Transphporm\Config $config) {
$config->registerPseudo(new CommentStripPseudo());
}
}
class CommentStripPseudo implements \Transphporm\Pseudo {
public function match($name, $args, \DomElement $element) {
if ($name === 'comment') {
$xpath = new \DomXPath($element->ownerDocument);
$xpath->query('//comment', $element);
foreach ($xpath->query('//comment()') as $comment) {
$comment->parentNode->removeChild($comment);
}
}
}
} $xml = '<html><!-- foo --><div>Foo</div><h1><!-- some other comment -->Bar</h1></html>';
$template = new \Transphporm\Builder($xml, 'html:comment { }');
$template->loadModule(new CommentStrip());
var_dump($template->output()); Really it would be better if it was $template = new \Transphporm\Builder($xml, 'html:comment { display:none; }'); but that will take a little more work. You also need to target the root element to remove comments from it. |
Great! Thanks much. Any interest in incorporating this into standard Transphporm itself? |
Outside of basic functionality, it's better if extensions are provided as distinct modules. With composer it's really easy to download and install them. Once I have it working as intended with |
I look forward to seeing it. |
Perhaps this is a request for new feature, or maybe just a discussion on how to do it myself.
I like to liberally document my TSS files using comments, just as I do my PHP code. I would like to do the same in my XML templates, especially to clarify various non-obvious Transphporm techniques.
However, I don't want end-users of my websites being able to see those comments via various browser's capability to display "page source" etc. I'd prefer to just output pure, clean HTML.
How hard would it be to make the Transphporm animation process do this? Is it inefficient? How could I do this myself easily?
The text was updated successfully, but these errors were encountered: