Skip to content

Commit

Permalink
#450@trivial: Continue on Range implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
capricorn86 committed Jun 22, 2022
1 parent fc7fef0 commit 781bf52
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 192 deletions.
1 change: 0 additions & 1 deletion packages/happy-dom/src/nodes/element/Element.ts
Expand Up @@ -2,7 +2,6 @@ import Node from '../node/Node';
import ShadowRoot from '../shadow-root/ShadowRoot';
import Attr from '../../attribute/Attr';
import DOMRect from './DOMRect';
import Range from '../../range/Range';
import DOMTokenList from '../../dom-token-list/DOMTokenList';
import IDOMTokenList from '../../dom-token-list/IDOMTokenList';
import QuerySelector from '../../query-selector/QuerySelector';
Expand Down
10 changes: 10 additions & 0 deletions packages/happy-dom/src/nodes/text/IText.ts
@@ -1,6 +1,16 @@
import ICharacterData from '../character-data/ICharacterData';

export default interface IText extends ICharacterData {
/**
* Breaks the Text node into two nodes at the specified offset, keeping both nodes in the tree as siblings.
*
* @see https://dom.spec.whatwg.org/#dom-text-splittext
* @see https://dom.spec.whatwg.org/#dom-text-splittext
* @param offset Offset.
* @returns New text node.
*/
splitText(offset: number): IText;

/**
* Clones a node.
*
Expand Down
33 changes: 33 additions & 0 deletions packages/happy-dom/src/nodes/text/Text.ts
@@ -1,6 +1,8 @@
import Node from '../node/Node';
import CharacterData from '../character-data/CharacterData';
import IText from './IText';
import DOMException from '../../exception/DOMException';
import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum';

/**
* Text node.
Expand All @@ -17,6 +19,37 @@ export default class Text extends CharacterData implements IText {
return '#text';
}

/**
* Breaks the Text node into two nodes at the specified offset, keeping both nodes in the tree as siblings.
*
* @see https://dom.spec.whatwg.org/#dom-text-splittext
* @see https://dom.spec.whatwg.org/#dom-text-splittext
* @param offset Offset.
* @returns New text node.
*/
public splitText(offset: number): IText {
const length = this._data.length;

if (offset > length) {
new DOMException(
'The index is not in the allowed range.',
DOMExceptionNameEnum.indexSizeError
);
}

const count = length - offset;
const newData = this.substringData(offset, count);
const newNode = <IText>this.ownerDocument.createTextNode(newData);

if (this.parentNode !== null) {
this.parentNode.insertBefore(newNode, this.nextSibling);
}

this.replaceData(offset, count, '');

return newNode;
}

/**
* Converts to string.
*
Expand Down

0 comments on commit 781bf52

Please sign in to comment.