mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-08 17:22:26 +03:00
* 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:
committed by
GitHub
parent
f28f19e64c
commit
a16b9f73b0
@@ -1,4 +1,5 @@
|
||||
import TagNode from '@bbob/plugin-helper/lib/TagNode';
|
||||
import { isTagNode } from '@bbob/plugin-helper';
|
||||
import { createLexer } from './lexer';
|
||||
import { createList } from './utils';
|
||||
|
||||
@@ -57,8 +58,25 @@ const parse = (input, opts = {}) => {
|
||||
return nestedTagsMap[token.getValue()];
|
||||
};
|
||||
|
||||
/**
|
||||
* @param tagName
|
||||
* @returns {boolean}
|
||||
*/
|
||||
const isTagNested = (tagName) => !!nestedTagsMap[tagName];
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {String} value
|
||||
* @return {boolean}
|
||||
*/
|
||||
const isAllowedTag = (value) => {
|
||||
if (options.onlyAllowTags && options.onlyAllowTags.length) {
|
||||
return options.onlyAllowTags.indexOf(value) >= 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Flushes temp tag nodes and its attributes buffers
|
||||
* @private
|
||||
@@ -86,29 +104,24 @@ const parse = (input, opts = {}) => {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {TagNode} tag
|
||||
* @param {string|TagNode} node
|
||||
*/
|
||||
const appendNodes = (tag) => {
|
||||
const appendNodes = (node) => {
|
||||
const items = getNodes();
|
||||
|
||||
if (Array.isArray(items)) {
|
||||
items.push(tag);
|
||||
if (isTagNode(node)) {
|
||||
if (isAllowedTag(node.tag)) {
|
||||
items.push(node.toTagNode());
|
||||
} else {
|
||||
items.push(node.toString());
|
||||
}
|
||||
} else {
|
||||
items.push(node);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {String} value
|
||||
* @return {boolean}
|
||||
*/
|
||||
const isAllowedTag = (value) => {
|
||||
if (options.onlyAllowTags && options.onlyAllowTags.length) {
|
||||
return options.onlyAllowTags.indexOf(value) >= 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @param {Token} token
|
||||
@@ -124,7 +137,7 @@ const parse = (input, opts = {}) => {
|
||||
if (isNested) {
|
||||
nestedNodes.push(tagNode);
|
||||
} else {
|
||||
appendNodes(tagNode);
|
||||
appendNodes(tagNode, token);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -138,8 +151,8 @@ const parse = (input, opts = {}) => {
|
||||
const lastNestedNode = nestedNodes.flushLast();
|
||||
|
||||
if (lastNestedNode) {
|
||||
appendNodes(lastNestedNode);
|
||||
} else if (options.onError) {
|
||||
appendNodes(lastNestedNode, token);
|
||||
} else if (typeof options.onError === 'function') {
|
||||
const tag = token.getValue();
|
||||
const line = token.getLine();
|
||||
const column = token.getColumn();
|
||||
@@ -217,7 +230,7 @@ const parse = (input, opts = {}) => {
|
||||
* @param {Token} token
|
||||
*/
|
||||
const onToken = (token) => {
|
||||
if (token.isTag() && isAllowedTag(token.getName())) {
|
||||
if (token.isTag()) {
|
||||
handleTag(token);
|
||||
} else {
|
||||
handleNode(token);
|
||||
|
||||
Reference in New Issue
Block a user