#!perl
my
$js
= (
my
$m
= new WWW::Scripter)->use_plugin(
'JavaScript'
);
$m
->get(URI::file->new_abs(
't/blank.html'
));
$js
->new_function(
$_
=> \
&$_
)
for
qw 'is
ok';
sub
data_url {
my
$u
= new URI
'data:'
;
$u
->media_type(
'text/html'
);
$u
->data(
shift
);
$u
}
{
my
$m
;
ok
eval
{
(
$m
= new WWW::Scripter)
->use_plugin(
'JavaScript'
,
engine
=>
'JE'
)
->bind_classes({
'My::Package'
=>
'foo'
,
'foo'
=> {}
}); 1
},
'bind_classes works before a page is fetched'
;
}
{
my
$warning
;
local
$SIG
{__WARN__} =
sub
{
$warning
=
shift
;};
(
my
$m
= new WWW::Scripter)->use_plugin(
'JavaScript'
,
engine
=>
"JE"
);
$m
->get(URI::file->new_abs(
't/js-die.html'
));
like
$warning
,
qr/line 8(?!\d)/
,
'line numbers for inline scripts'
;
SKIP :{
skip
"requires HTML::DOM 0.012 or higher"
, 1
if
HTML::DOM->VERSION < 0.012;
$m
->document->getElementsByTagName(
'a'
)->[0]->
trigger_event(
'click'
);
like
$warning
,
qr/line 11(?!\d)/
,
'line numbers for event attributes'
;
}
}
{
$m
->
eval
('
is(typeof this.screen,
"object"
,
"screen object"
);
');
}
{
$m
->
eval
('
open
(
"foo"
); // this will be a 404
');
like
$m
->uri,
qr/foo$/
,
'url after open()'
;
$m
->back;
like
$m
->uri,
qr/blank\.html$/
,
'open() adds to the history'
;
}
{
$m
->
eval
('
is(typeof this.navigator,
"object"
,
"navigator object"
);
is(navigator.appName,
"WWW::Scripter"
,
"navigator.appName"
);
') or diag $@;
}
{
$m
->get( data_url
'<script>foo="bar",baz=1</script>'
);
$m
->get( data_url
'<script>foo="baz"</script>'
);
is
$m
->
eval
(
'foo+window.baz'
),
'bazundefined'
,
'which JS env are we in after going to another page?'
;
$m
->back;
is
$m
->
eval
(
'foo+window.baz'
),
'bar1'
,
'and which one after we go back?'
;
$m
->back;
}
{
$m
->
eval
(
'is(location, location.href, "location stringification")'
);
}
{
my
$uri
=
$m
->uri;
$m
->get(
"Javascript:%20foo=%22ba%ca%80%22"
);
is
$m
->
eval
(
'foo'
),
'baʀ'
,
'javascript: URLs are executed'
or diag $@;
is
$m
->uri,
$uri
,
' and do not affect the page stack'
or diag
$m
->response->as_string;
}
{
(
my
$m
= new WWW::Scripter)->use_plugin(
'JavaScript'
);
$m
->get(
'data:text/plain,'
);
is
eval
{
$m
->
eval
(
"35"
)}, 35,
'JS is available even when the page is not HTML'
or diag $@;
}
{
my
$alert
;
(
my
$m
= new WWW::Scripter)->use_plugin(
JavaScript
=>);
$m
->set_alert_function(
sub
{
$alert
=
shift
} );
$m
->get(data_url
<<"_");
<html>
<head>
<script type="text/javascript" src="${\data_url(<<'__')}"></script>
<!--
window.alert("hello wrodl");
//-->
__
</head>
<body>
</body>
</html>
_
is
$alert
,
"hello wrodl"
,
'<!-- in external JS file'
;
$m
->get(data_url
<<'_');
<script>
<!--
window.alert("foobar");
-->
</script>
_
is
$alert
,
"foobar"
,
'trailing --> without //'
;
$m
->get(
'javascript:<!--%0aalert("hoetn")'
);
is
$alert
,
"hoetn"
,
"javascript:<!--%0a URLs"
;
my
$warning
;
local
$SIG
{__WARN__} =
sub
{
$warning
=
shift
;};
$m
->get(data_url
<<'_');
<title></title>>
<script type='application/javascript'>
<!--
pweegonk() // line 6
</script>
_
like
$warning
,
qr/line 6/
,
'line numbers after <script>\n\n\n<!--'
;
}
{
$m
->get(
my
$url
= data_url <<
''
);
<form name=f onsubmit=
'return false'
action=
"404"
>
$m
->submit;
is
$m
->uri,
$url
,
'<form onsubmit="return false">'
or back
$m
;
like
$m
->
eval
(
" document.f.onsubmit "
),
qr/return false/
,
'form.onsubmit returns a JS function when assigned via the HTML attr'
;
$m
->
eval
(
" document.f.onsubmit = function(){ return false } "
);
$m
->submit;
is
$m
->uri,
$url
,
'form.onsubmit=function(){return false}'
;
}
{
my
$iframe_url
= data_url <<
''
;
<script>top.pass = this == top[0]</script>
$m
->get(
my
$url
= data_url
"<iframe src='$iframe_url'>"
);
ok
$m
->
eval
(
'pass'
),
'scripts in iframes run in the correct JS environment'
;
}