Skip to content

Commit

Permalink
Merge pull request #854 from danielrentz/task/853-Range-cloneContents
Browse files Browse the repository at this point in the history
#853@minor: Fix method Range.cloneContents
  • Loading branch information
capricorn86 committed Apr 13, 2023
2 parents 9574b58 + cfa7c0b commit e697ebc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions packages/happy-dom/src/range/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ export default class Range {
const length = NodeUtility.getNodeLength(this._start.node);
if (this._start.offset > length) {
this._start.offset = length;
} else if (length === 0) {
this._start.offset = 0;
}
}

Expand All @@ -98,8 +96,6 @@ export default class Range {
const length = NodeUtility.getNodeLength(this._end.node);
if (this._end.offset > length) {
this._end.offset = length;
} else if (length === 0) {
this._end.offset = 0;
}
}

Expand Down Expand Up @@ -343,8 +339,8 @@ export default class Range {
fragment.appendChild(clone);

const subRange = new Range();
subRange._start.node = this._end.node;
subRange._start.offset = endOffset;
subRange._start.node = this._start.node;
subRange._start.offset = startOffset;
subRange._end.node = firstPartialContainedChild;
subRange._end.offset = NodeUtility.getNodeLength(firstPartialContainedChild);

Expand Down
16 changes: 16 additions & 0 deletions packages/happy-dom/test/range/Range.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ describe('Range', () => {

expect(document.body.innerHTML).toBe('ample: <i>italic</i> and <b>bol</b>');
});

// Fix for https://github.com/capricorn86/happy-dom/issues/853.
it('Clones multiple child elements inside a paragraph.', () => {
const paragraph = document.createElement('p');

paragraph.innerHTML = 'Example: <i>italic</i> and <b>bold</b>';

range.setStart(paragraph.querySelector('i').firstChild, 2);
range.setEnd(paragraph.querySelector('b').firstChild, 3);

const documentFragment = range.cloneContents();

document.body.appendChild(documentFragment);

expect(document.body.innerHTML).toBe('<i>alic</i> and <b>bol</b>');
});
});

describe('cloneRange()', () => {
Expand Down

0 comments on commit e697ebc

Please sign in to comment.