1 /*
2  * Copyright (c) 2024 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_ng/base/inspector_filter.h"
17 
18 #include <algorithm>
19 #include <map>
20 #include <string>
21 
22 namespace OHOS::Ace::NG {
23 namespace {
24 const static std::map<std::string, FixedAttrBit> FIXED_ATTR_MAP = {
25     { "id",          FIXED_ATTR_ID          },
26     { "content",     FIXED_ATTR_CONTENT     },
27     { "src",         FIXED_ATTR_SRC         },
28     { "editable",    FIXED_ATTR_EDITABLE    },
29     { "scrollable",  FIXED_ATTR_SCROLLABLE  },
30     { "selectable",  FIXED_ATTR_SELECTABLE  },
31     { "focusable",   FIXED_ATTR_FOCUSABLE   },
32     { "focused",     FIXED_ATTR_FOCUSED     },
33 };
34 }
35 
CheckFixedAttr(const FixedAttrBit attrBit) const36 bool InspectorFilter::CheckFixedAttr(const FixedAttrBit attrBit) const
37 {
38     if (FilterEmpty()) {
39         return true;
40     }
41     return (filterFixed >> attrBit) & 1;
42 }
43 
CheckExtAttr(const std::string & attr) const44 bool InspectorFilter::CheckExtAttr(const std::string& attr) const
45 {
46     if (FilterEmpty()) {
47         return true;
48     }
49     return std::find(filterExt.begin(), filterExt.end(), attr) != filterExt.end();
50 }
51 
CheckFilterAttr(const FixedAttrBit fixedAttrBit,const char * extAttr) const52 bool InspectorFilter::CheckFilterAttr(const FixedAttrBit fixedAttrBit, const char* extAttr) const
53 {
54     /* no filter attr set */
55     if (FilterEmpty()) {
56         return true;
57     }
58     /* fixed attr set */
59     if ((filterFixed >> fixedAttrBit) & 1) {
60         return true;
61     }
62     /* extend attr set */
63     if (std::find(filterExt.begin(), filterExt.end(), extAttr) != filterExt.end()) {
64         return true;
65     }
66     return false;
67 }
68 
IsFastFilter() const69 bool InspectorFilter::IsFastFilter() const
70 {
71     /* no filter attr set */
72     if (FilterEmpty()) {
73         return false;
74     }
75     /* no extend attr set */
76     if (!filterExt.empty()) {
77         return false;
78     }
79     return true;
80 }
81 
AddFilterAttr(const std::string & attr)82 void InspectorFilter::AddFilterAttr(const std::string& attr)
83 {
84     auto iter = FIXED_ATTR_MAP.find(attr);
85     if (iter != FIXED_ATTR_MAP.end()) {
86         filterFixed |= (1ULL << iter->second);  /* this is a fixed attr */
87     } else {
88         /* this is a extend attr */
89         if (std::find(filterExt.begin(), filterExt.end(), attr) == filterExt.end()) {
90             filterExt.emplace_back(attr);
91         }
92     }
93 }
94 
SetFilterID(std::string & id)95 void InspectorFilter::SetFilterID(std::string& id)
96 {
97     filterId = id;
98 }
99 
GetFilterID(void) const100 std::string InspectorFilter::GetFilterID(void) const
101 {
102     return filterId;
103 }
104 
SetFilterDepth(size_t depth)105 void InspectorFilter::SetFilterDepth(size_t depth)
106 {
107     filterDepth = depth;
108 }
109 
GetFilterDepth() const110 size_t InspectorFilter::GetFilterDepth() const
111 {
112     return filterDepth;
113 }
114 
FilterEmpty() const115 bool InspectorFilter::FilterEmpty() const
116 {
117     return !filterFixed && filterExt.empty();
118 }
119 
120 } // namespace OHOS::Ace::NG
121