!1598 Fix the bug of parsing CDATA in XML

Merge pull request !1598 from jiangkai/xml
This commit is contained in:
openharmony_ci 2024-11-11 12:49:51 +00:00 committed by Gitee
commit 1d8898b7ef
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 97 additions and 60 deletions

View File

@ -553,8 +553,39 @@ namespace OHOS::xml {
return true;
}
void XmlPullParser::Parse(napi_env env, napi_value thisVar)
std::string XmlPullParser::DealCdata(std::string data)
{
std::string_view cDataBegin = "<![CDATA[";
std::string_view cDataEnd = "]]>";
size_t foundPosBegin = data.find(cDataBegin);
size_t foundPosEnd = data.find(cDataEnd);
size_t count = 0;
while (foundPosBegin != std::string::npos) {
std::string temp = data.substr(foundPosBegin, foundPosEnd - foundPosBegin + cDataEnd.length());
std::string resStr = "";
for (char c : temp) {
if (c == '\r') {
resStr += "\\r";
count++;
} else if (c == '\n') {
resStr += "\\n";
count++;
} else {
resStr += c;
}
}
data.replace(foundPosBegin, temp.length(), resStr);
foundPosBegin = data.find(cDataBegin, foundPosBegin + 1);
foundPosEnd = data.find(cDataEnd, foundPosEnd + count + 1);
}
return data;
}
void XmlPullParser::Parse(napi_env env, napi_value thisVar, bool deprecated)
{
if (deprecated) {
strXml_ = DealCdata(strXml_);
}
if (tagFunc_ || attrFunc_ || tokenFunc_) {
while (type != TagEnum::END_DOCUMENT) {
if (ParseOneTag() == TagEnum::ERROR) {

View File

@ -336,7 +336,8 @@ namespace OHOS::xml {
bool DealLength(size_t minimum);
void Replace(std::string &strTemp, std::string strSrc, std::string strDes) const;
size_t GetNSCount(size_t iTemp);
void Parse(napi_env env, napi_value thisVar);
std::string DealCdata(std::string data);
void Parse(napi_env env, napi_value thisVar, bool deprecated);
std::string GetNamespace(const std::string &prefix);
napi_value DealOptionInfo(napi_env env, napi_value napiObj);
TagEnum ParseTagType(bool inDeclaration);

View File

@ -15,6 +15,7 @@
#include "native_module_xml.h"
#include "js_xml.h"
#include "tools/ets_error.h"
extern const char _binary_js_xml_js_start[];
extern const char _binary_js_xml_js_end[];
@ -22,6 +23,9 @@ extern const char _binary_xml_abc_start[];
extern const char _binary_xml_abc_end[];
namespace OHOS::xml {
using namespace OHOS::Tools;
static const int32_t ERROR_CODE = 401; // 401 : the parameter type is incorrect
static napi_value XmlSerializerConstructor(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
@ -82,38 +86,6 @@ namespace OHOS::xml {
return thisVar;
}
std::string DealCdata(void *data, size_t len)
{
std::string strEnd(reinterpret_cast<char*>(data), len);
strEnd = strEnd.substr(0, std::strlen(strEnd.c_str()));
std::string cDataBegin = "<![CDATA[";
std::string cDataEnd = "]]>";
size_t foundPosBegin = strEnd.find(cDataBegin);
size_t foundPosEnd = strEnd.find(cDataEnd);
size_t count = 0;
while (foundPosBegin != std::string::npos) {
std::string temp = strEnd.substr(foundPosBegin, foundPosEnd - foundPosBegin + cDataEnd.length());
std::string resStr = "";
for (char c : temp) {
if (c != '\r' && c != '\n') {
resStr += c;
}
if (c == '\r') {
resStr += "\\r";
count++;
}
if (c == '\n') {
resStr += "\\n";
count++;
}
}
strEnd.replace(foundPosBegin, temp.length(), resStr);
foundPosBegin = strEnd.find(cDataBegin, foundPosBegin + 1);
foundPosEnd = strEnd.find(cDataEnd, foundPosEnd + count + 1);
}
return strEnd;
}
static napi_value XmlPullParserConstructor(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
@ -143,7 +115,7 @@ namespace OHOS::xml {
}
}
if (data) {
std::string strEnd = DealCdata(data, len);
std::string strEnd(reinterpret_cast<char*>(data), len);
if (argc == 1) {
object = new (std::nothrow) XmlPullParser(env, strEnd, "utf-8");
if (object == nullptr) {
@ -416,12 +388,33 @@ namespace OHOS::xml {
XmlPullParser *object = nullptr;
NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&object)));
object->DealOptionInfo(env, args[0]);
object->Parse(env, thisVar);
object->Parse(env, thisVar, true);
napi_value result = nullptr;
NAPI_CALL(env, napi_get_undefined(env, &result));
return result;
}
static napi_value ParseXml(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
size_t argc = 1;
napi_value args[1] = { nullptr };
napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr);
XmlPullParser *xmlPullParser = nullptr;
napi_unwrap(env, thisVar, reinterpret_cast<void**>(&xmlPullParser));
napi_value result = nullptr;
if (xmlPullParser == nullptr) {
ErrorHelper::ThrowError(env, ERROR_CODE, "Parameter error. Parameter verification failed.");
napi_get_boolean(env, false, &result);
return result;
}
xmlPullParser->DealOptionInfo(env, args[0]);
xmlPullParser->Parse(env, thisVar, false);
std::string errStr = xmlPullParser->XmlPullParserError();
napi_get_boolean(env, !errStr.empty(), &result);
return result;
}
static napi_value XmlPullParserError(napi_env env, napi_callback_info info)
{
napi_value thisVar = nullptr;
@ -441,6 +434,7 @@ namespace OHOS::xml {
napi_value xmlClass = nullptr;
napi_property_descriptor xmlDesc[] = {
DECLARE_NAPI_FUNCTION("parse", Parse),
DECLARE_NAPI_FUNCTION("parseXml", ParseXml),
DECLARE_NAPI_FUNCTION("XmlPullParserError", XmlPullParserError)
};
NAPI_CALL(env, napi_define_class(env, xmlPullParserClass, strlen(xmlPullParserClass),

View File

@ -16,6 +16,7 @@
interface NativeXmlPullParser {
new(value: object, strEncoding?: string): NativeXmlPullParser;
parse(options: object): void;
parseXml(options: object): boolean;
XmlPullParserError(): string;
}
@ -249,6 +250,16 @@ class XmlPullParser {
throw new BusinessError(errStr);
}
}
parseXml(options: object): void {
if (typeof options !== 'object') {
throw new BusinessError(`Parameter error.The type of ${options} must be object`);
}
if (this.xmlPullParserClass.parseXml(options)) {
let errStr: string = this.xmlPullParserClass.XmlPullParserError();
throw new BusinessError(errStr);
}
}
}
enum EventType {

View File

@ -1145,7 +1145,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest001, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, false);
std::string res1 = " note [\n<!ENTITY foo \"baa\">]note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n";
std::string res2 = "Hello, World! companyJohn & Hanscompany titleHappytitletitleHappytitle";
std::string res3 = " todoWorktodo todoPlaytodo go thereabba table trtdApplestd tdBananastd trtablenote";
@ -1189,7 +1189,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest002, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "importancehighloggedtruexmlns:hhttp://www.w3.org/TR/html4/");
}
@ -1229,7 +1229,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest003, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, false);
std::string res1 = "note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\nHello, World! companyJohn &";
std::string res2 = " Hanscompany titleHappytitletitleHappytitle todoWorktodo todoPlaytodo go thereabba h:table";
std::string res3 = " h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
@ -1273,7 +1273,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest004, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "importancehighloggedtruexmlns:hhttp://www.w3.org/TR/html4/");
}
@ -1313,7 +1313,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest005, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, false);
std::string res1 = " note [\n<!ENTITY foo \"baa\">]note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n";
std::string res2 = "Hello, World! companyJohn & Hanscompany titleHappytitletitleHappytitle todoWorktodo";
std::string res3 = " todoPlaytodo go thereabba h:table h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
@ -1356,7 +1356,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest006, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, false);
std::string res1 = " note [\n<!ENTITY foo \"baa\">]note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n";
std::string res2 = "Hello, World! companyJohn & Hanscompany titleHappytitletitleHappytitle todoWorktodo";
std::string res3 = " todoPlaytodo go thereabba h:table h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
@ -1400,7 +1400,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest007, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "importancehighloggedtruexmlns:hhttp://www.w3.org/TR/html4/");
}
@ -1440,8 +1440,8 @@ HWTEST_F(NativeEngineTest, XmlParseTest008, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
std::string res1 = "note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\nHello, World! companyJohn &";
xmlPullParser.Parse(env, options, true);
std::string res1 = "note\\r\\nfuncrion matchwo(a,6)\\r\\n{\\r\\nreturn 1;\\r\\n}\\r\\nHello, World! companyJohn &";
std::string res2 = " Hanscompany titleHappytitletitleHappytitle todoWorktodo todoPlaytodo go thereabba h:table ";
std::string res3 = "h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
std::string result = res1 + res2 + res3;
@ -1483,8 +1483,8 @@ HWTEST_F(NativeEngineTest, XmlParseTest009, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
std::string res1 = "note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\nHello, World! companyJohn &";
xmlPullParser.Parse(env, options, true);
std::string res1 = "note\\r\\nfuncrion matchwo(a,6)\\r\\n{\\r\\nreturn 1;\\r\\n}\\r\\nHello, World! companyJohn &";
std::string res2 = " Hanscompany titleHappytitletitleHappytitle todoWorktodo todoPlaytodo go thereabba h:table";
std::string res3 = " h:trh:tdApplesh:td h:tdBananash:td h:trh:tablenote";
std::string result = res1 + res2 + res3;
@ -1527,9 +1527,9 @@ HWTEST_F(NativeEngineTest, XmlParseTest0010, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
std::string res1 = " note [\n<!ENTITY foo \"baa\">]note\r\nfuncrion matchwo(a,6)\r\n{\r\nreturn 1;\r\n}\r\n";
std::string res2 = "Hello, World! companyJohn & Hanscompany titleHappytitletitleHappytitle todoWorktodo";
xmlPullParser.Parse(env, options, true);
std::string res1 = " note [\n<!ENTITY foo \"baa\">]note\\r\\nfuncrion matchwo(a,6)\\r\\n{\\r\\nreturn 1;\\r\\n}";
std::string res2 = "\\r\\nHello, World! companyJohn & Hanscompany titleHappytitletitleHappytitle todoWorktodo";
std::string res3 = " todoPlaytodo go thereabba table trtdApplestd tdBananastd trtablenote";
std::string result = res1 + res2 + res3;
ASSERT_STREQ(g_testStr.c_str(), result.c_str());
@ -1571,7 +1571,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest0011, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "");
}
@ -1610,7 +1610,7 @@ HWTEST_F(NativeEngineTest, XmlParseTest0012, testing::ext::TestSize.Level0)
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "");
}
@ -2823,7 +2823,7 @@ HWTEST_F(NativeEngineTest, XmlParseTagValueFuncFunction001, testing::ext::TestSi
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
std::string result = "";
ASSERT_STREQ(g_testStr.c_str(), result.c_str());
}
@ -2941,7 +2941,7 @@ HWTEST_F(NativeEngineTest, XmlParseTagValueFuncFunction007, testing::ext::TestSi
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
std::string result = "";
ASSERT_STREQ(g_testStr.c_str(), result.c_str());
}
@ -3000,7 +3000,7 @@ HWTEST_F(NativeEngineTest, ParseInnerAttriDeclFunction001, testing::ext::TestSiz
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "");
}
@ -3034,7 +3034,7 @@ HWTEST_F(NativeEngineTest, ParseInnerAttriDeclFunction002, testing::ext::TestSiz
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "");
}
@ -3068,7 +3068,7 @@ HWTEST_F(NativeEngineTest, ParseInnerAttriDeclFunction003, testing::ext::TestSiz
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "");
}
@ -3102,7 +3102,7 @@ HWTEST_F(NativeEngineTest, ParseInnerAttriDeclFunction004, testing::ext::TestSiz
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "");
}
@ -3137,7 +3137,7 @@ HWTEST_F(NativeEngineTest, ParseInnerAttriDeclFuncFunction001, testing::ext::Tes
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "");
}
@ -3172,7 +3172,7 @@ HWTEST_F(NativeEngineTest, ParseInnerAttriDeclFuncFunction002, testing::ext::Tes
napi_set_named_property(env, object, key2, value2);
napi_set_named_property(env, object, key3, value3);
xmlPullParser.DealOptionInfo(env, object);
xmlPullParser.Parse(env, options);
xmlPullParser.Parse(env, options, true);
ASSERT_STREQ(g_testStr.c_str(), "");
}