[php-src] Issue #10299: new strip_comments($language, $code) function
| From: | a4992214 | Date: | Thu, 12 Jan 2023 18:09:54 +0000 |
| Subject: | [php-src] Issue #10299: new strip_comments($language, $code) function | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-243425@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/10299
Author: a4992214
### Description
Looking at stackoverflow there are large numbers of requests for regex to remove comments from
javascript code, but none of them seem to be good enough (either forgetting to handle quotes, or
.replace(/'/ type situations - or just so impossible to read [let alone develop or
support]).
I can see php_strip_comments is a close proximity - but it seems to get confused by said
.replace(/'/ lines.
Can we have a definitive strip_comments("javascript", $code|$jsFilename) that
can do the job once and for all?
A starting point testbed would be
```
$html=<<<'PATTERN'
doSomething('xxxxx//xxxxx'); // c1ccccccc
/* c2cc' cc'ccc */
/* c2cc'/* cc"ccc */
doSomething2(111, 222, 333); // c3ccccccc
abc.replace(/'/g, 'xxxxx//xxxxx'); /* c4ccccccc */
abc.replace(/"/g, 'xxxxx'); /* c5ccccccc */
doSomething("<div>xxxxx//xxxxx/*xxxx*/xxxxx xxxxx xxxxx</div>",1234);//
c6ccccccc
doSomething('<div>xxxxx//xxxxx/*xxxx*/xxxxx xxxxx xxxxx</div>',1234);//
c7ccccccc
PATTERN;
```
Less problematic, but equally useful would be a strip_comments("css",
$code|$filename) handler.