diff --git a/src/main/Rendering.js b/src/main/Rendering.js index 3190395..d26cb7a 100644 --- a/src/main/Rendering.js +++ b/src/main/Rendering.js @@ -663,7 +663,7 @@ class Rendering { * `` * @param {string} startPath * @param {string} endPath - * @returns {*} + * @returns {Range} */ renderWithPath(startPath, endPath) { const startContainer = this._deserializePath(startPath); @@ -678,12 +678,23 @@ class Rendering { throw 'Could not find start- and/or end-container in document'; } + /** + * Renders a result (that returned from `renderWithRange`) + * @param result + * @returns {Range} + */ + renderWithResult(result) { + return this.renderWithPath( + `${result.startContainerPath};${result.startOffset}`, + `${result.endContainerPath};${result.endOffset}`); + } + /** * Prepares a selection with a range object * @param {Range} range * @param {boolean} [withoutResult] optional do calculate a result, the selection would not be serializable - * @returns {*} + * @returns {Object} */ renderWithRange(range, withoutResult) { return this._renderWithElements(range.startContainer, range.endContainer, diff --git a/src/main/modules/Site.js b/src/main/modules/Site.js index 3885fd6..1576a18 100644 --- a/src/main/modules/Site.js +++ b/src/main/modules/Site.js @@ -29,7 +29,7 @@ document.addEventListener("DOMContentLoaded", function () { if (autoMarkText.parentNode.nextSibling) { let nextText = autoMarkText.parentNode.nextSibling.childNodes[0]; setTimeout(function () { - render(nextText, ++c, length); + render(nextText, ++c, length); }, speed); } }; @@ -46,9 +46,9 @@ document.addEventListener("DOMContentLoaded", function () { marker.renderWithPath(range.startContainerPath + ";" + range.startOffset, range.endContainerPath + ";" + range.endOffset); } catch (e) { - console.warn("Could not render:", range); + console.warn("Could not render:", range, e); localStorage.setItem(STORAGE_KEY, JSON.stringify([])); - throw 'Cleared local storage because of a rendering issue, the page might have been changed ;)'; + console.error('Cleared local storage because of a rendering issue, the page might have been changed ;)'); } }); @@ -71,6 +71,7 @@ document.addEventListener("DOMContentLoaded", function () { var selection = document.getSelection(), renderer = new Marklib.Rendering(document), result = renderer.renderWithRange(selection.getRangeAt(0)); selection.removeAllRanges(); + console.info("stored:", result); savedRanges.push(result); localStorage.setItem(STORAGE_KEY, JSON.stringify(savedRanges)); } catch (e) { diff --git a/src/main/util/Util.js b/src/main/util/Util.js index ba7ba81..1bea72d 100644 --- a/src/main/util/Util.js +++ b/src/main/util/Util.js @@ -119,18 +119,19 @@ class Util { static calcIndex(node) { let calculatedIndex = 0, foundWrapper = false; - const nodes = node.childNodes, length = nodes.length; + const nodes = node.parentNode.childNodes, length = nodes.length; for (let thisIndex = 0; thisIndex < length; thisIndex++) { const el = nodes[thisIndex]; if (el === node) { - return false; + break; } - const maybeIndexOfOriginal = el.getAttribute(ATTR_DATA_ORIGINAL_INDEX); - const isOriginal = maybeIndexOfOriginal !== undefined; + const maybeIndexOfOriginal = el.getAttribute ? el.getAttribute(ATTR_DATA_ORIGINAL_INDEX) : null; + const isOriginal = maybeIndexOfOriginal !== null; // Important: do not include pseudo elements - if (el !== node && (el.nodeType !== Node.TEXT_NODE || isOriginal) && !el.hasAttribute(DATA_PSEUDO)) { + if ((el.nodeType !== Node.TEXT_NODE || isOriginal)) { if (isOriginal) { - calculatedIndex = parseInt(maybeIndexOfOriginal); + calculatedIndex + = parseInt(maybeIndexOfOriginal) + 1; // + 1 because the textnode itself must be counted too foundWrapper = true; } else { calculatedIndex++;