html-format
Advanced tools
Comparing version 1.0.0 to 1.0.1
72
index.js
@@ -49,8 +49,8 @@ "use strict"; | ||
'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr', | ||
'!doctype', '!--' | ||
'!doctype', '--' | ||
]; | ||
function parseTokenValue(value) { | ||
let tagName = value.replace(/^<\/?|>$/g, '').toLowerCase(); | ||
if (tagName.startsWith('!--')) | ||
tagName = '!--'; | ||
if (tagName.startsWith('!--') || tagName.endsWith('--')) | ||
tagName = '--'; | ||
const isTagStart = /</.test(value); | ||
@@ -68,3 +68,5 @@ const isTagEnd = />/.test(value); | ||
let inEndTag = false; | ||
let inPre = false; | ||
let inSpecialElement = false; | ||
let tag = ''; | ||
let indentLevel = 0; | ||
@@ -80,25 +82,34 @@ let prevState = {}; | ||
// Token adjustments for edge cases | ||
// Remove space before tag name | ||
if (isTagStart && !tagName) { | ||
i = token.end; | ||
token = nextToken(html, i); | ||
if (!token) | ||
break; | ||
tokenValue += token.value; | ||
({ isTagStart, isTagEnd, isStartTag, isEndTag, tagName } = | ||
parseTokenValue(tokenValue)); | ||
} | ||
// Split attributes stuck together | ||
if (!isTagStart && (inStartTag || inEndTag)) { | ||
// If attribute has end quote followed by another attribute | ||
const regex = /[^=]"[^>]/g; | ||
const res = regex.exec(tokenValue); | ||
if (res && token.end != token.start + res.index + 2) { | ||
token.end = token.start + res.index + 2; | ||
tokenValue = token.value; | ||
if (!inSpecialElement) { | ||
// Remove space before tag name | ||
if (isTagStart && !tagName) { | ||
i = token.end; | ||
token = nextToken(html, i); | ||
if (!token) | ||
break; | ||
tokenValue += token.value; | ||
({ isTagStart, isTagEnd, isStartTag, isEndTag, tagName } = | ||
parseTokenValue(tokenValue)); | ||
pendingWhitespace = indent; | ||
} | ||
// Split attributes stuck together | ||
if (!isTagStart && (inStartTag || inEndTag)) { | ||
// If attribute has end quote followed by another attribute | ||
const regex = /[^=]"[^>]/g; | ||
const res = regex.exec(tokenValue); | ||
if (res && token.end != token.start + res.index + 2) { | ||
token.end = token.start + res.index + 2; | ||
tokenValue = token.value; | ||
({ isTagStart, isTagEnd, isStartTag, isEndTag, tagName } = | ||
parseTokenValue(tokenValue)); | ||
pendingWhitespace = indent; | ||
} | ||
} | ||
} | ||
if (!inSpecialElement && isStartTag) | ||
tag = tagName; | ||
const isEndSpecialTag = ((isEndTag && tagName != '--') || (isTagEnd && tagName == '--')) && | ||
tagName == tag; | ||
// Ignore any tags inside special elements | ||
if (inSpecialElement && !isEndSpecialTag) | ||
isTagStart = isTagEnd = isStartTag = isEndTag = false; | ||
// Current State | ||
@@ -111,4 +122,4 @@ if (isStartTag) | ||
--indentLevel; | ||
if (isTagStart && ['pre', 'script', 'style'].includes(tagName)) | ||
inSpecialElement = !inSpecialElement; | ||
const isStartSpecialTag = (inStartTag && isTagEnd && ['script', 'style'].includes(tag)) || | ||
(isStartTag && tag == '--'); | ||
// Convenience | ||
@@ -118,4 +129,5 @@ const inTag = inStartTag || inEndTag; | ||
const whitespace = tokenWhitespaceValue || prevState.pendingWhitespace; | ||
const ignoreSpace = inTag && (/^[=">]/.test(tokenValue) || /(^|=)"$/.test(prevState.tokenValue)); | ||
if (inSpecialElement && !inTag) | ||
const ignoreSpace = inTag && (/^[=">]([^=]|$)/.test(tokenValue) || /(^|=)"$/.test(prevState.tokenValue)); | ||
// Preserve whitespace inside special and pre elements | ||
if (inSpecialElement || inPre) | ||
output.push(tokenWhitespaceValue); | ||
@@ -139,2 +151,10 @@ else if (whitespace && !ignoreSpace) { | ||
// Next state | ||
if (isStartSpecialTag) | ||
inSpecialElement = true; | ||
if (isEndSpecialTag) | ||
inSpecialElement = false; | ||
if (inStartTag && isTagEnd && tag == 'pre') | ||
inPre = true; | ||
if (isEndTag && tagName == 'pre') | ||
inPre = false; | ||
if (inStartTag && isTagEnd && !inEndTag) // A void tag is both start & end | ||
@@ -141,0 +161,0 @@ ++indentLevel; |
{ | ||
"name": "html-format", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Format HTML strings.", | ||
@@ -12,6 +12,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"@types/node": "^10.0.4", | ||
"ts-node": "^6.0.3", | ||
"@types/node": "^10.5.2", | ||
"ts-node": "^7.0.0", | ||
"tslint": "^5.10.0", | ||
"typescript": "^2.8.3" | ||
"typescript": "^2.9.2" | ||
}, | ||
@@ -18,0 +18,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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8929
167
0