AndersDJohnson / AndersDJohnson/htmlcompressor

Event-handler preservation breaks script compression

Aperta
#70 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
auto-migrated Priority-Medium Type-Defect
Lingua principale
Java
Stelle
1
Fork
0
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

```
The code that finds inline event handlers in the HTML using a couple of regexps
(eventPattern1 and eventPattern2) is also finding them inside literals in
script blocks and breaking those scripts because of it.

Example input:

function t() {
var myvarname = 1;
return ' onclick="test('+myvarname+')"/>';
}

This should compress like this:

function t(){var a=1;return ' onclick="test('+a+')"/>';}

but instead becomes:

function t(){var a=1;return ' onclick="test('+myvarname+')"/>';}

This breaks the Javascript code, as 'myvarname' doesn't exist after
compression. It was the HTML event handling code that put it back.

This same regexp can break compression entirely. Example:

var a='a', b='b', x=' onclick="t('+"'"+a+"','"+b+"'"+')"';

This is a legitimate javascript statement, setting x to the string
onclick="t('a','b')"

HtmlCompressor goes ahead and breaks it because it tried to parse HTML with
regexps.

[ERROR] HtmlCompressor: "missing ; before statement" at line [2:62] during
JavaScript compression: var a='a', b='b', x='
onclick="%%%~COMPRESS~EVENT~0~%%%"'"+a+"','"+b+"'"+')"';
[ERROR] HtmlCompressor: "unterminated string literal" at line [2:78] during
JavaScript compression: var a='a', b='b', x='
onclick="%%%~COMPRESS~EVENT~0~%%%"'"+a+"','"+b+"'"+')"';
[ERROR] HtmlCompressor: "Compilation produced 2 syntax errors." at line [1:0]
during JavaScript compression

It doesn't even have to be a known event handler, any word beginning "on" will
do:

var x=' one="'+"'";

This is using htmlcompressor 1.5.3 on all platforms, configured to use
YUICompressor for JS compression.

```

Original issue reported on code.google.com by `stuart.c...@gmail.com` on 10 Apr 2012 at 1:25

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.