From 2d06e0dabfedcb49fb9f01b3a182ba41f831f9fc Mon Sep 17 00:00:00 2001 From: oleks Date: Tue, 21 May 2019 21:13:55 +0300 Subject: [PATCH] parser changed (has issues) --- src/convert/bTagHandler.js | 1 + src/convert/boldFontStyleHandler.js | 1 + src/convert/divOrPTagsHandler.js | 1 + src/convert/index.js | 13 +++++++++---- src/convert/notagHandler.js | 2 +- test/convert.js | 25 +++++++++++++++++-------- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/convert/bTagHandler.js b/src/convert/bTagHandler.js index 7d26191..b255154 100644 --- a/src/convert/bTagHandler.js +++ b/src/convert/bTagHandler.js @@ -1,4 +1,5 @@ module.exports = { + name: 'bTagHandler', stackName: 'bold', checkIsAllowed: (context, {tag}) => tag === 'b', before: (context) => { diff --git a/src/convert/boldFontStyleHandler.js b/src/convert/boldFontStyleHandler.js index 75eefae..cfa735a 100644 --- a/src/convert/boldFontStyleHandler.js +++ b/src/convert/boldFontStyleHandler.js @@ -3,6 +3,7 @@ const bTagHandler = require('./bTagHandler'); module.exports = { ...bTagHandler, + name: 'boldFontStyleHandler', checkIsAllowed: (context, {node}) => checkIsStyleBold(node), sanitizeHtml: { allowedAttributes: { diff --git a/src/convert/divOrPTagsHandler.js b/src/convert/divOrPTagsHandler.js index edfc8b6..e958abf 100644 --- a/src/convert/divOrPTagsHandler.js +++ b/src/convert/divOrPTagsHandler.js @@ -1,4 +1,5 @@ module.exports = { + name: 'dirOrPTagsHandler', checkIsAllowed: (context, {tag}) => ['div', 'p'].includes(tag), after: (context) => { context.commands.push({name: 'newLine'}); diff --git a/src/convert/index.js b/src/convert/index.js index 377ff71..28ee079 100644 --- a/src/convert/index.js +++ b/src/convert/index.js @@ -87,9 +87,10 @@ const process = async (context, node, depth) => { } } // remove notagHandler if found anything - if(handlers.length > 1) { - handlers.pop(); - } + // if(handlers.length > 1) { + // handlers.pop(); + // } + for(let i=0; i { normalizeWhitespace: true, xmlMode: true, }).root()[0].children[0]; + const nodes = root.children; let context = {data: [], commands: [], stack: {}}; for(let i=0; i sanitizers.reduce((acc, val) => { }; }, {}); +const collabsSpaces = (html) => html.replace(/(<(pre|script|style|textarea|p|div|span)[^]+?<\/\2)|(^|>)\s+|\s+(?=<|$)/g, "$1$3") + module.exports = function(dirtyXml) { const sanitizerObject = reduceSanitizeHtml(handlersCollection.map(handler => handler.sanitizeHtml)); - const cleanXml = sanitizeHtml(dirtyXml, sanitizerObject); + const cleanXml = collabsSpaces(sanitizeHtml(dirtyXml, sanitizerObject)); + console.log(cleanXml); // const cleanXml = dirtyXml; // const result = convert(cleanXml); // console.log('-----'); diff --git a/src/convert/notagHandler.js b/src/convert/notagHandler.js index a0374d4..fffe378 100644 --- a/src/convert/notagHandler.js +++ b/src/convert/notagHandler.js @@ -6,7 +6,7 @@ module.exports = { context.commands.push({name: 'print', data: node.data}); return context; } else if(!isHasNestedTagsPresent) { - context.commands.push({name: 'println', data: node.data}); + // context.commands.push({name: 'println', data: node.data}); return context; } return null; diff --git a/test/convert.js b/test/convert.js index 6ec3a1b..9b6ccaf 100644 --- a/test/convert.js +++ b/test/convert.js @@ -536,11 +536,10 @@ me

describe('right tag', async () => { it('right tag works', async () => { const template = ` - -

dsdasdas

-
oneone
-
-`; + +

dsdasdas

+
oneone
+
`; const exptectedResult = [ {name: 'alignRight'}, @@ -558,8 +557,8 @@ me

it('right tag works inside text', async () => { const template = ` -

dsd RRR asdas

-
oneone
+

dsd RRR asdas

+
oneone
`; const exptectedResult = [ @@ -576,7 +575,6 @@ me

{name: 'bold', data: false}, {name: 'newLine'}, ]; - assert.deepStrictEqual(await convert(template), exptectedResult); }); }); @@ -698,6 +696,17 @@ me

{name: "newLine"}, ]; +// const issuet = ` +// +//

1

+//
2
+//
+//

3

+// `; +// +// console.log(await convert(issuet)); +// process.exit(-1); + assert.deepStrictEqual(await convert(template), exptectedResult); });