diff --git a/lib/static.js b/lib/static.js index 2c4853eded..7d38646c7f 100644 --- a/lib/static.js +++ b/lib/static.js @@ -138,7 +138,12 @@ exports.parseHTML = function(data, context, keepScripts) { parsed('script').remove(); } - return parsed.root()[0].children; + // The `children` array is used by Cheerio internally to group elements that + // share the same parents. When nodes created through `parseHTML` are + // inserted into previously-existing DOM structures, they will be removed + // from the `children` array. The results of `parseHTML` should remain + // constant across these operations, so a shallow copy should be returned. + return parsed.root()[0].children.slice(); }; /** diff --git a/test/api/utils.js b/test/api/utils.js index 0d43de45bb..2ece35b21e 100644 --- a/test/api/utils.js +++ b/test/api/utils.js @@ -157,6 +157,14 @@ describe('cheerio', function() { expect(cheerio.parseHTML('<#if>
This is a test.