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 0000000000000000000000000000000000000000..a935913e6e899a4741ec354573e7fe80dff19dce --- /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 32507a2e8c49a00a1cb0bf2906b437b76e22be70..dcc6f40cd03153034fb1515fb61eae97ee77cce8 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 ae62f00eb80aa3cf30b97b0174aaa613e5b0ca93..ba777dc56cd464bbb562414d0a254460d9d2076f 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