Skip to content

Commit

Permalink
parser changed (has issues)
Browse files Browse the repository at this point in the history
  • Loading branch information
knyga committed May 21, 2019
1 parent e2f9770 commit 2d06e0d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/convert/bTagHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
name: 'bTagHandler',
stackName: 'bold',
checkIsAllowed: (context, {tag}) => tag === 'b',
before: (context) => {
Expand Down
1 change: 1 addition & 0 deletions src/convert/boldFontStyleHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const bTagHandler = require('./bTagHandler');

module.exports = {
...bTagHandler,
name: 'boldFontStyleHandler',
checkIsAllowed: (context, {node}) => checkIsStyleBold(node),
sanitizeHtml: {
allowedAttributes: {
Expand Down
1 change: 1 addition & 0 deletions src/convert/divOrPTagsHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
name: 'dirOrPTagsHandler',
checkIsAllowed: (context, {tag}) => ['div', 'p'].includes(tag),
after: (context) => {
context.commands.push({name: 'newLine'});
Expand Down
13 changes: 9 additions & 4 deletions src/convert/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<handlers.length; i++) {
const {before, stackName: handlerStackName, isWithoutClosingTag} = handlers[i];
const stackName = [null, undefined].includes(handlerStackName) ? nodeGroup.tag : handlerStackName;
Expand Down Expand Up @@ -132,6 +133,7 @@ const convert = async (xml) => {
normalizeWhitespace: true,
xmlMode: true,
}).root()[0].children[0];

const nodes = root.children;
let context = {data: [], commands: [], stack: {}};
for(let i=0; i<nodes.length; i++) {
Expand Down Expand Up @@ -163,9 +165,12 @@ const reduceSanitizeHtml = (sanitizers) => 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('-----');
Expand Down
2 changes: 1 addition & 1 deletion src/convert/notagHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 17 additions & 8 deletions test/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,10 @@ me</p>
describe('right tag', async () => {
it('right tag works', async () => {
const template = `
<right>
<p>dsdasdas</p>
<div style="font-weight: bold" disabled>oneone</div>
</right>
`;
<right>
<p>dsdasdas</p>
<div style="font-weight: bold" disabled>oneone</div>
</right>`;

const exptectedResult = [
{name: 'alignRight'},
Expand All @@ -558,8 +557,8 @@ me</p>

it('right tag works inside text', async () => {
const template = `
<p>dsd <right><b>RRR</b></right> asdas</p>
<div style="font-weight: bold" disabled>oneone</div>
<p>dsd <right><b>RRR</b></right> asdas</p>
<div style="font-weight: bold" disabled>oneone</div>
`;

const exptectedResult = [
Expand All @@ -576,7 +575,6 @@ me</p>
{name: 'bold', data: false},
{name: 'newLine'},
];

assert.deepStrictEqual(await convert(template), exptectedResult);
});
});
Expand Down Expand Up @@ -698,6 +696,17 @@ me</p>
{name: "newLine"},
];

// const issuet = `
// <normal>
// <p style="font-weight: bold">1</p>
// <div>2</div>
// </normal>
// <p>3</p>
// `;
//
// console.log(await convert(issuet));
// process.exit(-1);

assert.deepStrictEqual(await convert(template), exptectedResult);
});

Expand Down

0 comments on commit 2d06e0d

Please sign in to comment.