Bug 1480673 - Add nsContentUtils::StringifyJSON. r=qdot

Add nsContentUtils::StringifyJSON.

Differential Revision: https://phabricator.services.mozilla.com/D2700

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Zibi Braniecki 2018-08-07 15:49:13 +00:00
Родитель 66a657e508
Коммит de06dc8116
4 изменённых файлов: 78 добавлений и 0 удалений

Просмотреть файл

@ -11141,3 +11141,25 @@ nsContentUtils::CanShowPopup(nsIPrincipal* aPrincipal)
return !sDisablePopups;
}
static bool
JSONCreator(const char16_t* aBuf, uint32_t aLen, void* aData)
{
nsAString* result = static_cast<nsAString*>(aData);
result->Append(static_cast<const char16_t*>(aBuf),
static_cast<uint32_t>(aLen));
return true;
}
/* static */ bool
nsContentUtils::StringifyJSON(JSContext* aCx, JS::MutableHandle<JS::Value> aValue, nsAString& aOutStr)
{
MOZ_ASSERT(aCx);
aOutStr.Truncate();
JS::RootedValue value(aCx, aValue.get());
nsAutoString serializedValue;
NS_ENSURE_TRUE(JS_Stringify(aCx, &value, nullptr, JS::NullHandleValue,
JSONCreator, &serializedValue), false);
aOutStr = serializedValue;
return true;
}

Просмотреть файл

@ -3275,6 +3275,15 @@ public:
static bool CanShowPopup(nsIPrincipal* aPrincipal);
/**
* Serializes a JSON-like JS::Value into a string.
*
* Usage:
* nsAutoString serializedValue;
* nsContentUtils::StringifyJSON(cx, &value, serializedValue);
*/
static bool StringifyJSON(JSContext* aCx, JS::MutableHandle<JS::Value> vp, nsAString& aOutStr);
private:
static bool InitializeEventTable();

Просмотреть файл

@ -0,0 +1,46 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "jsapi.h"
#include "nsContentUtils.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/dom/SimpleGlobalObject.h"
TEST(DOM_Base_ContentUtils, StringifyJSON_EmptyValue)
{
JSObject* globalObject =
mozilla::dom::SimpleGlobalObject::Create(mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail);
mozilla::dom::AutoJSAPI jsAPI;
ASSERT_TRUE(jsAPI.Init(globalObject));
JSContext* cx = jsAPI.cx();
nsAutoString serializedValue;
JS::RootedValue jsValue(cx);
ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, &jsValue, serializedValue));
ASSERT_TRUE(serializedValue.EqualsLiteral("null"));
}
TEST(DOM_Base_ContentUtils, StringifyJSON_Object)
{
JSObject* globalObject =
mozilla::dom::SimpleGlobalObject::Create(mozilla::dom::SimpleGlobalObject::GlobalType::BindingDetail);
mozilla::dom::AutoJSAPI jsAPI;
ASSERT_TRUE(jsAPI.Init(globalObject));
JSContext* cx = jsAPI.cx();
nsAutoString serializedValue;
JS::RootedObject jsObj(cx, JS_NewPlainObject(cx));
JS::RootedString valueStr(cx, JS_NewStringCopyZ(cx, "Hello World!"));
ASSERT_TRUE(JS_DefineProperty(cx, jsObj, "key1", valueStr, JSPROP_ENUMERATE));
JS::RootedValue jsValue(cx, JS::ObjectValue(*jsObj));
ASSERT_TRUE(nsContentUtils::StringifyJSON(cx, &jsValue, serializedValue));
ASSERT_TRUE(serializedValue.EqualsLiteral("{\"key1\":\"Hello World!\"}"));
}

Просмотреть файл

@ -5,6 +5,7 @@
# file, you can obtain one at http://mozilla.org/MPL/2.0/.
UNIFIED_SOURCES += [
'TestContentUtils.cpp',
'TestPlainTextSerializer.cpp',
'TestXPathGenerator.cpp',
]