2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-05-24 14:04:06 +03:00

fix(plugin-helper): avoid some malformed attributes in attrsToString (#26)

* 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;
});
```
This commit is contained in:
Vladimir
2019-06-27 16:49:28 +10:00
committed by Nikolay Kostyurin
parent 5291543855
commit 09ff9af9a2
2 changed files with 12 additions and 2 deletions
+8 -2
View File
@@ -52,10 +52,16 @@ const attrValue = (name, value) => {
* Transforms attrs to html params string
* @param values
*/
const attrsToString = values =>
Object.keys(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,
@@ -79,4 +79,8 @@ describe('@bbob/plugin-helper', () => {
disabled: true
})).toBe(` tag="test" foo="bar" disabled`)
})
test('attrsToString undefined', () => {
expect(attrsToString(undefined)).toBe('')
})
});