Skip to content
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

Open
cxj opened this issue Jan 31, 2018 · 5 comments
Open

Optionally filter out XML comments #183

cxj opened this issue Jan 31, 2018 · 5 comments

Comments

@cxj
Copy link
Contributor

cxj commented Jan 31, 2018

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?

@cxj
Copy link
Contributor Author

cxj commented Jan 31, 2018

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);
}

@TRPB
Copy link
Member

TRPB commented Feb 6, 2018

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.

@cxj
Copy link
Contributor Author

cxj commented Feb 6, 2018

Great! Thanks much. Any interest in incorporating this into standard Transphporm itself?

@TRPB
Copy link
Member

TRPB commented Feb 12, 2018

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 display: none I'll upload it as its own repo/composer package.

@cxj
Copy link
Contributor Author

cxj commented Feb 16, 2018

I look forward to seeing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants