File tree Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Expand file tree Collapse file tree 2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -95,6 +95,19 @@ function jsxTemplate(templates, ...exprs) {
95
95
const JS_TO_CSS = { } ;
96
96
const CSS_REGEX = / [ A - Z ] / g;
97
97
98
+ /**
99
+ * Unwrap potential signals.
100
+ * @param {* } value
101
+ * @returns {* }
102
+ */
103
+ function normalizeAttrValue ( value ) {
104
+ return value !== null &&
105
+ typeof value === 'object' &&
106
+ typeof value . valueOf === 'function'
107
+ ? value . valueOf ( )
108
+ : value ;
109
+ }
110
+
98
111
/**
99
112
* Serialize an HTML attribute to a string. This function is not
100
113
* expected to be used directly, but rather through a precompile
@@ -109,6 +122,8 @@ function jsxAttr(name, value) {
109
122
if ( typeof result === 'string' ) return result ;
110
123
}
111
124
125
+ value = normalizeAttrValue ( value ) ;
126
+
112
127
if ( name === 'ref' || name === 'key' ) return '' ;
113
128
if ( name === 'style' && typeof value === 'object' ) {
114
129
let str = '' ;
@@ -145,7 +160,7 @@ function jsxAttr(name, value) {
145
160
return '' ;
146
161
} else if ( value === true ) return name ;
147
162
148
- return name + '="' + encodeEntities ( value ) + '"' ;
163
+ return name + '="' + encodeEntities ( '' + value ) + '"' ;
149
164
}
150
165
151
166
/**
Original file line number Diff line number Diff line change @@ -11,6 +11,24 @@ import {
11
11
import { setupScratch , teardown } from '../../../test/_util/helpers' ;
12
12
import { encodeEntities } from '../../src/utils' ;
13
13
14
+ function createSignal ( value ) {
15
+ return {
16
+ value,
17
+ peek ( ) {
18
+ return value ;
19
+ } ,
20
+ subscribe ( ) {
21
+ return ( ) => { } ;
22
+ } ,
23
+ valueOf ( ) {
24
+ return value ;
25
+ } ,
26
+ toString ( ) {
27
+ return String ( value ) ;
28
+ }
29
+ } ;
30
+ }
31
+
14
32
describe ( 'Babel jsx/jsxDEV' , ( ) => {
15
33
let scratch ;
16
34
let prevVNodeOption ;
@@ -167,6 +185,12 @@ describe('precompiled JSX', () => {
167
185
) ;
168
186
} ) ;
169
187
188
+ it ( 'should support signals' , ( ) => {
189
+ const sig = createSignal ( `&<'"` ) ;
190
+ expect ( jsxAttr ( 'foo' , sig ) ) . to . equal ( `foo="&<'""` ) ;
191
+ expect ( jsxAttr ( 'style' , sig ) ) . to . equal ( `style="&<'""` ) ;
192
+ } ) ;
193
+
170
194
it ( 'should call options.attr()' , ( ) => {
171
195
options . attr = ( name , value ) => {
172
196
return `data-${ name } ="foo${ value } "` ;
You can’t perform that action at this time.
0 commit comments