Skip to content

Commit bf04330

Browse files
authored
.html() send context to parse5 (#1627)
* .html() send context to parse5 Fixes #1083 * .html() test with script element Co-authored-by: 5saviahv <5saviahv@users.noreply.github.com>
1 parent 21de2c5 commit bf04330

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

lib/api/manipulation.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,13 +753,15 @@ exports.html = function (str) {
753753
return html(this[0].children, this.options);
754754
}
755755

756-
var opts = this.options;
756+
var opts = Object.apply({}, this.options); // keep main options
757757

758758
return domEach(this, function (_, el) {
759759
el.children.forEach(function (child) {
760760
child.next = child.prev = child.parent = null;
761761
});
762762

763+
opts.context = el;
764+
763765
var content = str.cheerio
764766
? str.clone().get()
765767
: parse('' + str, opts, false).children;

lib/parsers/parse5.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ var parse5 = require('parse5');
22
var htmlparser2Adapter = require('parse5-htmlparser2-tree-adapter');
33

44
exports.parse = function (content, options, isDocument) {
5-
var parse = isDocument ? parse5.parse : parse5.parseFragment;
6-
7-
return parse(content, {
5+
var opts = {
86
treeAdapter: htmlparser2Adapter,
97
sourceCodeLocationInfo: options.sourceCodeLocationInfo,
10-
});
8+
};
9+
10+
var context = options.context;
11+
12+
return isDocument
13+
? parse5.parse(content, opts)
14+
: parse5.parseFragment(context, content, opts);
1115
};
1216

1317
exports.render = function (dom) {

test/api/manipulation.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,19 @@ describe('$(...)', function () {
16111611
$remove.replaceWith($children);
16121612
expect($fruits.children()).toHaveLength(4);
16131613
});
1614+
1615+
it('(script value) : should add content as text', function () {
1616+
var $data = '<a><b>';
1617+
var $script = $('<script>').html($data);
1618+
1619+
expect($script).toHaveLength(1);
1620+
expect($script[0].type).toBe('script');
1621+
expect($script[0].name).toBe('script');
1622+
1623+
expect($script[0].children).toHaveLength(1);
1624+
expect($script[0].children[0].type).toBe('text');
1625+
expect($script[0].children[0].data).toBe($data);
1626+
});
16141627
});
16151628

16161629
describe('.toString', function () {

0 commit comments

Comments
 (0)