htmlfy
Version:
html formatter yo! Prettify or minify html.
78 lines (73 loc) • 2.49 kB
JavaScript
/**
* Enforce entity characters for textarea content.
* To also minifiy tags, pass `minify` as `true`.
*
* @param {string} html The HTML string to evaluate.
* @param {boolean} [minify] Minifies the textarea tags themselves.
* Defaults to `false`. We recommend a value of `true` if you're running `entify()`
* as a standalone function.
* @returns {string}
* @example <textarea>3 > 2</textarea> => <textarea>3 > 2</textarea>
* @example With minify.
* <textarea >3 > 2</textarea> => <textarea>3 > 2</textarea>
*/
export const entify = (html, minify = false) => {
/**
* Use entities inside textarea content.
*/
html = html.replace(/<\s*textarea[^>]*>((.|\n)*?)<s*\/\s*textarea\s*>/g, (match, capture) => {
return match.replace(capture, (match) => {
return match
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/\n/g, ' ')
.replace(/\r/g, ' ')
.replace(/\s/g, ' ')
})
})
if (minify) {
html = html.replace(/<\s*textarea[^>]*>(.|\n)*?<\s*\/\s*textarea\s*>/g, (match) => {
/* This only affects the html tags, since everything else has been entified. */
return match
.replace(/\s+/g, ' ')
.replace(/\s>/g, '>')
.replace(/>\s/g, '>')
.replace(/\s</g, '<')
.replace(/<\s/g, '<')
.replace(/<\/\s/g, '<\/')
.replace(/class=["']\s/g, (match) => match.replace(/\s/g, ''))
.replace(/(class=.*)\s(["'])/g, '$1'+'$2')
})
}
return html
}
/**
* Remove entity characters for textarea content.
* Currently internal use only.
*
* @param {string} html The HTML string to evaluate.
* @returns {string}
* @example <textarea>3 > 2</textarea> => <textarea>3 > 2</textarea>
*/
export const dentify = (html) => {
/**
* Remove entities inside textarea content.
*/
return html = html.replace(/<textarea[^>]*>((.|\n)*?)<\/textarea>/g, (match, capture) => {
return match.replace(capture, (match) => {
match = match
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/ /g, '\n')
.replace(/ /g, '\r')
.replace(/ /g, ' ')
// Ensure we collapse consecutive spaces, or they'll be completely removed later.
.replace(/\s+/g, ' ')
return match
})
})
}