mirror of
https://github.com/postlight/mercury-parser
synced 2024-11-17 03:25:31 +00:00
fix: handling case where node.get(0) returns null
This commit is contained in:
parent
2bf274114f
commit
6263e505d5
@ -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]}`)
|
||||||
|
@ -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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user