@@ -44,10 +44,19 @@ export class Attributes {
44
44
}
45
45
}
46
46
47
+ _handleError ( attr , error ) {
48
+ if ( ! this . base . isExists ( ) ) return
49
+ console . error (
50
+ `Failed to evaluate ${ attr } for Entity#${ this . base . id } :` ,
51
+ error
52
+ )
53
+ }
54
+
47
55
async evaluate ( ) {
48
56
await this . evaluateClass ( )
49
57
await this . evaluateText ( )
50
58
await this . evaluateValue ( )
59
+ await this . evaluateChecked ( )
51
60
52
61
for ( const attr of this . dynamicAttributes ) {
53
62
if ( Attributes . CUSTOM_ATTRIBUTES . includes ( attr ) ) continue
@@ -62,7 +71,8 @@ export class Attributes {
62
71
if ( attr === ':parent' ) this . evaluateParent ( )
63
72
else if ( attr === ':class' ) await this . evaluateClass ( )
64
73
else if ( attr === ':text' ) await this . evaluateText ( )
65
- else if ( [ ':value' , ':checked' ] . includes ( attr ) ) await this . evaluateValue ( )
74
+ else if ( attr === ':value' ) await this . evaluateValue ( )
75
+ else if ( attr === ':checked' ) await this . evaluateChecked ( )
66
76
else if ( attr === ':each' ) await this . evaluateEach ( )
67
77
else {
68
78
if ( ! this . dynamicAttributes . includes ( attr ) )
@@ -89,8 +99,7 @@ export class Attributes {
89
99
90
100
this . base . element . setAttribute ( 'class' , updatedClassNames )
91
101
} catch ( error ) {
92
- if ( ! this . base . isExists ( ) ) return
93
- throw error
102
+ this . _handleError ( ':class' , error )
94
103
}
95
104
}
96
105
@@ -101,54 +110,55 @@ export class Attributes {
101
110
try {
102
111
const newText = await this . base . _interpret ( textExpr )
103
112
104
- if ( newText || newText == '' ) this . base . element . innerText = newText
113
+ if ( newText || newText == '' ) this . base . element . textContent = newText
105
114
} catch ( error ) {
106
- if ( ! this . base . isExists ( ) ) return
107
- throw error
115
+ this . _handleError ( ':text' , error )
108
116
}
109
117
}
110
118
111
119
async evaluateValue ( ) {
120
+ const valueExpr = this . base . element . getAttribute ( ':value' )
121
+ if ( ! valueExpr ) return
112
122
try {
113
- const valueExpr = this . base . element . getAttribute ( ':value' )
114
-
115
- if ( valueExpr ) {
116
- const newValue = await this . base . _interpret ( valueExpr )
123
+ const newValue = await this . base . _interpret ( valueExpr )
117
124
118
- if ( this . base . element . value !== newValue && newValue != null )
119
- this . base . element . value = newValue
120
- }
125
+ if ( this . base . element . value !== newValue && newValue != null )
126
+ this . base . element . value = newValue
127
+ } catch ( error ) {
128
+ this . _handleError ( ':value' , error )
129
+ }
130
+ }
121
131
122
- const checkedExpr = this . base . element . getAttribute ( ':checked' )
132
+ async evaluateChecked ( ) {
133
+ const checkedExpr = this . base . element . getAttribute ( ':checked' )
134
+ if ( ! checkedExpr ) return
123
135
124
- if ( checkedExpr ) {
125
- const newValue = await this . base . _interpret ( checkedExpr )
136
+ try {
137
+ const isChecked = await this . base . _interpret ( checkedExpr )
126
138
127
- if ( newValue ) this . base . element . checked = newValue
128
- }
139
+ if ( this . base . element . checked !== isChecked && isChecked != null )
140
+ this . base . element . checked = isChecked
129
141
} catch ( error ) {
130
- if ( ! this . base . isExists ( ) ) return
131
- throw error
142
+ this . _handleError ( ':checked' , error )
132
143
}
133
144
}
134
145
135
146
async evaluateOtherAttributes ( ) {
136
- try {
137
- for ( const attr of this . dynamicAttributes ) {
138
- if ( Attributes . CUSTOM_ATTRIBUTES . includes ( attr ) ) continue
147
+ for ( const attr of this . dynamicAttributes ) {
148
+ if ( Attributes . CUSTOM_ATTRIBUTES . includes ( attr ) ) continue
139
149
140
- const expr = this . base . element . getAttribute ( attr )
141
- if ( ! expr ) return
150
+ const expr = this . base . element . getAttribute ( attr )
151
+ if ( ! expr ) return
142
152
153
+ try {
143
154
const newValue = await this . base . _interpret ( expr )
144
155
const nativeAttr = attr . slice ( 1 )
145
156
146
157
if ( this . base . element [ nativeAttr ] !== newValue && newValue != null )
147
158
this . base . element [ nativeAttr ] = newValue
159
+ } catch ( error ) {
160
+ this . _handleError ( attr , error )
148
161
}
149
- } catch ( error ) {
150
- if ( ! this . base . isExists ( ) ) return
151
- throw error
152
162
}
153
163
}
154
164
@@ -159,19 +169,24 @@ export class Attributes {
159
169
160
170
const [ args , iterable ] = eachExpr . split ( ' in ' )
161
171
const [ variable , indexName ] = args . split ( ',' ) . map ( ( v ) => v . trim ( ) )
162
- const items = await this . base . _interpret ( iterable )
163
- this . childClone ||= this . base . element . innerHTML
164
172
165
- let newHTML = ''
173
+ try {
174
+ const items = await this . base . _interpret ( iterable )
175
+ this . childClone ||= this . base . element . innerHTML
166
176
167
- items . forEach ( ( item , index ) => {
168
- // TODO: Use the lexer to replace the variables
169
- newHTML += this . childClone
170
- . replaceAll ( indexName , index )
171
- . replaceAll ( variable , `'${ escapeHTML ( item ) } '` )
172
- } )
177
+ let newHTML = ''
173
178
174
- // ObserveDOM will be called for updated DOM to initialize the entities
175
- this . base . element . innerHTML = newHTML
179
+ items . forEach ( ( item , index ) => {
180
+ // TODO: Use the lexer to replace the variables
181
+ newHTML += this . childClone
182
+ . replaceAll ( indexName , index )
183
+ . replaceAll ( variable , `'${ escapeHTML ( item ) } '` )
184
+ } )
185
+
186
+ // ObserveDOM will be called for updated DOM to initialize the entities
187
+ this . base . element . innerHTML = newHTML
188
+ } catch ( error ) {
189
+ this . _handleError ( ':each' , error )
190
+ }
176
191
}
177
192
}
0 commit comments