Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bbob/parser

Package Overview
Dependencies
Maintainers
1
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bbob/parser - npm Package Compare versions

Comparing version 1.3.1 to 1.3.2

1196

dist/index.js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@bbob/plugin-helper/lib/char'), require('@bbob/plugin-helper/lib/TagNode')) :
typeof define === 'function' && define.amd ? define(['exports', '@bbob/plugin-helper/lib/char', '@bbob/plugin-helper/lib/TagNode'], factory) :
(factory((global.BbobParser = {}),null,null));
}(this, (function (exports,char,TagNode) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.BbobParser = {})));
}(this, (function (exports) { 'use strict';
TagNode = TagNode && TagNode.hasOwnProperty('default') ? TagNode['default'] : TagNode;
function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
// type, value, line, row,
var TOKEN_TYPE_ID = 'type'; // 0;
var TOKEN_VALUE_ID = 'value'; // 1;
var TOKEN_COLUMN_ID = 'row'; // 2;
var TOKEN_LINE_ID = 'line'; // 3;
var char_1 = createCommonjsModule(function (module, exports) {
var TOKEN_TYPE_WORD = 'word';
var TOKEN_TYPE_TAG = 'tag';
var TOKEN_TYPE_ATTR_NAME = 'attr-name';
var TOKEN_TYPE_ATTR_VALUE = 'attr-value';
var TOKEN_TYPE_SPACE = 'space';
var TOKEN_TYPE_NEW_LINE = 'new-line';
exports.__esModule = true;
var N = '\n';
var TAB = '\t';
var F = '\f';
var R = '\r';
var getTokenValue = function getTokenValue(token) {
return token[TOKEN_VALUE_ID];
};
var getTokenLine = function getTokenLine(token) {
return token[TOKEN_LINE_ID];
};
var getTokenColumn = function getTokenColumn(token) {
return token[TOKEN_COLUMN_ID];
};
var EQ = '=';
var QUOTEMARK = '"';
var SPACE = ' ';
var isTextToken = function isTextToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD;
};
var OPEN_BRAKET = '[';
var CLOSE_BRAKET = ']';
var isTagToken = function isTagToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG;
};
var isTagEnd = function isTagEnd(token) {
return getTokenValue(token).charCodeAt(0) === char.SLASH.charCodeAt(0);
};
var isTagStart = function isTagStart(token) {
return !isTagEnd(token);
};
var isAttrNameToken = function isAttrNameToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME;
};
var isAttrValueToken = function isAttrValueToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE;
};
var SLASH = '/';
var BACKSLASH = '\\';
var getTagName = function getTagName(token) {
var value = getTokenValue(token);
var PLACEHOLDER_SPACE_TAB = ' ';
var PLACEHOLDER_SPACE = ' ';
return isTagEnd(token) ? value.slice(1) : value;
};
// const getChar = String.fromCharCode;
var convertTagToText = function convertTagToText(token) {
var text = char.OPEN_BRAKET;
exports.N = N;
exports.F = F;
exports.R = R;
exports.TAB = TAB;
exports.EQ = EQ;
exports.QUOTEMARK = QUOTEMARK;
exports.SPACE = SPACE;
exports.OPEN_BRAKET = OPEN_BRAKET;
exports.CLOSE_BRAKET = CLOSE_BRAKET;
exports.SLASH = SLASH;
exports.PLACEHOLDER_SPACE_TAB = PLACEHOLDER_SPACE_TAB;
exports.PLACEHOLDER_SPACE = PLACEHOLDER_SPACE;
exports.BACKSLASH = BACKSLASH;
});
if (isTagEnd(token)) {
text += char.SLASH;
}
unwrapExports(char_1);
var char_2 = char_1.N;
var char_3 = char_1.F;
var char_4 = char_1.R;
var char_5 = char_1.TAB;
var char_6 = char_1.EQ;
var char_7 = char_1.QUOTEMARK;
var char_8 = char_1.SPACE;
var char_9 = char_1.OPEN_BRAKET;
var char_10 = char_1.CLOSE_BRAKET;
var char_11 = char_1.SLASH;
var char_12 = char_1.PLACEHOLDER_SPACE_TAB;
var char_13 = char_1.PLACEHOLDER_SPACE;
var char_14 = char_1.BACKSLASH;
text += getTokenValue(token);
text += char.CLOSE_BRAKET;
var lib = createCommonjsModule(function (module, exports) {
return text;
};
exports.__esModule = true;
exports.isEOL = exports.isStringNode = exports.isTagNode = exports.getNodeLength = exports.appendToNode = exports.attrValue = undefined;
var Token = function () {
function Token(type, value, line, row) {
classCallCheck(this, Token);
this.type = String(type);
this.value = String(value);
this.line = Number(line);
this.row = Number(row);
}
Token.prototype.isEmpty = function isEmpty() {
return !!this.type;
};
var isTagNode = function isTagNode(el) {
return typeof el === 'object' && !!el.tag;
};
var isStringNode = function isStringNode(el) {
return typeof el === 'string';
};
var isEOL = function isEOL(el) {
return el === char_1.N;
};
Token.prototype.isText = function isText() {
return isTextToken(this);
};
var getNodeLength = function getNodeLength(node) {
if (isTagNode(node)) {
return node.content.reduce(function (count, contentNode) {
return count + getNodeLength(contentNode);
}, 0);
} else if (isStringNode(node)) {
return node.length;
}
Token.prototype.isTag = function isTag() {
return isTagToken(this);
};
return 0;
};
Token.prototype.isAttrName = function isAttrName() {
return isAttrNameToken(this);
};
var appendToNode = function appendToNode(node, value) {
node.content.push(value);
};
Token.prototype.isAttrValue = function isAttrValue() {
return isAttrValueToken(this);
};
var escapeQuote = function escapeQuote(value) {
return value.replace(/"/g, '"');
};
Token.prototype.isStart = function isStart() {
return isTagStart(this);
};
var attrValue = function attrValue(name, value) {
var type = typeof value;
Token.prototype.isEnd = function isEnd() {
return isTagEnd(this);
};
var types = {
boolean: function boolean() {
return value ? '' + name : '';
},
number: function number() {
return name + '="' + value + '"';
},
string: function string() {
return name + '="' + escapeQuote(value) + '"';
},
object: function object() {
return name + '="' + escapeQuote(JSON.stringify(value)) + '"';
}
};
Token.prototype.getName = function getName() {
return getTagName(this);
};
return types[type] ? types[type]() : '';
};
Token.prototype.getValue = function getValue() {
return getTokenValue(this);
};
exports.attrValue = attrValue;
exports.appendToNode = appendToNode;
exports.getNodeLength = getNodeLength;
exports.isTagNode = isTagNode;
exports.isStringNode = isStringNode;
exports.isEOL = isEOL;
});
Token.prototype.getLine = function getLine() {
return getTokenLine(this);
};
unwrapExports(lib);
var lib_1 = lib.isEOL;
var lib_2 = lib.isStringNode;
var lib_3 = lib.isTagNode;
var lib_4 = lib.getNodeLength;
var lib_5 = lib.appendToNode;
var lib_6 = lib.attrValue;
Token.prototype.getColumn = function getColumn() {
return getTokenColumn(this);
};
var TagNode_1 = createCommonjsModule(function (module, exports) {
Token.prototype.toString = function toString() {
return convertTagToText(this);
};
exports.__esModule = true;
exports.TagNode = undefined;
return Token;
}();
var TYPE_WORD = TOKEN_TYPE_WORD;
var TYPE_TAG = TOKEN_TYPE_TAG;
var TYPE_ATTR_NAME = TOKEN_TYPE_ATTR_NAME;
var TYPE_ATTR_VALUE = TOKEN_TYPE_ATTR_VALUE;
var TYPE_SPACE = TOKEN_TYPE_SPACE;
var TYPE_NEW_LINE = TOKEN_TYPE_NEW_LINE;
var _createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
}
}return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;
};
}();
/* eslint-disable no-plusplus,no-param-reassign */
var RESERVED_CHARS = [char.CLOSE_BRAKET, char.OPEN_BRAKET, char.QUOTEMARK, char.BACKSLASH, char.SPACE, char.TAB, char.EQ, char.N];
var NOT_CHAR_TOKENS = [char.OPEN_BRAKET, char.SPACE, char.TAB, char.N];
var WHITESPACES = [char.SPACE, char.TAB];
var isCharReserved = function isCharReserved(char$$1) {
return RESERVED_CHARS.indexOf(char$$1) >= 0;
};
var isWhiteSpace = function isWhiteSpace(char$$1) {
return WHITESPACES.indexOf(char$$1) >= 0;
};
var isCharToken = function isCharToken(char$$1) {
return NOT_CHAR_TOKENS.indexOf(char$$1) === -1;
};
var createCharGrabber = function createCharGrabber(source) {
var idx = 0;
var skip = function skip() {
idx += 1;
};
var hasNext = function hasNext() {
return source.length > idx;
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
return {
skip: skip,
hasNext: hasNext,
isLast: function isLast() {
return idx === source.length;
},
grabWhile: function grabWhile(cond) {
var start = idx;
var TagNode = function () {
function TagNode(tag, attrs, content) {
_classCallCheck(this, TagNode);
while (hasNext() && cond(source[idx])) {
skip();
}
this.tag = tag;
this.attrs = attrs;
this.content = content;
}
return source.substr(start, idx - start);
},
getNext: function getNext() {
return source[idx + 1];
},
getPrev: function getPrev() {
return source[idx - 1];
},
getCurr: function getCurr() {
return source[idx];
}
};
};
TagNode.prototype.attr = function attr(name, value) {
if (typeof value !== 'undefined') {
this.attrs[name] = value;
}
var trimChar = function trimChar(str, charToRemove) {
while (str.charAt(0) === charToRemove) {
str = str.substring(1);
}
return this.attrs[name];
};
while (str.charAt(str.length - 1) === charToRemove) {
str = str.substring(0, str.length - 1);
}
TagNode.prototype.append = function append(value) {
return (0, lib.appendToNode)(this, value);
};
return str;
};
TagNode.prototype.toString = function toString() {
var OB = char_1.OPEN_BRAKET;
var CB = char_1.CLOSE_BRAKET;
var unquote = function unquote(str) {
return str.replace(char.BACKSLASH + char.QUOTEMARK, char.QUOTEMARK);
};
var createToken = function createToken(type, value) {
var r = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var cl = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
return new Token(type, value, r, cl);
};
return OB + this.tag + CB + this.content.reduce(function (r, node) {
return r + node.toString();
}, '') + OB + char_1.SLASH + this.tag + CB;
};
function createLexer(buffer) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_createClass(TagNode, [{
key: 'length',
get: function get() {
return (0, lib.getNodeLength)(this);
}
}]);
var row = 0;
var col = 0;
return TagNode;
}();
var tokenIndex = -1;
var tokens = new Array(Math.floor(buffer.length));
var emitToken = function emitToken(token) {
if (options.onToken) {
options.onToken(token);
}
TagNode.create = function (tag) {
var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var content = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
return new TagNode(tag, attrs, content);
};
TagNode.isOf = function (node, type) {
return node.tag === type;
};
tokenIndex += 1;
tokens[tokenIndex] = token;
};
exports.TagNode = TagNode;
exports.default = TagNode;
});
var parseAttrs = function parseAttrs(str) {
var tagName = null;
var skipSpaces = false;
var TagNode = unwrapExports(TagNode_1);
var TagNode_2 = TagNode_1.TagNode;
var attrTokens = [];
var attrCharGrabber = createCharGrabber(str);
var validAttr = function validAttr(val) {
var isEQ = val === char.EQ;
var isWS = isWhiteSpace(val);
var isPrevSLASH = attrCharGrabber.getPrev() === char.SLASH;
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
if (tagName === null) {
return !(isEQ || isWS || attrCharGrabber.isLast());
}
// type, value, line, row,
var TOKEN_TYPE_ID = 'type'; // 0;
var TOKEN_VALUE_ID = 'value'; // 1;
var TOKEN_COLUMN_ID = 'row'; // 2;
var TOKEN_LINE_ID = 'line'; // 3;
if (skipSpaces && isWS) {
return true;
}
var TOKEN_TYPE_WORD = 'word';
var TOKEN_TYPE_TAG = 'tag';
var TOKEN_TYPE_ATTR_NAME = 'attr-name';
var TOKEN_TYPE_ATTR_VALUE = 'attr-value';
var TOKEN_TYPE_SPACE = 'space';
var TOKEN_TYPE_NEW_LINE = 'new-line';
if (val === char.QUOTEMARK && !isPrevSLASH) {
skipSpaces = !skipSpaces;
}
var getTokenValue = function getTokenValue(token) {
return token[TOKEN_VALUE_ID];
};
var getTokenLine = function getTokenLine(token) {
return token[TOKEN_LINE_ID];
};
var getTokenColumn = function getTokenColumn(token) {
return token[TOKEN_COLUMN_ID];
};
return !(isEQ || isWS);
};
var isTextToken = function isTextToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD;
};
var nextAttr = function nextAttr() {
var attrStr = attrCharGrabber.grabWhile(validAttr);
var isTagToken = function isTagToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG;
};
var isTagEnd = function isTagEnd(token) {
return getTokenValue(token).charCodeAt(0) === char_11.charCodeAt(0);
};
var isTagStart = function isTagStart(token) {
return !isTagEnd(token);
};
var isAttrNameToken = function isAttrNameToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME;
};
var isAttrValueToken = function isAttrValueToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE;
};
// first string before space is a tag name
if (tagName === null) {
tagName = attrStr;
} else if (isWhiteSpace(attrCharGrabber.getCurr()) || !attrCharGrabber.hasNext()) {
var escaped = unquote(trimChar(attrStr, char.QUOTEMARK));
attrTokens.push(createToken(TYPE_ATTR_VALUE, escaped, row, col));
} else {
attrTokens.push(createToken(TYPE_ATTR_NAME, attrStr, row, col));
}
var getTagName = function getTagName(token) {
var value = getTokenValue(token);
attrCharGrabber.skip();
};
return isTagEnd(token) ? value.slice(1) : value;
};
while (attrCharGrabber.hasNext()) {
nextAttr();
}
var convertTagToText = function convertTagToText(token) {
var text = char_9;
return { tag: tagName, attrs: attrTokens };
};
if (isTagEnd(token)) {
text += char_11;
}
var grabber = createCharGrabber(buffer);
text += getTokenValue(token);
text += char_10;
var next = function next() {
var char$$1 = grabber.getCurr();
return text;
};
if (char$$1 === char.N) {
grabber.skip();
col = 0;
row++;
var Token = function () {
function Token(type, value, line, row) {
classCallCheck(this, Token);
emitToken(createToken(TYPE_NEW_LINE, char$$1, row, col));
} else if (isWhiteSpace(char$$1)) {
var str = grabber.grabWhile(isWhiteSpace);
emitToken(createToken(TYPE_SPACE, str, row, col));
} else if (char$$1 === char.OPEN_BRAKET) {
var nextChar = grabber.getNext();
grabber.skip(); // skip [
this.type = String(type);
this.value = String(value);
this.line = Number(line);
this.row = Number(row);
}
if (isCharReserved(nextChar)) {
emitToken(createToken(TYPE_WORD, char$$1, row, col));
} else {
var _str = grabber.grabWhile(function (val) {
return val !== char.CLOSE_BRAKET;
});
grabber.skip(); // skip ]
Token.prototype.isEmpty = function isEmpty() {
return !!this.type;
};
if (!(_str.indexOf(char.EQ) > 0) || _str[0] === char.SLASH) {
emitToken(createToken(TYPE_TAG, _str, row, col));
} else {
var parsed = parseAttrs(_str);
Token.prototype.isText = function isText() {
return isTextToken(this);
};
emitToken(createToken(TYPE_TAG, parsed.tag, row, col));
parsed.attrs.map(emitToken);
}
}
} else if (char$$1 === char.CLOSE_BRAKET) {
grabber.skip();
Token.prototype.isTag = function isTag() {
return isTagToken(this);
};
emitToken(createToken(TYPE_WORD, char$$1, row, col));
} else if (isCharToken(char$$1)) {
var _str2 = grabber.grabWhile(isCharToken);
Token.prototype.isAttrName = function isAttrName() {
return isAttrNameToken(this);
};
emitToken(createToken(TYPE_WORD, _str2, row, col));
}
};
Token.prototype.isAttrValue = function isAttrValue() {
return isAttrValueToken(this);
};
var tokenize = function tokenize() {
while (grabber.hasNext()) {
next();
}
Token.prototype.isStart = function isStart() {
return isTagStart(this);
};
tokens.length = tokenIndex + 1;
Token.prototype.isEnd = function isEnd() {
return isTagEnd(this);
};
return tokens;
};
Token.prototype.getName = function getName() {
return getTagName(this);
};
var isTokenNested = function isTokenNested(token) {
var value = char.OPEN_BRAKET + char.SLASH + token.getValue();
return buffer.indexOf(value) > -1;
};
Token.prototype.getValue = function getValue() {
return getTokenValue(this);
};
return {
tokenize: tokenize,
isTokenNested: isTokenNested
};
}
Token.prototype.getLine = function getLine() {
return getTokenLine(this);
};
/**
* @private
* @type {Array}
*/
var nodes = void 0;
/**
* @private
* @type {Array}
*/
var nestedNodes = void 0;
/**
* @private
* @type {Array}
*/
var tagNodes = void 0;
/**
* @private
* @type {Array}
*/
var tagNodesAttrName = void 0;
Token.prototype.getColumn = function getColumn() {
return getTokenColumn(this);
};
var options = {};
var tokenizer = null;
Token.prototype.toString = function toString() {
return convertTagToText(this);
};
// eslint-disable-next-line no-unused-vars
var tokens = null;
return Token;
}();
var TYPE_WORD = TOKEN_TYPE_WORD;
var TYPE_TAG = TOKEN_TYPE_TAG;
var TYPE_ATTR_NAME = TOKEN_TYPE_ATTR_NAME;
var TYPE_ATTR_VALUE = TOKEN_TYPE_ATTR_VALUE;
var TYPE_SPACE = TOKEN_TYPE_SPACE;
var TYPE_NEW_LINE = TOKEN_TYPE_NEW_LINE;
var createTokenizer = function createTokenizer(input, onToken) {
return createLexer(input, { onToken: onToken });
};
/* eslint-disable no-plusplus,no-param-reassign */
/**
* @private
* @param token
* @return {*}
*/
var isTagNested = function isTagNested(token) {
return tokenizer.isTokenNested(token);
};
var RESERVED_CHARS = [char_10, char_9, char_7, char_14, char_8, char_5, char_6, char_2];
var NOT_CHAR_TOKENS = [char_9, char_8, char_5, char_2];
var WHITESPACES = [char_8, char_5];
/**
* @private
* @return {TagNode}
*/
var getTagNode = function getTagNode() {
return tagNodes.length ? tagNodes[tagNodes.length - 1] : null;
};
var isCharReserved = function isCharReserved(char$$1) {
return RESERVED_CHARS.indexOf(char$$1) >= 0;
};
var isWhiteSpace = function isWhiteSpace(char$$1) {
return WHITESPACES.indexOf(char$$1) >= 0;
};
var isCharToken = function isCharToken(char$$1) {
return NOT_CHAR_TOKENS.indexOf(char$$1) === -1;
};
/**
* @private
* @param {Token} token
* @return {Array}
*/
var createTagNode = function createTagNode(token) {
return tagNodes.push(TagNode.create(token.getValue()));
};
/**
* @private
* @param {Token} token
* @return {Array}
*/
var createTagNodeAttrName = function createTagNodeAttrName(token) {
return tagNodesAttrName.push(token.getValue());
};
var createCharGrabber = function createCharGrabber(source) {
var idx = 0;
/**
* @private
* @return {Array}
*/
var getTagNodeAttrName = function getTagNodeAttrName() {
return tagNodesAttrName.length ? tagNodesAttrName[tagNodesAttrName.length - 1] : getTagNode().tag;
};
var skip = function skip() {
idx += 1;
};
var hasNext = function hasNext() {
return source.length > idx;
};
/**
* @private
* @return {Array}
*/
var clearTagNodeAttrName = function clearTagNodeAttrName() {
if (tagNodesAttrName.length) {
tagNodesAttrName.pop();
}
};
return {
skip: skip,
hasNext: hasNext,
isLast: function isLast() {
return idx === source.length;
},
grabWhile: function grabWhile(cond) {
var start = idx;
/**
* @private
* @return {Array}
*/
var clearTagNode = function clearTagNode() {
if (tagNodes.length) {
tagNodes.pop();
while (hasNext() && cond(source[idx])) {
skip();
}
clearTagNodeAttrName();
}
};
return source.substr(start, idx - start);
},
getNext: function getNext() {
return source[idx + 1];
},
getPrev: function getPrev() {
return source[idx - 1];
},
getCurr: function getCurr() {
return source[idx];
}
};
};
/**
* @private
* @return {Array}
*/
var getNodes = function getNodes() {
if (nestedNodes.length) {
var nestedNode = nestedNodes[nestedNodes.length - 1];
return nestedNode.content;
}
var trimChar = function trimChar(str, charToRemove) {
while (str.charAt(0) === charToRemove) {
str = str.substring(1);
}
return nodes;
};
while (str.charAt(str.length - 1) === charToRemove) {
str = str.substring(0, str.length - 1);
}
/**
* @private
* @param tag
*/
var appendNode = function appendNode(tag) {
getNodes().push(tag);
};
return str;
};
/**
* @private
* @param value
* @return {boolean}
*/
var isAllowedTag = function isAllowedTag(value) {
if (options.onlyAllowTags && options.onlyAllowTags.length) {
return options.onlyAllowTags.indexOf(value) >= 0;
}
var unquote = function unquote(str) {
return str.replace(char_14 + char_7, char_7);
};
var createToken = function createToken(type, value) {
var r = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var cl = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
return new Token(type, value, r, cl);
};
return true;
};
/**
* @private
* @param {Token} token
*/
var handleTagStart = function handleTagStart(token) {
if (token.isStart()) {
createTagNode(token);
function createLexer(buffer) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (isTagNested(token)) {
nestedNodes.push(getTagNode());
} else {
appendNode(getTagNode());
clearTagNode();
}
}
};
var row = 0;
var col = 0;
/**
* @private
* @param {Token} token
*/
var handleTagEnd = function handleTagEnd(token) {
if (token.isEnd()) {
clearTagNode();
var tokenIndex = -1;
var tokens = new Array(Math.floor(buffer.length));
var emitToken = function emitToken(token) {
if (options.onToken) {
options.onToken(token);
}
var lastNestedNode = nestedNodes.pop();
tokenIndex += 1;
tokens[tokenIndex] = token;
};
if (lastNestedNode) {
appendNode(lastNestedNode);
} else if (options.onError) {
var tag = token.getValue();
var line = token.getLine();
var column = token.getColumn();
options.onError({
message: 'Inconsistent tag \'' + tag + '\' on line ' + line + ' and column ' + column,
lineNumber: line,
columnNumber: column
});
}
}
};
var parseAttrs = function parseAttrs(str) {
var tagName = null;
var skipSpaces = false;
/**
* @private
* @param {Token} token
*/
var handleTagToken = function handleTagToken(token) {
if (token.isTag()) {
if (isAllowedTag(token.getName())) {
// [tag]
handleTagStart(token);
var attrTokens = [];
var attrCharGrabber = createCharGrabber(str);
var validAttr = function validAttr(val) {
var isEQ = val === char_6;
var isWS = isWhiteSpace(val);
var isPrevSLASH = attrCharGrabber.getPrev() === char_11;
// [/tag]
handleTagEnd(token);
} else {
appendNode(token.toString());
}
}
};
if (tagName === null) {
return !(isEQ || isWS || attrCharGrabber.isLast());
}
/**
* @private
* @param {Token} token
*/
var handleTagNode = function handleTagNode(token) {
var tagNode = getTagNode();
if (skipSpaces && isWS) {
return true;
}
if (tagNode) {
if (token.isAttrName()) {
createTagNodeAttrName(token);
tagNode.attr(getTagNodeAttrName(), null);
} else if (token.isAttrValue()) {
tagNode.attr(getTagNodeAttrName(), token.getValue());
clearTagNodeAttrName();
} else if (token.isText()) {
tagNode.append(token.getValue());
}
} else if (token.isText()) {
appendNode(token.getValue());
}
};
if (val === char_7 && !isPrevSLASH) {
skipSpaces = !skipSpaces;
}
/**
* @private
* @param token
*/
var parseToken = function parseToken(token) {
handleTagToken(token);
handleTagNode(token);
};
return !(isEQ || isWS);
};
/**
* @public
* @return {Array}
*/
var parse = function parse(input) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var nextAttr = function nextAttr() {
var attrStr = attrCharGrabber.grabWhile(validAttr);
options = opts;
tokenizer = (opts.createTokenizer ? opts.createTokenizer : createTokenizer)(input, parseToken);
// first string before space is a tag name
if (tagName === null) {
tagName = attrStr;
} else if (isWhiteSpace(attrCharGrabber.getCurr()) || !attrCharGrabber.hasNext()) {
var escaped = unquote(trimChar(attrStr, char_7));
attrTokens.push(createToken(TYPE_ATTR_VALUE, escaped, row, col));
} else {
attrTokens.push(createToken(TYPE_ATTR_NAME, attrStr, row, col));
}
nodes = [];
nestedNodes = [];
tagNodes = [];
tagNodesAttrName = [];
attrCharGrabber.skip();
};
tokens = tokenizer.tokenize();
while (attrCharGrabber.hasNext()) {
nextAttr();
}
return nodes;
};
return { tag: tagName, attrs: attrTokens };
};
exports.parse = parse;
exports.createTagNode = createTagNode;
var grabber = createCharGrabber(buffer);
Object.defineProperty(exports, '__esModule', { value: true });
var next = function next() {
var char$$1 = grabber.getCurr();
if (char$$1 === char_2) {
grabber.skip();
col = 0;
row++;
emitToken(createToken(TYPE_NEW_LINE, char$$1, row, col));
} else if (isWhiteSpace(char$$1)) {
var str = grabber.grabWhile(isWhiteSpace);
emitToken(createToken(TYPE_SPACE, str, row, col));
} else if (char$$1 === char_9) {
var nextChar = grabber.getNext();
grabber.skip(); // skip [
if (isCharReserved(nextChar)) {
emitToken(createToken(TYPE_WORD, char$$1, row, col));
} else {
var _str = grabber.grabWhile(function (val) {
return val !== char_10;
});
grabber.skip(); // skip ]
if (!(_str.indexOf(char_6) > 0) || _str[0] === char_11) {
emitToken(createToken(TYPE_TAG, _str, row, col));
} else {
var parsed = parseAttrs(_str);
emitToken(createToken(TYPE_TAG, parsed.tag, row, col));
parsed.attrs.map(emitToken);
}
}
} else if (char$$1 === char_10) {
grabber.skip();
emitToken(createToken(TYPE_WORD, char$$1, row, col));
} else if (isCharToken(char$$1)) {
var _str2 = grabber.grabWhile(isCharToken);
emitToken(createToken(TYPE_WORD, _str2, row, col));
}
};
var tokenize = function tokenize() {
while (grabber.hasNext()) {
next();
}
tokens.length = tokenIndex + 1;
return tokens;
};
var isTokenNested = function isTokenNested(token) {
var value = char_9 + char_11 + token.getValue();
return buffer.indexOf(value) > -1;
};
return {
tokenize: tokenize,
isTokenNested: isTokenNested
};
}
/**
* @private
* @type {Array}
*/
var nodes = void 0;
/**
* @private
* @type {Array}
*/
var nestedNodes = void 0;
/**
* @private
* @type {Array}
*/
var tagNodes = void 0;
/**
* @private
* @type {Array}
*/
var tagNodesAttrName = void 0;
var options = {};
var tokenizer = null;
// eslint-disable-next-line no-unused-vars
var tokens = null;
var createTokenizer = function createTokenizer(input, onToken) {
return createLexer(input, { onToken: onToken });
};
/**
* @private
* @param token
* @return {*}
*/
var isTagNested = function isTagNested(token) {
return tokenizer.isTokenNested(token);
};
/**
* @private
* @return {TagNode}
*/
var getTagNode = function getTagNode() {
return tagNodes.length ? tagNodes[tagNodes.length - 1] : null;
};
/**
* @private
* @param {Token} token
* @return {Array}
*/
var createTagNode = function createTagNode(token) {
return tagNodes.push(TagNode.create(token.getValue()));
};
/**
* @private
* @param {Token} token
* @return {Array}
*/
var createTagNodeAttrName = function createTagNodeAttrName(token) {
return tagNodesAttrName.push(token.getValue());
};
/**
* @private
* @return {Array}
*/
var getTagNodeAttrName = function getTagNodeAttrName() {
return tagNodesAttrName.length ? tagNodesAttrName[tagNodesAttrName.length - 1] : getTagNode().tag;
};
/**
* @private
* @return {Array}
*/
var clearTagNodeAttrName = function clearTagNodeAttrName() {
if (tagNodesAttrName.length) {
tagNodesAttrName.pop();
}
};
/**
* @private
* @return {Array}
*/
var clearTagNode = function clearTagNode() {
if (tagNodes.length) {
tagNodes.pop();
clearTagNodeAttrName();
}
};
/**
* @private
* @return {Array}
*/
var getNodes = function getNodes() {
if (nestedNodes.length) {
var nestedNode = nestedNodes[nestedNodes.length - 1];
return nestedNode.content;
}
return nodes;
};
/**
* @private
* @param tag
*/
var appendNode = function appendNode(tag) {
getNodes().push(tag);
};
/**
* @private
* @param value
* @return {boolean}
*/
var isAllowedTag = function isAllowedTag(value) {
if (options.onlyAllowTags && options.onlyAllowTags.length) {
return options.onlyAllowTags.indexOf(value) >= 0;
}
return true;
};
/**
* @private
* @param {Token} token
*/
var handleTagStart = function handleTagStart(token) {
if (token.isStart()) {
createTagNode(token);
if (isTagNested(token)) {
nestedNodes.push(getTagNode());
} else {
appendNode(getTagNode());
clearTagNode();
}
}
};
/**
* @private
* @param {Token} token
*/
var handleTagEnd = function handleTagEnd(token) {
if (token.isEnd()) {
clearTagNode();
var lastNestedNode = nestedNodes.pop();
if (lastNestedNode) {
appendNode(lastNestedNode);
} else if (options.onError) {
var tag = token.getValue();
var line = token.getLine();
var column = token.getColumn();
options.onError({
message: 'Inconsistent tag \'' + tag + '\' on line ' + line + ' and column ' + column,
lineNumber: line,
columnNumber: column
});
}
}
};
/**
* @private
* @param {Token} token
*/
var handleTagToken = function handleTagToken(token) {
if (token.isTag()) {
if (isAllowedTag(token.getName())) {
// [tag]
handleTagStart(token);
// [/tag]
handleTagEnd(token);
} else {
appendNode(token.toString());
}
}
};
/**
* @private
* @param {Token} token
*/
var handleTagNode = function handleTagNode(token) {
var tagNode = getTagNode();
if (tagNode) {
if (token.isAttrName()) {
createTagNodeAttrName(token);
tagNode.attr(getTagNodeAttrName(), null);
} else if (token.isAttrValue()) {
tagNode.attr(getTagNodeAttrName(), token.getValue());
clearTagNodeAttrName();
} else if (token.isText()) {
tagNode.append(token.getValue());
}
} else if (token.isText()) {
appendNode(token.getValue());
}
};
/**
* @private
* @param token
*/
var parseToken = function parseToken(token) {
handleTagToken(token);
handleTagNode(token);
};
/**
* @public
* @return {Array}
*/
var parse = function parse(input) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
options = opts;
tokenizer = (opts.createTokenizer ? opts.createTokenizer : createTokenizer)(input, parseToken);
nodes = [];
nestedNodes = [];
tagNodes = [];
tagNodesAttrName = [];
tokens = tokenizer.tokenize();
return nodes;
};
exports.parse = parse;
exports.createTagNode = createTagNode;
Object.defineProperty(exports, '__esModule', { value: true });
})));
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@bbob/plugin-helper/lib/char'), require('@bbob/plugin-helper/lib/TagNode')) :
typeof define === 'function' && define.amd ? define(['exports', '@bbob/plugin-helper/lib/char', '@bbob/plugin-helper/lib/TagNode'], factory) :
(factory((global.BbobParser = {}),null,null));
}(this, (function (exports,char,TagNode) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.BbobParser = {})));
}(this, (function (exports) { 'use strict';
TagNode = TagNode && TagNode.hasOwnProperty('default') ? TagNode['default'] : TagNode;
function unwrapExports (x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
}
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
// type, value, line, row,
var TOKEN_TYPE_ID = 'type'; // 0;
var TOKEN_VALUE_ID = 'value'; // 1;
var TOKEN_COLUMN_ID = 'row'; // 2;
var TOKEN_LINE_ID = 'line'; // 3;
var char_1 = createCommonjsModule(function (module, exports) {
var TOKEN_TYPE_WORD = 'word';
var TOKEN_TYPE_TAG = 'tag';
var TOKEN_TYPE_ATTR_NAME = 'attr-name';
var TOKEN_TYPE_ATTR_VALUE = 'attr-value';
var TOKEN_TYPE_SPACE = 'space';
var TOKEN_TYPE_NEW_LINE = 'new-line';
exports.__esModule = true;
var N = '\n';
var TAB = '\t';
var F = '\f';
var R = '\r';
var getTokenValue = function getTokenValue(token) {
return token[TOKEN_VALUE_ID];
};
var getTokenLine = function getTokenLine(token) {
return token[TOKEN_LINE_ID];
};
var getTokenColumn = function getTokenColumn(token) {
return token[TOKEN_COLUMN_ID];
};
var EQ = '=';
var QUOTEMARK = '"';
var SPACE = ' ';
var isTextToken = function isTextToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD;
};
var OPEN_BRAKET = '[';
var CLOSE_BRAKET = ']';
var isTagToken = function isTagToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG;
};
var isTagEnd = function isTagEnd(token) {
return getTokenValue(token).charCodeAt(0) === char.SLASH.charCodeAt(0);
};
var isTagStart = function isTagStart(token) {
return !isTagEnd(token);
};
var isAttrNameToken = function isAttrNameToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME;
};
var isAttrValueToken = function isAttrValueToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE;
};
var SLASH = '/';
var BACKSLASH = '\\';
var getTagName = function getTagName(token) {
var value = getTokenValue(token);
var PLACEHOLDER_SPACE_TAB = ' ';
var PLACEHOLDER_SPACE = ' ';
return isTagEnd(token) ? value.slice(1) : value;
};
// const getChar = String.fromCharCode;
var convertTagToText = function convertTagToText(token) {
var text = char.OPEN_BRAKET;
exports.N = N;
exports.F = F;
exports.R = R;
exports.TAB = TAB;
exports.EQ = EQ;
exports.QUOTEMARK = QUOTEMARK;
exports.SPACE = SPACE;
exports.OPEN_BRAKET = OPEN_BRAKET;
exports.CLOSE_BRAKET = CLOSE_BRAKET;
exports.SLASH = SLASH;
exports.PLACEHOLDER_SPACE_TAB = PLACEHOLDER_SPACE_TAB;
exports.PLACEHOLDER_SPACE = PLACEHOLDER_SPACE;
exports.BACKSLASH = BACKSLASH;
});
if (isTagEnd(token)) {
text += char.SLASH;
}
unwrapExports(char_1);
var char_2 = char_1.N;
var char_3 = char_1.F;
var char_4 = char_1.R;
var char_5 = char_1.TAB;
var char_6 = char_1.EQ;
var char_7 = char_1.QUOTEMARK;
var char_8 = char_1.SPACE;
var char_9 = char_1.OPEN_BRAKET;
var char_10 = char_1.CLOSE_BRAKET;
var char_11 = char_1.SLASH;
var char_12 = char_1.PLACEHOLDER_SPACE_TAB;
var char_13 = char_1.PLACEHOLDER_SPACE;
var char_14 = char_1.BACKSLASH;
text += getTokenValue(token);
text += char.CLOSE_BRAKET;
var lib = createCommonjsModule(function (module, exports) {
return text;
};
exports.__esModule = true;
exports.isEOL = exports.isStringNode = exports.isTagNode = exports.getNodeLength = exports.appendToNode = exports.attrValue = undefined;
var Token = function () {
function Token(type, value, line, row) {
classCallCheck(this, Token);
this.type = String(type);
this.value = String(value);
this.line = Number(line);
this.row = Number(row);
}
Token.prototype.isEmpty = function isEmpty() {
return !!this.type;
};
var isTagNode = function isTagNode(el) {
return typeof el === 'object' && !!el.tag;
};
var isStringNode = function isStringNode(el) {
return typeof el === 'string';
};
var isEOL = function isEOL(el) {
return el === char_1.N;
};
Token.prototype.isText = function isText() {
return isTextToken(this);
};
var getNodeLength = function getNodeLength(node) {
if (isTagNode(node)) {
return node.content.reduce(function (count, contentNode) {
return count + getNodeLength(contentNode);
}, 0);
} else if (isStringNode(node)) {
return node.length;
}
Token.prototype.isTag = function isTag() {
return isTagToken(this);
};
return 0;
};
Token.prototype.isAttrName = function isAttrName() {
return isAttrNameToken(this);
};
var appendToNode = function appendToNode(node, value) {
node.content.push(value);
};
Token.prototype.isAttrValue = function isAttrValue() {
return isAttrValueToken(this);
};
var escapeQuote = function escapeQuote(value) {
return value.replace(/"/g, '&quot;');
};
Token.prototype.isStart = function isStart() {
return isTagStart(this);
};
var attrValue = function attrValue(name, value) {
var type = typeof value;
Token.prototype.isEnd = function isEnd() {
return isTagEnd(this);
};
var types = {
boolean: function boolean() {
return value ? '' + name : '';
},
number: function number() {
return name + '="' + value + '"';
},
string: function string() {
return name + '="' + escapeQuote(value) + '"';
},
object: function object() {
return name + '="' + escapeQuote(JSON.stringify(value)) + '"';
}
};
Token.prototype.getName = function getName() {
return getTagName(this);
};
return types[type] ? types[type]() : '';
};
Token.prototype.getValue = function getValue() {
return getTokenValue(this);
};
exports.attrValue = attrValue;
exports.appendToNode = appendToNode;
exports.getNodeLength = getNodeLength;
exports.isTagNode = isTagNode;
exports.isStringNode = isStringNode;
exports.isEOL = isEOL;
});
Token.prototype.getLine = function getLine() {
return getTokenLine(this);
};
unwrapExports(lib);
var lib_1 = lib.isEOL;
var lib_2 = lib.isStringNode;
var lib_3 = lib.isTagNode;
var lib_4 = lib.getNodeLength;
var lib_5 = lib.appendToNode;
var lib_6 = lib.attrValue;
Token.prototype.getColumn = function getColumn() {
return getTokenColumn(this);
};
var TagNode_1 = createCommonjsModule(function (module, exports) {
Token.prototype.toString = function toString() {
return convertTagToText(this);
};
exports.__esModule = true;
exports.TagNode = undefined;
return Token;
}();
var TYPE_WORD = TOKEN_TYPE_WORD;
var TYPE_TAG = TOKEN_TYPE_TAG;
var TYPE_ATTR_NAME = TOKEN_TYPE_ATTR_NAME;
var TYPE_ATTR_VALUE = TOKEN_TYPE_ATTR_VALUE;
var TYPE_SPACE = TOKEN_TYPE_SPACE;
var TYPE_NEW_LINE = TOKEN_TYPE_NEW_LINE;
var _createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);
}
}return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;
};
}();
/* eslint-disable no-plusplus,no-param-reassign */
var RESERVED_CHARS = [char.CLOSE_BRAKET, char.OPEN_BRAKET, char.QUOTEMARK, char.BACKSLASH, char.SPACE, char.TAB, char.EQ, char.N];
var NOT_CHAR_TOKENS = [char.OPEN_BRAKET, char.SPACE, char.TAB, char.N];
var WHITESPACES = [char.SPACE, char.TAB];
var isCharReserved = function isCharReserved(char$$1) {
return RESERVED_CHARS.indexOf(char$$1) >= 0;
};
var isWhiteSpace = function isWhiteSpace(char$$1) {
return WHITESPACES.indexOf(char$$1) >= 0;
};
var isCharToken = function isCharToken(char$$1) {
return NOT_CHAR_TOKENS.indexOf(char$$1) === -1;
};
var createCharGrabber = function createCharGrabber(source) {
var idx = 0;
var skip = function skip() {
idx += 1;
};
var hasNext = function hasNext() {
return source.length > idx;
};
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
return {
skip: skip,
hasNext: hasNext,
isLast: function isLast() {
return idx === source.length;
},
grabWhile: function grabWhile(cond) {
var start = idx;
var TagNode = function () {
function TagNode(tag, attrs, content) {
_classCallCheck(this, TagNode);
while (hasNext() && cond(source[idx])) {
skip();
}
this.tag = tag;
this.attrs = attrs;
this.content = content;
}
return source.substr(start, idx - start);
},
getNext: function getNext() {
return source[idx + 1];
},
getPrev: function getPrev() {
return source[idx - 1];
},
getCurr: function getCurr() {
return source[idx];
}
};
};
TagNode.prototype.attr = function attr(name, value) {
if (typeof value !== 'undefined') {
this.attrs[name] = value;
}
var trimChar = function trimChar(str, charToRemove) {
while (str.charAt(0) === charToRemove) {
str = str.substring(1);
}
return this.attrs[name];
};
while (str.charAt(str.length - 1) === charToRemove) {
str = str.substring(0, str.length - 1);
}
TagNode.prototype.append = function append(value) {
return (0, lib.appendToNode)(this, value);
};
return str;
};
TagNode.prototype.toString = function toString() {
var OB = char_1.OPEN_BRAKET;
var CB = char_1.CLOSE_BRAKET;
var unquote = function unquote(str) {
return str.replace(char.BACKSLASH + char.QUOTEMARK, char.QUOTEMARK);
};
var createToken = function createToken(type, value) {
var r = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var cl = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
return new Token(type, value, r, cl);
};
return OB + this.tag + CB + this.content.reduce(function (r, node) {
return r + node.toString();
}, '') + OB + char_1.SLASH + this.tag + CB;
};
function createLexer(buffer) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_createClass(TagNode, [{
key: 'length',
get: function get() {
return (0, lib.getNodeLength)(this);
}
}]);
var row = 0;
var col = 0;
return TagNode;
}();
var tokenIndex = -1;
var tokens = new Array(Math.floor(buffer.length));
var emitToken = function emitToken(token) {
if (options.onToken) {
options.onToken(token);
}
TagNode.create = function (tag) {
var attrs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var content = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
return new TagNode(tag, attrs, content);
};
TagNode.isOf = function (node, type) {
return node.tag === type;
};
tokenIndex += 1;
tokens[tokenIndex] = token;
};
exports.TagNode = TagNode;
exports.default = TagNode;
});
var parseAttrs = function parseAttrs(str) {
var tagName = null;
var skipSpaces = false;
var TagNode = unwrapExports(TagNode_1);
var TagNode_2 = TagNode_1.TagNode;
var attrTokens = [];
var attrCharGrabber = createCharGrabber(str);
var validAttr = function validAttr(val) {
var isEQ = val === char.EQ;
var isWS = isWhiteSpace(val);
var isPrevSLASH = attrCharGrabber.getPrev() === char.SLASH;
var classCallCheck = function (instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
};
if (tagName === null) {
return !(isEQ || isWS || attrCharGrabber.isLast());
}
// type, value, line, row,
var TOKEN_TYPE_ID = 'type'; // 0;
var TOKEN_VALUE_ID = 'value'; // 1;
var TOKEN_COLUMN_ID = 'row'; // 2;
var TOKEN_LINE_ID = 'line'; // 3;
if (skipSpaces && isWS) {
return true;
}
var TOKEN_TYPE_WORD = 'word';
var TOKEN_TYPE_TAG = 'tag';
var TOKEN_TYPE_ATTR_NAME = 'attr-name';
var TOKEN_TYPE_ATTR_VALUE = 'attr-value';
var TOKEN_TYPE_SPACE = 'space';
var TOKEN_TYPE_NEW_LINE = 'new-line';
if (val === char.QUOTEMARK && !isPrevSLASH) {
skipSpaces = !skipSpaces;
}
var getTokenValue = function getTokenValue(token) {
return token[TOKEN_VALUE_ID];
};
var getTokenLine = function getTokenLine(token) {
return token[TOKEN_LINE_ID];
};
var getTokenColumn = function getTokenColumn(token) {
return token[TOKEN_COLUMN_ID];
};
return !(isEQ || isWS);
};
var isTextToken = function isTextToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD;
};
var nextAttr = function nextAttr() {
var attrStr = attrCharGrabber.grabWhile(validAttr);
var isTagToken = function isTagToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG;
};
var isTagEnd = function isTagEnd(token) {
return getTokenValue(token).charCodeAt(0) === char_11.charCodeAt(0);
};
var isTagStart = function isTagStart(token) {
return !isTagEnd(token);
};
var isAttrNameToken = function isAttrNameToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME;
};
var isAttrValueToken = function isAttrValueToken(token) {
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE;
};
// first string before space is a tag name
if (tagName === null) {
tagName = attrStr;
} else if (isWhiteSpace(attrCharGrabber.getCurr()) || !attrCharGrabber.hasNext()) {
var escaped = unquote(trimChar(attrStr, char.QUOTEMARK));
attrTokens.push(createToken(TYPE_ATTR_VALUE, escaped, row, col));
} else {
attrTokens.push(createToken(TYPE_ATTR_NAME, attrStr, row, col));
}
var getTagName = function getTagName(token) {
var value = getTokenValue(token);
attrCharGrabber.skip();
};
return isTagEnd(token) ? value.slice(1) : value;
};
while (attrCharGrabber.hasNext()) {
nextAttr();
}
var convertTagToText = function convertTagToText(token) {
var text = char_9;
return { tag: tagName, attrs: attrTokens };
};
if (isTagEnd(token)) {
text += char_11;
}
var grabber = createCharGrabber(buffer);
text += getTokenValue(token);
text += char_10;
var next = function next() {
var char$$1 = grabber.getCurr();
return text;
};
if (char$$1 === char.N) {
grabber.skip();
col = 0;
row++;
var Token = function () {
function Token(type, value, line, row) {
classCallCheck(this, Token);
emitToken(createToken(TYPE_NEW_LINE, char$$1, row, col));
} else if (isWhiteSpace(char$$1)) {
var str = grabber.grabWhile(isWhiteSpace);
emitToken(createToken(TYPE_SPACE, str, row, col));
} else if (char$$1 === char.OPEN_BRAKET) {
var nextChar = grabber.getNext();
grabber.skip(); // skip [
this.type = String(type);
this.value = String(value);
this.line = Number(line);
this.row = Number(row);
}
if (isCharReserved(nextChar)) {
emitToken(createToken(TYPE_WORD, char$$1, row, col));
} else {
var _str = grabber.grabWhile(function (val) {
return val !== char.CLOSE_BRAKET;
});
grabber.skip(); // skip ]
Token.prototype.isEmpty = function isEmpty() {
return !!this.type;
};
if (!(_str.indexOf(char.EQ) > 0) || _str[0] === char.SLASH) {
emitToken(createToken(TYPE_TAG, _str, row, col));
} else {
var parsed = parseAttrs(_str);
Token.prototype.isText = function isText() {
return isTextToken(this);
};
emitToken(createToken(TYPE_TAG, parsed.tag, row, col));
parsed.attrs.map(emitToken);
}
}
} else if (char$$1 === char.CLOSE_BRAKET) {
grabber.skip();
Token.prototype.isTag = function isTag() {
return isTagToken(this);
};
emitToken(createToken(TYPE_WORD, char$$1, row, col));
} else if (isCharToken(char$$1)) {
var _str2 = grabber.grabWhile(isCharToken);
Token.prototype.isAttrName = function isAttrName() {
return isAttrNameToken(this);
};
emitToken(createToken(TYPE_WORD, _str2, row, col));
}
};
Token.prototype.isAttrValue = function isAttrValue() {
return isAttrValueToken(this);
};
var tokenize = function tokenize() {
while (grabber.hasNext()) {
next();
}
Token.prototype.isStart = function isStart() {
return isTagStart(this);
};
tokens.length = tokenIndex + 1;
Token.prototype.isEnd = function isEnd() {
return isTagEnd(this);
};
return tokens;
};
Token.prototype.getName = function getName() {
return getTagName(this);
};
var isTokenNested = function isTokenNested(token) {
var value = char.OPEN_BRAKET + char.SLASH + token.getValue();
return buffer.indexOf(value) > -1;
};
Token.prototype.getValue = function getValue() {
return getTokenValue(this);
};
return {
tokenize: tokenize,
isTokenNested: isTokenNested
};
}
Token.prototype.getLine = function getLine() {
return getTokenLine(this);
};
/**
* @private
* @type {Array}
*/
var nodes = void 0;
/**
* @private
* @type {Array}
*/
var nestedNodes = void 0;
/**
* @private
* @type {Array}
*/
var tagNodes = void 0;
/**
* @private
* @type {Array}
*/
var tagNodesAttrName = void 0;
Token.prototype.getColumn = function getColumn() {
return getTokenColumn(this);
};
var options = {};
var tokenizer = null;
Token.prototype.toString = function toString() {
return convertTagToText(this);
};
// eslint-disable-next-line no-unused-vars
var tokens = null;
return Token;
}();
var TYPE_WORD = TOKEN_TYPE_WORD;
var TYPE_TAG = TOKEN_TYPE_TAG;
var TYPE_ATTR_NAME = TOKEN_TYPE_ATTR_NAME;
var TYPE_ATTR_VALUE = TOKEN_TYPE_ATTR_VALUE;
var TYPE_SPACE = TOKEN_TYPE_SPACE;
var TYPE_NEW_LINE = TOKEN_TYPE_NEW_LINE;
var createTokenizer = function createTokenizer(input, onToken) {
return createLexer(input, { onToken: onToken });
};
/* eslint-disable no-plusplus,no-param-reassign */
/**
* @private
* @param token
* @return {*}
*/
var isTagNested = function isTagNested(token) {
return tokenizer.isTokenNested(token);
};
var RESERVED_CHARS = [char_10, char_9, char_7, char_14, char_8, char_5, char_6, char_2];
var NOT_CHAR_TOKENS = [char_9, char_8, char_5, char_2];
var WHITESPACES = [char_8, char_5];
/**
* @private
* @return {TagNode}
*/
var getTagNode = function getTagNode() {
return tagNodes.length ? tagNodes[tagNodes.length - 1] : null;
};
var isCharReserved = function isCharReserved(char$$1) {
return RESERVED_CHARS.indexOf(char$$1) >= 0;
};
var isWhiteSpace = function isWhiteSpace(char$$1) {
return WHITESPACES.indexOf(char$$1) >= 0;
};
var isCharToken = function isCharToken(char$$1) {
return NOT_CHAR_TOKENS.indexOf(char$$1) === -1;
};
/**
* @private
* @param {Token} token
* @return {Array}
*/
var createTagNode = function createTagNode(token) {
return tagNodes.push(TagNode.create(token.getValue()));
};
/**
* @private
* @param {Token} token
* @return {Array}
*/
var createTagNodeAttrName = function createTagNodeAttrName(token) {
return tagNodesAttrName.push(token.getValue());
};
var createCharGrabber = function createCharGrabber(source) {
var idx = 0;
/**
* @private
* @return {Array}
*/
var getTagNodeAttrName = function getTagNodeAttrName() {
return tagNodesAttrName.length ? tagNodesAttrName[tagNodesAttrName.length - 1] : getTagNode().tag;
};
var skip = function skip() {
idx += 1;
};
var hasNext = function hasNext() {
return source.length > idx;
};
/**
* @private
* @return {Array}
*/
var clearTagNodeAttrName = function clearTagNodeAttrName() {
if (tagNodesAttrName.length) {
tagNodesAttrName.pop();
}
};
return {
skip: skip,
hasNext: hasNext,
isLast: function isLast() {
return idx === source.length;
},
grabWhile: function grabWhile(cond) {
var start = idx;
/**
* @private
* @return {Array}
*/
var clearTagNode = function clearTagNode() {
if (tagNodes.length) {
tagNodes.pop();
while (hasNext() && cond(source[idx])) {
skip();
}
clearTagNodeAttrName();
}
};
return source.substr(start, idx - start);
},
getNext: function getNext() {
return source[idx + 1];
},
getPrev: function getPrev() {
return source[idx - 1];
},
getCurr: function getCurr() {
return source[idx];
}
};
};
/**
* @private
* @return {Array}
*/
var getNodes = function getNodes() {
if (nestedNodes.length) {
var nestedNode = nestedNodes[nestedNodes.length - 1];
return nestedNode.content;
}
var trimChar = function trimChar(str, charToRemove) {
while (str.charAt(0) === charToRemove) {
str = str.substring(1);
}
return nodes;
};
while (str.charAt(str.length - 1) === charToRemove) {
str = str.substring(0, str.length - 1);
}
/**
* @private
* @param tag
*/
var appendNode = function appendNode(tag) {
getNodes().push(tag);
};
return str;
};
/**
* @private
* @param value
* @return {boolean}
*/
var isAllowedTag = function isAllowedTag(value) {
if (options.onlyAllowTags && options.onlyAllowTags.length) {
return options.onlyAllowTags.indexOf(value) >= 0;
}
var unquote = function unquote(str) {
return str.replace(char_14 + char_7, char_7);
};
var createToken = function createToken(type, value) {
var r = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var cl = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
return new Token(type, value, r, cl);
};
return true;
};
/**
* @private
* @param {Token} token
*/
var handleTagStart = function handleTagStart(token) {
if (token.isStart()) {
createTagNode(token);
function createLexer(buffer) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (isTagNested(token)) {
nestedNodes.push(getTagNode());
} else {
appendNode(getTagNode());
clearTagNode();
}
}
};
var row = 0;
var col = 0;
/**
* @private
* @param {Token} token
*/
var handleTagEnd = function handleTagEnd(token) {
if (token.isEnd()) {
clearTagNode();
var tokenIndex = -1;
var tokens = new Array(Math.floor(buffer.length));
var emitToken = function emitToken(token) {
if (options.onToken) {
options.onToken(token);
}
var lastNestedNode = nestedNodes.pop();
tokenIndex += 1;
tokens[tokenIndex] = token;
};
if (lastNestedNode) {
appendNode(lastNestedNode);
} else if (options.onError) {
var tag = token.getValue();
var line = token.getLine();
var column = token.getColumn();
options.onError({
message: 'Inconsistent tag \'' + tag + '\' on line ' + line + ' and column ' + column,
lineNumber: line,
columnNumber: column
});
}
}
};
var parseAttrs = function parseAttrs(str) {
var tagName = null;
var skipSpaces = false;
/**
* @private
* @param {Token} token
*/
var handleTagToken = function handleTagToken(token) {
if (token.isTag()) {
if (isAllowedTag(token.getName())) {
// [tag]
handleTagStart(token);
var attrTokens = [];
var attrCharGrabber = createCharGrabber(str);
var validAttr = function validAttr(val) {
var isEQ = val === char_6;
var isWS = isWhiteSpace(val);
var isPrevSLASH = attrCharGrabber.getPrev() === char_11;
// [/tag]
handleTagEnd(token);
} else {
appendNode(token.toString());
}
}
};
if (tagName === null) {
return !(isEQ || isWS || attrCharGrabber.isLast());
}
/**
* @private
* @param {Token} token
*/
var handleTagNode = function handleTagNode(token) {
var tagNode = getTagNode();
if (skipSpaces && isWS) {
return true;
}
if (tagNode) {
if (token.isAttrName()) {
createTagNodeAttrName(token);
tagNode.attr(getTagNodeAttrName(), null);
} else if (token.isAttrValue()) {
tagNode.attr(getTagNodeAttrName(), token.getValue());
clearTagNodeAttrName();
} else if (token.isText()) {
tagNode.append(token.getValue());
}
} else if (token.isText()) {
appendNode(token.getValue());
}
};
if (val === char_7 && !isPrevSLASH) {
skipSpaces = !skipSpaces;
}
/**
* @private
* @param token
*/
var parseToken = function parseToken(token) {
handleTagToken(token);
handleTagNode(token);
};
return !(isEQ || isWS);
};
/**
* @public
* @return {Array}
*/
var parse = function parse(input) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var nextAttr = function nextAttr() {
var attrStr = attrCharGrabber.grabWhile(validAttr);
options = opts;
tokenizer = (opts.createTokenizer ? opts.createTokenizer : createTokenizer)(input, parseToken);
// first string before space is a tag name
if (tagName === null) {
tagName = attrStr;
} else if (isWhiteSpace(attrCharGrabber.getCurr()) || !attrCharGrabber.hasNext()) {
var escaped = unquote(trimChar(attrStr, char_7));
attrTokens.push(createToken(TYPE_ATTR_VALUE, escaped, row, col));
} else {
attrTokens.push(createToken(TYPE_ATTR_NAME, attrStr, row, col));
}
nodes = [];
nestedNodes = [];
tagNodes = [];
tagNodesAttrName = [];
attrCharGrabber.skip();
};
tokens = tokenizer.tokenize();
while (attrCharGrabber.hasNext()) {
nextAttr();
}
return nodes;
};
return { tag: tagName, attrs: attrTokens };
};
exports.parse = parse;
exports.createTagNode = createTagNode;
var grabber = createCharGrabber(buffer);
Object.defineProperty(exports, '__esModule', { value: true });
var next = function next() {
var char$$1 = grabber.getCurr();
if (char$$1 === char_2) {
grabber.skip();
col = 0;
row++;
emitToken(createToken(TYPE_NEW_LINE, char$$1, row, col));
} else if (isWhiteSpace(char$$1)) {
var str = grabber.grabWhile(isWhiteSpace);
emitToken(createToken(TYPE_SPACE, str, row, col));
} else if (char$$1 === char_9) {
var nextChar = grabber.getNext();
grabber.skip(); // skip [
if (isCharReserved(nextChar)) {
emitToken(createToken(TYPE_WORD, char$$1, row, col));
} else {
var _str = grabber.grabWhile(function (val) {
return val !== char_10;
});
grabber.skip(); // skip ]
if (!(_str.indexOf(char_6) > 0) || _str[0] === char_11) {
emitToken(createToken(TYPE_TAG, _str, row, col));
} else {
var parsed = parseAttrs(_str);
emitToken(createToken(TYPE_TAG, parsed.tag, row, col));
parsed.attrs.map(emitToken);
}
}
} else if (char$$1 === char_10) {
grabber.skip();
emitToken(createToken(TYPE_WORD, char$$1, row, col));
} else if (isCharToken(char$$1)) {
var _str2 = grabber.grabWhile(isCharToken);
emitToken(createToken(TYPE_WORD, _str2, row, col));
}
};
var tokenize = function tokenize() {
while (grabber.hasNext()) {
next();
}
tokens.length = tokenIndex + 1;
return tokens;
};
var isTokenNested = function isTokenNested(token) {
var value = char_9 + char_11 + token.getValue();
return buffer.indexOf(value) > -1;
};
return {
tokenize: tokenize,
isTokenNested: isTokenNested
};
}
/**
* @private
* @type {Array}
*/
var nodes = void 0;
/**
* @private
* @type {Array}
*/
var nestedNodes = void 0;
/**
* @private
* @type {Array}
*/
var tagNodes = void 0;
/**
* @private
* @type {Array}
*/
var tagNodesAttrName = void 0;
var options = {};
var tokenizer = null;
// eslint-disable-next-line no-unused-vars
var tokens = null;
var createTokenizer = function createTokenizer(input, onToken) {
return createLexer(input, { onToken: onToken });
};
/**
* @private
* @param token
* @return {*}
*/
var isTagNested = function isTagNested(token) {
return tokenizer.isTokenNested(token);
};
/**
* @private
* @return {TagNode}
*/
var getTagNode = function getTagNode() {
return tagNodes.length ? tagNodes[tagNodes.length - 1] : null;
};
/**
* @private
* @param {Token} token
* @return {Array}
*/
var createTagNode = function createTagNode(token) {
return tagNodes.push(TagNode.create(token.getValue()));
};
/**
* @private
* @param {Token} token
* @return {Array}
*/
var createTagNodeAttrName = function createTagNodeAttrName(token) {
return tagNodesAttrName.push(token.getValue());
};
/**
* @private
* @return {Array}
*/
var getTagNodeAttrName = function getTagNodeAttrName() {
return tagNodesAttrName.length ? tagNodesAttrName[tagNodesAttrName.length - 1] : getTagNode().tag;
};
/**
* @private
* @return {Array}
*/
var clearTagNodeAttrName = function clearTagNodeAttrName() {
if (tagNodesAttrName.length) {
tagNodesAttrName.pop();
}
};
/**
* @private
* @return {Array}
*/
var clearTagNode = function clearTagNode() {
if (tagNodes.length) {
tagNodes.pop();
clearTagNodeAttrName();
}
};
/**
* @private
* @return {Array}
*/
var getNodes = function getNodes() {
if (nestedNodes.length) {
var nestedNode = nestedNodes[nestedNodes.length - 1];
return nestedNode.content;
}
return nodes;
};
/**
* @private
* @param tag
*/
var appendNode = function appendNode(tag) {
getNodes().push(tag);
};
/**
* @private
* @param value
* @return {boolean}
*/
var isAllowedTag = function isAllowedTag(value) {
if (options.onlyAllowTags && options.onlyAllowTags.length) {
return options.onlyAllowTags.indexOf(value) >= 0;
}
return true;
};
/**
* @private
* @param {Token} token
*/
var handleTagStart = function handleTagStart(token) {
if (token.isStart()) {
createTagNode(token);
if (isTagNested(token)) {
nestedNodes.push(getTagNode());
} else {
appendNode(getTagNode());
clearTagNode();
}
}
};
/**
* @private
* @param {Token} token
*/
var handleTagEnd = function handleTagEnd(token) {
if (token.isEnd()) {
clearTagNode();
var lastNestedNode = nestedNodes.pop();
if (lastNestedNode) {
appendNode(lastNestedNode);
} else if (options.onError) {
var tag = token.getValue();
var line = token.getLine();
var column = token.getColumn();
options.onError({
message: 'Inconsistent tag \'' + tag + '\' on line ' + line + ' and column ' + column,
lineNumber: line,
columnNumber: column
});
}
}
};
/**
* @private
* @param {Token} token
*/
var handleTagToken = function handleTagToken(token) {
if (token.isTag()) {
if (isAllowedTag(token.getName())) {
// [tag]
handleTagStart(token);
// [/tag]
handleTagEnd(token);
} else {
appendNode(token.toString());
}
}
};
/**
* @private
* @param {Token} token
*/
var handleTagNode = function handleTagNode(token) {
var tagNode = getTagNode();
if (tagNode) {
if (token.isAttrName()) {
createTagNodeAttrName(token);
tagNode.attr(getTagNodeAttrName(), null);
} else if (token.isAttrValue()) {
tagNode.attr(getTagNodeAttrName(), token.getValue());
clearTagNodeAttrName();
} else if (token.isText()) {
tagNode.append(token.getValue());
}
} else if (token.isText()) {
appendNode(token.getValue());
}
};
/**
* @private
* @param token
*/
var parseToken = function parseToken(token) {
handleTagToken(token);
handleTagNode(token);
};
/**
* @public
* @return {Array}
*/
var parse = function parse(input) {
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
options = opts;
tokenizer = (opts.createTokenizer ? opts.createTokenizer : createTokenizer)(input, parseToken);
nodes = [];
nestedNodes = [];
tagNodes = [];
tagNodesAttrName = [];
tokens = tokenizer.tokenize();
return nodes;
};
exports.parse = parse;
exports.createTagNode = createTagNode;
Object.defineProperty(exports, '__esModule', { value: true });
})));
{
"name": "@bbob/parser",
"version": "1.3.1",
"version": "1.3.2",
"description": "Just parses BBcode to AST array",

@@ -16,3 +16,3 @@ "keywords": [

"dependencies": {
"@bbob/plugin-helper": "^1.1.1"
"@bbob/plugin-helper": "1.x"
},

@@ -42,3 +42,4 @@ "main": "lib/index.js",

"lint": "../../node_modules/.bin/eslint .",
"size": "../../node_modules/.bin/size-limit"
"size": "../../node_modules/.bin/size-limit",
"prepublishOnly": "npm run build"
},

@@ -45,0 +46,0 @@ "size-limit": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc