| 
									
										
										
										
											2020-02-04 07:16:09 +08:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |   ssrRenderAttr, | 
					
						
							|  |  |  |   ssrRenderAttrs, | 
					
						
							|  |  |  |   ssrRenderClass, | 
					
						
							|  |  |  |   ssrRenderStyle, | 
					
						
							|  |  |  | } from '../src/helpers/ssrRenderAttrs' | 
					
						
							| 
									
										
										
										
											2020-02-05 07:42:13 +08:00
										 |  |  | import { escapeHtml } from '@vue/shared' | 
					
						
							| 
									
										
										
										
											2020-01-28 06:23:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 07:42:13 +08:00
										 |  |  | describe('ssr: renderAttrs', () => { | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   test('ignore reserved props', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         key: 1, | 
					
						
							| 
									
										
										
										
											2022-04-14 10:03:17 +08:00
										 |  |  |         ref_key: 'foo', | 
					
						
							|  |  |  |         ref_for: 'bar', | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         ref: () => {}, | 
					
						
							|  |  |  |         onClick: () => {}, | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe('') | 
					
						
							| 
									
										
										
										
											2020-01-28 06:23:42 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   test('normal attrs', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         id: 'foo', | 
					
						
							|  |  |  |         title: 'bar', | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(` id="foo" title="bar"`) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-21 21:44:13 +08:00
										 |  |  |   test('empty value attrs', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							|  |  |  |       ssrRenderAttrs({ | 
					
						
							|  |  |  |         'data-v-abc': '', | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(` data-v-abc`) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   test('escape attrs', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         id: '"><script', | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(` id=""><script"`) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('boolean attrs', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         checked: true, | 
					
						
							| 
									
										
										
										
											2021-08-17 06:18:36 +08:00
										 |  |  |         multiple: false, | 
					
						
							|  |  |  |         readonly: 0, | 
					
						
							|  |  |  |         disabled: '', | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |       }), | 
					
						
							| 
									
										
										
										
											2021-08-17 06:18:36 +08:00
										 |  |  |     ).toBe(` checked disabled`) // boolean attr w/ false should be ignored
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('ignore falsy values', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         foo: false, | 
					
						
							|  |  |  |         title: null, | 
					
						
							|  |  |  |         baz: undefined, | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(` foo="false"`) // non boolean should render `false` as is
 | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-01 21:42:58 +08:00
										 |  |  |   test('ignore non-renderable values', () => { | 
					
						
							| 
									
										
										
										
											2020-02-06 04:21:20 +08:00
										 |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-02-06 04:21:20 +08:00
										 |  |  |         foo: {}, | 
					
						
							|  |  |  |         bar: [], | 
					
						
							|  |  |  |         baz: () => {}, | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(``) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   test('props to attrs', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         readOnly: true, // simple lower case conversion
 | 
					
						
							|  |  |  |         htmlFor: 'foobar', // special cases
 | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(` readonly for="foobar"`) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2020-01-30 06:36:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   test('preserve name on custom element', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs( | 
					
						
							| 
									
										
										
										
											2020-01-30 06:36:06 +08:00
										 |  |  |         { | 
					
						
							|  |  |  |           fooBar: 'ok', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'my-el', | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ).toBe(` fooBar="ok"`) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2022-09-27 17:40:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   test('preserve name on svg elements', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							|  |  |  |       ssrRenderAttrs( | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           viewBox: 'foo', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         'svg', | 
					
						
							|  |  |  |       ), | 
					
						
							|  |  |  |     ).toBe(` viewBox="foo"`) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  | }) | 
					
						
							| 
									
										
										
										
											2020-01-28 06:23:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-05 07:42:13 +08:00
										 |  |  | describe('ssr: renderAttr', () => { | 
					
						
							|  |  |  |   test('basic', () => { | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |     expect(ssrRenderAttr('foo', 'bar')).toBe(` foo="bar"`) | 
					
						
							| 
									
										
										
										
											2020-02-05 07:42:13 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('null and undefined', () => { | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |     expect(ssrRenderAttr('foo', null)).toBe(``) | 
					
						
							|  |  |  |     expect(ssrRenderAttr('foo', undefined)).toBe(``) | 
					
						
							| 
									
										
										
										
											2020-02-05 07:42:13 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('escape', () => { | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |     expect(ssrRenderAttr('foo', '<script>')).toBe( | 
					
						
							| 
									
										
										
										
											2020-02-05 07:42:13 +08:00
										 |  |  |       ` foo="${escapeHtml(`<script>`)}"`, | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  | describe('ssr: renderClass', () => { | 
					
						
							|  |  |  |   test('via renderProps', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         class: ['foo', 'bar'], | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(` class="foo bar"`) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('standalone', () => { | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |     expect(ssrRenderClass(`foo`)).toBe(`foo`) | 
					
						
							|  |  |  |     expect(ssrRenderClass([`foo`, `bar`])).toBe(`foo bar`) | 
					
						
							|  |  |  |     expect(ssrRenderClass({ foo: true, bar: false })).toBe(`foo`) | 
					
						
							|  |  |  |     expect(ssrRenderClass([{ foo: true, bar: false }, `baz`])).toBe(`foo baz`) | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('escape class values', () => { | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |     expect(ssrRenderClass(`"><script`)).toBe(`"><script`) | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   }) | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2020-01-28 06:23:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  | describe('ssr: renderStyle', () => { | 
					
						
							|  |  |  |   test('via renderProps', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderAttrs({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         style: { | 
					
						
							| 
									
										
										
										
											2022-11-08 10:53:44 +08:00
										 |  |  |           color: 'red', | 
					
						
							|  |  |  |           '--a': 2, | 
					
						
							|  |  |  |           '-webkit-line-clamp': 1, | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }), | 
					
						
							| 
									
										
										
										
											2022-11-08 10:53:44 +08:00
										 |  |  |     ).toBe(` style="color:red;--a:2;-webkit-line-clamp:1;"`) | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   }) | 
					
						
							| 
									
										
										
										
											2020-01-28 06:23:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   test('standalone', () => { | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |     expect(ssrRenderStyle(`color:red`)).toBe(`color:red`) | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderStyle({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         color: `red`, | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(`color:red;`) | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderStyle([ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         { color: `red` }, | 
					
						
							|  |  |  |         { fontSize: `15px` }, // case conversion
 | 
					
						
							|  |  |  |       ]), | 
					
						
							|  |  |  |     ).toBe(`color:red;font-size:15px;`) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2020-01-28 06:23:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   test('number handling', () => { | 
					
						
							|  |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderStyle({ | 
					
						
							| 
									
										
										
										
											2022-11-08 10:53:44 +08:00
										 |  |  |         fontSize: null, // invalid value should be ignored
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         opacity: 0.5, | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(`opacity:0.5;`) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2020-01-28 06:23:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |   test('escape inline CSS', () => { | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |     expect(ssrRenderStyle(`"><script`)).toBe(`"><script`) | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |     expect( | 
					
						
							| 
									
										
										
										
											2020-02-07 01:07:25 +08:00
										 |  |  |       ssrRenderStyle({ | 
					
						
							| 
									
										
										
										
											2020-01-30 04:10:45 +08:00
										 |  |  |         color: `"><script`, | 
					
						
							|  |  |  |       }), | 
					
						
							|  |  |  |     ).toBe(`color:"><script;`) | 
					
						
							|  |  |  |   }) | 
					
						
							| 
									
										
										
										
											2020-01-28 06:23:42 +08:00
										 |  |  | }) |