From 09ff9af9a244d194ef05da07c7356f570835fe72 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 27 Jun 2019 16:49:28 +1000 Subject: [PATCH] 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 () 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; }); ``` --- packages/bbob-plugin-helper/src/index.js | 10 ++++++++-- packages/bbob-plugin-helper/test/index.test.js | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/bbob-plugin-helper/src/index.js b/packages/bbob-plugin-helper/src/index.js index 5da62b88..d9db9aaf 100644 --- a/packages/bbob-plugin-helper/src/index.js +++ b/packages/bbob-plugin-helper/src/index.js @@ -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, diff --git a/packages/bbob-plugin-helper/test/index.test.js b/packages/bbob-plugin-helper/test/index.test.js index 4f4fde16..59f2dfdc 100644 --- a/packages/bbob-plugin-helper/test/index.test.js +++ b/packages/bbob-plugin-helper/test/index.test.js @@ -79,4 +79,8 @@ describe('@bbob/plugin-helper', () => { disabled: true })).toBe(` tag="test" foo="bar" disabled`) }) + + test('attrsToString undefined', () => { + expect(attrsToString(undefined)).toBe('') + }) });