1- import { isFalse , isText } from '../shared/nodes' ;
1+ import { isFalse , isText } from '../shared/nodes' ;
2+ import { anchorableElement } from './anchorableNode' ;
23import client from './client' ;
34import render from './render' ;
4- import { anchorableElement } from './anchorableNode' ;
55
66export default function rerender ( selector , current , next ) {
77
88 current = current === undefined ? client . virtualDom : current ;
99 next = next === undefined ? client . nextVirtualDom : next ;
1010
11- if ( next . instance ) {
11+ if ( next . instance ) {
1212 next . instance . _self . element = selector ;
1313 }
1414
15- if ( ! client . hydrated && selector ) {
16- for ( const element of selector . childNodes ) {
17- if ( element . tagName && element . tagName . toLowerCase ( ) == 'textarea' && element . childNodes . length == 0 ) {
15+ if ( ! client . hydrated && selector ) {
16+ for ( const element of selector . childNodes ) {
17+ if ( element . tagName && element . tagName . toLowerCase ( ) == 'textarea' && element . childNodes . length == 0 ) {
1818 element . appendChild ( document . createTextNode ( '' ) ) ;
1919 }
20- if ( element . COMMENT_NODE === 8 && element . textContent === '#' ) {
20+ if ( element . COMMENT_NODE === 8 && element . textContent === '#' ) {
2121 selector . removeChild ( element ) ;
2222 }
2323 }
2424 }
2525
26- if ( isFalse ( current ) && isFalse ( next ) ) {
26+ if ( isFalse ( current ) && isFalse ( next ) ) {
2727 return ;
2828 }
2929
30- if ( ( isFalse ( current ) || isFalse ( next ) ) && current != next ) {
30+ if ( ( isFalse ( current ) || isFalse ( next ) ) && current != next ) {
3131 const nextSelector = render ( next ) ;
3232 return selector . replaceWith ( nextSelector ) ;
3333 }
3434
35- if ( current . type == 'head' && next . type == 'head' ) {
35+ if ( current . type == 'head' && next . type == 'head' ) {
3636 return ;
3737 }
3838
39- if ( current . type == 'head' || next . type == 'head' ) {
39+ if ( current . type == 'head' || next . type == 'head' ) {
4040 const nextSelector = render ( next ) ;
4141 return selector . replaceWith ( nextSelector ) ;
4242 }
@@ -47,51 +47,51 @@ export default function rerender(selector, current, next) {
4747 }
4848
4949 if ( isText ( current ) && isText ( next ) ) {
50- if ( current != next ) {
50+ if ( current != next ) {
5151 selector . nodeValue = next ;
5252 }
5353 return ;
5454 }
5555
5656 if ( current . type === next . type ) {
5757
58- const attributeNames = Object . keys ( { ...current . attributes , ...next . attributes } ) ;
59- for ( const name of attributeNames ) {
60- if ( name === 'html' ) {
61- if ( next . attributes [ name ] !== current . attributes [ name ] ) {
58+ const attributeNames = Object . keys ( { ...current . attributes , ...next . attributes } ) ;
59+ for ( const name of attributeNames ) {
60+ if ( name === 'html' ) {
61+ if ( next . attributes [ name ] !== current . attributes [ name ] ) {
6262 selector . innerHTML = next . attributes [ name ] ;
6363 anchorableElement ( selector ) ;
64- }
65- } else if ( name === 'checked' ) {
66- if ( next . attributes [ name ] !== selector . value ) {
64+ }
65+ } else if ( name === 'checked' ) {
66+ if ( next . attributes [ name ] !== selector . value ) {
6767 selector . checked = next . attributes [ name ] ;
6868 }
69- } else if ( name === 'value' ) {
70- if ( next . attributes [ name ] !== selector . value ) {
69+ } else if ( name === 'value' ) {
70+ if ( next . attributes [ name ] !== selector . value ) {
7171 selector . value = next . attributes [ name ] ;
7272 }
73- } else if ( name . startsWith ( 'on' ) ) {
73+ } else if ( name . startsWith ( 'on' ) ) {
7474 const eventName = name . replace ( 'on' , '' ) ;
7575 const key = '_event.' + eventName ;
7676 selector . removeEventListener ( eventName , current [ key ] ) ;
77- if ( next . attributes [ name ] ) {
77+ if ( next . attributes [ name ] ) {
7878 next [ key ] = ( event ) => {
79- if ( next . attributes . default !== true ) {
79+ if ( next . attributes . default !== true ) {
8080 event . preventDefault ( ) ;
8181 }
82- next . attributes [ name ] ( { ...next . attributes , event} ) ;
82+ next . attributes [ name ] ( { ...next . attributes , event } ) ;
8383 } ;
8484 selector . addEventListener ( eventName , next [ key ] ) ;
8585 }
8686 } else {
87- const type = typeof ( next . attributes [ name ] ) ;
88- if ( type !== 'object' && type !== 'function' ) {
89- if ( current . attributes [ name ] !== undefined && next . attributes [ name ] === undefined ) {
87+ const type = typeof ( next . attributes [ name ] ) ;
88+ if ( type !== 'object' && type !== 'function' ) {
89+ if ( current . attributes [ name ] !== undefined && next . attributes [ name ] === undefined ) {
9090 selector . removeAttribute ( name ) ;
91- } else if ( current . attributes [ name ] !== next . attributes [ name ] ) {
92- if ( name != 'value' && next . attributes [ name ] === false || next . attributes [ name ] === null || next . attributes [ name ] === undefined ) {
91+ } else if ( current . attributes [ name ] !== next . attributes [ name ] ) {
92+ if ( name != 'value' && next . attributes [ name ] === false || next . attributes [ name ] === null || next . attributes [ name ] === undefined ) {
9393 selector . removeAttribute ( name ) ;
94- } else if ( name != 'value' && next . attributes [ name ] === true ) {
94+ } else if ( name != 'value' && next . attributes [ name ] === true ) {
9595 selector . setAttribute ( name , '' ) ;
9696 } else {
9797 selector . setAttribute ( name , next . attributes [ name ] ) ;
@@ -101,39 +101,39 @@ export default function rerender(selector, current, next) {
101101 }
102102 }
103103
104- if ( next . attributes . html ) return ;
104+ if ( next . attributes . html ) return ;
105105
106106 const limit = Math . max ( current . children . length , next . children . length ) ;
107- if ( next . children . length > current . children . length ) {
108- for ( let i = 0 ; i < current . children . length ; i ++ ) {
107+ if ( next . children . length > current . children . length ) {
108+ for ( let i = 0 ; i < current . children . length ; i ++ ) {
109109 rerender ( selector . childNodes [ i ] , current . children [ i ] , next . children [ i ] ) ;
110110 }
111- for ( let i = current . children . length ; i < next . children . length ; i ++ ) {
111+ for ( let i = current . children . length ; i < next . children . length ; i ++ ) {
112112 const nextSelector = render ( next . children [ i ] ) ;
113113 selector . appendChild ( nextSelector ) ;
114114 }
115- } else if ( current . children . length > next . children . length ) {
116- for ( let i = 0 ; i < next . children . length ; i ++ ) {
115+ } else if ( current . children . length > next . children . length ) {
116+ for ( let i = 0 ; i < next . children . length ; i ++ ) {
117117 rerender ( selector . childNodes [ i ] , current . children [ i ] , next . children [ i ] ) ;
118118 }
119- for ( let i = current . children . length - 1 ; i >= next . children . length ; i -- ) {
120- selector . removeChild ( selector . childNodes [ i ] ) ;
119+ for ( let i = current . children . length - 1 ; i >= next . children . length ; i -- ) {
120+ selector . removeChild ( selector . childNodes [ i ] ) ;
121121 }
122122 } else {
123- for ( let i = limit - 1 ; i > - 1 ; i -- ) {
124- if ( typeof selector . childNodes [ i ] === 'undefined' ) {
125- console . error ( `Virtual DOM does not match the DOM. Expected tag ${ current . type } but instead found undefined. This error usually happens because of an invalid HTML hierarchy like nested forms or tables without tr.` ) ;
123+ for ( let i = limit - 1 ; i > - 1 ; i -- ) {
124+ if ( typeof selector . childNodes [ i ] === 'undefined' ) {
125+ throw new Error ( `Virtual DOM does not match the DOM. Expected tag ${ current . type } but instead found undefined. This error usually happens because of an invalid HTML hierarchy like nested forms or tables without tr.` ) ;
126126 return ;
127127 }
128128 rerender ( selector . childNodes [ i ] , current . children [ i ] , next . children [ i ] ) ;
129129 }
130130 }
131131
132- if ( next . type == 'textarea' ) {
132+ if ( next . type == 'textarea' ) {
133133 selector . value = next . children . join ( "" ) ;
134134 }
135135
136- if ( next . type == 'select' ) {
136+ if ( next . type == 'select' ) {
137137 selector . value = next . attributes . value ;
138138 }
139139
0 commit comments