Skip to content

Commit

Permalink
Fixes mikechambers#114 - In non-strict mode, accept non-breaking spac…
Browse files Browse the repository at this point in the history
…e as whitespace.
  • Loading branch information
darronschall committed Aug 3, 2009
1 parent 53e316a commit b0593f3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/com/adobe/serialization/json/JSONTokenizer.as
Expand Up @@ -618,7 +618,18 @@ package com.adobe.serialization.json {
*/
private function isWhiteSpace( ch:String ):Boolean
{
return ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' );
// Check for the whitespace defined in the spec
if ( ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' )
{
return true;
}
// If we're not in strict mode, we also accept non-breaking space
else if ( !strict && ch.charCodeAt( 0 ) == 160 )
{
return true;
}

return false;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/src/com/adobe/serialization/json/JSONTest.as
Expand Up @@ -350,6 +350,7 @@ package com.adobe.serialization.json
public function testDecodeWhiteSpace():void
{
var n:Number;
var nbsp:String = String.fromCharCode( 160 ); // non-breaking space

n = JSON.decode( " 1 " );
assertEquals( 1, n );
Expand All @@ -366,6 +367,17 @@ package com.adobe.serialization.json
// Verify combined before/after spacing
n = JSON.decode( "\t \n\n\r \r\n\t 100 \r\n\t\r\r\r\n \n" ) as Number
assertEquals( 100, n );

// In non-strict mode, we should also accept non breaking space
n = JSON.decode( "\t \n"
+ nbsp
+ "\n\r \r\n\t 100 \r\n\t\r\r\r\n"
+ nbsp
+ " \n", false ) as Number
assertEquals( 100, n );

// In strict mode, we do NOT accept non breaking space, so expect a parse error
expectParseError( "\t \n" + nbsp + "\n\r \r\n\t 100 \r\n\t\r\r\r\n" + nbsp + " \n" );
}

public function testDecodeWithCharactersLeftInInputString():void
Expand Down

0 comments on commit b0593f3

Please sign in to comment.