Wow! JS in HTML in JS in HTML in Perl!
(I've broken the lines for readability. It's not quite equivalent with the line breaks, so they should be removed in the final version.)
Start with the inner JavaScript:
this.function.test('')
Add the inner HTML/XHTML:
<tr id="someid"><td>
<a href="" onclick="
this.function.test('')
"></a>
</td></tr>
Add the JavaScript:
this.element.insert('
<tr id="someid"><td>
<a href="" onclick="
this.function.test(\'\')
"></a>
</td></tr>
')
Add the outer HTML/XHTML: (XHTML requires that "<" be encoded. I encoded ">" as well, though it's not required.)
<a href="" onclick="
this.element.insert('
<tr id="someid"><td>
<a href="" onclick="
this.function.test(\'\')
"></a>
</td></tr>
')
">
Add the Perl:
print("
<a href=\"\" onclick=\"
this.element.insert('
<tr id="someid"><td>
<a href="" onclick="
this.function.test(\\'\\')
"></a>
</td></tr>
')
\">
");
For comparison purposes, here's what you had:
print("
<a href=\"\" onclick=\"
this.element.insert('
<tr id=\"someid\"><td>
<a href=\"\" onclick=\"
this.function.test(\'\')
\"></a>
</td></tr>
')
\"
");
Not even close. It's much better to do it programmatically:
use HTML::Entities qw( encode_entities );
sub text_to_ht_attr_val {
my $s = @_ ? $_[0] : $_;
$s = encode_entities($s);
return qq{"$s"};
}
sub text_to_js_lit {
my $s = @_ ? $_[0] : $_;
$s =~ s/\\/\\\\/g;
$s =~ s/'/\\'/g;
# ...
return qq{'$s'};
}
my $ht_to_insert = q{<tr id="someid"><td>}
. q{<a href="" onclick="this.function.test('')"></a>}
. q{</td></tr>};
my $js_onclick = 'this.element.insert('
. text_to_js_lit($ht_to_insert)
. ')';
my $ht_dyn_ele = '<a href="" onclick='
. text_to_ht_attr_val($js_onclick)
. '>';
print( "$ht_dyn_ele\n" );
<a href="" onclick="
this.element.insert('
<tr id="someid"><td>
<a href="" onclick="
this.function.test(\'\')
"></a>
</td></tr>
')
">
For comparison purposes, here's what I had:
<a href="" onclick="
this.element.insert('
<tr id="someid"><td>
<a href="" onclick="
this.function.test(\'\')
"></a>
</td></tr>
')
">
(Note that the single quotes didn't need to be encoded, but encode_entities harmlessly encodes them as "'".)
|