diff --git a/convertxml/BUILD.gn b/convertxml/BUILD.gn index a2ac7c536b7250ae16d2459827d607085065270f..5e130653494a2fda376dbcee36d22e4a765e9b9f 100755 --- a/convertxml/BUILD.gn +++ b/convertxml/BUILD.gn @@ -11,14 +11,58 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//ark/ts2abc/ts2panda/ts2abc_config.gni") import("//build/ohos.gni") import("//build/ohos/ace/ace.gni") +import("//foundation/ace/ace_engine/ace_config.gni") + +# compile .ts to .js. +action("build_ts_js") { + script = "//base/compileruntime/js_api_module/convertxml/build_ts_js.py" + depfile = "$target_gen_dir/$target_name.d" + outputs = [ target_out_dir + "/js_convertxml.js" ] +} base_output_path = get_label_info(":js_convertxml", "target_out_dir") js_xml_obj_path = base_output_path + "/convertxml.o" gen_js_obj("js_convertxml") { - input = "//base/compileruntime/js_api_module/convertxml/js_convertxml.js" + input = "$target_out_dir/js_convertxml.js" output = js_xml_obj_path + dep = ":build_ts_js" +} + +# compile .js to .abc. +action("gen_convertxml_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path(target_out_dir + "/js_convertxml.js"), + "--dst-file", + rebase_path(target_out_dir + "/convertxml.abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ + ":build_ts_js", + "//ark/ts2abc/ts2panda:ark_ts2abc_build", + ] + + inputs = [ target_out_dir + "/js_convertxml.js" ] + outputs = [ target_out_dir + "/convertxml.abc" ] +} + +abc_output_path = get_label_info(":convertxml_abc", "target_out_dir") +convertxml_abc_obj_path = abc_output_path + "/convertxml_abc.o" +gen_js_obj("convertxml_abc") { + input = "$target_out_dir/convertxml.abc" + output = convertxml_abc_obj_path + dep = ":gen_convertxml_abc" } ohos_shared_library("convertxml") { @@ -36,6 +80,7 @@ ohos_shared_library("convertxml") { ] deps = [ + ":convertxml_abc", ":js_convertxml", "//base/compileruntime/js_api_module/convertxml/:js_convertxml", "//foundation/ace/napi/:ace_napi", diff --git a/convertxml/build_ts_js.py b/convertxml/build_ts_js.py new file mode 100755 index 0000000000000000000000000000000000000000..83a660e130acfe632de5f40caf54bf199632ae56 --- /dev/null +++ b/convertxml/build_ts_js.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os + +if __name__ == '__main__': + + build_path = os.path.abspath(os.path.join(os.getcwd(), "../..")) + os.chdir("%s/base/compileruntime/js_api_module/convertxml" % build_path) + os.system('../../../../developtools/ace-ets2bundle/compiler/node_modules/typescript/bin/tsc') + + if os.access("../../../../out/ohos-arm64-release", os.F_OK): + os.system('cp -r ./out/js_convertxml.js ../../../../out/ohos-arm64-release/obj/base/compileruntime/js_api_module/convertxml/js_convertxml.js') + + if os.access("../../../../out/ohos-arm-release", os.F_OK): + os.system('cp -r ./out/js_convertxml.js ../../../../out/ohos-arm-release/obj/base/compileruntime/js_api_module/convertxml/js_convertxml.js') + + os.system('rm -rf ./out') \ No newline at end of file diff --git a/convertxml/js_convertxml.cpp b/convertxml/js_convertxml.cpp index 727565fbfb941d4e75a6feea024641ce787c9ef3..339bb99185719869fe1573acf596d51c72e46230 100755 --- a/convertxml/js_convertxml.cpp +++ b/convertxml/js_convertxml.cpp @@ -113,18 +113,24 @@ void ConvertXml::GetPrevNodeList(xmlNodePtr curNode) napi_create_object(env_, &elementsObject); if (curNode->type == xmlElementType::XML_PI_NODE && !m_Options.ignoreInstruction) { SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); - SetKeyValue(elementsObject, m_Options.name, (char*)curNode->name); - SetKeyValue(elementsObject, m_Options.instruction, (const char*)xmlNodeGetContent(curNode)); + SetKeyValue(elementsObject, m_Options.name, reinterpret_cast(curNode->name)); + if (xmlNodeGetContent(curNode) != nullptr) { + SetKeyValue(elementsObject, m_Options.instruction, + reinterpret_cast(xmlNodeGetContent(curNode))); + } m_prevObj.push_back(elementsObject); } if (curNode->type == xmlElementType::XML_COMMENT_NODE && !m_Options.ignoreComment) { SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); - SetKeyValue(elementsObject, m_Options.comment, (const char*)xmlNodeGetContent(curNode)); + if (xmlNodeGetContent(curNode) != nullptr) { + SetKeyValue(elementsObject, m_Options.comment, + reinterpret_cast(xmlNodeGetContent(curNode))); + } m_prevObj.push_back(elementsObject); } if (curNode->type == xmlElementType::XML_DTD_NODE && !m_Options.ignoreDoctype) { SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); - SetKeyValue(elementsObject, m_Options.doctype, (char*)curNode->name); + SetKeyValue(elementsObject, m_Options.doctype, reinterpret_cast(curNode->name)); m_prevObj.push_back(elementsObject); } } @@ -137,7 +143,8 @@ void ConvertXml::SetAttributes(xmlNodePtr curNode, napi_value &elementsObject) napi_value attrTitleObj = nullptr; napi_create_object(env_, &attrTitleObj); while (attr) { - SetKeyValue(attrTitleObj, (const char*)attr->name, (const char*)attr->children->content); + SetKeyValue(attrTitleObj, reinterpret_cast(attr->name), + reinterpret_cast(attr->children->content)); attr = attr->next; } napi_set_named_property(env_, elementsObject, m_Options.attributes.c_str(), attrTitleObj); @@ -147,51 +154,67 @@ void ConvertXml::SetAttributes(xmlNodePtr curNode, napi_value &elementsObject) void ConvertXml::SetXmlElementType(xmlNodePtr curNode, napi_value &elementsObject, bool &bFlag) { if (curNode->type == xmlElementType::XML_PI_NODE && !m_Options.ignoreInstruction) { - SetKeyValue(elementsObject, m_Options.instruction.c_str(), (const char*)xmlNodeGetContent(curNode)); - bFlag = true; + if (xmlNodeGetContent(curNode) != nullptr) { + SetKeyValue(elementsObject, m_Options.instruction.c_str(), + reinterpret_cast(xmlNodeGetContent(curNode))); + bFlag = true; + } } else if (curNode->type == xmlElementType::XML_COMMENT_NODE && !m_Options.ignoreComment) { - SetKeyValue(elementsObject, m_Options.comment.c_str(), (const char*)xmlNodeGetContent(curNode)); - bFlag = true; + if (xmlNodeGetContent(curNode) != nullptr) { + SetKeyValue(elementsObject, m_Options.comment.c_str(), + reinterpret_cast(xmlNodeGetContent(curNode))); + bFlag = true; + } } else if (curNode->type == xmlElementType::XML_CDATA_SECTION_NODE && !m_Options.ignoreCdata) { - SetKeyValue(elementsObject, m_Options.cdata, (const char*)xmlNodeGetContent(curNode)); - bFlag = true; + if (xmlNodeGetContent(curNode) != nullptr) { + SetKeyValue(elementsObject, m_Options.cdata, + reinterpret_cast(xmlNodeGetContent(curNode))); + bFlag = true; + } } } void ConvertXml::SetNodeInfo(xmlNodePtr curNode, napi_value &elementsObject) { - if (curNode->type == xmlElementType::XML_PI_NODE) { - if (!m_Options.ignoreInstruction) { - SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); - } + if (curNode->type == xmlElementType::XML_TEXT_NODE) { + return; } else { - SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); - } - if ((curNode->type != xmlElementType::XML_COMMENT_NODE) && - (curNode->type != xmlElementType::XML_CDATA_SECTION_NODE)) { - if (!(curNode->type == xmlElementType::XML_PI_NODE && m_Options.ignoreInstruction)) { - SetKeyValue(elementsObject, m_Options.name, (char*)curNode->name); + if (curNode->type == xmlElementType::XML_PI_NODE) { + if (!m_Options.ignoreInstruction) { + SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); + } + } else { + SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); + } + if ((curNode->type != xmlElementType::XML_COMMENT_NODE) && + (curNode->type != xmlElementType::XML_CDATA_SECTION_NODE)) { + if (!(curNode->type == xmlElementType::XML_PI_NODE && m_Options.ignoreInstruction)) { + SetKeyValue(elementsObject, m_Options.name, reinterpret_cast(curNode->name)); + } } } } -void ConvertXml::SetEndInfo(xmlNodePtr curNode, napi_value &elementsObject, bool &bFlag, bool &bText, int32_t index) +void ConvertXml::SetEndInfo(xmlNodePtr curNode, napi_value &elementsObject, bool &bFlag) { SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); if (curNode->type == xmlElementType::XML_ELEMENT_NODE) { - SetKeyValue(elementsObject, m_Options.name.c_str(), (const char*)curNode->name); + SetKeyValue(elementsObject, m_Options.name.c_str(), reinterpret_cast(curNode->name)); bFlag = true; } else if (curNode->type == xmlElementType::XML_TEXT_NODE) { if (m_Options.trim) { - SetKeyValue(elementsObject, m_Options.text, Trim((const char*)xmlNodeGetContent(curNode))); + if (xmlNodeGetContent(curNode) != nullptr) { + SetKeyValue(elementsObject, m_Options.text, + Trim(reinterpret_cast(xmlNodeGetContent(curNode)))); + } } else { - SetKeyValue(elementsObject, m_Options.text, (const char*)xmlNodeGetContent(curNode)); + if (xmlNodeGetContent(curNode) != nullptr) { + SetKeyValue(elementsObject, m_Options.text, + reinterpret_cast(xmlNodeGetContent(curNode))); + } } if (!m_Options.ignoreText) { bFlag = true; } - if (index != 0) { - bText = false; - } } } @@ -208,21 +231,17 @@ void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) { napi_value elements = nullptr; napi_create_array(env_, &elements); - napi_value recvElement; + napi_value recvElement = nullptr; napi_create_array(env_, &recvElement); xmlNodePtr pNode = curNode; int32_t index = 0; int32_t index1 = 0; bool bFlag = false; - bool bText = true; while (pNode != nullptr) { bFlag = false; - bText = true; napi_value elementsObject = nullptr; napi_create_object(env_, &elementsObject); - if (flag == 0 || (index % 2 != 0)) { // 2:pNode - SetNodeInfo(pNode, elementsObject); - } + SetNodeInfo(pNode, elementsObject); SetAttributes(pNode, elementsObject); napi_value tempElement = nullptr; napi_create_array(env_, &tempElement); @@ -233,16 +252,15 @@ void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) curNode = pNode->children; GetXMLInfo(curNode, elementsObject, 1); bFlag = true; - } else if (index % 2 != 0) { // 2:pNode + } else { SetXmlElementType(pNode, elementsObject, bFlag); - } else if (pNode->next == nullptr) { - SetEndInfo(pNode, elementsObject, bFlag, bText, index); + SetEndInfo(pNode, elementsObject, bFlag); } } SetPrevInfo(recvElement, flag, index1); - if (elementsObject != nullptr && bFlag && bText) { - napi_set_element(env_, recvElement, index1++, elementsObject); - elementsObject = nullptr; + if (elementsObject != nullptr && bFlag) { + napi_set_element(env_, recvElement, index1++, elementsObject); + elementsObject = nullptr; } index++; pNode = pNode->next; @@ -252,37 +270,8 @@ void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) } } -napi_value ConvertXml::convert(std::string strXml) +void ConvertXml::SetSpacesInfo(napi_value &object) { - xmlDocPtr doc = NULL; - xmlNodePtr curNode = NULL; - napi_status status = napi_ok; - size_t len = strXml.size(); - doc = xmlParseMemory(strXml.c_str(), len); - if (!doc) { - xmlFreeDoc(doc); - } - napi_value object = nullptr; - status = napi_create_object(env_, &object); - if (status != napi_ok) { - return NULL; - } - napi_value subObject = nullptr; - napi_value subSubObject = nullptr; - napi_value napiKey = nullptr; - napi_create_object(env_, &subSubObject); - napi_create_object(env_, &subObject); - napi_create_string_utf8(env_, (const char*)doc->version, NAPI_AUTO_LENGTH, &napiKey); - napi_set_named_property(env_, subSubObject, "version", napiKey); - napi_create_string_utf8(env_, (const char*)doc->encoding, NAPI_AUTO_LENGTH, &napiKey); - napi_set_named_property(env_, subSubObject, "encoding", napiKey); - if (!m_Options.ignoreDeclaration) { - napi_set_named_property(env_, subObject, m_Options.attributes.c_str(), subSubObject); - napi_set_named_property(env_, object, m_Options.declaration.c_str(), subObject); - } - curNode = xmlDocGetRootElement(doc); - GetPrevNodeList(curNode); - GetXMLInfo(curNode, object, 0); napi_value iTemp = nullptr; switch (m_SpaceType) { case (SpaceType::T_INT32): @@ -297,7 +286,50 @@ napi_value ConvertXml::convert(std::string strXml) break; default: break; + } +} + +napi_value ConvertXml::convert(std::string strXml) +{ + xmlDocPtr doc = nullptr; + xmlNodePtr curNode = nullptr; + napi_status status = napi_ok; + napi_value object = nullptr; + status = napi_create_object(env_, &object); + if (status != napi_ok) { + return nullptr; + } + Replace(strXml, "\\r", "\r"); + Replace(strXml, "\\n", "\n"); + Replace(strXml, "\\v", "\v"); + Replace(strXml, "]]> version != nullptr) { + SetKeyValue(subSubObject, "version", (const char*)doc->version); + } + if (doc != nullptr && doc->encoding != nullptr) { + SetKeyValue(subSubObject, "encoding", (const char*)doc->encoding); } + if (!m_Options.ignoreDeclaration && strXml.find("xml") != std::string::npos) { + napi_set_named_property(env_, subObject, m_Options.attributes.c_str(), subSubObject); + napi_set_named_property(env_, object, m_Options.declaration.c_str(), subObject); + } + if (doc != nullptr) { + curNode = xmlDocGetRootElement(doc); + GetPrevNodeList(curNode); + GetXMLInfo(curNode, object, 0); + } + SetSpacesInfo(object); return object; } @@ -310,9 +342,10 @@ napi_status ConvertXml::DealNapiStrValue(napi_value napi_StrValue, std::string & if (status != napi_ok) { return status; } - buffer = new char[bufferSize + 1]; - napi_get_value_string_utf8(env_, napi_StrValue, buffer, bufferSize + 1, &bufferSize); - + if (bufferSize > 0) { + buffer = new char[bufferSize + 1]; + napi_get_value_string_utf8(env_, napi_StrValue, buffer, bufferSize + 1, &bufferSize); + } if (buffer != nullptr) { result = buffer; delete []buffer; @@ -341,8 +374,9 @@ void ConvertXml::DealSpaces(napi_value napi_obj) void ConvertXml::DealIgnore(napi_value napi_obj) { - std::vectorvctIgnore = { "compact", "trim", "ignoreDeclaration", "ignoreInstruction", - "ignoreAttributes", "ignoreComment", "ignoreCdata", "ignoreDoctype", "ignoreText" }; + std::vector vctIgnore = {"compact", "trim", "ignoreDeclaration", "ignoreInstruction", + "ignoreAttributes", "ignoreComment", "ignoreCdata", + "ignoreDoctype", "ignoreText"}; for (size_t i = 0; i < vctIgnore.size(); ++i) { napi_value recvTemp = nullptr; bool bRecv = false; @@ -426,8 +460,9 @@ void ConvertXml::SetDefaultKey(size_t i, std::string strRecv) void ConvertXml::DealOptions(napi_value napi_obj) { - std::vectorvctOptions = { "declarationKey", "instructionKey", "attributesKey", "textKey", - "cdataKey", "doctypeKey", "commentKey", "parentKey", "typeKey", "nameKey", "elementsKey" }; + std::vector vctOptions = {"declarationKey", "instructionKey", "attributesKey", "textKey", + "cdataKey", "doctypeKey", "commentKey", "parentKey", "typeKey", + "nameKey", "elementsKey"}; for (size_t i = 0; i < vctOptions.size(); ++i) { napi_value recvTemp = nullptr; std::string strRecv = ""; @@ -439,3 +474,117 @@ void ConvertXml::DealOptions(napi_value napi_obj) DealIgnore(napi_obj); DealSpaces(napi_obj); } + +void ConvertXml::DealSingleLine(std::string &strXml, napi_value &object) +{ + size_t iXml = 0; + if ((iXml = strXml.find("xml")) != std::string::npos) { + m_XmlInfo.bXml = true; + napi_value declObj = nullptr; + napi_create_object(env_, &declObj); + napi_value attrObj = nullptr; + bool bFlag = false; + napi_create_object(env_, &attrObj); + if (strXml.find("version=") != std::string::npos) { + m_XmlInfo.bVersion = true; + SetKeyValue(attrObj, "version", "1.0"); + bFlag = true; + } + if (strXml.find("encoding=") != std::string::npos) { + m_XmlInfo.bEncoding = false; + SetKeyValue(attrObj, "encoding", "utf-8"); + bFlag = true; + } + if (bFlag) { + napi_set_named_property(env_, declObj, m_Options.attributes.c_str(), attrObj); + napi_set_named_property(env_, object, m_Options.declaration.c_str(), declObj); + } else { + napi_set_named_property(env_, object, m_Options.declaration.c_str(), declObj); + } + if (strXml.find(">", iXml) == strXml.size() - 1) { + strXml = ""; + } else { + strXml = strXml.substr(0, strXml.rfind("<", iXml)) + strXml.substr(strXml.find(">", iXml) + 1); + } + } + size_t iCount = 0; + size_t iLen = strXml.size(); + for (; iCount < iLen; ++iCount) { + if (strXml[iCount] != ' ' && strXml[iCount] != '\v' && + strXml[iCount] != '\t' && strXml[iCount] != '\n') { + break; + } + } + if (iCount < iLen) { + DealComplex(strXml, object); + } +} + +void ConvertXml::DealComplex(std::string &strXml, napi_value &object) +{ + if (strXml.find(""; + } else { + strXml = "" + strXml + ""; + } + xmlDocPtr doc = nullptr; + xmlNodePtr curNode = nullptr; + size_t len = strXml.size(); + doc = xmlParseMemory(strXml.c_str(), len); + if (!doc) { + xmlFreeDoc(doc); + } + if (doc) { + curNode = xmlDocGetRootElement(doc); + curNode = curNode->children; + napi_value elements = nullptr; + napi_create_array(env_, &elements); + bool bHasEle = false; + int index = 0; + bool bCData = false; + if (strXml.find("type == xmlElementType::XML_CDATA_SECTION_NODE && + curNode->next && curNode->next->type == xmlElementType::XML_TEXT_NODE && + curNode->next->next && curNode->next->next->type == xmlElementType::XML_CDATA_SECTION_NODE) { + if (xmlNodeGetContent(curNode->next) != nullptr) { + std::string strTemp = reinterpret_cast(xmlNodeGetContent(curNode->next)); + Replace(strTemp, " ", ""); + Replace(strTemp, "\v", ""); + Replace(strTemp, "\t", ""); + Replace(strTemp, "\n", ""); + if (strTemp == "") { + curNode = curNode->next->next; + } + } + } else { + curNode = curNode->next; + } +} \ No newline at end of file diff --git a/convertxml/js_convertxml.h b/convertxml/js_convertxml.h index bbe2381f45ee7f6bed5299e47fec8a62e98ed162..380fab061d5f01912e2b37e8dc2449bfecf5fb2e 100755 --- a/convertxml/js_convertxml.h +++ b/convertxml/js_convertxml.h @@ -30,33 +30,41 @@ enum class SpaceType { }; struct Options { - std::string declaration = "_declaration"; - std::string instruction = "_instruction"; - std::string attributes = "_attributes"; - std::string text = "_text"; - std::string cdata = "_cdata"; - std::string doctype = "_doctype"; - std::string comment = "_comment"; - std::string parent = "_parent"; - std::string type = "_type"; - std::string name = "_name"; - std::string elements = "_elements"; - bool compact = false; - bool trim = false; - bool nativetype = false; - bool nativetypeattributes = false; - bool addparent = false; - bool alwaysArray = false; - bool alwaysChildren = false; - bool instructionHasAttributes = false; - bool ignoreDeclaration = false; - bool ignoreInstruction = false; - bool ignoreAttributes = false; - bool ignoreComment = false; - bool ignoreCdata = false; - bool ignoreDoctype = false; - bool ignoreText = false; - bool spaces = false; + std::string declaration = "_declaration"; + std::string instruction = "_instruction"; + std::string attributes = "_attributes"; + std::string text = "_text"; + std::string cdata = "_cdata"; + std::string doctype = "_doctype"; + std::string comment = "_comment"; + std::string parent = "_parent"; + std::string type = "_type"; + std::string name = "_name"; + std::string elements = "_elements"; + bool compact = false; + bool trim = false; + bool nativetype = false; + bool nativetypeattributes = false; + bool addparent = false; + bool alwaysArray = false; + bool alwaysChildren = false; + bool instructionHasAttributes = false; + bool ignoreDeclaration = false; + bool ignoreInstruction = false; + bool ignoreAttributes = false; + bool ignoreComment = false; + bool ignoreCdata = false; + bool ignoreDoctype = false; + bool ignoreText = false; + bool spaces = false; +}; + +struct XmlInfo { + bool bXml = false; + bool bVersion = false; + std::string strVersion = ""; + bool bEncoding = false; + std::string strEncoding = ""; }; class ConvertXml { @@ -66,7 +74,7 @@ public: void SetAttributes(xmlNodePtr curNode, napi_value &elementsObject); void SetXmlElementType(xmlNodePtr curNode, napi_value &elementsObject, bool &bFlag); void SetNodeInfo(xmlNodePtr curNode, napi_value &elementsObject); - void SetEndInfo(xmlNodePtr curNode, napi_value &elementsObject, bool &bFlag, bool &bText, int32_t index); + void SetEndInfo(xmlNodePtr curNode, napi_value &elementsObject, bool &bFlag); void GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag = 0); napi_value convert(std::string strXml); std::string GetNodeType(xmlElementType enumType); @@ -79,6 +87,11 @@ public: void DealIgnore(napi_value napi_obj); void SetPrevInfo(napi_value &recvElement, int flag, int32_t &index1); void SetDefaultKey(size_t i, std::string strRecv); + void SetSpacesInfo(napi_value &object); + void DealSingleLine(std::string &strXml, napi_value &object); + void DealComplex(std::string &strXml, napi_value &object); + void Replace(std::string &str, const std::string src, const std::string dst); + void DealCDataInfo(bool bCData, xmlNodePtr &curNode); private: napi_env env_; SpaceType m_SpaceType; @@ -86,5 +99,6 @@ private: std::string m_strSpace; Options m_Options; std::vector m_prevObj; + XmlInfo m_XmlInfo; }; #endif \ No newline at end of file diff --git a/convertxml/js_convertxml.js b/convertxml/js_convertxml.js deleted file mode 100755 index a0305b036471c70e26b7e2e989878f63a6464e36..0000000000000000000000000000000000000000 --- a/convertxml/js_convertxml.js +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; -const convertXml = requireInternal("ConvertXML"); -class ConvertXml { - convertxmlclass; - constructor() { - this.convertxmlclass = new convertXml.ConvertXml(); - } - convert(strXml, options) { - let converted = this.convertxmlclass.convert(strXml, options); - let space = 0; - if (converted.hasOwnProperty("spaces")) { - space = converted.spaces; - delete converted.spaces; - } - return JSON.stringify(converted, null, space); - } -} - -export default { - ConvertXml : ConvertXml -} - - diff --git a/convertxml/native_module_convertxml.cpp b/convertxml/native_module_convertxml.cpp index 25223d8adad4cf2b0b437238bfab6b4d63aaa71f..4a8626bfc0928f0e08cb815f2e3a868707cf26b3 100755 --- a/convertxml/native_module_convertxml.cpp +++ b/convertxml/native_module_convertxml.cpp @@ -19,6 +19,8 @@ extern const char _binary_js_convertxml_js_start[]; extern const char _binary_js_convertxml_js_end[]; +extern const char _binary_convertxml_abc_start[]; +extern const char _binary_convertxml_abc_end[]; static napi_value ConvertXmlConstructor(napi_env env, napi_callback_info info) { @@ -29,7 +31,7 @@ static napi_value ConvertXmlConstructor(napi_env env, napi_callback_info info) napi_wrap( env, thisVar, objectInfo, [](napi_env env, void *data, void *hint) { - auto obj = (ConvertXml*)data; + auto obj = reinterpret_cast(data); if (obj != nullptr) { delete obj; } @@ -51,7 +53,7 @@ static napi_value Convert(napi_env env, napi_callback_info info) std::string strXml; napi_valuetype valuetype; ConvertXml *object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); if (args[0] == nullptr) { NAPI_CALL(env, napi_throw_error(env, "", "parameter is empty")); } else { @@ -95,6 +97,16 @@ __attribute__((visibility("default"))) void NAPI_convertxml_GetJSCode(const char *bufLen = _binary_js_convertxml_js_end - _binary_js_convertxml_js_start; } } +extern "C" +__attribute__((visibility("default"))) void NAPI_convertxml_GetABCCode(const char **buf, int *buflen) +{ + if (buf != nullptr) { + *buf = _binary_convertxml_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_convertxml_abc_end - _binary_convertxml_abc_start; + } +} static napi_module ConvertXmlModule = { .nm_version = 1, @@ -106,7 +118,8 @@ static napi_module ConvertXmlModule = { .reserved = { 0 }, }; -extern "C" __attribute__ ((constructor)) void RegisterModule() { +extern "C" __attribute__ ((constructor)) void RegisterModule() +{ napi_module_register(&ConvertXmlModule); } diff --git a/convertxml/src/js_convertxml.ts b/convertxml/src/js_convertxml.ts new file mode 100644 index 0000000000000000000000000000000000000000..93ebb9808f01181f6586a05b5c15830969bcace9 --- /dev/null +++ b/convertxml/src/js_convertxml.ts @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function requireInternal(s : string) : any; +const convertXml = requireInternal("ConvertXML"); +class ConvertXml { + convertxmlclass; + constructor() { + this.convertxmlclass = new convertXml.ConvertXml(); + } + convert(strXml : string, options : any) { + strXml = DealXml(strXml); + let converted = this.convertxmlclass.convert(strXml, options); + let space = 0; + if (converted.hasOwnProperty("spaces")) { + space = converted.spaces; + delete converted.spaces; + } + var strEnd = JSON.stringify(converted, null, space); + var idx = 0; + while ((idx = strEnd.indexOf('\\t')) != -1) { + strEnd = strEnd.substring(0, idx) + '\t' + strEnd.substring(idx + 2); + } + while ((idx = strEnd.indexOf('\\n')) != -1) { + strEnd = strEnd.substring(0, idx) + '\n' + strEnd.substring(idx + 2); + } + while ((idx = strEnd.indexOf('\\')) != -1) { + strEnd = strEnd.substring(0, idx) + '' + strEnd.substring(idx + 1); + } + return strEnd; + } +} + +function DealXml(strXml : string) +{ + var idx = 0; + var idxSec = 0; + var idxThir = 0; + var idxCData = 0; + var idxCDataSec = 0; + while ((idx = strXml.indexOf(']]>', idxSec)) != -1) { + idxThir = strXml.indexOf('<', idx); + strXml = DealReplace(strXml, idx, idxThir); + if (strXml.indexOf('<', idx) != -1) { + idxCData = strXml.indexOf('', idxCData); + var i = idx + 1; + for (; i < idxThir ; i++) { + var cXml = strXml.charAt(i) + switch (cXml) { + case '\n': + strXml = strXml.substring(0, i) + '\\n' + strXml.substring(i + 1); + break; + case '\v': + strXml = strXml.substring(0, i) + '\\v' + strXml.substring(i + 1); + break; + case '\t': + strXml = strXml.substring(0, i) + '\\t' + strXml.substring(i + 1); + break; + default: + break; + } + } + idxCDataSec = idxSec; + } + } + else { + break; + } + } + return strXml; +} + +function DealReplace(strXml : string, idx : any, idxThir : any) +{ + var i = idx + 1; + for (; i < idxThir ; i++) { + var cXml = strXml.charAt(i); + if (cXml != '\n' && cXml != '\v' && cXml != '\t' && cXml != ' ') + { + break; + } + } + var j = idx + 1; + for (; j < strXml.indexOf('<', idx) ; j++) { + var cXml = strXml.charAt(j); + if (i != idxThir) { + switch (cXml) { + case '\n': + strXml = strXml.substring(0, j) + '\\n' + strXml.substring(j + 1); + break; + case '\v': + strXml = strXml.substring(0, j) + '\\v' + strXml.substring(j + 1); + break; + case '\t': + strXml = strXml.substring(0, j) + '\\t' + strXml.substring(j + 1); + break; + default: + break; + } + } else { + strXml = strXml.substring(0, j) + strXml.substring(j + 1); + --j; + } + } + return strXml; +} + +export default { + ConvertXml : ConvertXml +} + + diff --git a/convertxml/tsconfig.json b/convertxml/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..c86e350c8cabc34f78dcfe27b31f16b871fd28c1 --- /dev/null +++ b/convertxml/tsconfig.json @@ -0,0 +1,15 @@ +{ +"compilerOptions": { + "target": "es6", + "module": "es6", + "rootDir": "./src", + //"outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./out", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noImplicitThis": false, + } +} + diff --git a/uri/BUILD.gn b/uri/BUILD.gn index 0249e7a398c05f7ad3fabe6685670b171ce9837a..a136c4bf7453f1930f5dd23b3380b1281f4827ac 100755 --- a/uri/BUILD.gn +++ b/uri/BUILD.gn @@ -10,15 +10,58 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import("//ark/ts2abc/ts2panda/ts2abc_config.gni") import("//build/ohos.gni") import("//build/ohos/ace/ace.gni") +import("//foundation/ace/ace_engine/ace_config.gni") + +# compile .ts to .js. +action("build_ts_js") { + script = "//base/compileruntime/js_api_module/uri/build_ts_js.py" + depfile = "$target_gen_dir/$target_name.d" + outputs = [ target_out_dir + "/js_uri.js" ] +} base_output_path = get_label_info(":js_uri", "target_out_dir") js_uri_obj_path = base_output_path + "/uri.o" gen_js_obj("js_uri") { - input = "//base/compileruntime/js_api_module/uri/js_uri.js" + input = "$target_out_dir/js_uri.js" output = js_uri_obj_path + dep = ":build_ts_js" +} + +# compile .js to .abc. +action("gen_uri_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path(target_out_dir + "/js_uri.js"), + "--dst-file", + rebase_path(target_out_dir + "/uri.abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ + ":build_ts_js", + "//ark/ts2abc/ts2panda:ark_ts2abc_build", + ] + + inputs = [ target_out_dir + "/js_uri.js" ] + outputs = [ target_out_dir + "/uri.abc" ] +} + +abc_output_path = get_label_info(":uri_abc", "target_out_dir") +uri_abc_obj_path = abc_output_path + "/uri_abc.o" +gen_js_obj("uri_abc") { + input = "$target_out_dir/uri.abc" + output = uri_abc_obj_path + dep = ":gen_uri_abc" } ohos_shared_library("uri") { @@ -36,6 +79,7 @@ ohos_shared_library("uri") { deps = [ ":js_uri", + ":uri_abc", "//base/compileruntime/js_api_module/uri/:js_uri", "//foundation/ace/napi/:ace_napi", "//foundation/ace/napi/:ace_napi_quickjs", @@ -57,4 +101,3 @@ ohos_shared_library("uri") { group("uri_packages") { deps = [ ":uri" ] } - diff --git a/uri/build_ts_js.py b/uri/build_ts_js.py new file mode 100755 index 0000000000000000000000000000000000000000..4461b2bb9c06e96d3ca0342b084647a40377f5e1 --- /dev/null +++ b/uri/build_ts_js.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os + +if __name__ == '__main__': + + build_path = os.path.abspath(os.path.join(os.getcwd(), "../..")) + os.chdir("%s/base/compileruntime/js_api_module/uri" % build_path) + os.system('../../../../developtools/ace-ets2bundle/compiler/node_modules/typescript/bin/tsc') + + if os.access("../../../../out/ohos-arm64-release", os.F_OK): + os.system('cp -r ./out/js_uri.js ../../../../out/ohos-arm64-release/obj/base/compileruntime/js_api_module/uri/js_uri.js') + + if os.access("../../../../out/ohos-arm-release", os.F_OK): + os.system('cp -r ./out/js_uri.js ../../../../out/ohos-arm-release/obj/base/compileruntime/js_api_module/uri/js_uri.js') + + os.system('rm -rf ./out') \ No newline at end of file diff --git a/uri/js_uri.cpp b/uri/js_uri.cpp index 3af8ab0c9e651343528f5e1b2bca77c21d5525ec..34452121891811f25686dfb49a533cf1a2d2b1fe 100755 --- a/uri/js_uri.cpp +++ b/uri/js_uri.cpp @@ -15,14 +15,14 @@ #include "js_uri.h" #include "utils/log.h" namespace OHOS::Uri { - std::bitset g_ruleAlpha; - std::bitset g_ruleScheme; - std::bitset g_ruleUrlc; - std::bitset g_rulePath; - std::bitset g_ruleUserInfo; - std::bitset g_ruleScope; - std::bitset g_ruleDigit; - std::bitset g_rulePort; + std::bitset g_ruleAlpha; + std::bitset g_ruleScheme; + std::bitset g_ruleUrlc; + std::bitset g_rulePath; + std::bitset g_ruleUserInfo; + std::bitset g_ruleScope; + std::bitset g_ruleDigit; + std::bitset g_rulePort; void Uri::PreliminaryWork() const { std::string digitAggregate = "0123456789"; diff --git a/uri/native_module_uri.cpp b/uri/native_module_uri.cpp index cb013be86fad1ddea9a9f16941cf4596062376e5..23eb1c6f21434628546362cc073fa77e4b5efb75 100755 --- a/uri/native_module_uri.cpp +++ b/uri/native_module_uri.cpp @@ -21,6 +21,9 @@ extern const char _binary_js_uri_js_start[]; extern const char _binary_js_uri_js_end[]; +extern const char _binary_uri_abc_start[]; +extern const char _binary_uri_abc_end[]; + namespace OHOS::Uri { static napi_value UriConstructor(napi_env env, napi_callback_info info) { @@ -49,7 +52,7 @@ namespace OHOS::Uri { } NAPI_CALL(env, napi_wrap(env, thisVar, object, [](napi_env env, void *data, void *hint) { - auto obj = (Uri*)data; + auto obj = reinterpret_cast(data); if (obj != nullptr) { delete obj; } @@ -62,7 +65,7 @@ namespace OHOS::Uri { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string normalizeUri = muri->Normalize(); napi_value result = nullptr; size_t tempLen = normalizeUri.size(); @@ -79,9 +82,9 @@ namespace OHOS::Uri { NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); Uri *other = nullptr; - NAPI_CALL(env, napi_unwrap(env, argv[0], (void**)&other)); + NAPI_CALL(env, napi_unwrap(env, argv[0], reinterpret_cast(&other))); bool flag = muri->Equals(*other); NAPI_CALL(env, napi_get_boolean(env, flag, &result)); @@ -94,7 +97,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); bool flag = muri->IsAbsolute(); NAPI_CALL(env, napi_get_boolean(env, flag, &result)); return result; @@ -106,7 +109,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->IsFailed(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -119,7 +122,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->ToString(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -132,7 +135,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetScheme(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -145,7 +148,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetAuthority(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -158,7 +161,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetSsp(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -171,7 +174,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetUserinfo(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -184,7 +187,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetHost(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -197,7 +200,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetPort(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -210,7 +213,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetPath(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -223,7 +226,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetQuery(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -236,7 +239,7 @@ namespace OHOS::Uri { napi_value result = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); Uri *muri = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&muri)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&muri))); std::string temp = muri->GetFragment(); size_t templen = temp.size(); NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); @@ -264,7 +267,7 @@ namespace OHOS::Uri { DECLARE_NAPI_GETTER("isFailed", IsFailed), }; NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor, - nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); + nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); static napi_property_descriptor desc[] = { DECLARE_NAPI_PROPERTY("Uri", uriClass) }; @@ -283,7 +286,16 @@ namespace OHOS::Uri { *bufLen = _binary_js_uri_js_end - _binary_js_uri_js_start; } } - + extern "C" + __attribute__((visibility("default"))) void NAPI_uri_GetABCCode(const char** buf, int* buflen) + { + if (buf != nullptr) { + *buf = _binary_uri_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_uri_abc_end - _binary_uri_abc_start; + } + } static napi_module UriModule = { .nm_version = 1, .nm_flags = 0, diff --git a/uri/js_uri.js b/uri/src/js_uri.ts old mode 100755 new mode 100644 similarity index 85% rename from uri/js_uri.js rename to uri/src/js_uri.ts index d2603eef19d2f5384fd397d9578af7acb5d2a25c..f94da8a92e58fb86cd533801806c65b72375a7de --- a/uri/js_uri.js +++ b/uri/src/js_uri.ts @@ -1,105 +1,100 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; -const uri = requireInternal("uri"); - -class URI { - constructor(input) { - if (typeof input !== 'string' || input.length === 0) { - throw new Error("input type err"); - } - this.uricalss = new uri.Uri(input); - let errStr = this.uricalss.isFailed; - if (errStr.length !== 0) { - throw new Error(errStr); - } - } - toString() { - return toAscllString(this.uricalss.toString()); - } - - equals(other) { - return this.uricalss.equals(other.uricalss); - } - - isAbsolute() { - return this.uricalss.isAbsolute(); - } - - normalize() { - let uriStr = this.uricalss.normalize(); - return createNewUri(uriStr); - } - - get scheme() { - return this.uricalss.scheme; - } - - get authority() { - return this.uricalss.authority; - } - - get ssp() { - return this.uricalss.ssp; - } - - get userinfo() { - return this.uricalss.userinfo; - } - - get host() { - return this.uricalss.host; - } - - get port() { - return this.uricalss.port; - } - - get path() { - return this.uricalss.path; - } - - get query() { - return this.uricalss.query; - } - - get fragment() { - return this.uricalss.fragment; - } - -} - -function toAscllString(uriStr) { - if (uriStr.indexOf('[') !== -1) { - let arr = uriStr.split("["); - let brr = arr[1].split("]"); - arr[1] = '[' + brr[0] + ']'; - arr[2] = brr[1]; - arr[0] = encodeURI(arr[0]); - arr[2] = encodeURI(arr[2]); - return arr.join(''); - } else { - return encodeURI(uriStr); - } -} - -function createNewUri(uriStr) { - return new URI(uriStr); -} - -export default { - URI: URI, -} +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +declare function requireInternal(s : string) : any; +const uri = requireInternal("uri"); + +class URI { + uricalss : any + constructor(input:any) { + if (typeof input !== 'string' || input.length === 0) { + throw new Error("input type err"); + } + this.uricalss = new uri.Uri(input); + let errStr = this.uricalss.isFailed; + if (errStr.length !== 0) { + throw new Error(errStr); + } + } + toString() { + return toAscllString(this.uricalss.toString()); + } + + equals(other:any) { + return this.uricalss.equals(other.uricalss); + } + + isAbsolute() { + return this.uricalss.isAbsolute(); + } + + normalize() { + return this.uricalss.normalize(); + } + + get scheme() { + return this.uricalss.scheme; + } + + get authority() { + return this.uricalss.authority; + } + + get ssp() { + return this.uricalss.ssp; + } + + get userinfo() { + return this.uricalss.userinfo; + } + + get host() { + return this.uricalss.host; + } + + get port() { + return this.uricalss.port; + } + + get path() { + return this.uricalss.path; + } + + get query() { + return this.uricalss.query; + } + + get fragment() { + return this.uricalss.fragment; + } + +} + +function toAscllString(uriStr:any) { + if (uriStr.indexOf('[') !== -1) { + let arr = uriStr.split("["); + let brr = arr[1].split("]"); + arr[1] = '[' + brr[0] + ']'; + arr[2] = brr[1]; + arr[0] = encodeURI(arr[0]); + arr[2] = encodeURI(arr[2]); + return arr.join(''); + } else { + return encodeURI(uriStr); + } +} + +export default { + URI: URI, +} diff --git a/uri/tsconfig.json b/uri/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..66ad148561b08a71630eb07c7484394c2acc7930 --- /dev/null +++ b/uri/tsconfig.json @@ -0,0 +1,14 @@ +{ +"compilerOptions": { + "target": "es6", + "module": "es6", + "rootDir": "./src", + //"outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./out", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noImplicitThis": false, + } +} diff --git a/url/BUILD.gn b/url/BUILD.gn index 5e8d16faf82d5734f13bca03768b8c4aef43c0e1..72187427873ea7175b1439bdbab889b52f4beafb 100755 --- a/url/BUILD.gn +++ b/url/BUILD.gn @@ -11,14 +11,59 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//ark/ts2abc/ts2panda/ts2abc_config.gni") import("//build/ohos.gni") import("//build/ohos/ace/ace.gni") +import("//foundation/ace/ace_engine/ace_config.gni") +# compile .ts to .js. + +action("build_ts_js") { + script = "//base/compileruntime/js_api_module/url/build_ts_js.py" + depfile = "$target_gen_dir/$target_name.d" + outputs = [ target_out_dir + "/js_url.js" ] +} base_output_path = get_label_info(":js_url", "target_out_dir") js_url_obj_path = base_output_path + "/url.o" + gen_js_obj("js_url") { - input = "//base/compileruntime/js_api_module/url/js_url.js" + input = "$target_out_dir/js_url.js" output = js_url_obj_path + dep = ":build_ts_js" +} + +# compile .js to .abc. +action("gen_url_abc") { + visibility = [ ":*" ] + script = "//ark/ts2abc/ts2panda/scripts/generate_js_bytecode.py" + + args = [ + "--src-js", + rebase_path(target_out_dir + "/js_url.js"), + "--dst-file", + rebase_path(target_out_dir + "/url.abc"), + "--node", + rebase_path("${node_path}"), + "--frontend-tool-path", + rebase_path("${ts2abc_build_path}"), + "--node-modules", + rebase_path("${node_modules}"), + ] + deps = [ + ":build_ts_js", + "//ark/ts2abc/ts2panda:ark_ts2abc_build", + ] + + inputs = [ target_out_dir + "/js_url.js" ] + outputs = [ target_out_dir + "/url.abc" ] +} + +abc_output_path = get_label_info(":url_abc", "target_out_dir") +url_abc_obj_path = abc_output_path + "/url_abc.o" +gen_js_obj("url_abc") { + input = "$target_out_dir/url.abc" + output = url_abc_obj_path + dep = ":gen_url_abc" } ohos_shared_library("url") { @@ -36,6 +81,7 @@ ohos_shared_library("url") { deps = [ ":js_url", + ":url_abc", "//base/compileruntime/js_api_module/url/:js_url", "//foundation/ace/napi/:ace_napi", "//foundation/ace/napi/:ace_napi_quickjs", diff --git a/url/build_ts_js.py b/url/build_ts_js.py new file mode 100755 index 0000000000000000000000000000000000000000..fb895e014581e8eac5f1948238fe4ea0af819d00 --- /dev/null +++ b/url/build_ts_js.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os + +if __name__ == '__main__': + + build_path = os.path.abspath(os.path.join(os.getcwd(), "../..")) + os.chdir("%s/base/compileruntime/js_api_module/url" % build_path) + os.system('../../../../developtools/ace-ets2bundle/compiler/node_modules/typescript/bin/tsc') + + if os.access("../../../../out/ohos-arm64-release", os.F_OK): + os.system('cp -r ./out/js_url.js ../../../../out/ohos-arm64-release/obj/base/compileruntime/js_api_module/url/js_url.js') + + if os.access("../../../../out/ohos-arm-release", os.F_OK): + os.system('cp -r ./out/js_url.js ../../../../out/ohos-arm-release/obj/base/compileruntime/js_api_module/url/js_url.js') + + os.system('rm -rf ./out') \ No newline at end of file diff --git a/url/js_url.cpp b/url/js_url.cpp index ba9f1b93303998245c5fce2510b399e6e78a845f..1b53b245079c25590b9fe717a8776b117ad7a624 100755 --- a/url/js_url.cpp +++ b/url/js_url.cpp @@ -1705,7 +1705,7 @@ namespace OHOS::Url { (charaEncode & 0x0000003F)]; // Acquisition method of the second byte output += output1 + output2; } else if ((charaEncode >= 0x0000E000) || - (charaEncode <= 0x0000D7FF)) { // Convert the Unicode code into three bytes + (charaEncode <= 0x0000D7FF)) { // Convert the Unicode code into three bytes std::string output1 = reviseChar[0x000000E0 | (charaEncode / 4096)]; // 4096:Acquisition method of the first byte std::string output2 = reviseChar[numOfAscii | @@ -1935,7 +1935,7 @@ namespace OHOS::Url { sname = ToUSVString(name); } delete[] name; - for (std::vector::iterator iter = searchParams.begin(); iter != searchParams.end();) { + for (auto iter = searchParams.begin(); iter != searchParams.end();) { if (*iter == sname) { iter = searchParams.erase(iter, iter + 2); // 2:Searching for the number and number of keys and values } else { @@ -2032,7 +2032,7 @@ namespace OHOS::Url { delete[] buffer1; } bool flag = false; - for (std::vector::iterator it = searchParams.begin(); it < searchParams.end() - 1;) { + for (auto it = searchParams.begin(); it < (searchParams.end() - 1);) { if (*it == cppName) { if (!flag) { *(it + 1) = cppValue; diff --git a/url/native_module_url.cpp b/url/native_module_url.cpp index 279f4316738e6d8449c3ebcb9578845a00c8589c..75cf918a592962293553f7e86cdacb7d40030e5e 100755 --- a/url/native_module_url.cpp +++ b/url/native_module_url.cpp @@ -20,6 +20,8 @@ extern const char _binary_js_url_js_start[]; extern const char _binary_js_url_js_end[]; +extern const char _binary_url_abc_start[]; +extern const char _binary_url_abc_end[]; namespace OHOS::Url { static void UrlStructor(napi_env &env, napi_callback_info &info, URL *&object) { @@ -58,7 +60,7 @@ namespace OHOS::Url { object = new URL(env, input, base); } else if (valuetype2 == napi_object) { URL *temp = nullptr; - napi_unwrap(env, argv[1], (void**)&temp); + napi_unwrap(env, argv[1], reinterpret_cast(&temp)); object = new URL(env, input, *temp); } else { HILOG_INFO("secondParameter error"); @@ -116,7 +118,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetHostname(); return retVal; } @@ -126,7 +128,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetSearch(); return retVal; } @@ -136,7 +138,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetUsername(); return retVal; } @@ -146,7 +148,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetPassword(); return retVal; } @@ -156,7 +158,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetFragment(); return retVal; } @@ -166,7 +168,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetScheme(); return retVal; } @@ -176,7 +178,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetPort(); return retVal; } @@ -186,7 +188,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetHost(); return retVal; } @@ -196,7 +198,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetPath(); return retVal; } @@ -206,7 +208,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetOnOrOff(); return retVal; } @@ -216,7 +218,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetIsIpv6(); return retVal; } @@ -241,7 +243,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetHref(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -268,7 +270,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetHostname(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -295,7 +297,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetPort(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -322,7 +324,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetHost(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -349,7 +351,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetSearch(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -376,7 +378,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetScheme(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -403,7 +405,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetFragment(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -430,7 +432,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetUsername(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -457,7 +459,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetPath(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -484,7 +486,7 @@ namespace OHOS::Url { type = nullptr; } URL *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetPassword(input); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -535,7 +537,7 @@ namespace OHOS::Url { } } URLSearchParams *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); murl->SetArray(vec); napi_value result = nullptr; NAPI_CALL(env, napi_get_undefined(env, &result)); @@ -547,7 +549,7 @@ namespace OHOS::Url { napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr)); URLSearchParams *murl = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&murl)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&murl))); napi_value retVal = murl->GetArray(); return retVal; } @@ -563,7 +565,7 @@ namespace OHOS::Url { return nullptr; } URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); napi_value result = object->Get(args); return result; } @@ -579,7 +581,7 @@ namespace OHOS::Url { return nullptr; } URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); napi_value result = object->GetAll(args); return result; } @@ -596,7 +598,7 @@ namespace OHOS::Url { return nullptr; } URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); object->Append(args[0], args[1]); return nullptr; } @@ -612,7 +614,7 @@ namespace OHOS::Url { return nullptr; } URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); object->Delete(args); return nullptr; } @@ -624,7 +626,7 @@ namespace OHOS::Url { napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); object->ForEach(args, thisVar); return nullptr; } @@ -636,7 +638,7 @@ namespace OHOS::Url { napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); napi_value result = object->Entries(); return result; } @@ -648,7 +650,7 @@ namespace OHOS::Url { napi_value args = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr)); URLSearchParams *object = nullptr; - NAPI_CALL(env, napi_unwrap(env, thisVar, (void**)&object)); + NAPI_CALL(env, napi_unwrap(env, thisVar, reinterpret_cast(&object))); napi_value result = object->IsHas(args); return result; } @@ -660,7 +662,7 @@ namespace OHOS::Url { napi_value args[2] = { 0 }; napi_get_cb_info(env, info, &argc, args, &thisVar, nullptr); URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); object->Set(args[0], args[1]); return nullptr; } @@ -672,7 +674,7 @@ namespace OHOS::Url { napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); object->Sort(); return nullptr; } @@ -684,7 +686,7 @@ namespace OHOS::Url { napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); napi_value result = object->ToString(); return result; } @@ -696,7 +698,7 @@ namespace OHOS::Url { napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); napi_value result = object->IterByKeys(); return result; } @@ -708,7 +710,7 @@ namespace OHOS::Url { napi_value args = nullptr; napi_get_cb_info(env, info, &argc, &args, &thisVar, nullptr); URLSearchParams *object = nullptr; - napi_unwrap(env, thisVar, (void**)&object); + napi_unwrap(env, thisVar, reinterpret_cast(&object)); napi_value result = object->IterByValues(); return result; } @@ -884,7 +886,7 @@ namespace OHOS::Url { DECLARE_NAPI_GETTER("GetIsIpv6", GetIsIpv6), }; NAPI_CALL(env, napi_define_class(env, urlClassName, strlen(urlClassName), UrlConstructor, - nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &urlClass)); + nullptr, sizeof(UrlDesc) / sizeof(UrlDesc[0]), UrlDesc, &urlClass)); static napi_property_descriptor desc[] = { DECLARE_NAPI_PROPERTY("Url", urlClass) }; @@ -914,7 +916,16 @@ namespace OHOS::Url { *bufLen = _binary_js_url_js_end - _binary_js_url_js_start; } } - + extern "C" + __attribute__((visibility("default"))) void NAPI_url_GetABCCode(const char** buf, int* buflen) + { + if (buf != nullptr) { + *buf = _binary_url_abc_start; + } + if (buflen != nullptr) { + *buflen = _binary_url_abc_end - _binary_url_abc_start; + } + } static napi_module UrlModule = { .nm_version = 1, .nm_flags = 0, diff --git a/url/js_url.js b/url/src/js_url.ts old mode 100755 new mode 100644 similarity index 82% rename from url/js_url.js rename to url/src/js_url.ts index 7eef1b4c43b8ffd514dd0fea8fbdbe789660c41e..aae31cf4459ab5329a42f9043bc2830f97f4aa9c --- a/url/js_url.js +++ b/url/src/js_url.ts @@ -1,416 +1,420 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict'; -const Url = requireInternal("url"); - -let seachParamsArr = []; -class URLSearchParams { - urlcalss; - constructor(input) { - let out = []; - out = parameterProcessing(input); - this.urlcalss = new Url.URLSearchParams1(); - this.urlcalss.array = out; - } - append(params1, params2) { - this.urlcalss.append(params1, params2); - } - - set(setname, setvalues) { - this.urlcalss.set(setname, setvalues); - } - - sort() { - this.urlcalss.sort(); - } - - has(hasname) { - return this.urlcalss.has(hasname); - } - toString() { - return this.urlcalss.toString(); - } - - keys() { - return this.urlcalss.keys(); - } - - values() { - console.log('constructor failed'); - return this.urlcalss.values(); - } - - getAll(getAllname) { - return this.urlcalss.getAll(getAllname); - } - - get(getname) { - return this.urlcalss.get(getname); - } - - entries() { - - return this.urlcalss.entries(); - } - - delete(deletename) { - this.urlcalss.delete(deletename); - } - - forEach(objfun) { - return this.urlcalss.forEach(objfun); - } - - [Symbol.iterator]() { - return this.urlcalss.entries(); - } - - updateParams(input) { - let out = []; - out = parameterProcessing(input); - this.urlcalss.array = out; - } -} - -function toHleString(arg) { - return arg.toString(); -} - -function parameterProcessing(input) { - if (input === null || typeof input === 'undefined') { - seachParamsArr = []; - return seachParamsArr; - } else if (typeof input === 'object' || typeof input === 'function') { - return initObjectSeachParams(input); - } else { - return initToStringSeachParams(input); - } -} -function initObjectSeachParams(input) { - if (typeof input[Symbol.iterator] === 'function') { - return iteratorMethod(input); - } - return recordMethod(input); -} -function recordMethod(input) { - const objectkeys = Reflect.ownKeys(input); - seachParamsArr = []; - let objectlength = objectkeys.length; - for (let i = 0; i <= objectlength; i++) { - const objectkey = objectkeys[i]; - const descry = Reflect.getOwnPropertyDescriptor(input, objectkey); - if (descry !== undefined && descry.enumerable) { - const inputkey = toHleString(objectkey); - const inputValue = toHleString(input[objectkey]); - seachParamsArr.push(inputkey, inputValue); - } - } - return seachParamsArr; -} -function iteratorMethod(input) { - let pairs = []; - seachParamsArr = []; - for (const pair of input) { - const conversionsPair = []; - for (let element of pair) { - conversionsPair.push(element); - } - pairs.push(conversionsPair); - } - - for (const pair of pairs) { - if (pair.length !== 2) { - console.log('key-value-is-worong'); - } - seachParamsArr.push(pair[0], pair[1]); - } - return seachParamsArr; -} - -function initToStringSeachParams(input) { - if (input[0] === '?') { - input = input.slice(1); - } - let strVal = decodeURI(input); - seachParamsArr = Url.stringParmas(strVal); - return seachParamsArr; -} -class URL { - href_; - search_; - origin_; - username_; - password_; - hostname_; - host_; - hash_; - protocol_; - pathname_; - port_; - searchParamsClass_; - c_info; - constructor() { - let nativeUrl; - let inputUrl; - let inputBase; - - if (arguments.length === 1) { - inputUrl = arguments[0]; - if (typeof inputUrl === 'string' && inputUrl.length > 0) { - nativeUrl = new Url.Url(inputUrl); - } else { - console.log('Input parameter error'); - } - } - - if (arguments.length === 2) { - inputUrl = arguments[0]; - inputBase = arguments[1]; - if (typeof inputUrl === 'string') { - if (typeof inputBase === 'string') { - if (inputBase.length > 0) { - nativeUrl = new Url.Url(inputUrl, inputBase); - } else { - console.log('Input parameter error'); - return; - } - } - if (typeof inputBase === 'object') { - let nativeBase = inputBase.getInfo(); - nativeUrl = new Url.Url(inputUrl, nativeBase); - } - } - } - this.c_info = nativeUrl; - if (nativeUrl.onOrOff) { - this.search_ = encodeURI(nativeUrl.search); - this.username_ = encodeURI(nativeUrl.username); - this.password_ = encodeURI(nativeUrl.password); - if (nativeUrl.GetIsIpv6) { - this.hostname_ = nativeUrl.hostname; - this.host_ = nativeUrl.host; - } else { - this.hostname_ = encodeURI(nativeUrl.hostname); - this.host_ = encodeURI(nativeUrl.host); - } - this.hash_ = encodeURI(nativeUrl.hash); - this.protocol_ = encodeURI(nativeUrl.protocol); - this.pathname_ = encodeURI(nativeUrl.pathname); - this.port_ = nativeUrl.port; - this.origin_ = nativeUrl.protocol + '//' + nativeUrl.host; - this.searchParamsClass_ = new URLSearchParams(this.search_); - this.set_href(); - } else { - console.log('constructor failed'); - } - } - getInfo() { - return this.c_info; - } - toString() { - return this.href_; - } - - get protocol() { - return this.protocol_; - } - set protocol(scheme) { - if (scheme.length === 0) { - return; - } - if (this.protocol_ === 'file:' - && (this.host_ === '' || this.host_ == null)) { - return; - } - this.c_info.protocol = scheme; - this.protocol_ = this.c_info.protocol; - this.set_href() - } - get origin() { - let kOpaqueOrigin = 'null'; - switch (this.protocol_) { - case 'ftp:': - case 'gopher:': - case 'http:': - case 'https:': - case 'ws:': - case 'wss:': - return this.origin_; - } - return kOpaqueOrigin; - } - get username() { - return this.username_; - } - set username(input) { - if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') { - return; - } - const usname_ = escape(input); - this.c_info.username = usname_; - this.username_ = this.c_info.username; - this.set_href(); - } - get password() { - return this.password_; - } - set password(input) { - if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') { - return; - } - const passwd_ = escape(input); - this.c_info.password = passwd_; - this.password_ = this.c_info.password; - this.set_href(); - } - get hash() { - return this.hash_; - } - set hash(fragment) { - const fragment_ = encodeURI(fragment); - this.c_info.hash = fragment_; - this.hash_ = this.c_info.hash; - this.set_href(); - } - get search() { - return this.search_; - } - set search(query) { - const query_ = encodeURI(query); - this.c_info.search = query_; - this.search_ = this.c_info.search; - this.searchParamsClass_.updateParams(this.search_); - this.set_href(); - } - get hostname() { - return this.hostname_; - } - set hostname(hostname) { - this.c_info.hostname = hostname; - if (this.c_info.GetIsIpv6) { - this.hostname_ = this.c_info.hostname; - } else { - this.hostname_ = encodeURI(this.c_info.hostname); - } - this.set_href(); - } - get host() { - return this.host_; - } - set host(host_) { - this.c_info.host = host_; - if (this.c_info.GetIsIpv6) { - this.host_ = this.c_info.host; - this.hostname_ = this.c_info.hostname; - this.port_ = this.c_info.port; - } else { - this.host_ = encodeURI(this.c_info.host); - this.hostname_ = encodeURI(this.c_info.hostname); - this.port_ = this.c_info.port; - } - this.set_href(); - } - get port() { - return this.port_; - } - set port(port) { - if (this.host_ === '' || this.protocol_ === 'file:' || port === '') { - return; - } - this.c_info.port = port; - this.port_ = this.c_info.port; - this.set_href(); - } - get href() { - return this.href_; - } - set href(href_) { - this.c_info.href(href_); - if (this.c_info.onOrOff) { - this.search_ = encodeURI(this.c_info.search); - this.username_ = encodeURI(this.c_info.username); - this.password_ = encodeURI(this.c_info.password); - if (this.c_info.GetIsIpv6) { - this.hostname_ = this.c_info.hostname; - this.host_ = this.c_info.host; - } else { - this.hostname_ = encodeURI(this.c_info.hostname); - this.host_ = encodeURI(this.c_info.host); - } - this.hash_ = encodeURI(this.c_info.hash); - this.protocol_ = encodeURI(this.c_info.protocol); - this.pathname_ = encodeURI(this.c_info.pathname); - this.port_ = this.c_info.port; - this.origin_ = this.protocol_ + '//' + this.host_; - this.searchParamsClass_.updateParams(this.search_); - this.set_href(); - } - } - - get pathname() { - return this.pathname_; - } - set pathname(path) { - const path_ = encodeURI(path); - this.c_info.pathname = path_; - this.pathname_ = this.c_info.pathname; - this.set_href(); - } - - get searchParams() { - return this.searchParamsClass_; - } - - toJSON() { - return this.href_; - } - set_href() { - let temp = this.protocol_; - if (this.hostname_ !== '') { - temp += '//'; - if (this.password_ !== '' || this.username_ !== '') { - if (this.username_ !== '') { - temp += this.username_; - } - if (this.password_ !== '') { - temp += ':'; - temp += this.password_; - } - temp += '@'; - } - temp += this.hostname_; - if (this.port_ !== '') { - temp += ':'; - temp += this.port_; - } - } else if (this.protocol_ === 'file:') { - temp += '//'; - } - temp += this.pathname_; - if (this.search_) { - temp += this.search_; - } - if (this.hash_) { - temp += this.hash_; - } - this.href_ = temp; - } -} - -export default { - URLSearchParams: URLSearchParams, - URL: URL, -} +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +declare function requireInternal(s : string) : any; +const urlUtil = requireInternal('url'); + +let seachParamsArr:Array = []; + +class URLSearchParams { + urlcalss:any; + constructor(input:any) { + let out = []; + out = parameterProcessing(input); + this.urlcalss = new urlUtil.URLSearchParams1(); + this.urlcalss.array = out; + } + append(params1:string, params2:string) { + this.urlcalss.append(params1, params2); + } + + set(setname:string, setvalues:string) { + this.urlcalss.set(setname, setvalues); + } + + sort() { + this.urlcalss.sort(); + } + + has(hasname:string) { + return this.urlcalss.has(hasname); + } + + toString() { + return this.urlcalss.toString(); + } + + keys() { + return this.urlcalss.keys(); + } + + values() { + return this.urlcalss.values(); + } + + getAll(getAllname:string) { + return this.urlcalss.getAll(getAllname); + } + + get(getname:string) { + return this.urlcalss.get(getname); + } + + entries() { + + return this.urlcalss.entries(); + } + + delete(deletename:string) { + this.urlcalss.delete(deletename); + } + + forEach(objfun:object) { + return this.urlcalss.forEach(objfun); + } + + [Symbol.iterator]() { + return this.urlcalss.entries(); + } + + updateParams(input:any) { + let out = []; + out = parameterProcessing(input); + this.urlcalss.array = out; + } +} + +function toHleString(arg:any) { + return arg.toString(); +} + +function parameterProcessing(input:any) { + if (input === null || typeof input === 'undefined') { + seachParamsArr = []; + return seachParamsArr; + } else if (typeof input === 'object' || typeof input === 'function') { + return initObjectSeachParams(input); + } else { + return initToStringSeachParams(input); + } +} + +function initObjectSeachParams(input:any) { + if (typeof input[Symbol.iterator] === 'function') { + return iteratorMethod(input); + } + return recordMethod(input); +} + +function recordMethod(input:any) { + const keys = Reflect.ownKeys(input); + seachParamsArr = []; + for (let i = 0; i <= keys.length; i++) { + const key = keys[i]; + const desc = Reflect.getOwnPropertyDescriptor(input, key); + if (desc !== undefined && desc.enumerable) { + const typedKey = toHleString(key); + const typedValue = toHleString(input[key]); + seachParamsArr.push(typedKey, typedValue); + } + } + return seachParamsArr; +} + +function iteratorMethod(input:any) { + let pairs = []; + seachParamsArr = []; + for (const pair of input) { + const convertedPair = []; + for (let element of pair) { + convertedPair.push(element); + } + pairs.push(convertedPair); + } + + for (const pair of pairs) { + if (pair.length !== 2) { + console.log('key-value-is-worong'); + } + seachParamsArr.push(pair[0], pair[1]); + } + return seachParamsArr; +} + +function initToStringSeachParams(input:any) { + if (input[0] === '?') { + input = input.slice(1); + } + let strVal = decodeURI(input); + seachParamsArr = urlUtil.stringParmas(strVal); + return seachParamsArr; +} + +class URL { + href_:string = ''; + search_:string = ''; + origin_:string = ''; + username_:string = ''; + password_:string = ''; + hostname_:string = ''; + host_:string = ''; + hash_:string = ''; + protocol_:string = ''; + pathname_:string = ''; + port_:string = ''; + searchParamsClass_:any; + c_info:any; + constructor() { + let nativeUrl:any; + let inputUrl:string = ''; + let inputBase:any; + + if (arguments.length === 1) { + inputUrl = arguments[0]; + if (typeof inputUrl === 'string' && inputUrl.length > 0) { + nativeUrl = new urlUtil.Url(inputUrl); + } else { + console.log('Input parameter error'); + } + } + + if (arguments.length === 2) { + inputUrl = arguments[0]; + inputBase = arguments[1]; + if (typeof inputUrl === 'string') { + if (typeof inputBase === 'string') { + if (inputBase.length > 0) { + nativeUrl = new urlUtil.Url(inputUrl, inputBase); + } else { + console.log('Input parameter error'); + return; + } + } + if (typeof inputBase === 'object') { + let nativeBase = inputBase.getInfo(); + nativeUrl = new urlUtil.Url(inputUrl, nativeBase); + } + } + } + this.c_info = nativeUrl; + if (nativeUrl.onOrOff) { + this.search_ = encodeURI(nativeUrl.search); + this.username_ = encodeURI(nativeUrl.username); + this.password_ = encodeURI(nativeUrl.password); + if (nativeUrl.GetIsIpv6) { + this.hostname_ = nativeUrl.hostname; + this.host_ = nativeUrl.host; + } else { + this.hostname_ = encodeURI(nativeUrl.hostname); + this.host_ = encodeURI(nativeUrl.host); + } + this.hash_ = encodeURI(nativeUrl.hash); + this.protocol_ = encodeURI(nativeUrl.protocol); + this.pathname_ = encodeURI(nativeUrl.pathname); + this.port_ = nativeUrl.port; + this.origin_ = nativeUrl.protocol + '//' + nativeUrl.host; + this.searchParamsClass_ = new URLSearchParams(this.search_); + this.set_href(); + } else { + console.log('constructor failed'); + } + } + getInfo() { + return this.c_info; + } + toString() { + return this.href_; + } + + get protocol() { + return this.protocol_; + } + set protocol(scheme) { + if (scheme.length === 0) { + return; + } + if (this.protocol_ === 'file:' + && (this.host_ === '' || this.host_ == null)) { + return; + } + this.c_info.protocol = scheme; + this.protocol_ = this.c_info.protocol; + this.set_href() + } + get origin() { + let kOpaqueOrigin = 'null'; + switch (this.protocol_) { + case 'ftp:': + case 'gopher:': + case 'http:': + case 'https:': + case 'ws:': + case 'wss:': + return this.origin_; + } + return kOpaqueOrigin; + } + get username() { + return this.username_; + } + set username(input) { + if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') { + return; + } + const usname_ = escape(input); + this.c_info.username = usname_; + this.username_ = this.c_info.username; + this.set_href(); + } + get password() { + return this.password_; + } + set password(input) { + if (this.host_ == null || this.host_ === '' || this.protocol_ === 'file:') { + return; + } + const passwd_ = escape(input); + this.c_info.password = passwd_; + this.password_ = this.c_info.password; + this.set_href(); + } + get hash() { + return this.hash_; + } + set hash(fragment) { + const fragment_ = encodeURI(fragment); + this.c_info.hash = fragment_; + this.hash_ = this.c_info.hash; + this.set_href(); + } + get search() { + return this.search_; + } + set search(query) { + const query_ = encodeURI(query); + this.c_info.search = query_; + this.search_ = this.c_info.search; + this.searchParamsClass_.updateParams(this.search_); + this.set_href(); + } + get hostname() { + return this.hostname_; + } + set hostname(hostname) { + this.c_info.hostname = hostname; + if (this.c_info.GetIsIpv6) { + this.hostname_ = this.c_info.hostname; + } else { + this.hostname_ = encodeURI(this.c_info.hostname); + } + this.set_href(); + } + get host() { + return this.host_; + } + set host(host_) { + this.c_info.host = host_; + if (this.c_info.GetIsIpv6) { + this.host_ = this.c_info.host; + this.hostname_ = this.c_info.hostname; + this.port_ = this.c_info.port; + } else { + this.host_ = encodeURI(this.c_info.host); + this.hostname_ = encodeURI(this.c_info.hostname); + this.port_ = this.c_info.port; + } + this.set_href(); + } + get port() { + return this.port_; + } + set port(port) { + if (this.host_ === '' || this.protocol_ === 'file:' || port === '') { + return; + } + this.c_info.port = port; + this.port_ = this.c_info.port; + this.set_href(); + } + get href() { + return this.href_; + } + set href(href_) { + this.c_info.href(href_); + if (this.c_info.onOrOff) { + this.search_ = encodeURI(this.c_info.search); + this.username_ = encodeURI(this.c_info.username); + this.password_ = encodeURI(this.c_info.password); + if (this.c_info.GetIsIpv6) { + this.hostname_ = this.c_info.hostname; + this.host_ = this.c_info.host; + } else { + this.hostname_ = encodeURI(this.c_info.hostname); + this.host_ = encodeURI(this.c_info.host); + } + this.hash_ = encodeURI(this.c_info.hash); + this.protocol_ = encodeURI(this.c_info.protocol); + this.pathname_ = encodeURI(this.c_info.pathname); + this.port_ = this.c_info.port; + this.origin_ = this.protocol_ + '//' + this.host_; + this.searchParamsClass_.updateParams(this.search_); + this.set_href(); + } + } + + get pathname() { + return this.pathname_; + } + set pathname(path) { + const path_ = encodeURI(path); + this.c_info.pathname = path_; + this.pathname_ = this.c_info.pathname; + this.set_href(); + } + + get searchParams() { + return this.searchParamsClass_; + } + + toJSON() { + return this.href_; + } + set_href() { + let temp = this.protocol_; + if (this.hostname_ !== '') { + temp += '//'; + if (this.password_ !== '' || this.username_ !== '') { + if (this.username_ !== '') { + temp += this.username_; + } + if (this.password_ !== '') { + temp += ':'; + temp += this.password_; + } + temp += '@'; + } + temp += this.hostname_; + if (this.port_ !== '') { + temp += ':'; + temp += this.port_; + } + } else if (this.protocol_ === 'file:') { + temp += '//'; + } + temp += this.pathname_; + if (this.search_) { + temp += this.search_; + } + if (this.hash_) { + temp += this.hash_; + } + this.href_ = temp; + } +} + +export default { + URLSearchParams: URLSearchParams, + URL: URL, +} diff --git a/url/tsconfig.json b/url/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..66ad148561b08a71630eb07c7484394c2acc7930 --- /dev/null +++ b/url/tsconfig.json @@ -0,0 +1,14 @@ +{ +"compilerOptions": { + "target": "es6", + "module": "es6", + "rootDir": "./src", + //"outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./out", /* Specify an output folder for all emitted files. */ + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "skipLibCheck": true, + "noImplicitThis": false, + } +}