Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only escape forward slashes when encode_html_chars=True #114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 22 additions & 3 deletions lib/ultrajsonenc.c
Expand Up @@ -183,14 +183,21 @@ int Buffer_EscapeStringUnvalidated (JSONObjectEncoder *enc, const char *io, cons
}
case '\"': (*of++) = '\\'; (*of++) = '\"'; break;
case '\\': (*of++) = '\\'; (*of++) = '\\'; break;
case '/': (*of++) = '\\'; (*of++) = '/'; break;
case '/':
{
if (enc->encodeHTMLChars)
{
(*of++) = '\\';
}
(*of++) = '/'; break;
}
case '\b': (*of++) = '\\'; (*of++) = 'b'; break;
case '\f': (*of++) = '\\'; (*of++) = 'f'; break;
case '\n': (*of++) = '\\'; (*of++) = 'n'; break;
case '\r': (*of++) = '\\'; (*of++) = 'r'; break;
case '\t': (*of++) = '\\'; (*of++) = 't'; break;

case 0x26: // '/'
case 0x26: // '&'
case 0x3c: // '<'
case 0x3e: // '>'
{
Expand Down Expand Up @@ -413,14 +420,26 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char
io ++;
continue;
}
case 24:
{
if (enc->encodeHTMLChars)
{
// Fall through to \u00XX case 22 below.
}
else
{
// Same as case 1 above.
*(of++) = (*io++);
continue;
}
}
case 10:
case 12:
case 14:
case 16:
case 18:
case 20:
case 22:
case 24:
{
*(of++) = *( (char *) (g_escapeChars + utflen + 0));
*(of++) = *( (char *) (g_escapeChars + utflen + 1));
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.py
Expand Up @@ -41,7 +41,7 @@ def test_encodeDecimal(self):

def test_encodeStringConversion(self):
input = "A string \\ / \b \f \n \r \t </script> &"
not_html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t <\\/script> &"'
not_html_encoded = '"A string \\\\ / \\b \\f \\n \\r \\t </script> &"'
html_encoded = '"A string \\\\ \\/ \\b \\f \\n \\r \\t \\u003c\\/script\\u003e \\u0026"'

def helper(expected_output, **encode_kwargs):
Expand Down Expand Up @@ -174,7 +174,7 @@ def test_encodeStringConversion(self):
input = "A string \\ / \b \f \n \r \t"
output = ujson.encode(input)
self.assertEquals(input, json.loads(output))
self.assertEquals(output, '"A string \\\\ \\/ \\b \\f \\n \\r \\t"')
self.assertEquals(output, '"A string \\\\ / \\b \\f \\n \\r \\t"')
self.assertEquals(input, ujson.decode(output))

def test_decodeUnicodeConversion(self):
Expand Down