html-format
Advanced tools
Comparing version 1.1.2 to 1.1.3
49
index.js
@@ -88,29 +88,33 @@ const tagName = String.raw`[A-Za-z][^/\s>]*`; | ||
let pendingIndent = false; | ||
let lineLength = 0; | ||
let span = ""; | ||
let spanLevel = 0; | ||
let lastSpace = ""; | ||
const addOutput = (/** @type {string[]} */ ...args) => { | ||
for (const s of args) { | ||
if (!specialElement) { | ||
if (s == "\n") pendingIndent = true; | ||
else { | ||
const newline = s.indexOf("\n"); | ||
const len = newline == -1 ? s.length : newline; | ||
if ( | ||
lineLength + len > width && | ||
/^[ \t]+$/.test(output[output.length - 1]) | ||
) { | ||
output.pop(); | ||
addOutput("\n"); | ||
} | ||
if (pendingIndent) { | ||
pendingIndent = false; | ||
addOutput(indent.repeat(level)); | ||
} | ||
let out = ""; | ||
if (!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 = ""; | ||
lastSpace = s; | ||
} else { | ||
if (!span) spanLevel = level; | ||
span += s; | ||
} | ||
const pos = s.lastIndexOf("\n"); | ||
if (pos == -1) lineLength += s.length; | ||
else lineLength = s.length - pos - 1; | ||
output.push(s); | ||
if (out) { | ||
const pos = out.lastIndexOf("\n"); | ||
if (pos == -1) lineLength += out.length; | ||
else lineLength = out.length - pos - 1; | ||
output.push(out); | ||
} | ||
} | ||
@@ -206,2 +210,5 @@ }; | ||
// Flush remaining output | ||
addOutput(""); | ||
let newline = false; | ||
@@ -208,0 +215,0 @@ while (/^\s+$/.test(output[output.length - 1])) { |
{ | ||
"name": "html-format", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Format HTML strings.", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"typescript": "^5.0.4" | ||
"typescript": "^5.3.3" | ||
}, | ||
@@ -14,0 +14,0 @@ "scripts": { |
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
9287
196