html-format
Advanced tools
Comparing version 1.1.5 to 1.1.6
45
index.js
@@ -93,16 +93,26 @@ const tagName = String.raw`[A-Za-z][^/\s>]*`; | ||
const flushOutput = () => { | ||
if (lastSpace && lastSpace != "\n") { | ||
const newline = span.indexOf("\n"); | ||
const len = newline == -1 ? span.length : newline; | ||
if (lineLength + lastSpace.length + len > width) lastSpace = "\n"; | ||
} | ||
const ind = lastSpace == "\n" && span ? indent.repeat(spanLevel) : ""; | ||
const out = `${lastSpace}${ind}${span}`; | ||
if (out) { | ||
const pos = out.lastIndexOf("\n"); | ||
if (pos == -1) lineLength += out.length; | ||
else lineLength = out.length - pos - 1; | ||
output.push(out); | ||
} | ||
span = lastSpace = ""; | ||
}; | ||
const addOutput = (/** @type {string[]} */ ...args) => { | ||
for (const s of args) { | ||
let out = ""; | ||
if (!s || (!specialElement && /^\s+$/.test(s))) { | ||
const newline = span.indexOf("\n"); | ||
const len = newline == -1 ? span.length : newline; | ||
if (lastSpace) { | ||
if (lineLength + lastSpace.length + len > width) lastSpace = "\n"; | ||
out += lastSpace; | ||
if (lastSpace == "\n" && span) out += indent.repeat(spanLevel); | ||
} | ||
out += span; | ||
span = ""; | ||
if (!specialElement && /^\s+$/.test(s)) { | ||
flushOutput(); | ||
lastSpace = s; | ||
@@ -113,9 +123,2 @@ } else { | ||
} | ||
if (out) { | ||
const pos = out.lastIndexOf("\n"); | ||
if (pos == -1) lineLength += out.length; | ||
else lineLength = out.length - pos - 1; | ||
output.push(out); | ||
} | ||
} | ||
@@ -205,3 +208,3 @@ }; | ||
if (hasClosingSlash || voidTags.has(tagName)) --level; | ||
else if (["pre", "script", "style"].includes(tagName)) | ||
else if (["pre", "textarea", "script", "style"].includes(tagName)) | ||
specialElement = tagName; | ||
@@ -213,3 +216,3 @@ } | ||
// Flush remaining output | ||
addOutput(""); | ||
flushOutput(); | ||
@@ -216,0 +219,0 @@ let newline = false; |
{ | ||
"name": "html-format", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "Format HTML strings.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9337
197