• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-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 "stylemgr/app_style_item.h"
17 #include "ace_log.h"
18 #include "keys.h"
19 #include "number_parser.h"
20 
21 namespace OHOS {
22 namespace ACELite {
EstimatePseudoClassType(const char * const styleKey,uint16_t * keyLength)23 uint8_t AppStyleItem::EstimatePseudoClassType(const char * const styleKey, uint16_t *keyLength)
24 {
25     char *p = strchr(const_cast<char *>(styleKey), ':');
26     if (p == nullptr) {
27         return PSEUDO_CLASS_UNKNOWN;
28     }
29 
30     uint8_t type = PSEUDO_CLASS_UNKNOWN;
31     // :active meets
32     if (!strcmp(p, PSEUDO_CLASS_TYPE_ACTIVE)) {
33         type = PSEUDO_CLASS_ACTIVE;
34     } else if (!strcmp(p, PSEUDO_CLASS_TYPE_CHECKED)) {
35         type = PSEUDO_CLASS_CHECKED;
36     }
37 
38     // drop :xxxx
39     if (type != PSEUDO_CLASS_UNKNOWN) {
40         *p = '\0';
41         *keyLength = (uint16_t)(p - styleKey);
42     }
43     return type;
44 }
45 
SetStringValue(const char * const value)46 void AppStyleItem::SetStringValue(const char * const value)
47 {
48     if (value == nullptr) {
49         return;
50     }
51     size_t len = strlen(value);
52     if (len >= UINT16_MAX) {
53         return;
54     }
55     valueType_ = STYLE_PROP_VALUE_TYPE_STRING;
56     styleValue_.string = static_cast<char *>(ace_malloc(sizeof(char) * (len + 1)));
57     if (styleValue_.string == nullptr) {
58         HILOG_ERROR(HILOG_MODULE_ACE, "create style item string failed.");
59         return;
60     }
61     if (memcpy_s(styleValue_.string, len, value, len) != 0) {
62         HILOG_ERROR(HILOG_MODULE_ACE, "app_style set string value error");
63         ace_free(styleValue_.string);
64         styleValue_.string = nullptr;
65         return;
66     }
67     *(styleValue_.string + len) = '\0';
68 }
69 
UpdateNumValToStr()70 bool AppStyleItem::UpdateNumValToStr()
71 {
72     if (GetValueType() == STYLE_PROP_VALUE_TYPE_NUMBER) {
73         int32_t numVal = GetNumValue();
74         const uint8_t len = 11; // max size of int32_t
75         char strVal[len] = { 0 };
76         if (sprintf_s(strVal, len, "%d", numVal) < 0) {
77             HILOG_ERROR(HILOG_MODULE_ACE, "style item transform num to string fail");
78             return false;
79         }
80         SetStringValue((const char *)strVal);
81     }
82     return true;
83 }
84 
GenerateFromJSValue(jerry_value_t stylePropName,jerry_value_t stylePropValue)85 AppStyleItem *AppStyleItem::GenerateFromJSValue(jerry_value_t stylePropName, jerry_value_t stylePropValue)
86 {
87     uint16_t strLen = 0;
88     char *keyNameBuffer = MallocStringOf(stylePropName, &strLen);
89     if (keyNameBuffer == nullptr) {
90         HILOG_ERROR(HILOG_MODULE_ACE, "convert style prop name to char failed, will be dropped");
91         return nullptr;
92     }
93 
94     if (strLen == 0) {
95         HILOG_ERROR(HILOG_MODULE_ACE, "style prop name length is 0, will be dropped");
96         ace_free(keyNameBuffer);
97         keyNameBuffer = nullptr;
98         return nullptr;
99     }
100 
101     uint8_t pseudoType_ = EstimatePseudoClassType(static_cast<char *>(keyNameBuffer), &strLen);
102     AppStyleItem *styleItem =
103         CreateStyleItem(KeyParser::ParseKeyId((const char *)keyNameBuffer, strLen), stylePropValue, pseudoType_);
104     ace_free(keyNameBuffer);
105     keyNameBuffer = nullptr;
106     return styleItem;
107 }
108 
CreateStyleItem(uint16_t keyId,const jerry_value_t stylePropValue,uint8_t pseudoClassType)109 AppStyleItem *AppStyleItem::CreateStyleItem(uint16_t keyId, const jerry_value_t stylePropValue, uint8_t pseudoClassType)
110 {
111     AppStyleItem *newStyleItem = new AppStyleItem();
112     if (newStyleItem == nullptr) {
113         HILOG_ERROR(HILOG_MODULE_ACE, "new styleItem error");
114         return nullptr;
115     }
116 
117     char *strValueBuffer = nullptr;
118 
119     newStyleItem->propNameId_ = keyId;
120     newStyleItem->pseudoClassType_ = pseudoClassType;
121 
122     if (jerry_value_is_number(stylePropValue)) {
123         if (keyId == K_OPACITY) {
124             newStyleItem->SetFloatingValue(jerry_get_number_value(stylePropValue));
125         } else {
126             newStyleItem->SetNumValue((int32_t)(jerry_get_number_value(stylePropValue)));
127         }
128     } else if (jerry_value_is_boolean(stylePropValue)) {
129         newStyleItem->SetBoolValue(jerry_get_boolean_value(stylePropValue));
130     } else {
131         uint16_t strLength = 0;
132         strValueBuffer = MallocStringOf(stylePropValue, &strLength);
133         if (strValueBuffer == nullptr) {
134             HILOG_ERROR(HILOG_MODULE_ACE, "convert style value to char failed, will be dropped");
135             delete newStyleItem;
136             newStyleItem = nullptr;
137             return nullptr;
138         }
139         float percentValue = 0;
140         if (NumberParser::ParsePercentValue(strValueBuffer, strLength, percentValue)) {
141             newStyleItem->SetPercentValue(percentValue);
142         } else {
143             newStyleItem->SetStringValue((const char *)strValueBuffer);
144         }
145         ace_free(strValueBuffer);
146         strValueBuffer = nullptr;
147     }
148 
149     return newStyleItem;
150 }
151 
CopyFrom(const AppStyleItem * from)152 AppStyleItem *AppStyleItem::CopyFrom(const AppStyleItem *from)
153 {
154     if (from == nullptr) {
155         return nullptr;
156     }
157 
158     AppStyleItem *styleItem = new AppStyleItem();
159     if (styleItem == nullptr) {
160         HILOG_ERROR(HILOG_MODULE_ACE, "new styleItem error");
161         return nullptr;
162     }
163 
164     styleItem->UpdateValueFrom(*from);
165 
166     return styleItem;
167 }
168 
UpdateValueFrom(const AppStyleItem & from)169 void AppStyleItem::UpdateValueFrom(const AppStyleItem &from)
170 {
171     if (valueType_ == STYLE_PROP_VALUE_TYPE_STRING) {
172         ACE_FREE(styleValue_.string);
173     }
174     propNameId_ = from.propNameId_;
175     pseudoClassType_ = from.pseudoClassType_;
176     switch (from.GetValueType()) {
177         case STYLE_PROP_VALUE_TYPE_STRING: {
178             const char *strValue = from.GetStrValue();
179             if (strValue != nullptr) {
180                 SetStringValue(strValue);
181             }
182             break;
183         }
184         case STYLE_PROP_VALUE_TYPE_BOOL:
185             SetBoolValue(from.GetBoolValue());
186             break;
187         case STYLE_PROP_VALUE_TYPE_NUMBER:
188             SetNumValue(from.GetNumValue());
189             break;
190         case STYLE_PROP_VALUE_TYPE_FLOATING:
191             SetFloatingValue(from.GetFloatingValue());
192             break;
193         case STYLE_PROP_VALUE_TYPE_PERCENT:
194             SetPercentValue(from.GetPercentValue());
195             break;
196         default:
197             break;
198     }
199 }
200 } // namespace ACELite
201 } // namespace OHOS
202