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

@dreamworld/web-util

Package Overview
Dependencies
Maintainers
4
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dreamworld/web-util - npm Package Compare versions

Comparing version 1.3.0-html-trim.4 to 1.3.0-html-trim.5

50

htmlTrim.js
import textToHtml from './textToHtml';
/**
* Remove empty nodes from given html as an argument.
* Removes empty nodes/html from the beginning of a given html template.
* Removes whitespace from beginning of a first non-empty nodes/html.
* @param {*} html The template string OR The template node
* @param {Boolean} deep Deeply remove the empty child or not.
* @returns {html} html stripped of empty nodes from beginning.
*/
const removeEmptyNodes = (html, deep) => {
export const htmlTrimStart = (html) => {
if (typeof html === 'string') {

@@ -14,9 +15,9 @@ html = textToHtml(html);

let children = Array.from(html.children || []);
let bRemoveChild = true;
children.forEach(el => {
if (el.textContent.trim() === "") {
if (bRemoveChild && el.textContent.trim() === "") {
html.removeChild(el)
} else {
if (deep && el && el.children && el.children.length > 0) {
removeEmptyNodes(el, deep);
}
el.textContent = el.textContent.trimStart();
bRemoveChild = false;
}

@@ -29,7 +30,33 @@ });

/**
* Remove empty nodes from given html as an argument.
* Removes empty nodes/html from the end of a given html template.
* Removes whitespace from end of a last non-empty nodes/html.
* @param {*} html The template string OR The template node
* @param {Boolean} deep Deeply remove the empty child or not.
* @returns {html} html stripped of empty nodes from end.
*/
export const htmlTrim = (html, deep) => {
export const htmlTrimEnd = (html) => {
if (typeof html === 'string') {
html = textToHtml(html);
}
let children = Array.from(html.children || []).reverse();
let bRemoveChild = true;
bRemoveChild = true;
children.forEach(el => {
if (bRemoveChild && el.textContent.trim() === "") {
html.removeChild(el)
} else {
el.textContent = el.textContent.trimEnd();
bRemoveChild = false;
}
});
return html;
}
/**
* Removes empty nodes/html from the both side of a given html template.
* @param {*} html The template string OR The template node.
* @returns {html} html stripped of empty nodes from both ends.
*/
export const htmlTrim = (html) => {
if (!html) {

@@ -46,3 +73,4 @@ return '';

}
html = removeEmptyNodes(html, deep);
html = htmlTrimStart(html);
html = htmlTrimEnd(html);
return html.innerHTML;

@@ -49,0 +77,0 @@ }

{
"name": "@dreamworld/web-util",
"version": "1.3.0-html-trim.4",
"version": "1.3.0-html-trim.5",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",

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