2007-01-18 22:24:27 +00:00
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
|
|
|
<head>
|
|
|
|
<title>Prototype Unit test file</title>
|
|
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
|
|
|
|
<script src="../../dist/prototype.js" type="text/javascript"></script>
|
|
|
|
<script src="../lib/unittest.js" type="text/javascript"></script>
|
|
|
|
<link rel="stylesheet" href="../test.css" type="text/css" />
|
|
|
|
<style type="text/css" media="screen">
|
|
|
|
/* <![CDATA[ */
|
|
|
|
#testcss1 { font-size:11px; color: #f00; }
|
|
|
|
#testcss2 { font-size:12px; color: #0f0; display: none; }
|
|
|
|
/* ]]> */
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Prototype Unit test file</h1>
|
|
|
|
<p>
|
|
|
|
Test of utility functions in string.js
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<!-- Log output -->
|
|
|
|
<div id="testlog"> </div>
|
|
|
|
|
|
|
|
<!-- Tests follow -->
|
|
|
|
<script type="text/javascript" language="javascript" charset="utf-8">
|
|
|
|
// <![CDATA[
|
2007-03-09 03:23:24 +00:00
|
|
|
var attackTarget;
|
2007-01-18 22:24:27 +00:00
|
|
|
var evalScriptsCounter = 0,
|
|
|
|
largeTextEscaped = '<span>test</span>',
|
|
|
|
largeTextUnescaped = '<span>test</span>';
|
|
|
|
(2048).times(function(){
|
|
|
|
largeTextEscaped += ' ABC';
|
|
|
|
largeTextUnescaped += ' ABC';
|
|
|
|
});
|
|
|
|
|
|
|
|
new Test.Unit.Runner({
|
2008-03-16 18:53:51 +00:00
|
|
|
testInterpret: function(){
|
|
|
|
this.assertIdentical('true', String.interpret(true));
|
|
|
|
this.assertIdentical('123', String.interpret(123));
|
|
|
|
this.assertIdentical('foo bar', String.interpret('foo bar'));
|
|
|
|
this.assertIdentical(
|
2007-01-18 22:24:27 +00:00
|
|
|
'object string',
|
|
|
|
String.interpret({ toString: function(){ return 'object string' } }));
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertIdentical('0', String.interpret(0));
|
|
|
|
this.assertIdentical('false', String.interpret(false));
|
|
|
|
this.assertIdentical('', String.interpret(undefined));
|
|
|
|
this.assertIdentical('', String.interpret(null));
|
|
|
|
this.assertIdentical('', String.interpret(''));
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testGsubWithReplacementFunction: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = 'foo boo boz';
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('Foo Boo BoZ',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/[^o]+/, function(match) {
|
|
|
|
return match[0].toUpperCase()
|
|
|
|
}));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('f2 b2 b1z',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/o+/, function(match) {
|
|
|
|
return match[0].length;
|
|
|
|
}));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('f0 b0 b1z',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/o+/, function(match) {
|
|
|
|
return match[0].length % 2;
|
|
|
|
}));
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testGsubWithReplacementString: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = 'foo boo boz';
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('foobooboz',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/\s+/, ''));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(' z',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/(.)(o+)/, ''));
|
2007-03-28 11:35:05 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('ウィメンズ2007<br/>クルーズコレクション',
|
2007-03-28 11:35:05 +00:00
|
|
|
'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'<br/>'));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('ウィメンズ2007<br/>クルーズコレクション',
|
2007-03-28 11:35:05 +00:00
|
|
|
'ウィメンズ2007\nクルーズコレクション'.gsub('\n','<br/>'));
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testGsubWithReplacementTemplateString: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = 'foo boo boz';
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('-oo-#{1}- -oo-#{1}- -o-#{1}-z',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/(.)(o+)/, '-#{2}-\\#{1}-'));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('-foo-f- -boo-b- -bo-b-z',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/(.)(o+)/, '-#{0}-#{1}-'));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('-oo-f- -oo-b- -o-b-z',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/(.)(o+)/, '-#{2}-#{1}-'));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(' z',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.gsub(/(.)(o+)/, '#{3}'));
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testSubWithReplacementFunction: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = 'foo boo boz';
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('Foo boo boz',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.sub(/[^o]+/, function(match) {
|
|
|
|
return match[0].toUpperCase()
|
|
|
|
}), 1);
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('Foo Boo boz',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.sub(/[^o]+/, function(match) {
|
|
|
|
return match[0].toUpperCase()
|
|
|
|
}, 2), 2);
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(source,
|
2007-01-18 22:24:27 +00:00
|
|
|
source.sub(/[^o]+/, function(match) {
|
|
|
|
return match[0].toUpperCase()
|
|
|
|
}, 0), 0);
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(source,
|
2007-01-18 22:24:27 +00:00
|
|
|
source.sub(/[^o]+/, function(match) {
|
|
|
|
return match[0].toUpperCase()
|
|
|
|
}, -1), -1);
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testSubWithReplacementString: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = 'foo boo boz';
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('oo boo boz',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.sub(/[^o]+/, ''));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('oooo boz',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.sub(/[^o]+/, '', 2));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('-f-oo boo boz',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.sub(/[^o]+/, '-#{0}-'));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('-f-oo- b-oo boz',
|
2007-01-18 22:24:27 +00:00
|
|
|
source.sub(/[^o]+/, '-#{0}-', 2));
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testScan: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = 'foo boo boz', results = [];
|
2007-07-24 16:30:19 +00:00
|
|
|
var str = source.scan(/[o]+/, function(match) {
|
2007-01-18 22:24:27 +00:00
|
|
|
results.push(match[0].length);
|
|
|
|
});
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEnumEqual([2, 2, 1], results);
|
|
|
|
this.assertEqual(source, source.scan(/x/, this.fail));
|
|
|
|
this.assert(typeof str == 'string');
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testToArray: function() {
|
|
|
|
this.assertEnumEqual([],''.toArray());
|
|
|
|
this.assertEnumEqual(['a'],'a'.toArray());
|
|
|
|
this.assertEnumEqual(['a','b'],'ab'.toArray());
|
|
|
|
this.assertEnumEqual(['f','o','o'],'foo'.toArray());
|
|
|
|
},
|
2007-07-24 17:24:25 +00:00
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
/*
|
|
|
|
Note that camelize() differs from its Rails counterpart,
|
|
|
|
as it is optimized for dealing with JavaScript object
|
|
|
|
properties in conjunction with CSS property names:
|
|
|
|
- Looks for dashes, not underscores
|
|
|
|
- CamelCases first word if there is a front dash
|
|
|
|
*/
|
2008-03-16 18:53:51 +00:00
|
|
|
testCamelize: function() {
|
|
|
|
this.assertEqual('', ''.camelize());
|
|
|
|
this.assertEqual('', '-'.camelize());
|
|
|
|
this.assertEqual('foo', 'foo'.camelize());
|
|
|
|
this.assertEqual('foo_bar', 'foo_bar'.camelize());
|
|
|
|
this.assertEqual('FooBar', '-foo-bar'.camelize());
|
|
|
|
this.assertEqual('FooBar', 'FooBar'.camelize());
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('fooBar', 'foo-bar'.camelize());
|
|
|
|
this.assertEqual('borderBottomWidth', 'border-bottom-width'.camelize());
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('classNameTest','class-name-test'.camelize());
|
|
|
|
this.assertEqual('classNameTest','className-test'.camelize());
|
|
|
|
this.assertEqual('classNameTest','class-nameTest'.camelize());
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
/* this.benchmark(function(){
|
2007-01-18 22:24:27 +00:00
|
|
|
'class-name-test'.camelize();
|
|
|
|
},10000); */
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testCapitalize: function() {
|
|
|
|
this.assertEqual('',''.capitalize());
|
|
|
|
this.assertEqual('Ä','ä'.capitalize());
|
|
|
|
this.assertEqual('A','A'.capitalize());
|
|
|
|
this.assertEqual('Hello','hello'.capitalize());
|
|
|
|
this.assertEqual('Hello','HELLO'.capitalize());
|
|
|
|
this.assertEqual('Hello','Hello'.capitalize());
|
|
|
|
this.assertEqual('Hello world','hello WORLD'.capitalize());
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testUnderscore: function() {
|
|
|
|
this.assertEqual('', ''.underscore());
|
|
|
|
this.assertEqual('_', '-'.underscore());
|
|
|
|
this.assertEqual('foo', 'foo'.underscore());
|
|
|
|
this.assertEqual('foo', 'Foo'.underscore());
|
|
|
|
this.assertEqual('foo_bar', 'foo_bar'.underscore());
|
|
|
|
this.assertEqual('border_bottom', 'borderBottom'.underscore());
|
|
|
|
this.assertEqual('border_bottom_width', 'borderBottomWidth'.underscore());
|
|
|
|
this.assertEqual('border_bottom_width', 'border-Bottom-Width'.underscore());
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testDasherize: function() {
|
|
|
|
this.assertEqual('', ''.dasherize());
|
|
|
|
this.assertEqual('foo', 'foo'.dasherize());
|
|
|
|
this.assertEqual('Foo', 'Foo'.dasherize());
|
|
|
|
this.assertEqual('foo-bar', 'foo-bar'.dasherize());
|
|
|
|
this.assertEqual('border-bottom-width', 'border_bottom_width'.dasherize());
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTruncate: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = 'foo boo boz foo boo boz foo boo boz foo boo boz';
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(source, source.truncate(source.length));
|
|
|
|
this.assertEqual('foo boo boz foo boo boz foo...', source.truncate(0));
|
|
|
|
this.assertEqual('fo...', source.truncate(5));
|
|
|
|
this.assertEqual('foo b', source.truncate(5, ''));
|
2007-05-31 22:32:25 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assert(typeof 'foo'.truncate(5) == 'string');
|
|
|
|
this.assert(typeof 'foo bar baz'.truncate(5) == 'string');
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testStrip: function() {
|
|
|
|
this.assertEqual('hello world', ' hello world '.strip());
|
|
|
|
this.assertEqual('hello world', 'hello world'.strip());
|
|
|
|
this.assertEqual('hello \n world', ' hello \n world '.strip());
|
|
|
|
this.assertEqual('', ' '.strip());
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testStripTags: function() {
|
|
|
|
this.assertEqual('hello world', 'hello world'.stripTags());
|
|
|
|
this.assertEqual('hello world', 'hello <span>world</span>'.stripTags());
|
|
|
|
this.assertEqual('hello world', '<a href="#" onclick="moo!">hello</a> world'.stripTags());
|
|
|
|
this.assertEqual('hello world', 'h<b><em>e</em></b>l<i>l</i>o w<span class="moo" id="x"><b>o</b></span>rld'.stripTags());
|
|
|
|
this.assertEqual('1\n2', '1\n2'.stripTags());
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testStripScripts: function() {
|
|
|
|
this.assertEqual('foo bar', 'foo bar'.stripScripts());
|
|
|
|
this.assertEqual('foo bar', ('foo <script>boo();<'+'/script>bar').stripScripts());
|
|
|
|
this.assertEqual('foo bar', ('foo <script type="text/javascript">boo();\nmoo();<'+'/script>bar').stripScripts());
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testExtractScripts: function() {
|
|
|
|
this.assertEnumEqual([], 'foo bar'.extractScripts());
|
|
|
|
this.assertEnumEqual(['boo();'], ('foo <script>boo();<'+'/script>bar').extractScripts());
|
|
|
|
this.assertEnumEqual(['boo();','boo();\nmoo();'],
|
2007-01-18 22:24:27 +00:00
|
|
|
('foo <script>boo();<'+'/script><script type="text/javascript">boo();\nmoo();<'+'/script>bar').extractScripts());
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEnumEqual(['boo();','boo();\nmoo();'],
|
2007-01-18 22:24:27 +00:00
|
|
|
('foo <script>boo();<'+'/script>blub\nblub<script type="text/javascript">boo();\nmoo();<'+'/script>bar').extractScripts());
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testEvalScripts: function() {
|
|
|
|
this.assertEqual(0, evalScriptsCounter);
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
('foo <script>evalScriptsCounter++<'+'/script>bar').evalScripts();
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(1, evalScriptsCounter);
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
var stringWithScripts = '';
|
|
|
|
(3).times(function(){ stringWithScripts += 'foo <script>evalScriptsCounter++<'+'/script>bar' });
|
|
|
|
stringWithScripts.evalScripts();
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(4, evalScriptsCounter);
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testEscapeHTML: function() {
|
|
|
|
this.assertEqual('foo bar', 'foo bar'.escapeHTML());
|
|
|
|
this.assertEqual('foo <span>bar</span>', 'foo <span>bar</span>'.escapeHTML());
|
|
|
|
this.assertEqual('foo ß bar', 'foo ß bar'.escapeHTML());
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('ウィメンズ2007\nクルーズコレクション',
|
2007-03-28 11:35:05 +00:00
|
|
|
'ウィメンズ2007\nクルーズコレクション'.escapeHTML());
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g',
|
2007-03-28 11:35:05 +00:00
|
|
|
'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.escapeHTML());
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(largeTextEscaped, largeTextUnescaped.escapeHTML());
|
2007-03-28 11:35:05 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('1\n2', '1\n2'.escapeHTML());
|
2007-03-29 17:39:48 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.benchmark(function() { largeTextUnescaped.escapeHTML() }, 1000);
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testUnescapeHTML: function() {
|
|
|
|
this.assertEqual('foo bar', 'foo bar'.unescapeHTML());
|
|
|
|
this.assertEqual('foo <span>bar</span>', 'foo <span>bar</span>'.unescapeHTML());
|
|
|
|
this.assertEqual('foo ß bar', 'foo ß bar'.unescapeHTML());
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g',
|
2007-01-18 22:24:27 +00:00
|
|
|
'a<a href="blah">blub</a>b<span><div></div></span>cdef<strong>!!!!</strong>g'.unescapeHTML());
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual(largeTextUnescaped, largeTextEscaped.unescapeHTML());
|
2007-03-28 11:35:05 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('1\n2', '1\n2'.unescapeHTML());
|
|
|
|
this.assertEqual('Pride & Prejudice', '<h1>Pride & Prejudice</h1>'.unescapeHTML());
|
2007-03-29 17:39:48 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.benchmark(function() { largeTextEscaped.unescapeHTML() }, 1000);
|
2007-03-29 17:39:48 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTemplateEvaluation: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = '<tr><td>#{name}</td><td>#{age}</td></tr>';
|
|
|
|
var person = {name: 'Sam', age: 21};
|
|
|
|
var template = new Template(source);
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('<tr><td>Sam</td><td>21</td></tr>',
|
2007-01-18 22:24:27 +00:00
|
|
|
template.evaluate(person));
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('<tr><td></td><td></td></tr>',
|
2007-01-18 22:24:27 +00:00
|
|
|
template.evaluate({}));
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTemplateEvaluationWithEmptyReplacement: function() {
|
2007-10-17 14:14:27 +00:00
|
|
|
var template = new Template('##{}');
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('#', template.evaluate({}));
|
|
|
|
this.assertEqual('#', template.evaluate({foo: 'bar'}));
|
2007-10-17 14:14:27 +00:00
|
|
|
|
|
|
|
template = new Template('#{}');
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('', template.evaluate({}));
|
|
|
|
},
|
2007-10-17 14:14:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTemplateEvaluationWithFalses: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
var source = '<tr><td>#{zero}</td><td>#{false_}</td><td>#{undef}</td><td>#{null_}</td><td>#{empty}</td></tr>';
|
|
|
|
var falses = {zero:0, false_:false, undef:undefined, null_:null, empty:""};
|
|
|
|
var template = new Template(source);
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('<tr><td>0</td><td>false</td><td></td><td></td><td></td></tr>',
|
2007-01-18 22:24:27 +00:00
|
|
|
template.evaluate(falses));
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-07-24 17:24:25 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTemplateEvaluationWithNested: function() {
|
2007-07-24 17:24:25 +00:00
|
|
|
var source = '#{name} #{manager.name} #{manager.age} #{manager.undef} #{manager.age.undef} #{colleagues.first.name}';
|
2008-03-16 18:53:51 +00:00
|
|
|
var subject = { manager: { name: 'John', age: 29 }, name: 'Stephan', age: 22, colleagues: { first: { name: 'Mark' }} };
|
|
|
|
this.assertEqual('Stephan', new Template('#{name}').evaluate(subject));
|
|
|
|
this.assertEqual('John', new Template('#{manager.name}').evaluate(subject));
|
|
|
|
this.assertEqual('29', new Template('#{manager.age}').evaluate(subject));
|
|
|
|
this.assertEqual('', new Template('#{manager.undef}').evaluate(subject));
|
|
|
|
this.assertEqual('', new Template('#{manager.age.undef}').evaluate(subject));
|
|
|
|
this.assertEqual('Mark', new Template('#{colleagues.first.name}').evaluate(subject));
|
|
|
|
this.assertEqual('Stephan John 29 Mark', new Template(source).evaluate(subject));
|
|
|
|
},
|
2007-07-24 17:24:25 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTemplateEvaluationWithIndexing: function() {
|
2007-07-24 17:24:25 +00:00
|
|
|
var source = '#{0} = #{[0]} - #{1} = #{[1]} - #{[2][0]} - #{[2].name} - #{first[0]} - #{[first][0]} - #{[\\]]} - #{first[\\]]}';
|
|
|
|
var subject = [ 'zero', 'one', [ 'two-zero' ] ];
|
|
|
|
subject[2].name = 'two-zero-name';
|
|
|
|
subject.first = subject[2];
|
|
|
|
subject[']'] = '\\';
|
|
|
|
subject.first[']'] = 'first\\';
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('zero', new Template('#{[0]}').evaluate(subject));
|
|
|
|
this.assertEqual('one', new Template('#{[1]}').evaluate(subject));
|
|
|
|
this.assertEqual('two-zero', new Template('#{[2][0]}').evaluate(subject));
|
|
|
|
this.assertEqual('two-zero-name', new Template('#{[2].name}').evaluate(subject));
|
|
|
|
this.assertEqual('two-zero', new Template('#{first[0]}').evaluate(subject));
|
|
|
|
this.assertEqual('\\', new Template('#{[\\]]}').evaluate(subject));
|
|
|
|
this.assertEqual('first\\', new Template('#{first[\\]]}').evaluate(subject));
|
|
|
|
this.assertEqual('empty - empty2', new Template('#{[]} - #{m[]}').evaluate({ '': 'empty', m: {'': 'empty2'}}));
|
|
|
|
this.assertEqual('zero = zero - one = one - two-zero - two-zero-name - two-zero - two-zero - \\ - first\\', new Template(source).evaluate(subject));
|
|
|
|
},
|
2007-07-24 17:24:25 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTemplateToTemplateReplacements: function() {
|
2007-07-24 17:24:25 +00:00
|
|
|
var source = 'My name is #{name}, my job is #{job}';
|
|
|
|
var subject = {
|
|
|
|
name: 'Stephan',
|
|
|
|
getJob: function() { return 'Web developer'; },
|
|
|
|
toTemplateReplacements: function() { return { name: this.name, job: this.getJob() } }
|
|
|
|
};
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('My name is Stephan, my job is Web developer', new Template(source).evaluate(subject));
|
|
|
|
},
|
2007-07-24 17:24:25 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTemplateEvaluationCombined: function() {
|
2007-07-24 17:24:25 +00:00
|
|
|
var source = '#{name} is #{age} years old, managed by #{manager.name}, #{manager.age}.\n' +
|
|
|
|
'Colleagues include #{colleagues[0].name} and #{colleagues[1].name}.';
|
|
|
|
var subject = {
|
|
|
|
name: 'Stephan', age: 22,
|
|
|
|
manager: { name: 'John', age: 29 },
|
|
|
|
colleagues: [ { name: 'Mark' }, { name: 'Indy' } ]
|
|
|
|
};
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('Stephan is 22 years old, managed by John, 29.\n' +
|
2007-07-24 17:24:25 +00:00
|
|
|
'Colleagues include Mark and Indy.',
|
|
|
|
new Template(source).evaluate(subject));
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-07-24 17:24:25 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testInterpolate: function() {
|
2007-07-24 17:24:25 +00:00
|
|
|
var subject = { name: 'Stephan' };
|
|
|
|
var pattern = /(^|.|\r|\n)(#\((.*?)\))/;
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('#{name}: Stephan', '\\#{name}: #{name}'.interpolate(subject));
|
|
|
|
this.assertEqual('#(name): Stephan', '\\#(name): #(name)'.interpolate(subject, pattern));
|
|
|
|
},
|
2007-07-24 17:24:25 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testToQueryParams: function() {
|
2007-03-27 20:55:56 +00:00
|
|
|
// only the query part
|
|
|
|
var result = {a:undefined, b:'c'};
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertHashEqual({}, ''.toQueryParams(), 'empty query');
|
|
|
|
this.assertHashEqual({}, 'foo?'.toQueryParams(), 'empty query with URL');
|
|
|
|
this.assertHashEqual(result, 'foo?a&b=c'.toQueryParams(), 'query with URL');
|
|
|
|
this.assertHashEqual(result, 'foo?a&b=c#fragment'.toQueryParams(), 'query with URL and fragment');
|
|
|
|
this.assertHashEqual(result, 'a;b=c'.toQueryParams(';'), 'custom delimiter');
|
2007-03-27 20:55:56 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertHashEqual({a:undefined}, 'a'.toQueryParams(), 'key without value');
|
|
|
|
this.assertHashEqual({a:'b'}, 'a=b&=c'.toQueryParams(), 'empty key');
|
|
|
|
this.assertHashEqual({a:'b', c:''}, 'a=b&c='.toQueryParams(), 'empty value');
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertHashEqual({'a b':'c', d:'e f', g:'h'},
|
2007-03-27 20:55:56 +00:00
|
|
|
'a%20b=c&d=e%20f&g=h'.toQueryParams(), 'proper decoding');
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertHashEqual({a:'b=c=d'}, 'a=b=c=d'.toQueryParams(), 'multiple equal signs');
|
|
|
|
this.assertHashEqual({a:'b', c:'d'}, '&a=b&&&c=d'.toQueryParams(), 'proper splitting');
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEnumEqual($w('r g b'), 'col=r&col=g&col=b'.toQueryParams()['col'],
|
2007-03-27 20:55:56 +00:00
|
|
|
'collection without square brackets');
|
|
|
|
var msg = 'empty values inside collection';
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEnumEqual(['r', '', 'b'], 'c=r&c=&c=b'.toQueryParams()['c'], msg);
|
|
|
|
this.assertEnumEqual(['', 'blue'], 'c=&c=blue'.toQueryParams()['c'], msg);
|
|
|
|
this.assertEnumEqual(['blue', ''], 'c=blue&c='.toQueryParams()['c'], msg);
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testInspect: function() {
|
|
|
|
this.assertEqual('\'\'', ''.inspect());
|
|
|
|
this.assertEqual('\'test\'', 'test'.inspect());
|
|
|
|
this.assertEqual('\'test \\\'test\\\' "test"\'', 'test \'test\' "test"'.inspect());
|
|
|
|
this.assertEqual('\"test \'test\' \\"test\\"\"', 'test \'test\' "test"'.inspect(true));
|
|
|
|
this.assertEqual('\'\\b\\t\\n\\f\\r"\\\\\'', '\b\t\n\f\r"\\'.inspect());
|
|
|
|
this.assertEqual('\"\\b\\t\\n\\f\\r\\"\\\\\"', '\b\t\n\f\r"\\'.inspect(true));
|
|
|
|
this.assertEqual('\'\\b\\t\\n\\f\\r\'', '\x08\x09\x0a\x0c\x0d'.inspect());
|
|
|
|
this.assertEqual('\'\\u001a\'', '\x1a'.inspect());
|
|
|
|
},
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testInclude: function() {
|
|
|
|
this.assert('hello world'.include('h'));
|
|
|
|
this.assert('hello world'.include('hello'));
|
|
|
|
this.assert('hello world'.include('llo w'));
|
|
|
|
this.assert('hello world'.include('world'));
|
|
|
|
this.assert(!'hello world'.include('bye'));
|
|
|
|
this.assert(!''.include('bye'));
|
|
|
|
},
|
2007-01-27 18:45:54 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testStartsWith: function() {
|
|
|
|
this.assert('hello world'.startsWith('h'));
|
|
|
|
this.assert('hello world'.startsWith('hello'));
|
|
|
|
this.assert(!'hello world'.startsWith('bye'));
|
|
|
|
this.assert(!''.startsWith('bye'));
|
|
|
|
this.assert(!'hell'.startsWith('hello'));
|
|
|
|
},
|
2007-01-27 18:45:54 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testEndsWith: function() {
|
|
|
|
this.assert('hello world'.endsWith('d'));
|
|
|
|
this.assert('hello world'.endsWith(' world'));
|
|
|
|
this.assert(!'hello world'.endsWith('planet'));
|
|
|
|
this.assert(!''.endsWith('planet'));
|
|
|
|
this.assert('hello world world'.endsWith(' world'));
|
|
|
|
this.assert(!'z'.endsWith('az'));
|
|
|
|
},
|
2007-01-27 18:45:54 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testBlank: function() {
|
|
|
|
this.assert(''.blank());
|
|
|
|
this.assert(' '.blank());
|
|
|
|
this.assert('\t\r\n '.blank());
|
|
|
|
this.assert(!'a'.blank());
|
|
|
|
this.assert(!'\t y \n'.blank());
|
|
|
|
},
|
2007-02-19 21:32:37 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testEmpty: function() {
|
|
|
|
this.assert(''.empty());
|
|
|
|
this.assert(!' '.empty());
|
|
|
|
this.assert(!'\t\r\n '.empty());
|
|
|
|
this.assert(!'a'.empty());
|
|
|
|
this.assert(!'\t y \n'.empty());
|
|
|
|
},
|
2007-02-19 21:32:37 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testSucc: function() {
|
|
|
|
this.assertEqual('b', 'a'.succ());
|
|
|
|
this.assertEqual('B', 'A'.succ());
|
|
|
|
this.assertEqual('1', '0'.succ());
|
|
|
|
this.assertEqual('abce', 'abcd'.succ());
|
|
|
|
this.assertEqual('{', 'z'.succ());
|
|
|
|
this.assertEqual(':', '9'.succ());
|
|
|
|
},
|
2007-03-09 03:23:24 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testTimes: function() {
|
2007-10-13 19:05:19 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('', ''.times(0));
|
|
|
|
this.assertEqual('', ''.times(5));
|
|
|
|
this.assertEqual('', 'a'.times(-1));
|
|
|
|
this.assertEqual('', 'a'.times(0));
|
|
|
|
this.assertEqual('a', 'a'.times(1));
|
|
|
|
this.assertEqual('aa', 'a'.times(2));
|
|
|
|
this.assertEqual('aaaaa', 'a'.times(5));
|
|
|
|
this.assertEqual('foofoofoofoofoo', 'foo'.times(5));
|
|
|
|
this.assertEqual('', 'foo'.times(-5));
|
2007-10-13 19:05:19 +00:00
|
|
|
|
|
|
|
/*window.String.prototype.oldTimes = function(count) {
|
|
|
|
var result = '';
|
|
|
|
for (var i = 0; i < count; i++) result += this;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.benchmark(function() {
|
2007-10-13 19:05:19 +00:00
|
|
|
'foo'.times(15);
|
|
|
|
}, 1000, 'new: ');
|
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.benchmark(function() {
|
2007-10-13 19:05:19 +00:00
|
|
|
'foo'.oldTimes(15);
|
|
|
|
}, 1000, 'previous: ');*/
|
2008-03-16 18:53:51 +00:00
|
|
|
},
|
2007-03-09 03:23:24 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testToJSON: function() {
|
|
|
|
this.assertEqual('\"\"', ''.toJSON());
|
|
|
|
this.assertEqual('\"test\"', 'test'.toJSON());
|
|
|
|
},
|
2007-06-17 16:03:38 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testIsJSON: function() {
|
|
|
|
this.assert(!''.isJSON());
|
|
|
|
this.assert(!' '.isJSON());
|
|
|
|
this.assert('""'.isJSON());
|
|
|
|
this.assert('"foo"'.isJSON());
|
|
|
|
this.assert('{}'.isJSON());
|
|
|
|
this.assert('[]'.isJSON());
|
|
|
|
this.assert('null'.isJSON());
|
|
|
|
this.assert('123'.isJSON());
|
|
|
|
this.assert('true'.isJSON());
|
|
|
|
this.assert('false'.isJSON());
|
|
|
|
this.assert('"\\""'.isJSON());
|
|
|
|
this.assert(!'\\"'.isJSON());
|
|
|
|
this.assert(!'new'.isJSON());
|
|
|
|
this.assert(!'\u0028\u0029'.isJSON());
|
2007-06-17 16:03:38 +00:00
|
|
|
// we use '@' as a placeholder for characters authorized only inside brackets,
|
2007-11-15 19:37:26 +00:00
|
|
|
// so this tests make sure it is not considered authorized elsewhere.
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assert(!'@'.isJSON());
|
|
|
|
},
|
2007-03-09 03:23:24 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
testEvalJSON: function() {
|
2007-11-15 19:37:26 +00:00
|
|
|
var valid = '{"test": \n\r"hello world!"}';
|
|
|
|
var invalid = '{"test": "hello world!"';
|
2007-03-09 03:23:24 +00:00
|
|
|
var dangerous = '{});attackTarget = "attack succeeded!";({}';
|
2007-06-19 23:31:41 +00:00
|
|
|
|
|
|
|
// use smaller huge string size for KHTML
|
|
|
|
var size = navigator.userAgent.include('KHTML') ? 20 : 100;
|
|
|
|
var longString = '"' + '123456789\\"'.times(size * 10) + '"';
|
2007-06-17 16:03:38 +00:00
|
|
|
var object = '{' + longString + ': ' + longString + '},';
|
2007-06-19 23:31:41 +00:00
|
|
|
var huge = '[' + object.times(size) + '{"test": 123}]';
|
2007-03-09 03:23:24 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('hello world!', valid.evalJSON().test);
|
|
|
|
this.assertEqual('hello world!', valid.evalJSON(true).test);
|
|
|
|
this.assertRaise('SyntaxError', function() { invalid.evalJSON() });
|
|
|
|
this.assertRaise('SyntaxError', function() { invalid.evalJSON(true) });
|
2007-03-09 03:23:24 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
attackTarget = "scared";
|
2007-03-09 03:23:24 +00:00
|
|
|
dangerous.evalJSON();
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual("attack succeeded!", attackTarget);
|
2007-03-09 03:23:24 +00:00
|
|
|
|
|
|
|
attackTarget = "Not scared!";
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertRaise('SyntaxError', function(){dangerous.evalJSON(true)});
|
|
|
|
this.assertEqual("Not scared!", attackTarget);
|
2007-04-24 03:31:14 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('hello world!', ('/*-secure- \r \n ' + valid + ' \n */').evalJSON().test);
|
2007-04-24 03:31:14 +00:00
|
|
|
var temp = Prototype.JSONFilter;
|
2007-06-13 20:57:19 +00:00
|
|
|
Prototype.JSONFilter = /^\/\*([\s\S]*)\*\/$/; // test custom delimiters.
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('hello world!', ('/*' + valid + '*/').evalJSON().test);
|
2007-04-24 03:31:14 +00:00
|
|
|
Prototype.JSONFilter = temp;
|
2007-06-17 16:03:38 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertMatch(123, huge.evalJSON(true).last().test);
|
2007-11-15 19:37:26 +00:00
|
|
|
|
2008-03-16 18:53:51 +00:00
|
|
|
this.assertEqual('', '""'.evalJSON());
|
|
|
|
this.assertEqual('foo', '"foo"'.evalJSON());
|
|
|
|
this.assert('object', typeof '{}'.evalJSON());
|
|
|
|
this.assert(Object.isArray('[]'.evalJSON()));
|
|
|
|
this.assertNull('null'.evalJSON());
|
|
|
|
this.assert(123, '123'.evalJSON());
|
|
|
|
this.assertIdentical(true, 'true'.evalJSON());
|
|
|
|
this.assertIdentical(false, 'false'.evalJSON());
|
|
|
|
this.assertEqual('"', '"\\""'.evalJSON());
|
|
|
|
}
|
2008-01-06 00:34:39 +00:00
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
// ]]>
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|