Best JavaScript code snippet using wpt
Range-impl.js
Source:Range-impl.js
...35 // https://dom.spec.whatwg.org/#dom-range-commonancestorcontainer36 get commonAncestorContainer() {37 const { _start, _end } = this;38 for (const container of domSymbolTree.ancestorsIterator(_start.node)) {39 if (isInclusiveAncestor(container, _end.node)) {40 return container;41 }42 }43 return null;44 }45 // https://dom.spec.whatwg.org/#dom-range-setstart46 setStart(node, offset) {47 setBoundaryPointStart(this, node, offset);48 }49 // https://dom.spec.whatwg.org/#dom-range-setend50 setEnd(node, offset) {51 setBoundaryPointEnd(this, node, offset);52 }53 // https://dom.spec.whatwg.org/#dom-range-setstartbefore54 setStartBefore(node) {55 const parent = domSymbolTree.parent(node);56 if (!parent) {57 throw DOMException.create(this._globalObject, ["The given Node has no parent.", "InvalidNodeTypeError"]);58 }59 setBoundaryPointStart(this, parent, domSymbolTree.index(node));60 }61 // https://dom.spec.whatwg.org/#dom-range-setstartafter62 setStartAfter(node) {63 const parent = domSymbolTree.parent(node);64 if (!parent) {65 throw DOMException.create(this._globalObject, ["The given Node has no parent.", "InvalidNodeTypeError"]);66 }67 setBoundaryPointStart(this, parent, domSymbolTree.index(node) + 1);68 }69 // https://dom.spec.whatwg.org/#dom-range-setendbefore70 setEndBefore(node) {71 const parent = domSymbolTree.parent(node);72 if (!parent) {73 throw DOMException.create(this._globalObject, ["The given Node has no parent.", "InvalidNodeTypeError"]);74 }75 setBoundaryPointEnd(this, parent, domSymbolTree.index(node));76 }77 // https://dom.spec.whatwg.org/#dom-range-setendafter78 setEndAfter(node) {79 const parent = domSymbolTree.parent(node);80 if (!parent) {81 throw DOMException.create(this._globalObject, ["The given Node has no parent.", "InvalidNodeTypeError"]);82 }83 setBoundaryPointEnd(this, parent, domSymbolTree.index(node) + 1);84 }85 // https://dom.spec.whatwg.org/#dom-range-collapse86 collapse(toStart) {87 if (toStart) {88 this._setLiveRangeEnd(this._start.node, this._start.offset);89 } else {90 this._setLiveRangeStart(this._end.node, this._end.offset);91 }92 }93 // https://dom.spec.whatwg.org/#dom-range-selectnode94 selectNode(node) {95 selectNodeWithinRange(node, this);96 }97 // https://dom.spec.whatwg.org/#dom-range-selectnodecontents98 selectNodeContents(node) {99 if (node.nodeType === NODE_TYPE.DOCUMENT_TYPE_NODE) {100 throw DOMException.create(this._globalObject, [101 "DocumentType Node can't be used as boundary point.",102 "InvalidNodeTypeError"103 ]);104 }105 const length = nodeLength(node);106 this._setLiveRangeStart(node, 0);107 this._setLiveRangeEnd(node, length);108 }109 // https://dom.spec.whatwg.org/#dom-range-compareboundarypoints110 compareBoundaryPoints(how, sourceRange) {111 if (112 how !== RANGE_COMPARISON_TYPE.START_TO_START &&113 how !== RANGE_COMPARISON_TYPE.START_TO_END &&114 how !== RANGE_COMPARISON_TYPE.END_TO_END &&115 how !== RANGE_COMPARISON_TYPE.END_TO_START116 ) {117 const message = "The comparison method provided must be one of 'START_TO_START', 'START_TO_END', 'END_TO_END', " +118 "or 'END_TO_START'.";119 throw DOMException.create(this._globalObject, [message, "NotSupportedError"]);120 }121 if (this._root !== sourceRange._root) {122 throw DOMException.create(this._globalObject, ["The two Ranges are not in the same tree.", "WrongDocumentError"]);123 }124 let thisPoint, otherPoint;125 if (how === RANGE_COMPARISON_TYPE.START_TO_START) {126 thisPoint = this._start;127 otherPoint = sourceRange._start;128 } else if (how === RANGE_COMPARISON_TYPE.START_TO_END) {129 thisPoint = this._end;130 otherPoint = sourceRange._start;131 } else if (how === RANGE_COMPARISON_TYPE.END_TO_END) {132 thisPoint = this._end;133 otherPoint = sourceRange._end;134 } else {135 thisPoint = this._start;136 otherPoint = sourceRange._end;137 }138 return compareBoundaryPointsPosition(thisPoint, otherPoint);139 }140 // https://dom.spec.whatwg.org/#dom-range-deletecontents141 deleteContents() {142 if (this.collapsed) {143 return;144 }145 const { _start: originalStart, _end: originalEnd } = this;146 if (147 originalStart.node === originalEnd.node &&148 (149 originalStart.node.nodeType === NODE_TYPE.TEXT_NODE ||150 originalStart.node.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||151 originalStart.node.nodeType === NODE_TYPE.COMMENT_NODE152 )153 ) {154 originalStart.node.replaceData(originalStart.offset, originalEnd.offset - originalStart.offset, "");155 return;156 }157 const nodesToRemove = [];158 let currentNode = this._start.node;159 const endNode = nextNodeDescendant(this._end.node);160 while (currentNode && currentNode !== endNode) {161 if (162 isContained(currentNode, this) &&163 !isContained(domSymbolTree.parent(currentNode), this)164 ) {165 nodesToRemove.push(currentNode);166 }167 currentNode = domSymbolTree.following(currentNode);168 }169 let newNode, newOffset;170 if (isInclusiveAncestor(originalStart.node, originalEnd.node)) {171 newNode = originalStart.node;172 newOffset = originalStart.offset;173 } else {174 let referenceNode = originalStart.node;175 while (176 referenceNode &&177 !isInclusiveAncestor(domSymbolTree.parent(referenceNode), originalEnd.node)178 ) {179 referenceNode = domSymbolTree.parent(referenceNode);180 }181 newNode = domSymbolTree.parent(referenceNode);182 newOffset = domSymbolTree.index(referenceNode) + 1;183 }184 if (185 originalStart.node.nodeType === NODE_TYPE.TEXT_NODE ||186 originalStart.node.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||187 originalStart.node.nodeType === NODE_TYPE.COMMENT_NODE188 ) {189 originalStart.node.replaceData(originalStart.offset, nodeLength(originalStart.node) - originalStart.offset, "");190 }191 for (const node of nodesToRemove) {192 const parent = domSymbolTree.parent(node);193 parent.removeChild(node);194 }195 if (196 originalEnd.node.nodeType === NODE_TYPE.TEXT_NODE ||197 originalEnd.node.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||198 originalEnd.node.nodeType === NODE_TYPE.COMMENT_NODE199 ) {200 originalEnd.node.replaceData(0, originalEnd.offset, "");201 }202 this._setLiveRangeStart(newNode, newOffset);203 this._setLiveRangeEnd(newNode, newOffset);204 }205 // https://dom.spec.whatwg.org/#dom-range-extractcontents206 extractContents() {207 return extractRange(this);208 }209 // https://dom.spec.whatwg.org/#dom-range-clonecontents210 cloneContents() {211 return cloneRange(this);212 }213 // https://dom.spec.whatwg.org/#dom-range-insertnode214 insertNode(node) {215 insertNodeInRange(node, this);216 }217 // https://dom.spec.whatwg.org/#dom-range-surroundcontents218 surroundContents(newParent) {219 let node = this.commonAncestorContainer;220 const endNode = nextNodeDescendant(node);221 while (node !== endNode) {222 if (node.nodeType !== NODE_TYPE.TEXT_NODE && isPartiallyContained(node, this)) {223 throw DOMException.create(this._globalObject, [224 "The Range has partially contains a non-Text node.",225 "InvalidStateError"226 ]);227 }228 node = domSymbolTree.following(node);229 }230 if (231 newParent.nodeType === NODE_TYPE.DOCUMENT_NODE ||232 newParent.nodeType === NODE_TYPE.DOCUMENT_TYPE_NODE ||233 newParent.nodeType === NODE_TYPE.DOCUMENT_FRAGMENT_NODE234 ) {235 throw DOMException.create(this._globalObject, ["Invalid element type.", "InvalidNodeTypeError"]);236 }237 const fragment = extractRange(this);238 while (domSymbolTree.firstChild(newParent)) {239 newParent.removeChild(domSymbolTree.firstChild(newParent));240 }241 insertNodeInRange(newParent, this);242 newParent.appendChild(fragment);243 selectNodeWithinRange(newParent, this);244 }245 // https://dom.spec.whatwg.org/#dom-range-clonerange246 cloneRange() {247 const { _start, _end, _globalObject } = this;248 return Range.createImpl(_globalObject, [], {249 start: { node: _start.node, offset: _start.offset },250 end: { node: _end.node, offset: _end.offset }251 });252 }253 // https://dom.spec.whatwg.org/#dom-range-detach254 detach() {255 // Do nothing by spec!256 }257 // https://dom.spec.whatwg.org/#dom-range-ispointinrange258 isPointInRange(node, offset) {259 if (nodeRoot(node) !== this._root) {260 return false;261 }262 validateSetBoundaryPoint(node, offset);263 const bp = { node, offset };264 if (265 compareBoundaryPointsPosition(bp, this._start) === -1 ||266 compareBoundaryPointsPosition(bp, this._end) === 1267 ) {268 return false;269 }270 return true;271 }272 // https://dom.spec.whatwg.org/#dom-range-comparepoint273 comparePoint(node, offset) {274 if (nodeRoot(node) !== this._root) {275 throw DOMException.create(this._globalObject, [276 "The given Node and the Range are not in the same tree.",277 "WrongDocumentError"278 ]);279 }280 validateSetBoundaryPoint(node, offset);281 const bp = { node, offset };282 if (compareBoundaryPointsPosition(bp, this._start) === -1) {283 return -1;284 } else if (compareBoundaryPointsPosition(bp, this._end) === 1) {285 return 1;286 }287 return 0;288 }289 // https://dom.spec.whatwg.org/#dom-range-intersectsnode290 intersectsNode(node) {291 if (nodeRoot(node) !== this._root) {292 return false;293 }294 const parent = domSymbolTree.parent(node);295 if (!parent) {296 return true;297 }298 const offset = domSymbolTree.index(node);299 return (300 compareBoundaryPointsPosition({ node: parent, offset }, this._end) === -1 &&301 compareBoundaryPointsPosition({ node: parent, offset: offset + 1 }, this._start) === 1302 );303 }304 // https://dom.spec.whatwg.org/#dom-range-stringifier305 toString() {306 let s = "";307 const { _start, _end } = this;308 if (_start.node === _end.node && _start.node.nodeType === NODE_TYPE.TEXT_NODE) {309 return _start.node.data.slice(_start.offset, _end.offset);310 }311 if (_start.node.nodeType === NODE_TYPE.TEXT_NODE) {312 s += _start.node.data.slice(_start.offset);313 }314 let currentNode = _start.node;315 const endNode = nextNodeDescendant(_end.node);316 while (currentNode && currentNode !== endNode) {317 if (currentNode.nodeType === NODE_TYPE.TEXT_NODE && isContained(currentNode, this)) {318 s += currentNode.data;319 }320 currentNode = domSymbolTree.following(currentNode);321 }322 if (_end.node.nodeType === NODE_TYPE.TEXT_NODE) {323 s += _end.node.data.slice(0, _end.offset);324 }325 return s;326 }327 // https://w3c.github.io/DOM-Parsing/#dom-range-createcontextualfragment328 createContextualFragment(fragment) {329 const { node } = this._start;330 let element;331 switch (node.nodeType) {332 case NODE_TYPE.DOCUMENT_NODE:333 case NODE_TYPE.DOCUMENT_FRAGMENT_NODE:334 element = null;335 break;336 case NODE_TYPE.ELEMENT_NODE:337 element = node;338 break;339 case NODE_TYPE.TEXT_NODE:340 case NODE_TYPE.COMMENT_NODE:341 element = node.parentElement;342 break;343 default:344 throw new Error("Internal error: Invalid range start node");345 }346 if (347 element === null || (348 element._ownerDocument._parsingMode === "html" &&349 element._localName === "html" &&350 element._namespaceURI === HTML_NS351 )352 ) {353 element = createElement(node._ownerDocument, "body", HTML_NS);354 }355 return parseFragment(fragment, element);356 }357 // https://dom.spec.whatwg.org/#concept-range-root358 get _root() {359 return nodeRoot(this._start.node);360 }361 _setLiveRangeStart(node, offset) {362 if (this._start && this._start.node !== node) {363 this._start.node._referencedRanges.delete(this);364 }365 if (!node._referencedRanges.has(this)) {366 node._referencedRanges.add(this);367 }368 this._start = {369 node,370 offset371 };372 }373 _setLiveRangeEnd(node, offset) {374 if (this._end && this._end.node !== node) {375 this._end.node._referencedRanges.delete(this);376 }377 if (!node._referencedRanges.has(this)) {378 node._referencedRanges.add(this);379 }380 this._end = {381 node,382 offset383 };384 }385}386function nextNodeDescendant(node) {387 while (node && !domSymbolTree.nextSibling(node)) {388 node = domSymbolTree.parent(node);389 }390 if (!node) {391 return null;392 }393 return domSymbolTree.nextSibling(node);394}395// https://dom.spec.whatwg.org/#concept-range-bp-set396function validateSetBoundaryPoint(node, offset) {397 if (node.nodeType === NODE_TYPE.DOCUMENT_TYPE_NODE) {398 throw DOMException.create(node._globalObject, [399 "DocumentType Node can't be used as boundary point.",400 "InvalidNodeTypeError"401 ]);402 }403 if (offset > nodeLength(node)) {404 throw DOMException.create(node._globalObject, ["Offset out of bound.", "IndexSizeError"]);405 }406}407function setBoundaryPointStart(range, node, offset) {408 validateSetBoundaryPoint(node, offset);409 const bp = { node, offset };410 if (411 nodeRoot(node) !== range._root ||412 compareBoundaryPointsPosition(bp, range._end) === 1413 ) {414 range._setLiveRangeEnd(node, offset);415 }416 range._setLiveRangeStart(node, offset);417}418function setBoundaryPointEnd(range, node, offset) {419 validateSetBoundaryPoint(node, offset);420 const bp = { node, offset };421 if (422 nodeRoot(node) !== range._root ||423 compareBoundaryPointsPosition(bp, range._start) === -1424 ) {425 range._setLiveRangeStart(node, offset);426 }427 range._setLiveRangeEnd(node, offset);428}429// https://dom.spec.whatwg.org/#concept-range-select430function selectNodeWithinRange(node, range) {431 const parent = domSymbolTree.parent(node);432 if (!parent) {433 throw DOMException.create(node._globalObject, ["The given Node has no parent.", "InvalidNodeTypeError"]);434 }435 const index = domSymbolTree.index(node);436 range._setLiveRangeStart(parent, index);437 range._setLiveRangeEnd(parent, index + 1);438}439// https://dom.spec.whatwg.org/#contained440function isContained(node, range) {441 const { _start, _end } = range;442 return (443 compareBoundaryPointsPosition({ node, offset: 0 }, _start) === 1 &&444 compareBoundaryPointsPosition({ node, offset: nodeLength(node) }, _end) === -1445 );446}447// https://dom.spec.whatwg.org/#partially-contained448function isPartiallyContained(node, range) {449 const { _start, _end } = range;450 return (451 (isInclusiveAncestor(node, _start.node) && !isInclusiveAncestor(node, _end.node)) ||452 (!isInclusiveAncestor(node, _start.node) && isInclusiveAncestor(node, _end.node))453 );454}455// https://dom.spec.whatwg.org/#concept-range-insert456function insertNodeInRange(node, range) {457 const { node: startNode, offset: startOffset } = range._start;458 if (459 startNode.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||460 startNode.nodeType === NODE_TYPE.COMMENT_NODE ||461 (startNode.nodeType === NODE_TYPE.TEXT_NODE && !domSymbolTree.parent(startNode)) ||462 node === startNode463 ) {464 throw DOMException.create(node._globalObject, ["Invalid start node.", "HierarchyRequestError"]);465 }466 let referenceNode = startNode.nodeType === NODE_TYPE.TEXT_NODE ?467 startNode :468 domSymbolTree.childrenToArray(startNode)[startOffset] || null;469 const parent = !referenceNode ?470 startNode :471 domSymbolTree.parent(referenceNode);472 parent._preInsertValidity(node, referenceNode);473 if (startNode.nodeType === NODE_TYPE.TEXT_NODE) {474 referenceNode = startNode.splitText(startOffset);475 }476 if (node === referenceNode) {477 referenceNode = domSymbolTree.nextSibling(referenceNode);478 }479 const nodeParent = domSymbolTree.parent(node);480 if (nodeParent) {481 nodeParent.removeChild(node);482 }483 let newOffset = !referenceNode ? nodeLength(parent) : domSymbolTree.index(referenceNode);484 newOffset += node.nodeType === NODE_TYPE.DOCUMENT_FRAGMENT_NODE ? nodeLength(node) : 1;485 parent.insertBefore(node, referenceNode);486 if (range.collapsed) {487 range._setLiveRangeEnd(parent, newOffset);488 }489}490// https://dom.spec.whatwg.org/#concept-range-clone491function cloneRange(range) {492 const { _start: originalStart, _end: originalEnd, _globalObject } = range;493 const fragment = DocumentFragment.createImpl(_globalObject, [], {494 ownerDocument: originalStart.node._ownerDocument495 });496 if (range.collapsed) {497 return fragment;498 }499 if (500 originalStart.node === originalEnd.node &&501 (502 originalStart.node.nodeType === NODE_TYPE.TEXT_NODE ||503 originalStart.node.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||504 originalStart.node.nodeType === NODE_TYPE.COMMENT_NODE505 )506 ) {507 const cloned = clone(originalStart.node);508 cloned._data = cloned.substringData(originalStart.offset, originalEnd.offset - originalStart.offset);509 fragment.appendChild(cloned);510 return fragment;511 }512 let commonAncestor = originalStart.node;513 while (!isInclusiveAncestor(commonAncestor, originalEnd.node)) {514 commonAncestor = domSymbolTree.parent(commonAncestor);515 }516 let firstPartialContainedChild = null;517 if (!isInclusiveAncestor(originalStart.node, originalEnd.node)) {518 let candidate = domSymbolTree.firstChild(commonAncestor);519 while (!firstPartialContainedChild) {520 if (isPartiallyContained(candidate, range)) {521 firstPartialContainedChild = candidate;522 }523 candidate = domSymbolTree.nextSibling(candidate);524 }525 }526 let lastPartiallyContainedChild = null;527 if (!isInclusiveAncestor(originalEnd.node, originalStart.node)) {528 let candidate = domSymbolTree.lastChild(commonAncestor);529 while (!lastPartiallyContainedChild) {530 if (isPartiallyContained(candidate, range)) {531 lastPartiallyContainedChild = candidate;532 }533 candidate = domSymbolTree.previousSibling(candidate);534 }535 }536 const containedChildren = domSymbolTree.childrenToArray(commonAncestor)537 .filter(node => isContained(node, range));538 const hasDoctypeChildren = containedChildren.some(node => node.nodeType === NODE_TYPE.DOCUMENT_TYPE_NODE);539 if (hasDoctypeChildren) {540 throw DOMException.create(range._globalObject, ["Invalid document type element.", "HierarchyRequestError"]);541 }542 if (543 firstPartialContainedChild !== null &&544 (545 firstPartialContainedChild.nodeType === NODE_TYPE.TEXT_NODE ||546 firstPartialContainedChild.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||547 firstPartialContainedChild.nodeType === NODE_TYPE.COMMENT_NODE548 )549 ) {550 const cloned = clone(originalStart.node);551 cloned._data = cloned.substringData(originalStart.offset, nodeLength(originalStart.node) - originalStart.offset);552 fragment.appendChild(cloned);553 } else if (firstPartialContainedChild !== null) {554 const cloned = clone(firstPartialContainedChild);555 fragment.appendChild(cloned);556 const subrange = Range.createImpl(_globalObject, [], {557 start: { node: originalStart.node, offset: originalStart.offset },558 end: { node: firstPartialContainedChild, offset: nodeLength(firstPartialContainedChild) }559 });560 const subfragment = cloneRange(subrange);561 cloned.appendChild(subfragment);562 }563 for (const containedChild of containedChildren) {564 const cloned = clone(containedChild, undefined, true);565 fragment.appendChild(cloned);566 }567 if (568 lastPartiallyContainedChild !== null &&569 (570 lastPartiallyContainedChild.nodeType === NODE_TYPE.TEXT_NODE ||571 lastPartiallyContainedChild.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||572 lastPartiallyContainedChild.nodeType === NODE_TYPE.COMMENT_NODE573 )574 ) {575 const cloned = clone(originalEnd.node);576 cloned._data = cloned.substringData(0, originalEnd.offset);577 fragment.appendChild(cloned);578 } else if (lastPartiallyContainedChild !== null) {579 const cloned = clone(lastPartiallyContainedChild);580 fragment.appendChild(cloned);581 const subrange = Range.createImpl(_globalObject, [], {582 start: { node: lastPartiallyContainedChild, offset: 0 },583 end: { node: originalEnd.node, offset: originalEnd.offset }584 });585 const subfragment = cloneRange(subrange);586 cloned.appendChild(subfragment);587 }588 return fragment;589}590// https://dom.spec.whatwg.org/#concept-range-extract591function extractRange(range) {592 const { _start: originalStart, _end: originalEnd, _globalObject } = range;593 const fragment = DocumentFragment.createImpl(_globalObject, [], {594 ownerDocument: originalStart.node._ownerDocument595 });596 if (range.collapsed) {597 return fragment;598 }599 if (600 originalStart.node === originalEnd.node &&601 (602 originalStart.node.nodeType === NODE_TYPE.TEXT_NODE ||603 originalStart.node.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||604 originalStart.node.nodeType === NODE_TYPE.COMMENT_NODE605 )606 ) {607 const cloned = clone(originalStart.node);608 cloned._data = cloned.substringData(originalStart.offset, originalEnd.offset - originalStart.offset);609 fragment.appendChild(cloned);610 originalStart.node.replaceData(originalStart.offset, originalEnd.offset - originalStart.offset, "");611 return fragment;612 }613 let commonAncestor = originalStart.node;614 while (!isInclusiveAncestor(commonAncestor, originalEnd.node)) {615 commonAncestor = domSymbolTree.parent(commonAncestor);616 }617 let firstPartialContainedChild = null;618 if (!isInclusiveAncestor(originalStart.node, originalEnd.node)) {619 let candidate = domSymbolTree.firstChild(commonAncestor);620 while (!firstPartialContainedChild) {621 if (isPartiallyContained(candidate, range)) {622 firstPartialContainedChild = candidate;623 }624 candidate = domSymbolTree.nextSibling(candidate);625 }626 }627 let lastPartiallyContainedChild = null;628 if (!isInclusiveAncestor(originalEnd.node, originalStart.node)) {629 let candidate = domSymbolTree.lastChild(commonAncestor);630 while (!lastPartiallyContainedChild) {631 if (isPartiallyContained(candidate, range)) {632 lastPartiallyContainedChild = candidate;633 }634 candidate = domSymbolTree.previousSibling(candidate);635 }636 }637 const containedChildren = domSymbolTree.childrenToArray(commonAncestor)638 .filter(node => isContained(node, range));639 const hasDoctypeChildren = containedChildren.some(node => node.nodeType === NODE_TYPE.DOCUMENT_TYPE_NODE);640 if (hasDoctypeChildren) {641 throw DOMException.create(range._globalObject, ["Invalid document type element.", "HierarchyRequestError"]);642 }643 let newNode, newOffset;644 if (isInclusiveAncestor(originalStart.node, originalEnd.node)) {645 newNode = originalStart.node;646 newOffset = originalStart.offset;647 } else {648 let referenceNode = originalStart.node;649 while (650 referenceNode &&651 !isInclusiveAncestor(domSymbolTree.parent(referenceNode), originalEnd.node)652 ) {653 referenceNode = domSymbolTree.parent(referenceNode);654 }655 newNode = domSymbolTree.parent(referenceNode);656 newOffset = domSymbolTree.index(referenceNode) + 1;657 }658 if (659 firstPartialContainedChild !== null &&660 (661 firstPartialContainedChild.nodeType === NODE_TYPE.TEXT_NODE ||662 firstPartialContainedChild.nodeType === NODE_TYPE.PROCESSING_INSTRUCTION_NODE ||663 firstPartialContainedChild.nodeType === NODE_TYPE.COMMENT_NODE664 )665 ) {...
RangeUtility.ts
Source:RangeUtility.ts
...37 }38 if (NodeUtility.isFollowing(pointA.node, pointB.node)) {39 return this.compareBoundaryPointsPosition(pointB, pointA) === -1 ? 1 : -1;40 }41 if (NodeUtility.isInclusiveAncestor(pointA.node, pointB.node)) {42 let child = pointB.node;43 while (child.parentNode !== pointA.node) {44 child = child.parentNode;45 }46 if (child.parentNode.childNodes.indexOf(child) < pointA.offset) {47 return 1;48 }49 }50 return -1;51 }52 /**53 * Validates a boundary point.54 *55 * @throws DOMException56 * @param point Boundary point.57 */58 public static validateBoundaryPoint(point: IRangeBoundaryPoint): void {59 if (point.node.nodeType === NodeTypeEnum.documentTypeNode) {60 throw new DOMException(61 `DocumentType Node can't be used as boundary point.`,62 DOMExceptionNameEnum.invalidNodeTypeError63 );64 }65 if (point.offset > NodeUtility.getNodeLength(point.node)) {66 throw new DOMException(`Offset out of bound.`, DOMExceptionNameEnum.indexSizeError);67 }68 }69 /**70 * Returns "true" if contained.71 *72 * @param node Node.73 * @param range Range.74 * @returns "true" if contained.75 */76 public static isContained(node: INode, range: Range): boolean {77 return (78 this.compareBoundaryPointsPosition(79 { node, offset: 0 },80 { node: range.startContainer, offset: range.startOffset }81 ) === 1 &&82 this.compareBoundaryPointsPosition(83 { node, offset: NodeUtility.getNodeLength(node) },84 { node: range.endContainer, offset: range.endOffset }85 ) === -186 );87 }88 /**89 * Returns "true" if partially contained.90 *91 * @param node Node.92 * @param range Range.93 * @returns "true" if partially contained.94 */95 public static isPartiallyContained(node: INode, range: Range): boolean {96 return (97 (NodeUtility.isInclusiveAncestor(node, range.startContainer) &&98 !NodeUtility.isInclusiveAncestor(node, range.endContainer)) ||99 (!NodeUtility.isInclusiveAncestor(node, range.startContainer) &&100 NodeUtility.isInclusiveAncestor(node, range.endContainer))101 );102 }...
HTMLLabelElement-impl.js
Source:HTMLLabelElement-impl.js
...56 }57 _activationBehavior(event) {58 // Check if the event's target is an inclusive descendant of any interactive content descendant of this <label>.59 // If so, do nothing.60 if (event.target && event.target !== this && isInclusiveAncestor(this, event.target)) {61 for (const ancestor of domSymbolTree.ancestorsIterator(event.target)) {62 if (ancestor === this) {63 break;64 }65 if (isInteractiveContent(ancestor)) {66 return;67 }68 }69 }70 const node = this.control;71 if (node && !isDisabled(node)) {72 // Check if the control is an inclusive ancestor of the event's target (and has already received this event).73 // If so, do nothing.74 // See https://github.com/whatwg/html/issues/5415.75 if (event.target && isInclusiveAncestor(node, event.target)) {76 return;77 }78 sendClickToAssociatedNode(node);79 }80 }81}82module.exports = {83 implementation: HTMLLabelElementImpl...
Using AI Code Generation
1var isInclusiveAncestor = require('wptoolkit').isInclusiveAncestor;2var getAncestor = require('wptoolkit').getAncestor;3var getAncestorByClass = require('wptoolkit').getAncestorByClass;4var getAncestorByTag = require('wptoolkit').getAncestorByTag;5var getAncestorByClass = require('wptoolkit').getAncestorByClass;6var getAncestorByTag = require('wptoolkit').getAncestorByTag;7var getAncestorByClass = require('wptoolkit').getAncestorByClass;8var getAncestorByTag = require('wptoolkit').getAncestorByTag;9var getAncestorByClass = require('wptoolkit').getAncestorByClass;10var getAncestorByTag = require('wptoolkit').getAncestorByTag;11var getAncestorByClass = require('wptoolkit').getAncestorByClass;12var getAncestorByTag = require('wptoolkit').getAncestorByTag;13var getAncestorByClass = require('wptoolkit').getAncestorByClass;14var getAncestorByTag = require('wptoolkit').getAncestorByTag;
Using AI Code Generation
1var wptbElementObject = new WPTB_ElementObject( 'wptb-element-dummy-1' );2var wptbElementObject = new WPTB_ElementObject( 'wptb-element-dummy-1' );3var wptbElementObject = new WPTB_ElementObject( 'wptb-element-dummy-1' );4var wptbElementObject = new WPTB_ElementObject( 'wptb-element-dummy-1' );5var wptbElementObject = new WPTB_ElementObject( 'wptb-element-dummy-1' );6var result = wptbElementObject.isInclusiveAncestor( 'wptb-element-dummy
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools('Taj Mahal');3wp.get(function(err, response) {4 if (err) {5 console.log(err);6 } else {7 var page = response;8 }9});10var wptools = require('wptools');11var wp = new wptools('Taj Mahal');12wp.get(function(err, response) {13 if (err) {14 console.log(err);15 } else {16 var page = response;17 }18});19var wptools = require('wptools');20var wp = new wptools('Taj Mahal');21wp.get(function(err, response) {22 if (err) {23 console.log(err);24 } else {25 var page = response;26 }27});28var wptools = require('wptools');29var wp = new wptools('Taj Mahal');30wp.get(function(err, response) {31 if (err) {32 console.log(err);33 } else {34 var page = response;35 }36});
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('India');3page.get(function(err, resp) {4 if (!err) {5 var html = resp.html();6 var p = html('p');7 var p1 = p.eq(0);8 var p2 = p.eq(1);9 }10});11Copyright (c) 2013-2015
Using AI Code Generation
1function isInclusiveAncestor(element, ancestor) {2 return wpt.dom.isInclusiveAncestor(element, ancestor);3}4function isInclusiveDescendant(element, descendant) {5 return wpt.dom.isInclusiveDescendant(element, descendant);6}7function isAncestor(element, ancestor) {8 return wpt.dom.isAncestor(element, ancestor);9}10function isDescendant(element, descendant) {11 return wpt.dom.isDescendant(element, descendant);12}13function isSibling(element, sibling) {14 return wpt.dom.isSibling(element, sibling);15}16function isPreceding(element, preceding) {17 return wpt.dom.isPreceding(element, preceding);18}19function isFollowing(element, following) {20 return wpt.dom.isFollowing(element, following);21}22function isPrecedingSibling(element, precedingSibling) {23 return wpt.dom.isPrecedingSibling(element, precedingSibling);24}25function isFollowingSibling(element, followingSibling) {26 return wpt.dom.isFollowingSibling(element, followingSibling);27}
Using AI Code Generation
1var selectedNode = document.getElementById('selectedNode');2var targetNode = document.getElementById('targetNode');3if (wptoolkit.isInclusiveAncestor(selectedNode, targetNode)) {4}5var selectedNode = document.getElementById('selectedNode');6var targetNode = document.getElementById('targetNode');7if (wptoolkit.isInclusiveAncestor(selectedNode, targetNode)) {8}9var selectedNode = document.getElementById('selectedNode');10var targetNode = document.getElementById('targetNode');11if (wptoolkit.isInclusiveAncestor(selectedNode, targetNode)) {12}13var selectedNode = document.getElementById('selectedNode');14var targetNode = document.getElementById('targetNode');15if (wptoolkit.isInclusiveAncestor(selectedNode, targetNode)) {16}17var selectedNode = document.getElementById('selectedNode');18var targetNode = document.getElementById('targetNode');19if (wptoolkit.isInclusiveAncestor(selectedNode, targetNode)) {20}21var selectedNode = document.getElementById('selectedNode');22var targetNode = document.getElementById('targetNode');23if (wptoolkit.isInclusiveAncestor(selectedNode, targetNode)) {24}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!