This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
MOC-150 handle DOM mutations #165
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
23bf7be
Infrastructure for tracking reactor changes in DOM
jonathankap 2dec321
Full mutation handling
jonathankap e630dd3
Fix bug unapplying after DOM modifications
jonathankap f11300a
Fix MOC-223: handle undoing text changes at the end of an element
jonathankap 46caca4
Fix colors in diff
jonathankap File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
import { AppliableModification } from "../interfaces"; | ||
import * as cssSelector from "css-selector-generator"; | ||
|
||
export class ReplaceAllModification extends AppliableModification { | ||
element: Element; | ||
content: string; | ||
changes: TreeChange[] = []; | ||
observer: MutationObserver; | ||
|
||
constructor(doc: Document, element: Element, content: string) { | ||
super(doc); | ||
this.element = element; | ||
this.content = content; | ||
|
||
this.observer = new MutationObserver(this.handleMutation.bind(this)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
apply(): void { | ||
// mark the element as modified | ||
this.addModifiedElement(this.element); | ||
|
||
this.changes = walkTree( | ||
this.element, | ||
checkText(this.content), | ||
replaceText(this.content, this.addHighlightNode.bind(this)), | ||
replaceText(this.content, | ||
this.addModifiedElement.bind(this), | ||
this.addHighlightNode.bind(this)), | ||
); | ||
|
||
this.observer.observe(this.element, { childList: true, subtree: true }); | ||
} | ||
|
||
unapply(): void { | ||
this.observer.disconnect(); | ||
|
||
const reverseChanges = [...this.changes].reverse(); | ||
for (const change of reverseChanges) { | ||
const parentElement = this.doc.querySelector(change.parentSelector); | ||
const parentElement = this.getModifiedElement(change.parentMocksiId); | ||
if (!parentElement) { | ||
continue; | ||
} | ||
|
@@ -43,10 +54,45 @@ export class ReplaceAllModification extends AppliableModification { | |
parentElement.insertBefore(newTextNode, nextSibling); | ||
} | ||
} | ||
|
||
handleMutation(mutations: MutationRecord[]) { | ||
this.observer.disconnect(); | ||
|
||
for (const mutation of mutations) { | ||
if (mutation.type === "childList") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: an early return/continue would make this code a tiny bit neater |
||
for (const added of mutation.addedNodes) { | ||
const changes = walkTree( | ||
added, | ||
checkText(this.content), | ||
replaceText(this.content, | ||
this.addModifiedElement.bind(this), | ||
this.addHighlightNode.bind(this)), | ||
); | ||
|
||
console.debug(`Added: ${added.nodeName} changes: ${changes.length}`); | ||
|
||
this.changes = this.changes.concat(changes); | ||
} | ||
} | ||
} | ||
|
||
this.observer.observe(this.element, { childList: true, subtree: true }); | ||
} | ||
|
||
modifiedElementRemoved(element: Element, mocksiId: string): boolean { | ||
const noState = super.modifiedElementRemoved(element, mocksiId); | ||
|
||
// remove any changes that were made to this element | ||
this.changes = this.changes.filter((c) => c.parentMocksiId !== mocksiId); | ||
|
||
// if all changed nodes have been removed (including the element itself), | ||
// it is safe to remove this modification | ||
return noState && this.changes.length === 0; | ||
} | ||
} | ||
|
||
type TreeChange = { | ||
parentSelector: string; | ||
parentMocksiId: string; | ||
origText: string; | ||
replaceStart: number; | ||
replaceCount: number; | ||
|
@@ -110,6 +156,7 @@ function checkText(pattern: string): (node: Node) => boolean { | |
|
||
function replaceText( | ||
pattern: string, | ||
addModifiedElement: (element: Element) => string, | ||
addHighlightNode: (node: Node) => void, | ||
): (node: Node) => TreeChange | null { | ||
const { patternRegexp, replacement } = toRegExpPattern(pattern); | ||
|
@@ -127,7 +174,7 @@ function replaceText( | |
if (!parentElement) { | ||
return null; | ||
} | ||
const parentSelector = cssSelector.getCssSelector(parentElement); | ||
const parentMocksiId = addModifiedElement(parentElement); | ||
|
||
let replaceStart = 0; | ||
const nextSibling = node.nextSibling; | ||
|
@@ -154,7 +201,7 @@ function replaceText( | |
} | ||
|
||
return { | ||
parentSelector: parentSelector, | ||
parentMocksiId: parentMocksiId, | ||
origText: node.nodeValue || "", | ||
replaceStart: replaceStart, | ||
replaceCount: split.length, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this apply and unapply func look very similar... No idea how to remove the duplication though, so just leaving this as a note, feel free to ignore 😄