From 657d74ba23f10cde79ba761d38d55572ea517d53 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Thu, 16 Sep 2021 11:42:52 +0800 Subject: [PATCH 1/4] modify bug Signed-off-by: lifansheng --- api/js_uri.cpp | 22 +++++++++++------ convertxml/js_convertxml.cpp | 32 ++++++++++++++++--------- convertxml/js_convertxml.h | 2 +- convertxml/native_module_convertxml.cpp | 3 +-- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/api/js_uri.cpp b/api/js_uri.cpp index 306c1180..7a0cb25c 100755 --- a/api/js_uri.cpp +++ b/api/js_uri.cpp @@ -87,6 +87,9 @@ namespace OHOS::Api { if (pos != std::string::npos) { if (pos != 0) { AnalysisFragment(pos); + if (!errStr_.empty()) { + return; + } } else { errStr_ = "#It can't be the first"; return; @@ -229,11 +232,14 @@ namespace OHOS::Api { return; } } - bool isLawfulProt = false; + bool isLawfulProt = true; // find port pos = data_.rfind(':'); if (pos != std::string::npos) { - isLawfulProt = AnalysisPort(pos); + size_t pos1 = data_.rfind(']'); + if (pos1 == std::string::npos || pos > pos1) { + isLawfulProt = AnalysisPort(pos); + } if (!errStr_.empty()) { return; } @@ -244,12 +250,12 @@ namespace OHOS::Api { if (data_[data_.size() - 1] == ']') { // IPV6 if (!isLawfulProt) { - errStr_ = "ipv6 does not conform to the rule"; + errStr_ = "Prot does not conform to the rule"; return; } AnalysisIPV6(); } else { - errStr_ = "ipv6 does not conform to the rule"; + errStr_ = "IPv6 is missing a closing bracket"; return; } } else { @@ -303,8 +309,10 @@ namespace OHOS::Api { bool Uri::AnalysisIPV4() { + HILOG_INFO("------CJX-------data_ = %{public}s ",data_.c_str()); std::regex ipv4("((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"); - std::regex hostname("([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?\\.)+([a-zA-Z]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?)"); + std::regex hostname("(([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?\\.)+([a-zA-Z]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?))|" + "([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?)"); bool isIpv4 = std::regex_match(data_, ipv4); bool isHosName = std::regex_match(data_, hostname); if (!isIpv4 && !isHosName) { @@ -319,7 +327,7 @@ namespace OHOS::Api { void Uri::AnalysisIPV6() { std::string str = data_.substr(1, data_.size() - 2); // 2:Intercept the string from the second subscript - std::regex ipv6("^(::|(:((:[0-9A-Fa-f]{1,4}){1,7}))|(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|" + std::regex ipv6("(::|(:((:[0-9A-Fa-f]{1,4}){1,7}))|(([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|" "(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|:))|(([0-9A-Fa-f]{1,4}:){2}" "(((:[0-9A-Fa-f]{1,4}){1,5})|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})" "|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|:))|(([0-9A-Fa-f]{1,4}:){5}" @@ -328,7 +336,7 @@ namespace OHOS::Api { "|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}:)|(([0-9A-Fa-f]{1,4}:){3}" "(:[0-9A-Fa-f]{1,4}){0,2}:)|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4})?:)|" "(([0-9A-Fa-f]{1,4}:){5}:)|(([0-9A-Fa-f]{1,4}:){6}))((25[0-5]|2[0-4]\\d|1\\d{2}|" - "[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)))(%[a-zA-Z0-9._]+)?$"); + "[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)))(%[a-zA-Z0-9._]+)?"); if (!std::regex_match(str, ipv6)) { errStr_ = "ipv6 does not conform to the rule"; return; diff --git a/convertxml/js_convertxml.cpp b/convertxml/js_convertxml.cpp index ba50cbd6..d0b5e101 100755 --- a/convertxml/js_convertxml.cpp +++ b/convertxml/js_convertxml.cpp @@ -111,18 +111,22 @@ void ConvertXml::GetPrevNodeList(xmlNodePtr curNode) curNode = curNode->prev; napi_value elementsObject = nullptr; napi_create_object(env_, &elementsObject); - SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); - if (curNode->type == xmlElementType::XML_PI_NODE) { + 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)); + m_prevObj.push_back(elementsObject); } - if (curNode->type == xmlElementType::XML_COMMENT_NODE) { + 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)); + m_prevObj.push_back(elementsObject); } - if (curNode->type == xmlElementType::XML_DTD_NODE) { + 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); + m_prevObj.push_back(elementsObject); } - m_prevObj.push_back(elementsObject); } } @@ -140,27 +144,33 @@ void ConvertXml::SetAttributes(xmlNodePtr curNode, napi_value &elementsObject) } } -void ConvertXml::SetXmlElementType(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; } else if (curNode->type == xmlElementType::XML_COMMENT_NODE && !m_Options.ignoreComment) { SetKeyValue(elementsObject, m_Options.comment.c_str(), (const char*)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; } } void ConvertXml::SetNodeInfo(xmlNodePtr curNode, napi_value &elementsObject) { if (curNode->type == xmlElementType::XML_PI_NODE) { - SetKeyValue(elementsObject, m_Options.type, m_Options.instruction); + if (!m_Options.ignoreInstruction) { + SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); + } } else { SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); } - SetKeyValue(elementsObject, m_Options.type, GetNodeType(curNode->type)); if ((curNode->type != xmlElementType::XML_COMMENT_NODE) && (curNode->type != xmlElementType::XML_CDATA_SECTION_NODE)) { - SetKeyValue(elementsObject, m_Options.name, (char*)curNode->name); + if (!(curNode->type == xmlElementType::XML_PI_NODE && m_Options.ignoreInstruction)) { + SetKeyValue(elementsObject, m_Options.name, (char*)curNode->name); + } } } @@ -192,6 +202,7 @@ void ConvertXml::SetPrevInfo(napi_value &recvElement, int flag, int32_t &index1) napi_set_element(env_, recvElement, index1++, m_prevObj[i]); } } + } void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) @@ -224,8 +235,7 @@ void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) GetXMLInfo(curNode, elementsObject, 1); bFlag = true; } else if (index % 2 != 0) { // 2:pNode - SetXmlElementType(pNode, elementsObject); - bFlag = true; + SetXmlElementType(pNode, elementsObject, bFlag); } else if (pNode->next == nullptr) { SetEndInfo(pNode, elementsObject, bFlag, bText, index); } diff --git a/convertxml/js_convertxml.h b/convertxml/js_convertxml.h index 2fb94b2f..bbe2381f 100755 --- a/convertxml/js_convertxml.h +++ b/convertxml/js_convertxml.h @@ -64,7 +64,7 @@ public: explicit ConvertXml(napi_env env); virtual ~ConvertXml() {} void SetAttributes(xmlNodePtr curNode, napi_value &elementsObject); - void SetXmlElementType(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 GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag = 0); diff --git a/convertxml/native_module_convertxml.cpp b/convertxml/native_module_convertxml.cpp index 96b73c4a..730a9255 100755 --- a/convertxml/native_module_convertxml.cpp +++ b/convertxml/native_module_convertxml.cpp @@ -109,8 +109,7 @@ static napi_module ConvertXmlModule = { .reserved = { 0 }, }; -extern "C" __attribute__ ((constructor)) void RegisterModule() -{ +extern "C" __attribute__ ((constructor)) void RegisterModule() { napi_module_register(&ConvertXmlModule); } -- Gitee From 5ca6e92f3cebb817ac798f1fd86d08faf2d554c4 Mon Sep 17 00:00:00 2001 From: lifansheng Date: Thu, 16 Sep 2021 16:03:37 +0800 Subject: [PATCH 2/4] modify codecheck Signed-off-by: lifansheng --- api/js_uri.cpp | 25 ++++++++++--------------- convertxml/js_convertxml.cpp | 1 - 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/api/js_uri.cpp b/api/js_uri.cpp index 7a0cb25c..5a49c969 100755 --- a/api/js_uri.cpp +++ b/api/js_uri.cpp @@ -82,18 +82,15 @@ namespace OHOS::Api { void Uri::AnalysisUri() { data_ = inputUri_; - // Fragment - size_t pos = data_.find('#'); - if (pos != std::string::npos) { - if (pos != 0) { - AnalysisFragment(pos); - if (!errStr_.empty()) { - return; - } - } else { - errStr_ = "#It can't be the first"; - return; - } + size_t pos = data_.find('#'); // Fragment + if ((pos != std::string::npos) && (pos != 0)) { + AnalysisFragment(pos); + if (!errStr_.empty()) { + return; + } + if ((pos != std::string::npos) && (pos == 0)) + errStr_ = "#It can't be the first"; + return; } pos = data_.find('?'); // Query if (pos != std::string::npos) { @@ -116,8 +113,7 @@ namespace OHOS::Api { uriData_.SchemeSpecificPart = data_ + "?" + uriData_.query; return; } - // userInfo path host port ipv4 or ipv6 - pos = data_.find("//"); + pos = data_.find("//"); // userInfo path host port ipv4 or ipv6 if (pos != std::string::npos && pos == 0) { uriData_.SchemeSpecificPart = data_ + "?" + uriData_.query; data_ = data_.substr(2); // 2:Intercept the string from the second subscript @@ -309,7 +305,6 @@ namespace OHOS::Api { bool Uri::AnalysisIPV4() { - HILOG_INFO("------CJX-------data_ = %{public}s ",data_.c_str()); std::regex ipv4("((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)"); std::regex hostname("(([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?\\.)+([a-zA-Z]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?))|" "([a-zA-Z0-9]([a-zA-Z0-9\\-]*[a-zA-Z0-9])?)"); diff --git a/convertxml/js_convertxml.cpp b/convertxml/js_convertxml.cpp index d0b5e101..e635af00 100755 --- a/convertxml/js_convertxml.cpp +++ b/convertxml/js_convertxml.cpp @@ -202,7 +202,6 @@ void ConvertXml::SetPrevInfo(napi_value &recvElement, int flag, int32_t &index1) napi_set_element(env_, recvElement, index1++, m_prevObj[i]); } } - } void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) -- Gitee From bee85b0646523c0bf911a96b8cfcfdad1a608dfd Mon Sep 17 00:00:00 2001 From: lifansheng Date: Fri, 17 Sep 2021 10:27:58 +0800 Subject: [PATCH 3/4] modify js_api_module Signed-off-by: lifansheng --- convertxml/js_convertxml.cpp | 1 + ohos.build | 3 +- {api => uri}/BUILD.gn | 26 +- {api => uri}/js_uri.cpp | 11 +- {api => uri}/js_uri.h | 2 +- uri/js_uri.js | 100 ++++++ uri/native_module_uri.cpp | 299 ++++++++++++++++++ url/BUILD.gn | 59 ++++ {api => url}/js_url.cpp | 2 +- {api => url}/js_url.h | 2 +- api/js_api.js => url/js_url.js | 92 +----- .../native_module_url.cpp | 270 +--------------- 12 files changed, 498 insertions(+), 369 deletions(-) rename {api => uri}/BUILD.gn (73%) rename {api => uri}/js_uri.cpp (95%) rename {api => uri}/js_uri.h (96%) create mode 100755 uri/js_uri.js create mode 100755 uri/native_module_uri.cpp create mode 100755 url/BUILD.gn rename {api => url}/js_url.cpp (97%) rename {api => url}/js_url.h (97%) rename api/js_api.js => url/js_url.js (82%) rename api/native_module_api.cpp => url/native_module_url.cpp (74%) diff --git a/convertxml/js_convertxml.cpp b/convertxml/js_convertxml.cpp index e635af00..d0b5e101 100755 --- a/convertxml/js_convertxml.cpp +++ b/convertxml/js_convertxml.cpp @@ -202,6 +202,7 @@ void ConvertXml::SetPrevInfo(napi_value &recvElement, int flag, int32_t &index1) napi_set_element(env_, recvElement, index1++, m_prevObj[i]); } } + } void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) diff --git a/ohos.build b/ohos.build index 1997a40b..64cf5d14 100755 --- a/ohos.build +++ b/ohos.build @@ -7,7 +7,8 @@ "phone" ], "module_list": [ - "//base/compileruntime/js_api_module/api:api_packages", + "//base/compileruntime/js_api_module/uri:uri_packages", + "//base/compileruntime/js_api_module/url:url_packages", "//base/compileruntime/js_api_module/convertxml:convertxml_packages" ], "inner_kits": [ diff --git a/api/BUILD.gn b/uri/BUILD.gn similarity index 73% rename from api/BUILD.gn rename to uri/BUILD.gn index 829f0f2b..0249e7a3 100755 --- a/api/BUILD.gn +++ b/uri/BUILD.gn @@ -14,30 +14,29 @@ import("//build/ohos.gni") import("//build/ohos/ace/ace.gni") -base_output_path = get_label_info(":js_api", "target_out_dir") -js_api_obj_path = base_output_path + "/api.o" -gen_js_obj("js_api") { - input = "//base/compileruntime/js_api_module/api/js_api.js" - output = js_api_obj_path +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" + output = js_uri_obj_path } -ohos_shared_library("api") { +ohos_shared_library("uri") { include_dirs = [ "//third_party/icu/icu4c/source/common", "//third_party/node/src", "//foundation/ace/napi/interfaces/kits", - "//base/compileruntime/js_api_module/api", + "//base/compileruntime/js_api_module/uri", ] sources = [ "js_uri.cpp", - "js_url.cpp", - "native_module_api.cpp", + "native_module_uri.cpp", ] deps = [ - ":js_api", - "//base/compileruntime/js_api_module/api/:js_api", + ":js_uri", + "//base/compileruntime/js_api_module/uri/:js_uri", "//foundation/ace/napi/:ace_napi", "//foundation/ace/napi/:ace_napi_quickjs", "//third_party/icu/icu4c:static_icuuc", @@ -55,6 +54,7 @@ ohos_shared_library("api") { relative_install_dir = "module" } -group("api_packages") { - deps = [ ":api" ] +group("uri_packages") { + deps = [ ":uri" ] } + diff --git a/api/js_uri.cpp b/uri/js_uri.cpp similarity index 95% rename from api/js_uri.cpp rename to uri/js_uri.cpp index 5a49c969..e8192f2d 100755 --- a/api/js_uri.cpp +++ b/uri/js_uri.cpp @@ -14,7 +14,7 @@ */ #include "js_uri.h" #include "utils/log.h" -namespace OHOS::Api { +namespace OHOS::Uri { std::bitset g_ruleAlpha; std::bitset g_ruleScheme; std::bitset g_ruleUrlc; @@ -86,9 +86,10 @@ namespace OHOS::Api { if ((pos != std::string::npos) && (pos != 0)) { AnalysisFragment(pos); if (!errStr_.empty()) { - return; + return; + } } - if ((pos != std::string::npos) && (pos == 0)) + if ((pos != std::string::npos) && (pos == 0)) { errStr_ = "#It can't be the first"; return; } @@ -289,10 +290,10 @@ namespace OHOS::Api { bool Uri::AnalysisPort(size_t pos) { std::string port = data_.substr(pos + 1); - if (!CheckCharacter(port, g_rulePort, true)) { // 存在非规则内字符 + if (!CheckCharacter(port, g_rulePort, true)) { errStr_ = "port does not conform to the rule"; return false; - } else if (CheckCharacter(port, g_ruleDigit, false)) { // 纯数字 + } else if (CheckCharacter(port, g_ruleDigit, false)) { uriData_.port = std::stoi(port); data_ = data_.substr(0, pos); return true; diff --git a/api/js_uri.h b/uri/js_uri.h similarity index 96% rename from api/js_uri.h rename to uri/js_uri.h index d2774f38..923be07f 100755 --- a/api/js_uri.h +++ b/uri/js_uri.h @@ -24,7 +24,7 @@ #include #include "napi/native_api.h" #include "napi/native_node_api.h" -namespace OHOS::Api { +namespace OHOS::Uri { constexpr int MAX_BIT_SIZE = 128; struct uri_data { int port = -1; diff --git a/uri/js_uri.js b/uri/js_uri.js new file mode 100755 index 00000000..746f2e7b --- /dev/null +++ b/uri/js_uri.js @@ -0,0 +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() { + 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) { + 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/native_module_uri.cpp b/uri/native_module_uri.cpp new file mode 100755 index 00000000..39f3f59b --- /dev/null +++ b/uri/native_module_uri.cpp @@ -0,0 +1,299 @@ + /* + * 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. + */ + +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "js_uri.h" +#include "utils/log.h" + +extern const char _binary_js_uri_js_start[]; +extern const char _binary_js_uri_js_end[]; +namespace OHOS::Uri { + napi_value g_uriClass = nullptr; + static napi_value UriConstructor(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + void *data = nullptr; + size_t argc = 1; + napi_value argv[1] = { 0 }; + Uri *object = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); + napi_valuetype valuetype; + NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); + if (valuetype == napi_string) { + char *type = nullptr; + size_t typelen = 0; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); + type = new char[typelen + 1]; + NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); + object = new Uri(env, type); + delete[] type; + } else { + napi_throw_error(env, nullptr, "parameter type is error"); + } + NAPI_CALL(env, napi_wrap(env, thisVar, object, + [](napi_env env, void *data, void *hint) { + auto object = (Uri*)data; + if (object != nullptr) { + delete object; + } + }, nullptr, nullptr)); + return thisVar; + } + + static napi_value Normalize(napi_env env, napi_callback_info info) + { + 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)); + std::string normalizeUri = muri->Normalize(); + size_t argc = 1; + napi_value args[1] = { 0 }; + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), normalizeUri.size(), args)); + NAPI_CALL(env, napi_new_instance(env, g_uriClass, argc, args, &result)); + return result; + } + + static napi_value Equals(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + napi_value result = nullptr; + size_t argc = 1; + napi_value argv[1] = { 0 }; + 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)); + Uri *other = nullptr; + NAPI_CALL(env, napi_unwrap(env, argv[0], (void**)&other)); + + bool flag = muri->Equals(*other); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + + static napi_value IsAbsolute(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + bool flag = muri->IsAbsolute(); + NAPI_CALL(env, napi_get_boolean(env, flag, &result)); + return result; + } + + static napi_value IsFailed(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->IsFailed(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value UriToString(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->ToString(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetScheme(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetScheme(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetAuthority(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetAuthority(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetSsp(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetSsp(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetUserinfo(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetUserinfo(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetHost(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetHost(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetPort(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetPort(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetPath(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetPath(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetQuery(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetQuery(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value GetFragment(napi_env env, napi_callback_info info) + { + napi_value thisVar = nullptr; + 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)); + std::string temp = muri->GetFragment(); + size_t templen = temp.size(); + NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); + return result; + } + + static napi_value UriInit(napi_env env, napi_value exports) + { + const char *uriClassName = "uri"; + napi_value uriClass = nullptr; + static napi_property_descriptor uriDesc[] = { + DECLARE_NAPI_FUNCTION("normalize", Normalize), + DECLARE_NAPI_FUNCTION("equals", Equals), + DECLARE_NAPI_FUNCTION("isAbsolute", IsAbsolute), + DECLARE_NAPI_FUNCTION("toString", UriToString), + DECLARE_NAPI_GETTER("scheme", GetScheme), + DECLARE_NAPI_GETTER("authority", GetAuthority), + DECLARE_NAPI_GETTER("ssp", GetSsp), + DECLARE_NAPI_GETTER("userinfo", GetUserinfo), + DECLARE_NAPI_GETTER("host", GetHost), + DECLARE_NAPI_GETTER("port", GetPort), + DECLARE_NAPI_GETTER("path", GetPath), + DECLARE_NAPI_GETTER("query", GetQuery), + DECLARE_NAPI_GETTER("fragment", GetFragment), + DECLARE_NAPI_GETTER("isFailed", IsFailed), + }; + NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor, + nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); + g_uriClass = uriClass; + static napi_property_descriptor desc[] = { + DECLARE_NAPI_PROPERTY("Uri", uriClass) + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); + return exports; + } + + extern "C" + __attribute__((visibility("default"))) void NAPI_uri_GetJSCode(const char **buf, int *bufLen) + { + if (buf != nullptr) { + *buf = _binary_js_uri_js_start; + } + + if (bufLen != nullptr) { + *bufLen = _binary_js_uri_js_end - _binary_js_uri_js_start; + } + } + + static napi_module UriModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = UriInit, + .nm_modname = "uri", + .nm_priv = ((void*)0), + .reserved = {0}, + }; + extern "C" __attribute__((constructor)) void RegisterModule() + { + napi_module_register(&UriModule); + } +} // namespace \ No newline at end of file diff --git a/url/BUILD.gn b/url/BUILD.gn new file mode 100755 index 00000000..5e8d16fa --- /dev/null +++ b/url/BUILD.gn @@ -0,0 +1,59 @@ +# 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("//build/ohos.gni") +import("//build/ohos/ace/ace.gni") + +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" + output = js_url_obj_path +} + +ohos_shared_library("url") { + include_dirs = [ + "//third_party/icu/icu4c/source/common", + "//third_party/node/src", + "//foundation/ace/napi/interfaces/kits", + "//base/compileruntime/js_api_module/url", + ] + + sources = [ + "js_url.cpp", + "native_module_url.cpp", + ] + + deps = [ + ":js_url", + "//base/compileruntime/js_api_module/url/:js_url", + "//foundation/ace/napi/:ace_napi", + "//foundation/ace/napi/:ace_napi_quickjs", + "//third_party/icu/icu4c:static_icuuc", + "//utils/native/base:utils", + ] + + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + subsystem_name = "ccruntime" + part_name = "jsapi_api" + + relative_install_dir = "module" +} + +group("url_packages") { + deps = [ ":url" ] +} diff --git a/api/js_url.cpp b/url/js_url.cpp similarity index 97% rename from api/js_url.cpp rename to url/js_url.cpp index 1175b0cf..8b80b8c9 100755 --- a/api/js_url.cpp +++ b/url/js_url.cpp @@ -18,7 +18,7 @@ #include #include "securec.h" #include "utils/log.h" -namespace OHOS::Api { +namespace OHOS::Url { static std::map g_head = { {"ftp:", 21}, {"file:", -1}, {"gopher:", 70}, {"http:", 80}, {"https:", 443}, {"ws:", 80}, {"wss:", 443} diff --git a/api/js_url.h b/url/js_url.h similarity index 97% rename from api/js_url.h rename to url/js_url.h index f306f3e8..ac55ff44 100755 --- a/api/js_url.h +++ b/url/js_url.h @@ -25,7 +25,7 @@ #include #include "napi/native_api.h" #include "napi/native_node_api.h" -namespace OHOS::Api { +namespace OHOS::Url { enum class BitsetStatusFlag { BIT0 = 0, // 0:Bit 0 Set to true,The URL analysis failed BIT1 = 1, // 1:Bit 1 Set to true,The protocol is the default protocol diff --git a/api/js_api.js b/url/js_url.js similarity index 82% rename from api/js_api.js rename to url/js_url.js index 4abd6fbe..7eef1b4c 100755 --- a/api/js_api.js +++ b/url/js_url.js @@ -14,86 +14,7 @@ */ 'use strict'; -const api = requireInternal("api"); - -class URI { - constructor(input) { - if (typeof input !== 'string' || input.length === 0) { - throw new Error("input type err"); - } - this.uricalss = new api.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() { - 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) { - 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); - } -} +const Url = requireInternal("url"); let seachParamsArr = []; class URLSearchParams { @@ -101,7 +22,7 @@ class URLSearchParams { constructor(input) { let out = []; out = parameterProcessing(input); - this.urlcalss = new api.URLSearchParams1(); + this.urlcalss = new Url.URLSearchParams1(); this.urlcalss.array = out; } append(params1, params2) { @@ -224,7 +145,7 @@ function initToStringSeachParams(input) { input = input.slice(1); } let strVal = decodeURI(input); - seachParamsArr = api.stringParmas(strVal); + seachParamsArr = Url.stringParmas(strVal); return seachParamsArr; } class URL { @@ -249,7 +170,7 @@ class URL { if (arguments.length === 1) { inputUrl = arguments[0]; if (typeof inputUrl === 'string' && inputUrl.length > 0) { - nativeUrl = new api.Url(inputUrl); + nativeUrl = new Url.Url(inputUrl); } else { console.log('Input parameter error'); } @@ -261,7 +182,7 @@ class URL { if (typeof inputUrl === 'string') { if (typeof inputBase === 'string') { if (inputBase.length > 0) { - nativeUrl = new api.Url(inputUrl, inputBase); + nativeUrl = new Url.Url(inputUrl, inputBase); } else { console.log('Input parameter error'); return; @@ -269,7 +190,7 @@ class URL { } if (typeof inputBase === 'object') { let nativeBase = inputBase.getInfo(); - nativeUrl = new api.Url(inputUrl, nativeBase); + nativeUrl = new Url.Url(inputUrl, nativeBase); } } } @@ -490,7 +411,6 @@ class URL { } export default { - URI: URI, URLSearchParams: URLSearchParams, URL: URL, } diff --git a/api/native_module_api.cpp b/url/native_module_url.cpp similarity index 74% rename from api/native_module_api.cpp rename to url/native_module_url.cpp index 1a2b1abb..2d61ebcd 100755 --- a/api/native_module_api.cpp +++ b/url/native_module_url.cpp @@ -15,233 +15,12 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" -#include "js_uri.h" #include "js_url.h" #include "utils/log.h" -extern const char _binary_js_api_js_start[]; -extern const char _binary_js_api_js_end[]; -namespace OHOS::Api { - napi_value g_uriClass = nullptr; - static napi_value UriConstructor(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - void *data = nullptr; - size_t argc = 1; - napi_value argv[1] = { 0 }; - Uri *object = nullptr; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, &data)); - napi_valuetype valuetype; - NAPI_CALL(env, napi_typeof(env, argv[0], &valuetype)); - if (valuetype == napi_string) { - char *type = nullptr; - size_t typelen = 0; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], nullptr, 0, &typelen)); - type = new char[typelen + 1]; - NAPI_CALL(env, napi_get_value_string_utf8(env, argv[0], type, typelen + 1, &typelen)); - object = new Uri(env, type); - delete[] type; - } else { - napi_throw_error(env, nullptr, "parameter type is error"); - } - NAPI_CALL(env, napi_wrap(env, thisVar, object, - [](napi_env env, void *data, void *hint) { - auto object = (Uri*)data; - if (object != nullptr) { - delete object; - } - }, nullptr, nullptr)); - return thisVar; - } - - static napi_value Normalize(napi_env env, napi_callback_info info) - { - 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)); - std::string normalizeUri = muri->Normalize(); - size_t argc = 1; - napi_value args[1] = { 0 }; - napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, normalizeUri.c_str(), normalizeUri.size(), args)); - NAPI_CALL(env, napi_new_instance(env, g_uriClass, argc, args, &result)); - return result; - } - - static napi_value Equals(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - napi_value result = nullptr; - size_t argc = 1; - napi_value argv[1] = { 0 }; - 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)); - Uri *other = nullptr; - NAPI_CALL(env, napi_unwrap(env, argv[0], (void**)&other)); - - bool flag = muri->Equals(*other); - NAPI_CALL(env, napi_get_boolean(env, flag, &result)); - return result; - } - - static napi_value IsAbsolute(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - bool flag = muri->IsAbsolute(); - NAPI_CALL(env, napi_get_boolean(env, flag, &result)); - return result; - } - - static napi_value IsFailed(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->IsFailed(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value UriToString(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->ToString(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetScheme(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetScheme(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetAuthority(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetAuthority(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetSsp(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetSsp(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetUserinfo(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetUserinfo(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetHost(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetHost(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetPort(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetPort(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetPath(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetPath(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetQuery(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetQuery(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - - static napi_value GetFragment(napi_env env, napi_callback_info info) - { - napi_value thisVar = nullptr; - 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)); - std::string temp = muri->GetFragment(); - size_t templen = temp.size(); - NAPI_CALL(env, napi_create_string_utf8(env, temp.c_str(), templen, &result)); - return result; - } - +extern const char _binary_js_url_js_start[]; +extern const char _binary_js_url_js_end[]; +namespace OHOS::Url { static void UrlStructor(napi_env &env, napi_callback_info &info, URL *&object) { napi_value thisVar = nullptr; @@ -1113,36 +892,6 @@ namespace OHOS::Api { return exports; } - static napi_value UriInit(napi_env env, napi_value exports) - { - const char *uriClassName = "uri"; - napi_value uriClass = nullptr; - static napi_property_descriptor uriDesc[] = { - DECLARE_NAPI_FUNCTION("normalize", Normalize), - DECLARE_NAPI_FUNCTION("equals", Equals), - DECLARE_NAPI_FUNCTION("isAbsolute", IsAbsolute), - DECLARE_NAPI_FUNCTION("toString", UriToString), - DECLARE_NAPI_GETTER("scheme", GetScheme), - DECLARE_NAPI_GETTER("authority", GetAuthority), - DECLARE_NAPI_GETTER("ssp", GetSsp), - DECLARE_NAPI_GETTER("userinfo", GetUserinfo), - DECLARE_NAPI_GETTER("host", GetHost), - DECLARE_NAPI_GETTER("port", GetPort), - DECLARE_NAPI_GETTER("path", GetPath), - DECLARE_NAPI_GETTER("query", GetQuery), - DECLARE_NAPI_GETTER("fragment", GetFragment), - DECLARE_NAPI_GETTER("isFailed", IsFailed), - }; - NAPI_CALL(env, napi_define_class(env, uriClassName, strlen(uriClassName), UriConstructor, - nullptr, sizeof(uriDesc) / sizeof(uriDesc[0]), uriDesc, &uriClass)); - g_uriClass = uriClass; - static napi_property_descriptor desc[] = { - DECLARE_NAPI_PROPERTY("Uri", uriClass) - }; - napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); - return exports; - } - static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { @@ -1151,33 +900,32 @@ namespace OHOS::Api { NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); SeachParamsInit(env, exports); UrlInit(env, exports); - UriInit(env, exports); return exports; } extern "C" - __attribute__((visibility("default"))) void NAPI_api_GetJSCode(const char **buf, int *bufLen) + __attribute__((visibility("default"))) void NAPI_url_GetJSCode(const char **buf, int *bufLen) { if (buf != nullptr) { - *buf = _binary_js_api_js_start; + *buf = _binary_js_url_js_start; } if (bufLen != nullptr) { - *bufLen = _binary_js_api_js_end - _binary_js_api_js_start; + *bufLen = _binary_js_url_js_end - _binary_js_url_js_start; } } - static napi_module ApiModule = { + static napi_module UrlModule = { .nm_version = 1, .nm_flags = 0, .nm_filename = nullptr, .nm_register_func = Init, - .nm_modname = "api", + .nm_modname = "url", .nm_priv = ((void*)0), .reserved = {0}, }; extern "C" __attribute__((constructor)) void RegisterModule() { - napi_module_register(&ApiModule); + napi_module_register(&UrlModule); } } // namespace \ No newline at end of file -- Gitee From 5551811042028705567c545b750534fec738ec1e Mon Sep 17 00:00:00 2001 From: lifansheng Date: Fri, 17 Sep 2021 15:31:21 +0800 Subject: [PATCH 4/4] modify codecheck Signed-off-by: lifansheng --- README.md | 202 +++++++++++++++++++---- README_zh.md | 209 +++++++++++++++++++----- convertxml/js_convertxml.cpp | 3 +- convertxml/native_module_convertxml.cpp | 3 - uri/js_uri.cpp | 12 +- 5 files changed, 345 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index 576e68b5..c73d3b9a 100755 --- a/README.md +++ b/README.md @@ -10,46 +10,69 @@ ## Introduction -The interface of URL is used to parse, construct, normalize, and encode URLs. The URL constructor creates a new URL object. In order to make changes to the resolved components of the URL or to the URL. The URLSearchParams interface defines some practical methods to process URL query strings. +The interface of URL is used to parse, construct, normalize, and encode URLs. The constructor of URL creates a new URL object. In order to make changes to the resolved components of the URL or to the URL. The URLSearchParams interface defines some practical methods to process URL query strings. + +URI Represents a Uniform Resource Identifier (URI) reference. + +XML representation refers to extensible markup language。 ## Contents ``` base/compileruntime/js_api_module/ -├── Class:URL # URL class -│ ├── new URL(input[, base]) # Create URL object -│ ├── hash # hash attribute -│ ├── host # host attribute -│ ├── hostname # hostname attribute -│ ├── href # href attribute -│ ├── origin # origin attribute -│ ├── password # password attribute -│ ├── pathname # pathname attribute -│ ├── port # port attribute -│ ├── protocol # protocol attribute -│ ├── search # search attribute -│ ├── searchParams # searchParams attribute -│ ├── username # username attribute -│ ├── toString() # toString method -│ └── toJSON() # toJSON method -└─── Class: URLSearchParams # URLSearchParams class - ├── new URLSearchParams() # Create URLSearchParams object - ├── new URLSearchParams(string) # Create URLSearchParams object - ├── new URLSearchParams(obj) # Create URLSearchParams object - ├── new URLSearchParams(iterable) # Create URLSearchParams object - ├── append(name, value) # append method - ├── delete(name) # delete method - ├── entries() # entries method - ├── forEach(fn[, thisArg]) # forEach method - ├── get(name) # get method - ├── getAll(name) # getAll method - ├── has(name) # has method - ├── keys() # keys method - ├── set(name, value) # set method - ├── sort() # sort method - ├── toString() # toString method - ├── values() # values method - └── urlSearchParams[Symbol.iterator]() # Create URLSearchParams object +├── Class:URL # URL class +│ ├── new URL(input[, base]) # Create URL object +│ ├── hash # hash attribute +│ ├── host # host attribute +│ ├── hostname # hostname attribute +│ ├── href # href attribute +│ ├── origin # origin attribute +│ ├── password # password attribute +│ ├── pathname # pathname attribute +│ ├── port # port attribute +│ ├── protocol # protocol attribute +│ ├── search # search attribute +│ ├── searchParams # searchParams attribute +│ ├── username # username attribute +│ ├── toString() # toString method +│ └── toJSON() # toJSON method +├── Class: URLSearchParams # URLSearchParams class +│ ├── new URLSearchParams() # Create URLSearchParams object +│ ├── new URLSearchParams(string) # Create URLSearchParams object +│ ├── new URLSearchParams(obj) # Create URLSearchParams object +│ ├── new URLSearchParams(iterable) # Create URLSearchParams object +│ ├── append(name, value) # append method +│ ├── delete(name) # delete method +│ ├── entries() # entries method +│ ├── forEach(fn[, thisArg]) # forEach method +│ ├── get(name) # get method +│ ├── getAll(name) # getAll method +│ ├── has(name) # has method +│ ├── keys() # keys method +│ ├── set(name, value) # set method +│ ├── sort() # sort method +│ ├── toString() # toString method +│ ├── values() # values method +│ └── urlSearchParams[Symbol.iterator]() # Create URLSearchParams object +├── Class:URI # URI class +│ ├── URI​(String str) # URI class +│ ├── scheme # Create URI object +│ ├── authority # scheme attribute +│ ├── ssp # authority attribute +│ ├── userinfo # ssp attribute +│ ├── host # userinfo attribute +│ ├── port # host attribute +│ ├── query # port attribute +│ ├── fragment # query attribute +│ ├── path # fragment attribute +│ ├── equals(Object ob) # path method +│ ├── normalize​() # equals method +│ ├── isAbsolute​() # normalize method +│ ├── normalize​() # isAbsolute method +│ └── toString() # normalize method +└── Class:ConvertXml # ConvertXml class + ├── ConvertXml() # Create convertxml class object + └── convert(String xml, Object options) # Convert method ``` ## Illustrate @@ -59,6 +82,7 @@ base/compileruntime/js_api_module/ | Interface name | Illustrate | | -------- | -------- | +//URL | new URL(url: string,base?:string I URL) | Create and return a URL object that references the URL specified by the absolute URL string, the relative URL string, and the basic URL string. | | tostring():string | The stringification method returns a USVString containing the complete URL. It is equivalent to the read-only URL.href. | | toJSON():string | This method returns a USVString, which contains a serialized URL version. | @@ -79,11 +103,31 @@ base/compileruntime/js_api_module/ | entries(): iterableIterator<[string, string]> | Returns an iterator that allows iterating through all key/value pairs contained in the searchParams object. | | forEach(): void | Through the callback function to traverse the key-value pairs on the URLSearchParams instance object. | | urlSearchParams[Symbol.iterator] () | Returns an ES6 iterator for each name-value pair in the query string. Each item of the iterator is a JavaScript array. | +//URI +| URI​(String str) | Construct the URI by parsing the given input parameter (String str). This constructor parses the given string strictly in accordance with the grammatical provisions in RFC 2396 Appendix A. | +| getScheme​() | Return the scheme component of this URI, or null if the scheme is not defined | +| getAuthority​() | Returns the decoded authority component of this URI, or null if authority is not defined. The string returned by this method is the same as the string returned by the getRawAuthority method, except that all escaped octet sequences are decoded. | +| getSchemeSpecificPart​() | Returns the decoding scheme-specific part of this URI. The string returned by this method is the same as the string returned by the getRawSchemeSpecificPart method, except that all escaped octet sequences are decoded. | +| getUserInfo​() | Returns the decoded userinfo component of this URI. The userinfo component of the URI (if defined) only contains characters in unreserved, punctuation, escape, and other categories. | +| getHost​() | Return the host component of this URI, or null if host is not defined. | +| getPort​() | Return the port of this URI, or -1 if the port is not defined. The port component of the URI (if defined) is a non-negative integer. | +| getQuery​() | Returns the decoded query component of this URI, or null if the query is not defined. The string returned by this method is the same as the string returned by the getRawQuery method, except that all escaped octet sequences are decoded. | +| getFragment​() | Returns the decoded fragment component of this URI, or null if the fragment is not defined. The string returned by this method is the same as the string returned by the getRawFragment method, except that all escaped octet sequences are decoded. | +| getPath​() | Returns the decoded path component of this URI, or null if path is not defined. The string returned by this method is the same as the string returned by the getRawPath method, except that all escaped octet sequences are decoded. | +| equals(Object ob) | Test whether this URI is equal to another object. If the given object is not a URI, this method immediately returns false. | +| normalize​() | Normalize the path of this URI. If this URI is opaque, or its path is already in normal form, then this URI is returned. Otherwise, a new URI identical to this URI will be constructed. | +| isAbsolute​() | Determine whether this URI is absolute. If and only if it has a scheme component, the URI is absolute and the return value is true, otherwise the return value is false. | +| toString() | Return the content of this URI as a string. | +//ConvertXml +| ConvertXml() | The constructor used to construct the convertxml class object. This constructor does not need to pass in parameters. | +| convert(String xml, Object options) | Returns a JavaScript object that converts an XML string as required by the option. | ### Instructions for use The usage of each interface is as follows: +1.URL + 1、new URL(url: string,base?:string|URL) ``` let b = new URL('https://developer.mozilla.org'); // => 'https://developer.mozilla.org/' @@ -207,6 +251,94 @@ for (const [name, value] of params) { // foo bar // xyz ba ``` + +2.URI + +1、URI​(String str) +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +``` +2、scheme +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.scheme // => "http"; +``` +3、authority +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.authority // => "gg:gaogao@www.baidu.com:99"; +``` +4、ssp +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.ssp " // => gg:gaogao@www.baidu.com:99/path/path?query"; +``` +5、userinfo +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.userinfo // => "gg:gaogao"; +``` +6、host +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.host // => "www.baidu.com"; +``` +7、port +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.port // => "99"; +``` +8、query +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.query // => "query"; +``` +9、fragment +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.fragment // => "fagment"; +``` +10、path +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.path // => "/path/path"; +``` +11、equals(Object ob) +``` +let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment'); +let gaogao1 = gaogao; +let res = gaogao.equals(gaogao1); +console.log(res); // => true; +``` +12、normalize​() +``` +let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path/66./../././mm/.././path1?query#fagment'); +let res = gaogao.normalize(); +console.log(res.path); // => "/path/path1" +console.log(res.toString()); // => "http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path/path1?query#fagment" +``` +13、isAbsolute​() +``` +let gaogao = new Uri.URI('f/tp://username:password@www.baidu.com:88/path?query#fagment'); +let res = gaogao.isAbsolute(); +console.log(res); //=> false; +``` +14、toString() +``` +let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment'); +let res = gaogao.toString(); +console.log(res.toString()); // => 'http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment'; +``` + +3.ConvertXml + +1、ConvertXml() +``` +var convertml = new convertXml.ConvertXml(); +``` +2、convert(String xml, Object options) +``` +var result = convertml.convert(xml, {compact: false, spaces: 4}); ## Related warehouse [js_api_module Subsystem](https://gitee.com/OHOS_STD/js_api_module) diff --git a/README_zh.md b/README_zh.md index 6eccad8e..21b80b58 100755 --- a/README_zh.md +++ b/README_zh.md @@ -12,44 +12,67 @@ URL接口用于解析,构造,规范化和编码 URLs。 URL的构造函数创建新的URL对象。 以便对URL的已解析组成部分或对URL进行更改。URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串。 +URI表示统一资源标识符引用。 + +xml表示指可扩展标记语言。 + ## 目录 ``` base/compileruntime/js_api_module/ -├── Class:URL # URL类 -│ ├── new URL(input[, base]) # 创建URL对象 -│ ├── hash # hash属性 -│ ├── host # host属性 -│ ├── hostname # hostname属性 -│ ├── href # href属性 -│ ├── origin # origin属性 -│ ├── password # password属性 -│ ├── pathname # pathname属性 -│ ├── port # port属性 -│ ├── protocol # protocol属性 -│ ├── search # search属性 -│ ├── searchParams # searchParams属性 -│ ├── username # username属性 -│ ├── toString() # toString方法 -│ └── toJSON() # toJSON方法 -└─── Class: URLSearchParams # URLSearchParams类 - ├── new URLSearchParams() # 创建URLSearchParams对象 - ├── new URLSearchParams(string) # 创建URLSearchParams对象 - ├── new URLSearchParams(obj) # 创建URLSearchParams对象 - ├── new URLSearchParams(iterable) # 创建URLSearchParams对象 - ├── append(name, value) # append方法 - ├── delete(name) # delete方法 - ├── entries() # entries方法 - ├── forEach(fn[, thisArg]) # forEach方法 - ├── get(name) # get方法 - ├── getAll(name) # getAll方法 - ├── has(name) # has方法 - ├── keys() # keys方法 - ├── set(name, value) # set方法 - ├── sort() # sort方法 - ├── toString() # toString方法 - ├── values() # values方法 - └── urlSearchParams[Symbol.iterator]() # 创建URLSearchParams对象 +├── Class:URL # URL类 +│ ├── new URL(input[, base]) # 创建URL对象 +│ ├── hash # hash属性 +│ ├── host # host属性 +│ ├── hostname # hostname属性 +│ ├── href # href属性 +│ ├── origin # origin属性 +│ ├── password # password属性 +│ ├── pathname # pathname属性 +│ ├── port # port属性 +│ ├── protocol # protocol属性 +│ ├── search # search属性 +│ ├── searchParams # searchParams属性 +│ ├── username # username属性 +│ ├── toString() # toString方法 +│ └── toJSON() # toJSON方法 +├── Class: URLSearchParams # URLSearchParams类 +│ ├── new URLSearchParams() # 创建URLSearchParams对象 +│ ├── new URLSearchParams(string) # 创建URLSearchParams对象 +│ ├── new URLSearchParams(obj) # 创建URLSearchParams对象 +│ ├── new URLSearchParams(iterable) # 创建URLSearchParams对象 +│ ├── append(name, value) # append方法 +│ ├── delete(name) # delete方法 +│ ├── entries() # entries方法 +│ ├── forEach(fn[, thisArg]) # forEach方法 +│ ├── get(name) # get方法 +│ ├── getAll(name) # getAll方法 +│ ├── has(name) # has方法 +│ ├── keys() # keys方法 +│ ├── set(name, value) # set方法 +│ ├── sort() # sort方法 +│ ├── toString() # toString方法 +│ ├── values() # values方法 +│ └── urlSearchParams[Symbol.iterator]() # 创建URLSearchParams对象 +├── Class:URI # URI类 +│ ├── URI​(String str) # 创建URI对象 +│ ├── scheme # scheme属性 +│ ├── authority # authority属性 +│ ├── ssp # ssp属性 +│ ├── userinfo # userinfo属性 +│ ├── host # host属性 +│ ├── port # port属性 +│ ├── query # query属性 +│ ├── fragment # fragment属性 +│ ├── path # path属性 +│ ├── equals(Object ob) # equals方法 +│ ├── normalize​() # normalize方法 +│ ├── isAbsolute​() # isAbsolute方法 +│ ├── normalize​() # normalize方法 +│ └── toString() # toString方法 +└── Class:ConvertXml # ConvertXml类 + ├── ConvertXml() # 创建ConvertXml类对象 + └── convert(String xml, Object options) # convert方法 ``` ## 说明 @@ -58,8 +81,9 @@ base/compileruntime/js_api_module/ | 接口名 | 说明 | -| -------- | -------- | -| new URL(url: string,base?:string I URL) | 创建并返回一个URL对象,该URL对象引用使用绝对URL字符串,相对URL字符串和基本URL字符串指定的URL。 | +| -------- | -------- | +//URL +| URL(url: string,base?:string I URL) | 创建并返回一个URL对象,该URL对象引用使用绝对URL字符串,相对URL字符串和基本URL字符串指定的URL。 | | tostring():string | 该字符串化方法返回一个包含完整 URL 的 USVString。它的作用等同于只读的 URL.href。 | | toJSON():string | 该方法返回一个USVString,其中包含一个序列化的URL版本。 | | new URLSearchParams() | URLSearchParams() 构造器无入参,该方法创建并返回一个新的URLSearchParams 对象。 开头的'?' 字符会被忽略。 | @@ -80,10 +104,31 @@ base/compileruntime/js_api_module/ | forEach(): void | 通过回调函数来遍历URLSearchParams实例对象上的键值对。 | | urlSearchParams[Symbol.iterator] () | 返回查询字符串中每个名称-值对的ES6迭代器。迭代器的每个项都是一个JavaScript数组。 | +//URI +| URI​(String str) | 通过解析给定入参(String str)来构造URI。此构造函数严格按照RFC 2396附录A中的语法规定解析给定字符串。 | +| scheme​ | 返回此 URI 的scheme部分,如果scheme未定义,则返回 null | +| authority​ | 返回此 URI 的解码authority部分,如果authority未定义,则返回 null。 | +| ssp​ | 返回此 URI 的解码scheme-specific部分。 | +| userinfo​ | 返回此 URI 的解码userinfo部分。包含passworld和username。 | +| host​ | 返回此 URI 的host部分,如果host未定义,则返回 null。 | +| port​ | 返回此 URI 的port部分,如果port未定义,则返回 -1。URI 的port组件(如果已定义)是一个非负整数。 | +| query​ | 返回此 URI 的query部分,如果query未定义,则返回 null。 | +| fragment​ | 返回此 URI 的解码fragment组件,如果fragment未定义,则返回 null。 +| path​ | 返回此 URI 的解码path组件,如果path未定义,则返回 null。 | +| equals(Object ob) | 测试此 URI 是否与另一个对象相等。如果给定的对象不是 URI,则此方法立即返回 false。 | +| normalize​() | 规范化这个 URI 的路径。如果这个 URI 的path不规范,将规范后构造一个新 URI对象返回。 | +| isAbsolute​() | 判断这个 URI 是否是绝对的。当且仅当它具有scheme部分时,URI 是绝对的,返回值为true,否则返回值为false。 | + +//ConvertXml +| ConvertXml() | 用于构造ConvertXml类对象的构造函数。此构造函数无需传入参数。 | +| convert(String xml, Object options) | 返回按选项要求转化xml字符串的JavaScrip对象。 | + ### 使用说明 各接口使用方法如下: +1.URL + 1、new URL(url: string,base?:string|URL) ``` let b = new URL('https://developer.mozilla.org'); // => 'https://developer.mozilla.org/' @@ -207,7 +252,97 @@ for (const [name, value] of params) { // foo bar // xyz ba ``` + +2.URI + +1、URI​(String str) +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +``` +2、scheme +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.scheme // => "http"; +``` +3、authority +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.authority // => "gg:gaogao@www.baidu.com:99"; +``` +4、ssp +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.ssp " // => gg:gaogao@www.baidu.com:99/path/path?query"; +``` +5、userinfo +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.userinfo // => "gg:gaogao"; +``` +6、host +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.host // => "www.baidu.com"; +``` +7、port +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.port // => "99"; +``` +8、query +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.query // => "query"; +``` +9、fragment +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.fragment // => "fagment"; +``` +10、path +``` +let gaogao = new Uri.URI('http://gg:gaogao@www.baidu.com:99/path/path?query#fagment'); +gaogao.path // => "/path/path"; +``` +11、equals(Object ob) +``` +let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path1?query#fagment'); +let gaogao1 = gaogao; +let res = gaogao.equals(gaogao1); +console.log(res); // => true; +``` +12、normalize​() +``` +let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path/66./../././mm/.././path1?query#fagment'); +let res = gaogao.normalize(); +console.log(res.path); // => "/path/path1" +console.log(res.toString()); // => "http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/path/path1?query#fagment" +``` +13、isAbsolute​() +``` +let gaogao = new Uri.URI('f/tp://username:password@www.baidu.com:88/path?query#fagment'); +let res = gaogao.isAbsolute(); +console.log(res); //=> false; +``` +14、toString() +``` +let gaogao = new Uri.URI('http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment'); +let res = gaogao.toString(); +console.log(res.toString()); // => 'http://gg:gaogao@[1:0:0:1:2:1:2:1]:99/../../path/.././../aa/bb/cc?query#fagment'; +``` + +3.ConvertXml + +1、ConvertXml() +``` +var convertml = new convertXml.ConvertXml(); +``` +2、convert(String xml, Object options) +``` +var result = convertml.convert(xml, {compact: false, spaces: 4}); +``` + ## 相关仓 [js_api_module子系统](https://gitee.com/OHOS_STD/js_api_module) -[base/compileruntime/js_api_module/](base/compileruntime/js_api_module-readme.md) +[base/compileruntime/js_api_module/](base/compileruntime/js_api_module/readme.md) diff --git a/convertxml/js_convertxml.cpp b/convertxml/js_convertxml.cpp index d0b5e101..727565fb 100755 --- a/convertxml/js_convertxml.cpp +++ b/convertxml/js_convertxml.cpp @@ -202,7 +202,6 @@ void ConvertXml::SetPrevInfo(napi_value &recvElement, int flag, int32_t &index1) napi_set_element(env_, recvElement, index1++, m_prevObj[i]); } } - } void ConvertXml::GetXMLInfo(xmlNodePtr curNode, napi_value &object, int flag) @@ -307,7 +306,7 @@ napi_status ConvertXml::DealNapiStrValue(napi_value napi_StrValue, std::string & char *buffer = nullptr; size_t bufferSize = 0; napi_status status = napi_ok; - status = napi_get_value_string_utf8(env_, napi_StrValue, buffer, -1, &bufferSize); + status = napi_get_value_string_utf8(env_, napi_StrValue, nullptr, -1, &bufferSize); if (status != napi_ok) { return status; } diff --git a/convertxml/native_module_convertxml.cpp b/convertxml/native_module_convertxml.cpp index 730a9255..25223d8a 100755 --- a/convertxml/native_module_convertxml.cpp +++ b/convertxml/native_module_convertxml.cpp @@ -63,14 +63,12 @@ static napi_value Convert(napi_env env, napi_callback_info info) object->DealOptions(args[1]); } napi_value result = object->convert(strXml); - HILOG_INFO("LHC....Convert start011"); return result; } static napi_value ConvertXmlInit(napi_env env, napi_value exports) { - HILOG_INFO("LHC....ConvertXmlInit start02"); const char *ConvertXmlClassName = "ConvertXml"; napi_value ConvertXmlClass = nullptr; static napi_property_descriptor ConvertXmlDesc[] = { @@ -83,7 +81,6 @@ static napi_value ConvertXmlInit(napi_env env, napi_value exports) DECLARE_NAPI_PROPERTY("ConvertXml", ConvertXmlClass) }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - HILOG_INFO("LHC....ConvertXmlInit end02"); return exports; } diff --git a/uri/js_uri.cpp b/uri/js_uri.cpp index e8192f2d..b6e7de17 100755 --- a/uri/js_uri.cpp +++ b/uri/js_uri.cpp @@ -83,16 +83,12 @@ namespace OHOS::Uri { { data_ = inputUri_; size_t pos = data_.find('#'); // Fragment - if ((pos != std::string::npos) && (pos != 0)) { + if (pos != std::string::npos) { AnalysisFragment(pos); if (!errStr_.empty()) { return; } } - if ((pos != std::string::npos) && (pos == 0)) { - errStr_ = "#It can't be the first"; - return; - } pos = data_.find('?'); // Query if (pos != std::string::npos) { AnalysisQuery(pos); @@ -126,12 +122,10 @@ namespace OHOS::Uri { uriData_.path = data_; uriData_.SchemeSpecificPart = data_ + uriData_.query; data_ = ""; - return; } else if (!data_.empty()) { uriData_.SchemeSpecificPart = data_ + uriData_.query; uriData_.query = ""; data_ = ""; - return; } } @@ -163,6 +157,10 @@ namespace OHOS::Uri { void Uri::AnalysisFragment(size_t pos) { + if (pos == 0) { + errStr_ = "#It can't be the first"; + return; + } std::string fragment = data_.substr(pos + 1); if (!CheckCharacter(fragment, g_ruleUrlc, true)) { errStr_ = "Fragment does not conform to the rule"; -- Gitee