aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHenry Jameson <me@hjkos.com>2019-11-14 22:40:20 +0200
committerHenry Jameson <me@hjkos.com>2019-11-14 22:40:20 +0200
commitbd2a682b83743311645241fe644e853e1a359b67 (patch)
tree18a7b1f65d059f819da1e30adee2351029feb18b /src
parent51ea295704c52b1f9a922868aedf264e53a5ec92 (diff)
tests + updates
Diffstat (limited to 'src')
-rw-r--r--src/services/tiny_post_html_processor/tiny_post_html_processor.service.js19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/services/tiny_post_html_processor/tiny_post_html_processor.service.js b/src/services/tiny_post_html_processor/tiny_post_html_processor.service.js
index b96c1ccf..de6f20ef 100644
--- a/src/services/tiny_post_html_processor/tiny_post_html_processor.service.js
+++ b/src/services/tiny_post_html_processor/tiny_post_html_processor.service.js
@@ -2,6 +2,8 @@
* This is a tiny purpose-built HTML parser/processor. This basically detects any type of visual newline and
* allows it to be processed, useful for greentexting, mostly
*
+ * known issue: doesn't handle CDATA so nested CDATA might not work well
+ *
* @param {Object} input - input data
* @param {(string) => string} processor - function that will be called on every line
* @return {string} processed html
@@ -22,11 +24,15 @@ export const processHtml = (html, processor) => {
}
const flush = () => { // Processes current line buffer, adds it to output buffer and clears line buffer
- buffer += processor(textBuffer)
+ if (textBuffer.trim().length > 0) {
+ buffer += processor(textBuffer)
+ } else {
+ buffer += textBuffer
+ }
textBuffer = ''
}
- const handleBr = (tag) => { // handles single newlines/linebreaks
+ const handleBr = (tag) => { // handles single newlines/linebreaks/selfclosing
flush()
buffer += tag
}
@@ -59,10 +65,12 @@ export const processHtml = (html, processor) => {
if (handledTags.has(tagName)) {
if (tagName === 'br') {
handleBr(tagFull)
- }
- if (openCloseTags.has(tagFull)) {
+ } else if (openCloseTags.has(tagName)) {
if (tagFull[1] === '/') {
handleClose(tagFull)
+ } else if (tagFull[tagFull.length - 2] === '/') {
+ // self-closing
+ handleBr(tagFull)
} else {
handleOpen(tagFull)
}
@@ -76,6 +84,9 @@ export const processHtml = (html, processor) => {
textBuffer += char
}
}
+ if (tagBuffer) {
+ textBuffer += tagBuffer
+ }
flush()