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 @@ -86,6 +86,19 @@ function jsxTemplate(templates, ...exprs) {
86
86
const JS_TO_CSS = { } ;
87
87
const CSS_REGEX = / [ A - Z ] / g;
88
88
89
+ /**
90
+ * Unwrap potential signals.
91
+ * @param {* } value
92
+ * @returns {* }
93
+ */
94
+ function normalizeAttrValue ( value ) {
95
+ return value !== null &&
96
+ typeof value === 'object' &&
97
+ typeof value . valueOf === 'function'
98
+ ? value . valueOf ( )
99
+ : value ;
100
+ }
101
+
89
102
/**
90
103
* Serialize an HTML attribute to a string. This function is not
91
104
* expected to be used directly, but rather through a precompile
@@ -100,6 +113,8 @@ function jsxAttr(name, value) {
100
113
if ( typeof result === 'string' ) return result ;
101
114
}
102
115
116
+ value = normalizeAttrValue ( value ) ;
117
+
103
118
if ( name === 'ref' || name === 'key' ) return '' ;
104
119
if ( name === 'style' && typeof value === 'object' ) {
105
120
let str = '' ;
@@ -127,7 +142,7 @@ function jsxAttr(name, value) {
127
142
return '' ;
128
143
} else if ( value === true ) return name ;
129
144
130
- return name + '="' + encodeEntities ( value ) + '"' ;
145
+ return name + '="' + encodeEntities ( '' + value ) + '"' ;
131
146
}
132
147
133
148
/**
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 ;
@@ -127,6 +145,12 @@ describe('precompiled JSX', () => {
127
145
) ;
128
146
} ) ;
129
147
148
+ it ( 'should support signals' , ( ) => {
149
+ const sig = createSignal ( `&<'"` ) ;
150
+ expect ( jsxAttr ( 'foo' , sig ) ) . to . equal ( `foo="&<'""` ) ;
151
+ expect ( jsxAttr ( 'style' , sig ) ) . to . equal ( `style="&<'""` ) ;
152
+ } ) ;
153
+
130
154
it ( 'should call options.attr()' , ( ) => {
131
155
options . attr = ( name , value ) => {
132
156
return `data-${ name } ="foo${ value } "` ;
You can’t perform that action at this time.
0 commit comments