Skip to content

Commit 0f6ee21

Browse files
committed
lint
1 parent a7a0942 commit 0f6ee21

File tree

5 files changed

+156
-34
lines changed

5 files changed

+156
-34
lines changed

src/diffDOM/dom/apply.ts

+69-10
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ export function applyDiff(
5151

5252
switch (action) {
5353
case options._const.addAttribute:
54-
if (!node || !checkElementType(node, options.simplifiedElementCheck, "Element")) {
54+
if (
55+
!node ||
56+
!checkElementType(
57+
node,
58+
options.simplifiedElementCheck,
59+
"Element",
60+
)
61+
) {
5562
return false
5663
}
5764
node.setAttribute(
@@ -60,28 +67,49 @@ export function applyDiff(
6067
)
6168
break
6269
case options._const.modifyAttribute:
63-
if (!node || !checkElementType(node, options.simplifiedElementCheck, "Element")) {
70+
if (
71+
!node ||
72+
!checkElementType(
73+
node,
74+
options.simplifiedElementCheck,
75+
"Element",
76+
)
77+
) {
6478
return false
6579
}
6680
node.setAttribute(
6781
diff[options._const.name] as string,
6882
diff[options._const.newValue] as string,
6983
)
7084
if (
71-
checkElementType(node, options.simplifiedElementCheck, "HTMLInputElement") &&
85+
checkElementType(
86+
node,
87+
options.simplifiedElementCheck,
88+
"HTMLInputElement",
89+
) &&
7290
diff[options._const.name] === "value"
7391
) {
7492
node.value = diff[options._const.newValue] as string
7593
}
7694
break
7795
case options._const.removeAttribute:
78-
if (!node || !checkElementType(node, options.simplifiedElementCheck, "Element")) {
96+
if (
97+
!node ||
98+
!checkElementType(
99+
node,
100+
options.simplifiedElementCheck,
101+
"Element",
102+
)
103+
) {
79104
return false
80105
}
81106
node.removeAttribute(diff[options._const.name] as string)
82107
break
83108
case options._const.modifyTextElement:
84-
if (!node || !checkElementType(node, options.simplifiedElementCheck, "Text")) {
109+
if (
110+
!node ||
111+
!checkElementType(node, options.simplifiedElementCheck, "Text")
112+
) {
85113
return false
86114
}
87115
options.textDiff(
@@ -90,7 +118,13 @@ export function applyDiff(
90118
diff[options._const.oldValue] as string,
91119
diff[options._const.newValue] as string,
92120
)
93-
if (checkElementType(node.parentNode, options.simplifiedElementCheck, "HTMLTextAreaElement")) {
121+
if (
122+
checkElementType(
123+
node.parentNode,
124+
options.simplifiedElementCheck,
125+
"HTMLTextAreaElement",
126+
)
127+
) {
94128
node.parentNode.value = diff[options._const.newValue] as string
95129
}
96130
break
@@ -101,7 +135,14 @@ export function applyDiff(
101135
node.value = diff[options._const.newValue]
102136
break
103137
case options._const.modifyComment:
104-
if (!node || !checkElementType(node, options.simplifiedElementCheck, "Comment")) {
138+
if (
139+
!node ||
140+
!checkElementType(
141+
node,
142+
options.simplifiedElementCheck,
143+
"Comment",
144+
)
145+
) {
105146
return false
106147
}
107148
options.textDiff(
@@ -161,7 +202,13 @@ export function applyDiff(
161202
const parentRoute = route.slice()
162203
const c: number = parentRoute.splice(parentRoute.length - 1, 1)[0]
163204
node = getFromRoute(tree, parentRoute)
164-
if (!checkElementType(node, options.simplifiedElementCheck, "Element")) {
205+
if (
206+
!checkElementType(
207+
node,
208+
options.simplifiedElementCheck,
209+
"Element",
210+
)
211+
) {
165212
return false
166213
}
167214
node.insertBefore(
@@ -180,7 +227,13 @@ export function applyDiff(
180227
}
181228
const parentNode = node.parentNode
182229
parentNode.removeChild(node)
183-
if (checkElementType(parentNode, options.simplifiedElementCheck, "HTMLTextAreaElement")) {
230+
if (
231+
checkElementType(
232+
parentNode,
233+
options.simplifiedElementCheck,
234+
"HTMLTextAreaElement",
235+
)
236+
) {
184237
parentNode.value = ""
185238
}
186239
break
@@ -196,7 +249,13 @@ export function applyDiff(
196249
return false
197250
}
198251
node.insertBefore(newNode, node.childNodes[c] || null)
199-
if (checkElementType(node.parentNode, options.simplifiedElementCheck, "HTMLTextAreaElement")) {
252+
if (
253+
checkElementType(
254+
node.parentNode,
255+
options.simplifiedElementCheck,
256+
"HTMLTextAreaElement",
257+
)
258+
) {
200259
node.parentNode.value = diff[options._const.value] as string
201260
}
202261
break

src/diffDOM/dom/fromVirtual.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,23 @@ export function objToNode(
6969
| HTMLParamElement
7070
).value = objNode.value
7171
}
72-
if (objNode.checked && checkElementType(node, options.simplifiedElementCheck, "HTMLInputElement")) {
72+
if (
73+
objNode.checked &&
74+
checkElementType(
75+
node,
76+
options.simplifiedElementCheck,
77+
"HTMLInputElement",
78+
)
79+
) {
7380
;(node as HTMLInputElement).checked = objNode.checked
7481
}
7582
if (
7683
objNode.selected &&
77-
checkElementType(node, options.simplifiedElementCheck, "HTMLOptionElement")
84+
checkElementType(
85+
node,
86+
options.simplifiedElementCheck,
87+
"HTMLOptionElement",
88+
)
7889
) {
7990
;(node as HTMLOptionElement).selected = objNode.selected
8091
}

src/diffDOM/helpers.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ export class Diff {
3232
* @param simplifiedCheck If true, uses simplified checking based on nodeName/nodeType
3333
* @returns boolean indicating if the element matches any of the specified types
3434
*/
35-
export function checkElementType(element, simplifiedCheck = false, ...elementTypeNames: string[]) {
35+
export function checkElementType(
36+
element,
37+
simplifiedCheck = false,
38+
...elementTypeNames: string[]
39+
) {
3640
if (typeof element === "undefined" || element === null) {
3741
return false
3842
}
@@ -42,28 +46,34 @@ export function checkElementType(element, simplifiedCheck = false, ...elementTyp
4246
return elementTypeNames.some((elementTypeName) => {
4347
// Special case for basic element types
4448
if (elementTypeName === "Element") {
45-
return element.nodeType === 1 ||
46-
(typeof element.nodeName === "string" &&
49+
return (
50+
element.nodeType === 1 ||
51+
(typeof element.nodeName === "string" &&
4752
element.nodeName !== "#text" &&
48-
element.nodeName !== "#comment");
53+
element.nodeName !== "#comment")
54+
)
4955
}
5056
if (elementTypeName === "Text") {
51-
return element.nodeType === 3 ||
52-
element.nodeName === "#text";
57+
return element.nodeType === 3 || element.nodeName === "#text"
5358
}
5459
if (elementTypeName === "Comment") {
55-
return element.nodeType === 8 ||
56-
element.nodeName === "#comment";
60+
return element.nodeType === 8 || element.nodeName === "#comment"
5761
}
5862

5963
// For HTML element types, check nodeName
60-
if (elementTypeName.startsWith("HTML") && elementTypeName.endsWith("Element")) {
61-
const tagName = elementTypeName.slice(4, -7).toLowerCase();
62-
return (element.nodeName && element.nodeName.toLowerCase() === tagName);
64+
if (
65+
elementTypeName.startsWith("HTML") &&
66+
elementTypeName.endsWith("Element")
67+
) {
68+
const tagName = elementTypeName.slice(4, -7).toLowerCase()
69+
return (
70+
element.nodeName &&
71+
element.nodeName.toLowerCase() === tagName
72+
)
6373
}
6474

65-
return false;
66-
});
75+
return false
76+
})
6777
}
6878

6979
// DOM-based check

src/diffDOM/virtual/diff.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ export class DiffFinder {
4040
this.options = options
4141
this.t1 = (
4242
typeof Element !== "undefined" &&
43-
checkElementType(t1Node, this.options.simplifiedElementCheck, "Element")
43+
checkElementType(
44+
t1Node,
45+
this.options.simplifiedElementCheck,
46+
"Element",
47+
)
4448
? nodeToObj(t1Node as Element, this.options)
4549
: typeof t1Node === "string"
4650
? stringToObj(t1Node, this.options)
4751
: JSON.parse(JSON.stringify(t1Node))
4852
) as elementDiffNodeType
4953
this.t2 = (
5054
typeof Element !== "undefined" &&
51-
checkElementType(t2Node, this.options.simplifiedElementCheck, "Element")
55+
checkElementType(
56+
t2Node,
57+
this.options.simplifiedElementCheck,
58+
"Element",
59+
)
5260
? nodeToObj(t2Node as Element, this.options)
5361
: typeof t2Node === "string"
5462
? stringToObj(t2Node, this.options)
@@ -59,14 +67,22 @@ export class DiffFinder {
5967
if (this.debug) {
6068
this.t1Orig =
6169
typeof Element !== "undefined" &&
62-
checkElementType(t1Node, this.options.simplifiedElementCheck, "Element")
70+
checkElementType(
71+
t1Node,
72+
this.options.simplifiedElementCheck,
73+
"Element",
74+
)
6375
? nodeToObj(t1Node as Element, this.options)
6476
: typeof t1Node === "string"
6577
? stringToObj(t1Node, this.options)
6678
: JSON.parse(JSON.stringify(t1Node))
6779
this.t2Orig =
6880
typeof Element !== "undefined" &&
69-
checkElementType(t2Node, this.options.simplifiedElementCheck, "Element")
81+
checkElementType(
82+
t2Node,
83+
this.options.simplifiedElementCheck,
84+
"Element",
85+
)
7086
? nodeToObj(t2Node as Element, this.options)
7187
: typeof t2Node === "string"
7288
? stringToObj(t2Node, this.options)

src/diffDOM/virtual/fromDOM.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@ import { checkElementType } from "../helpers"
33

44
export function nodeToObj(
55
aNode: Element,
6-
options: DiffDOMOptionsPartial = { valueDiffing: true, simplifiedElementCheck: true },
6+
options: DiffDOMOptionsPartial = {
7+
valueDiffing: true,
8+
simplifiedElementCheck: true,
9+
},
710
) {
811
const objNode: elementNodeType | textNodeType = {
912
nodeName: aNode.nodeName,
1013
}
11-
if (checkElementType(aNode, options.simplifiedElementCheck, "Text", "Comment")) {
14+
if (
15+
checkElementType(
16+
aNode,
17+
options.simplifiedElementCheck,
18+
"Text",
19+
"Comment",
20+
)
21+
) {
1222
;(objNode as unknown as textNodeType).data = (
1323
aNode as unknown as Text | Comment
1424
).data
@@ -29,11 +39,21 @@ export function nodeToObj(
2939
)
3040
}
3141
if (options.valueDiffing) {
32-
if (checkElementType(aNode, options.simplifiedElementCheck, "HTMLTextAreaElement")) {
42+
if (
43+
checkElementType(
44+
aNode,
45+
options.simplifiedElementCheck,
46+
"HTMLTextAreaElement",
47+
)
48+
) {
3349
objNode.value = (aNode as HTMLTextAreaElement).value
3450
}
3551
if (
36-
checkElementType(aNode, options.simplifiedElementCheck, "HTMLInputElement") &&
52+
checkElementType(
53+
aNode,
54+
options.simplifiedElementCheck,
55+
"HTMLInputElement",
56+
) &&
3757
["radio", "checkbox"].includes(
3858
(aNode as HTMLInputElement).type.toLowerCase(),
3959
) &&
@@ -66,7 +86,13 @@ export function nodeToObj(
6686
| HTMLParamElement
6787
).value
6888
}
69-
if (checkElementType(aNode, options.simplifiedElementCheck, "HTMLOptionElement")) {
89+
if (
90+
checkElementType(
91+
aNode,
92+
options.simplifiedElementCheck,
93+
"HTMLOptionElement",
94+
)
95+
) {
7096
objNode.selected = (aNode as HTMLOptionElement).selected
7197
}
7298
}

0 commit comments

Comments
 (0)