AndersDJohnson / AndersDJohnson/htmlcompressor

Event-handler preservation breaks script compression

Abierto
#70 0 comentarios 0 reacciones 0 asignados Ver en GitHub
auto-migrated Priority-Medium Type-Defect
Lenguaje dominante
Java
Estrellas
1
Forks
0
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.