Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

degausser

Package Overview
Dependencies
Maintainers
3
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

degausser - npm Package Compare versions

Comparing version 2.3.2 to 2.4.0

83

dist/degausser.js

@@ -294,3 +294,38 @@ (function (global, factory) {

};
/**
* Gets the alt text from an element, if it exists, otherwise returns placeholder alt text composed of 100 unit separator character.
* If node has empty alt attribute or alt attribute with empty string, this will return the placeholder alt text instead.
* @param node
* @param placeholderCharacter
* @param placeholderLength
* @returns {string}
*/
var getAltText = function getAltText(node, placeholderCharacter, placeholderLength) {
var altText = node.getAttribute('alt');
if (!altText) {
var altTextPlaceholder = placeholderCharacter.repeat(placeholderLength);
return altTextPlaceholder;
}
return altText;
};
/**
* Checks if element with given tagname can have an alt attribute.
* @param tagName
* @returns {boolean}
*/
var elementCanHaveAltText = function elementCanHaveAltText(tagName) {
if (!tagName) {
return false;
}
var tagNameLowerCase = tagName.toLowerCase();
return tagNameLowerCase === 'img' || tagNameLowerCase === 'image' || tagNameLowerCase === 'area';
};
var StringCollector = /*#__PURE__*/function () {

@@ -399,5 +434,6 @@ function StringCollector() {

if (node.hasAttribute('alt')) {
if (elementCanHaveAltText(node.tagName)) {
this.processBreaks();
this.text.push(" ".concat(node.getAttribute('alt'), " "));
var altText = getAltText(node, this.options.placeholderString, this.options.placeholderCopies);
this.text.push(" ".concat(altText, " "));
return true;

@@ -649,7 +685,8 @@ }

if (node.hasAttribute('alt')) {
if (elementCanHaveAltText(node.tagName)) {
this.processBreaks();
var altText = getAltText(node, this.options.placeholderString, this.options.placeholderCopies);
this.text.push({
node: node,
string: " ".concat(node.getAttribute('alt'), " ")
string: " ".concat(altText, " ")
});

@@ -741,4 +778,11 @@ return true;

if (entity.node.nodeType === Node.TEXT_NODE || entity.node.tagName === 'img') {
var nodeContent = entity.node.tagName === 'img' ? entity.node.getAttribute('alt').normalize() : entity.node.textContent.normalize();
var nodeContent = void 0;
if (elementCanHaveAltText(entity.node.tagName)) {
var altText = getAltText(entity.node, this.options.placeholderString, this.options.placeholderCopies).normalize();
nodeContent = altText;
} else {
nodeContent = entity.node.textContent.normalize();
}
for (var charInMap = 0, charInNode = 0; charInNode < nodeContent.length; ++charInNode) {

@@ -851,8 +895,24 @@ var isEqual = entity.content.charAt(charInMap) === nodeContent.charAt(charInNode);

/**
* Extracts text from the given node.
* Options include (but are not limited to):
* - placeholderString: string to take the place of alt text when alt it is empty/undefined
* - placeholderCopies: the number of times placeholderString repeats
* @param parentNode
* @param options
* @returns {*}
*/
var degausser = function degausser(parentNode) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var collector = new StringCollector(options);
var unitSeparatorCode = 31;
var defaultOptions = {
placeholderString: String.fromCharCode(unitSeparatorCode),
placeholderCopies: 100
};
var finalOptions = Object.assign(defaultOptions, options);
var collector = new StringCollector(finalOptions);
if (options.map) {
collector = new MapCollector(options);
if (finalOptions.map) {
collector = new MapCollector(finalOptions);
}

@@ -865,2 +925,3 @@

var map = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
var docType = doc.nodeType;

@@ -873,5 +934,5 @@

if (map === null) {
map = degausser(doc, {
map: true
});
var finalOptions = Object.assign({}, options);
finalOptions.map = true;
map = degausser(doc, finalOptions);
}

@@ -878,0 +939,0 @@

2

package.json
{
"name": "degausser",
"version": "2.3.2",
"version": "2.4.0",
"description": "Transforms HTML to plain text by eliminating tags from a document.",

@@ -5,0 +5,0 @@ "author": "FlowPub",

@@ -5,7 +5,23 @@ import { StringCollector } from './stringCollector'

/**
* Extracts text from the given node.
* Options include (but are not limited to):
* - placeholderString: string to take the place of alt text when alt it is empty/undefined
* - placeholderCopies: the number of times placeholderString repeats
* @param parentNode
* @param options
* @returns {*}
*/
export const degausser = (parentNode, options = {}) => {
let collector = new StringCollector(options)
const unitSeparatorCode = 31
const defaultOptions = {
placeholderString: String.fromCharCode(unitSeparatorCode),
placeholderCopies: 100,
}
const finalOptions = Object.assign(defaultOptions, options)
if (options.map) {
collector = new MapCollector(options)
let collector = new StringCollector(finalOptions)
if (finalOptions.map) {
collector = new MapCollector(finalOptions)
}

@@ -16,3 +32,3 @@

export const getRangeFromOffset = (start, end, doc = document, map = null) => {
export const getRangeFromOffset = (start, end, doc = document, map = null, options = {}) => {
const docType = doc.nodeType

@@ -27,3 +43,5 @@ if (

if (map === null) {
map = degausser(doc, { map: true })
const finalOptions = Object.assign({}, options)
finalOptions.map = true
map = degausser(doc, finalOptions)
}

@@ -30,0 +48,0 @@

@@ -9,2 +9,4 @@ import {

isElementBlacklisted,
getAltText,
elementCanHaveAltText,
} from './util'

@@ -185,6 +187,8 @@

if (node.hasAttribute('alt')) {
if (elementCanHaveAltText(node.tagName)) {
this.processBreaks()
this.text.push({ node, string: ` ${node.getAttribute('alt')} ` })
const altText = getAltText(node, this.options.placeholderString, this.options.placeholderCopies)
this.text.push({ node, string: ` ${altText} ` })
return true

@@ -269,6 +273,13 @@ }

) {
const nodeContent =
entity.node.tagName === 'img'
? entity.node.getAttribute('alt').normalize()
: entity.node.textContent.normalize()
let nodeContent
if (elementCanHaveAltText(entity.node.tagName)) {
const altText = getAltText(
entity.node,
this.options.placeholderString,
this.options.placeholderCopies
).normalize()
nodeContent = altText
} else {
nodeContent = entity.node.textContent.normalize()
}

@@ -275,0 +286,0 @@ for (

@@ -8,2 +8,4 @@ import {

isElementBlacklisted,
getAltText,
elementCanHaveAltText,
} from './util'

@@ -116,6 +118,12 @@

if (node.hasAttribute('alt')) {
if (elementCanHaveAltText(node.tagName)) {
this.processBreaks()
this.text.push(` ${node.getAttribute('alt')} `)
const altText = getAltText(
node,
this.options.placeholderString,
this.options.placeholderCopies
)
this.text.push(` ${altText} `)
return true

@@ -122,0 +130,0 @@ }

@@ -217,2 +217,34 @@ function autoBind() {

/**
* Gets the alt text from an element, if it exists, otherwise returns placeholder alt text composed of 100 unit separator character.
* If node has empty alt attribute or alt attribute with empty string, this will return the placeholder alt text instead.
* @param node
* @param placeholderCharacter
* @param placeholderLength
* @returns {string}
*/
const getAltText = (node, placeholderCharacter, placeholderLength) => {
const altText = node.getAttribute('alt')
if (!altText) {
const altTextPlaceholder = placeholderCharacter.repeat(placeholderLength)
return altTextPlaceholder
}
return altText
}
/**
* Checks if element with given tagname can have an alt attribute.
* @param tagName
* @returns {boolean}
*/
const elementCanHaveAltText = (tagName) => {
if (!tagName) {
return false
}
const tagNameLowerCase = tagName.toLowerCase()
return tagNameLowerCase === 'img' || tagNameLowerCase === 'image' || tagNameLowerCase === 'area'
}
export {

@@ -227,2 +259,4 @@ autoBind,

isCharWhitespace,
getAltText,
elementCanHaveAltText,
}
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc