@bbob/parser
Advanced tools
Comparing version 2.7.1 to 2.8.0
2585
dist/index.js
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = global || self, factory(global.BbobParser = {})); | ||
}(this, (function (exports) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BbobParser = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
function unwrapExports (x) { | ||
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; | ||
} | ||
var TagNode$1 = {}; | ||
function createCommonjsModule(fn, module) { | ||
return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
} | ||
var char = {}; | ||
var char_1 = createCommonjsModule(function (module, exports) { | ||
Object.defineProperty(char, "__esModule", { | ||
value: true | ||
}); | ||
var BACKSLASH_1 = char.BACKSLASH = char.PLACEHOLDER_SPACE = char.PLACEHOLDER_SPACE_TAB = SLASH_1 = char.SLASH = CLOSE_BRAKET_1 = char.CLOSE_BRAKET = OPEN_BRAKET_1 = char.OPEN_BRAKET = SPACE_1 = char.SPACE = QUOTEMARK_1 = char.QUOTEMARK = EQ_1 = char.EQ = TAB_1 = char.TAB = char.R = char.F = N_1 = char.N = void 0; | ||
var N = '\n'; | ||
var TAB = '\t'; | ||
var F = '\f'; | ||
var R = '\r'; | ||
var EQ = '='; | ||
var QUOTEMARK = '"'; | ||
var SPACE = ' '; | ||
var OPEN_BRAKET = '['; | ||
var CLOSE_BRAKET = ']'; | ||
var SLASH = '/'; | ||
var BACKSLASH = '\\'; | ||
var PLACEHOLDER_SPACE_TAB = ' '; | ||
var PLACEHOLDER_SPACE = ' '; | ||
var N_1 = char.N = N; | ||
char.F = F; | ||
char.R = R; | ||
var TAB_1 = char.TAB = TAB; | ||
var EQ_1 = char.EQ = EQ; | ||
var QUOTEMARK_1 = char.QUOTEMARK = QUOTEMARK; | ||
var SPACE_1 = char.SPACE = SPACE; | ||
var OPEN_BRAKET_1 = char.OPEN_BRAKET = OPEN_BRAKET; | ||
var CLOSE_BRAKET_1 = char.CLOSE_BRAKET = CLOSE_BRAKET; | ||
var SLASH_1 = char.SLASH = SLASH; | ||
char.PLACEHOLDER_SPACE_TAB = PLACEHOLDER_SPACE_TAB; | ||
char.PLACEHOLDER_SPACE = PLACEHOLDER_SPACE; | ||
BACKSLASH_1 = char.BACKSLASH = BACKSLASH; | ||
exports.__esModule = true; | ||
exports.BACKSLASH = exports.PLACEHOLDER_SPACE = exports.PLACEHOLDER_SPACE_TAB = exports.SLASH = exports.CLOSE_BRAKET = exports.OPEN_BRAKET = exports.SPACE = exports.QUOTEMARK = exports.EQ = exports.TAB = exports.R = exports.F = exports.N = void 0; | ||
var N = '\n'; | ||
exports.N = N; | ||
var TAB = '\t'; | ||
exports.TAB = TAB; | ||
var F = '\f'; | ||
exports.F = F; | ||
var R = '\r'; | ||
exports.R = R; | ||
var EQ = '='; | ||
exports.EQ = EQ; | ||
var QUOTEMARK = '"'; | ||
exports.QUOTEMARK = QUOTEMARK; | ||
var SPACE = ' '; | ||
exports.SPACE = SPACE; | ||
var OPEN_BRAKET = '['; | ||
exports.OPEN_BRAKET = OPEN_BRAKET; | ||
var CLOSE_BRAKET = ']'; | ||
exports.CLOSE_BRAKET = CLOSE_BRAKET; | ||
var SLASH = '/'; | ||
exports.SLASH = SLASH; | ||
var BACKSLASH = '\\'; | ||
exports.BACKSLASH = BACKSLASH; | ||
var PLACEHOLDER_SPACE_TAB = ' '; | ||
exports.PLACEHOLDER_SPACE_TAB = PLACEHOLDER_SPACE_TAB; | ||
var PLACEHOLDER_SPACE = ' '; // const getChar = String.fromCharCode; | ||
var lib = {}; | ||
exports.PLACEHOLDER_SPACE = PLACEHOLDER_SPACE; | ||
}); | ||
Object.defineProperty(lib, "__esModule", { | ||
value: true | ||
}); | ||
lib.isEOL = lib.isStringNode = isTagNode_1 = lib.isTagNode = lib.getUniqAttr = lib.getNodeLength = lib.escapeHTML = lib.appendToNode = lib.attrValue = lib.attrsToString = void 0; | ||
var _char$1 = char; | ||
function _arrayWithoutHoles(arr) { | ||
if (Array.isArray(arr)) { | ||
for(var i = 0, arr2 = new Array(arr.length); i < arr.length; i++){ | ||
arr2[i] = arr[i]; | ||
} | ||
return arr2; | ||
} | ||
} | ||
function _iterableToArray(iter) { | ||
if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); | ||
} | ||
function _nonIterableSpread() { | ||
throw new TypeError("Invalid attempt to spread non-iterable instance"); | ||
} | ||
function _toConsumableArray(arr) { | ||
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); | ||
} | ||
var _typeof = function(obj) { | ||
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; | ||
}; | ||
var isTagNode = function(el) { | ||
return typeof el === 'object' && !!el.tag; | ||
}; | ||
var isStringNode = function(el) { | ||
return typeof el === 'string'; | ||
}; | ||
var isEOL = function(el) { | ||
return el === _char$1.N; | ||
}; | ||
var keysReduce = function(obj, reduce, def) { | ||
return Object.keys(obj).reduce(reduce, def); | ||
}; | ||
var getNodeLength = function(node) { | ||
if (isTagNode(node)) { | ||
return node.content.reduce(function(count, contentNode) { | ||
return count + getNodeLength(contentNode); | ||
}, 0); | ||
} | ||
if (isStringNode(node)) { | ||
return node.length; | ||
} | ||
return 0; | ||
}; | ||
/** | ||
* Appends value to Tag Node | ||
* @param {TagNode} node | ||
* @param value | ||
*/ var appendToNode = function(node, value) { | ||
node.content.push(value); | ||
}; | ||
/** | ||
* Replaces " to &qquot; | ||
* @param {String} value | ||
*/ var escapeHTML = function(value) { | ||
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''')// eslint-disable-next-line no-script-url | ||
.replace(/(javascript):/gi, '$1%3A'); | ||
}; | ||
/** | ||
* Acept name and value and return valid html5 attribute string | ||
* @param {String} name | ||
* @param {String} value | ||
* @return {string} | ||
*/ var attrValue = function(name, value) { | ||
var type = typeof value === "undefined" ? "undefined" : _typeof(value); | ||
var types = { | ||
boolean: function() { | ||
return value ? "".concat(name) : ''; | ||
}, | ||
number: function() { | ||
return "".concat(name, "=\"").concat(value, "\""); | ||
}, | ||
string: function() { | ||
return "".concat(name, "=\"").concat(escapeHTML(value), "\""); | ||
}, | ||
object: function() { | ||
return "".concat(name, "=\"").concat(escapeHTML(JSON.stringify(value)), "\""); | ||
} | ||
}; | ||
return types[type] ? types[type]() : ''; | ||
}; | ||
/** | ||
* Transforms attrs to html params string | ||
* @param values | ||
*/ var attrsToString = function(values) { | ||
// To avoid some malformed attributes | ||
if (values == null) { | ||
return ''; | ||
} | ||
return keysReduce(values, function(arr, key) { | ||
return _toConsumableArray(arr).concat([ | ||
attrValue(key, values[key]) | ||
]); | ||
}, [ | ||
'' | ||
]).join(' '); | ||
}; | ||
/** | ||
* Gets value from | ||
* @example | ||
* getUniqAttr({ 'foo': true, 'bar': bar' }) => 'bar' | ||
* @param attrs | ||
* @returns {string} | ||
*/ var getUniqAttr = function(attrs) { | ||
return keysReduce(attrs, function(res, key) { | ||
return attrs[key] === key ? attrs[key] : null; | ||
}, null); | ||
}; | ||
lib.attrsToString = attrsToString; | ||
lib.attrValue = attrValue; | ||
lib.appendToNode = appendToNode; | ||
lib.escapeHTML = escapeHTML; | ||
lib.getNodeLength = getNodeLength; | ||
lib.getUniqAttr = getUniqAttr; | ||
var isTagNode_1 = lib.isTagNode = isTagNode; | ||
lib.isStringNode = isStringNode; | ||
lib.isEOL = isEOL; | ||
unwrapExports(char_1); | ||
var char_2 = char_1.BACKSLASH; | ||
var char_3 = char_1.PLACEHOLDER_SPACE; | ||
var char_4 = char_1.PLACEHOLDER_SPACE_TAB; | ||
var char_5 = char_1.SLASH; | ||
var char_6 = char_1.CLOSE_BRAKET; | ||
var char_7 = char_1.OPEN_BRAKET; | ||
var char_8 = char_1.SPACE; | ||
var char_9 = char_1.QUOTEMARK; | ||
var char_10 = char_1.EQ; | ||
var char_11 = char_1.TAB; | ||
var char_12 = char_1.R; | ||
var char_13 = char_1.F; | ||
var char_14 = char_1.N; | ||
Object.defineProperty(TagNode$1, "__esModule", { | ||
value: true | ||
}); | ||
var default_1 = TagNode$1.default = exports.TagNode = TagNode$1.TagNode = void 0; | ||
var _char = char; | ||
var _index = lib; | ||
function _classCallCheck$1(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _defineProperties$1(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); | ||
} | ||
} | ||
function _createClass$1(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties$1(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties$1(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
function _objectSpread(target) { | ||
for(var i = 1; i < arguments.length; i++){ | ||
var source = arguments[i] != null ? arguments[i] : { | ||
}; | ||
var ownKeys = Object.keys(source); | ||
if (typeof Object.getOwnPropertySymbols === "function") { | ||
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) { | ||
return Object.getOwnPropertyDescriptor(source, sym).enumerable; | ||
})); | ||
} | ||
ownKeys.forEach(function(key) { | ||
_defineProperty(target, key, source[key]); | ||
}); | ||
} | ||
return target; | ||
} | ||
var getTagAttrs = function(tag, params) { | ||
var uniqAattr = (_index).getUniqAttr(params); | ||
if (uniqAattr) { | ||
var tagAttr = (_index).attrValue(tag, uniqAattr); | ||
var attrs = _objectSpread({ | ||
}, params); | ||
delete attrs[uniqAattr]; | ||
var attrsStr = (_index).attrsToString(attrs); | ||
return "".concat(tagAttr).concat(attrsStr); | ||
} | ||
return "".concat(tag).concat((_index).attrsToString(params)); | ||
}; | ||
var TagNode = /*#__PURE__*/ function() { | ||
function TagNode(tag, attrs, content) { | ||
_classCallCheck$1(this, TagNode); | ||
this.tag = tag; | ||
this.attrs = attrs; | ||
this.content = Array.isArray(content) ? content : [ | ||
content | ||
]; | ||
} | ||
_createClass$1(TagNode, [ | ||
{ | ||
key: "attr", | ||
value: function attr(name, value) { | ||
if (typeof value !== 'undefined') { | ||
this.attrs[name] = value; | ||
} | ||
return this.attrs[name]; | ||
} | ||
}, | ||
{ | ||
key: "append", | ||
value: function append(value) { | ||
return (_index).appendToNode(this, value); | ||
} | ||
}, | ||
{ | ||
key: "length", | ||
get: function get() { | ||
return (_index).getNodeLength(this); | ||
} | ||
}, | ||
{ | ||
key: "toTagStart", | ||
value: function toTagStart(param) { | ||
var ref = param === void 0 ? { | ||
} : param, _openTag = ref.openTag, openTag = _openTag === void 0 ? _char.OPEN_BRAKET : _openTag, _closeTag = ref.closeTag, closeTag = _closeTag === void 0 ? _char.CLOSE_BRAKET : _closeTag; | ||
var tagAttrs = getTagAttrs(this.tag, this.attrs); | ||
return "".concat(openTag).concat(tagAttrs).concat(closeTag); | ||
} | ||
}, | ||
{ | ||
key: "toTagEnd", | ||
value: function toTagEnd(param) { | ||
var ref = param === void 0 ? { | ||
} : param, _openTag = ref.openTag, openTag = _openTag === void 0 ? _char.OPEN_BRAKET : _openTag, _closeTag = ref.closeTag, closeTag = _closeTag === void 0 ? _char.CLOSE_BRAKET : _closeTag; | ||
return "".concat(openTag).concat(_char.SLASH).concat(this.tag).concat(closeTag); | ||
} | ||
}, | ||
{ | ||
key: "toTagNode", | ||
value: function toTagNode() { | ||
return new TagNode(this.tag.toLowerCase(), this.attrs, this.content); | ||
} | ||
}, | ||
{ | ||
key: "toString", | ||
value: function toString(param) { | ||
var ref = param === void 0 ? { | ||
} : param, _openTag = ref.openTag, openTag = _openTag === void 0 ? _char.OPEN_BRAKET : _openTag, _closeTag = ref.closeTag, closeTag = _closeTag === void 0 ? _char.CLOSE_BRAKET : _closeTag; | ||
var isEmpty = this.content.length === 0; | ||
var content = this.content.reduce(function(r, node) { | ||
return r + node.toString({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
}); | ||
}, ''); | ||
var tagStart = this.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
}); | ||
if (isEmpty) { | ||
return tagStart; | ||
} | ||
return "".concat(tagStart).concat(content).concat(this.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
} | ||
]); | ||
return TagNode; | ||
}(); | ||
TagNode.create = function(tag, param, param1) { | ||
var attrs = param === void 0 ? { | ||
} : param, content = param1 === void 0 ? [] : param1; | ||
return new TagNode(tag, attrs, content); | ||
}; | ||
TagNode.isOf = function(node, type) { | ||
return node.tag === type; | ||
}; | ||
exports.TagNode = TagNode$1.TagNode = TagNode; | ||
var _default = TagNode; | ||
default_1 = TagNode$1.default = _default; | ||
var lib = createCommonjsModule(function (module, exports) { | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a 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); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
// 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 TOKEN_TYPE_WORD = 1; // 'word'; | ||
var TOKEN_TYPE_TAG = 2; // 'tag'; | ||
var TOKEN_TYPE_ATTR_NAME = 3; // 'attr-name'; | ||
var TOKEN_TYPE_ATTR_VALUE = 4; // 'attr-value'; | ||
var TOKEN_TYPE_SPACE = 5; // 'space'; | ||
var TOKEN_TYPE_NEW_LINE = 6; // 'new-line'; | ||
/** | ||
* @param {Token} token | ||
* @returns {string} | ||
*/ var getTokenValue = function(token) { | ||
if (token && typeof token[TOKEN_VALUE_ID] !== 'undefined') { | ||
return token[TOKEN_VALUE_ID]; | ||
} | ||
return ''; | ||
}; | ||
/** | ||
* @param {Token}token | ||
* @returns {number} | ||
*/ var getTokenLine = function(token) { | ||
return token && token[TOKEN_LINE_ID] || 0; | ||
}; | ||
var getTokenColumn = function(token) { | ||
return token && token[TOKEN_COLUMN_ID] || 0; | ||
}; | ||
/** | ||
* @param {Token} token | ||
* @returns {boolean} | ||
*/ var isTextToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD; | ||
} | ||
return false; | ||
}; | ||
/** | ||
* @param {Token} token | ||
* @returns {boolean} | ||
*/ var isTagToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG; | ||
} | ||
return false; | ||
}; | ||
var isTagEnd = function(token) { | ||
return getTokenValue(token).charCodeAt(0) === SLASH_1.charCodeAt(0); | ||
}; | ||
var isTagStart = function(token) { | ||
return !isTagEnd(token); | ||
}; | ||
var isAttrNameToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME; | ||
} | ||
return false; | ||
}; | ||
/** | ||
* @param {Token} token | ||
* @returns {boolean} | ||
*/ var isAttrValueToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE; | ||
} | ||
return false; | ||
}; | ||
var getTagName = function(token) { | ||
var value = getTokenValue(token); | ||
return isTagEnd(token) ? value.slice(1) : value; | ||
}; | ||
var convertTagToText = function(token) { | ||
var text = OPEN_BRAKET_1; | ||
text += getTokenValue(token); | ||
text += CLOSE_BRAKET_1; | ||
return text; | ||
}; | ||
var Token = /*#__PURE__*/ function() { | ||
function Token(type, value, line, row) { | ||
_classCallCheck(this, Token); | ||
this[TOKEN_TYPE_ID] = Number(type); | ||
this[TOKEN_VALUE_ID] = String(value); | ||
this[TOKEN_LINE_ID] = Number(line); | ||
this[TOKEN_COLUMN_ID] = Number(row); | ||
} | ||
_createClass(Token, [ | ||
{ | ||
key: "isEmpty", | ||
value: function isEmpty() { | ||
// eslint-disable-next-line no-restricted-globals | ||
return isNaN(this[TOKEN_TYPE_ID]); | ||
} | ||
}, | ||
{ | ||
key: "isText", | ||
value: function isText() { | ||
return isTextToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isTag", | ||
value: function isTag() { | ||
return isTagToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isAttrName", | ||
value: function isAttrName() { | ||
return isAttrNameToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isAttrValue", | ||
value: function isAttrValue() { | ||
return isAttrValueToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isStart", | ||
value: function isStart() { | ||
return isTagStart(this); | ||
} | ||
}, | ||
{ | ||
key: "isEnd", | ||
value: function isEnd() { | ||
return isTagEnd(this); | ||
} | ||
}, | ||
{ | ||
key: "getName", | ||
value: function getName() { | ||
return getTagName(this); | ||
} | ||
}, | ||
{ | ||
key: "getValue", | ||
value: function getValue() { | ||
return getTokenValue(this); | ||
} | ||
}, | ||
{ | ||
key: "getLine", | ||
value: function getLine() { | ||
return getTokenLine(this); | ||
} | ||
}, | ||
{ | ||
key: "getColumn", | ||
value: function getColumn() { | ||
return getTokenColumn(this); | ||
} | ||
}, | ||
{ | ||
key: "toString", | ||
value: function toString() { | ||
return convertTagToText(this); | ||
} | ||
} | ||
]); | ||
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; | ||
exports.__esModule = true; | ||
exports.isEOL = exports.isStringNode = exports.isTagNode = exports.getUniqAttr = exports.getNodeLength = exports.escapeHTML = exports.appendToNode = exports.attrValue = exports.attrsToString = void 0; | ||
function CharGrabber(source, options) { | ||
var cursor = { | ||
pos: 0, | ||
len: source.length | ||
}; | ||
var substrUntilChar = function(char) { | ||
var pos = cursor.pos; | ||
var idx = source.indexOf(char, pos); | ||
return idx >= 0 ? source.substr(pos, idx - pos) : ''; | ||
}; | ||
var includes = function(val) { | ||
return source.indexOf(val, cursor.pos) >= 0; | ||
}; | ||
var hasNext = function() { | ||
return cursor.len > cursor.pos; | ||
}; | ||
var isLast = function() { | ||
return cursor.pos === cursor.len; | ||
}; | ||
var skip = function(param, silent) { | ||
var num = param === void 0 ? 1 : param; | ||
cursor.pos += num; | ||
if (options && options.onSkip && !silent) { | ||
options.onSkip(); | ||
} | ||
}; | ||
var rest = function() { | ||
return source.substr(cursor.pos); | ||
}; | ||
var curr = function() { | ||
return source[cursor.pos]; | ||
}; | ||
var prev = function() { | ||
var prevPos = cursor.pos - 1; | ||
return typeof source[prevPos] !== 'undefined' ? source[prevPos] : null; | ||
}; | ||
var next = function() { | ||
var nextPos = cursor.pos + 1; | ||
return nextPos <= source.length - 1 ? source[nextPos] : null; | ||
}; | ||
var grabWhile = function(cond, silent) { | ||
var start = 0; | ||
if (hasNext()) { | ||
start = cursor.pos; | ||
while(hasNext() && cond(curr())){ | ||
skip(1, silent); | ||
} | ||
} | ||
return source.substr(start, cursor.pos - start); | ||
}; | ||
/** | ||
* @type {skip} | ||
*/ this.skip = skip; | ||
/** | ||
* @returns {Boolean} | ||
*/ this.hasNext = hasNext; | ||
/** | ||
* @returns {String} | ||
*/ this.getCurr = curr; | ||
/** | ||
* @returns {String} | ||
*/ this.getRest = rest; | ||
/** | ||
* @returns {String} | ||
*/ this.getNext = next; | ||
/** | ||
* @returns {String} | ||
*/ this.getPrev = prev; | ||
/** | ||
* @returns {Boolean} | ||
*/ this.isLast = isLast; | ||
/** | ||
* @returns {Boolean} | ||
*/ this.includes = includes; | ||
/** | ||
* @param {Function} cond | ||
* @param {Boolean} silent | ||
* @return {String} | ||
*/ this.grabWhile = grabWhile; | ||
/** | ||
* Grabs rest of string until it find a char | ||
* @param {String} char | ||
* @return {String} | ||
*/ this.substrUntilChar = substrUntilChar; | ||
} | ||
/** | ||
* Creates a grabber wrapper for source string, that helps to iterate over string char by char | ||
* @param {String} source | ||
* @param {Object} options | ||
* @param {Function} options.onSkip | ||
* @return CharGrabber | ||
*/ var createCharGrabber = function(source, options) { | ||
return new CharGrabber(source, options); | ||
}; | ||
/** | ||
* Trims string from start and end by char | ||
* @example | ||
* trimChar('*hello*', '*') ==> 'hello' | ||
* @param {String} str | ||
* @param {String} charToRemove | ||
* @returns {String} | ||
*/ var trimChar = function(str, charToRemove) { | ||
while(str.charAt(0) === charToRemove){ | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(1); | ||
} | ||
while(str.charAt(str.length - 1) === charToRemove){ | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
}; | ||
/** | ||
* Unquotes \" to " | ||
* @param str | ||
* @return {String} | ||
*/ var unquote = function(str) { | ||
return str.replace(BACKSLASH_1 + QUOTEMARK_1, QUOTEMARK_1); | ||
}; | ||
function NodeList(param) { | ||
var values = param === void 0 ? [] : param; | ||
var nodes = values; | ||
var getLast = function() { | ||
return Array.isArray(nodes) && nodes.length > 0 && typeof nodes[nodes.length - 1] !== 'undefined' ? nodes[nodes.length - 1] : null; | ||
}; | ||
var flushLast = function() { | ||
return nodes.length ? nodes.pop() : false; | ||
}; | ||
var push = function(value) { | ||
return nodes.push(value); | ||
}; | ||
var toArray = function() { | ||
return nodes; | ||
}; | ||
this.push = push; | ||
this.toArray = toArray; | ||
this.getLast = getLast; | ||
this.flushLast = flushLast; | ||
} | ||
/** | ||
* | ||
* @param values | ||
* @return {NodeList} | ||
*/ var createList = function(param) { | ||
var values = param === void 0 ? [] : param; | ||
return new NodeList(values); | ||
}; | ||
// for cases <!-- --> | ||
var EM = '!'; | ||
/** | ||
* Creates a Token entity class | ||
* @param {Number} type | ||
* @param {String} value | ||
* @param {Number} r line number | ||
* @param {Number} cl char number in line | ||
*/ var createToken = function(type, value, param, param1) { | ||
var r = param === void 0 ? 0 : param, cl = param1 === void 0 ? 0 : param1; | ||
return new Token(type, value, r, cl); | ||
}; | ||
/** | ||
* @typedef {Object} Lexer | ||
* @property {Function} tokenize | ||
* @property {Function} isTokenNested | ||
*/ /** | ||
* @param {String} buffer | ||
* @param {Object} options | ||
* @param {Function} options.onToken | ||
* @param {String} options.openTag | ||
* @param {String} options.closeTag | ||
* @param {Boolean} options.enableEscapeTags | ||
* @return {Lexer} | ||
*/ function createLexer(buffer, param) { | ||
var options = param === void 0 ? { | ||
} : param; | ||
var emitToken = /** | ||
* Emits newly created token to subscriber | ||
* @param {Number} type | ||
* @param {String} value | ||
*/ function emitToken(type, value) { | ||
var token = createToken(type, value, row, col); | ||
onToken(token); | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
}; | ||
var nextTagState = function nextTagState(tagChars, isSingleValueTag) { | ||
if (tagMode === TAG_STATE_ATTR) { | ||
var validAttrName = function(char) { | ||
return !(char === EQ_1 || isWhiteSpace(char)); | ||
}; | ||
var name = tagChars.grabWhile(validAttrName); | ||
var isEnd = tagChars.isLast(); | ||
var isValue = tagChars.getCurr() !== EQ_1; | ||
tagChars.skip(); | ||
if (isEnd || isValue) { | ||
emitToken(TYPE_ATTR_VALUE, unq(name)); | ||
} else { | ||
emitToken(TYPE_ATTR_NAME, name); | ||
} | ||
if (isEnd) { | ||
return TAG_STATE_NAME; | ||
} | ||
if (isValue) { | ||
return TAG_STATE_ATTR; | ||
} | ||
return TAG_STATE_VALUE; | ||
} | ||
if (tagMode === TAG_STATE_VALUE) { | ||
var stateSpecial = false; | ||
var validAttrValue = function(char) { | ||
// const isEQ = char === EQ; | ||
var isQM = char === QUOTEMARK_1; | ||
var prevChar = tagChars.getPrev(); | ||
var nextChar = tagChars.getNext(); | ||
var isPrevSLASH = prevChar === BACKSLASH_1; | ||
var isNextEQ = nextChar === EQ_1; | ||
var isWS = isWhiteSpace(char); | ||
// const isPrevWS = isWhiteSpace(prevChar); | ||
var isNextWS = isWhiteSpace(nextChar); | ||
if (stateSpecial && isSpecialChar(char)) { | ||
return true; | ||
} | ||
if (isQM && !isPrevSLASH) { | ||
stateSpecial = !stateSpecial; | ||
if (!stateSpecial && !(isNextEQ || isNextWS)) { | ||
return false; | ||
} | ||
} | ||
if (!isSingleValueTag) { | ||
return isWS === false; | ||
// return (isEQ || isWS) === false; | ||
} | ||
return true; | ||
}; | ||
var name = tagChars.grabWhile(validAttrValue); | ||
tagChars.skip(); | ||
emitToken(TYPE_ATTR_VALUE, unq(name)); | ||
if (tagChars.isLast()) { | ||
return TAG_STATE_NAME; | ||
} | ||
return TAG_STATE_ATTR; | ||
} | ||
var validName = function(char) { | ||
return !(char === EQ_1 || isWhiteSpace(char) || tagChars.isLast()); | ||
}; | ||
var name = tagChars.grabWhile(validName); | ||
emitToken(TYPE_TAG, name); | ||
tagChars.skip(); | ||
// in cases when we has [url=someval]GET[/url] and we dont need to parse all | ||
if (isSingleValueTag) { | ||
return TAG_STATE_VALUE; | ||
} | ||
var hasEQ = tagChars.includes(EQ_1); | ||
return hasEQ ? TAG_STATE_ATTR : TAG_STATE_VALUE; | ||
}; | ||
var stateTag = function stateTag() { | ||
var currChar = chars.getCurr(); | ||
if (currChar === openTag) { | ||
var nextChar = chars.getNext(); | ||
chars.skip(); | ||
// detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = chars.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || chars.isLast()) { | ||
emitToken(TYPE_WORD, currChar); | ||
return STATE_WORD; | ||
} | ||
// [myTag ] | ||
var isNoAttrsInTag = substr.indexOf(EQ_1) === -1; | ||
// [/myTag] | ||
var isClosingTag = substr[0] === SLASH_1; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
var name = chars.grabWhile(function(char) { | ||
return char !== closeTag; | ||
}); | ||
chars.skip(); // skip closeTag | ||
emitToken(TYPE_TAG, name); | ||
return STATE_WORD; | ||
} | ||
return STATE_TAG_ATTRS; | ||
} | ||
return STATE_WORD; | ||
}; | ||
var stateAttrs = function stateAttrs() { | ||
var silent = true; | ||
var tagStr = chars.grabWhile(function(char) { | ||
return char !== closeTag; | ||
}, silent); | ||
var tagGrabber = createCharGrabber(tagStr, { | ||
onSkip: onSkip | ||
}); | ||
var hasSpace = tagGrabber.includes(SPACE_1); | ||
tagMode = TAG_STATE_NAME; | ||
while(tagGrabber.hasNext()){ | ||
tagMode = nextTagState(tagGrabber, !hasSpace); | ||
} | ||
chars.skip(); // skip closeTag | ||
return STATE_WORD; | ||
}; | ||
var stateWord = function stateWord() { | ||
if (isNewLine(chars.getCurr())) { | ||
emitToken(TYPE_NEW_LINE, chars.getCurr()); | ||
chars.skip(); | ||
col = 0; | ||
row++; | ||
return STATE_WORD; | ||
} | ||
if (isWhiteSpace(chars.getCurr())) { | ||
emitToken(TYPE_SPACE, chars.grabWhile(isWhiteSpace)); | ||
return STATE_WORD; | ||
} | ||
if (chars.getCurr() === openTag) { | ||
if (chars.includes(closeTag)) { | ||
return STATE_TAG; | ||
} | ||
emitToken(TYPE_WORD, chars.getCurr()); | ||
chars.skip(); | ||
return STATE_WORD; | ||
} | ||
if (escapeTags) { | ||
if (isEscapeChar(chars.getCurr())) { | ||
var currChar = chars.getCurr(); | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // skip the \ without emitting anything | ||
if (isEscapableChar(nextChar)) { | ||
chars.skip(); // skip past the [, ] or \ as well | ||
emitToken(TYPE_WORD, nextChar); | ||
return STATE_WORD; | ||
} | ||
emitToken(TYPE_WORD, currChar); | ||
return STATE_WORD; | ||
} | ||
var isChar = function(char) { | ||
return isCharToken(char) && !isEscapeChar(char); | ||
}; | ||
emitToken(TYPE_WORD, chars.grabWhile(isChar)); | ||
return STATE_WORD; | ||
} | ||
emitToken(TYPE_WORD, chars.grabWhile(isCharToken)); | ||
return STATE_WORD; | ||
}; | ||
var tokenize = function tokenize() { | ||
stateMode = STATE_WORD; | ||
while(chars.hasNext()){ | ||
switch(stateMode){ | ||
case STATE_TAG: | ||
stateMode = stateTag(); | ||
break; | ||
case STATE_TAG_ATTRS: | ||
stateMode = stateAttrs(); | ||
break; | ||
case STATE_WORD: | ||
stateMode = stateWord(); | ||
break; | ||
default: | ||
stateMode = STATE_WORD; | ||
break; | ||
} | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
}; | ||
var isTokenNested = function isTokenNested(token) { | ||
var value = openTag + SLASH_1 + token.getValue(); | ||
// potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
}; | ||
var STATE_WORD = 0; | ||
var STATE_TAG = 1; | ||
var STATE_TAG_ATTRS = 2; | ||
var TAG_STATE_NAME = 0; | ||
var TAG_STATE_ATTR = 1; | ||
var TAG_STATE_VALUE = 2; | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var stateMode = STATE_WORD; | ||
var tagMode = TAG_STATE_NAME; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || OPEN_BRAKET_1; | ||
var closeTag = options.closeTag || CLOSE_BRAKET_1; | ||
var escapeTags = !!options.enableEscapeTags; | ||
var onToken = options.onToken || function() { | ||
}; | ||
var RESERVED_CHARS = [ | ||
closeTag, | ||
openTag, | ||
QUOTEMARK_1, | ||
BACKSLASH_1, | ||
SPACE_1, | ||
TAB_1, | ||
EQ_1, | ||
N_1, | ||
EM | ||
]; | ||
var NOT_CHAR_TOKENS = [ | ||
// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, | ||
SPACE_1, | ||
TAB_1, | ||
N_1, | ||
]; | ||
var WHITESPACES = [ | ||
SPACE_1, | ||
TAB_1 | ||
]; | ||
var SPECIAL_CHARS = [ | ||
EQ_1, | ||
SPACE_1, | ||
TAB_1 | ||
]; | ||
var isCharReserved = function(char) { | ||
return RESERVED_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isNewLine = function(char) { | ||
return char === N_1; | ||
}; | ||
var isWhiteSpace = function(char) { | ||
return WHITESPACES.indexOf(char) >= 0; | ||
}; | ||
var isCharToken = function(char) { | ||
return NOT_CHAR_TOKENS.indexOf(char) === -1; | ||
}; | ||
var isSpecialChar = function(char) { | ||
return SPECIAL_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isEscapableChar = function(char) { | ||
return char === openTag || char === closeTag || char === BACKSLASH_1; | ||
}; | ||
var isEscapeChar = function(char) { | ||
return char === BACKSLASH_1; | ||
}; | ||
var onSkip = function() { | ||
col++; | ||
}; | ||
var unq = function(val) { | ||
return unquote(trimChar(val, QUOTEMARK_1)); | ||
}; | ||
var chars = createCharGrabber(buffer, { | ||
onSkip: onSkip | ||
}); | ||
return { | ||
tokenize: tokenize, | ||
isTokenNested: isTokenNested | ||
}; | ||
} | ||
/** | ||
* @public | ||
* @param {String} input | ||
* @param {Object} opts | ||
* @param {Function} opts.createTokenizer | ||
* @param {Array<string>} opts.onlyAllowTags | ||
* @param {String} opts.openTag | ||
* @param {String} opts.closeTag | ||
* @param {Boolean} opts.enableEscapeTags | ||
* @return {Array} | ||
*/ var parse = function(input, param) { | ||
var opts = param === void 0 ? { | ||
} : param; | ||
var options = opts; | ||
var openTag = options.openTag || OPEN_BRAKET_1; | ||
var closeTag = options.closeTag || CLOSE_BRAKET_1; | ||
var tokenizer = null; | ||
/** | ||
* Result AST of nodes | ||
* @private | ||
* @type {NodeList} | ||
*/ var nodes = createList(); | ||
/** | ||
* Temp buffer of nodes that's nested to another node | ||
* @private | ||
* @type {NodeList} | ||
*/ var nestedNodes = createList(); | ||
/** | ||
* Temp buffer of nodes [tag..]...[/tag] | ||
* @private | ||
* @type {NodeList} | ||
*/ var tagNodes = createList(); | ||
/** | ||
* Temp buffer of tag attributes | ||
* @private | ||
* @type {NodeList} | ||
*/ var tagNodesAttrName = createList(); | ||
/** | ||
* Cache for nested tags checks | ||
*/ var nestedTagsMap = new Set(); | ||
/** | ||
* | ||
* @param token | ||
* @returns {boolean} | ||
*/ var isTokenNested = function(token) { | ||
var value = token.getValue(); | ||
if (!nestedTagsMap.has(value) && tokenizer.isTokenNested && tokenizer.isTokenNested(token)) { | ||
nestedTagsMap.add(value); | ||
return true; | ||
} | ||
return nestedTagsMap.has(value); | ||
}; | ||
/** | ||
* @param tagName | ||
* @returns {boolean} | ||
*/ var isTagNested = function(tagName) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
}; | ||
/** | ||
* @private | ||
* @param {String} value | ||
* @return {boolean} | ||
*/ var isAllowedTag = function(value) { | ||
if (options.onlyAllowTags && options.onlyAllowTags.length) { | ||
return options.onlyAllowTags.indexOf(value) >= 0; | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Flushes temp tag nodes and its attributes buffers | ||
* @private | ||
* @return {Array} | ||
*/ var flushTagNodes = function() { | ||
if (tagNodes.flushLast()) { | ||
tagNodesAttrName.flushLast(); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @return {Array} | ||
*/ var getNodes = function() { | ||
var lastNestedNode = nestedNodes.getLast(); | ||
if (lastNestedNode && Array.isArray(lastNestedNode.content)) { | ||
return lastNestedNode.content; | ||
} | ||
return nodes.toArray(); | ||
}; | ||
/** | ||
* @private | ||
* @param {string|TagNode} node | ||
*/ var appendNodes = function(node) { | ||
var items = getNodes(); | ||
if (Array.isArray(items)) { | ||
if (isTagNode_1(node)) { | ||
if (isAllowedTag(node.tag)) { | ||
items.push(node.toTagNode()); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach(function(item) { | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
} | ||
} else { | ||
items.push(node); | ||
} | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ var handleTagStart = function(token) { | ||
flushTagNodes(); | ||
var tagNode = default_1.create(token.getValue()); | ||
var isNested = isTokenNested(token); | ||
tagNodes.push(tagNode); | ||
if (isNested) { | ||
nestedNodes.push(tagNode); | ||
} else { | ||
appendNodes(tagNode); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ var handleTagEnd = function(token) { | ||
flushTagNodes(); | ||
var lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode) { | ||
appendNodes(lastNestedNode); | ||
} else if (typeof options.onError === 'function') { | ||
var tag = token.getValue(); | ||
var line = token.getLine(); | ||
var column = token.getColumn(); | ||
options.onError({ | ||
message: "Inconsistent tag '".concat(tag, "' on line ").concat(line, " and column ").concat(column), | ||
tagName: tag, | ||
lineNumber: line, | ||
columnNumber: column | ||
}); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ var handleTag = function(token) { | ||
// [tag] | ||
if (token.isStart()) { | ||
handleTagStart(token); | ||
} | ||
// [/tag] | ||
if (token.isEnd()) { | ||
handleTagEnd(token); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ var handleNode = function(token) { | ||
/** | ||
* @type {TagNode} | ||
*/ var lastTagNode = tagNodes.getLast(); | ||
var tokenValue = token.getValue(); | ||
var isNested = isTagNested(token); | ||
if (lastTagNode) { | ||
if (token.isAttrName()) { | ||
tagNodesAttrName.push(tokenValue); | ||
lastTagNode.attr(tagNodesAttrName.getLast(), ''); | ||
} else if (token.isAttrValue()) { | ||
var attrName = tagNodesAttrName.getLast(); | ||
if (attrName) { | ||
lastTagNode.attr(attrName, tokenValue); | ||
tagNodesAttrName.flushLast(); | ||
} else { | ||
lastTagNode.attr(tokenValue, tokenValue); | ||
} | ||
} else if (token.isText()) { | ||
if (isNested) { | ||
lastTagNode.append(tokenValue); | ||
} else { | ||
appendNodes(tokenValue); | ||
} | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
} else if (token.isText()) { | ||
appendNodes(tokenValue); | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ var onToken = function(token) { | ||
if (token.isTag()) { | ||
handleTag(token); | ||
} else { | ||
handleNode(token); | ||
} | ||
}; | ||
tokenizer = (opts.createTokenizer ? opts.createTokenizer : createLexer)(input, { | ||
onToken: onToken, | ||
onlyAllowTags: options.onlyAllowTags, | ||
openTag: openTag, | ||
closeTag: closeTag, | ||
enableEscapeTags: options.enableEscapeTags | ||
}); | ||
// eslint-disable-next-line no-unused-vars | ||
tokenizer.tokenize(); | ||
return nodes.toArray(); | ||
}; | ||
var isTagNode = function isTagNode(el) { | ||
return typeof el === 'object' && !!el.tag; | ||
}; | ||
exports["default"] = parse; | ||
exports.parse = parse; | ||
exports.isTagNode = isTagNode; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var isStringNode = function isStringNode(el) { | ||
return typeof el === 'string'; | ||
}; | ||
exports.isStringNode = isStringNode; | ||
var isEOL = function isEOL(el) { | ||
return el === char_1.N; | ||
}; | ||
exports.isEOL = isEOL; | ||
var keysReduce = function keysReduce(obj, reduce, def) { | ||
return Object.keys(obj).reduce(reduce, def); | ||
}; | ||
var getNodeLength = function getNodeLength(node) { | ||
if (isTagNode(node)) { | ||
return node.content.reduce(function (count, contentNode) { | ||
return count + getNodeLength(contentNode); | ||
}, 0); | ||
} | ||
if (isStringNode(node)) { | ||
return node.length; | ||
} | ||
return 0; | ||
}; | ||
/** | ||
* Appends value to Tag Node | ||
* @param {TagNode} node | ||
* @param value | ||
*/ | ||
exports.getNodeLength = getNodeLength; | ||
var appendToNode = function appendToNode(node, value) { | ||
node.content.push(value); | ||
}; | ||
/** | ||
* Replaces " to &qquot; | ||
* @param {String} value | ||
*/ | ||
exports.appendToNode = appendToNode; | ||
var escapeHTML = function escapeHTML(value) { | ||
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''') // eslint-disable-next-line no-script-url | ||
.replace(/(javascript):/gi, '$1%3A'); | ||
}; | ||
/** | ||
* Acept name and value and return valid html5 attribute string | ||
* @param {String} name | ||
* @param {String} value | ||
* @return {string} | ||
*/ | ||
exports.escapeHTML = escapeHTML; | ||
var attrValue = function attrValue(name, value) { | ||
var type = typeof value; | ||
var types = { | ||
"boolean": function boolean() { | ||
return value ? "" + name : ''; | ||
}, | ||
number: function number() { | ||
return name + "=\"" + value + "\""; | ||
}, | ||
string: function string() { | ||
return name + "=\"" + escapeHTML(value) + "\""; | ||
}, | ||
object: function object() { | ||
return name + "=\"" + escapeHTML(JSON.stringify(value)) + "\""; | ||
} | ||
}; | ||
return types[type] ? types[type]() : ''; | ||
}; | ||
/** | ||
* Transforms attrs to html params string | ||
* @param values | ||
*/ | ||
exports.attrValue = attrValue; | ||
var attrsToString = function attrsToString(values) { | ||
// To avoid some malformed attributes | ||
if (values == null) { | ||
return ''; | ||
} | ||
return keysReduce(values, function (arr, key) { | ||
return [].concat(arr, [attrValue(key, values[key])]); | ||
}, ['']).join(' '); | ||
}; | ||
/** | ||
* Gets value from | ||
* @example | ||
* getUniqAttr({ 'foo': true, 'bar': bar' }) => 'bar' | ||
* @param attrs | ||
* @returns {string} | ||
*/ | ||
exports.attrsToString = attrsToString; | ||
var getUniqAttr = function getUniqAttr(attrs) { | ||
return keysReduce(attrs, function (res, key) { | ||
return attrs[key] === key ? attrs[key] : null; | ||
}, null); | ||
}; | ||
exports.getUniqAttr = getUniqAttr; | ||
}); | ||
unwrapExports(lib); | ||
var lib_1 = lib.isEOL; | ||
var lib_2 = lib.isStringNode; | ||
var lib_3 = lib.isTagNode; | ||
var lib_4 = lib.getUniqAttr; | ||
var lib_5 = lib.getNodeLength; | ||
var lib_6 = lib.escapeHTML; | ||
var lib_7 = lib.appendToNode; | ||
var lib_8 = lib.attrValue; | ||
var lib_9 = lib.attrsToString; | ||
var TagNode_1 = createCommonjsModule(function (module, exports) { | ||
exports.__esModule = true; | ||
exports["default"] = exports.TagNode = void 0; | ||
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); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
var getTagAttrs = function getTagAttrs(tag, params) { | ||
var uniqAattr = (0, lib.getUniqAttr)(params); | ||
if (uniqAattr) { | ||
var tagAttr = (0, lib.attrValue)(tag, uniqAattr); | ||
var attrs = _extends({}, params); | ||
delete attrs[uniqAattr]; | ||
var attrsStr = (0, lib.attrsToString)(attrs); | ||
return "" + tagAttr + attrsStr; | ||
} | ||
return "" + tag + (0, lib.attrsToString)(params); | ||
}; | ||
var TagNode = | ||
/*#__PURE__*/ | ||
function () { | ||
function TagNode(tag, attrs, content) { | ||
this.tag = tag; | ||
this.attrs = attrs; | ||
this.content = Array.isArray(content) ? content : [content]; | ||
} | ||
var _proto = TagNode.prototype; | ||
_proto.attr = function attr(name, value) { | ||
if (typeof value !== 'undefined') { | ||
this.attrs[name] = value; | ||
} | ||
return this.attrs[name]; | ||
}; | ||
_proto.append = function append(value) { | ||
return (0, lib.appendToNode)(this, value); | ||
}; | ||
_proto.toTagStart = function toTagStart(_temp) { | ||
var _ref = _temp === void 0 ? {} : _temp, | ||
_ref$openTag = _ref.openTag, | ||
openTag = _ref$openTag === void 0 ? char_1.OPEN_BRAKET : _ref$openTag, | ||
_ref$closeTag = _ref.closeTag, | ||
closeTag = _ref$closeTag === void 0 ? char_1.CLOSE_BRAKET : _ref$closeTag; | ||
var tagAttrs = getTagAttrs(this.tag, this.attrs); | ||
return "" + openTag + tagAttrs + closeTag; | ||
}; | ||
_proto.toTagEnd = function toTagEnd(_temp2) { | ||
var _ref2 = _temp2 === void 0 ? {} : _temp2, | ||
_ref2$openTag = _ref2.openTag, | ||
openTag = _ref2$openTag === void 0 ? char_1.OPEN_BRAKET : _ref2$openTag, | ||
_ref2$closeTag = _ref2.closeTag, | ||
closeTag = _ref2$closeTag === void 0 ? char_1.CLOSE_BRAKET : _ref2$closeTag; | ||
return "" + openTag + char_1.SLASH + this.tag + closeTag; | ||
}; | ||
_proto.toTagNode = function toTagNode() { | ||
return new TagNode(this.tag.toLowerCase(), this.attrs, this.content); | ||
}; | ||
_proto.toString = function toString(_temp3) { | ||
var _ref3 = _temp3 === void 0 ? {} : _temp3, | ||
_ref3$openTag = _ref3.openTag, | ||
openTag = _ref3$openTag === void 0 ? char_1.OPEN_BRAKET : _ref3$openTag, | ||
_ref3$closeTag = _ref3.closeTag, | ||
closeTag = _ref3$closeTag === void 0 ? char_1.CLOSE_BRAKET : _ref3$closeTag; | ||
var isEmpty = this.content.length === 0; | ||
var content = this.content.reduce(function (r, node) { | ||
return r + node.toString({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
}); | ||
}, ''); | ||
var tagStart = this.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
}); | ||
if (isEmpty) { | ||
return tagStart; | ||
} | ||
return "" + tagStart + content + this.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
}); | ||
}; | ||
_createClass(TagNode, [{ | ||
key: "length", | ||
get: function get() { | ||
return (0, lib.getNodeLength)(this); | ||
} | ||
}]); | ||
return TagNode; | ||
}(); | ||
exports.TagNode = TagNode; | ||
TagNode.create = function (tag, attrs, content) { | ||
if (attrs === void 0) { | ||
attrs = {}; | ||
} | ||
if (content === void 0) { | ||
content = []; | ||
} | ||
return new TagNode(tag, attrs, content); | ||
}; | ||
TagNode.isOf = function (node, type) { | ||
return node.tag === type; | ||
}; | ||
var _default = TagNode; | ||
exports["default"] = _default; | ||
}); | ||
var TagNode = unwrapExports(TagNode_1); | ||
var TagNode_2 = TagNode_1.TagNode; | ||
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 TOKEN_TYPE_WORD = 1; // 'word'; | ||
var TOKEN_TYPE_TAG = 2; // 'tag'; | ||
var TOKEN_TYPE_ATTR_NAME = 3; // 'attr-name'; | ||
var TOKEN_TYPE_ATTR_VALUE = 4; // 'attr-value'; | ||
var TOKEN_TYPE_SPACE = 5; // 'space'; | ||
var TOKEN_TYPE_NEW_LINE = 6; // 'new-line'; | ||
/** | ||
* @param {Token} token | ||
* @returns {string} | ||
*/ | ||
var getTokenValue = function getTokenValue(token) { | ||
if (token && typeof token[TOKEN_VALUE_ID] !== 'undefined') { | ||
return token[TOKEN_VALUE_ID]; | ||
} | ||
return ''; | ||
}; | ||
/** | ||
* @param {Token}token | ||
* @returns {number} | ||
*/ | ||
var getTokenLine = function getTokenLine(token) { | ||
return token && token[TOKEN_LINE_ID] || 0; | ||
}; | ||
var getTokenColumn = function getTokenColumn(token) { | ||
return token && token[TOKEN_COLUMN_ID] || 0; | ||
}; | ||
/** | ||
* @param {Token} token | ||
* @returns {boolean} | ||
*/ | ||
var isTextToken = function isTextToken(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD; | ||
} | ||
return false; | ||
}; | ||
/** | ||
* @param {Token} token | ||
* @returns {boolean} | ||
*/ | ||
var isTagToken = function isTagToken(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG; | ||
} | ||
return false; | ||
}; | ||
var isTagEnd = function isTagEnd(token) { | ||
return getTokenValue(token).charCodeAt(0) === char_5.charCodeAt(0); | ||
}; | ||
var isTagStart = function isTagStart(token) { | ||
return !isTagEnd(token); | ||
}; | ||
var isAttrNameToken = function isAttrNameToken(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME; | ||
} | ||
return false; | ||
}; | ||
/** | ||
* @param {Token} token | ||
* @returns {boolean} | ||
*/ | ||
var isAttrValueToken = function isAttrValueToken(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE; | ||
} | ||
return false; | ||
}; | ||
var getTagName = function getTagName(token) { | ||
var value = getTokenValue(token); | ||
return isTagEnd(token) ? value.slice(1) : value; | ||
}; | ||
var convertTagToText = function convertTagToText(token) { | ||
var text = char_7; | ||
text += getTokenValue(token); | ||
text += char_6; | ||
return text; | ||
}; | ||
var Token = | ||
/*#__PURE__*/ | ||
function () { | ||
/** | ||
* @param {String} type | ||
* @param {String} value | ||
* @param line | ||
* @param row | ||
*/ | ||
function Token(type, value, line, row) { | ||
this[TOKEN_TYPE_ID] = Number(type); | ||
this[TOKEN_VALUE_ID] = String(value); | ||
this[TOKEN_LINE_ID] = Number(line); | ||
this[TOKEN_COLUMN_ID] = Number(row); | ||
} | ||
var _proto = Token.prototype; | ||
_proto.isEmpty = function isEmpty() { | ||
// eslint-disable-next-line no-restricted-globals | ||
return isNaN(this[TOKEN_TYPE_ID]); | ||
}; | ||
_proto.isText = function isText() { | ||
return isTextToken(this); | ||
}; | ||
_proto.isTag = function isTag() { | ||
return isTagToken(this); | ||
}; | ||
_proto.isAttrName = function isAttrName() { | ||
return isAttrNameToken(this); | ||
}; | ||
_proto.isAttrValue = function isAttrValue() { | ||
return isAttrValueToken(this); | ||
}; | ||
_proto.isStart = function isStart() { | ||
return isTagStart(this); | ||
}; | ||
_proto.isEnd = function isEnd() { | ||
return isTagEnd(this); | ||
}; | ||
_proto.getName = function getName() { | ||
return getTagName(this); | ||
}; | ||
_proto.getValue = function getValue() { | ||
return getTokenValue(this); | ||
}; | ||
_proto.getLine = function getLine() { | ||
return getTokenLine(this); | ||
}; | ||
_proto.getColumn = function getColumn() { | ||
return getTokenColumn(this); | ||
}; | ||
_proto.toString = function toString() { | ||
return convertTagToText(this); | ||
}; | ||
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; | ||
function CharGrabber(source, options) { | ||
var cursor = { | ||
pos: 0, | ||
len: source.length | ||
}; | ||
var substrUntilChar = function substrUntilChar(char) { | ||
var pos = cursor.pos; | ||
var idx = source.indexOf(char, pos); | ||
return idx >= 0 ? source.substr(pos, idx - pos) : ''; | ||
}; | ||
var includes = function includes(val) { | ||
return source.indexOf(val, cursor.pos) >= 0; | ||
}; | ||
var hasNext = function hasNext() { | ||
return cursor.len > cursor.pos; | ||
}; | ||
var isLast = function isLast() { | ||
return cursor.pos === cursor.len; | ||
}; | ||
var skip = function skip(num, silent) { | ||
if (num === void 0) { | ||
num = 1; | ||
} | ||
cursor.pos += num; | ||
if (options && options.onSkip && !silent) { | ||
options.onSkip(); | ||
} | ||
}; | ||
var rest = function rest() { | ||
return source.substr(cursor.pos); | ||
}; | ||
var curr = function curr() { | ||
return source[cursor.pos]; | ||
}; | ||
var prev = function prev() { | ||
var prevPos = cursor.pos - 1; | ||
return typeof source[prevPos] !== 'undefined' ? source[prevPos] : null; | ||
}; | ||
var next = function next() { | ||
var nextPos = cursor.pos + 1; | ||
return nextPos <= source.length - 1 ? source[nextPos] : null; | ||
}; | ||
var grabWhile = function grabWhile(cond, silent) { | ||
var start = 0; | ||
if (hasNext()) { | ||
start = cursor.pos; | ||
while (hasNext() && cond(curr())) { | ||
skip(1, silent); | ||
} | ||
} | ||
return source.substr(start, cursor.pos - start); | ||
}; | ||
/** | ||
* @type {skip} | ||
*/ | ||
this.skip = skip; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.hasNext = hasNext; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getCurr = curr; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getRest = rest; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getNext = next; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getPrev = prev; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.isLast = isLast; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.includes = includes; | ||
/** | ||
* @param {Function} cond | ||
* @param {Boolean} silent | ||
* @return {String} | ||
*/ | ||
this.grabWhile = grabWhile; | ||
/** | ||
* Grabs rest of string until it find a char | ||
* @param {String} char | ||
* @return {String} | ||
*/ | ||
this.substrUntilChar = substrUntilChar; | ||
} | ||
/** | ||
* Creates a grabber wrapper for source string, that helps to iterate over string char by char | ||
* @param {String} source | ||
* @param {Object} options | ||
* @param {Function} options.onSkip | ||
* @return CharGrabber | ||
*/ | ||
var createCharGrabber = function createCharGrabber(source, options) { | ||
return new CharGrabber(source, options); | ||
}; | ||
/** | ||
* Trims string from start and end by char | ||
* @example | ||
* trimChar('*hello*', '*') ==> 'hello' | ||
* @param {String} str | ||
* @param {String} charToRemove | ||
* @returns {String} | ||
*/ | ||
var trimChar = function trimChar(str, charToRemove) { | ||
while (str.charAt(0) === charToRemove) { | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(1); | ||
} | ||
while (str.charAt(str.length - 1) === charToRemove) { | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
}; | ||
/** | ||
* Unquotes \" to " | ||
* @param str | ||
* @return {String} | ||
*/ | ||
var unquote = function unquote(str) { | ||
return str.replace(char_2 + char_9, char_9); | ||
}; | ||
function NodeList(values) { | ||
if (values === void 0) { | ||
values = []; | ||
} | ||
var nodes = values; | ||
var getLast = function getLast() { | ||
return Array.isArray(nodes) && nodes.length > 0 && typeof nodes[nodes.length - 1] !== 'undefined' ? nodes[nodes.length - 1] : null; | ||
}; | ||
var flushLast = function flushLast() { | ||
return nodes.length ? nodes.pop() : false; | ||
}; | ||
var push = function push(value) { | ||
return nodes.push(value); | ||
}; | ||
var toArray = function toArray() { | ||
return nodes; | ||
}; | ||
this.push = push; | ||
this.toArray = toArray; | ||
this.getLast = getLast; | ||
this.flushLast = flushLast; | ||
} | ||
/** | ||
* | ||
* @param values | ||
* @return {NodeList} | ||
*/ | ||
var createList = function createList(values) { | ||
if (values === void 0) { | ||
values = []; | ||
} | ||
return new NodeList(values); | ||
}; | ||
/* eslint-disable no-plusplus,no-param-reassign */ | ||
var EM = '!'; | ||
/** | ||
* Creates a Token entity class | ||
* @param {Number} type | ||
* @param {String} value | ||
* @param {Number} r line number | ||
* @param {Number} cl char number in line | ||
*/ | ||
var createToken = function createToken(type, value, r, cl) { | ||
if (r === void 0) { | ||
r = 0; | ||
} | ||
if (cl === void 0) { | ||
cl = 0; | ||
} | ||
return new Token(type, value, r, cl); | ||
}; | ||
/** | ||
* @typedef {Object} Lexer | ||
* @property {Function} tokenize | ||
* @property {Function} isTokenNested | ||
*/ | ||
/** | ||
* @param {String} buffer | ||
* @param {Object} options | ||
* @param {Function} options.onToken | ||
* @param {String} options.openTag | ||
* @param {String} options.closeTag | ||
* @param {Boolean} options.enableEscapeTags | ||
* @return {Lexer} | ||
*/ | ||
function createLexer(buffer, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var STATE_WORD = 0; | ||
var STATE_TAG = 1; | ||
var STATE_TAG_ATTRS = 2; | ||
var TAG_STATE_NAME = 0; | ||
var TAG_STATE_ATTR = 1; | ||
var TAG_STATE_VALUE = 2; | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var stateMode = STATE_WORD; | ||
var tagMode = TAG_STATE_NAME; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || char_7; | ||
var closeTag = options.closeTag || char_6; | ||
var escapeTags = !!options.enableEscapeTags; | ||
var onToken = options.onToken || function () {}; | ||
var RESERVED_CHARS = [closeTag, openTag, char_9, char_2, char_8, char_11, char_10, char_14, EM]; | ||
var NOT_CHAR_TOKENS = [// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, char_8, char_11, char_14]; | ||
var WHITESPACES = [char_8, char_11]; | ||
var SPECIAL_CHARS = [char_10, char_8, char_11]; | ||
var isCharReserved = function isCharReserved(char) { | ||
return RESERVED_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isNewLine = function isNewLine(char) { | ||
return char === char_14; | ||
}; | ||
var isWhiteSpace = function isWhiteSpace(char) { | ||
return WHITESPACES.indexOf(char) >= 0; | ||
}; | ||
var isCharToken = function isCharToken(char) { | ||
return NOT_CHAR_TOKENS.indexOf(char) === -1; | ||
}; | ||
var isSpecialChar = function isSpecialChar(char) { | ||
return SPECIAL_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isEscapableChar = function isEscapableChar(char) { | ||
return char === openTag || char === closeTag || char === char_2; | ||
}; | ||
var isEscapeChar = function isEscapeChar(char) { | ||
return char === char_2; | ||
}; | ||
var onSkip = function onSkip() { | ||
col++; | ||
}; | ||
var unq = function unq(val) { | ||
return unquote(trimChar(val, char_9)); | ||
}; | ||
var chars = createCharGrabber(buffer, { | ||
onSkip: onSkip | ||
}); | ||
/** | ||
* Emits newly created token to subscriber | ||
* @param {Number} type | ||
* @param {String} value | ||
*/ | ||
function emitToken(type, value) { | ||
var token = createToken(type, value, row, col); | ||
onToken(token); | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
} | ||
function nextTagState(tagChars, isSingleValueTag) { | ||
if (tagMode === TAG_STATE_ATTR) { | ||
var validAttrName = function validAttrName(char) { | ||
return !(char === char_10 || isWhiteSpace(char)); | ||
}; | ||
var _name = tagChars.grabWhile(validAttrName); | ||
var isEnd = tagChars.isLast(); | ||
var isValue = tagChars.getCurr() !== char_10; | ||
tagChars.skip(); | ||
if (isEnd || isValue) { | ||
emitToken(TYPE_ATTR_VALUE, unq(_name)); | ||
} else { | ||
emitToken(TYPE_ATTR_NAME, _name); | ||
} | ||
if (isEnd) { | ||
return TAG_STATE_NAME; | ||
} | ||
if (isValue) { | ||
return TAG_STATE_ATTR; | ||
} | ||
return TAG_STATE_VALUE; | ||
} | ||
if (tagMode === TAG_STATE_VALUE) { | ||
var stateSpecial = false; | ||
var validAttrValue = function validAttrValue(char) { | ||
// const isEQ = char === EQ; | ||
var isQM = char === char_9; | ||
var prevChar = tagChars.getPrev(); | ||
var nextChar = tagChars.getNext(); | ||
var isPrevSLASH = prevChar === char_2; | ||
var isNextEQ = nextChar === char_10; | ||
var isWS = isWhiteSpace(char); // const isPrevWS = isWhiteSpace(prevChar); | ||
var isNextWS = isWhiteSpace(nextChar); | ||
if (stateSpecial && isSpecialChar(char)) { | ||
return true; | ||
} | ||
if (isQM && !isPrevSLASH) { | ||
stateSpecial = !stateSpecial; | ||
if (!stateSpecial && !(isNextEQ || isNextWS)) { | ||
return false; | ||
} | ||
} | ||
if (!isSingleValueTag) { | ||
return isWS === false; // return (isEQ || isWS) === false; | ||
} | ||
return true; | ||
}; | ||
var _name2 = tagChars.grabWhile(validAttrValue); | ||
tagChars.skip(); | ||
emitToken(TYPE_ATTR_VALUE, unq(_name2)); | ||
if (tagChars.isLast()) { | ||
return TAG_STATE_NAME; | ||
} | ||
return TAG_STATE_ATTR; | ||
} | ||
var validName = function validName(char) { | ||
return !(char === char_10 || isWhiteSpace(char) || tagChars.isLast()); | ||
}; | ||
var name = tagChars.grabWhile(validName); | ||
emitToken(TYPE_TAG, name); | ||
tagChars.skip(); // in cases when we has [url=someval]GET[/url] and we dont need to parse all | ||
if (isSingleValueTag) { | ||
return TAG_STATE_VALUE; | ||
} | ||
var hasEQ = tagChars.includes(char_10); | ||
return hasEQ ? TAG_STATE_ATTR : TAG_STATE_VALUE; | ||
} | ||
function stateTag() { | ||
var currChar = chars.getCurr(); | ||
if (currChar === openTag) { | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = chars.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || chars.isLast()) { | ||
emitToken(TYPE_WORD, currChar); | ||
return STATE_WORD; | ||
} // [myTag ] | ||
var isNoAttrsInTag = substr.indexOf(char_10) === -1; // [/myTag] | ||
var isClosingTag = substr[0] === char_5; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
var name = chars.grabWhile(function (char) { | ||
return char !== closeTag; | ||
}); | ||
chars.skip(); // skip closeTag | ||
emitToken(TYPE_TAG, name); | ||
return STATE_WORD; | ||
} | ||
return STATE_TAG_ATTRS; | ||
} | ||
return STATE_WORD; | ||
} | ||
function stateAttrs() { | ||
var silent = true; | ||
var tagStr = chars.grabWhile(function (char) { | ||
return char !== closeTag; | ||
}, silent); | ||
var tagGrabber = createCharGrabber(tagStr, { | ||
onSkip: onSkip | ||
}); | ||
var hasSpace = tagGrabber.includes(char_8); | ||
tagMode = TAG_STATE_NAME; | ||
while (tagGrabber.hasNext()) { | ||
tagMode = nextTagState(tagGrabber, !hasSpace); | ||
} | ||
chars.skip(); // skip closeTag | ||
return STATE_WORD; | ||
} | ||
function stateWord() { | ||
if (isNewLine(chars.getCurr())) { | ||
emitToken(TYPE_NEW_LINE, chars.getCurr()); | ||
chars.skip(); | ||
col = 0; | ||
row++; | ||
return STATE_WORD; | ||
} | ||
if (isWhiteSpace(chars.getCurr())) { | ||
emitToken(TYPE_SPACE, chars.grabWhile(isWhiteSpace)); | ||
return STATE_WORD; | ||
} | ||
if (chars.getCurr() === openTag) { | ||
if (chars.includes(closeTag)) { | ||
return STATE_TAG; | ||
} | ||
emitToken(TYPE_WORD, chars.getCurr()); | ||
chars.skip(); | ||
return STATE_WORD; | ||
} | ||
if (escapeTags) { | ||
if (isEscapeChar(chars.getCurr())) { | ||
var currChar = chars.getCurr(); | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // skip the \ without emitting anything | ||
if (isEscapableChar(nextChar)) { | ||
chars.skip(); // skip past the [, ] or \ as well | ||
emitToken(TYPE_WORD, nextChar); | ||
return STATE_WORD; | ||
} | ||
emitToken(TYPE_WORD, currChar); | ||
return STATE_WORD; | ||
} | ||
var isChar = function isChar(char) { | ||
return isCharToken(char) && !isEscapeChar(char); | ||
}; | ||
emitToken(TYPE_WORD, chars.grabWhile(isChar)); | ||
return STATE_WORD; | ||
} | ||
emitToken(TYPE_WORD, chars.grabWhile(isCharToken)); | ||
return STATE_WORD; | ||
} | ||
function tokenize() { | ||
stateMode = STATE_WORD; | ||
while (chars.hasNext()) { | ||
switch (stateMode) { | ||
case STATE_TAG: | ||
stateMode = stateTag(); | ||
break; | ||
case STATE_TAG_ATTRS: | ||
stateMode = stateAttrs(); | ||
break; | ||
case STATE_WORD: | ||
stateMode = stateWord(); | ||
break; | ||
default: | ||
stateMode = STATE_WORD; | ||
break; | ||
} | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
} | ||
function isTokenNested(token) { | ||
var value = openTag + char_5 + token.getValue(); // potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
} | ||
return { | ||
tokenize: tokenize, | ||
isTokenNested: isTokenNested | ||
}; | ||
} | ||
/** | ||
* @public | ||
* @param {String} input | ||
* @param {Object} opts | ||
* @param {Function} opts.createTokenizer | ||
* @param {Array<string>} opts.onlyAllowTags | ||
* @param {String} opts.openTag | ||
* @param {String} opts.closeTag | ||
* @param {Boolean} opts.enableEscapeTags | ||
* @return {Array} | ||
*/ | ||
var parse = function parse(input, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
var options = opts; | ||
var openTag = options.openTag || char_7; | ||
var closeTag = options.closeTag || char_6; | ||
var tokenizer = null; | ||
/** | ||
* Result AST of nodes | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var nodes = createList(); | ||
/** | ||
* Temp buffer of nodes that's nested to another node | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var nestedNodes = createList(); | ||
/** | ||
* Temp buffer of nodes [tag..]...[/tag] | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var tagNodes = createList(); | ||
/** | ||
* Temp buffer of tag attributes | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var tagNodesAttrName = createList(); | ||
/** | ||
* Cache for nested tags checks | ||
*/ | ||
var nestedTagsMap = new Set(); | ||
/** | ||
* | ||
* @param token | ||
* @returns {boolean} | ||
*/ | ||
var isTokenNested = function isTokenNested(token) { | ||
var value = token.getValue(); | ||
if (!nestedTagsMap.has(value) && tokenizer.isTokenNested && tokenizer.isTokenNested(token)) { | ||
nestedTagsMap.add(value); | ||
return true; | ||
} | ||
return nestedTagsMap.has(value); | ||
}; | ||
/** | ||
* @param tagName | ||
* @returns {boolean} | ||
*/ | ||
var isTagNested = function isTagNested(tagName) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
}; | ||
/** | ||
* @private | ||
* @param {String} value | ||
* @return {boolean} | ||
*/ | ||
var isAllowedTag = function 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 | ||
* @return {Array} | ||
*/ | ||
var flushTagNodes = function flushTagNodes() { | ||
if (tagNodes.flushLast()) { | ||
tagNodesAttrName.flushLast(); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @return {Array} | ||
*/ | ||
var getNodes = function getNodes() { | ||
var lastNestedNode = nestedNodes.getLast(); | ||
if (lastNestedNode && Array.isArray(lastNestedNode.content)) { | ||
return lastNestedNode.content; | ||
} | ||
return nodes.toArray(); | ||
}; | ||
/** | ||
* @private | ||
* @param {string|TagNode} node | ||
*/ | ||
var appendNodes = function appendNodes(node) { | ||
var items = getNodes(); | ||
if (Array.isArray(items)) { | ||
if (lib_3(node)) { | ||
if (isAllowedTag(node.tag)) { | ||
items.push(node.toTagNode()); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach(function (item) { | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
} | ||
} else { | ||
items.push(node); | ||
} | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTagStart = function handleTagStart(token) { | ||
flushTagNodes(); | ||
var tagNode = TagNode.create(token.getValue()); | ||
var isNested = isTokenNested(token); | ||
tagNodes.push(tagNode); | ||
if (isNested) { | ||
nestedNodes.push(tagNode); | ||
} else { | ||
appendNodes(tagNode); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTagEnd = function handleTagEnd(token) { | ||
flushTagNodes(); | ||
var lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode) { | ||
appendNodes(lastNestedNode); | ||
} else if (typeof options.onError === 'function') { | ||
var tag = token.getValue(); | ||
var line = token.getLine(); | ||
var column = token.getColumn(); | ||
options.onError({ | ||
message: "Inconsistent tag '" + tag + "' on line " + line + " and column " + column, | ||
tagName: tag, | ||
lineNumber: line, | ||
columnNumber: column | ||
}); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTag = function handleTag(token) { | ||
// [tag] | ||
if (token.isStart()) { | ||
handleTagStart(token); | ||
} // [/tag] | ||
if (token.isEnd()) { | ||
handleTagEnd(token); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleNode = function handleNode(token) { | ||
/** | ||
* @type {TagNode} | ||
*/ | ||
var lastTagNode = tagNodes.getLast(); | ||
var tokenValue = token.getValue(); | ||
var isNested = isTagNested(token); | ||
if (lastTagNode) { | ||
if (token.isAttrName()) { | ||
tagNodesAttrName.push(tokenValue); | ||
lastTagNode.attr(tagNodesAttrName.getLast(), ''); | ||
} else if (token.isAttrValue()) { | ||
var attrName = tagNodesAttrName.getLast(); | ||
if (attrName) { | ||
lastTagNode.attr(attrName, tokenValue); | ||
tagNodesAttrName.flushLast(); | ||
} else { | ||
lastTagNode.attr(tokenValue, tokenValue); | ||
} | ||
} else if (token.isText()) { | ||
if (isNested) { | ||
lastTagNode.append(tokenValue); | ||
} else { | ||
appendNodes(tokenValue); | ||
} | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
} else if (token.isText()) { | ||
appendNodes(tokenValue); | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var onToken = function onToken(token) { | ||
if (token.isTag()) { | ||
handleTag(token); | ||
} else { | ||
handleNode(token); | ||
} | ||
}; | ||
tokenizer = (opts.createTokenizer ? opts.createTokenizer : createLexer)(input, { | ||
onToken: onToken, | ||
onlyAllowTags: options.onlyAllowTags, | ||
openTag: openTag, | ||
closeTag: closeTag, | ||
enableEscapeTags: options.enableEscapeTags | ||
}); // eslint-disable-next-line no-unused-vars | ||
var tokens = tokenizer.tokenize(); | ||
return nodes.toArray(); | ||
}; | ||
exports.TagNode = TagNode_2; | ||
exports.default = parse; | ||
exports.parse = parse; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
})); |
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t=t||self).BbobParser={})}(this,(function(t){"use strict";function n(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function e(t,n){return t(n={exports:{}},n.exports),n.exports}var r=e((function(t,n){n.__esModule=!0,n.BACKSLASH=n.PLACEHOLDER_SPACE=n.PLACEHOLDER_SPACE_TAB=n.SLASH=n.CLOSE_BRAKET=n.OPEN_BRAKET=n.SPACE=n.QUOTEMARK=n.EQ=n.TAB=n.R=n.F=n.N=void 0;n.N="\n";n.TAB="\t";n.F="\f";n.R="\r";n.EQ="=";n.QUOTEMARK='"';n.SPACE=" ";n.OPEN_BRAKET="[";n.CLOSE_BRAKET="]";n.SLASH="/";n.BACKSLASH="\\";n.PLACEHOLDER_SPACE_TAB=" ";n.PLACEHOLDER_SPACE=" "}));n(r);var i=r.BACKSLASH,o=r.SLASH,u=r.CLOSE_BRAKET,a=r.OPEN_BRAKET,s=r.SPACE,c=r.QUOTEMARK,f=r.EQ,g=r.TAB,l=r.N,p=e((function(t,n){n.__esModule=!0,n.isEOL=n.isStringNode=n.isTagNode=n.getUniqAttr=n.getNodeLength=n.escapeHTML=n.appendToNode=n.attrValue=n.attrsToString=void 0;var e=function(t){return"object"==typeof t&&!!t.tag};n.isTagNode=e;var i=function(t){return"string"==typeof t};n.isStringNode=i;n.isEOL=function(t){return t===r.N};var o=function(t,n,e){return Object.keys(t).reduce(n,e)};n.getNodeLength=function t(n){return e(n)?n.content.reduce((function(n,e){return n+t(e)}),0):i(n)?n.length:0};n.appendToNode=function(t,n){t.content.push(n)};var u=function(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/(javascript):/gi,"$1%3A")};n.escapeHTML=u;var a=function(t,n){var e=typeof n,r={boolean:function(){return n?""+t:""},number:function(){return t+'="'+n+'"'},string:function(){return t+'="'+u(n)+'"'},object:function(){return t+'="'+u(JSON.stringify(n))+'"'}};return r[e]?r[e]():""};n.attrValue=a;n.attrsToString=function(t){return null==t?"":o(t,(function(n,e){return[].concat(n,[a(e,t[e])])}),[""]).join(" ")};n.getUniqAttr=function(t){return o(t,(function(n,e){return t[e]===e?t[e]:null}),null)}}));n(p);var h=p.isTagNode,d=e((function(t,n){function e(t,n){for(var e=0;n.length>e;e++){var r=n[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function i(){return(i=Object.assign||function(t){for(var n=1;arguments.length>n;n++){var e=arguments[n];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t}).apply(this,arguments)}n.__esModule=!0,n.default=n.TagNode=void 0;var o=function(){function t(t,n,e){this.tag=t,this.attrs=n,this.content=Array.isArray(e)?e:[e]}var n,o,u,a=t.prototype;return a.attr=function(t,n){return void 0!==n&&(this.attrs[t]=n),this.attrs[t]},a.append=function(t){return(0,p.appendToNode)(this,t)},a.toTagStart=function(t){var n=void 0===t?{}:t,e=n.openTag,o=n.closeTag,u=void 0===o?r.CLOSE_BRAKET:o;return""+(void 0===e?r.OPEN_BRAKET:e)+function(t,n){var e=(0,p.getUniqAttr)(n);if(e){var r=(0,p.attrValue)(t,e),o=i({},n);return delete o[e],""+r+(0,p.attrsToString)(o)}return""+t+(0,p.attrsToString)(n)}(this.tag,this.attrs)+u},a.toTagEnd=function(t){var n=void 0===t?{}:t,e=n.openTag,i=n.closeTag;return""+(void 0===e?r.OPEN_BRAKET:e)+r.SLASH+this.tag+(void 0===i?r.CLOSE_BRAKET:i)},a.toTagNode=function(){return new t(this.tag.toLowerCase(),this.attrs,this.content)},a.toString=function(t){var n=void 0===t?{}:t,e=n.openTag,i=void 0===e?r.OPEN_BRAKET:e,o=n.closeTag,u=void 0===o?r.CLOSE_BRAKET:o,a=0===this.content.length,s=this.content.reduce((function(t,n){return t+n.toString({openTag:i,closeTag:u})}),""),c=this.toTagStart({openTag:i,closeTag:u});return a?c:""+c+s+this.toTagEnd({openTag:i,closeTag:u})},n=t,(o=[{key:"length",get:function(){return(0,p.getNodeLength)(this)}}])&&e(n.prototype,o),u&&e(n,u),t}();n.TagNode=o,o.create=function(t,n,e){return void 0===n&&(n={}),void 0===e&&(e=[]),new o(t,n,e)},o.isOf=function(t,n){return t.tag===n},n.default=o})),v=n(d),T=d.TagNode,A=function(t){return t&&void 0!==t.value?t.value:""},E=function(t){return A(t).charCodeAt(0)===o.charCodeAt(0)},y=function(){function t(t,n,e,r){this.type=Number(t),this.value=n+"",this.line=Number(e),this.row=Number(r)}var n=t.prototype;return n.isEmpty=function(){return isNaN(this.type)},n.isText=function(){return!(!(t=this)||void 0===t.type||5!==t.type&&6!==t.type&&1!==t.type);var t},n.isTag=function(){return!(!(t=this)||void 0===t.type)&&2===t.type;var t},n.isAttrName=function(){return!(!(t=this)||void 0===t.type)&&3===t.type;var t},n.isAttrValue=function(){return!(!(t=this)||void 0===t.type)&&4===t.type;var t},n.isStart=function(){return!E(this)},n.isEnd=function(){return E(this)},n.getName=function(){return n=A(t=this),E(t)?n.slice(1):n;var t,n},n.getValue=function(){return A(this)},n.getLine=function(){return(t=this)&&t.line||0;var t},n.getColumn=function(){return(t=this)&&t.row||0;var t},n.toString=function(){return t=a,t+=A(this),t+=u;var t},t}();function N(t,n){var e={pos:0,len:t.length},r=function(){return e.len>e.pos},i=function(t,r){void 0===t&&(t=1),e.pos+=t,n&&n.onSkip&&!r&&n.onSkip()},o=function(){return t[e.pos]};this.skip=i,this.hasNext=r,this.getCurr=o,this.getRest=function(){return t.substr(e.pos)},this.getNext=function(){var n=e.pos+1;return n>t.length-1?null:t[n]},this.getPrev=function(){var n=e.pos-1;return void 0!==t[n]?t[n]:null},this.isLast=function(){return e.pos===e.len},this.includes=function(n){return t.indexOf(n,e.pos)>=0},this.grabWhile=function(n,u){var a=0;if(r())for(a=e.pos;r()&&n(o());)i(1,u);return t.substr(a,e.pos-a)},this.substrUntilChar=function(n){var r=e.pos,i=t.indexOf(n,r);return 0>i?"":t.substr(r,i-r)}}var S=function(t,n){return new N(t,n)};function L(t){void 0===t&&(t=[]);var n=t;this.push=function(t){return n.push(t)},this.toArray=function(){return n},this.getLast=function(){return Array.isArray(n)&&n.length>0&&void 0!==n[n.length-1]?n[n.length-1]:null},this.flushLast=function(){return!!n.length&&n.pop()}}var b=function(t){return void 0===t&&(t=[]),new L(t)};function O(t,n){void 0===n&&(n={});var e=0,r=0,p=-1,h=0,d=0,v=Array(Math.floor(t.length)),T=n.openTag||a,A=n.closeTag||u,E=!!n.enableEscapeTags,N=n.onToken||function(){},L=[A,T,c,i,s,g,f,l,"!"],b=[T,s,g,l],O=[s,g],C=[f,s,g],k=function(t){return O.indexOf(t)>=0},_=function(t){return-1===b.indexOf(t)},x=function(t){return t===i},P=function(){r++},R=function(t){return function(t){return t.replace(i+c,c)}(function(t,n){for(;t.charAt(0)===n;)t=t.substring(1);for(;t.charAt(t.length-1)===n;)t=t.substring(0,t.length-1);return t}(t,c))},m=S(t,{onSkip:P});function B(t,n){var i=function(t,n,e,r){return void 0===e&&(e=0),void 0===r&&(r=0),new y(t,n,e,r)}(t,n,e,r);N(i),v[p+=1]=i}function w(t,n){if(1===d){var e=t.grabWhile((function(t){return!(t===f||k(t))})),r=t.isLast(),o=t.getCurr()!==f;return t.skip(),r||o?B(4,R(e)):B(3,e),r?0:o?1:2}if(2===d){var u=!1,a=t.grabWhile((function(e){var r=e===c,o=t.getPrev(),a=t.getNext(),s=o===i,g=a===f,l=k(e),p=k(a);return!(!u||!function(t){return C.indexOf(t)>=0}(e))||!!(!r||s||(u=!u)||g||p)&&(!!n||!1===l)}));return t.skip(),B(4,R(a)),t.isLast()?0:1}return B(2,t.grabWhile((function(n){return!(n===f||k(n)||t.isLast())}))),t.skip(),n?2:t.includes(f)?1:2}function K(){var t=m.getCurr();if(t===T){var n=m.getNext();m.skip();var e=m.substrUntilChar(A),r=0===e.length||e.indexOf(T)>=0;if(L.indexOf(n)>=0||r||m.isLast())return B(1,t),0;if(-1===e.indexOf(f)||e[0]===o){var i=m.grabWhile((function(t){return t!==A}));return m.skip(),B(2,i),0}return 2}return 0}function H(){var t=m.grabWhile((function(t){return t!==A}),!0),n=S(t,{onSkip:P}),e=n.includes(s);for(d=0;n.hasNext();)d=w(n,!e);return m.skip(),0}function j(){if(m.getCurr()===l)return B(6,m.getCurr()),m.skip(),r=0,e++,0;if(k(m.getCurr()))return B(5,m.grabWhile(k)),0;if(m.getCurr()===T)return m.includes(A)?1:(B(1,m.getCurr()),m.skip(),0);if(E){if(x(m.getCurr())){var t=m.getCurr(),n=m.getNext();return m.skip(),function(t){return t===T||t===A||t===i}(n)?(m.skip(),B(1,n),0):(B(1,t),0)}return B(1,m.grabWhile((function(t){return _(t)&&!x(t)}))),0}return B(1,m.grabWhile(_)),0}return{tokenize:function(){for(h=0;m.hasNext();)switch(h){case 1:h=K();break;case 2:h=H();break;case 0:h=j();break;default:h=0}return v.length=p+1,v},isTokenNested:function(n){var e=T+o+n.getValue();return t.indexOf(e)>-1}}}var C=function(t,n){void 0===n&&(n={});var e=n,r=e.openTag||a,i=e.closeTag||u,o=null,s=b(),c=b(),f=b(),g=b(),l=new Set,p=function(){f.flushLast()&&g.flushLast()},d=function(t){var n,o,u=(n=c.getLast())&&Array.isArray(n.content)?n.content:s.toArray();Array.isArray(u)&&(h(t)?(o=t.tag,e.onlyAllowTags&&e.onlyAllowTags.length&&0>e.onlyAllowTags.indexOf(o)?(u.push(t.toTagStart({openTag:r,closeTag:i})),t.content.length&&(t.content.forEach((function(t){u.push(t)})),u.push(t.toTagEnd({openTag:r,closeTag:i})))):u.push(t.toTagNode())):u.push(t))},T=function(t){p();var n=v.create(t.getValue()),e=function(t){var n=t.getValue();return!l.has(n)&&o.isTokenNested&&o.isTokenNested(t)?(l.add(n),!0):l.has(n)}(t);f.push(n),e?c.push(n):d(n)},A=function(t){t.isStart()&&T(t),t.isEnd()&&function(t){p();var n=c.flushLast();if(n)d(n);else if("function"==typeof e.onError){var r=t.getValue(),i=t.getLine(),o=t.getColumn();e.onError({message:"Inconsistent tag '"+r+"' on line "+i+" and column "+o,tagName:r,lineNumber:i,columnNumber:o})}}(t)},E=function(t){var n=f.getLast(),e=t.getValue(),r=!!l.has(t);if(n)if(t.isAttrName())g.push(e),n.attr(g.getLast(),"");else if(t.isAttrValue()){var i=g.getLast();i?(n.attr(i,e),g.flushLast()):n.attr(e,e)}else t.isText()?r?n.append(e):d(e):t.isTag()&&d(""+t);else t.isText()?d(e):t.isTag()&&d(""+t)};(o=(n.createTokenizer?n.createTokenizer:O)(t,{onToken:function(t){t.isTag()?A(t):E(t)},onlyAllowTags:e.onlyAllowTags,openTag:r,closeTag:i,enableEscapeTags:e.enableEscapeTags})).tokenize();return s.toArray()};t.TagNode=T,t.default=C,t.parse=C,Object.defineProperty(t,"__esModule",{value:!0})})); | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).BbobParser={})}(this,(function(t){"use strict";var n={},e={};Object.defineProperty(e,"__esModule",{value:!0});var r=e.BACKSLASH=e.PLACEHOLDER_SPACE=e.PLACEHOLDER_SPACE_TAB=l=e.SLASH=e.CLOSE_BRAKET=e.OPEN_BRAKET=e.SPACE=e.QUOTEMARK=e.EQ=e.TAB=e.R=e.F=e.N=void 0,o=e.N="\n";e.F="\f",e.R="\r";var i=e.TAB="\t",a=e.EQ="=",u=e.QUOTEMARK='"',c=e.SPACE=" ",s=e.OPEN_BRAKET="[",f=e.CLOSE_BRAKET="]",l=e.SLASH="/";e.PLACEHOLDER_SPACE_TAB=" ",e.PLACEHOLDER_SPACE=" ",r=e.BACKSLASH="\\";var g={};Object.defineProperty(g,"__esModule",{value:!0}),g.isEOL=g.isStringNode=E=g.isTagNode=g.getUniqAttr=g.getNodeLength=g.escapeHTML=g.appendToNode=g.attrValue=g.attrsToString=void 0;var v=e;function p(t){return function(t){if(Array.isArray(t)){for(var n=0,e=Array(t.length);t.length>n;n++)e[n]=t[n];return e}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}var h=function(t){return"object"==typeof t&&!!t.tag},d=function(t){return"string"==typeof t},y=function(t,n,e){return Object.keys(t).reduce(n,e)},T=function(t){return h(t)?t.content.reduce((function(t,n){return t+T(n)}),0):d(t)?t.length:0},b=function(t){return t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'").replace(/(javascript):/gi,"$1%3A")},A=function(t,n){var e,r=void 0===n?"undefined":(e=n)&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e,o={boolean:function(){return n?"".concat(t):""},number:function(){return"".concat(t,'="').concat(n,'"')},string:function(){return"".concat(t,'="').concat(b(n),'"')},object:function(){return"".concat(t,'="').concat(b(JSON.stringify(n)),'"')}};return o[r]?o[r]():""};g.attrsToString=function(t){return null==t?"":y(t,(function(n,e){return p(n).concat([A(e,t[e])])}),[""]).join(" ")},g.attrValue=A,g.appendToNode=function(t,n){t.content.push(n)},g.escapeHTML=b,g.getNodeLength=T,g.getUniqAttr=function(t){return y(t,(function(n,e){return t[e]===e?t[e]:null}),null)};var E=g.isTagNode=h;g.isStringNode=d,g.isEOL=function(t){return t===v.N},Object.defineProperty(n,"__esModule",{value:!0});var k=n.default=t.TagNode=n.TagNode=void 0,O=e,S=g;function N(t,n){for(var e=0;n.length>e;e++){var r=n[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}function L(t,n,e){return n in t?Object.defineProperty(t,n,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[n]=e,t}var C=function(t,n){var e=S.getUniqAttr(n);if(e){var r=S.attrValue(t,e),o=function(t){for(var n=1;arguments.length>n;n++){var e=null!=arguments[n]?arguments[n]:{},r=Object.keys(e);"function"==typeof Object.getOwnPropertySymbols&&(r=r.concat(Object.getOwnPropertySymbols(e).filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})))),r.forEach((function(n){L(t,n,e[n])}))}return t}({},n);delete o[e];var i=S.attrsToString(o);return"".concat(r).concat(i)}return"".concat(t).concat(S.attrsToString(n))},m=function(){function t(n,e,r){!function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}(this,t),this.tag=n,this.attrs=e,this.content=Array.isArray(r)?r:[r]}var n,e,r;return n=t,(e=[{key:"attr",value:function(t,n){return void 0!==n&&(this.attrs[t]=n),this.attrs[t]}},{key:"append",value:function(t){return S.appendToNode(this,t)}},{key:"length",get:function(){return S.getNodeLength(this)}},{key:"toTagStart",value:function(t){var n=void 0===t?{}:t,e=n.openTag,r=void 0===e?O.OPEN_BRAKET:e,o=n.closeTag,i=void 0===o?O.CLOSE_BRAKET:o,a=C(this.tag,this.attrs);return"".concat(r).concat(a).concat(i)}},{key:"toTagEnd",value:function(t){var n=void 0===t?{}:t,e=n.openTag,r=n.closeTag,o=void 0===r?O.CLOSE_BRAKET:r;return"".concat(void 0===e?O.OPEN_BRAKET:e).concat(O.SLASH).concat(this.tag).concat(o)}},{key:"toTagNode",value:function(){return new t(this.tag.toLowerCase(),this.attrs,this.content)}},{key:"toString",value:function(t){var n=void 0===t?{}:t,e=n.openTag,r=void 0===e?O.OPEN_BRAKET:e,o=n.closeTag,i=void 0===o?O.CLOSE_BRAKET:o,a=0===this.content.length,u=this.content.reduce((function(t,n){return t+n.toString({openTag:r,closeTag:i})}),""),c=this.toTagStart({openTag:r,closeTag:i});return a?c:"".concat(c).concat(u).concat(this.toTagEnd({openTag:r,closeTag:i}))}}])&&N(n.prototype,e),r&&N(n,r),t}();function P(t,n){for(var e=0;n.length>e;e++){var r=n[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}m.create=function(t,n,e){return new m(t,void 0===n?{}:n,void 0===e?[]:e)},m.isOf=function(t,n){return t.tag===n},t.TagNode=n.TagNode=m,k=n.default=m;var w=function(t){return t&&void 0!==t.value?t.value:""},_=function(t){return w(t).charCodeAt(0)===l.charCodeAt(0)},x=function(){function t(n,e,r,o){!function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")}(this,t),this.type=Number(n),this.value=e+"",this.line=Number(r),this.row=Number(o)}var n,e,r;return n=t,(e=[{key:"isEmpty",value:function(){return isNaN(this.type)}},{key:"isText",value:function(){return!(!(t=this)||void 0===t.type||5!==t.type&&6!==t.type&&1!==t.type);var t}},{key:"isTag",value:function(){return!(!(t=this)||void 0===t.type)&&2===t.type;var t}},{key:"isAttrName",value:function(){return!(!(t=this)||void 0===t.type)&&3===t.type;var t}},{key:"isAttrValue",value:function(){return!(!(t=this)||void 0===t.type)&&4===t.type;var t}},{key:"isStart",value:function(){return!_(this)}},{key:"isEnd",value:function(){return _(this)}},{key:"getName",value:function(){return n=w(t=this),_(t)?n.slice(1):n;var t,n}},{key:"getValue",value:function(){return w(this)}},{key:"getLine",value:function(){return(t=this)&&t.line||0;var t}},{key:"getColumn",value:function(){return(t=this)&&t.row||0;var t}},{key:"toString",value:function(){return t=s,(t+=w(this))+f;var t}}])&&P(n.prototype,e),r&&P(n,r),t}();function j(t,n){var e={pos:0,len:t.length},r=function(){return e.len>e.pos},o=function(t,r){e.pos+=void 0===t?1:t,n&&n.onSkip&&!r&&n.onSkip()},i=function(){return t[e.pos]};this.skip=o,this.hasNext=r,this.getCurr=i,this.getRest=function(){return t.substr(e.pos)},this.getNext=function(){var n=e.pos+1;return n>t.length-1?null:t[n]},this.getPrev=function(){var n=e.pos-1;return void 0!==t[n]?t[n]:null},this.isLast=function(){return e.pos===e.len},this.includes=function(n){return t.indexOf(n,e.pos)>=0},this.grabWhile=function(n,a){var u=0;if(r())for(u=e.pos;r()&&n(i());)o(1,a);return t.substr(u,e.pos-u)},this.substrUntilChar=function(n){var r=e.pos,o=t.indexOf(n,r);return 0>o?"":t.substr(r,o-r)}}var R=function(t,n){return new j(t,n)};function B(t){var n=void 0===t?[]:t;this.push=function(t){return n.push(t)},this.toArray=function(){return n},this.getLast=function(){return Array.isArray(n)&&n.length>0&&void 0!==n[n.length-1]?n[n.length-1]:null},this.flushLast=function(){return!!n.length&&n.pop()}}var K=function(t){return new B(void 0===t?[]:t)};function H(t,n){var e=void 0===n?{}:n,g=function(t,n){var e=function(t,n,e,r){return new x(t,n,void 0===e?0:e,void 0===r?0:r)}(t,n,O,S);j(e),m[N+=1]=e},v=function(t,n){if(C===E){var e=t.grabWhile((function(t){return!(t===a||U(t))})),o=t.isLast(),i=t.getCurr()!==a;return t.skip(),o||i?g(4,I(e)):g(3,e),o?A:i?E:k}if(C===k){var c=!1;e=t.grabWhile((function(e){var o=e===u,i=t.getPrev(),s=t.getNext(),f=i===r,l=s===a,g=U(e),v=U(s);return!(!c||!q(e))||!!(!o||f||(c=!c)||l||v)&&(!!n||!1===g)}));return t.skip(),g(4,I(e)),t.isLast()?A:E}e=t.grabWhile((function(n){return!(n===a||U(n)||t.isLast())}));return g(2,e),t.skip(),n?k:t.includes(a)?E:k},p=function(){var t=J.getCurr();if(t===P){var n=J.getNext();J.skip();var e=J.substrUntilChar(w),r=0===e.length||e.indexOf(P)>=0;if(M(n)||r||J.isLast())return g(1,t),y;if(-1===e.indexOf(a)||e[0]===l){var o=J.grabWhile((function(t){return t!==w}));return J.skip(),g(2,o),y}return b}return y},h=function(){var t=J.grabWhile((function(t){return t!==w}),!0),n=R(t,{onSkip:F}),e=n.includes(c);for(C=A;n.hasNext();)C=v(n,!e);return J.skip(),y},d=function(){if(W(J.getCurr()))return g(6,J.getCurr()),J.skip(),S=0,O++,y;if(U(J.getCurr()))return g(5,J.grabWhile(U)),y;if(J.getCurr()===P)return J.includes(w)?T:(g(1,J.getCurr()),J.skip(),y);if(_){if(Q(J.getCurr())){var t=J.getCurr(),n=J.getNext();return J.skip(),z(n)?(J.skip(),g(1,n),y):(g(1,t),y)}return g(1,J.grabWhile((function(t){return D(t)&&!Q(t)}))),y}return g(1,J.grabWhile(D)),y},y=0,T=1,b=2,A=0,E=1,k=2,O=0,S=0,N=-1,L=y,C=A,m=Array(Math.floor(t.length)),P=e.openTag||s,w=e.closeTag||f,_=!!e.enableEscapeTags,j=e.onToken||function(){},B=[w,P,u,r,c,i,a,o,"!"],K=[P,c,i,o],H=[c,i],V=[a,c,i],M=function(t){return B.indexOf(t)>=0},W=function(t){return t===o},U=function(t){return H.indexOf(t)>=0},D=function(t){return-1===K.indexOf(t)},q=function(t){return V.indexOf(t)>=0},z=function(t){return t===P||t===w||t===r},Q=function(t){return t===r},F=function(){S++},I=function(t){return function(t,n){for(;t.charAt(0)===n;)t=t.substring(1);for(;t.charAt(t.length-1)===n;)t=t.substring(0,t.length-1);return t}(t,u).replace(r+u,u)},J=R(t,{onSkip:F});return{tokenize:function(){for(L=y;J.hasNext();)switch(L){case T:L=p();break;case b:L=h();break;case y:L=d();break;default:L=y}return m.length=N+1,m},isTokenNested:function(n){var e=P+l+n.getValue();return t.indexOf(e)>-1}}}var V=function(t,n){var e=void 0===n?{}:n,r=e,o=r.openTag||s,i=r.closeTag||f,a=null,u=K(),c=K(),l=K(),g=K(),v=new Set,p=function(){l.flushLast()&&g.flushLast()},h=function(t){var n,e,a=(n=c.getLast())&&Array.isArray(n.content)?n.content:u.toArray();Array.isArray(a)&&(E(t)?(e=t.tag,r.onlyAllowTags&&r.onlyAllowTags.length&&0>r.onlyAllowTags.indexOf(e)?(a.push(t.toTagStart({openTag:o,closeTag:i})),t.content.length&&(t.content.forEach((function(t){a.push(t)})),a.push(t.toTagEnd({openTag:o,closeTag:i})))):a.push(t.toTagNode())):a.push(t))},d=function(t){p();var n=k.create(t.getValue()),e=function(t){var n=t.getValue();return!v.has(n)&&a.isTokenNested&&a.isTokenNested(t)?(v.add(n),!0):v.has(n)}(t);l.push(n),e?c.push(n):h(n)},y=function(t){t.isStart()&&d(t),t.isEnd()&&function(t){p();var n=c.flushLast();if(n)h(n);else if("function"==typeof r.onError){var e=t.getValue(),o=t.getLine(),i=t.getColumn();r.onError({message:"Inconsistent tag '".concat(e,"' on line ").concat(o," and column ").concat(i),tagName:e,lineNumber:o,columnNumber:i})}}(t)},T=function(t){var n=l.getLast(),e=t.getValue(),r=!!v.has(t);if(n)if(t.isAttrName())g.push(e),n.attr(g.getLast(),"");else if(t.isAttrValue()){var o=g.getLast();o?(n.attr(o,e),g.flushLast()):n.attr(e,e)}else t.isText()?r?n.append(e):h(e):t.isTag()&&h(""+t);else t.isText()?h(e):t.isTag()&&h(""+t)};return(a=(e.createTokenizer?e.createTokenizer:H)(t,{onToken:function(t){t.isTag()?y(t):T(t)},onlyAllowTags:r.onlyAllowTags,openTag:o,closeTag:i,enableEscapeTags:r.enableEscapeTags})).tokenize(),u.toArray()};t.default=V,t.parse=V,Object.defineProperty(t,"__esModule",{value:!0})})); |
@@ -1,17 +0,2 @@ | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.TagNode = exports.parse = exports.default = void 0; | ||
var _parse = _interopRequireWildcard(require("./parse")); | ||
exports.default = _parse.default; | ||
exports.parse = _parse.parse; | ||
var _TagNode = require("@bbob/plugin-helper/lib/TagNode"); | ||
exports.TagNode = _TagNode.TagNode; | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
export { default, parse } from './parse'; | ||
export { TagNode } from '@bbob/plugin-helper/lib/TagNode'; |
@@ -1,14 +0,4 @@ | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.createLexer = createLexer; | ||
exports.createTokenOfType = void 0; | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
var _Token = require("./Token"); | ||
var _utils = require("./utils"); | ||
/* eslint-disable no-plusplus,no-param-reassign */ | ||
/* eslint-disable no-plusplus,no-param-reassign */ import { OPEN_BRAKET, CLOSE_BRAKET, QUOTEMARK, BACKSLASH, SLASH, SPACE, TAB, EQ, N } from '@bbob/plugin-helper/lib/char'; | ||
import { Token, TYPE_ATTR_NAME, TYPE_ATTR_VALUE, TYPE_NEW_LINE, TYPE_SPACE, TYPE_TAG, TYPE_WORD } from './Token'; | ||
import { createCharGrabber, trimChar, unquote } from './utils'; | ||
// for cases <!-- --> | ||
@@ -22,14 +12,5 @@ var EM = '!'; | ||
* @param {Number} cl char number in line | ||
*/ | ||
var createToken = function createToken(type, value, r, cl) { | ||
if (r === void 0) { | ||
r = 0; | ||
} | ||
if (cl === void 0) { | ||
cl = 0; | ||
} | ||
return new _Token.Token(type, value, r, cl); | ||
*/ var createToken = function(type, value, param, param1) { | ||
var r = param === void 0 ? 0 : param, cl = param1 === void 0 ? 0 : param1; | ||
return new Token(type, value, r, cl); | ||
}; | ||
@@ -40,5 +21,3 @@ /** | ||
* @property {Function} isTokenNested | ||
*/ | ||
/** | ||
*/ /** | ||
* @param {String} buffer | ||
@@ -51,209 +30,199 @@ * @param {Object} options | ||
* @return {Lexer} | ||
*/ | ||
function createLexer(buffer, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || _char.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char.CLOSE_BRAKET; | ||
var escapeTags = options.enableEscapeTags; | ||
var RESERVED_CHARS = [closeTag, openTag, _char.QUOTEMARK, _char.BACKSLASH, _char.SPACE, _char.TAB, _char.EQ, _char.N, EM]; | ||
var NOT_CHAR_TOKENS = [// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, _char.SPACE, _char.TAB, _char.N]; | ||
var WHITESPACES = [_char.SPACE, _char.TAB]; | ||
var SPECIAL_CHARS = [_char.EQ, _char.SPACE, _char.TAB]; | ||
var isCharReserved = char => RESERVED_CHARS.indexOf(char) >= 0; | ||
var isWhiteSpace = char => WHITESPACES.indexOf(char) >= 0; | ||
var isCharToken = char => NOT_CHAR_TOKENS.indexOf(char) === -1; | ||
var isSpecialChar = char => SPECIAL_CHARS.indexOf(char) >= 0; | ||
var isEscapableChar = char => char === openTag || char === closeTag || char === _char.BACKSLASH; | ||
var isEscapeChar = char => char === _char.BACKSLASH; | ||
/** | ||
*/ function createLexer(buffer, param) { | ||
var options = param === void 0 ? { | ||
} : param; | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || OPEN_BRAKET; | ||
var closeTag = options.closeTag || CLOSE_BRAKET; | ||
var escapeTags = options.enableEscapeTags; | ||
var RESERVED_CHARS = [ | ||
closeTag, | ||
openTag, | ||
QUOTEMARK, | ||
BACKSLASH, | ||
SPACE, | ||
TAB, | ||
EQ, | ||
N, | ||
EM | ||
]; | ||
var NOT_CHAR_TOKENS = [ | ||
// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, | ||
SPACE, | ||
TAB, | ||
N, | ||
]; | ||
var WHITESPACES = [ | ||
SPACE, | ||
TAB | ||
]; | ||
var SPECIAL_CHARS = [ | ||
EQ, | ||
SPACE, | ||
TAB | ||
]; | ||
var isCharReserved = function(char) { | ||
return RESERVED_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isWhiteSpace = function(char) { | ||
return WHITESPACES.indexOf(char) >= 0; | ||
}; | ||
var isCharToken = function(char) { | ||
return NOT_CHAR_TOKENS.indexOf(char) === -1; | ||
}; | ||
var isSpecialChar = function(char) { | ||
return SPECIAL_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isEscapableChar = function(char) { | ||
return char === openTag || char === closeTag || char === BACKSLASH; | ||
}; | ||
var isEscapeChar = function(char) { | ||
return char === BACKSLASH; | ||
}; | ||
/** | ||
* Emits newly created token to subscriber | ||
* @param token | ||
*/ | ||
var emitToken = token => { | ||
if (options.onToken) { | ||
options.onToken(token); | ||
} | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
}; | ||
/** | ||
*/ var emitToken = function(token) { | ||
if (options.onToken) { | ||
options.onToken(token); | ||
} | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
}; | ||
/** | ||
* Parses params inside [myTag---params goes here---]content[/myTag] | ||
* @param str | ||
* @returns {{tag: *, attrs: Array}} | ||
*/ | ||
var parseAttrs = str => { | ||
var tagName = null; | ||
var skipSpecialChars = false; | ||
var attrTokens = []; | ||
var attrCharGrabber = (0, _utils.createCharGrabber)(str); | ||
var validAttr = char => { | ||
var isEQ = char === _char.EQ; | ||
var isWS = isWhiteSpace(char); | ||
var prevChar = attrCharGrabber.getPrev(); | ||
var nextChar = attrCharGrabber.getNext(); | ||
var isPrevSLASH = prevChar === _char.BACKSLASH; | ||
var isTagNameEmpty = tagName === null; | ||
if (isTagNameEmpty) { | ||
return (isEQ || isWS || attrCharGrabber.isLast()) === false; | ||
} | ||
if (skipSpecialChars && isSpecialChar(char)) { | ||
return true; | ||
} | ||
if (char === _char.QUOTEMARK && !isPrevSLASH) { | ||
skipSpecialChars = !skipSpecialChars; | ||
if (!skipSpecialChars && !(nextChar === _char.EQ || isWhiteSpace(nextChar))) { | ||
return false; | ||
*/ var parseAttrs = function(str) { | ||
var tagName = null; | ||
var skipSpecialChars = false; | ||
var attrTokens = []; | ||
var attrCharGrabber = createCharGrabber(str); | ||
var validAttr = function(char) { | ||
var isEQ = char === EQ; | ||
var isWS = isWhiteSpace(char); | ||
var prevChar = attrCharGrabber.getPrev(); | ||
var nextChar = attrCharGrabber.getNext(); | ||
var isPrevSLASH = prevChar === BACKSLASH; | ||
var isTagNameEmpty = tagName === null; | ||
if (isTagNameEmpty) { | ||
return (isEQ || isWS || attrCharGrabber.isLast()) === false; | ||
} | ||
if (skipSpecialChars && isSpecialChar(char)) { | ||
return true; | ||
} | ||
if (char === QUOTEMARK && !isPrevSLASH) { | ||
skipSpecialChars = !skipSpecialChars; | ||
if (!skipSpecialChars && !(nextChar === EQ || isWhiteSpace(nextChar))) { | ||
return false; | ||
} | ||
} | ||
return (isEQ || isWS) === false; | ||
}; | ||
var nextAttr = function() { | ||
var attrStr = attrCharGrabber.grabWhile(validAttr); | ||
var currChar = attrCharGrabber.getCurr(); | ||
// first string before space is a tag name [tagName params...] | ||
if (tagName === null) { | ||
tagName = attrStr; | ||
} else if (isWhiteSpace(currChar) || currChar === QUOTEMARK || !attrCharGrabber.hasNext()) { | ||
var escaped = unquote(trimChar(attrStr, QUOTEMARK)); | ||
attrTokens.push(createToken(TYPE_ATTR_VALUE, escaped, row, col)); | ||
} else { | ||
attrTokens.push(createToken(TYPE_ATTR_NAME, attrStr, row, col)); | ||
} | ||
attrCharGrabber.skip(); | ||
}; | ||
while(attrCharGrabber.hasNext()){ | ||
nextAttr(); | ||
} | ||
} | ||
return (isEQ || isWS) === false; | ||
return { | ||
tag: tagName, | ||
attrs: attrTokens | ||
}; | ||
}; | ||
var nextAttr = () => { | ||
var attrStr = attrCharGrabber.grabWhile(validAttr); | ||
var currChar = attrCharGrabber.getCurr(); // first string before space is a tag name [tagName params...] | ||
if (tagName === null) { | ||
tagName = attrStr; | ||
} else if (isWhiteSpace(currChar) || currChar === _char.QUOTEMARK || !attrCharGrabber.hasNext()) { | ||
var escaped = (0, _utils.unquote)((0, _utils.trimChar)(attrStr, _char.QUOTEMARK)); | ||
attrTokens.push(createToken(_Token.TYPE_ATTR_VALUE, escaped, row, col)); | ||
} else { | ||
attrTokens.push(createToken(_Token.TYPE_ATTR_NAME, attrStr, row, col)); | ||
} | ||
attrCharGrabber.skip(); | ||
var bufferGrabber = createCharGrabber(buffer, { | ||
onSkip: function() { | ||
col++; | ||
} | ||
}); | ||
var next = function() { | ||
var currChar = bufferGrabber.getCurr(); | ||
var nextChar = bufferGrabber.getNext(); | ||
if (currChar === N) { | ||
bufferGrabber.skip(); | ||
col = 0; | ||
row++; | ||
emitToken(createToken(TYPE_NEW_LINE, currChar, row, col)); | ||
} else if (isWhiteSpace(currChar)) { | ||
var str = bufferGrabber.grabWhile(isWhiteSpace); | ||
emitToken(createToken(TYPE_SPACE, str, row, col)); | ||
} else if (escapeTags && isEscapeChar(currChar) && isEscapableChar(nextChar)) { | ||
bufferGrabber.skip(); // skip the \ without emitting anything | ||
bufferGrabber.skip(); // skip past the [, ] or \ as well | ||
emitToken(createToken(TYPE_WORD, nextChar, row, col)); | ||
} else if (currChar === openTag) { | ||
bufferGrabber.skip(); // skip openTag | ||
// detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = bufferGrabber.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || bufferGrabber.isLast()) { | ||
emitToken(createToken(TYPE_WORD, currChar, row, col)); | ||
} else { | ||
var str = bufferGrabber.grabWhile(function(val) { | ||
return val !== closeTag; | ||
}); | ||
bufferGrabber.skip(); // skip closeTag | ||
// [myTag ] | ||
var isNoAttrsInTag = str.indexOf(EQ) === -1; | ||
// [/myTag] | ||
var isClosingTag = str[0] === SLASH; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
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 (currChar === closeTag) { | ||
bufferGrabber.skip(); // skip closeTag | ||
emitToken(createToken(TYPE_WORD, currChar, row, col)); | ||
} else if (isCharToken(currChar)) { | ||
if (escapeTags && isEscapeChar(currChar) && !isEscapableChar(nextChar)) { | ||
bufferGrabber.skip(); | ||
emitToken(createToken(TYPE_WORD, currChar, row, col)); | ||
} else { | ||
var str = bufferGrabber.grabWhile(function(char) { | ||
if (escapeTags) { | ||
return isCharToken(char) && !isEscapeChar(char); | ||
} | ||
return isCharToken(char); | ||
}); | ||
emitToken(createToken(TYPE_WORD, str, row, col)); | ||
} | ||
} | ||
}; | ||
while (attrCharGrabber.hasNext()) { | ||
nextAttr(); | ||
} | ||
var tokenize = function() { | ||
while(bufferGrabber.hasNext()){ | ||
next(); | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
}; | ||
var isTokenNested = function(token) { | ||
var value = openTag + SLASH + token.getValue(); | ||
// potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
}; | ||
return { | ||
tag: tagName, | ||
attrs: attrTokens | ||
tokenize: tokenize, | ||
isTokenNested: isTokenNested | ||
}; | ||
}; | ||
var bufferGrabber = (0, _utils.createCharGrabber)(buffer, { | ||
onSkip: () => { | ||
col++; | ||
} | ||
}); | ||
var next = () => { | ||
var currChar = bufferGrabber.getCurr(); | ||
var nextChar = bufferGrabber.getNext(); | ||
if (currChar === _char.N) { | ||
bufferGrabber.skip(); | ||
col = 0; | ||
row++; | ||
emitToken(createToken(_Token.TYPE_NEW_LINE, currChar, row, col)); | ||
} else if (isWhiteSpace(currChar)) { | ||
var str = bufferGrabber.grabWhile(isWhiteSpace); | ||
emitToken(createToken(_Token.TYPE_SPACE, str, row, col)); | ||
} else if (escapeTags && isEscapeChar(currChar) && isEscapableChar(nextChar)) { | ||
bufferGrabber.skip(); // skip the \ without emitting anything | ||
bufferGrabber.skip(); // skip past the [, ] or \ as well | ||
emitToken(createToken(_Token.TYPE_WORD, nextChar, row, col)); | ||
} else if (currChar === openTag) { | ||
bufferGrabber.skip(); // skip openTag | ||
// detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = bufferGrabber.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || bufferGrabber.isLast()) { | ||
emitToken(createToken(_Token.TYPE_WORD, currChar, row, col)); | ||
} else { | ||
var _str = bufferGrabber.grabWhile(val => val !== closeTag); | ||
bufferGrabber.skip(); // skip closeTag | ||
// [myTag ] | ||
var isNoAttrsInTag = _str.indexOf(_char.EQ) === -1; // [/myTag] | ||
var isClosingTag = _str[0] === _char.SLASH; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
emitToken(createToken(_Token.TYPE_TAG, _str, row, col)); | ||
} else { | ||
var parsed = parseAttrs(_str); | ||
emitToken(createToken(_Token.TYPE_TAG, parsed.tag, row, col)); | ||
parsed.attrs.map(emitToken); | ||
} | ||
} | ||
} else if (currChar === closeTag) { | ||
bufferGrabber.skip(); // skip closeTag | ||
emitToken(createToken(_Token.TYPE_WORD, currChar, row, col)); | ||
} else if (isCharToken(currChar)) { | ||
if (escapeTags && isEscapeChar(currChar) && !isEscapableChar(nextChar)) { | ||
bufferGrabber.skip(); | ||
emitToken(createToken(_Token.TYPE_WORD, currChar, row, col)); | ||
} else { | ||
var _str2 = bufferGrabber.grabWhile(char => { | ||
if (escapeTags) { | ||
return isCharToken(char) && !isEscapeChar(char); | ||
} | ||
return isCharToken(char); | ||
}); | ||
emitToken(createToken(_Token.TYPE_WORD, _str2, row, col)); | ||
} | ||
} | ||
}; | ||
var tokenize = () => { | ||
while (bufferGrabber.hasNext()) { | ||
next(); | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
}; | ||
var isTokenNested = token => { | ||
var value = openTag + _char.SLASH + token.getValue(); // potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
}; | ||
return { | ||
tokenize, | ||
isTokenNested | ||
}; | ||
} | ||
var createTokenOfType = createToken; | ||
exports.createTokenOfType = createTokenOfType; | ||
export var createTokenOfType = createToken; | ||
export { createLexer }; |
587
es/lexer.js
@@ -1,14 +0,4 @@ | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.createLexer = createLexer; | ||
exports.createTokenOfType = void 0; | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
var _Token = require("./Token"); | ||
var _utils = require("./utils"); | ||
/* eslint-disable no-plusplus,no-param-reassign */ | ||
/* eslint-disable no-plusplus,no-param-reassign */ import { OPEN_BRAKET, CLOSE_BRAKET, QUOTEMARK, BACKSLASH, SLASH, SPACE, TAB, EQ, N } from '@bbob/plugin-helper/lib/char'; | ||
import { Token, TYPE_ATTR_NAME, TYPE_ATTR_VALUE, TYPE_NEW_LINE, TYPE_SPACE, TYPE_TAG, TYPE_WORD } from './Token'; | ||
import { createCharGrabber, trimChar, unquote } from './utils'; | ||
// for cases <!-- --> | ||
@@ -22,14 +12,5 @@ var EM = '!'; | ||
* @param {Number} cl char number in line | ||
*/ | ||
var createToken = function createToken(type, value, r, cl) { | ||
if (r === void 0) { | ||
r = 0; | ||
} | ||
if (cl === void 0) { | ||
cl = 0; | ||
} | ||
return new _Token.Token(type, value, r, cl); | ||
*/ var createToken = function(type, value, param, param1) { | ||
var r = param === void 0 ? 0 : param, cl = param1 === void 0 ? 0 : param1; | ||
return new Token(type, value, r, cl); | ||
}; | ||
@@ -40,5 +21,3 @@ /** | ||
* @property {Function} isTokenNested | ||
*/ | ||
/** | ||
*/ /** | ||
* @param {String} buffer | ||
@@ -51,301 +30,277 @@ * @param {Object} options | ||
* @return {Lexer} | ||
*/ | ||
function createLexer(buffer, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var STATE_WORD = 0; | ||
var STATE_TAG = 1; | ||
var STATE_TAG_ATTRS = 2; | ||
var TAG_STATE_NAME = 0; | ||
var TAG_STATE_ATTR = 1; | ||
var TAG_STATE_VALUE = 2; | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var stateMode = STATE_WORD; | ||
var tagMode = TAG_STATE_NAME; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || _char.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char.CLOSE_BRAKET; | ||
var escapeTags = !!options.enableEscapeTags; | ||
var onToken = options.onToken || (() => {}); | ||
var RESERVED_CHARS = [closeTag, openTag, _char.QUOTEMARK, _char.BACKSLASH, _char.SPACE, _char.TAB, _char.EQ, _char.N, EM]; | ||
var NOT_CHAR_TOKENS = [// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, _char.SPACE, _char.TAB, _char.N]; | ||
var WHITESPACES = [_char.SPACE, _char.TAB]; | ||
var SPECIAL_CHARS = [_char.EQ, _char.SPACE, _char.TAB]; | ||
var isCharReserved = char => RESERVED_CHARS.indexOf(char) >= 0; | ||
var isNewLine = char => char === _char.N; | ||
var isWhiteSpace = char => WHITESPACES.indexOf(char) >= 0; | ||
var isCharToken = char => NOT_CHAR_TOKENS.indexOf(char) === -1; | ||
var isSpecialChar = char => SPECIAL_CHARS.indexOf(char) >= 0; | ||
var isEscapableChar = char => char === openTag || char === closeTag || char === _char.BACKSLASH; | ||
var isEscapeChar = char => char === _char.BACKSLASH; | ||
var onSkip = () => { | ||
col++; | ||
}; | ||
var unq = val => (0, _utils.unquote)((0, _utils.trimChar)(val, _char.QUOTEMARK)); | ||
var chars = (0, _utils.createCharGrabber)(buffer, { | ||
onSkip | ||
}); | ||
/** | ||
*/ function createLexer(buffer, param) { | ||
var options = param === void 0 ? { | ||
} : param; | ||
var emitToken = /** | ||
* Emits newly created token to subscriber | ||
* @param {Number} type | ||
* @param {String} value | ||
*/ | ||
function emitToken(type, value) { | ||
var token = createToken(type, value, row, col); | ||
onToken(token); | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
} | ||
function nextTagState(tagChars, isSingleValueTag) { | ||
if (tagMode === TAG_STATE_ATTR) { | ||
var validAttrName = char => !(char === _char.EQ || isWhiteSpace(char)); | ||
var _name = tagChars.grabWhile(validAttrName); | ||
var isEnd = tagChars.isLast(); | ||
var isValue = tagChars.getCurr() !== _char.EQ; | ||
tagChars.skip(); | ||
if (isEnd || isValue) { | ||
emitToken(_Token.TYPE_ATTR_VALUE, unq(_name)); | ||
} else { | ||
emitToken(_Token.TYPE_ATTR_NAME, _name); | ||
} | ||
if (isEnd) { | ||
return TAG_STATE_NAME; | ||
} | ||
if (isValue) { | ||
return TAG_STATE_ATTR; | ||
} | ||
return TAG_STATE_VALUE; | ||
} | ||
if (tagMode === TAG_STATE_VALUE) { | ||
var stateSpecial = false; | ||
var validAttrValue = char => { | ||
// const isEQ = char === EQ; | ||
var isQM = char === _char.QUOTEMARK; | ||
var prevChar = tagChars.getPrev(); | ||
var nextChar = tagChars.getNext(); | ||
var isPrevSLASH = prevChar === _char.BACKSLASH; | ||
var isNextEQ = nextChar === _char.EQ; | ||
var isWS = isWhiteSpace(char); // const isPrevWS = isWhiteSpace(prevChar); | ||
var isNextWS = isWhiteSpace(nextChar); | ||
if (stateSpecial && isSpecialChar(char)) { | ||
return true; | ||
*/ function emitToken(type, value) { | ||
var token = createToken(type, value, row, col); | ||
onToken(token); | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
}; | ||
var nextTagState = function nextTagState(tagChars, isSingleValueTag) { | ||
if (tagMode === TAG_STATE_ATTR) { | ||
var validAttrName = function(char) { | ||
return !(char === EQ || isWhiteSpace(char)); | ||
}; | ||
var name = tagChars.grabWhile(validAttrName); | ||
var isEnd = tagChars.isLast(); | ||
var isValue = tagChars.getCurr() !== EQ; | ||
tagChars.skip(); | ||
if (isEnd || isValue) { | ||
emitToken(TYPE_ATTR_VALUE, unq(name)); | ||
} else { | ||
emitToken(TYPE_ATTR_NAME, name); | ||
} | ||
if (isEnd) { | ||
return TAG_STATE_NAME; | ||
} | ||
if (isValue) { | ||
return TAG_STATE_ATTR; | ||
} | ||
return TAG_STATE_VALUE; | ||
} | ||
if (isQM && !isPrevSLASH) { | ||
stateSpecial = !stateSpecial; | ||
if (!stateSpecial && !(isNextEQ || isNextWS)) { | ||
return false; | ||
} | ||
if (tagMode === TAG_STATE_VALUE) { | ||
var stateSpecial = false; | ||
var validAttrValue = function(char) { | ||
// const isEQ = char === EQ; | ||
var isQM = char === QUOTEMARK; | ||
var prevChar = tagChars.getPrev(); | ||
var nextChar = tagChars.getNext(); | ||
var isPrevSLASH = prevChar === BACKSLASH; | ||
var isNextEQ = nextChar === EQ; | ||
var isWS = isWhiteSpace(char); | ||
// const isPrevWS = isWhiteSpace(prevChar); | ||
var isNextWS = isWhiteSpace(nextChar); | ||
if (stateSpecial && isSpecialChar(char)) { | ||
return true; | ||
} | ||
if (isQM && !isPrevSLASH) { | ||
stateSpecial = !stateSpecial; | ||
if (!stateSpecial && !(isNextEQ || isNextWS)) { | ||
return false; | ||
} | ||
} | ||
if (!isSingleValueTag) { | ||
return isWS === false; | ||
// return (isEQ || isWS) === false; | ||
} | ||
return true; | ||
}; | ||
var name = tagChars.grabWhile(validAttrValue); | ||
tagChars.skip(); | ||
emitToken(TYPE_ATTR_VALUE, unq(name)); | ||
if (tagChars.isLast()) { | ||
return TAG_STATE_NAME; | ||
} | ||
return TAG_STATE_ATTR; | ||
} | ||
if (!isSingleValueTag) { | ||
return isWS === false; // return (isEQ || isWS) === false; | ||
var validName = function(char) { | ||
return !(char === EQ || isWhiteSpace(char) || tagChars.isLast()); | ||
}; | ||
var name = tagChars.grabWhile(validName); | ||
emitToken(TYPE_TAG, name); | ||
tagChars.skip(); | ||
// in cases when we has [url=someval]GET[/url] and we dont need to parse all | ||
if (isSingleValueTag) { | ||
return TAG_STATE_VALUE; | ||
} | ||
return true; | ||
}; | ||
var _name2 = tagChars.grabWhile(validAttrValue); | ||
tagChars.skip(); | ||
emitToken(_Token.TYPE_ATTR_VALUE, unq(_name2)); | ||
if (tagChars.isLast()) { | ||
return TAG_STATE_NAME; | ||
} | ||
return TAG_STATE_ATTR; | ||
} | ||
var validName = char => !(char === _char.EQ || isWhiteSpace(char) || tagChars.isLast()); | ||
var name = tagChars.grabWhile(validName); | ||
emitToken(_Token.TYPE_TAG, name); | ||
tagChars.skip(); // in cases when we has [url=someval]GET[/url] and we dont need to parse all | ||
if (isSingleValueTag) { | ||
return TAG_STATE_VALUE; | ||
} | ||
var hasEQ = tagChars.includes(_char.EQ); | ||
return hasEQ ? TAG_STATE_ATTR : TAG_STATE_VALUE; | ||
} | ||
function stateTag() { | ||
var currChar = chars.getCurr(); | ||
if (currChar === openTag) { | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = chars.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || chars.isLast()) { | ||
emitToken(_Token.TYPE_WORD, currChar); | ||
var hasEQ = tagChars.includes(EQ); | ||
return hasEQ ? TAG_STATE_ATTR : TAG_STATE_VALUE; | ||
}; | ||
var stateTag = function stateTag() { | ||
var currChar = chars.getCurr(); | ||
if (currChar === openTag) { | ||
var nextChar = chars.getNext(); | ||
chars.skip(); | ||
// detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = chars.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || chars.isLast()) { | ||
emitToken(TYPE_WORD, currChar); | ||
return STATE_WORD; | ||
} | ||
// [myTag ] | ||
var isNoAttrsInTag = substr.indexOf(EQ) === -1; | ||
// [/myTag] | ||
var isClosingTag = substr[0] === SLASH; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
var name = chars.grabWhile(function(char) { | ||
return char !== closeTag; | ||
}); | ||
chars.skip(); // skip closeTag | ||
emitToken(TYPE_TAG, name); | ||
return STATE_WORD; | ||
} | ||
return STATE_TAG_ATTRS; | ||
} | ||
return STATE_WORD; | ||
} // [myTag ] | ||
var isNoAttrsInTag = substr.indexOf(_char.EQ) === -1; // [/myTag] | ||
var isClosingTag = substr[0] === _char.SLASH; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
var name = chars.grabWhile(char => char !== closeTag); | ||
}; | ||
var stateAttrs = function stateAttrs() { | ||
var silent = true; | ||
var tagStr = chars.grabWhile(function(char) { | ||
return char !== closeTag; | ||
}, silent); | ||
var tagGrabber = createCharGrabber(tagStr, { | ||
onSkip: onSkip | ||
}); | ||
var hasSpace = tagGrabber.includes(SPACE); | ||
tagMode = TAG_STATE_NAME; | ||
while(tagGrabber.hasNext()){ | ||
tagMode = nextTagState(tagGrabber, !hasSpace); | ||
} | ||
chars.skip(); // skip closeTag | ||
emitToken(_Token.TYPE_TAG, name); | ||
return STATE_WORD; | ||
} | ||
return STATE_TAG_ATTRS; | ||
} | ||
return STATE_WORD; | ||
} | ||
function stateAttrs() { | ||
var silent = true; | ||
var tagStr = chars.grabWhile(char => char !== closeTag, silent); | ||
var tagGrabber = (0, _utils.createCharGrabber)(tagStr, { | ||
onSkip | ||
}); | ||
var hasSpace = tagGrabber.includes(_char.SPACE); | ||
tagMode = TAG_STATE_NAME; | ||
while (tagGrabber.hasNext()) { | ||
tagMode = nextTagState(tagGrabber, !hasSpace); | ||
} | ||
chars.skip(); // skip closeTag | ||
return STATE_WORD; | ||
} | ||
function stateWord() { | ||
if (isNewLine(chars.getCurr())) { | ||
emitToken(_Token.TYPE_NEW_LINE, chars.getCurr()); | ||
chars.skip(); | ||
col = 0; | ||
row++; | ||
return STATE_WORD; | ||
} | ||
if (isWhiteSpace(chars.getCurr())) { | ||
emitToken(_Token.TYPE_SPACE, chars.grabWhile(isWhiteSpace)); | ||
return STATE_WORD; | ||
} | ||
if (chars.getCurr() === openTag) { | ||
if (chars.includes(closeTag)) { | ||
return STATE_TAG; | ||
} | ||
emitToken(_Token.TYPE_WORD, chars.getCurr()); | ||
chars.skip(); | ||
return STATE_WORD; | ||
} | ||
if (escapeTags) { | ||
if (isEscapeChar(chars.getCurr())) { | ||
var currChar = chars.getCurr(); | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // skip the \ without emitting anything | ||
if (isEscapableChar(nextChar)) { | ||
chars.skip(); // skip past the [, ] or \ as well | ||
emitToken(_Token.TYPE_WORD, nextChar); | ||
return STATE_WORD; | ||
}; | ||
var stateWord = function stateWord() { | ||
if (isNewLine(chars.getCurr())) { | ||
emitToken(TYPE_NEW_LINE, chars.getCurr()); | ||
chars.skip(); | ||
col = 0; | ||
row++; | ||
return STATE_WORD; | ||
} | ||
emitToken(_Token.TYPE_WORD, currChar); | ||
if (isWhiteSpace(chars.getCurr())) { | ||
emitToken(TYPE_SPACE, chars.grabWhile(isWhiteSpace)); | ||
return STATE_WORD; | ||
} | ||
if (chars.getCurr() === openTag) { | ||
if (chars.includes(closeTag)) { | ||
return STATE_TAG; | ||
} | ||
emitToken(TYPE_WORD, chars.getCurr()); | ||
chars.skip(); | ||
return STATE_WORD; | ||
} | ||
if (escapeTags) { | ||
if (isEscapeChar(chars.getCurr())) { | ||
var currChar = chars.getCurr(); | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // skip the \ without emitting anything | ||
if (isEscapableChar(nextChar)) { | ||
chars.skip(); // skip past the [, ] or \ as well | ||
emitToken(TYPE_WORD, nextChar); | ||
return STATE_WORD; | ||
} | ||
emitToken(TYPE_WORD, currChar); | ||
return STATE_WORD; | ||
} | ||
var isChar = function(char) { | ||
return isCharToken(char) && !isEscapeChar(char); | ||
}; | ||
emitToken(TYPE_WORD, chars.grabWhile(isChar)); | ||
return STATE_WORD; | ||
} | ||
emitToken(TYPE_WORD, chars.grabWhile(isCharToken)); | ||
return STATE_WORD; | ||
} | ||
var isChar = char => isCharToken(char) && !isEscapeChar(char); | ||
emitToken(_Token.TYPE_WORD, chars.grabWhile(isChar)); | ||
return STATE_WORD; | ||
} | ||
emitToken(_Token.TYPE_WORD, chars.grabWhile(isCharToken)); | ||
return STATE_WORD; | ||
} | ||
function tokenize() { | ||
stateMode = STATE_WORD; | ||
while (chars.hasNext()) { | ||
switch (stateMode) { | ||
case STATE_TAG: | ||
stateMode = stateTag(); | ||
break; | ||
case STATE_TAG_ATTRS: | ||
stateMode = stateAttrs(); | ||
break; | ||
case STATE_WORD: | ||
stateMode = stateWord(); | ||
break; | ||
default: | ||
stateMode = STATE_WORD; | ||
break; | ||
} | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
} | ||
function isTokenNested(token) { | ||
var value = openTag + _char.SLASH + token.getValue(); // potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
} | ||
return { | ||
tokenize, | ||
isTokenNested | ||
}; | ||
}; | ||
var tokenize = function tokenize() { | ||
stateMode = STATE_WORD; | ||
while(chars.hasNext()){ | ||
switch(stateMode){ | ||
case STATE_TAG: | ||
stateMode = stateTag(); | ||
break; | ||
case STATE_TAG_ATTRS: | ||
stateMode = stateAttrs(); | ||
break; | ||
case STATE_WORD: | ||
stateMode = stateWord(); | ||
break; | ||
default: | ||
stateMode = STATE_WORD; | ||
break; | ||
} | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
}; | ||
var isTokenNested = function isTokenNested(token) { | ||
var value = openTag + SLASH + token.getValue(); | ||
// potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
}; | ||
var STATE_WORD = 0; | ||
var STATE_TAG = 1; | ||
var STATE_TAG_ATTRS = 2; | ||
var TAG_STATE_NAME = 0; | ||
var TAG_STATE_ATTR = 1; | ||
var TAG_STATE_VALUE = 2; | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var stateMode = STATE_WORD; | ||
var tagMode = TAG_STATE_NAME; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || OPEN_BRAKET; | ||
var closeTag = options.closeTag || CLOSE_BRAKET; | ||
var escapeTags = !!options.enableEscapeTags; | ||
var onToken = options.onToken || function() { | ||
}; | ||
var RESERVED_CHARS = [ | ||
closeTag, | ||
openTag, | ||
QUOTEMARK, | ||
BACKSLASH, | ||
SPACE, | ||
TAB, | ||
EQ, | ||
N, | ||
EM | ||
]; | ||
var NOT_CHAR_TOKENS = [ | ||
// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, | ||
SPACE, | ||
TAB, | ||
N, | ||
]; | ||
var WHITESPACES = [ | ||
SPACE, | ||
TAB | ||
]; | ||
var SPECIAL_CHARS = [ | ||
EQ, | ||
SPACE, | ||
TAB | ||
]; | ||
var isCharReserved = function(char) { | ||
return RESERVED_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isNewLine = function(char) { | ||
return char === N; | ||
}; | ||
var isWhiteSpace = function(char) { | ||
return WHITESPACES.indexOf(char) >= 0; | ||
}; | ||
var isCharToken = function(char) { | ||
return NOT_CHAR_TOKENS.indexOf(char) === -1; | ||
}; | ||
var isSpecialChar = function(char) { | ||
return SPECIAL_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isEscapableChar = function(char) { | ||
return char === openTag || char === closeTag || char === BACKSLASH; | ||
}; | ||
var isEscapeChar = function(char) { | ||
return char === BACKSLASH; | ||
}; | ||
var onSkip = function() { | ||
col++; | ||
}; | ||
var unq = function(val) { | ||
return unquote(trimChar(val, QUOTEMARK)); | ||
}; | ||
var chars = createCharGrabber(buffer, { | ||
onSkip: onSkip | ||
}); | ||
return { | ||
tokenize: tokenize, | ||
isTokenNested: isTokenNested | ||
}; | ||
} | ||
var createTokenOfType = createToken; | ||
exports.createTokenOfType = createTokenOfType; | ||
export var createTokenOfType = createToken; | ||
export { createLexer }; |
427
es/parse.js
@@ -1,18 +0,6 @@ | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = exports.parse = void 0; | ||
var _TagNode = _interopRequireDefault(require("@bbob/plugin-helper/lib/TagNode")); | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
var _index = require("@bbob/plugin-helper/lib/index"); | ||
var _lexer = require("./lexer"); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
import TagNode from '@bbob/plugin-helper/lib/TagNode'; | ||
import { CLOSE_BRAKET, OPEN_BRAKET } from '@bbob/plugin-helper/lib/char'; | ||
import { isTagNode } from '@bbob/plugin-helper/lib/index'; | ||
import { createLexer } from './lexer'; | ||
import { createList } from './utils'; | ||
/** | ||
@@ -28,276 +16,215 @@ * @public | ||
* @return {Array} | ||
*/ | ||
var parse = function parse(input, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
var options = opts; | ||
var openTag = options.openTag || _char.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char.CLOSE_BRAKET; | ||
var tokenizer = null; | ||
/** | ||
*/ var parse = function(input, param) { | ||
var opts = param === void 0 ? { | ||
} : param; | ||
var options = opts; | ||
var openTag = options.openTag || OPEN_BRAKET; | ||
var closeTag = options.closeTag || CLOSE_BRAKET; | ||
var tokenizer = null; | ||
/** | ||
* Result AST of nodes | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var nodes = (0, _utils.createList)(); | ||
/** | ||
*/ var nodes = createList(); | ||
/** | ||
* Temp buffer of nodes that's nested to another node | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var nestedNodes = (0, _utils.createList)(); | ||
/** | ||
*/ var nestedNodes = createList(); | ||
/** | ||
* Temp buffer of nodes [tag..]...[/tag] | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var tagNodes = (0, _utils.createList)(); | ||
/** | ||
*/ var tagNodes = createList(); | ||
/** | ||
* Temp buffer of tag attributes | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var tagNodesAttrName = (0, _utils.createList)(); | ||
/** | ||
*/ var tagNodesAttrName = createList(); | ||
/** | ||
* Cache for nested tags checks | ||
*/ | ||
var nestedTagsMap = new Set(); | ||
/** | ||
*/ var nestedTagsMap = new Set(); | ||
/** | ||
* | ||
* @param token | ||
* @returns {boolean} | ||
*/ | ||
var isTokenNested = token => { | ||
var value = token.getValue(); | ||
if (!nestedTagsMap.has(value) && tokenizer.isTokenNested && tokenizer.isTokenNested(token)) { | ||
nestedTagsMap.add(value); | ||
return true; | ||
} | ||
return nestedTagsMap.has(value); | ||
}; | ||
/** | ||
*/ var isTokenNested = function(token) { | ||
var value = token.getValue(); | ||
if (!nestedTagsMap.has(value) && tokenizer.isTokenNested && tokenizer.isTokenNested(token)) { | ||
nestedTagsMap.add(value); | ||
return true; | ||
} | ||
return nestedTagsMap.has(value); | ||
}; | ||
/** | ||
* @param tagName | ||
* @returns {boolean} | ||
*/ | ||
var isTagNested = tagName => Boolean(nestedTagsMap.has(tagName)); | ||
/** | ||
*/ var isTagNested = function(tagName) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
}; | ||
/** | ||
* @private | ||
* @param {String} value | ||
* @return {boolean} | ||
*/ | ||
var isAllowedTag = value => { | ||
if (options.onlyAllowTags && options.onlyAllowTags.length) { | ||
return options.onlyAllowTags.indexOf(value) >= 0; | ||
} | ||
return true; | ||
}; | ||
/** | ||
*/ var isAllowedTag = function(value) { | ||
if (options.onlyAllowTags && options.onlyAllowTags.length) { | ||
return options.onlyAllowTags.indexOf(value) >= 0; | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Flushes temp tag nodes and its attributes buffers | ||
* @private | ||
* @return {Array} | ||
*/ | ||
var flushTagNodes = () => { | ||
if (tagNodes.flushLast()) { | ||
tagNodesAttrName.flushLast(); | ||
} | ||
}; | ||
/** | ||
*/ var flushTagNodes = function() { | ||
if (tagNodes.flushLast()) { | ||
tagNodesAttrName.flushLast(); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @return {Array} | ||
*/ | ||
var getNodes = () => { | ||
var lastNestedNode = nestedNodes.getLast(); | ||
if (lastNestedNode && Array.isArray(lastNestedNode.content)) { | ||
return lastNestedNode.content; | ||
} | ||
return nodes.toArray(); | ||
}; | ||
/** | ||
*/ var getNodes = function() { | ||
var lastNestedNode = nestedNodes.getLast(); | ||
if (lastNestedNode && Array.isArray(lastNestedNode.content)) { | ||
return lastNestedNode.content; | ||
} | ||
return nodes.toArray(); | ||
}; | ||
/** | ||
* @private | ||
* @param {string|TagNode} node | ||
*/ | ||
var appendNodes = node => { | ||
var items = getNodes(); | ||
if (Array.isArray(items)) { | ||
if ((0, _index.isTagNode)(node)) { | ||
if (isAllowedTag(node.tag)) { | ||
items.push(node.toTagNode()); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag, | ||
closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach(item => { | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag, | ||
closeTag | ||
})); | ||
} | ||
*/ var appendNodes = function(node) { | ||
var items = getNodes(); | ||
if (Array.isArray(items)) { | ||
if (isTagNode(node)) { | ||
if (isAllowedTag(node.tag)) { | ||
items.push(node.toTagNode()); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach(function(item) { | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
} | ||
} else { | ||
items.push(node); | ||
} | ||
} | ||
} else { | ||
items.push(node); | ||
} | ||
} | ||
}; | ||
/** | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTagStart = token => { | ||
flushTagNodes(); | ||
var tagNode = _TagNode.default.create(token.getValue()); | ||
var isNested = isTokenNested(token); | ||
tagNodes.push(tagNode); | ||
if (isNested) { | ||
nestedNodes.push(tagNode); | ||
} else { | ||
appendNodes(tagNode, token); | ||
} | ||
}; | ||
/** | ||
*/ var handleTagStart = function(token) { | ||
flushTagNodes(); | ||
var tagNode = TagNode.create(token.getValue()); | ||
var isNested = isTokenNested(token); | ||
tagNodes.push(tagNode); | ||
if (isNested) { | ||
nestedNodes.push(tagNode); | ||
} else { | ||
appendNodes(tagNode, token); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTagEnd = token => { | ||
flushTagNodes(); | ||
var lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode) { | ||
appendNodes(lastNestedNode, token); | ||
} else if (typeof options.onError === 'function') { | ||
var tag = token.getValue(); | ||
var line = token.getLine(); | ||
var column = token.getColumn(); | ||
options.onError({ | ||
message: "Inconsistent tag '" + tag + "' on line " + line + " and column " + column, | ||
tagName: tag, | ||
lineNumber: line, | ||
columnNumber: column | ||
}); | ||
} | ||
}; | ||
/** | ||
*/ var handleTagEnd = function(token) { | ||
flushTagNodes(); | ||
var lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode) { | ||
appendNodes(lastNestedNode, token); | ||
} else if (typeof options.onError === 'function') { | ||
var tag = token.getValue(); | ||
var line = token.getLine(); | ||
var column = token.getColumn(); | ||
options.onError({ | ||
message: "Inconsistent tag '".concat(tag, "' on line ").concat(line, " and column ").concat(column), | ||
tagName: tag, | ||
lineNumber: line, | ||
columnNumber: column | ||
}); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTag = token => { | ||
// [tag] | ||
if (token.isStart()) { | ||
handleTagStart(token); | ||
} // [/tag] | ||
if (token.isEnd()) { | ||
handleTagEnd(token); | ||
} | ||
}; | ||
/** | ||
*/ var handleTag = function(token) { | ||
// [tag] | ||
if (token.isStart()) { | ||
handleTagStart(token); | ||
} | ||
// [/tag] | ||
if (token.isEnd()) { | ||
handleTagEnd(token); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleNode = token => { | ||
/** | ||
*/ var handleNode = function(token) { | ||
/** | ||
* @type {TagNode} | ||
*/ | ||
var lastTagNode = tagNodes.getLast(); | ||
var tokenValue = token.getValue(); | ||
var isNested = isTagNested(token); | ||
if (lastTagNode) { | ||
if (token.isAttrName()) { | ||
tagNodesAttrName.push(tokenValue); | ||
lastTagNode.attr(tagNodesAttrName.getLast(), ''); | ||
} else if (token.isAttrValue()) { | ||
var attrName = tagNodesAttrName.getLast(); | ||
if (attrName) { | ||
lastTagNode.attr(attrName, tokenValue); | ||
tagNodesAttrName.flushLast(); | ||
} else { | ||
lastTagNode.attr(tokenValue, tokenValue); | ||
*/ var lastTagNode = tagNodes.getLast(); | ||
var tokenValue = token.getValue(); | ||
var isNested = isTagNested(token); | ||
if (lastTagNode) { | ||
if (token.isAttrName()) { | ||
tagNodesAttrName.push(tokenValue); | ||
lastTagNode.attr(tagNodesAttrName.getLast(), ''); | ||
} else if (token.isAttrValue()) { | ||
var attrName = tagNodesAttrName.getLast(); | ||
if (attrName) { | ||
lastTagNode.attr(attrName, tokenValue); | ||
tagNodesAttrName.flushLast(); | ||
} else { | ||
lastTagNode.attr(tokenValue, tokenValue); | ||
} | ||
} else if (token.isText()) { | ||
if (isNested) { | ||
lastTagNode.append(tokenValue); | ||
} else { | ||
appendNodes(tokenValue); | ||
} | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
} else if (token.isText()) { | ||
appendNodes(tokenValue); | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
} else if (token.isText()) { | ||
if (isNested) { | ||
lastTagNode.append(tokenValue); | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ var onToken = function(token) { | ||
if (token.isTag()) { | ||
handleTag(token); | ||
} else { | ||
appendNodes(tokenValue); | ||
handleNode(token); | ||
} | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
} else if (token.isText()) { | ||
appendNodes(tokenValue); | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var onToken = token => { | ||
if (token.isTag()) { | ||
handleTag(token); | ||
} else { | ||
handleNode(token); | ||
} | ||
}; | ||
tokenizer = (opts.createTokenizer ? opts.createTokenizer : _lexer.createLexer)(input, { | ||
onToken, | ||
onlyAllowTags: options.onlyAllowTags, | ||
openTag, | ||
closeTag, | ||
enableEscapeTags: options.enableEscapeTags | ||
}); // eslint-disable-next-line no-unused-vars | ||
var tokens = tokenizer.tokenize(); | ||
return nodes.toArray(); | ||
}; | ||
tokenizer = (opts.createTokenizer ? opts.createTokenizer : createLexer)(input, { | ||
onToken: onToken, | ||
onlyAllowTags: options.onlyAllowTags, | ||
openTag: openTag, | ||
closeTag: closeTag, | ||
enableEscapeTags: options.enableEscapeTags | ||
}); | ||
// eslint-disable-next-line no-unused-vars | ||
var tokens = tokenizer.tokenize(); | ||
return nodes.toArray(); | ||
}; | ||
exports.parse = parse; | ||
var _default = parse; | ||
exports.default = _default; | ||
export { parse }; | ||
export default parse; |
331
es/Token.js
@@ -1,40 +0,40 @@ | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = exports.Token = exports.TYPE_NEW_LINE = exports.TYPE_SPACE = exports.TYPE_ATTR_VALUE = exports.TYPE_ATTR_NAME = exports.TYPE_TAG = exports.TYPE_WORD = exports.COLUMN_ID = exports.LINE_ID = exports.VALUE_ID = exports.TYPE_ID = void 0; | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
import { OPEN_BRAKET, CLOSE_BRAKET, SLASH } from '@bbob/plugin-helper/lib/char'; | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a 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); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
// 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 TOKEN_TYPE_WORD = 1; // 'word'; | ||
var TOKEN_TYPE_TAG = 2; // 'tag'; | ||
var TOKEN_TYPE_ATTR_NAME = 3; // 'attr-name'; | ||
var TOKEN_TYPE_ATTR_VALUE = 4; // 'attr-value'; | ||
var TOKEN_TYPE_SPACE = 5; // 'space'; | ||
var TOKEN_TYPE_NEW_LINE = 6; // 'new-line'; | ||
/** | ||
* @param {Token} token | ||
* @returns {string} | ||
*/ | ||
var getTokenValue = token => { | ||
if (token && typeof token[TOKEN_VALUE_ID] !== 'undefined') { | ||
return token[TOKEN_VALUE_ID]; | ||
} | ||
return ''; | ||
*/ var getTokenValue = function(token) { | ||
if (token && typeof token[TOKEN_VALUE_ID] !== 'undefined') { | ||
return token[TOKEN_VALUE_ID]; | ||
} | ||
return ''; | ||
}; | ||
@@ -44,20 +44,16 @@ /** | ||
* @returns {number} | ||
*/ | ||
var getTokenLine = token => token && token[TOKEN_LINE_ID] || 0; | ||
var getTokenColumn = token => token && token[TOKEN_COLUMN_ID] || 0; | ||
*/ var getTokenLine = function(token) { | ||
return token && token[TOKEN_LINE_ID] || 0; | ||
}; | ||
var getTokenColumn = function(token) { | ||
return token && token[TOKEN_COLUMN_ID] || 0; | ||
}; | ||
/** | ||
* @param {Token} token | ||
* @returns {boolean} | ||
*/ | ||
var isTextToken = token => { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD; | ||
} | ||
return false; | ||
*/ var isTextToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD; | ||
} | ||
return false; | ||
}; | ||
@@ -67,137 +63,136 @@ /** | ||
* @returns {boolean} | ||
*/ | ||
var isTagToken = token => { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG; | ||
} | ||
return false; | ||
*/ var isTagToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG; | ||
} | ||
return false; | ||
}; | ||
var isTagEnd = token => getTokenValue(token).charCodeAt(0) === _char.SLASH.charCodeAt(0); | ||
var isTagStart = token => !isTagEnd(token); | ||
var isAttrNameToken = token => { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME; | ||
} | ||
return false; | ||
var isTagEnd = function(token) { | ||
return getTokenValue(token).charCodeAt(0) === SLASH.charCodeAt(0); | ||
}; | ||
var isTagStart = function(token) { | ||
return !isTagEnd(token); | ||
}; | ||
var isAttrNameToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME; | ||
} | ||
return false; | ||
}; | ||
/** | ||
* @param {Token} token | ||
* @returns {boolean} | ||
*/ | ||
var isAttrValueToken = token => { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE; | ||
} | ||
return false; | ||
*/ var isAttrValueToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE; | ||
} | ||
return false; | ||
}; | ||
var getTagName = token => { | ||
var value = getTokenValue(token); | ||
return isTagEnd(token) ? value.slice(1) : value; | ||
var getTagName = function(token) { | ||
var value = getTokenValue(token); | ||
return isTagEnd(token) ? value.slice(1) : value; | ||
}; | ||
var convertTagToText = token => { | ||
var text = _char.OPEN_BRAKET; | ||
text += getTokenValue(token); | ||
text += _char.CLOSE_BRAKET; | ||
return text; | ||
var convertTagToText = function(token) { | ||
var text = OPEN_BRAKET; | ||
text += getTokenValue(token); | ||
text += CLOSE_BRAKET; | ||
return text; | ||
}; | ||
class Token { | ||
/** | ||
* @param {String} type | ||
* @param {String} value | ||
* @param line | ||
* @param row | ||
*/ | ||
constructor(type, value, line, row) { | ||
this[TOKEN_TYPE_ID] = Number(type); | ||
this[TOKEN_VALUE_ID] = String(value); | ||
this[TOKEN_LINE_ID] = Number(line); | ||
this[TOKEN_COLUMN_ID] = Number(row); | ||
} | ||
isEmpty() { | ||
// eslint-disable-next-line no-restricted-globals | ||
return isNaN(this[TOKEN_TYPE_ID]); | ||
} | ||
isText() { | ||
return isTextToken(this); | ||
} | ||
isTag() { | ||
return isTagToken(this); | ||
} | ||
isAttrName() { | ||
return isAttrNameToken(this); | ||
} | ||
isAttrValue() { | ||
return isAttrValueToken(this); | ||
} | ||
isStart() { | ||
return isTagStart(this); | ||
} | ||
isEnd() { | ||
return isTagEnd(this); | ||
} | ||
getName() { | ||
return getTagName(this); | ||
} | ||
getValue() { | ||
return getTokenValue(this); | ||
} | ||
getLine() { | ||
return getTokenLine(this); | ||
} | ||
getColumn() { | ||
return getTokenColumn(this); | ||
} | ||
toString() { | ||
return convertTagToText(this); | ||
} | ||
} | ||
exports.Token = Token; | ||
var TYPE_ID = TOKEN_TYPE_ID; | ||
exports.TYPE_ID = TYPE_ID; | ||
var VALUE_ID = TOKEN_VALUE_ID; | ||
exports.VALUE_ID = VALUE_ID; | ||
var LINE_ID = TOKEN_LINE_ID; | ||
exports.LINE_ID = LINE_ID; | ||
var COLUMN_ID = TOKEN_COLUMN_ID; | ||
exports.COLUMN_ID = COLUMN_ID; | ||
var TYPE_WORD = TOKEN_TYPE_WORD; | ||
exports.TYPE_WORD = TYPE_WORD; | ||
var TYPE_TAG = TOKEN_TYPE_TAG; | ||
exports.TYPE_TAG = TYPE_TAG; | ||
var TYPE_ATTR_NAME = TOKEN_TYPE_ATTR_NAME; | ||
exports.TYPE_ATTR_NAME = TYPE_ATTR_NAME; | ||
var TYPE_ATTR_VALUE = TOKEN_TYPE_ATTR_VALUE; | ||
exports.TYPE_ATTR_VALUE = TYPE_ATTR_VALUE; | ||
var TYPE_SPACE = TOKEN_TYPE_SPACE; | ||
exports.TYPE_SPACE = TYPE_SPACE; | ||
var TYPE_NEW_LINE = TOKEN_TYPE_NEW_LINE; | ||
exports.TYPE_NEW_LINE = TYPE_NEW_LINE; | ||
var _default = Token; | ||
exports.default = _default; | ||
var Token = /*#__PURE__*/ function() { | ||
"use strict"; | ||
function Token(type, value, line, row) { | ||
_classCallCheck(this, Token); | ||
this[TOKEN_TYPE_ID] = Number(type); | ||
this[TOKEN_VALUE_ID] = String(value); | ||
this[TOKEN_LINE_ID] = Number(line); | ||
this[TOKEN_COLUMN_ID] = Number(row); | ||
} | ||
_createClass(Token, [ | ||
{ | ||
key: "isEmpty", | ||
value: function isEmpty() { | ||
// eslint-disable-next-line no-restricted-globals | ||
return isNaN(this[TOKEN_TYPE_ID]); | ||
} | ||
}, | ||
{ | ||
key: "isText", | ||
value: function isText() { | ||
return isTextToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isTag", | ||
value: function isTag() { | ||
return isTagToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isAttrName", | ||
value: function isAttrName() { | ||
return isAttrNameToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isAttrValue", | ||
value: function isAttrValue() { | ||
return isAttrValueToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isStart", | ||
value: function isStart() { | ||
return isTagStart(this); | ||
} | ||
}, | ||
{ | ||
key: "isEnd", | ||
value: function isEnd() { | ||
return isTagEnd(this); | ||
} | ||
}, | ||
{ | ||
key: "getName", | ||
value: function getName() { | ||
return getTagName(this); | ||
} | ||
}, | ||
{ | ||
key: "getValue", | ||
value: function getValue() { | ||
return getTokenValue(this); | ||
} | ||
}, | ||
{ | ||
key: "getLine", | ||
value: function getLine() { | ||
return getTokenLine(this); | ||
} | ||
}, | ||
{ | ||
key: "getColumn", | ||
value: function getColumn() { | ||
return getTokenColumn(this); | ||
} | ||
}, | ||
{ | ||
key: "toString", | ||
value: function toString() { | ||
return convertTagToText(this); | ||
} | ||
} | ||
]); | ||
return Token; | ||
}(); | ||
export var TYPE_ID = TOKEN_TYPE_ID; | ||
export var VALUE_ID = TOKEN_VALUE_ID; | ||
export var LINE_ID = TOKEN_LINE_ID; | ||
export var COLUMN_ID = TOKEN_COLUMN_ID; | ||
export var TYPE_WORD = TOKEN_TYPE_WORD; | ||
export var TYPE_TAG = TOKEN_TYPE_TAG; | ||
export var TYPE_ATTR_NAME = TOKEN_TYPE_ATTR_NAME; | ||
export var TYPE_ATTR_VALUE = TOKEN_TYPE_ATTR_VALUE; | ||
export var TYPE_SPACE = TOKEN_TYPE_SPACE; | ||
export var TYPE_NEW_LINE = TOKEN_TYPE_NEW_LINE; | ||
export { Token }; | ||
export default Token; |
274
es/utils.js
@@ -1,122 +0,86 @@ | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.createList = exports.unquote = exports.trimChar = exports.createCharGrabber = void 0; | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
import { QUOTEMARK, BACKSLASH } from '@bbob/plugin-helper/lib/char'; | ||
function CharGrabber(source, options) { | ||
var cursor = { | ||
pos: 0, | ||
len: source.length | ||
}; | ||
var substrUntilChar = char => { | ||
var { | ||
pos | ||
} = cursor; | ||
var idx = source.indexOf(char, pos); | ||
return idx >= 0 ? source.substr(pos, idx - pos) : ''; | ||
}; | ||
var includes = val => source.indexOf(val, cursor.pos) >= 0; | ||
var hasNext = () => cursor.len > cursor.pos; | ||
var isLast = () => cursor.pos === cursor.len; | ||
var skip = function skip(num, silent) { | ||
if (num === void 0) { | ||
num = 1; | ||
} | ||
cursor.pos += num; | ||
if (options && options.onSkip && !silent) { | ||
options.onSkip(); | ||
} | ||
}; | ||
var rest = () => source.substr(cursor.pos); | ||
var curr = () => source[cursor.pos]; | ||
var prev = () => { | ||
var prevPos = cursor.pos - 1; | ||
return typeof source[prevPos] !== 'undefined' ? source[prevPos] : null; | ||
}; | ||
var next = () => { | ||
var nextPos = cursor.pos + 1; | ||
return nextPos <= source.length - 1 ? source[nextPos] : null; | ||
}; | ||
var grabWhile = (cond, silent) => { | ||
var start = 0; | ||
if (hasNext()) { | ||
start = cursor.pos; | ||
while (hasNext() && cond(curr())) { | ||
skip(1, silent); | ||
} | ||
} | ||
return source.substr(start, cursor.pos - start); | ||
}; | ||
/** | ||
var cursor = { | ||
pos: 0, | ||
len: source.length | ||
}; | ||
var substrUntilChar = function(char) { | ||
var pos = cursor.pos; | ||
var idx = source.indexOf(char, pos); | ||
return idx >= 0 ? source.substr(pos, idx - pos) : ''; | ||
}; | ||
var includes = function(val) { | ||
return source.indexOf(val, cursor.pos) >= 0; | ||
}; | ||
var hasNext = function() { | ||
return cursor.len > cursor.pos; | ||
}; | ||
var isLast = function() { | ||
return cursor.pos === cursor.len; | ||
}; | ||
var skip = function(param, silent) { | ||
var num = param === void 0 ? 1 : param; | ||
cursor.pos += num; | ||
if (options && options.onSkip && !silent) { | ||
options.onSkip(); | ||
} | ||
}; | ||
var rest = function() { | ||
return source.substr(cursor.pos); | ||
}; | ||
var curr = function() { | ||
return source[cursor.pos]; | ||
}; | ||
var prev = function() { | ||
var prevPos = cursor.pos - 1; | ||
return typeof source[prevPos] !== 'undefined' ? source[prevPos] : null; | ||
}; | ||
var next = function() { | ||
var nextPos = cursor.pos + 1; | ||
return nextPos <= source.length - 1 ? source[nextPos] : null; | ||
}; | ||
var grabWhile = function(cond, silent) { | ||
var start = 0; | ||
if (hasNext()) { | ||
start = cursor.pos; | ||
while(hasNext() && cond(curr())){ | ||
skip(1, silent); | ||
} | ||
} | ||
return source.substr(start, cursor.pos - start); | ||
}; | ||
/** | ||
* @type {skip} | ||
*/ | ||
this.skip = skip; | ||
/** | ||
*/ this.skip = skip; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.hasNext = hasNext; | ||
/** | ||
*/ this.hasNext = hasNext; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getCurr = curr; | ||
/** | ||
*/ this.getCurr = curr; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getRest = rest; | ||
/** | ||
*/ this.getRest = rest; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getNext = next; | ||
/** | ||
*/ this.getNext = next; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getPrev = prev; | ||
/** | ||
*/ this.getPrev = prev; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.isLast = isLast; | ||
/** | ||
*/ this.isLast = isLast; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.includes = includes; | ||
/** | ||
*/ this.includes = includes; | ||
/** | ||
* @param {Function} cond | ||
* @param {Boolean} silent | ||
* @return {String} | ||
*/ | ||
this.grabWhile = grabWhile; | ||
/** | ||
*/ this.grabWhile = grabWhile; | ||
/** | ||
* Grabs rest of string until it find a char | ||
* @param {String} char | ||
* @return {String} | ||
*/ | ||
this.substrUntilChar = substrUntilChar; | ||
*/ this.substrUntilChar = substrUntilChar; | ||
} | ||
@@ -129,6 +93,5 @@ /** | ||
* @return CharGrabber | ||
*/ | ||
var createCharGrabber = (source, options) => new CharGrabber(source, options); | ||
*/ export var createCharGrabber = function(source, options) { | ||
return new CharGrabber(source, options); | ||
}; | ||
/** | ||
@@ -141,19 +104,12 @@ * Trims string from start and end by char | ||
* @returns {String} | ||
*/ | ||
exports.createCharGrabber = createCharGrabber; | ||
var trimChar = (str, charToRemove) => { | ||
while (str.charAt(0) === charToRemove) { | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(1); | ||
} | ||
while (str.charAt(str.length - 1) === charToRemove) { | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
*/ export var trimChar = function(str, charToRemove) { | ||
while(str.charAt(0) === charToRemove){ | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(1); | ||
} | ||
while(str.charAt(str.length - 1) === charToRemove){ | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
}; | ||
@@ -164,30 +120,24 @@ /** | ||
* @return {String} | ||
*/ | ||
exports.trimChar = trimChar; | ||
var unquote = str => str.replace(_char.BACKSLASH + _char.QUOTEMARK, _char.QUOTEMARK); | ||
exports.unquote = unquote; | ||
function NodeList(values) { | ||
if (values === void 0) { | ||
values = []; | ||
} | ||
var nodes = values; | ||
var getLast = () => Array.isArray(nodes) && nodes.length > 0 && typeof nodes[nodes.length - 1] !== 'undefined' ? nodes[nodes.length - 1] : null; | ||
var flushLast = () => nodes.length ? nodes.pop() : false; | ||
var push = value => nodes.push(value); | ||
var toArray = () => nodes; | ||
this.push = push; | ||
this.toArray = toArray; | ||
this.getLast = getLast; | ||
this.flushLast = flushLast; | ||
*/ export var unquote = function(str) { | ||
return str.replace(BACKSLASH + QUOTEMARK, QUOTEMARK); | ||
}; | ||
function NodeList(param) { | ||
var values = param === void 0 ? [] : param; | ||
var nodes = values; | ||
var getLast = function() { | ||
return Array.isArray(nodes) && nodes.length > 0 && typeof nodes[nodes.length - 1] !== 'undefined' ? nodes[nodes.length - 1] : null; | ||
}; | ||
var flushLast = function() { | ||
return nodes.length ? nodes.pop() : false; | ||
}; | ||
var push = function(value) { | ||
return nodes.push(value); | ||
}; | ||
var toArray = function() { | ||
return nodes; | ||
}; | ||
this.push = push; | ||
this.toArray = toArray; | ||
this.getLast = getLast; | ||
this.flushLast = flushLast; | ||
} | ||
@@ -198,13 +148,5 @@ /** | ||
* @return {NodeList} | ||
*/ | ||
var createList = function createList(values) { | ||
if (values === void 0) { | ||
values = []; | ||
} | ||
return new NodeList(values); | ||
*/ export var createList = function(param) { | ||
var values = param === void 0 ? [] : param; | ||
return new NodeList(values); | ||
}; | ||
exports.createList = createList; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.TagNode = exports.parse = exports["default"] = void 0; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "default", { | ||
enumerable: true, | ||
get: function() { | ||
return _parse.default; | ||
} | ||
}); | ||
Object.defineProperty(exports, "parse", { | ||
enumerable: true, | ||
get: function() { | ||
return _parse.parse; | ||
} | ||
}); | ||
Object.defineProperty(exports, "TagNode", { | ||
enumerable: true, | ||
get: function() { | ||
return _tagNode.TagNode; | ||
} | ||
}); | ||
var _parse = _interopRequireWildcard(require("./parse")); | ||
exports["default"] = _parse["default"]; | ||
exports.parse = _parse.parse; | ||
var _TagNode = require("@bbob/plugin-helper/lib/TagNode"); | ||
exports.TagNode = _TagNode.TagNode; | ||
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
var _tagNode = require("@bbob/plugin-helper/lib/TagNode"); | ||
function _interopRequireWildcard(obj) { | ||
if (obj && obj.__esModule) { | ||
return obj; | ||
} else { | ||
var newObj = { | ||
}; | ||
if (obj != null) { | ||
for(var key in obj){ | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : { | ||
}; | ||
if (desc.get || desc.set) { | ||
Object.defineProperty(newObj, key, desc); | ||
} else { | ||
newObj[key] = obj[key]; | ||
} | ||
} | ||
} | ||
} | ||
newObj.default = obj; | ||
return newObj; | ||
} | ||
} |
"use strict"; | ||
exports.__esModule = true; | ||
exports.createLexer = createLexer; | ||
exports.createTokenOfType = void 0; | ||
var _char9 = require("@bbob/plugin-helper/lib/char"); | ||
var _Token = require("./Token"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createLexer = exports.createTokenOfType = void 0; | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
var _token = require("./Token"); | ||
var _utils = require("./utils"); | ||
/* eslint-disable no-plusplus,no-param-reassign */ | ||
// for cases <!-- --> | ||
@@ -22,14 +17,5 @@ var EM = '!'; | ||
* @param {Number} cl char number in line | ||
*/ | ||
var createToken = function createToken(type, value, r, cl) { | ||
if (r === void 0) { | ||
r = 0; | ||
} | ||
if (cl === void 0) { | ||
cl = 0; | ||
} | ||
return new _Token.Token(type, value, r, cl); | ||
*/ var createToken = function(type, value, param, param1) { | ||
var r = param === void 0 ? 0 : param, cl = param1 === void 0 ? 0 : param1; | ||
return new _token.Token(type, value, r, cl); | ||
}; | ||
@@ -40,5 +26,3 @@ /** | ||
* @property {Function} isTokenNested | ||
*/ | ||
/** | ||
*/ /** | ||
* @param {String} buffer | ||
@@ -51,223 +35,200 @@ * @param {Object} options | ||
* @return {Lexer} | ||
*/ | ||
function createLexer(buffer, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || _char9.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char9.CLOSE_BRAKET; | ||
var escapeTags = options.enableEscapeTags; | ||
var RESERVED_CHARS = [closeTag, openTag, _char9.QUOTEMARK, _char9.BACKSLASH, _char9.SPACE, _char9.TAB, _char9.EQ, _char9.N, EM]; | ||
var NOT_CHAR_TOKENS = [// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, _char9.SPACE, _char9.TAB, _char9.N]; | ||
var WHITESPACES = [_char9.SPACE, _char9.TAB]; | ||
var SPECIAL_CHARS = [_char9.EQ, _char9.SPACE, _char9.TAB]; | ||
var isCharReserved = function isCharReserved(_char) { | ||
return RESERVED_CHARS.indexOf(_char) >= 0; | ||
}; | ||
var isWhiteSpace = function isWhiteSpace(_char2) { | ||
return WHITESPACES.indexOf(_char2) >= 0; | ||
}; | ||
var isCharToken = function isCharToken(_char3) { | ||
return NOT_CHAR_TOKENS.indexOf(_char3) === -1; | ||
}; | ||
var isSpecialChar = function isSpecialChar(_char4) { | ||
return SPECIAL_CHARS.indexOf(_char4) >= 0; | ||
}; | ||
var isEscapableChar = function isEscapableChar(_char5) { | ||
return _char5 === openTag || _char5 === closeTag || _char5 === _char9.BACKSLASH; | ||
}; | ||
var isEscapeChar = function isEscapeChar(_char6) { | ||
return _char6 === _char9.BACKSLASH; | ||
}; | ||
/** | ||
*/ function createLexer(buffer, param) { | ||
var options = param === void 0 ? { | ||
} : param; | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || _char.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char.CLOSE_BRAKET; | ||
var escapeTags = options.enableEscapeTags; | ||
var RESERVED_CHARS = [ | ||
closeTag, | ||
openTag, | ||
_char.QUOTEMARK, | ||
_char.BACKSLASH, | ||
_char.SPACE, | ||
_char.TAB, | ||
_char.EQ, | ||
_char.N, | ||
EM | ||
]; | ||
var NOT_CHAR_TOKENS = [ | ||
// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, | ||
_char.SPACE, | ||
_char.TAB, | ||
_char.N, | ||
]; | ||
var WHITESPACES = [ | ||
_char.SPACE, | ||
_char.TAB | ||
]; | ||
var SPECIAL_CHARS = [ | ||
_char.EQ, | ||
_char.SPACE, | ||
_char.TAB | ||
]; | ||
var isCharReserved = function(char) { | ||
return RESERVED_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isWhiteSpace = function(char) { | ||
return WHITESPACES.indexOf(char) >= 0; | ||
}; | ||
var isCharToken = function(char) { | ||
return NOT_CHAR_TOKENS.indexOf(char) === -1; | ||
}; | ||
var isSpecialChar = function(char) { | ||
return SPECIAL_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isEscapableChar = function(char) { | ||
return char === openTag || char === closeTag || char === _char.BACKSLASH; | ||
}; | ||
var isEscapeChar = function(char) { | ||
return char === _char.BACKSLASH; | ||
}; | ||
/** | ||
* Emits newly created token to subscriber | ||
* @param token | ||
*/ | ||
var emitToken = function emitToken(token) { | ||
if (options.onToken) { | ||
options.onToken(token); | ||
} | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
}; | ||
/** | ||
*/ var emitToken = function(token) { | ||
if (options.onToken) { | ||
options.onToken(token); | ||
} | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
}; | ||
/** | ||
* Parses params inside [myTag---params goes here---]content[/myTag] | ||
* @param str | ||
* @returns {{tag: *, attrs: Array}} | ||
*/ | ||
var parseAttrs = function parseAttrs(str) { | ||
var tagName = null; | ||
var skipSpecialChars = false; | ||
var attrTokens = []; | ||
var attrCharGrabber = (0, _utils.createCharGrabber)(str); | ||
var validAttr = function validAttr(_char7) { | ||
var isEQ = _char7 === _char9.EQ; | ||
var isWS = isWhiteSpace(_char7); | ||
var prevChar = attrCharGrabber.getPrev(); | ||
var nextChar = attrCharGrabber.getNext(); | ||
var isPrevSLASH = prevChar === _char9.BACKSLASH; | ||
var isTagNameEmpty = tagName === null; | ||
if (isTagNameEmpty) { | ||
return (isEQ || isWS || attrCharGrabber.isLast()) === false; | ||
} | ||
if (skipSpecialChars && isSpecialChar(_char7)) { | ||
return true; | ||
} | ||
if (_char7 === _char9.QUOTEMARK && !isPrevSLASH) { | ||
skipSpecialChars = !skipSpecialChars; | ||
if (!skipSpecialChars && !(nextChar === _char9.EQ || isWhiteSpace(nextChar))) { | ||
return false; | ||
*/ var parseAttrs = function(str) { | ||
var tagName = null; | ||
var skipSpecialChars = false; | ||
var attrTokens = []; | ||
var attrCharGrabber = (0, _utils).createCharGrabber(str); | ||
var validAttr = function(char) { | ||
var isEQ = char === _char.EQ; | ||
var isWS = isWhiteSpace(char); | ||
var prevChar = attrCharGrabber.getPrev(); | ||
var nextChar = attrCharGrabber.getNext(); | ||
var isPrevSLASH = prevChar === _char.BACKSLASH; | ||
var isTagNameEmpty = tagName === null; | ||
if (isTagNameEmpty) { | ||
return (isEQ || isWS || attrCharGrabber.isLast()) === false; | ||
} | ||
if (skipSpecialChars && isSpecialChar(char)) { | ||
return true; | ||
} | ||
if (char === _char.QUOTEMARK && !isPrevSLASH) { | ||
skipSpecialChars = !skipSpecialChars; | ||
if (!skipSpecialChars && !(nextChar === _char.EQ || isWhiteSpace(nextChar))) { | ||
return false; | ||
} | ||
} | ||
return (isEQ || isWS) === false; | ||
}; | ||
var nextAttr = function() { | ||
var attrStr = attrCharGrabber.grabWhile(validAttr); | ||
var currChar = attrCharGrabber.getCurr(); | ||
// first string before space is a tag name [tagName params...] | ||
if (tagName === null) { | ||
tagName = attrStr; | ||
} else if (isWhiteSpace(currChar) || currChar === _char.QUOTEMARK || !attrCharGrabber.hasNext()) { | ||
var escaped = (0, _utils).unquote((0, _utils).trimChar(attrStr, _char.QUOTEMARK)); | ||
attrTokens.push(createToken(_token.TYPE_ATTR_VALUE, escaped, row, col)); | ||
} else { | ||
attrTokens.push(createToken(_token.TYPE_ATTR_NAME, attrStr, row, col)); | ||
} | ||
attrCharGrabber.skip(); | ||
}; | ||
while(attrCharGrabber.hasNext()){ | ||
nextAttr(); | ||
} | ||
} | ||
return (isEQ || isWS) === false; | ||
return { | ||
tag: tagName, | ||
attrs: attrTokens | ||
}; | ||
}; | ||
var nextAttr = function nextAttr() { | ||
var attrStr = attrCharGrabber.grabWhile(validAttr); | ||
var currChar = attrCharGrabber.getCurr(); // first string before space is a tag name [tagName params...] | ||
if (tagName === null) { | ||
tagName = attrStr; | ||
} else if (isWhiteSpace(currChar) || currChar === _char9.QUOTEMARK || !attrCharGrabber.hasNext()) { | ||
var escaped = (0, _utils.unquote)((0, _utils.trimChar)(attrStr, _char9.QUOTEMARK)); | ||
attrTokens.push(createToken(_Token.TYPE_ATTR_VALUE, escaped, row, col)); | ||
} else { | ||
attrTokens.push(createToken(_Token.TYPE_ATTR_NAME, attrStr, row, col)); | ||
} | ||
attrCharGrabber.skip(); | ||
var bufferGrabber = (0, _utils).createCharGrabber(buffer, { | ||
onSkip: function() { | ||
col++; | ||
} | ||
}); | ||
var next = function() { | ||
var currChar = bufferGrabber.getCurr(); | ||
var nextChar = bufferGrabber.getNext(); | ||
if (currChar === _char.N) { | ||
bufferGrabber.skip(); | ||
col = 0; | ||
row++; | ||
emitToken(createToken(_token.TYPE_NEW_LINE, currChar, row, col)); | ||
} else if (isWhiteSpace(currChar)) { | ||
var str = bufferGrabber.grabWhile(isWhiteSpace); | ||
emitToken(createToken(_token.TYPE_SPACE, str, row, col)); | ||
} else if (escapeTags && isEscapeChar(currChar) && isEscapableChar(nextChar)) { | ||
bufferGrabber.skip(); // skip the \ without emitting anything | ||
bufferGrabber.skip(); // skip past the [, ] or \ as well | ||
emitToken(createToken(_token.TYPE_WORD, nextChar, row, col)); | ||
} else if (currChar === openTag) { | ||
bufferGrabber.skip(); // skip openTag | ||
// detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = bufferGrabber.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || bufferGrabber.isLast()) { | ||
emitToken(createToken(_token.TYPE_WORD, currChar, row, col)); | ||
} else { | ||
var str = bufferGrabber.grabWhile(function(val) { | ||
return val !== closeTag; | ||
}); | ||
bufferGrabber.skip(); // skip closeTag | ||
// [myTag ] | ||
var isNoAttrsInTag = str.indexOf(_char.EQ) === -1; | ||
// [/myTag] | ||
var isClosingTag = str[0] === _char.SLASH; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
emitToken(createToken(_token.TYPE_TAG, str, row, col)); | ||
} else { | ||
var parsed = parseAttrs(str); | ||
emitToken(createToken(_token.TYPE_TAG, parsed.tag, row, col)); | ||
parsed.attrs.map(emitToken); | ||
} | ||
} | ||
} else if (currChar === closeTag) { | ||
bufferGrabber.skip(); // skip closeTag | ||
emitToken(createToken(_token.TYPE_WORD, currChar, row, col)); | ||
} else if (isCharToken(currChar)) { | ||
if (escapeTags && isEscapeChar(currChar) && !isEscapableChar(nextChar)) { | ||
bufferGrabber.skip(); | ||
emitToken(createToken(_token.TYPE_WORD, currChar, row, col)); | ||
} else { | ||
var str = bufferGrabber.grabWhile(function(char) { | ||
if (escapeTags) { | ||
return isCharToken(char) && !isEscapeChar(char); | ||
} | ||
return isCharToken(char); | ||
}); | ||
emitToken(createToken(_token.TYPE_WORD, str, row, col)); | ||
} | ||
} | ||
}; | ||
while (attrCharGrabber.hasNext()) { | ||
nextAttr(); | ||
} | ||
var tokenize = function() { | ||
while(bufferGrabber.hasNext()){ | ||
next(); | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
}; | ||
var isTokenNested = function(token) { | ||
var value = openTag + _char.SLASH + token.getValue(); | ||
// potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
}; | ||
return { | ||
tag: tagName, | ||
attrs: attrTokens | ||
tokenize: tokenize, | ||
isTokenNested: isTokenNested | ||
}; | ||
}; | ||
var bufferGrabber = (0, _utils.createCharGrabber)(buffer, { | ||
onSkip: function onSkip() { | ||
col++; | ||
} | ||
}); | ||
var next = function next() { | ||
var currChar = bufferGrabber.getCurr(); | ||
var nextChar = bufferGrabber.getNext(); | ||
if (currChar === _char9.N) { | ||
bufferGrabber.skip(); | ||
col = 0; | ||
row++; | ||
emitToken(createToken(_Token.TYPE_NEW_LINE, currChar, row, col)); | ||
} else if (isWhiteSpace(currChar)) { | ||
var str = bufferGrabber.grabWhile(isWhiteSpace); | ||
emitToken(createToken(_Token.TYPE_SPACE, str, row, col)); | ||
} else if (escapeTags && isEscapeChar(currChar) && isEscapableChar(nextChar)) { | ||
bufferGrabber.skip(); // skip the \ without emitting anything | ||
bufferGrabber.skip(); // skip past the [, ] or \ as well | ||
emitToken(createToken(_Token.TYPE_WORD, nextChar, row, col)); | ||
} else if (currChar === openTag) { | ||
bufferGrabber.skip(); // skip openTag | ||
// detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = bufferGrabber.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || bufferGrabber.isLast()) { | ||
emitToken(createToken(_Token.TYPE_WORD, currChar, row, col)); | ||
} else { | ||
var _str = bufferGrabber.grabWhile(function (val) { | ||
return val !== closeTag; | ||
}); | ||
bufferGrabber.skip(); // skip closeTag | ||
// [myTag ] | ||
var isNoAttrsInTag = _str.indexOf(_char9.EQ) === -1; // [/myTag] | ||
var isClosingTag = _str[0] === _char9.SLASH; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
emitToken(createToken(_Token.TYPE_TAG, _str, row, col)); | ||
} else { | ||
var parsed = parseAttrs(_str); | ||
emitToken(createToken(_Token.TYPE_TAG, parsed.tag, row, col)); | ||
parsed.attrs.map(emitToken); | ||
} | ||
} | ||
} else if (currChar === closeTag) { | ||
bufferGrabber.skip(); // skip closeTag | ||
emitToken(createToken(_Token.TYPE_WORD, currChar, row, col)); | ||
} else if (isCharToken(currChar)) { | ||
if (escapeTags && isEscapeChar(currChar) && !isEscapableChar(nextChar)) { | ||
bufferGrabber.skip(); | ||
emitToken(createToken(_Token.TYPE_WORD, currChar, row, col)); | ||
} else { | ||
var _str2 = bufferGrabber.grabWhile(function (_char8) { | ||
if (escapeTags) { | ||
return isCharToken(_char8) && !isEscapeChar(_char8); | ||
} | ||
return isCharToken(_char8); | ||
}); | ||
emitToken(createToken(_Token.TYPE_WORD, _str2, row, col)); | ||
} | ||
} | ||
}; | ||
var tokenize = function tokenize() { | ||
while (bufferGrabber.hasNext()) { | ||
next(); | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
}; | ||
var isTokenNested = function isTokenNested(token) { | ||
var value = openTag + _char9.SLASH + token.getValue(); // potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
}; | ||
return { | ||
tokenize: tokenize, | ||
isTokenNested: isTokenNested | ||
}; | ||
} | ||
var createTokenOfType = createToken; | ||
exports.createTokenOfType = createTokenOfType; | ||
exports.createTokenOfType = createTokenOfType; | ||
exports.createLexer = createLexer; |
609
lib/lexer.js
"use strict"; | ||
exports.__esModule = true; | ||
exports.createLexer = createLexer; | ||
exports.createTokenOfType = void 0; | ||
var _char14 = require("@bbob/plugin-helper/lib/char"); | ||
var _Token = require("./Token"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createLexer = exports.createTokenOfType = void 0; | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
var _token = require("./Token"); | ||
var _utils = require("./utils"); | ||
/* eslint-disable no-plusplus,no-param-reassign */ | ||
// for cases <!-- --> | ||
@@ -22,14 +17,5 @@ var EM = '!'; | ||
* @param {Number} cl char number in line | ||
*/ | ||
var createToken = function createToken(type, value, r, cl) { | ||
if (r === void 0) { | ||
r = 0; | ||
} | ||
if (cl === void 0) { | ||
cl = 0; | ||
} | ||
return new _Token.Token(type, value, r, cl); | ||
*/ var createToken = function(type, value, param, param1) { | ||
var r = param === void 0 ? 0 : param, cl = param1 === void 0 ? 0 : param1; | ||
return new _token.Token(type, value, r, cl); | ||
}; | ||
@@ -40,5 +26,3 @@ /** | ||
* @property {Function} isTokenNested | ||
*/ | ||
/** | ||
*/ /** | ||
* @param {String} buffer | ||
@@ -51,327 +35,278 @@ * @param {Object} options | ||
* @return {Lexer} | ||
*/ | ||
function createLexer(buffer, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var STATE_WORD = 0; | ||
var STATE_TAG = 1; | ||
var STATE_TAG_ATTRS = 2; | ||
var TAG_STATE_NAME = 0; | ||
var TAG_STATE_ATTR = 1; | ||
var TAG_STATE_VALUE = 2; | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var stateMode = STATE_WORD; | ||
var tagMode = TAG_STATE_NAME; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || _char14.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char14.CLOSE_BRAKET; | ||
var escapeTags = !!options.enableEscapeTags; | ||
var onToken = options.onToken || function () {}; | ||
var RESERVED_CHARS = [closeTag, openTag, _char14.QUOTEMARK, _char14.BACKSLASH, _char14.SPACE, _char14.TAB, _char14.EQ, _char14.N, EM]; | ||
var NOT_CHAR_TOKENS = [// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, _char14.SPACE, _char14.TAB, _char14.N]; | ||
var WHITESPACES = [_char14.SPACE, _char14.TAB]; | ||
var SPECIAL_CHARS = [_char14.EQ, _char14.SPACE, _char14.TAB]; | ||
var isCharReserved = function isCharReserved(_char) { | ||
return RESERVED_CHARS.indexOf(_char) >= 0; | ||
}; | ||
var isNewLine = function isNewLine(_char2) { | ||
return _char2 === _char14.N; | ||
}; | ||
var isWhiteSpace = function isWhiteSpace(_char3) { | ||
return WHITESPACES.indexOf(_char3) >= 0; | ||
}; | ||
var isCharToken = function isCharToken(_char4) { | ||
return NOT_CHAR_TOKENS.indexOf(_char4) === -1; | ||
}; | ||
var isSpecialChar = function isSpecialChar(_char5) { | ||
return SPECIAL_CHARS.indexOf(_char5) >= 0; | ||
}; | ||
var isEscapableChar = function isEscapableChar(_char6) { | ||
return _char6 === openTag || _char6 === closeTag || _char6 === _char14.BACKSLASH; | ||
}; | ||
var isEscapeChar = function isEscapeChar(_char7) { | ||
return _char7 === _char14.BACKSLASH; | ||
}; | ||
var onSkip = function onSkip() { | ||
col++; | ||
}; | ||
var unq = function unq(val) { | ||
return (0, _utils.unquote)((0, _utils.trimChar)(val, _char14.QUOTEMARK)); | ||
}; | ||
var chars = (0, _utils.createCharGrabber)(buffer, { | ||
onSkip: onSkip | ||
}); | ||
/** | ||
*/ function createLexer(buffer, param) { | ||
var options = param === void 0 ? { | ||
} : param; | ||
var emitToken = /** | ||
* Emits newly created token to subscriber | ||
* @param {Number} type | ||
* @param {String} value | ||
*/ | ||
function emitToken(type, value) { | ||
var token = createToken(type, value, row, col); | ||
onToken(token); | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
} | ||
function nextTagState(tagChars, isSingleValueTag) { | ||
if (tagMode === TAG_STATE_ATTR) { | ||
var validAttrName = function validAttrName(_char8) { | ||
return !(_char8 === _char14.EQ || isWhiteSpace(_char8)); | ||
}; | ||
var _name = tagChars.grabWhile(validAttrName); | ||
var isEnd = tagChars.isLast(); | ||
var isValue = tagChars.getCurr() !== _char14.EQ; | ||
tagChars.skip(); | ||
if (isEnd || isValue) { | ||
emitToken(_Token.TYPE_ATTR_VALUE, unq(_name)); | ||
} else { | ||
emitToken(_Token.TYPE_ATTR_NAME, _name); | ||
} | ||
if (isEnd) { | ||
return TAG_STATE_NAME; | ||
} | ||
if (isValue) { | ||
return TAG_STATE_ATTR; | ||
} | ||
return TAG_STATE_VALUE; | ||
} | ||
if (tagMode === TAG_STATE_VALUE) { | ||
var stateSpecial = false; | ||
var validAttrValue = function validAttrValue(_char9) { | ||
// const isEQ = char === EQ; | ||
var isQM = _char9 === _char14.QUOTEMARK; | ||
var prevChar = tagChars.getPrev(); | ||
var nextChar = tagChars.getNext(); | ||
var isPrevSLASH = prevChar === _char14.BACKSLASH; | ||
var isNextEQ = nextChar === _char14.EQ; | ||
var isWS = isWhiteSpace(_char9); // const isPrevWS = isWhiteSpace(prevChar); | ||
var isNextWS = isWhiteSpace(nextChar); | ||
if (stateSpecial && isSpecialChar(_char9)) { | ||
return true; | ||
*/ function emitToken(type, value) { | ||
var token = createToken(type, value, row, col); | ||
onToken(token); | ||
tokenIndex += 1; | ||
tokens[tokenIndex] = token; | ||
}; | ||
var nextTagState = function nextTagState(tagChars, isSingleValueTag) { | ||
if (tagMode === TAG_STATE_ATTR) { | ||
var validAttrName = function(char) { | ||
return !(char === _char.EQ || isWhiteSpace(char)); | ||
}; | ||
var name = tagChars.grabWhile(validAttrName); | ||
var isEnd = tagChars.isLast(); | ||
var isValue = tagChars.getCurr() !== _char.EQ; | ||
tagChars.skip(); | ||
if (isEnd || isValue) { | ||
emitToken(_token.TYPE_ATTR_VALUE, unq(name)); | ||
} else { | ||
emitToken(_token.TYPE_ATTR_NAME, name); | ||
} | ||
if (isEnd) { | ||
return TAG_STATE_NAME; | ||
} | ||
if (isValue) { | ||
return TAG_STATE_ATTR; | ||
} | ||
return TAG_STATE_VALUE; | ||
} | ||
if (isQM && !isPrevSLASH) { | ||
stateSpecial = !stateSpecial; | ||
if (!stateSpecial && !(isNextEQ || isNextWS)) { | ||
return false; | ||
} | ||
if (tagMode === TAG_STATE_VALUE) { | ||
var stateSpecial = false; | ||
var validAttrValue = function(char) { | ||
// const isEQ = char === EQ; | ||
var isQM = char === _char.QUOTEMARK; | ||
var prevChar = tagChars.getPrev(); | ||
var nextChar = tagChars.getNext(); | ||
var isPrevSLASH = prevChar === _char.BACKSLASH; | ||
var isNextEQ = nextChar === _char.EQ; | ||
var isWS = isWhiteSpace(char); | ||
// const isPrevWS = isWhiteSpace(prevChar); | ||
var isNextWS = isWhiteSpace(nextChar); | ||
if (stateSpecial && isSpecialChar(char)) { | ||
return true; | ||
} | ||
if (isQM && !isPrevSLASH) { | ||
stateSpecial = !stateSpecial; | ||
if (!stateSpecial && !(isNextEQ || isNextWS)) { | ||
return false; | ||
} | ||
} | ||
if (!isSingleValueTag) { | ||
return isWS === false; | ||
// return (isEQ || isWS) === false; | ||
} | ||
return true; | ||
}; | ||
var name = tagChars.grabWhile(validAttrValue); | ||
tagChars.skip(); | ||
emitToken(_token.TYPE_ATTR_VALUE, unq(name)); | ||
if (tagChars.isLast()) { | ||
return TAG_STATE_NAME; | ||
} | ||
return TAG_STATE_ATTR; | ||
} | ||
if (!isSingleValueTag) { | ||
return isWS === false; // return (isEQ || isWS) === false; | ||
var validName = function(char) { | ||
return !(char === _char.EQ || isWhiteSpace(char) || tagChars.isLast()); | ||
}; | ||
var name = tagChars.grabWhile(validName); | ||
emitToken(_token.TYPE_TAG, name); | ||
tagChars.skip(); | ||
// in cases when we has [url=someval]GET[/url] and we dont need to parse all | ||
if (isSingleValueTag) { | ||
return TAG_STATE_VALUE; | ||
} | ||
return true; | ||
}; | ||
var _name2 = tagChars.grabWhile(validAttrValue); | ||
tagChars.skip(); | ||
emitToken(_Token.TYPE_ATTR_VALUE, unq(_name2)); | ||
if (tagChars.isLast()) { | ||
return TAG_STATE_NAME; | ||
} | ||
return TAG_STATE_ATTR; | ||
} | ||
var validName = function validName(_char10) { | ||
return !(_char10 === _char14.EQ || isWhiteSpace(_char10) || tagChars.isLast()); | ||
var hasEQ = tagChars.includes(_char.EQ); | ||
return hasEQ ? TAG_STATE_ATTR : TAG_STATE_VALUE; | ||
}; | ||
var name = tagChars.grabWhile(validName); | ||
emitToken(_Token.TYPE_TAG, name); | ||
tagChars.skip(); // in cases when we has [url=someval]GET[/url] and we dont need to parse all | ||
if (isSingleValueTag) { | ||
return TAG_STATE_VALUE; | ||
} | ||
var hasEQ = tagChars.includes(_char14.EQ); | ||
return hasEQ ? TAG_STATE_ATTR : TAG_STATE_VALUE; | ||
} | ||
function stateTag() { | ||
var currChar = chars.getCurr(); | ||
if (currChar === openTag) { | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = chars.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || chars.isLast()) { | ||
emitToken(_Token.TYPE_WORD, currChar); | ||
var stateTag = function stateTag() { | ||
var currChar = chars.getCurr(); | ||
if (currChar === openTag) { | ||
var nextChar = chars.getNext(); | ||
chars.skip(); | ||
// detect case where we have '[My word [tag][/tag]' or we have '[My last line word' | ||
var substr = chars.substrUntilChar(closeTag); | ||
var hasInvalidChars = substr.length === 0 || substr.indexOf(openTag) >= 0; | ||
if (isCharReserved(nextChar) || hasInvalidChars || chars.isLast()) { | ||
emitToken(_token.TYPE_WORD, currChar); | ||
return STATE_WORD; | ||
} | ||
// [myTag ] | ||
var isNoAttrsInTag = substr.indexOf(_char.EQ) === -1; | ||
// [/myTag] | ||
var isClosingTag = substr[0] === _char.SLASH; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
var name = chars.grabWhile(function(char) { | ||
return char !== closeTag; | ||
}); | ||
chars.skip(); // skip closeTag | ||
emitToken(_token.TYPE_TAG, name); | ||
return STATE_WORD; | ||
} | ||
return STATE_TAG_ATTRS; | ||
} | ||
return STATE_WORD; | ||
} // [myTag ] | ||
var isNoAttrsInTag = substr.indexOf(_char14.EQ) === -1; // [/myTag] | ||
var isClosingTag = substr[0] === _char14.SLASH; | ||
if (isNoAttrsInTag || isClosingTag) { | ||
var name = chars.grabWhile(function (_char11) { | ||
return _char11 !== closeTag; | ||
}; | ||
var stateAttrs = function stateAttrs() { | ||
var silent = true; | ||
var tagStr = chars.grabWhile(function(char) { | ||
return char !== closeTag; | ||
}, silent); | ||
var tagGrabber = (0, _utils).createCharGrabber(tagStr, { | ||
onSkip: onSkip | ||
}); | ||
var hasSpace = tagGrabber.includes(_char.SPACE); | ||
tagMode = TAG_STATE_NAME; | ||
while(tagGrabber.hasNext()){ | ||
tagMode = nextTagState(tagGrabber, !hasSpace); | ||
} | ||
chars.skip(); // skip closeTag | ||
emitToken(_Token.TYPE_TAG, name); | ||
return STATE_WORD; | ||
} | ||
return STATE_TAG_ATTRS; | ||
} | ||
return STATE_WORD; | ||
} | ||
function stateAttrs() { | ||
var silent = true; | ||
var tagStr = chars.grabWhile(function (_char12) { | ||
return _char12 !== closeTag; | ||
}, silent); | ||
var tagGrabber = (0, _utils.createCharGrabber)(tagStr, { | ||
onSkip: onSkip | ||
}); | ||
var hasSpace = tagGrabber.includes(_char14.SPACE); | ||
tagMode = TAG_STATE_NAME; | ||
while (tagGrabber.hasNext()) { | ||
tagMode = nextTagState(tagGrabber, !hasSpace); | ||
} | ||
chars.skip(); // skip closeTag | ||
return STATE_WORD; | ||
} | ||
function stateWord() { | ||
if (isNewLine(chars.getCurr())) { | ||
emitToken(_Token.TYPE_NEW_LINE, chars.getCurr()); | ||
chars.skip(); | ||
col = 0; | ||
row++; | ||
return STATE_WORD; | ||
} | ||
if (isWhiteSpace(chars.getCurr())) { | ||
emitToken(_Token.TYPE_SPACE, chars.grabWhile(isWhiteSpace)); | ||
return STATE_WORD; | ||
} | ||
if (chars.getCurr() === openTag) { | ||
if (chars.includes(closeTag)) { | ||
return STATE_TAG; | ||
} | ||
emitToken(_Token.TYPE_WORD, chars.getCurr()); | ||
chars.skip(); | ||
return STATE_WORD; | ||
} | ||
if (escapeTags) { | ||
if (isEscapeChar(chars.getCurr())) { | ||
var currChar = chars.getCurr(); | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // skip the \ without emitting anything | ||
if (isEscapableChar(nextChar)) { | ||
chars.skip(); // skip past the [, ] or \ as well | ||
emitToken(_Token.TYPE_WORD, nextChar); | ||
return STATE_WORD; | ||
}; | ||
var stateWord = function stateWord() { | ||
if (isNewLine(chars.getCurr())) { | ||
emitToken(_token.TYPE_NEW_LINE, chars.getCurr()); | ||
chars.skip(); | ||
col = 0; | ||
row++; | ||
return STATE_WORD; | ||
} | ||
emitToken(_Token.TYPE_WORD, currChar); | ||
if (isWhiteSpace(chars.getCurr())) { | ||
emitToken(_token.TYPE_SPACE, chars.grabWhile(isWhiteSpace)); | ||
return STATE_WORD; | ||
} | ||
if (chars.getCurr() === openTag) { | ||
if (chars.includes(closeTag)) { | ||
return STATE_TAG; | ||
} | ||
emitToken(_token.TYPE_WORD, chars.getCurr()); | ||
chars.skip(); | ||
return STATE_WORD; | ||
} | ||
if (escapeTags) { | ||
if (isEscapeChar(chars.getCurr())) { | ||
var currChar = chars.getCurr(); | ||
var nextChar = chars.getNext(); | ||
chars.skip(); // skip the \ without emitting anything | ||
if (isEscapableChar(nextChar)) { | ||
chars.skip(); // skip past the [, ] or \ as well | ||
emitToken(_token.TYPE_WORD, nextChar); | ||
return STATE_WORD; | ||
} | ||
emitToken(_token.TYPE_WORD, currChar); | ||
return STATE_WORD; | ||
} | ||
var isChar = function(char) { | ||
return isCharToken(char) && !isEscapeChar(char); | ||
}; | ||
emitToken(_token.TYPE_WORD, chars.grabWhile(isChar)); | ||
return STATE_WORD; | ||
} | ||
emitToken(_token.TYPE_WORD, chars.grabWhile(isCharToken)); | ||
return STATE_WORD; | ||
} | ||
var isChar = function isChar(_char13) { | ||
return isCharToken(_char13) && !isEscapeChar(_char13); | ||
}; | ||
emitToken(_Token.TYPE_WORD, chars.grabWhile(isChar)); | ||
return STATE_WORD; | ||
} | ||
emitToken(_Token.TYPE_WORD, chars.grabWhile(isCharToken)); | ||
return STATE_WORD; | ||
} | ||
function tokenize() { | ||
stateMode = STATE_WORD; | ||
while (chars.hasNext()) { | ||
switch (stateMode) { | ||
case STATE_TAG: | ||
stateMode = stateTag(); | ||
break; | ||
case STATE_TAG_ATTRS: | ||
stateMode = stateAttrs(); | ||
break; | ||
case STATE_WORD: | ||
stateMode = stateWord(); | ||
break; | ||
default: | ||
stateMode = STATE_WORD; | ||
break; | ||
} | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
} | ||
function isTokenNested(token) { | ||
var value = openTag + _char14.SLASH + token.getValue(); // potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
} | ||
return { | ||
tokenize: tokenize, | ||
isTokenNested: isTokenNested | ||
}; | ||
}; | ||
var tokenize = function tokenize() { | ||
stateMode = STATE_WORD; | ||
while(chars.hasNext()){ | ||
switch(stateMode){ | ||
case STATE_TAG: | ||
stateMode = stateTag(); | ||
break; | ||
case STATE_TAG_ATTRS: | ||
stateMode = stateAttrs(); | ||
break; | ||
case STATE_WORD: | ||
stateMode = stateWord(); | ||
break; | ||
default: | ||
stateMode = STATE_WORD; | ||
break; | ||
} | ||
} | ||
tokens.length = tokenIndex + 1; | ||
return tokens; | ||
}; | ||
var isTokenNested = function isTokenNested(token) { | ||
var value = openTag + _char.SLASH + token.getValue(); | ||
// potential bottleneck | ||
return buffer.indexOf(value) > -1; | ||
}; | ||
var STATE_WORD = 0; | ||
var STATE_TAG = 1; | ||
var STATE_TAG_ATTRS = 2; | ||
var TAG_STATE_NAME = 0; | ||
var TAG_STATE_ATTR = 1; | ||
var TAG_STATE_VALUE = 2; | ||
var row = 0; | ||
var col = 0; | ||
var tokenIndex = -1; | ||
var stateMode = STATE_WORD; | ||
var tagMode = TAG_STATE_NAME; | ||
var tokens = new Array(Math.floor(buffer.length)); | ||
var openTag = options.openTag || _char.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char.CLOSE_BRAKET; | ||
var escapeTags = !!options.enableEscapeTags; | ||
var onToken = options.onToken || function() { | ||
}; | ||
var RESERVED_CHARS = [ | ||
closeTag, | ||
openTag, | ||
_char.QUOTEMARK, | ||
_char.BACKSLASH, | ||
_char.SPACE, | ||
_char.TAB, | ||
_char.EQ, | ||
_char.N, | ||
EM | ||
]; | ||
var NOT_CHAR_TOKENS = [ | ||
// ...(options.enableEscapeTags ? [BACKSLASH] : []), | ||
openTag, | ||
_char.SPACE, | ||
_char.TAB, | ||
_char.N, | ||
]; | ||
var WHITESPACES = [ | ||
_char.SPACE, | ||
_char.TAB | ||
]; | ||
var SPECIAL_CHARS = [ | ||
_char.EQ, | ||
_char.SPACE, | ||
_char.TAB | ||
]; | ||
var isCharReserved = function(char) { | ||
return RESERVED_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isNewLine = function(char) { | ||
return char === _char.N; | ||
}; | ||
var isWhiteSpace = function(char) { | ||
return WHITESPACES.indexOf(char) >= 0; | ||
}; | ||
var isCharToken = function(char) { | ||
return NOT_CHAR_TOKENS.indexOf(char) === -1; | ||
}; | ||
var isSpecialChar = function(char) { | ||
return SPECIAL_CHARS.indexOf(char) >= 0; | ||
}; | ||
var isEscapableChar = function(char) { | ||
return char === openTag || char === closeTag || char === _char.BACKSLASH; | ||
}; | ||
var isEscapeChar = function(char) { | ||
return char === _char.BACKSLASH; | ||
}; | ||
var onSkip = function() { | ||
col++; | ||
}; | ||
var unq = function(val) { | ||
return (0, _utils).unquote((0, _utils).trimChar(val, _char.QUOTEMARK)); | ||
}; | ||
var chars = (0, _utils).createCharGrabber(buffer, { | ||
onSkip: onSkip | ||
}); | ||
return { | ||
tokenize: tokenize, | ||
isTokenNested: isTokenNested | ||
}; | ||
} | ||
var createTokenOfType = createToken; | ||
exports.createTokenOfType = createTokenOfType; | ||
exports.createTokenOfType = createTokenOfType; | ||
exports.createLexer = createLexer; |
426
lib/parse.js
"use strict"; | ||
exports.__esModule = true; | ||
exports["default"] = exports.parse = void 0; | ||
var _TagNode = _interopRequireDefault(require("@bbob/plugin-helper/lib/TagNode")); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = exports.parse = void 0; | ||
var _tagNode = _interopRequireDefault(require("@bbob/plugin-helper/lib/TagNode")); | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
var _index = require("@bbob/plugin-helper/lib/index"); | ||
var _lexer = require("./lexer"); | ||
var _utils = require("./utils"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
} | ||
/** | ||
@@ -28,278 +26,216 @@ * @public | ||
* @return {Array} | ||
*/ | ||
var parse = function parse(input, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
var options = opts; | ||
var openTag = options.openTag || _char.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char.CLOSE_BRAKET; | ||
var tokenizer = null; | ||
/** | ||
*/ var parse = function(input, param) { | ||
var opts = param === void 0 ? { | ||
} : param; | ||
var options = opts; | ||
var openTag = options.openTag || _char.OPEN_BRAKET; | ||
var closeTag = options.closeTag || _char.CLOSE_BRAKET; | ||
var tokenizer = null; | ||
/** | ||
* Result AST of nodes | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var nodes = (0, _utils.createList)(); | ||
/** | ||
*/ var nodes = (0, _utils).createList(); | ||
/** | ||
* Temp buffer of nodes that's nested to another node | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var nestedNodes = (0, _utils.createList)(); | ||
/** | ||
*/ var nestedNodes = (0, _utils).createList(); | ||
/** | ||
* Temp buffer of nodes [tag..]...[/tag] | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var tagNodes = (0, _utils.createList)(); | ||
/** | ||
*/ var tagNodes = (0, _utils).createList(); | ||
/** | ||
* Temp buffer of tag attributes | ||
* @private | ||
* @type {NodeList} | ||
*/ | ||
var tagNodesAttrName = (0, _utils.createList)(); | ||
/** | ||
*/ var tagNodesAttrName = (0, _utils).createList(); | ||
/** | ||
* Cache for nested tags checks | ||
*/ | ||
var nestedTagsMap = new Set(); | ||
/** | ||
*/ var nestedTagsMap = new Set(); | ||
/** | ||
* | ||
* @param token | ||
* @returns {boolean} | ||
*/ | ||
var isTokenNested = function isTokenNested(token) { | ||
var value = token.getValue(); | ||
if (!nestedTagsMap.has(value) && tokenizer.isTokenNested && tokenizer.isTokenNested(token)) { | ||
nestedTagsMap.add(value); | ||
return true; | ||
} | ||
return nestedTagsMap.has(value); | ||
}; | ||
/** | ||
*/ var isTokenNested = function(token) { | ||
var value = token.getValue(); | ||
if (!nestedTagsMap.has(value) && tokenizer.isTokenNested && tokenizer.isTokenNested(token)) { | ||
nestedTagsMap.add(value); | ||
return true; | ||
} | ||
return nestedTagsMap.has(value); | ||
}; | ||
/** | ||
* @param tagName | ||
* @returns {boolean} | ||
*/ | ||
var isTagNested = function isTagNested(tagName) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
}; | ||
/** | ||
*/ var isTagNested = function(tagName) { | ||
return Boolean(nestedTagsMap.has(tagName)); | ||
}; | ||
/** | ||
* @private | ||
* @param {String} value | ||
* @return {boolean} | ||
*/ | ||
var isAllowedTag = function isAllowedTag(value) { | ||
if (options.onlyAllowTags && options.onlyAllowTags.length) { | ||
return options.onlyAllowTags.indexOf(value) >= 0; | ||
} | ||
return true; | ||
}; | ||
/** | ||
*/ var isAllowedTag = function(value) { | ||
if (options.onlyAllowTags && options.onlyAllowTags.length) { | ||
return options.onlyAllowTags.indexOf(value) >= 0; | ||
} | ||
return true; | ||
}; | ||
/** | ||
* Flushes temp tag nodes and its attributes buffers | ||
* @private | ||
* @return {Array} | ||
*/ | ||
var flushTagNodes = function flushTagNodes() { | ||
if (tagNodes.flushLast()) { | ||
tagNodesAttrName.flushLast(); | ||
} | ||
}; | ||
/** | ||
*/ var flushTagNodes = function() { | ||
if (tagNodes.flushLast()) { | ||
tagNodesAttrName.flushLast(); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @return {Array} | ||
*/ | ||
var getNodes = function getNodes() { | ||
var lastNestedNode = nestedNodes.getLast(); | ||
if (lastNestedNode && Array.isArray(lastNestedNode.content)) { | ||
return lastNestedNode.content; | ||
} | ||
return nodes.toArray(); | ||
}; | ||
/** | ||
*/ var getNodes = function() { | ||
var lastNestedNode = nestedNodes.getLast(); | ||
if (lastNestedNode && Array.isArray(lastNestedNode.content)) { | ||
return lastNestedNode.content; | ||
} | ||
return nodes.toArray(); | ||
}; | ||
/** | ||
* @private | ||
* @param {string|TagNode} node | ||
*/ | ||
var appendNodes = function appendNodes(node) { | ||
var items = getNodes(); | ||
if (Array.isArray(items)) { | ||
if ((0, _index.isTagNode)(node)) { | ||
if (isAllowedTag(node.tag)) { | ||
items.push(node.toTagNode()); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach(function (item) { | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
*/ var appendNodes = function(node) { | ||
var items = getNodes(); | ||
if (Array.isArray(items)) { | ||
if ((0, _index).isTagNode(node)) { | ||
if (isAllowedTag(node.tag)) { | ||
items.push(node.toTagNode()); | ||
} else { | ||
items.push(node.toTagStart({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
if (node.content.length) { | ||
node.content.forEach(function(item) { | ||
items.push(item); | ||
}); | ||
items.push(node.toTagEnd({ | ||
openTag: openTag, | ||
closeTag: closeTag | ||
})); | ||
} | ||
} | ||
} else { | ||
items.push(node); | ||
} | ||
} | ||
} else { | ||
items.push(node); | ||
} | ||
} | ||
}; | ||
/** | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTagStart = function handleTagStart(token) { | ||
flushTagNodes(); | ||
var tagNode = _TagNode["default"].create(token.getValue()); | ||
var isNested = isTokenNested(token); | ||
tagNodes.push(tagNode); | ||
if (isNested) { | ||
nestedNodes.push(tagNode); | ||
} else { | ||
appendNodes(tagNode, token); | ||
} | ||
}; | ||
/** | ||
*/ var handleTagStart = function(token) { | ||
flushTagNodes(); | ||
var tagNode = _tagNode.default.create(token.getValue()); | ||
var isNested = isTokenNested(token); | ||
tagNodes.push(tagNode); | ||
if (isNested) { | ||
nestedNodes.push(tagNode); | ||
} else { | ||
appendNodes(tagNode, token); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTagEnd = function handleTagEnd(token) { | ||
flushTagNodes(); | ||
var lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode) { | ||
appendNodes(lastNestedNode, token); | ||
} else if (typeof options.onError === 'function') { | ||
var tag = token.getValue(); | ||
var line = token.getLine(); | ||
var column = token.getColumn(); | ||
options.onError({ | ||
message: "Inconsistent tag '" + tag + "' on line " + line + " and column " + column, | ||
tagName: tag, | ||
lineNumber: line, | ||
columnNumber: column | ||
}); | ||
} | ||
}; | ||
/** | ||
*/ var handleTagEnd = function(token) { | ||
flushTagNodes(); | ||
var lastNestedNode = nestedNodes.flushLast(); | ||
if (lastNestedNode) { | ||
appendNodes(lastNestedNode, token); | ||
} else if (typeof options.onError === 'function') { | ||
var tag = token.getValue(); | ||
var line = token.getLine(); | ||
var column = token.getColumn(); | ||
options.onError({ | ||
message: "Inconsistent tag '".concat(tag, "' on line ").concat(line, " and column ").concat(column), | ||
tagName: tag, | ||
lineNumber: line, | ||
columnNumber: column | ||
}); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleTag = function handleTag(token) { | ||
// [tag] | ||
if (token.isStart()) { | ||
handleTagStart(token); | ||
} // [/tag] | ||
if (token.isEnd()) { | ||
handleTagEnd(token); | ||
} | ||
}; | ||
/** | ||
*/ var handleTag = function(token) { | ||
// [tag] | ||
if (token.isStart()) { | ||
handleTagStart(token); | ||
} | ||
// [/tag] | ||
if (token.isEnd()) { | ||
handleTagEnd(token); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var handleNode = function handleNode(token) { | ||
/** | ||
*/ var handleNode = function(token) { | ||
/** | ||
* @type {TagNode} | ||
*/ | ||
var lastTagNode = tagNodes.getLast(); | ||
var tokenValue = token.getValue(); | ||
var isNested = isTagNested(token); | ||
if (lastTagNode) { | ||
if (token.isAttrName()) { | ||
tagNodesAttrName.push(tokenValue); | ||
lastTagNode.attr(tagNodesAttrName.getLast(), ''); | ||
} else if (token.isAttrValue()) { | ||
var attrName = tagNodesAttrName.getLast(); | ||
if (attrName) { | ||
lastTagNode.attr(attrName, tokenValue); | ||
tagNodesAttrName.flushLast(); | ||
} else { | ||
lastTagNode.attr(tokenValue, tokenValue); | ||
*/ var lastTagNode = tagNodes.getLast(); | ||
var tokenValue = token.getValue(); | ||
var isNested = isTagNested(token); | ||
if (lastTagNode) { | ||
if (token.isAttrName()) { | ||
tagNodesAttrName.push(tokenValue); | ||
lastTagNode.attr(tagNodesAttrName.getLast(), ''); | ||
} else if (token.isAttrValue()) { | ||
var attrName = tagNodesAttrName.getLast(); | ||
if (attrName) { | ||
lastTagNode.attr(attrName, tokenValue); | ||
tagNodesAttrName.flushLast(); | ||
} else { | ||
lastTagNode.attr(tokenValue, tokenValue); | ||
} | ||
} else if (token.isText()) { | ||
if (isNested) { | ||
lastTagNode.append(tokenValue); | ||
} else { | ||
appendNodes(tokenValue); | ||
} | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
} else if (token.isText()) { | ||
appendNodes(tokenValue); | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
} else if (token.isText()) { | ||
if (isNested) { | ||
lastTagNode.append(tokenValue); | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ var onToken = function(token) { | ||
if (token.isTag()) { | ||
handleTag(token); | ||
} else { | ||
appendNodes(tokenValue); | ||
handleNode(token); | ||
} | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
} else if (token.isText()) { | ||
appendNodes(tokenValue); | ||
} else if (token.isTag()) { | ||
// if tag is not allowed, just past it as is | ||
appendNodes(token.toString()); | ||
} | ||
}; | ||
/** | ||
* @private | ||
* @param {Token} token | ||
*/ | ||
var onToken = function onToken(token) { | ||
if (token.isTag()) { | ||
handleTag(token); | ||
} else { | ||
handleNode(token); | ||
} | ||
}; | ||
tokenizer = (opts.createTokenizer ? opts.createTokenizer : _lexer.createLexer)(input, { | ||
onToken: onToken, | ||
onlyAllowTags: options.onlyAllowTags, | ||
openTag: openTag, | ||
closeTag: closeTag, | ||
enableEscapeTags: options.enableEscapeTags | ||
}); // eslint-disable-next-line no-unused-vars | ||
var tokens = tokenizer.tokenize(); | ||
return nodes.toArray(); | ||
}; | ||
tokenizer = (opts.createTokenizer ? opts.createTokenizer : _lexer.createLexer)(input, { | ||
onToken: onToken, | ||
onlyAllowTags: options.onlyAllowTags, | ||
openTag: openTag, | ||
closeTag: closeTag, | ||
enableEscapeTags: options.enableEscapeTags | ||
}); | ||
// eslint-disable-next-line no-unused-vars | ||
var tokens = tokenizer.tokenize(); | ||
return nodes.toArray(); | ||
}; | ||
exports.parse = parse; | ||
var _default = parse; | ||
exports["default"] = _default; | ||
exports.default = _default; |
304
lib/Token.js
"use strict"; | ||
exports.__esModule = true; | ||
exports["default"] = exports.Token = exports.TYPE_NEW_LINE = exports.TYPE_SPACE = exports.TYPE_ATTR_VALUE = exports.TYPE_ATTR_NAME = exports.TYPE_TAG = exports.TYPE_WORD = exports.COLUMN_ID = exports.LINE_ID = exports.VALUE_ID = exports.TYPE_ID = void 0; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = exports.Token = exports.TYPE_NEW_LINE = exports.TYPE_SPACE = exports.TYPE_ATTR_VALUE = exports.TYPE_ATTR_NAME = exports.TYPE_TAG = exports.TYPE_WORD = exports.COLUMN_ID = exports.LINE_ID = exports.VALUE_ID = exports.TYPE_ID = void 0; | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a 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); | ||
} | ||
} | ||
function _createClass(Constructor, protoProps, staticProps) { | ||
if (protoProps) _defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) _defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
} | ||
// 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 TOKEN_TYPE_WORD = 1; // 'word'; | ||
var TOKEN_TYPE_TAG = 2; // 'tag'; | ||
var TOKEN_TYPE_ATTR_NAME = 3; // 'attr-name'; | ||
var TOKEN_TYPE_ATTR_VALUE = 4; // 'attr-value'; | ||
var TOKEN_TYPE_SPACE = 5; // 'space'; | ||
var TOKEN_TYPE_NEW_LINE = 6; // 'new-line'; | ||
/** | ||
* @param {Token} token | ||
* @returns {string} | ||
*/ | ||
var getTokenValue = function getTokenValue(token) { | ||
if (token && typeof token[TOKEN_VALUE_ID] !== 'undefined') { | ||
return token[TOKEN_VALUE_ID]; | ||
} | ||
return ''; | ||
*/ var getTokenValue = function(token) { | ||
if (token && typeof token[TOKEN_VALUE_ID] !== 'undefined') { | ||
return token[TOKEN_VALUE_ID]; | ||
} | ||
return ''; | ||
}; | ||
@@ -44,11 +49,7 @@ /** | ||
* @returns {number} | ||
*/ | ||
var getTokenLine = function getTokenLine(token) { | ||
return token && token[TOKEN_LINE_ID] || 0; | ||
*/ var getTokenLine = function(token) { | ||
return token && token[TOKEN_LINE_ID] || 0; | ||
}; | ||
var getTokenColumn = function getTokenColumn(token) { | ||
return token && token[TOKEN_COLUMN_ID] || 0; | ||
var getTokenColumn = function(token) { | ||
return token && token[TOKEN_COLUMN_ID] || 0; | ||
}; | ||
@@ -58,11 +59,7 @@ /** | ||
* @returns {boolean} | ||
*/ | ||
var isTextToken = function isTextToken(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD; | ||
} | ||
return false; | ||
*/ var isTextToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_SPACE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_NEW_LINE || token[TOKEN_TYPE_ID] === TOKEN_TYPE_WORD; | ||
} | ||
return false; | ||
}; | ||
@@ -72,27 +69,19 @@ /** | ||
* @returns {boolean} | ||
*/ | ||
var isTagToken = function isTagToken(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG; | ||
} | ||
return false; | ||
*/ var isTagToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_TAG; | ||
} | ||
return false; | ||
}; | ||
var isTagEnd = function isTagEnd(token) { | ||
return getTokenValue(token).charCodeAt(0) === _char.SLASH.charCodeAt(0); | ||
var isTagEnd = function(token) { | ||
return getTokenValue(token).charCodeAt(0) === _char.SLASH.charCodeAt(0); | ||
}; | ||
var isTagStart = function isTagStart(token) { | ||
return !isTagEnd(token); | ||
var isTagStart = function(token) { | ||
return !isTagEnd(token); | ||
}; | ||
var isAttrNameToken = function isAttrNameToken(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME; | ||
} | ||
return false; | ||
var isAttrNameToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_NAME; | ||
} | ||
return false; | ||
}; | ||
@@ -102,96 +91,104 @@ /** | ||
* @returns {boolean} | ||
*/ | ||
var isAttrValueToken = function isAttrValueToken(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE; | ||
} | ||
return false; | ||
*/ var isAttrValueToken = function(token) { | ||
if (token && typeof token[TOKEN_TYPE_ID] !== 'undefined') { | ||
return token[TOKEN_TYPE_ID] === TOKEN_TYPE_ATTR_VALUE; | ||
} | ||
return false; | ||
}; | ||
var getTagName = function getTagName(token) { | ||
var value = getTokenValue(token); | ||
return isTagEnd(token) ? value.slice(1) : value; | ||
var getTagName = function(token) { | ||
var value = getTokenValue(token); | ||
return isTagEnd(token) ? value.slice(1) : value; | ||
}; | ||
var convertTagToText = function convertTagToText(token) { | ||
var text = _char.OPEN_BRAKET; | ||
text += getTokenValue(token); | ||
text += _char.CLOSE_BRAKET; | ||
return text; | ||
var convertTagToText = function(token) { | ||
var text = _char.OPEN_BRAKET; | ||
text += getTokenValue(token); | ||
text += _char.CLOSE_BRAKET; | ||
return text; | ||
}; | ||
var Token = | ||
/*#__PURE__*/ | ||
function () { | ||
/** | ||
* @param {String} type | ||
* @param {String} value | ||
* @param line | ||
* @param row | ||
*/ | ||
function Token(type, value, line, row) { | ||
this[TOKEN_TYPE_ID] = Number(type); | ||
this[TOKEN_VALUE_ID] = String(value); | ||
this[TOKEN_LINE_ID] = Number(line); | ||
this[TOKEN_COLUMN_ID] = Number(row); | ||
} | ||
var _proto = Token.prototype; | ||
_proto.isEmpty = function isEmpty() { | ||
// eslint-disable-next-line no-restricted-globals | ||
return isNaN(this[TOKEN_TYPE_ID]); | ||
}; | ||
_proto.isText = function isText() { | ||
return isTextToken(this); | ||
}; | ||
_proto.isTag = function isTag() { | ||
return isTagToken(this); | ||
}; | ||
_proto.isAttrName = function isAttrName() { | ||
return isAttrNameToken(this); | ||
}; | ||
_proto.isAttrValue = function isAttrValue() { | ||
return isAttrValueToken(this); | ||
}; | ||
_proto.isStart = function isStart() { | ||
return isTagStart(this); | ||
}; | ||
_proto.isEnd = function isEnd() { | ||
return isTagEnd(this); | ||
}; | ||
_proto.getName = function getName() { | ||
return getTagName(this); | ||
}; | ||
_proto.getValue = function getValue() { | ||
return getTokenValue(this); | ||
}; | ||
_proto.getLine = function getLine() { | ||
return getTokenLine(this); | ||
}; | ||
_proto.getColumn = function getColumn() { | ||
return getTokenColumn(this); | ||
}; | ||
_proto.toString = function toString() { | ||
return convertTagToText(this); | ||
}; | ||
return Token; | ||
var Token = /*#__PURE__*/ function() { | ||
"use strict"; | ||
function Token(type, value, line, row) { | ||
_classCallCheck(this, Token); | ||
this[TOKEN_TYPE_ID] = Number(type); | ||
this[TOKEN_VALUE_ID] = String(value); | ||
this[TOKEN_LINE_ID] = Number(line); | ||
this[TOKEN_COLUMN_ID] = Number(row); | ||
} | ||
_createClass(Token, [ | ||
{ | ||
key: "isEmpty", | ||
value: function isEmpty() { | ||
// eslint-disable-next-line no-restricted-globals | ||
return isNaN(this[TOKEN_TYPE_ID]); | ||
} | ||
}, | ||
{ | ||
key: "isText", | ||
value: function isText() { | ||
return isTextToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isTag", | ||
value: function isTag() { | ||
return isTagToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isAttrName", | ||
value: function isAttrName() { | ||
return isAttrNameToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isAttrValue", | ||
value: function isAttrValue() { | ||
return isAttrValueToken(this); | ||
} | ||
}, | ||
{ | ||
key: "isStart", | ||
value: function isStart() { | ||
return isTagStart(this); | ||
} | ||
}, | ||
{ | ||
key: "isEnd", | ||
value: function isEnd() { | ||
return isTagEnd(this); | ||
} | ||
}, | ||
{ | ||
key: "getName", | ||
value: function getName() { | ||
return getTagName(this); | ||
} | ||
}, | ||
{ | ||
key: "getValue", | ||
value: function getValue() { | ||
return getTokenValue(this); | ||
} | ||
}, | ||
{ | ||
key: "getLine", | ||
value: function getLine() { | ||
return getTokenLine(this); | ||
} | ||
}, | ||
{ | ||
key: "getColumn", | ||
value: function getColumn() { | ||
return getTokenColumn(this); | ||
} | ||
}, | ||
{ | ||
key: "toString", | ||
value: function toString() { | ||
return convertTagToText(this); | ||
} | ||
} | ||
]); | ||
return Token; | ||
}(); | ||
exports.Token = Token; | ||
var TYPE_ID = TOKEN_TYPE_ID; | ||
@@ -217,3 +214,4 @@ exports.TYPE_ID = TYPE_ID; | ||
exports.TYPE_NEW_LINE = TYPE_NEW_LINE; | ||
exports.Token = Token; | ||
var _default = Token; | ||
exports["default"] = _default; | ||
exports.default = _default; |
310
lib/utils.js
"use strict"; | ||
exports.__esModule = true; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createList = exports.unquote = exports.trimChar = exports.createCharGrabber = void 0; | ||
var _char2 = require("@bbob/plugin-helper/lib/char"); | ||
var _char = require("@bbob/plugin-helper/lib/char"); | ||
function CharGrabber(source, options) { | ||
var cursor = { | ||
pos: 0, | ||
len: source.length | ||
}; | ||
var substrUntilChar = function substrUntilChar(_char) { | ||
var pos = cursor.pos; | ||
var idx = source.indexOf(_char, pos); | ||
return idx >= 0 ? source.substr(pos, idx - pos) : ''; | ||
}; | ||
var includes = function includes(val) { | ||
return source.indexOf(val, cursor.pos) >= 0; | ||
}; | ||
var hasNext = function hasNext() { | ||
return cursor.len > cursor.pos; | ||
}; | ||
var isLast = function isLast() { | ||
return cursor.pos === cursor.len; | ||
}; | ||
var skip = function skip(num, silent) { | ||
if (num === void 0) { | ||
num = 1; | ||
} | ||
cursor.pos += num; | ||
if (options && options.onSkip && !silent) { | ||
options.onSkip(); | ||
} | ||
}; | ||
var rest = function rest() { | ||
return source.substr(cursor.pos); | ||
}; | ||
var curr = function curr() { | ||
return source[cursor.pos]; | ||
}; | ||
var prev = function prev() { | ||
var prevPos = cursor.pos - 1; | ||
return typeof source[prevPos] !== 'undefined' ? source[prevPos] : null; | ||
}; | ||
var next = function next() { | ||
var nextPos = cursor.pos + 1; | ||
return nextPos <= source.length - 1 ? source[nextPos] : null; | ||
}; | ||
var grabWhile = function grabWhile(cond, silent) { | ||
var start = 0; | ||
if (hasNext()) { | ||
start = cursor.pos; | ||
while (hasNext() && cond(curr())) { | ||
skip(1, silent); | ||
} | ||
} | ||
return source.substr(start, cursor.pos - start); | ||
}; | ||
/** | ||
var cursor = { | ||
pos: 0, | ||
len: source.length | ||
}; | ||
var substrUntilChar = function(char) { | ||
var pos = cursor.pos; | ||
var idx = source.indexOf(char, pos); | ||
return idx >= 0 ? source.substr(pos, idx - pos) : ''; | ||
}; | ||
var includes = function(val) { | ||
return source.indexOf(val, cursor.pos) >= 0; | ||
}; | ||
var hasNext = function() { | ||
return cursor.len > cursor.pos; | ||
}; | ||
var isLast = function() { | ||
return cursor.pos === cursor.len; | ||
}; | ||
var skip = function(param, silent) { | ||
var num = param === void 0 ? 1 : param; | ||
cursor.pos += num; | ||
if (options && options.onSkip && !silent) { | ||
options.onSkip(); | ||
} | ||
}; | ||
var rest = function() { | ||
return source.substr(cursor.pos); | ||
}; | ||
var curr = function() { | ||
return source[cursor.pos]; | ||
}; | ||
var prev = function() { | ||
var prevPos = cursor.pos - 1; | ||
return typeof source[prevPos] !== 'undefined' ? source[prevPos] : null; | ||
}; | ||
var next = function() { | ||
var nextPos = cursor.pos + 1; | ||
return nextPos <= source.length - 1 ? source[nextPos] : null; | ||
}; | ||
var grabWhile = function(cond, silent) { | ||
var start = 0; | ||
if (hasNext()) { | ||
start = cursor.pos; | ||
while(hasNext() && cond(curr())){ | ||
skip(1, silent); | ||
} | ||
} | ||
return source.substr(start, cursor.pos - start); | ||
}; | ||
/** | ||
* @type {skip} | ||
*/ | ||
this.skip = skip; | ||
/** | ||
*/ this.skip = skip; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.hasNext = hasNext; | ||
/** | ||
*/ this.hasNext = hasNext; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getCurr = curr; | ||
/** | ||
*/ this.getCurr = curr; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getRest = rest; | ||
/** | ||
*/ this.getRest = rest; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getNext = next; | ||
/** | ||
*/ this.getNext = next; | ||
/** | ||
* @returns {String} | ||
*/ | ||
this.getPrev = prev; | ||
/** | ||
*/ this.getPrev = prev; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.isLast = isLast; | ||
/** | ||
*/ this.isLast = isLast; | ||
/** | ||
* @returns {Boolean} | ||
*/ | ||
this.includes = includes; | ||
/** | ||
*/ this.includes = includes; | ||
/** | ||
* @param {Function} cond | ||
* @param {Boolean} silent | ||
* @return {String} | ||
*/ | ||
this.grabWhile = grabWhile; | ||
/** | ||
*/ this.grabWhile = grabWhile; | ||
/** | ||
* Grabs rest of string until it find a char | ||
* @param {String} char | ||
* @return {String} | ||
*/ | ||
this.substrUntilChar = substrUntilChar; | ||
*/ this.substrUntilChar = substrUntilChar; | ||
} | ||
/** | ||
* Creates a grabber wrapper for source string, that helps to iterate over string char by char | ||
* @param {String} source | ||
* @param {Object} options | ||
* @param {Function} options.onSkip | ||
* @return CharGrabber | ||
*/ | ||
var createCharGrabber = function createCharGrabber(source, options) { | ||
return new CharGrabber(source, options); | ||
var createCharGrabber = function(source, options) { | ||
return new CharGrabber(source, options); | ||
}; | ||
/** | ||
* Trims string from start and end by char | ||
* @example | ||
* trimChar('*hello*', '*') ==> 'hello' | ||
* @param {String} str | ||
* @param {String} charToRemove | ||
* @returns {String} | ||
*/ | ||
exports.createCharGrabber = createCharGrabber; | ||
var trimChar = function trimChar(str, charToRemove) { | ||
while (str.charAt(0) === charToRemove) { | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(1); | ||
} | ||
while (str.charAt(str.length - 1) === charToRemove) { | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
var trimChar = function(str, charToRemove) { | ||
while(str.charAt(0) === charToRemove){ | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(1); | ||
} | ||
while(str.charAt(str.length - 1) === charToRemove){ | ||
// eslint-disable-next-line no-param-reassign | ||
str = str.substring(0, str.length - 1); | ||
} | ||
return str; | ||
}; | ||
/** | ||
* Unquotes \" to " | ||
* @param str | ||
* @return {String} | ||
*/ | ||
exports.trimChar = trimChar; | ||
var unquote = function unquote(str) { | ||
return str.replace(_char2.BACKSLASH + _char2.QUOTEMARK, _char2.QUOTEMARK); | ||
var unquote = function(str) { | ||
return str.replace(_char.BACKSLASH + _char.QUOTEMARK, _char.QUOTEMARK); | ||
}; | ||
exports.unquote = unquote; | ||
function NodeList(values) { | ||
if (values === void 0) { | ||
values = []; | ||
} | ||
var nodes = values; | ||
var getLast = function getLast() { | ||
return Array.isArray(nodes) && nodes.length > 0 && typeof nodes[nodes.length - 1] !== 'undefined' ? nodes[nodes.length - 1] : null; | ||
}; | ||
var flushLast = function flushLast() { | ||
return nodes.length ? nodes.pop() : false; | ||
}; | ||
var push = function push(value) { | ||
return nodes.push(value); | ||
}; | ||
var toArray = function toArray() { | ||
return nodes; | ||
}; | ||
this.push = push; | ||
this.toArray = toArray; | ||
this.getLast = getLast; | ||
this.flushLast = flushLast; | ||
function NodeList(param) { | ||
var values = param === void 0 ? [] : param; | ||
var nodes = values; | ||
var getLast = function() { | ||
return Array.isArray(nodes) && nodes.length > 0 && typeof nodes[nodes.length - 1] !== 'undefined' ? nodes[nodes.length - 1] : null; | ||
}; | ||
var flushLast = function() { | ||
return nodes.length ? nodes.pop() : false; | ||
}; | ||
var push = function(value) { | ||
return nodes.push(value); | ||
}; | ||
var toArray = function() { | ||
return nodes; | ||
}; | ||
this.push = push; | ||
this.toArray = toArray; | ||
this.getLast = getLast; | ||
this.flushLast = flushLast; | ||
} | ||
/** | ||
* | ||
* @param values | ||
* @return {NodeList} | ||
*/ | ||
var createList = function createList(values) { | ||
if (values === void 0) { | ||
values = []; | ||
} | ||
return new NodeList(values); | ||
var createList = function(param) { | ||
var values = param === void 0 ? [] : param; | ||
return new NodeList(values); | ||
}; | ||
exports.createList = createList; | ||
exports.createList = createList; |
{ | ||
"name": "@bbob/parser", | ||
"version": "2.7.1", | ||
"version": "2.8.0", | ||
"description": "A BBCode to AST Parser part of @bbob", | ||
@@ -16,3 +16,3 @@ "keywords": [ | ||
"dependencies": { | ||
"@bbob/plugin-helper": "^2.7.1" | ||
"@bbob/plugin-helper": "^2.8.0" | ||
}, | ||
@@ -35,11 +35,11 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"build:commonjs": "../../node_modules/.bin/cross-env BABEL_ENV=commonjs NODE_ENV=production ../../node_modules/.bin/babel src --out-dir lib", | ||
"build:es": "../../node_modules/.bin/cross-env BABEL_ENV=es NODE_ENV=production ../../node_modules/.bin/babel src --out-dir es", | ||
"build:umd": "../../node_modules/.bin/cross-env BABEL_ENV=rollup NODE_ENV=production ../../node_modules/.bin/rollup --config ../../rollup.config.js", | ||
"build:commonjs": "../../scripts/pkg-task build-commonjs", | ||
"build:es": "../../scripts/pkg-task build-es", | ||
"build:umd": "../../scripts/pkg-task build-umd", | ||
"build": "npm run build:commonjs && npm run build:es && npm run build:umd", | ||
"test": "../../node_modules/.bin/jest", | ||
"cover": "../../node_modules/.bin/jest --coverage", | ||
"lint": "../../node_modules/.bin/eslint .", | ||
"size": "../../node_modules/.bin/cross-env NODE_ENV=production ../../node_modules/.bin/size-limit", | ||
"bundlesize": "../../node_modules/.bin/cross-env NODE_ENV=production ../../node_modules/.bin/bundlesize", | ||
"test": "../../scripts/pkg-task test", | ||
"cover": "../../scripts/pkg-task cover", | ||
"lint": "../../scripts/pkg-task lint", | ||
"size": "../../scripts/pkg-task size", | ||
"bundlesize": "../../scripts/pkg-task bundlesize", | ||
"prepublishOnly": "npm run build" | ||
@@ -67,3 +67,3 @@ }, | ||
], | ||
"gitHead": "90563965c0626834e359a9c81ade8ca872536891" | ||
"gitHead": "4fb4fffbc69ee34cb9d27812b23105e28645d45f" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
182101
26
4790
Updated@bbob/plugin-helper@^2.8.0