Skip to content

Commit

Permalink
fix(marking): removed treeWalker for now because it yields to wrong r…
Browse files Browse the repository at this point in the history
…esults
  • Loading branch information
BowlingX committed Feb 26, 2016
1 parent 236fe95 commit 1524a00
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 88 deletions.
49 changes: 21 additions & 28 deletions build/js/marklib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/js/marklib.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/js/marklib.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/js/marklib.min.map

Large diffs are not rendered by default.

49 changes: 21 additions & 28 deletions build/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -3838,7 +3838,7 @@ return /******/ (function(modules) { // webpackBootstrap

var maybeFoundNode = null;

Util.walkTextNodes(container, function (n) {
Util.walkDom(container, function (n) {
var atrOffsetStart = n.parentNode.getAttribute(_Rendering.ATTR_DATA_ORIGINAL_OFFSET_START);
atrOffsetStart = atrOffsetStart === null ? 0 : atrOffsetStart;
var atrIndex = n.parentNode.getAttribute(ATTR_DATA_ORIGINAL_INDEX);
Expand All @@ -3854,63 +3854,56 @@ return /******/ (function(modules) { // webpackBootstrap
return false;
}
return true;
}, null);
});

return maybeFoundNode;
}

/**
* Walks the dom tree unless func returns false
* Applies node to function
* Recursively walks the dom tree unless func returns false
* This is a lot more efficient then using any jQuery operations
*
* Applies node to function
* @param {Node} node
* @param {Function} func
* @param {int} type, see `NodeFilter`
* @param {Object} [filter] skips empty text nodes by default
*
* @returns {boolean} true if function did abort walk
* @returns {*}
*/

}, {
key: 'walkDom',
value: function walkDom(node, func) {
var _document;

var type = arguments.length <= 2 || arguments[2] === undefined ? NodeFilter.SHOW_ALL : arguments[2];
var filter = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3];

if (!node) {
return false;
}
var args = [node, type, filter || function () {
return true;
}, false];
args.push(false);
var walker = (_document = document).createTreeWalker.apply(_document, args);
while (walker.nextNode()) {
if (!func(walker.currentNode)) {
return true;
var children = node.childNodes;
if (!children) {
return false;
}
for (var i = 0; i < children.length; i++) {
if (!Util.walkDom(children[i], func)) {
return false;
}
}
return false;
return func(node);
}

/**
* Extracts all TextNodes inside a container
* @param {Node} el
* @param {Function} func
* @param {Object} [filter] skips empty text nodes by default
* @returns {boolean} true if function did abort walk
* @returns {Array.<Text>}
*/

}, {
key: 'walkTextNodes',
value: function walkTextNodes(el, func) {
var filter = arguments.length <= 2 || arguments[2] === undefined ? function (node) {
return !Util.nodeIsEmpty(node);
} : arguments[2];

return Util.walkDom(el, func, NodeFilter.SHOW_TEXT, filter);
Util.walkDom(el, function (node) {
if (Node.TEXT_NODE === node.nodeType && !Util.nodeIsEmpty(node)) {
func(node);
}
return true;
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion build/js/site.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions build/js/site.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/js/site.min.map

Large diffs are not rendered by default.

42 changes: 23 additions & 19 deletions src/main/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class Util {

let maybeFoundNode = null;

Util.walkTextNodes(container, (n) => {
Util.walkDom(container, (n) => {
let atrOffsetStart = n.parentNode.getAttribute(ATTR_DATA_ORIGINAL_OFFSET_START);
atrOffsetStart = atrOffsetStart === null ? 0 : atrOffsetStart;
let atrIndex = n.parentNode.getAttribute(ATTR_DATA_ORIGINAL_INDEX);
Expand All @@ -384,47 +384,51 @@ class Util {
return false;
}
return true;
}, null);
});

return maybeFoundNode;
}


/**
* Walks the dom tree unless func returns false
* Applies node to function
* Recursively walks the dom tree unless func returns false
* This is a lot more efficient then using any jQuery operations
*
* Applies node to function
* @param {Node} node
* @param {Function} func
* @param {int} type, see `NodeFilter`
* @param {Object} [filter] skips empty text nodes by default
*
* @returns {boolean} true if function did abort walk
* @returns {*}
*/
static walkDom(node, func, type = NodeFilter.SHOW_ALL, filter = null) {
static walkDom(node, func) {
if (!node) {
return false;
}
const args = [node, type, filter || (() => true), false];
args.push(false);
const walker = document.createTreeWalker(...args);
while (walker.nextNode()) {
if (!func(walker.currentNode)) {
return true;
const children = node.childNodes;
if (!children) {
return false;
}
for (let i = 0; i < children.length; i++) {
if (!Util.walkDom(children[i], func)) {
return false;
}
}
return false;
return func(node);
}

/**
* Extracts all TextNodes inside a container
* @param {Node} el
* @param {Function} func
* @param {Object} [filter] skips empty text nodes by default
* @returns {boolean} true if function did abort walk
* @returns {Array.<Text>}
*/
static walkTextNodes(el, func, filter = (node) => !Util.nodeIsEmpty(node)) {
return Util.walkDom(el, func, NodeFilter.SHOW_TEXT, filter);
static walkTextNodes(el, func) {
Util.walkDom(el, (node) => {
if (Node.TEXT_NODE === node.nodeType && !Util.nodeIsEmpty(node)) {
func(node);
}
return true;
});
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/test/Marklib.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ describe("Test some wrappings", () => {
expect(result.serialize()).toEqual(rangeResult);

// do a second marking over the old one
const resultedText = new Rendering(document).renderWithResult(rangeResult);
const newRender = new Rendering(document);
const resultedText = newRender.renderWithResult(rangeResult);
expect(resultedText).toEqual('This');
});
});
Expand Down

0 comments on commit 1524a00

Please sign in to comment.