Skip to content

Commit 4e3453c

Browse files
committed
better handling of iframes, relates to #126
1 parent 8f3444f commit 4e3453c

File tree

7 files changed

+61
-34
lines changed

7 files changed

+61
-34
lines changed

browser/diffDOM.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

browser/diffDOM.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/TraceLogger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {checkElementType} from "./diffDOM/helpers"
2+
13
/**
24
* Use TraceLogger to figure out function calls inside
35
* JS objects by wrapping an object with a TraceLogger
@@ -113,8 +115,8 @@ export class TraceLogger {
113115
if (typeof v === "string") {
114116
return v
115117
}
116-
if (v instanceof HTMLElement) {
117-
return v.outerHTML || "<empty>"
118+
if (checkElementType(v, "HTMLElement")) {
119+
return (v as HTMLElement).outerHTML || "<empty>"
118120
}
119121
if (v instanceof Array) {
120122
return `[${v.map(stringCollapse).join(",")}]`

src/diffDOM/helpers.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,29 @@ export class Diff {
2323
return this
2424
}
2525
}
26+
27+
export const checkElementType = (element, elementTypeName) => {
28+
if (typeof element === 'undefined' || element === null) {
29+
return false
30+
}
31+
32+
33+
if (typeof window !== 'undefined') {
34+
// For browser environment
35+
if (typeof window[elementTypeName] === 'function') {
36+
if(element instanceof (window[elementTypeName] as any) || (
37+
element.ownerDocument?.defaultView &&
38+
element instanceof element.ownerDocument.defaultView[elementTypeName]
39+
)) {
40+
return true
41+
} else {
42+
return false
43+
}
44+
} else {
45+
return false
46+
}
47+
} else {
48+
// For node.js
49+
return typeof global !== 'undefined' && element instanceof global[elementTypeName]
50+
}
51+
}

src/diffDOM/virtual/diff.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
removeDone,
1616
roughlyEqual,
1717
} from "./helpers"
18-
import { Diff } from "../helpers"
18+
import { Diff, checkElementType, } from "../helpers"
1919
import { applyVirtual } from "./apply"
2020
import { nodeToObj } from "./fromDOM"
2121
import { stringToObj } from "./fromString"
@@ -39,15 +39,15 @@ export class DiffFinder {
3939
) {
4040
this.options = options
4141
this.t1 = (
42-
typeof Element !== "undefined" && t1Node instanceof Element
43-
? nodeToObj(t1Node, this.options)
42+
typeof Element !== "undefined" && checkElementType(t1Node, "Element")
43+
? nodeToObj((t1Node as Element), this.options)
4444
: typeof t1Node === "string"
4545
? stringToObj(t1Node, this.options)
4646
: JSON.parse(JSON.stringify(t1Node))
4747
) as elementDiffNodeType
4848
this.t2 = (
49-
typeof Element !== "undefined" && t2Node instanceof Element
50-
? nodeToObj(t2Node, this.options)
49+
typeof Element !== "undefined" && checkElementType(t2Node, "Element")
50+
? nodeToObj((t2Node as Element), this.options)
5151
: typeof t2Node === "string"
5252
? stringToObj(t2Node, this.options)
5353
: JSON.parse(JSON.stringify(t2Node))
@@ -56,14 +56,14 @@ export class DiffFinder {
5656
this.foundAll = false
5757
if (this.debug) {
5858
this.t1Orig =
59-
typeof Element !== "undefined" && t1Node instanceof Element
60-
? nodeToObj(t1Node, this.options)
59+
typeof Element !== "undefined" && checkElementType(t1Node, "Element")
60+
? nodeToObj((t1Node as Element), this.options)
6161
: typeof t1Node === "string"
6262
? stringToObj(t1Node, this.options)
6363
: JSON.parse(JSON.stringify(t1Node))
6464
this.t2Orig =
65-
typeof Element !== "undefined" && t2Node instanceof Element
66-
? nodeToObj(t2Node, this.options)
65+
typeof Element !== "undefined" && checkElementType(t2Node, "Element")
66+
? nodeToObj((t2Node as Element), this.options)
6767
: typeof t2Node === "string"
6868
? stringToObj(t2Node, this.options)
6969
: JSON.parse(JSON.stringify(t2Node))

src/diffDOM/virtual/fromDOM.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { DiffDOMOptionsPartial, elementNodeType, textNodeType } from "../types"
2+
import {checkElementType} from "../helpers"
23

34
export function nodeToObj(
45
aNode: Element,
@@ -7,8 +8,8 @@ export function nodeToObj(
78
const objNode: elementNodeType | textNodeType = {
89
nodeName: aNode.nodeName,
910
}
10-
if (aNode instanceof Text || aNode instanceof Comment) {
11-
;(objNode as unknown as textNodeType).data = aNode.data
11+
if (checkElementType(aNode, "Text") || checkElementType(aNode, "Comment")) {
12+
;(objNode as unknown as textNodeType).data = (aNode as unknown as Text | Comment).data
1213
} else {
1314
if (aNode.attributes && aNode.attributes.length > 0) {
1415
objNode.attributes = {}
@@ -26,29 +27,29 @@ export function nodeToObj(
2627
)
2728
}
2829
if (options.valueDiffing) {
29-
if (aNode instanceof HTMLTextAreaElement) {
30-
objNode.value = aNode.value
30+
if (checkElementType(aNode, "HTMLTextAreaElement")) {
31+
objNode.value = (aNode as HTMLTextAreaElement).value
3132
}
3233
if (
33-
aNode instanceof HTMLInputElement &&
34-
["radio", "checkbox"].includes(aNode.type.toLowerCase()) &&
35-
aNode.checked !== undefined
34+
checkElementType(aNode, "HTMLInputElement") &&
35+
["radio", "checkbox"].includes((aNode as HTMLInputElement).type.toLowerCase()) &&
36+
(aNode as HTMLInputElement).checked !== undefined
3637
) {
37-
objNode.checked = aNode.checked
38+
objNode.checked = (aNode as HTMLInputElement).checked
3839
} else if (
39-
aNode instanceof HTMLButtonElement ||
40-
aNode instanceof HTMLDataElement ||
41-
aNode instanceof HTMLInputElement ||
42-
aNode instanceof HTMLLIElement ||
43-
aNode instanceof HTMLMeterElement ||
44-
aNode instanceof HTMLOptionElement ||
45-
aNode instanceof HTMLProgressElement ||
46-
aNode instanceof HTMLParamElement
40+
checkElementType(aNode, "HTMLButtonElement") ||
41+
checkElementType(aNode, "HTMLDataElement") ||
42+
checkElementType(aNode, "HTMLInputElement") ||
43+
checkElementType(aNode, "HTMLLIElement") ||
44+
checkElementType(aNode, "HTMLMeterElement") ||
45+
checkElementType(aNode, "HTMLOptionElement") ||
46+
checkElementType(aNode, "HTMLProgressElement") ||
47+
checkElementType(aNode, "HTMLParamElement")
4748
) {
48-
objNode.value = aNode.value
49+
objNode.value = (aNode as HTMLButtonElement | HTMLDataElement | HTMLInputElement | HTMLLIElement | HTMLMeterElement | HTMLOptionElement | HTMLProgressElement | HTMLParamElement).value
4950
}
50-
if (aNode instanceof HTMLOptionElement) {
51-
objNode.selected = aNode.selected
51+
if (checkElementType(aNode, "HTMLOptionElement")) {
52+
objNode.selected = (aNode as HTMLOptionElement).selected
5253
}
5354
}
5455
}

src/diffDOM/virtual/helpers.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,5 +461,3 @@ export class DiffTracker {
461461
this.list.forEach((li: Diff) => fn(li))
462462
}
463463
}
464-
465-
//export const elementHasValue = (element: Element) : boolean => element instanceof HTMLButtonElement || element instanceof HTMLDataElement || element instanceof HTMLInputElement || element instanceof HTMLLIElement || element instanceof HTMLMeterElement || element instanceof HTMLOptionElement || element instanceof HTMLProgressElement || element instanceof HTMLParamElement

0 commit comments

Comments
 (0)