1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "core/components/declaration/richtext/rich_text_declaration.h"
17
18 #include "base/utils/string_utils.h"
19 #include "core/components/declaration/common/declaration_constants.h"
20 #include "frameworks/bridge/common/utils/utils.h"
21
22 namespace OHOS::Ace {
23
24 using namespace Framework;
25
InitSpecialized()26 void RichTextDeclaration::InitSpecialized()
27 {
28 AddSpecializedAttribute(DeclarationConstants::DEFAULT_RICH_TEXT_ATTR);
29 AddSpecializedEvent(DeclarationConstants::DEFAULT_RICH_TEXT_EVENT);
30 }
31
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)32 bool RichTextDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
33 {
34 static const LinearMapNode<void (*)(RichTextDeclaration&, const std::string&)> richTextAttrOperators[] = {
35 { DOM_RICH_TEXT_DATA,
36 [](RichTextDeclaration& declaration, const std::string& data) { declaration.SetData(data); }
37 },
38 };
39 auto item = BinarySearchFindIndex(richTextAttrOperators, ArraySize(richTextAttrOperators), attr.first.c_str());
40 if (item != -1) {
41 richTextAttrOperators[item].value(*this, attr.second);
42 return true;
43 }
44 return false;
45 }
46
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)47 bool RichTextDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
48 {
49 static const LinearMapNode<void (*)(RichTextDeclaration&, const EventMarker&)> eventOperators[] = {
50 { DOM_LOAD_COMPLETE, [](RichTextDeclaration& declaration, const EventMarker& event) {
51 auto& richTextEvent = declaration.MaybeResetEvent<RichTextEvent>(EventTag::SPECIALIZED_EVENT);
52 if (richTextEvent.IsValid()) {
53 richTextEvent.pageFinishEventId = event;
54 }}
55 },
56 { DOM_LOAD_START, [](RichTextDeclaration& declaration, const EventMarker& event) {
57 auto& richTextEvent = declaration.MaybeResetEvent<RichTextEvent>(EventTag::SPECIALIZED_EVENT);
58 if (richTextEvent.IsValid()) {
59 richTextEvent.pageStartEventId = event;
60 }}
61 },
62 };
63 auto operatorIter = BinarySearchFindIndex(eventOperators, ArraySize(eventOperators), event.c_str());
64 if (operatorIter != -1) {
65 eventOperators[operatorIter].value(*this, EventMarker(eventId, event, pageId));
66 return true;
67 }
68 return false;
69 }
70
71 } // namespace OHOS::Ace
72