Socket
Socket
Sign inDemoInstall

@perlego-oss/text-highlighter

Package Overview
Dependencies
0
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.1.0-alpha.6 to 2.1.0-alpha.7

28

cjs/utils/dom.js

@@ -229,2 +229,30 @@ "use strict";

/**
* Traverses up the tree to to get the previous closest sibling of a node
* or any of it's parents.
*
* This is used in scenarios where you have already consumed the parents while
* traversing the tree but not the siblings of parents.
*
* @param {HTMLElement | undefined} rootNode The root node which acts as a threshold
* for how deep we can go in the tree when getting siblings or their parents.
*
* @returns {HTMLElement | null}
*/
previousClosestSibling: function previousClosestSibling(rootNode) {
var current = el;
var prevClosestSibling;
do {
prevClosestSibling = current.previousSibling;
current = current.parentNode;
} while (!prevClosestSibling && current.parentNode && rootNode.contains(current));
if (!rootNode.contains(current)) {
prevClosestSibling = null;
}
return prevClosestSibling;
},
/**
* Normalizes text nodes within base element, ie. merges sibling text nodes and assures that every

@@ -231,0 +259,0 @@ * element node has only one text node.

54

cjs/utils/highlights.js

@@ -161,18 +161,58 @@ "use strict";

* Deals with normalising text for when carriage returns and white space
* that directly follows or when white space and white space that directly
* that directly follows should be ignored.
*
* @param {string} text
*/
function normaliseText(text) {
return text.replace(/((\r\n|\n\r|\n|\r)\s*)/g, "");
}
/**
* Deals with normalising text for when carriage returns and white space
* that directly follows or when white space and white space that directly
* follows should be ignored.
*
* @param {string} text
*
* @returns {string}
*/
function normaliseText(text) {
function normaliseTextWithLeadingSpaces(text) {
return text.replace(/(((\r\n|\n\r|\n|\r)\s*)|(^\s+))/g, "");
}
/**
* Checks whether previous siblings end with a carriage return followed by
* zero or more whitespaces and selects the function that should be used to
* normalise the text.
*
* @param {Node} node
* @param {Node} parentNode
* @param {string} text
*
* @returns {string}
*/
function normaliseBasedOnPrevSibling(node, parentNode, text) {
var prevNode = (0, _dom["default"])(node).previousClosestSibling(parentNode);
if (prevNode) {
var matchRegex = /((\r\n|\n\r|\n|\r)\s*)$/;
var matches = matchRegex.test(prevNode.textContent);
if (matches) {
return normaliseTextWithLeadingSpaces(text);
}
}
return normaliseText(text);
}
/**
*
* @param {number} offsetWithinNode
* @param {string} text
*
* @return {number}
* @returns {number}
*/

@@ -222,3 +262,3 @@

var textContent = textContentExcludingTags(currentNode, excludeNodeNames);
var reducedTextContent = excludeWhiteSpaceAndReturns ? normaliseText(textContent) : "";
var reducedTextContent = excludeWhiteSpaceAndReturns ? normaliseBasedOnPrevSibling(currentNode, parentNode, textContent) : "";

@@ -230,3 +270,3 @@ if (currentNode == parentNode) {

var textLength = textContent.length;
var normalisedTextLength = normaliseText(textContent).length;
var normalisedTextLength = normaliseBasedOnPrevSibling(currentNode, parentNode, textContent).length;
var endOfCurrentNodeOffset = currentOffset + textLength;

@@ -248,3 +288,3 @@ var normalisedEOCNodeOffset = excludeWhiteSpaceAndReturns ? currentOffset + normalisedTextLength : endOfCurrentNodeOffset;

var textFromNormalisedOffset = textContent.substr(normalisedOffset);
var charactersToIgnoreInside = excludeWhiteSpaceAndReturns ? textFromNormalisedOffset.length - normaliseText(textFromNormalisedOffset).length : 0;
var charactersToIgnoreInside = excludeWhiteSpaceAndReturns ? textFromNormalisedOffset.length - normaliseBasedOnPrevSibling(currentNode, parentNode, textFromNormalisedOffset).length : 0;
var nextNodeOffset = endOfCurrentNodeOffset - normalisedOffsetDiff - charactersToIgnoreInside;

@@ -264,3 +304,3 @@ var lengthInHighlight = highlightEndOffset >= nextNodeOffset ? textLength - offsetWithinNode : // While counting the actual amount of text in the DOM node, we need to retain

length: normalisedLengthInHighlight,
normalisedText: excludeWhiteSpaceAndReturns ? normaliseText(currentNode.textContent) : currentNode.textContent
normalisedText: excludeWhiteSpaceAndReturns ? normaliseBasedOnPrevSibling(currentNode, parentNode, currentNode.textContent) : currentNode.textContent
});

@@ -267,0 +307,0 @@ }

2

package.json
{
"name": "@perlego-oss/text-highlighter",
"version": "2.1.0-alpha.6",
"version": "2.1.0-alpha.7",
"description": "TextHighlighter allows you to highlight text on web pages.",

@@ -5,0 +5,0 @@ "dependencies": {},

@@ -204,2 +204,29 @@ import { isElementHighlight } from "./highlights";

/**
* Traverses up the tree to to get the previous closest sibling of a node
* or any of it's parents.
*
* This is used in scenarios where you have already consumed the parents while
* traversing the tree but not the siblings of parents.
*
* @param {HTMLElement | undefined} rootNode The root node which acts as a threshold
* for how deep we can go in the tree when getting siblings or their parents.
*
* @returns {HTMLElement | null}
*/
previousClosestSibling: function(rootNode) {
let current = el;
let prevClosestSibling;
do {
prevClosestSibling = current.previousSibling;
current = current.parentNode;
} while (!prevClosestSibling && current.parentNode && rootNode.contains(current));
if (!rootNode.contains(current)) {
prevClosestSibling = null;
}
return prevClosestSibling;
},
/**
* Normalizes text nodes within base element, ie. merges sibling text nodes and assures that every

@@ -206,0 +233,0 @@ * element node has only one text node.

@@ -134,8 +134,21 @@ /**

* Deals with normalising text for when carriage returns and white space
* that directly follows or when white space and white space that directly
* that directly follows should be ignored.
*
* @param {string} text
*/
function normaliseText(text) {
return text.replace(/((\r\n|\n\r|\n|\r)\s*)/g, "");
}
/**
* Deals with normalising text for when carriage returns and white space
* that directly follows or when white space and white space that directly
* follows should be ignored.
*
* @param {string} text
*
* @returns {string}
*/
function normaliseText(text) {
function normaliseTextWithLeadingSpaces(text) {
return text.replace(/(((\r\n|\n\r|\n|\r)\s*)|(^\s+))/g, "");

@@ -145,7 +158,31 @@ }

/**
* Checks whether previous siblings end with a carriage return followed by
* zero or more whitespaces and selects the function that should be used to
* normalise the text.
*
* @param {Node} node
* @param {Node} parentNode
* @param {string} text
*
* @returns {string}
*/
function normaliseBasedOnPrevSibling(node, parentNode, text) {
const prevNode = dom(node).previousClosestSibling(parentNode);
if (prevNode) {
const matchRegex = /((\r\n|\n\r|\n|\r)\s*)$/;
const matches = matchRegex.test(prevNode.textContent);
if (matches) {
return normaliseTextWithLeadingSpaces(text);
}
}
return normaliseText(text);
}
/**
*
* @param {number} offsetWithinNode
* @param {string} text
*
* @return {number}
* @returns {number}
*/

@@ -193,3 +230,5 @@ function normaliseOffset(offsetWithinNode, text) {

const textContent = textContentExcludingTags(currentNode, excludeNodeNames);
const reducedTextContent = excludeWhiteSpaceAndReturns ? normaliseText(textContent) : "";
const reducedTextContent = excludeWhiteSpaceAndReturns
? normaliseBasedOnPrevSibling(currentNode, parentNode, textContent)
: "";

@@ -200,3 +239,4 @@ if (currentNode == parentNode) {

const textLength = textContent.length;
const normalisedTextLength = normaliseText(textContent).length;
const normalisedTextLength = normaliseBasedOnPrevSibling(currentNode, parentNode, textContent)
.length;
const endOfCurrentNodeOffset = currentOffset + textLength;

@@ -226,3 +266,5 @@ const normalisedEOCNodeOffset = excludeWhiteSpaceAndReturns

const charactersToIgnoreInside = excludeWhiteSpaceAndReturns
? textFromNormalisedOffset.length - normaliseText(textFromNormalisedOffset).length
? textFromNormalisedOffset.length -
normaliseBasedOnPrevSibling(currentNode, parentNode, textFromNormalisedOffset)
.length
: 0;

@@ -254,3 +296,3 @@

normalisedText: excludeWhiteSpaceAndReturns
? normaliseText(currentNode.textContent)
? normaliseBasedOnPrevSibling(currentNode, parentNode, currentNode.textContent)
: currentNode.textContent,

@@ -257,0 +299,0 @@ });

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc