Skip to content

Commit

Permalink
fix(plugin-helper): avoid some malformed attributes in attrsToString (#…
Browse files Browse the repository at this point in the history
…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;
});
```
  • Loading branch information
arkhamvm authored and JiLiZART committed Jun 27, 2019
1 parent 5291543 commit 09ff9af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/bbob-plugin-helper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions packages/bbob-plugin-helper/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ describe('@bbob/plugin-helper', () => {
disabled: true
})).toBe(` tag="test" foo="bar" disabled`)
})

test('attrsToString undefined', () => {
expect(attrsToString(undefined)).toBe('')
})
});

0 comments on commit 09ff9af

Please sign in to comment.