1N/A <
title>Test Page</
title>
1N/A <
style type="text/css" id="styleblock" class="highlight-ignore">
1N/A font: normal 125%/1.4 Arial, sans-serif;
1N/A .yui-skin-sam .yui-console .yui-console-content {
1N/A .yui-skin-sam .yui-console-entry-pass .yui-console-entry-cat {
1N/A .yui-skin-sam .yui-console-entry-fail .yui-console-entry-cat {
1N/A .highlight-example {
.yui-skin-sam .yui-console-entry-time {
<
body class="yui-skin-sam">
<
h3>Form used for field value extraction, stringification</
h3>
<
input type="text" id="empty_text">
<
input type="text" id="text" value="text">
<
input type="radio" name="radio" id="unchecked_radio" value="unchecked">
<
input type="radio" name="radio" id="checked_radio" value="radio" checked="checked">
<
input type="checkbox" name="box" id="unchecked_box" value="unchecked">
<
input type="checkbox" name="box" id="checked_box" value="box" checked="checked">
<
textarea id="empty_textarea"></
textarea>
<
textarea id="textarea">textarea</
textarea>
<
option value="unselected">Unselected</
option>
<
option value="selected" selected="selected">Selected</
option>
<
select id="multiple_select" multiple="multiple" size="3">
<
option value="unselected">Unselected</
option>
<
option value="selected" selected="selected">Selected</
option>
<
option value="selected also" selected="selected">Selected also</
option>
<
button id="button" type="button">content; no value</
button>
<
button id="button_with_value" type="button" value="button value">content and value</
button>
<
button id="button_submit" type="submit">content; no value</
button>
<
button id="button_submit_with_value" type="submit" value="submit button value">content and value</
button>
<
input type="button" id="input_button" value="input button">
<
input type="submit" id="input_submit" value="input submit">
<
input type="image" id="input_image" src="404.png" value="input image">
useBrowserConsole : false,
logInclude : { TestRunner: true, json: true, JSON: true }
}).use('test','console','json',function (Y) {
"JSON Test Pattern pass1",\
{"object with 1 member":["array with 1 element"]},\
"controls": "\\b\\f\\n\\r\\t",\
"alpha": "abcdefghijklmnopqrstuvwxyz",\
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",\
"special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",\
"hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",\
"address": "50 St. James Street",\
"comment": "// /*
<!-- --",\ " s p a c e d " :[1,2 , 3\
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],\
"jsontext": "{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}",\
"quotes": "" \\u0022 %22 0x22 034 "",\
"\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?" : "A key can be any string"\
/*****************************/
/*****************************/
// wrapped in a function to allow repeating tests with native behavior disabled
test_failOnEmptyString : true,
test_failOnFunction : true,
test_failOnUnquotedVal : true,
test_failOnUnquotedKey : true,
test_failOnUnclosedObject : true,
test_failOnUnclosedArray : true,
test_failOnExtraCommaInObject : true,
test_failOnDoubleExtraCommaInObject : true,
test_failOnExtraCommaInArray : true,
test_failOnDoubleExtraCommaInArray : true,
test_failOnMissingValue : true,
test_failOnCommaAfterClose : true,
test_failOnValueAfterClose : true,
test_failOnExtraClose : true,
test_failOnExpression : true,
test_failOnZeroPrefixedNumber : true,
test_failOnIllegalBackslashEscape : true,
test_failOnMissingColon : true,
test_failOnDoubleColon : true,
test_failOnCommaInsteadOfColon : true,
test_failOnExtraClose : true,
test_failOnColonInsteadOfComma : true,
test_failOnSingleQuote : true,
test_failOnTabCharacter : true,
test_failOnLineBreakChar : true,
test_failOnMismatchedClose : true
test_emptyObject : function () {
test_emptyArray : function () {
test_JSONNatives : function () {
// Note: backslashes are double escaped to emulate string returned from
var data =
Y.JSON.parse('{"obj":{},"arr":[],"f":false,"t":true,"n":null,"int":12345,"fl":1.2345,"str":"String\\nwith\\tescapes"}');
test_basics : function () {
Y.Assert.areSame("{\"object with 1 member\":[\"array with 1 element\"]}",data[8].jsontext);
Y.Assert.areSame("A key can be any string",data[8]["\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"]);
test_nonObjectWrapper : function () {
test_failOnEmptyString : function () {
// parse should throw an error
Y.log("Parsed empty string, but should have failed.","warn","TestRunner");
test_failOnFunction : function () {
// parse should throw an error
Y.log("Parsed a function, but should have failed.","warn","TestRunner");
test_failOnRegex : function () {
// parse should throw an error
Y.log("Parsed regular expression literal, but should have failed.","warn","TestRunner");
test_failOnNew : function () {
// parse should throw an error
Y.log("Parsed <
code>new Date()</
code>, but should have failed.","warn","TestRunner");
test_failOnUnquotedVal : function () {
// parse should throw an error
Y.log("Parsed unquoted non-native value, but should have failed.","warn","TestRunner");
test_failOnUnquotedKey : function () {
// parse should throw an error
Y.log("Parsed unquoted object key, but should have failed.","warn","TestRunner");
test_failOnUnclosedObject : function () {
// parse should throw an error
Y.log("Parsed unclosed object, but should have failed.","warn","TestRunner");
test_failOnUnclosedArray : function () {
// parse should throw an error
Y.log("Parsed unclosed array, but should have failed.","warn","TestRunner");
test_failOnExtraCommaInObject : function () {
// JS validator will allow, FF 3.1b2 native will allow. IE8 errors.
// eval will fail in IE6-7, but pass in others
// Trailing commas are invalid, but not a security risk, so acceptable
Y.log("Parsed object with extra comma, but should have failed.","warn","TestRunner");
throw new Error("Parsed object with extra comma, but should have failed.");
test_failOnDoubleExtraCommaInObject : function () {
// parse should throw an error
Y.log("Parsed object with two extra commas, but should have failed.","warn","TestRunner");
test_failOnExtraCommaInArray : function () {
// Correct failure in IE6-8. FF accepts trailing commas without error
// Trailing commas are invalid, but not a security risk, so acceptable
Y.log("Parsed array with extra comma, but should have failed.","warn","TestRunner");
throw new Error("Parsed array with extra comma, but should have failed.");
test_failOnDoubleExtraCommaInArray : function () {
// Correct failure in IE6-8. FF accepts trailing commas without error
// Trailing commas are invalid, but not a security risk, so acceptable
Y.log("Parsed array with two extra commas, but should have failed.","warn","TestRunner");
throw new Error("Parsed array with two extra commas, but should have failed.");
test_failOnMissingValue : function () {
// Correct failure in IE6-8. FF accepts trailing commas without error
// Trailing commas are invalid, but not a security risk, so acceptable
Y.log("Parsed array with missing value (
"+data[0]+"),
but should have failed.
","warn","TestRunner"); throw new Error("Parsed array with missing value (
"+data[0]+"),
but should have failed.
"); test_failOnCommaAfterClose : function () { // parse should throw an error Y.log("Parsed comma after array close,
but should have failed.
","warn","TestRunner"); test_failOnValueAfterClose : function () { // parse should throw an error Y.log("Parsed string value after object close,
but should have failed.
","warn","TestRunner"); test_failOnExtraClose : function () { // parse should throw an error Y.log("Parsed extra closing curly brace on object,
but should have failed.
","warn","TestRunner"); throw new Error("Parsed extra closing curly brace on object,
but should have failed.
"); test_failOnExpression : function () { // parse should throw an error Y.log("Parsed expression,
but should have failed.
","warn","TestRunner"); test_failOnZeroPrefixedNumber : function () { // Correct failure in IE8. FF accepts leading zeros without error // Leading zeros are invalid, but not a security risk, so acceptable Y.log("Parsed zero prefixed number,
but should have failed.
","warn","TestRunner"); throw new Error("Parsed zero prefixed number,
but should have failed.
"); test_failOnHex : function () { // parse should throw an error Y.log("Parsed hex value,
but should have failed.
","warn","TestRunner"); test_failOnIllegalBackslashEscape : function () { // Correctly fails in all but IE8's native parse. // The spec does not specify a limitation to the escape characters a // decoder supports, so either is acceptable. var data = Y.JSON.parse('["illegal backslash escape: \\
x15"]'); Y.log("Parsed illegal backslash escape \\
x15,
but should have failed.
","warn","TestRunner"); throw new Error("Parsed illegal backslash escape \\
x15,
but should have failed.
"); test_failOnMissingColon : function () { // parse should throw an error Y.log("Parsed object with missing colon,
but should have failed.
","warn","TestRunner"); test_failOnDoubleColon : function () { // parse should throw an error Y.log("Parsed double colon in object,
but should have failed.
","warn","TestRunner"); test_failOnCommaInsteadOfColon : function () { // parse should throw an error Y.log("Parsed comma in place of colon,
but should have failed.
","warn","TestRunner"); test_failOnColonInsteadOfComma : function () { // parse should throw an error Y.log("Parsed colon in place of comma,
but should have failed.
","warn","TestRunner"); test_failOnSingleQuote : function () { // parse should throw an error Y.log("Parsed single quote,
but should have failed.
","warn","TestRunner"); test_failOnLineBreakChar : function () { // FF3.1b2 currently allows linebreak chars in native implementation // Harmless, so permissable Y.log("Parsed unescaped line break character,
but should have failed.
","warn","TestRunner"); throw new Error("Parsed unescaped line break character,
but should have failed.
"); test_failOnMismatchedClose : function () { // parse should throw an error Y.log("Parsed curly brace close for array,
but should have failed.
","warn","TestRunner"); test_failOnStringifyCyclicalRef1 : true, test_failOnStringifyCyclicalRef2 : true, test_failOnStringifyCyclicalRef3 : true test_stringifyNatives: function () { Y.Assert.areSame('[true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}]', Y.JSON.stringify([true,false,null,-0.12345,"string",{"object with one member":["array with one element"]}])); test_stringifyObject : function () { // stringify sorts the keys Y.Assert.areSame('{"one":1,"two":true,"three":false,"four":null,"five":"String with\\
nnewline","six":{"nested":-0.12345}}', Y.JSON.stringify({one:1,two:true,three:false,four:null,five:"String with\
nnewline",six : {nested:-0.12345}})); test_failOnStringifyCyclicalRef1 : function () { var o = { key: 'value' }; Y.log("Stringified Object with cyclical reference,
but should have failed.
","warn","TestRunner"); test_failOnStringifyCyclicalRef2 : function () { Y.log("Stringified Array with cyclical reference,
but should have failed.
","warn","TestRunner"); test_failOnStringifyCyclicalRef3 : function () { var o = [1,2,3,{key:"value",nest:[4,5,6,{foo:"bar"}]}]; o[4] = o[3].x = o[3].nest[4] = o[3].nest[3].y = o[3].nest[3].z = o; Y.log("Stringified Object with cyclical reference,
but should have failed.
","warn","TestRunner"); test_stringifyFunction : function () { functions : function (are,ignored) {}, test_stringifyRegex : function () { regex : /are treated as objects/, arr : [ new RegExp("in array") ] test_stringifyUndefined : function () { test_stringifyDate : function () { // This assumes Pacific Time Zone (GMT -7:00) test_stringifyFormValue : function () { empty_text : $('empty_text').value, unchecked_radio : $('unchecked_radio').value, checked_radio : $('checked_radio').value, unchecked_box : $('unchecked_box').value, checked_box : $('checked_box').value, empty_textarea : $('empty_textarea').value, textarea : $('textarea').value, select : $('select').value, multiple_select : $('multiple_select').value, button : $('button').value, button_with_value : $('button_with_value').value, button_submit : $('button_submit').value, button_submit_with_value: $('button_submit_with_value').value, input_button : $('input_button').value, input_submit : $('input_submit').value, input_image : $('input_image').value '"unchecked_radio":"unchecked",'+ '"checked_radio":"radio",'+ '"unchecked_box":"unchecked",'+ '"textarea":"textarea",'+ '"multiple_select":"selected",'+ '"button_with_value":"button value",'+ '"button_submit_with_value":"submit button value",'+ '"input_button":"input button",'+ '"input_submit":"input submit",'+ '"input_image":"input image"}', test_emptyWhitelistArray : function () { },["string",undefined,/some regex/] test_whitelistArray : function () { baz : true},["foo","baz"])); // REMOVED for spec compatibility test_whitelistObject : function () { // (undocumented) supports an obj literal as well }, {foo : true, baz : false})); // values ignored test_falseyIndents : function () { /* Commented out because FF3.5 has infinite loop bug for neg indents * Fixed in FF for next version. test_indentNumber : function () { test_indentString : function () { XoXoXoXoXoXoXo\
"deep enough\":
7\
n\
test_reviver : function () { case "alpha" : return "LOWER CASE"; case "ALPHA" : return "upper case"; case "null" : return undefined; if (typeof v === 'number') { test_replacer : function () { // replacer applies to even simple value stringifications return typeof(v) === 'number' ? v * 2 : v; // replacer is applied to every nested property as well. // can modify the host object en route // executes from the context of the key:value container Y.Assert.areSame('{"num":2,"alpha":"ABC","obj":{"nested_num":100,"alpha":"abc"},"arr":[2,null,4]}', // this property should then be ignored } else if (k === 'ignore') { // this property should then be ignored } else if (t === 'number') { // undefined returned to arrays should become null return v % 7 ? v * 2 : undefined; // this refers to the object containing the key:value // modify the object during stringification // replacer works in conjunction with indent Y.Assert.areSame("{\
n_\
"num\":
2,\
n_\
"alpha\": \
"ABC\",\
n_\
"obj\": {\
n__\
"nested_num\":
100,\
n__\
"alpha\": \
"abc\"\
n_},\
n_\
"arr\": [\
n__2,\
n__null,\
n__4\
n_]\
n}
", // this property should then be ignored } else if (k === 'ignore') { // this property should then be ignored } else if (t === 'number') { // undefined returned to arrays should become null return v % 7 ? v * 2 : undefined; // this refers to the object containing the key:value // modify the object during stringification // If native JSON is available, run the tests again without native calls test_disableNative : function () { Y.log("Rerunning tests without native JSON API","section","TestRunner");