@perlego-oss/text-highlighter
Advanced tools
Comparing version 2.1.0-alpha.10 to 2.2.0
@@ -118,2 +118,11 @@ "use strict"; | ||
var rangeRelativeToRootElement = (0, _highlights.extractRangeRelativeToRootElement)(range, this.el); | ||
if (!rangeRelativeToRootElement) { | ||
return; | ||
} | ||
console.log({ | ||
rangeRelativeToRootElement: rangeRelativeToRootElement | ||
}); | ||
var eventItems = []; | ||
@@ -128,3 +137,3 @@ (0, _dom["default"])(this.el).turnOffEventHandlers(eventItems); | ||
rootElement: this.el, | ||
range: range, | ||
range: rangeRelativeToRootElement, | ||
wrapper: wrapper, | ||
@@ -136,3 +145,3 @@ excludeNodeNames: this.options.excludeNodes, | ||
var _this$options$preproc = this.options.preprocessDescriptors(range, descriptors, timestamp), | ||
var _this$options$preproc = this.options.preprocessDescriptors(rangeRelativeToRootElement, descriptors, timestamp), | ||
processedDescriptors = _this$options$preproc.descriptors, | ||
@@ -143,3 +152,3 @@ meta = _this$options$preproc.meta; | ||
this.deserializeHighlights(JSON.stringify(processedDescriptors)); | ||
this.options.onAfterHighlight(range, processedDescriptors, timestamp, meta); | ||
this.options.onAfterHighlight(rangeRelativeToRootElement, processedDescriptors, timestamp, meta); | ||
} | ||
@@ -146,0 +155,0 @@ } |
@@ -25,7 +25,6 @@ "use strict"; | ||
exports.validateIndependenciaDescriptors = validateIndependenciaDescriptors; | ||
exports.extractRangeRelativeToRootElement = extractRangeRelativeToRootElement; | ||
var _dom = _interopRequireWildcard(require("./dom")); | ||
var _priorities = require("./priorities"); | ||
var _config = require("../config"); | ||
@@ -409,2 +408,5 @@ | ||
var currentNode = startNodeOrContainer.nextSibling; | ||
console.log({ | ||
currentNodeBeforeWhile: currentNode | ||
}); | ||
@@ -832,1 +834,90 @@ while (currentNode && !foundEndNodeSibling) { | ||
} | ||
/** | ||
* Extracts a sub-range from a given window selection range | ||
* that only includes the given root element and its descendants. | ||
* | ||
* @param {RangeLite} range The current text selection range for the window. | ||
* @param {HTMLElement} rootElement The root element to extract a sub-range for. | ||
* | ||
* @returns {Range | null} The sub-range or null if rootElement is not in the text selection. | ||
*/ | ||
function extractRangeRelativeToRootElement(range, rootElement) { | ||
// It's really important that we extract sub-ranges without manipulating | ||
// the window selection as there are situations where multiple highlighters | ||
// will be extracting sub-ranges from the current selection at the same time. | ||
// This is why we won't be using Range.extractContents. | ||
var hasStartContainer = (0, _dom["default"])(rootElement).contains(range.startContainer); | ||
var hasEndContainer = (0, _dom["default"])(rootElement).contains(range.endContainer); | ||
if (!hasStartContainer && !hasEndContainer) { | ||
return null; | ||
} | ||
if (hasStartContainer && !hasEndContainer) { | ||
console.log({ | ||
range: range | ||
}); | ||
var endContainer = getLastDescendantTerminalNode(rootElement); | ||
var subRange = new Range(); | ||
subRange.setStart(range.startContainer, range.startOffset); | ||
subRange.setEnd(endContainer, endContainer.textContent.length - 1); | ||
return subRange; | ||
} | ||
if (!hasStartContainer && hasEndContainer) { | ||
console.log({ | ||
range: range | ||
}); | ||
var startContainer = getFirstDescendantTerminalNode(rootElement); | ||
var _subRange = new Range(); | ||
_subRange.setStart(startContainer, 0); | ||
_subRange.setEnd(range.endContainer, range.endOffset); | ||
return _subRange; | ||
} | ||
return range; | ||
} | ||
/** | ||
* Finds the first descendant node without any children | ||
* of it's own starting with the given root node. | ||
* | ||
* @param {Node} rootNode The root node from which to find the first descendant terminal node for. | ||
* | ||
* @returns {Node} the first descendant terminal node. | ||
*/ | ||
function getFirstDescendantTerminalNode(rootNode) { | ||
var currentNode = rootNode; | ||
while (currentNode.childNodes.length > 0) { | ||
currentNode = currentNode.childNodes[0]; | ||
} | ||
return currentNode; | ||
} | ||
/** | ||
* Finds the last descendant node without any children | ||
* of it's own starting with the given root node. | ||
* | ||
* @param {Node} rootNode The root node from which to find the last descendant terminal node for. | ||
* | ||
* @returns {Node} the last descendant terminal node. | ||
*/ | ||
function getLastDescendantTerminalNode(rootNode) { | ||
var currentNode = rootNode; | ||
while (currentNode.childNodes.length > 0) { | ||
currentNode = currentNode.childNodes[currentNode.childNodes.length - 1]; | ||
} | ||
return currentNode; | ||
} |
{ | ||
"name": "@perlego-oss/text-highlighter", | ||
"version": "2.1.0-alpha.10", | ||
"version": "2.2.0", | ||
"description": "TextHighlighter allows you to highlight text on web pages.", | ||
@@ -5,0 +5,0 @@ "dependencies": {}, |
@@ -12,2 +12,3 @@ import { | ||
findHigherPriorityHighlights, | ||
extractRangeRelativeToRootElement, | ||
} from "../utils/highlights"; | ||
@@ -100,2 +101,8 @@ import { START_OFFSET_ATTR, LENGTH_ATTR, TIMESTAMP_ATTR } from "../config"; | ||
const rangeRelativeToRootElement = extractRangeRelativeToRootElement(range, this.el); | ||
if (!rangeRelativeToRootElement) { | ||
return; | ||
} | ||
console.log({ rangeRelativeToRootElement }); | ||
let eventItems = []; | ||
@@ -111,3 +118,3 @@ dom(this.el).turnOffEventHandlers(eventItems); | ||
rootElement: this.el, | ||
range, | ||
range: rangeRelativeToRootElement, | ||
wrapper, | ||
@@ -120,3 +127,3 @@ excludeNodeNames: this.options.excludeNodes, | ||
const { descriptors: processedDescriptors, meta } = this.options.preprocessDescriptors( | ||
range, | ||
rangeRelativeToRootElement, | ||
descriptors, | ||
@@ -127,3 +134,8 @@ timestamp, | ||
this.deserializeHighlights(JSON.stringify(processedDescriptors)); | ||
this.options.onAfterHighlight(range, processedDescriptors, timestamp, meta); | ||
this.options.onAfterHighlight( | ||
rangeRelativeToRootElement, | ||
processedDescriptors, | ||
timestamp, | ||
meta, | ||
); | ||
} | ||
@@ -130,0 +142,0 @@ } |
@@ -14,6 +14,11 @@ /** | ||
* @property {string} allText | ||
* | ||
* @typedef {Object} RangeLite | ||
* @property {HTMLElement} startContainer | ||
* @property {number} startOffset | ||
* @property {HTMLElement} endContainer | ||
* @property {number} endOffset | ||
*/ | ||
import dom, { NODE_TYPE } from "./dom"; | ||
import { isHighestPriority } from "./priorities"; | ||
import { DATA_ATTR, START_OFFSET_ATTR, LENGTH_ATTR, IGNORE_TAGS } from "../config"; | ||
@@ -407,2 +412,3 @@ import { arrayToLower } from "./arrays"; | ||
let currentNode = startNodeOrContainer.nextSibling; | ||
console.log({ currentNodeBeforeWhile: currentNode }); | ||
while (currentNode && !foundEndNodeSibling) { | ||
@@ -871,1 +877,75 @@ if (currentNode === endNode || currentNode.contains(endNode)) { | ||
} | ||
/** | ||
* Extracts a sub-range from a given window selection range | ||
* that only includes the given root element and its descendants. | ||
* | ||
* @param {RangeLite} range The current text selection range for the window. | ||
* @param {HTMLElement} rootElement The root element to extract a sub-range for. | ||
* | ||
* @returns {Range | null} The sub-range or null if rootElement is not in the text selection. | ||
*/ | ||
export function extractRangeRelativeToRootElement(range, rootElement) { | ||
// It's really important that we extract sub-ranges without manipulating | ||
// the window selection as there are situations where multiple highlighters | ||
// will be extracting sub-ranges from the current selection at the same time. | ||
// This is why we won't be using Range.extractContents. | ||
const hasStartContainer = dom(rootElement).contains(range.startContainer); | ||
const hasEndContainer = dom(rootElement).contains(range.endContainer); | ||
if (!hasStartContainer && !hasEndContainer) { | ||
return null; | ||
} | ||
if (hasStartContainer && !hasEndContainer) { | ||
console.log({ range }); | ||
const endContainer = getLastDescendantTerminalNode(rootElement); | ||
const subRange = new Range(); | ||
subRange.setStart(range.startContainer, range.startOffset); | ||
subRange.setEnd(endContainer, endContainer.textContent.length - 1); | ||
return subRange; | ||
} | ||
if (!hasStartContainer && hasEndContainer) { | ||
console.log({ range }); | ||
const startContainer = getFirstDescendantTerminalNode(rootElement); | ||
const subRange = new Range(); | ||
subRange.setStart(startContainer, 0); | ||
subRange.setEnd(range.endContainer, range.endOffset); | ||
return subRange; | ||
} | ||
return range; | ||
} | ||
/** | ||
* Finds the first descendant node without any children | ||
* of it's own starting with the given root node. | ||
* | ||
* @param {Node} rootNode The root node from which to find the first descendant terminal node for. | ||
* | ||
* @returns {Node} the first descendant terminal node. | ||
*/ | ||
function getFirstDescendantTerminalNode(rootNode) { | ||
let currentNode = rootNode; | ||
while (currentNode.childNodes.length > 0) { | ||
currentNode = currentNode.childNodes[0]; | ||
} | ||
return currentNode; | ||
} | ||
/** | ||
* Finds the last descendant node without any children | ||
* of it's own starting with the given root node. | ||
* | ||
* @param {Node} rootNode The root node from which to find the last descendant terminal node for. | ||
* | ||
* @returns {Node} the last descendant terminal node. | ||
*/ | ||
function getLastDescendantTerminalNode(rootNode) { | ||
let currentNode = rootNode; | ||
while (currentNode.childNodes.length > 0) { | ||
currentNode = currentNode.childNodes[currentNode.childNodes.length - 1]; | ||
} | ||
return currentNode; | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5551070
109
161909
2
13