AndersDJohnson / AndersDJohnson/htmlcompressor

Event-handler preservation breaks script compression

Offen
#70 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
auto-migrated Priority-Medium Type-Defect
Vorherrschende Sprache
Java
Sterne
1
Forks
0
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

```
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

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.