From 6263e505d523746aa0ce8c80f8d97730962c4691 Mon Sep 17 00:00:00 2001 From: Adam Pash Date: Thu, 15 Sep 2016 12:16:01 -0400 Subject: [PATCH] fix: handling case where node.get(0) returns null --- src/utils/dom/convert-node-to.js | 4 ++++ src/utils/dom/convert-node-to.test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/utils/dom/convert-node-to.js b/src/utils/dom/convert-node-to.js index 167e8084..9a5eb8ef 100644 --- a/src/utils/dom/convert-node-to.js +++ b/src/utils/dom/convert-node-to.js @@ -1,6 +1,10 @@ import 'babel-polyfill'; export default function convertNodeTo($node, $, tag = 'p') { + const node = $node.get(0); + if (!node) { + return $; + } const { attribs } = $node.get(0); const attribString = Reflect.ownKeys(attribs) .map(key => `${key}=${attribs[key]}`) diff --git a/src/utils/dom/convert-node-to.test.js b/src/utils/dom/convert-node-to.test.js index 4fe473f0..12848a4a 100644 --- a/src/utils/dom/convert-node-to.test.js +++ b/src/utils/dom/convert-node-to.test.js @@ -25,5 +25,17 @@ describe('convertNodeTo(node, $)', () => { assert.equal(result, after); }); + + it('does nothing if node.get returns null', () => { + const html = 'Should keep its attrs'; + const $ = cheerio.load(html); + const node = { + get: () => null, + }; + + const result = convertNodeTo(node, $, 'div').html(); + + assert.equal(result, html); + }); });