From 8eb1a9dc729cbd4e3cd78ef860105513130f7d04 Mon Sep 17 00:00:00 2001 From: pavelyevivan Date: Thu, 29 May 2025 15:52:45 +0300 Subject: [PATCH] Creating a StyledString using the CustomSpan Type Signed-off-by: pavelyevivan Change-Id: I8184e18cdb6e30f1b387b12a5ae3d1e57699276b --- .../native/implementation/custom_span_peer.h | 30 ++++++++++++++++ .../implementation/styled_string_accessor.cpp | 7 ++-- .../accessors/styled_string_accessor_test.cpp | 35 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 frameworks/core/interfaces/native/implementation/custom_span_peer.h diff --git a/frameworks/core/interfaces/native/implementation/custom_span_peer.h b/frameworks/core/interfaces/native/implementation/custom_span_peer.h new file mode 100644 index 00000000000..a935913e6e8 --- /dev/null +++ b/frameworks/core/interfaces/native/implementation/custom_span_peer.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 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. +*/ + +#pragma once + + +#include "core/event/key_event.h" + +#include "core/components_ng/pattern/text/span/span_object.h" + +struct CustomSpanPeer final { + OHOS::Ace::RefPtr span; + +protected: + explicit CustomSpanPeer(OHOS::Ace::RefPtr customSpan) : span(customSpan) {} + ~CustomSpanPeer() = default; + friend OHOS::Ace::NG::PeerUtils; +}; \ No newline at end of file diff --git a/frameworks/core/interfaces/native/implementation/styled_string_accessor.cpp b/frameworks/core/interfaces/native/implementation/styled_string_accessor.cpp index 32507a2e8c4..dcc6f40cd03 100644 --- a/frameworks/core/interfaces/native/implementation/styled_string_accessor.cpp +++ b/frameworks/core/interfaces/native/implementation/styled_string_accessor.cpp @@ -22,6 +22,7 @@ #include "core/interfaces/native/utility/reverse_converter.h" #include "core/interfaces/native/implementation/background_color_style_peer.h" #include "core/interfaces/native/implementation/baseline_offset_style_peer.h" +#include "core/interfaces/native/implementation/custom_span_peer.h" #include "core/interfaces/native/implementation/decoration_style_peer.h" #include "core/interfaces/native/implementation/gesture_style_peer.h" #include "core/interfaces/native/implementation/image_attachment_peer.h" @@ -163,8 +164,10 @@ Ark_StyledString CtorImpl(const Ark_Union_String_ImageAttachment_CustomSpan* val auto options = peerImageAttachment->span->GetImageSpanOptions(); peer->spanString = AceType::MakeRefPtr(options); }, - [](const Ark_CustomSpan& arkCustomSpan) { - LOGE("StyledStringAccessor::CtorImpl unsupported Ark_CustomSpan"); + [&peer](const Ark_CustomSpan& arkCustomSpan) { + CustomSpanPeer* peerCustomSpan = arkCustomSpan; + CHECK_NULL_VOID(peerCustomSpan && peerCustomSpan->span); + peer->spanString = AceType::MakeRefPtr(peerCustomSpan->span); }, []() {} ); diff --git a/test/unittest/capi/accessors/styled_string_accessor_test.cpp b/test/unittest/capi/accessors/styled_string_accessor_test.cpp index ae62f00eb80..ba777dc56cd 100644 --- a/test/unittest/capi/accessors/styled_string_accessor_test.cpp +++ b/test/unittest/capi/accessors/styled_string_accessor_test.cpp @@ -20,6 +20,7 @@ #include "core/interfaces/native/utility/reverse_converter.h" #include "core/interfaces/native/implementation/background_color_style_peer.h" #include "core/interfaces/native/implementation/baseline_offset_style_peer.h" +#include "core/interfaces/native/implementation/custom_span_peer.h" #include "core/interfaces/native/implementation/decoration_style_peer.h" #include "core/interfaces/native/implementation/gesture_style_peer.h" #include "core/interfaces/native/implementation/length_metrics_peer.h" @@ -424,6 +425,20 @@ private: ImageAttachmentPeer* peer = nullptr; }; +struct StyledStringUnionCustomSpan { + Ark_Union_String_ImageAttachment_CustomSpan* Union() + { + peer = PeerUtils::CreatePeer(AceType::MakeRefPtr()); + static Ark_Union_String_ImageAttachment_CustomSpan value = Converter::ArkUnion< + Ark_Union_String_ImageAttachment_CustomSpan, Ark_CustomSpan>(peer); + return &value; + } + Opt_Array_StyleOptions* Styles() { return nullptr; } + void TearDown() {} +private: + CustomSpanPeer* peer = nullptr; +}; + template class StyledStringAccessorTest : public AccessorTestCtorBase { @@ -487,6 +502,7 @@ private: using StyledStringAccessorUnionNullTest = StyledStringAccessorTest; using StyledStringAccessorUnionStringTest = StyledStringAccessorTest; using StyledStringAccessorUnionImageAttachmentTest = StyledStringAccessorTest; +using StyledStringAccessorUnionCustomSpanTest = StyledStringAccessorTest; /** * @tc.name: peerSucceeded @@ -1115,4 +1131,23 @@ HWTEST_F(StyledStringAccessorUnionImageAttachmentTest, ctorImageAttachmentTest, auto paddingStr = imageAttribute.value().paddingProp.value().ToString(); EXPECT_EQ(paddingStr, TEST_LENGTHMETRICS_STR); } + +/** + * @tc.name: ctorCustomSpanTest + * @tc.desc: CustomSpan check + * @tc.type: FUNC + */ +HWTEST_F(StyledStringAccessorUnionCustomSpanTest, ctorCustomSpanTest, TestSize.Level1) +{ + ASSERT_NE(peer_->spanString, nullptr); + auto spans = peer_->spanString->GetSpans(0, 1); + ASSERT_EQ(spans.size(), 1); + auto customSpan = AceType::DynamicCast(spans[0]); + ASSERT_NE(customSpan, nullptr); + EXPECT_EQ(customSpan->ToString(), "CustomSpan [0:1]"); + EXPECT_EQ(customSpan->GetLength(), 1); + EXPECT_EQ(customSpan->GetStartIndex(), 0); + EXPECT_EQ(customSpan->GetEndIndex(), 1); + EXPECT_EQ(customSpan->GetSpanType(), SpanType::CustomSpan); +} } // namespace OHOS::Ace::NG -- Gitee