fix: handling case where node.get(0) returns null

This commit is contained in:
Adam Pash 2016-09-15 12:16:01 -04:00
parent 2bf274114f
commit 6263e505d5
2 changed files with 16 additions and 0 deletions

View File

@ -1,6 +1,10 @@
import 'babel-polyfill'; import 'babel-polyfill';
export default function convertNodeTo($node, $, tag = 'p') { export default function convertNodeTo($node, $, tag = 'p') {
const node = $node.get(0);
if (!node) {
return $;
}
const { attribs } = $node.get(0); const { attribs } = $node.get(0);
const attribString = Reflect.ownKeys(attribs) const attribString = Reflect.ownKeys(attribs)
.map(key => `${key}=${attribs[key]}`) .map(key => `${key}=${attribs[key]}`)

View File

@ -25,5 +25,17 @@ describe('convertNodeTo(node, $)', () => {
assert.equal(result, after); assert.equal(result, after);
}); });
it('does nothing if node.get returns null', () => {
const html = '<span class="foo" score="100">Should keep its attrs</span>';
const $ = cheerio.load(html);
const node = {
get: () => null,
};
const result = convertNodeTo(node, $, 'div').html();
assert.equal(result, html);
});
}); });