mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-05 16:42:27 +03:00
09ff9af9a2
* attrsToString: To avoid some malformed attributes
Error:
```
TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at attrsToString
```
This errors appears if no `attrs` setted in custom tag:
```
const BBcodePresetTemp = BbobPresetHTML5.extend((tags: any) => {
tags.br = () => ({
tag: 'br',
// attrs: {}, // <-- Comment this line for error and add [br] to text
content: null,
});
return tags;
});
```
75 lines
1.6 KiB
JavaScript
75 lines
1.6 KiB
JavaScript
import { N } from './char';
|
|
|
|
const isTagNode = el => typeof el === 'object' && !!el.tag;
|
|
const isStringNode = el => typeof el === 'string';
|
|
const isEOL = el => el === N;
|
|
|
|
const getNodeLength = (node) => {
|
|
if (isTagNode(node)) {
|
|
return node.content.reduce((count, contentNode) => count + getNodeLength(contentNode), 0);
|
|
} else if (isStringNode(node)) {
|
|
return node.length;
|
|
}
|
|
|
|
return 0;
|
|
};
|
|
|
|
/**
|
|
* Appends value to Tag Node
|
|
* @param {TagNode} node
|
|
* @param value
|
|
*/
|
|
const appendToNode = (node, value) => {
|
|
node.content.push(value);
|
|
};
|
|
|
|
/**
|
|
* Replaces " to &qquot;
|
|
* @param {String} value
|
|
*/
|
|
const escapeQuote = value => value.replace(/"/g, '"');
|
|
|
|
/**
|
|
* Acept name and value and return valid html5 attribute string
|
|
* @param {String} name
|
|
* @param {String} value
|
|
* @return {string}
|
|
*/
|
|
const attrValue = (name, value) => {
|
|
const type = typeof value;
|
|
|
|
const types = {
|
|
boolean: () => (value ? `${name}` : ''),
|
|
number: () => `${name}="${value}"`,
|
|
string: () => `${name}="${escapeQuote(value)}"`,
|
|
object: () => `${name}="${escapeQuote(JSON.stringify(value))}"`,
|
|
};
|
|
|
|
return types[type] ? types[type]() : '';
|
|
};
|
|
|
|
/**
|
|
* Transforms attrs to html params string
|
|
* @param values
|
|
*/
|
|
const attrsToString = (values) => {
|
|
// To avoid some malformed attributes
|
|
if (typeof values === 'undefined') {
|
|
return '';
|
|
}
|
|
|
|
return Object.keys(values)
|
|
.reduce((arr, key) => [...arr, attrValue(key, values[key])], [''])
|
|
.join(' ');
|
|
};
|
|
|
|
export {
|
|
attrsToString,
|
|
attrValue,
|
|
appendToNode,
|
|
getNodeLength,
|
|
isTagNode,
|
|
isStringNode,
|
|
isEOL,
|
|
};
|