11import { DiffDOMOptions , diffType , nodeType } from "../types"
2- import { Diff } from "../helpers"
2+ import { Diff , checkElementType } from "../helpers"
33
44import { objToNode } from "./fromVirtual"
55
66// ===== Apply a diff =====
77
88const getFromRoute = (
99 node : Element ,
10- route : number [ ]
10+ route : number [ ] ,
1111) : Element | Text | false => {
1212 route = route . slice ( )
1313 while ( route . length > 0 ) {
@@ -20,15 +20,15 @@ const getFromRoute = (
2020export function applyDiff (
2121 tree : Element ,
2222 diff : diffType ,
23- options : DiffDOMOptions // {preDiffApply, postDiffApply, textDiff, valueDiffing, _const}
23+ options : DiffDOMOptions , // {preDiffApply, postDiffApply, textDiff, valueDiffing, _const}
2424) {
2525 const action = diff [ options . _const . action ] as string | number
2626 const route = diff [ options . _const . route ] as number [ ]
2727 let node
2828
2929 if (
3030 ! [ options . _const . addElement , options . _const . addTextElement ] . includes (
31- action
31+ action ,
3232 )
3333 ) {
3434 // For adding nodes, we calculate the route later on. It's different because it includes the position of the newly added item.
@@ -51,46 +51,46 @@ export function applyDiff(
5151
5252 switch ( action ) {
5353 case options . _const . addAttribute :
54- if ( ! node || ! ( node instanceof Element ) ) {
54+ if ( ! node || ! checkElementType ( node , " Element" ) ) {
5555 return false
5656 }
5757 node . setAttribute (
5858 diff [ options . _const . name ] as string ,
59- diff [ options . _const . value ] as string
59+ diff [ options . _const . value ] as string ,
6060 )
6161 break
6262 case options . _const . modifyAttribute :
63- if ( ! node || ! ( node instanceof Element ) ) {
63+ if ( ! node || ! checkElementType ( node , " Element" ) ) {
6464 return false
6565 }
6666 node . setAttribute (
6767 diff [ options . _const . name ] as string ,
68- diff [ options . _const . newValue ] as string
68+ diff [ options . _const . newValue ] as string ,
6969 )
7070 if (
71- node instanceof HTMLInputElement &&
71+ checkElementType ( node , " HTMLInputElement" ) &&
7272 diff [ options . _const . name ] === "value"
7373 ) {
7474 node . value = diff [ options . _const . newValue ] as string
7575 }
7676 break
7777 case options . _const . removeAttribute :
78- if ( ! node || ! ( node instanceof Element ) ) {
78+ if ( ! node || ! checkElementType ( node , " Element" ) ) {
7979 return false
8080 }
8181 node . removeAttribute ( diff [ options . _const . name ] as string )
8282 break
8383 case options . _const . modifyTextElement :
84- if ( ! node || ! ( node instanceof Text ) ) {
84+ if ( ! node || ! checkElementType ( node , " Text" ) ) {
8585 return false
8686 }
8787 options . textDiff (
8888 node ,
8989 node . data ,
9090 diff [ options . _const . oldValue ] as string ,
91- diff [ options . _const . newValue ] as string
91+ diff [ options . _const . newValue ] as string ,
9292 )
93- if ( node . parentNode instanceof HTMLTextAreaElement ) {
93+ if ( checkElementType ( node . parentNode , " HTMLTextAreaElement" ) ) {
9494 node . parentNode . value = diff [ options . _const . newValue ] as string
9595 }
9696 break
@@ -101,14 +101,14 @@ export function applyDiff(
101101 node . value = diff [ options . _const . newValue ]
102102 break
103103 case options . _const . modifyComment :
104- if ( ! node || ! ( node instanceof Comment ) ) {
104+ if ( ! node || ! checkElementType ( node , " Comment" ) ) {
105105 return false
106106 }
107107 options . textDiff (
108108 node ,
109109 node . data ,
110110 diff [ options . _const . oldValue ] as string ,
111- diff [ options . _const . newValue ] as string
111+ diff [ options . _const . newValue ] as string ,
112112 )
113113 break
114114 case options . _const . modifyChecked :
@@ -133,19 +133,19 @@ export function applyDiff(
133133 objToNode (
134134 diff [ options . _const . newValue ] as nodeType ,
135135 insideSvg ,
136- options
136+ options ,
137137 ) ,
138- node
138+ node ,
139139 )
140140 break
141141 }
142142 case options . _const . relocateGroup :
143143 nodeArray = Array (
144- ...new Array ( diff [ options . _const . groupLength ] )
144+ ...new Array ( diff [ options . _const . groupLength ] ) ,
145145 ) . map ( ( ) =>
146146 node . removeChild (
147- node . childNodes [ diff [ options . _const . from ] as number ]
148- )
147+ node . childNodes [ diff [ options . _const . from ] as number ] ,
148+ ) ,
149149 )
150150 nodeArray . forEach ( ( childNode , index ) => {
151151 if ( index === 0 ) {
@@ -162,16 +162,16 @@ export function applyDiff(
162162 const parentRoute = route . slice ( )
163163 const c : number = parentRoute . splice ( parentRoute . length - 1 , 1 ) [ 0 ]
164164 node = getFromRoute ( tree , parentRoute )
165- if ( ! ( node instanceof Element ) ) {
165+ if ( ! checkElementType ( node , " Element" ) ) {
166166 return false
167167 }
168168 node . insertBefore (
169169 objToNode (
170170 diff [ options . _const . element ] as nodeType ,
171171 node . namespaceURI === "http://www.w3.org/2000/svg" ,
172- options
172+ options ,
173173 ) ,
174- node . childNodes [ c ] || null
174+ node . childNodes [ c ] || null ,
175175 )
176176 break
177177 }
@@ -181,7 +181,7 @@ export function applyDiff(
181181 }
182182 const parentNode = node . parentNode
183183 parentNode . removeChild ( node )
184- if ( parentNode instanceof HTMLTextAreaElement ) {
184+ if ( checkElementType ( parentNode , " HTMLTextAreaElement" ) ) {
185185 parentNode . value = ""
186186 }
187187 break
@@ -190,14 +190,14 @@ export function applyDiff(
190190 const parentRoute = route . slice ( )
191191 const c : number = parentRoute . splice ( parentRoute . length - 1 , 1 ) [ 0 ]
192192 newNode = options . document . createTextNode (
193- diff [ options . _const . value ] as string
193+ diff [ options . _const . value ] as string ,
194194 )
195195 node = getFromRoute ( tree , parentRoute )
196196 if ( ! node . childNodes ) {
197197 return false
198198 }
199199 node . insertBefore ( newNode , node . childNodes [ c ] || null )
200- if ( node . parentNode instanceof HTMLTextAreaElement ) {
200+ if ( checkElementType ( node . parentNode , " HTMLTextAreaElement" ) ) {
201201 node . parentNode . value = diff [ options . _const . value ] as string
202202 }
203203 break
@@ -220,9 +220,9 @@ export function applyDiff(
220220export function applyDOM (
221221 tree : Element ,
222222 diffs : ( Diff | diffType ) [ ] ,
223- options : DiffDOMOptions
223+ options : DiffDOMOptions ,
224224) {
225225 return diffs . every ( ( diff : Diff | diffType ) =>
226- applyDiff ( tree , diff as diffType , options )
226+ applyDiff ( tree , diff as diffType , options ) ,
227227 )
228228}
0 commit comments