Skip to content

Commit

Permalink
New method: get range from rendered result
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Oct 22, 2015
1 parent 90f817a commit 2029191
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
9 changes: 4 additions & 5 deletions src/main/Rendering.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ class Rendering extends RenderingEvents {

super(options, document);

/**
* @type {Document}
*/
this.document = document;

/**
* ID of rendering, will be set on each element that is part of it
* @type {String}
Expand Down Expand Up @@ -524,11 +519,15 @@ class Rendering extends RenderingEvents {
this._markTextSameNode(startContainer, startOffset, endOffset);
} else {
const result = this._markTextDifferentNode(startContainer, endContainer, startOffset, endOffset);
const index = this.wrapperNodes.indexOf(result.endT);
// remove endContainer, to keep order:
this.wrapperNodes.splice(index, 1);
if (!outer) {
this.wrapSiblings(result.startT.nextSibling, endContainer);
} else {
this.walk(result.startT, endContainer, contextContainer);
}
this.wrapperNodes.push(result.endT);
}
}

Expand Down
29 changes: 29 additions & 0 deletions src/main/RenderingEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ export default class RenderingEvents extends EventEmitter {
this.wrapperNodes = [];

this._registerEvents(document);

/**
* @type {Document}
*/
this.document = document;
}

/**
* Constructs a new Range from rendered result
* @returns {Range}
*/
get range() {
const range = this.document.createRange();
const textNodes = [];

this.wrapperNodes.forEach((wrapper) => {
Util.walkTextNodes(wrapper, (node) => {
textNodes.push(node);
});
});

if(textNodes.length > 0) {
const lastTextNode = textNodes[textNodes.length - 1];
range.setStart(textNodes[0], 0);
range.setEnd(lastTextNode, lastTextNode.length);
return range;
}

return null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/modules/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ document.addEventListener("DOMContentLoaded", function () {
return;
}

tooltip.createTooltip(this.wrapperNodes[0], this.result.text);
tooltip.createTooltip(this.wrapperNodes[0], this.result.text, false);

setTimeout(function () {
if (tooltip.getCurrentTarget()) {
Expand Down
1 change: 0 additions & 1 deletion src/main/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ class Util {
});
}


/**
* @param {Node} container
* @param {Number} thisIndex
Expand Down
5 changes: 5 additions & 0 deletions src/test/Marklib.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ describe("Test some wrappings", () => {
endContainerPath: 'html>body>div>div>p:nth-of-type(4);0'
};

expect(marklib.range.startOffset).toEqual(0);
expect(marklib.range.endOffset).toEqual(0);

expect(result.serialize()).toEqual(expectedResult);
});

Expand All @@ -64,6 +67,8 @@ describe("Test some wrappings", () => {
startContainerPath: 'html>body>div>div>p:nth-of-type(1);0',
endContainerPath: 'html>body>div>div>p:nth-of-type(1);0'
};
expect(marklib.range.startOffset).toEqual(0);
expect(marklib.range.endOffset).toEqual(0);

expect(result.serialize()).toEqual(rangeResult);

Expand Down

0 comments on commit 2029191

Please sign in to comment.