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 "core/components/declaration/common/declaration_constants.h"
19 
20 namespace OHOS::Ace {
21 
22 using namespace Framework;
23 
InitSpecialized()24 void RichTextDeclaration::InitSpecialized()
25 {
26     AddSpecializedAttribute(DeclarationConstants::DEFAULT_RICH_TEXT_ATTR);
27     AddSpecializedEvent(DeclarationConstants::DEFAULT_RICH_TEXT_EVENT);
28 }
29 
SetSpecializedAttr(const std::pair<std::string,std::string> & attr)30 bool RichTextDeclaration::SetSpecializedAttr(const std::pair<std::string, std::string>& attr)
31 {
32     static const LinearMapNode<void (*)(RichTextDeclaration&, const std::string&)> richTextAttrOperators[] = {
33         { DOM_RICH_TEXT_DATA,
34           [](RichTextDeclaration& declaration, const std::string& data) { declaration.SetData(data); }
35         },
36     };
37     auto item = BinarySearchFindIndex(richTextAttrOperators, ArraySize(richTextAttrOperators), attr.first.c_str());
38     if (item != -1) {
39         richTextAttrOperators[item].value(*this, attr.second);
40         return true;
41     }
42     return false;
43 }
44 
SetSpecializedEvent(int32_t pageId,const std::string & eventId,const std::string & event)45 bool RichTextDeclaration::SetSpecializedEvent(int32_t pageId, const std::string& eventId, const std::string& event)
46 {
47     static const LinearMapNode<void (*)(RichTextDeclaration&, const EventMarker&)> eventOperators[] = {
48         { DOM_LOAD_COMPLETE, [](RichTextDeclaration& declaration, const EventMarker& event) {
49             auto& richTextEvent = declaration.MaybeResetEvent<RichTextEvent>(EventTag::SPECIALIZED_EVENT);
50             if (richTextEvent.IsValid()) {
51                 richTextEvent.pageFinishEventId = event;
52             }}
53         },
54         { DOM_LOAD_START, [](RichTextDeclaration& declaration, const EventMarker& event) {
55             auto& richTextEvent = declaration.MaybeResetEvent<RichTextEvent>(EventTag::SPECIALIZED_EVENT);
56             if (richTextEvent.IsValid()) {
57                 richTextEvent.pageStartEventId = event;
58             }}
59         },
60     };
61     auto operatorIter = BinarySearchFindIndex(eventOperators, ArraySize(eventOperators), event.c_str());
62     if (operatorIter != -1) {
63         eventOperators[operatorIter].value(*this, EventMarker(eventId, event, pageId));
64         return true;
65     }
66     return false;
67 }
68 
69 } // namespace OHOS::Ace
70