114cf0368Sopenharmony_ci/*
214cf0368Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
314cf0368Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
414cf0368Sopenharmony_ci * you may not use this file except in compliance with the License.
514cf0368Sopenharmony_ci * You may obtain a copy of the License at
614cf0368Sopenharmony_ci *
714cf0368Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
814cf0368Sopenharmony_ci *
914cf0368Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1014cf0368Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1114cf0368Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1214cf0368Sopenharmony_ci * See the License for the specific language governing permissions and
1314cf0368Sopenharmony_ci * limitations under the License.
1414cf0368Sopenharmony_ci */
1514cf0368Sopenharmony_ci#define LOG_TAG "HtmlNapi"
1614cf0368Sopenharmony_ci#include "html_napi.h"
1714cf0368Sopenharmony_ci
1814cf0368Sopenharmony_ci#include "html.h"
1914cf0368Sopenharmony_ci#include "text_napi.h"
2014cf0368Sopenharmony_ci#include "unified_record_napi.h"
2114cf0368Sopenharmony_ci
2214cf0368Sopenharmony_cinamespace OHOS {
2314cf0368Sopenharmony_cinamespace UDMF {
2414cf0368Sopenharmony_cinapi_value HtmlNapi::Constructor(napi_env env)
2514cf0368Sopenharmony_ci{
2614cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
2714cf0368Sopenharmony_ci    napi_property_descriptor properties[] = {
2814cf0368Sopenharmony_ci        /* Html extends UnifiedRecord */
2914cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getType", UnifiedRecordNapi::GetType),
3014cf0368Sopenharmony_ci        DECLARE_NAPI_FUNCTION("getValue", UnifiedRecordNapi::GetValue),
3114cf0368Sopenharmony_ci        /* Html extends Text */
3214cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("details", TextNapi::GetDetails, TextNapi::SetDetails),
3314cf0368Sopenharmony_ci        /* Html properties */
3414cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("htmlContent", GetHtmlContent, SetHtmlContent),
3514cf0368Sopenharmony_ci        DECLARE_NAPI_GETTER_SETTER("plainContent", GetPlainContent, SetPlainContent),
3614cf0368Sopenharmony_ci    };
3714cf0368Sopenharmony_ci    size_t count = sizeof(properties) / sizeof(properties[0]);
3814cf0368Sopenharmony_ci    return NapiDataUtils::DefineClass(env, "HTML", properties, count, HtmlNapi::New);
3914cf0368Sopenharmony_ci}
4014cf0368Sopenharmony_ci
4114cf0368Sopenharmony_cinapi_value HtmlNapi::New(napi_env env, napi_callback_info info)
4214cf0368Sopenharmony_ci{
4314cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
4414cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
4514cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
4614cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
4714cf0368Sopenharmony_ci
4814cf0368Sopenharmony_ci    auto *html = new (std::nothrow) HtmlNapi();
4914cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, html != nullptr, Status::E_ERROR, "no memory for html!");
5014cf0368Sopenharmony_ci    html->value_ = std::make_shared<Html>();
5114cf0368Sopenharmony_ci    ASSERT_CALL(env, napi_wrap(env, ctxt->self, html, Destructor, nullptr, nullptr), html);
5214cf0368Sopenharmony_ci    return ctxt->self;
5314cf0368Sopenharmony_ci}
5414cf0368Sopenharmony_ci
5514cf0368Sopenharmony_civoid HtmlNapi::NewInstance(napi_env env, std::shared_ptr<UnifiedRecord> in, napi_value &out)
5614cf0368Sopenharmony_ci{
5714cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
5814cf0368Sopenharmony_ci    ASSERT_CALL_VOID(env, napi_new_instance(env, Constructor(env), 0, nullptr, &out));
5914cf0368Sopenharmony_ci    auto *html = new (std::nothrow) HtmlNapi();
6014cf0368Sopenharmony_ci    ASSERT_ERR_VOID(env, html != nullptr, Status::E_ERROR, "no memory for html!");
6114cf0368Sopenharmony_ci    html->value_ = std::static_pointer_cast<Html>(in);
6214cf0368Sopenharmony_ci    ASSERT_CALL_DELETE(env, napi_wrap(env, out, html, Destructor, nullptr, nullptr), html);
6314cf0368Sopenharmony_ci}
6414cf0368Sopenharmony_ci
6514cf0368Sopenharmony_civoid HtmlNapi::Destructor(napi_env env, void *data, void *hint)
6614cf0368Sopenharmony_ci{
6714cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi finalize.");
6814cf0368Sopenharmony_ci    auto *html = static_cast<HtmlNapi *>(data);
6914cf0368Sopenharmony_ci    ASSERT_VOID(html != nullptr, "finalize null!");
7014cf0368Sopenharmony_ci    delete html;
7114cf0368Sopenharmony_ci}
7214cf0368Sopenharmony_ci
7314cf0368Sopenharmony_ciHtmlNapi *HtmlNapi::GetHtml(napi_env env, napi_callback_info info, std::shared_ptr<ContextBase> ctxt)
7414cf0368Sopenharmony_ci{
7514cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
7614cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info);
7714cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
7814cf0368Sopenharmony_ci    return static_cast<HtmlNapi *>(ctxt->native);
7914cf0368Sopenharmony_ci}
8014cf0368Sopenharmony_ci
8114cf0368Sopenharmony_cinapi_value HtmlNapi::GetPlainContent(napi_env env, napi_callback_info info)
8214cf0368Sopenharmony_ci{
8314cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
8414cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
8514cf0368Sopenharmony_ci    auto html = GetHtml(env, info, ctxt);
8614cf0368Sopenharmony_ci    ASSERT_ERR(
8714cf0368Sopenharmony_ci        ctxt->env, (html != nullptr && html->value_ != nullptr), Status::E_ERROR, "invalid object!");
8814cf0368Sopenharmony_ci    ctxt->status = NapiDataUtils::SetValue(env, html->value_->GetPlainContent(), ctxt->output);
8914cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set plain content failed!");
9014cf0368Sopenharmony_ci    return ctxt->output;
9114cf0368Sopenharmony_ci}
9214cf0368Sopenharmony_ci
9314cf0368Sopenharmony_cinapi_value HtmlNapi::SetPlainContent(napi_env env, napi_callback_info info)
9414cf0368Sopenharmony_ci{
9514cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
9614cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
9714cf0368Sopenharmony_ci    std::string plainContent;
9814cf0368Sopenharmony_ci    auto input = [env, ctxt, &plainContent](size_t argc, napi_value *argv) {
9914cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
10014cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
10114cf0368Sopenharmony_ci        ctxt->status = NapiDataUtils::GetValue(env, argv[0], plainContent);
10214cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
10314cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter plainContent type must be string");
10414cf0368Sopenharmony_ci    };
10514cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
10614cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
10714cf0368Sopenharmony_ci    auto html = static_cast<HtmlNapi *>(ctxt->native);
10814cf0368Sopenharmony_ci    ASSERT_ERR(
10914cf0368Sopenharmony_ci        ctxt->env, (html != nullptr && html->value_ != nullptr), Status::E_ERROR, "invalid object!");
11014cf0368Sopenharmony_ci    html->value_->SetPlainContent(plainContent);
11114cf0368Sopenharmony_ci    return nullptr;
11214cf0368Sopenharmony_ci}
11314cf0368Sopenharmony_ci
11414cf0368Sopenharmony_cinapi_value HtmlNapi::GetHtmlContent(napi_env env, napi_callback_info info)
11514cf0368Sopenharmony_ci{
11614cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
11714cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
11814cf0368Sopenharmony_ci    auto html = GetHtml(env, info, ctxt);
11914cf0368Sopenharmony_ci    ASSERT_ERR(
12014cf0368Sopenharmony_ci        ctxt->env, (html != nullptr && html->value_ != nullptr), Status::E_ERROR, "invalid object!");
12114cf0368Sopenharmony_ci    ctxt->status = NapiDataUtils::SetValue(env, html->value_->GetHtmlContent(), ctxt->output);
12214cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, "set html failed!");
12314cf0368Sopenharmony_ci    return ctxt->output;
12414cf0368Sopenharmony_ci}
12514cf0368Sopenharmony_ci
12614cf0368Sopenharmony_cinapi_value HtmlNapi::SetHtmlContent(napi_env env, napi_callback_info info)
12714cf0368Sopenharmony_ci{
12814cf0368Sopenharmony_ci    LOG_DEBUG(UDMF_KITS_NAPI, "HtmlNapi");
12914cf0368Sopenharmony_ci    auto ctxt = std::make_shared<ContextBase>();
13014cf0368Sopenharmony_ci    std::string htmlContent;
13114cf0368Sopenharmony_ci    auto input = [env, ctxt, &htmlContent](size_t argc, napi_value *argv) {
13214cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, argc >= 1,
13314cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: Mandatory parameters are left unspecified");
13414cf0368Sopenharmony_ci        ctxt->status = NapiDataUtils::GetValue(env, argv[0], htmlContent);
13514cf0368Sopenharmony_ci        ASSERT_BUSINESS_ERR(ctxt, ctxt->status == napi_ok,
13614cf0368Sopenharmony_ci            Status::E_INVALID_PARAMETERS, "Parameter error: parameter htmlContent type must be string");
13714cf0368Sopenharmony_ci    };
13814cf0368Sopenharmony_ci    ctxt->GetCbInfoSync(env, info, input);
13914cf0368Sopenharmony_ci    ASSERT_ERR(ctxt->env, ctxt->status == napi_ok, Status::E_ERROR, ctxt->error);
14014cf0368Sopenharmony_ci    auto html = static_cast<HtmlNapi *>(ctxt->native);
14114cf0368Sopenharmony_ci    ASSERT_ERR(
14214cf0368Sopenharmony_ci        ctxt->env, (html != nullptr && html->value_ != nullptr), Status::E_ERROR, "invalid object!");
14314cf0368Sopenharmony_ci    html->value_->SetHtmlContent(htmlContent);
14414cf0368Sopenharmony_ci    return nullptr;
14514cf0368Sopenharmony_ci}
14614cf0368Sopenharmony_ci} // namespace UDMF
14714cf0368Sopenharmony_ci} // namespace OHOS