Skip to content

Commit 02e0203

Browse files
committed
+ Minor update.
- Remove fragment - Update HTML attribute detection.
1 parent b0d4530 commit 02e0203

File tree

6 files changed

+43
-193
lines changed

6 files changed

+43
-193
lines changed

README.md

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Update your `tsconfig.json` with:
3939
}
4040
```
4141

42-
> The reason why "jsx" should be set to "react-jsx" is this plugin matches the new JSX transform.
42+
> The reason why "jsx" should be set to "react-jsx" is this plugin has to meet the new JSX transform.
4343
4444
### SWC
4545

@@ -80,10 +80,10 @@ Please read the section below.
8080

8181
```tsx
8282
// In setup.
83-
<button diabled={isDisabledRef.value}>Wow such a button</button>
83+
<button disabled={isDisabledRef.value}>Wow such a button</button>
8484

8585
// In render function.
86-
<button diabled='isDisable'>Very button</button>
86+
<button disabled={this.isDisable}>Very button</button>
8787
```
8888

8989
### On
@@ -108,6 +108,12 @@ Please read the section below.
108108

109109
Native is only available for Vue components.
110110

111+
### Rendering HTMl
112+
113+
```tsx
114+
<div innerHTML={'<h1>Title</h1>'}></div>
115+
```
116+
111117
### HTML / Component ref
112118

113119
```tsx
@@ -138,7 +144,7 @@ const Wrapper = defineComponent({
138144
})
139145
```
140146

141-
Due to limitations, using ref is a little different form to Vue 3.
147+
Due to limitations, using ref is a little different from to Vue 3.
142148

143149
You can check [this](https://github.com/vuejs/composition-api#limitations) out for more information.
144150

@@ -314,30 +320,6 @@ If you want to disable this behavior, add `direct` modifier:
314320
<input v-model={userInputRef} >
315321
```
316322

317-
### Fragment (experimental)
318-
319-
Vue 2 doesn't come with fragment support so this feature is supported by [vue-fragment](https://github.com/Thunberg087/vue-fragment) under the hood:
320-
321-
```tsx
322-
<div>
323-
<>
324-
<h1>Wow such a fragment</h1>
325-
<small>I can smell it</small>
326-
</>
327-
</div>
328-
```
329-
330-
This will be rendered as
331-
332-
```html
333-
<div>
334-
<!--fragment#head-->
335-
<h1>Wow such a fragment</h1>
336-
<small>I can smell it</small>
337-
<!--fragment#tail-->
338-
</div>
339-
```
340-
341323
## Compatibility
342324

343325
These format below are also available, but they are NOT recommended, just for compatibility.

lib/jsx.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
checkKeyIsNativeOn,
66
checkKeyIsChildren,
77
checkKeyIsClass,
8-
checkKeyIsDomProps,
8+
checkKeyIsHtmlAttr,
99
checkKeyIsKey,
1010
checkKeyIsOnEvent,
1111
checkKeyIsOnObject,
@@ -18,7 +18,6 @@ import {
1818
removeNativeOn,
1919
removeOn, checkKeyIsRef
2020
} from './utils'
21-
import { Fragment } from './modules/fragment'
2221
import { dealWithVModel } from './modules/v-model'
2322
import { dealWithDirective } from './modules/directive'
2423
import { ConfigType, TagType } from './type'
@@ -42,15 +41,6 @@ const jsx = function (
4241
on: {}
4342
}
4443

45-
const isFragment = isUndefined(tag) && config.children
46-
if (isFragment) {
47-
return h(
48-
Fragment,
49-
vNodeData,
50-
isArray(config.children) ? config.children : [config.children]
51-
)
52-
}
53-
5444
// I treat every lowercase string as HTML element.
5545
// Because in JSX Vue component should be (Upper) CamelCase named.
5646
const isHTMLElement = checkIsHTMLElement(tag)
@@ -82,17 +72,13 @@ const jsx = function (
8272
continue
8373
}
8474

85-
if (checkKeyIsDomProps(key) && isHTMLElement) {
86-
vNodeData.domProps[key] = value
87-
continue
88-
}
89-
9075
// on.
9176
if (checkKeyIsOnObject(key)) {
9277
vNodeData.on = value
9378
continue
9479
}
9580

81+
// onXX:native
9682
if (checkKeyIsNativeOn(key) && !isHTMLElement) {
9783
const _key = removeNativeOn(key)
9884
vNodeData.nativeOn = vNodeData.nativeOn || {}
@@ -125,6 +111,7 @@ const jsx = function (
125111
continue
126112
}
127113

114+
// ref.
128115
if (checkKeyIsRef(key)) {
129116
vNodeData.ref = value
130117
continue
@@ -142,13 +129,17 @@ const jsx = function (
142129
}
143130

144131
if (isHTMLElement) {
145-
vNodeData.attrs[key] = value
132+
if (checkKeyIsHtmlAttr(key)) {
133+
vNodeData.attrs[key] = value
134+
} else {
135+
vNodeData.domProps[key] = value
136+
}
146137
} else {
147138
vNodeData.props[key] = value
148139
}
149140
}
150141

151-
// Check is JSXS function.
142+
// Check if it is JSXS function.
152143
const isJsxsFunc = typeof tag === 'function' && isUndefined((tag as any).cid)
153144
if (isJsxsFunc) {
154145
return h({

lib/modules/fragment.ts

Lines changed: 0 additions & 93 deletions
This file was deleted.

lib/utils.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
import { camelCase } from 'change-case'
22

3-
const domProps = [
4-
'value', 'defaultValue',
5-
'innerHTML', 'innerText', 'textContent',
6-
'src', 'checked'
7-
]
3+
// https://github.com/vuejs/vue/blob/0603ff695d2f41286239298210113cbe2b209e28/src/platforms/web/server/util.js#L5
4+
// Difference:
5+
// + role
6+
// - id
7+
const htmlAttrList = (
8+
'accept,accept-charset,accesskey,action,align,alt,async,autocomplete,' +
9+
'autofocus,autoplay,autosave,bgcolor,border,buffered,challenge,charset,' +
10+
'checked,cite,class,code,codebase,color,cols,colspan,content,' +
11+
'contenteditable,contextmenu,controls,coords,data,datetime,default,' +
12+
'defer,dir,dirname,disabled,download,draggable,dropzone,enctype,for,' +
13+
'form,formaction,headers,height,hidden,high,href,hreflang,http-equiv,' +
14+
'icon,ismap,itemprop,keytype,kind,label,lang,language,list,loop,low,' +
15+
'manifest,max,maxlength,media,method,GET,POST,min,multiple,email,file,' +
16+
'muted,name,novalidate,open,optimum,pattern,ping,placeholder,poster,' +
17+
'preload,radiogroup,readonly,rel,required,reversed,role,rows,rowspan,sandbox,' +
18+
'scope,scoped,seamless,selected,shape,size,type,text,password,sizes,span,' +
19+
'spellcheck,src,srcdoc,srclang,srcset,start,step,style,summary,tabindex,' +
20+
'target,title,usemap,value,width,wrap'
21+
).split(',')
822

923
const ON_EVENT_REGEXP = /^((v(-o|O)n:)|on)/
1024
const HTML_TAG_REGEXP = /^[\da-z]+$/
@@ -23,7 +37,7 @@ const checkIsHTMLElement = (tag: unknown) => isString(tag) && HTML_TAG_REGEXP.te
2337
const checkKeyIsClass = (key: string) => key === 'class'
2438
const checkKeyIsChildren = (key: string) => key === 'children'
2539
const checkKeyIsStyle = (key: string) => key === 'style'
26-
const checkKeyIsDomProps = (key: string) => domProps.includes(key)
40+
const checkKeyIsHtmlAttr = (key: string) => htmlAttrList.includes(key) || key.indexOf('data-') === 0 || key.indexOf('aria-') === 0
2741
const checkKeyIsOnEvent = (key: string) => ON_EVENT_REGEXP.test(key)
2842
const checkKeyIsOnObject = (key: string) => key === 'on'
2943
const checkKeyIsSlot = (key: string) => key === 'slot'
@@ -63,7 +77,7 @@ export {
6377
checkKeyIsClass,
6478
checkKeyIsChildren,
6579
checkKeyIsStyle,
66-
checkKeyIsDomProps,
80+
checkKeyIsHtmlAttr,
6781
checkKeyIsOnEvent,
6882
checkKeyIsOnObject,
6983
checkKeyIsSlot,

test/attrs.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('HTML testing.', () => {
1616
)
1717
}
1818
})
19-
expect(wrapper.html()).toBe('<div id="an-id" data-type="element" aria-label="label" role="button" class="this-is-class"></div>')
19+
expect(wrapper.html()).toBe('<div data-type="element" aria-label="label" role="button" class="this-is-class" id="an-id"></div>')
2020
})
2121

2222
it('It should deal with reactive attrs.', done => {
@@ -32,15 +32,15 @@ describe('HTML testing.', () => {
3232
contentRef.value = 'Doge II'
3333

3434
nextTick(() => {
35-
expect(wrapper.html()).toBe('<div id="id-1" data-id="id-1" class="static class-1">Doge II</div>')
35+
expect(wrapper.html()).toBe('<div data-id="id-1" class="static class-1" id="id-1">Doge II</div>')
3636
done()
3737
})
3838
}
3939

4040
const check = () => {
4141
// Wait wrapper is initialized.
4242
setTimeout(() => {
43-
expect(wrapper.html()).toBe('<div id="id-0" data-id="id-0" class="static class-0">Doge</div>')
43+
expect(wrapper.html()).toBe('<div data-id="id-0" class="static class-0" id="id-0">Doge</div>')
4444
increase()
4545
}, 500)
4646
}

test/fragment.spec.tsx

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)