Skip to content

Commit

Permalink
fix lint error, move helper methods to char and token files
Browse files Browse the repository at this point in the history
  • Loading branch information
JiLiZART committed Jun 12, 2018
1 parent 77ddfca commit a5d3fea
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 346 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Expand Up @@ -7,5 +7,7 @@
"node": true,
"jest/globals": true
},
"rules": {}
"rules": {
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }]
}
}
36 changes: 5 additions & 31 deletions packages/bbob-html/index.js
@@ -1,4 +1,3 @@

function attrs(obj) {
let attr = '';

Expand Down Expand Up @@ -27,9 +26,7 @@ function traverse(tree, cb) {
return tree;
}

function render(tree, options) {
return html(tree);

function render(ast) {
function html(tree) {
let result = '';

Expand All @@ -42,43 +39,20 @@ function render(tree, options) {
return;
}

if (typeof node.tag === 'boolean' && !node.tag) {
typeof node.content !== 'object' && (result += node.content);

return node.content;
if (typeof node === 'object') {
result += `<${node.tag} ${attrs(node.attrs)}></${node.tag}>`;
}

// treat as new root tree if node is an array
if (Array.isArray(node)) {
result += html(node);

return;
}

const tag = node.tag || 'div';

if (isSingleTag(tag, singleTags, singleRegExp)) {
result += `<${tag}${attrs(node.attrs)}`;

switch (closingSingleTag) {
case 'tag':
result += `></${tag}>`;

break;
case 'slash':
result += ' />';

break;
default:
result += '>';
}
} else {
result += `<${tag}${node.attrs ? attrs(node.attrs) : ''}>${node.content ? html(node.content) : ''}</${tag}>`;
}
});

return result;
}

return html(ast);
}

module.exports = render;
6 changes: 6 additions & 0 deletions packages/bbob-html/index.test.js
@@ -0,0 +1,6 @@

describe('bbob-html', () => {
test('render proper markup', () => {

});
});
25 changes: 14 additions & 11 deletions packages/bbob-parser/Parser.js
Expand Up @@ -10,12 +10,12 @@ const {
isTagToken,
isTextToken,
isTagEnd,
} = require('./Tokenizer');
} = require('./token');

const Tokenizer = require('./Tokenizer');

const TokenChar = Tokenizer.CHAR;
const getChar = Tokenizer.getChar;
const {
SLASH,
getChar,
} = require('./char');

const createTagNode = (tag, attrs = {}, content = []) => ({ tag, attrs, content });

Expand All @@ -41,7 +41,7 @@ module.exports = class Parser {
const curTags = [];
const curTagsAttrName = [];

const closableTags = this.findNestedTags(this.tokens);
const closableTags = this.findNestedTags();

const isNestedTag = token => closableTags.indexOf(getTokenValue(token)) >= 0;

Expand Down Expand Up @@ -123,6 +123,7 @@ module.exports = class Parser {
if (lastNestedNode) {
getNodes().push(lastNestedNode);
} else {
// eslint-disable-next-line no-console
console.warn(`Inconsistent tag '${getTokenValue(token)}' on line ${getTokenLine(token)} and column ${getTokenColumn(token)}`);
}
}
Expand All @@ -149,14 +150,14 @@ module.exports = class Parser {
return nodes;
}

findNestedTags(tokens) {
const tags = tokens.filter(isTagToken).reduce((acc, token) => {
findNestedTags() {
const tags = this.tokens.filter(isTagToken).reduce((acc, token) => {
acc[getTokenValue(token)] = true;

return acc;
}, {});

const closeChar = getChar(TokenChar.SLASH);
const closeChar = getChar(SLASH);

return Object.keys(tags).reduce((arr, key) => {
if (tags[key] && tags[closeChar + key]) {
Expand All @@ -168,10 +169,12 @@ module.exports = class Parser {
}

isAllowedTag(value) {
if (this.options.allowOnlyTags && this.options.allowOnlyTags.length) {
return this.options.allowOnlyTags.indexOf(value) >= 0;
if (this.options.onlyAllowTags && this.options.onlyAllowTags.length) {
return this.options.onlyAllowTags.indexOf(value) >= 0;
}

return true;
}
};

module.exports.createTagNode = createTagNode;

0 comments on commit a5d3fea

Please sign in to comment.