Browse Source

Fixed escaping in JSON.stringify

Dmitry Panov 9 years ago
parent
commit
a5faf93e7a
2 changed files with 30 additions and 21 deletions
  1. 21 21
      builtin_json.go
  2. 9 0
      runtime_test.go

+ 21 - 21
builtin_json.go

@@ -142,7 +142,7 @@ func (r *Runtime) builtinJSON_stringify(call FunctionCall) Value {
 			seen := map[string]bool{}
 			propertyList := make([]Value, length)
 			length = 0
-			for index, _ := range propertyList {
+			for index := range propertyList {
 				var name string
 				value := replacer.self.get(intToValue(int64(index)))
 				if s, ok := value.assertString(); ok {
@@ -410,28 +410,28 @@ func (ctx *_builtinJSON_stringifyContext) quote(str valueString) {
 		if err != nil {
 			break
 		}
-		if r < 0x0020 {
-			switch r {
-			case '"', '\\':
-				ctx.buf.WriteByte('\\')
-				ctx.buf.WriteByte(byte(r))
-			case 0x0008:
-				ctx.buf.WriteString("\\b")
-			case 0x0009:
-				ctx.buf.WriteString("\\t")
-			case 0x000A:
-				ctx.buf.WriteString("\\n")
-			case 0x000C:
-				ctx.buf.WriteString("\\f")
-			case 0x000D:
-				ctx.buf.WriteString("\\r")
-			default:
+		switch r {
+		case '"', '\\':
+			ctx.buf.WriteByte('\\')
+			ctx.buf.WriteByte(byte(r))
+		case 0x08:
+			ctx.buf.WriteString(`\b`)
+		case 0x09:
+			ctx.buf.WriteString(`\t`)
+		case 0x0A:
+			ctx.buf.WriteString(`\n`)
+		case 0x0C:
+			ctx.buf.WriteString(`\f`)
+		case 0x0D:
+			ctx.buf.WriteString(`\r`)
+		default:
+			if r < 0x20 {
 				ctx.buf.WriteString(`\u00`)
-				ctx.buf.WriteByte(hex[r>>4])
-				ctx.buf.WriteByte(hex[r&0xF])
+				ctx.buf.WriteByte(hex[r >> 4])
+				ctx.buf.WriteByte(hex[r & 0xF])
+			} else {
+				ctx.buf.WriteRune(r)
 			}
-		} else {
-			ctx.buf.WriteRune(r)
 		}
 	}
 	ctx.buf.WriteByte('"')

+ 9 - 0
runtime_test.go

@@ -486,6 +486,15 @@ func TestGoFuncError(t *testing.T) {
 	}
 }
 
+func TestJSONEscape(t *testing.T) {
+	const SCRIPT = `
+	var a = "\\+1";
+	JSON.stringify(a);
+	`
+
+	testScript1(SCRIPT, asciiString(`"\\+1"`), t)
+}
+
 /*
 func TestArrayConcatSparse(t *testing.T) {
 function foo(a,b,c)