This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Use DOM tree to debug browser against SimpleDOMBuilder #4658
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d26fd4
add in-browser diff console debugging. fix bootstrap dependencies in …
jasonsanjose 1f56f09
Use DOM tree to debug browser against SimpleDOMBuilder
jasonsanjose 48b692d
suppress console log browser and in-editor DOM match
jasonsanjose b277f67
remove debug flag
jasonsanjose 6f2aede
Merge remote-tracking branch 'origin/ResearchLiveHTML' into jasonsanj…
jasonsanjose 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 |
---|---|---|
|
@@ -344,6 +344,11 @@ define(function (require, exports, module) { | |
return textNode.parent.children[childIndex - 1].tagID + "t"; | ||
} | ||
|
||
|
||
function _getTextNodeHash(text) { | ||
return MurmurHash3.hashString(text, text.length, seed); | ||
} | ||
|
||
SimpleDOMBuilder.prototype.build = function (strict) { | ||
var self = this; | ||
var token, tagLabel, lastClosedTag, lastIndex = 0; | ||
|
@@ -450,9 +455,9 @@ define(function (require, exports, module) { | |
var newNode = { | ||
parent: stack[stack.length - 1], | ||
content: token.contents, | ||
weight: token.contents.length, | ||
signature: MurmurHash3.hashString(token.contents, token.contents.length, seed) | ||
weight: token.contents.length | ||
}; | ||
newNode.signature = _getTextNodeHash(newNode.content); | ||
parent.children.push(newNode); | ||
newNode.tagID = getTextNodeID(newNode); | ||
nodeMap[newNode.tagID] = newNode; | ||
|
@@ -844,7 +849,10 @@ define(function (require, exports, module) { | |
// extract forEach iterator callback | ||
var queuePush = function (child) { queue.push(child); }; | ||
|
||
while (!!(currentElement = queue.shift())) { | ||
do { | ||
// we can assume queue is non-empty for the first loop iteration | ||
currentElement = queue.shift(); | ||
|
||
oldElement = oldNode.nodeMap[currentElement.tagID]; | ||
if (oldElement) { | ||
matches[currentElement.tagID] = true; | ||
|
@@ -869,23 +877,30 @@ define(function (require, exports, module) { | |
currentElement.children.forEach(queuePush); | ||
} | ||
} | ||
} | ||
} while (queue.length); | ||
|
||
Object.keys(matches).forEach(function (tagID) { | ||
var currentElement; | ||
var subtreeRoot = newNode.nodeMap[tagID]; | ||
if (subtreeRoot.children) { | ||
attributeCompare(edits, oldNode.nodeMap[tagID], subtreeRoot); | ||
var nav = new DOMNavigator(subtreeRoot); | ||
while (!!(currentElement = nav.next())) { | ||
var currentTagID = currentElement.tagID; | ||
if (!oldNode.nodeMap[currentTagID]) { | ||
// this condition can happen for new elements | ||
continue; | ||
var nav = new DOMNavigator(subtreeRoot), | ||
currentElement; | ||
|
||
// breadth-first traversal of DOM tree to diff attributes | ||
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. This is actually the main traversal that looks for diffs, not just diffing the attributes (though that's kind of subtle... the addition toe matches for currentTagID below is what the generated diff is based on). (In other words, the comment above should be something more like "breadth-first traversal of DOM tree to diff attributes and match up the old and new nodes"). 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. fixed |
||
while (true) { | ||
currentElement = nav.next(); | ||
|
||
if (!currentElement) { | ||
break; | ||
} | ||
matches[currentTagID] = true; | ||
if (currentElement.children) { | ||
attributeCompare(edits, oldNode.nodeMap[currentTagID], currentElement); | ||
|
||
var currentTagID = currentElement.tagID; | ||
|
||
if (oldNode.nodeMap[currentTagID]) { | ||
matches[currentTagID] = true; | ||
if (currentElement.children) { | ||
attributeCompare(edits, oldNode.nodeMap[currentTagID], currentElement); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -1001,21 +1016,82 @@ define(function (require, exports, module) { | |
} | ||
} | ||
|
||
/** | ||
* @private | ||
* Add SimpleDOMBuilder metadata to browser DOM tree JSON representation | ||
* @param {Object} root | ||
*/ | ||
function _processBrowserSimpleDOM(root) { | ||
var nodeMap = {}, | ||
signatureMap = {}; | ||
|
||
function _processElement(elem) { | ||
elem.tagID = elem.attributes["data-brackets-id"]; | ||
elem.weight = 0; | ||
|
||
// remove data-brackets-id attribute for diff | ||
delete elem.attributes["data-brackets-id"]; | ||
|
||
elem.children.forEach(function (child) { | ||
// set parent | ||
child.parent = elem; | ||
|
||
if (child.children) { | ||
_processElement(child); | ||
} else if (child.content) { | ||
child.weight = child.content.length; | ||
child.signature = _getTextNodeHash(child.content); | ||
child.tagID = getTextNodeID(child); | ||
|
||
nodeMap[child.tagID] = child; | ||
signatureMap[child.signature] = child; | ||
} | ||
}); | ||
|
||
elem.signature = _updateHash(elem); | ||
|
||
nodeMap[elem.tagID] = elem; | ||
signatureMap[elem.signature] = elem; | ||
} | ||
|
||
_processElement(root); | ||
|
||
root.nodeMap = nodeMap; | ||
root.signatureMap = signatureMap; | ||
} | ||
|
||
/** | ||
* @private | ||
* Diff the browser DOM with the in-editor DOM | ||
* @param {Editor} editor | ||
* @param {Object} browserSimpleDOM | ||
*/ | ||
function _getBrowserDiff(editor, browserSimpleDOM) { | ||
var cachedValue = _cachedValues[editor.document.file.fullPath], | ||
editorDOM = cachedValue.dom; | ||
|
||
_processBrowserSimpleDOM(browserSimpleDOM); | ||
|
||
return domdiff(editorDOM, browserSimpleDOM); | ||
} | ||
|
||
$(DocumentManager).on("beforeDocumentDelete", _removeDocFromCache); | ||
|
||
exports.scanDocument = scanDocument; | ||
exports.generateInstrumentedHTML = generateInstrumentedHTML; | ||
exports.getUnappliedEditList = getUnappliedEditList; | ||
|
||
// For unit testing | ||
exports._markText = _markText; | ||
exports._getMarkerAtDocumentPos = _getMarkerAtDocumentPos; | ||
exports._getTagIDAtDocumentPos = _getTagIDAtDocumentPos; | ||
exports._buildSimpleDOM = _buildSimpleDOM; | ||
exports._markTextFromDOM = _markTextFromDOM; | ||
exports._updateDOM = _updateDOM; | ||
exports._DOMNavigator = DOMNavigator; | ||
exports._seed = seed; | ||
exports._allowIncremental = allowIncremental; | ||
exports._dumpDOM = _dumpDOM; | ||
// private methods | ||
exports._markText = _markText; | ||
exports._getMarkerAtDocumentPos = _getMarkerAtDocumentPos; | ||
exports._getTagIDAtDocumentPos = _getTagIDAtDocumentPos; | ||
exports._buildSimpleDOM = _buildSimpleDOM; | ||
exports._markTextFromDOM = _markTextFromDOM; | ||
exports._updateDOM = _updateDOM; | ||
exports._DOMNavigator = DOMNavigator; | ||
exports._seed = seed; | ||
exports._allowIncremental = allowIncremental; | ||
exports._dumpDOM = _dumpDOM; | ||
exports._getBrowserDiff = _getBrowserDiff; | ||
|
||
// public API | ||
exports.scanDocument = scanDocument; | ||
exports.generateInstrumentedHTML = generateInstrumentedHTML; | ||
exports.getUnappliedEditList = getUnappliedEditList; | ||
}); |
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.
Based on the comment, it seems like this is supposed to be conditional on a debug flag?
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.
Removing the comment.