2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-17 19:21:20 +03:00

fix(parser): don't eat not allowed tags with params (#58) fixes #54

* feat(parser): write test for only allowed tags parsing

* chore(parser): rename only allowed test

* fix(parser): only allowed tag rendering

* fix(plugin-helper): add new TagNode toString tests
This commit is contained in:
Nikolay Kostyurin
2020-04-12 21:14:52 +02:00
committed by GitHub
parent f28f19e64c
commit a16b9f73b0
4 changed files with 122 additions and 27 deletions
+33 -3
View File
@@ -1,9 +1,28 @@
import { OPEN_BRAKET, CLOSE_BRAKET, SLASH } from './char';
import { getNodeLength, appendToNode } from './index';
import {
getNodeLength, appendToNode, attrsToString, attrValue, getUniqAttr,
} from './index';
const getTagAttrs = (tag, params) => {
const uniqAattr = getUniqAttr(params);
if (uniqAattr) {
const tagAttr = attrValue(tag, uniqAattr);
const attrs = { ...params };
delete attrs[uniqAattr];
const attrsStr = attrsToString(attrs);
return `${tagAttr}${attrsStr}`;
}
return `${tag}${attrsToString(params)}`;
};
class TagNode {
constructor(tag, attrs, content) {
this.tag = tag.toLowerCase();
this.tag = tag;
this.attrs = attrs;
this.content = [].concat(content);
}
@@ -24,11 +43,22 @@ class TagNode {
return getNodeLength(this);
}
toTagNode() {
return new TagNode(this.tag.toLowerCase(), this.attrs, this.content);
}
toString() {
const OB = OPEN_BRAKET;
const CB = CLOSE_BRAKET;
const isEmpty = this.content.length === 0;
const content = this.content.reduce((r, node) => r + node.toString(), '');
const tagAttrs = getTagAttrs(this.tag, this.attrs);
return OB + this.tag + CB + this.content.reduce((r, node) => r + node.toString(), '') + OB + SLASH + this.tag + CB;
if (isEmpty) {
return `${OB}${tagAttrs}${CB}`;
}
return `${OB}${tagAttrs}${CB}${content}${OB}${SLASH}${this.tag}${CB}`;
}
}