Best JavaScript code snippet using wpt
common.js
Source:common.js
...354 * ancestors."355 *356 * Is node1 an ancestor container of node2?357 */358function isAncestorContainer(node1, node2) {359 return node1 == node2 ||360 (node2.compareDocumentPosition(node1) & Node.DOCUMENT_POSITION_CONTAINS);361}362/**363 * Returns the first Node that's after node in tree order, or null if node is364 * the last Node.365 */366function nextNode(node) {367 if (node.hasChildNodes()) {368 return node.firstChild;369 }370 return nextNodeDescendants(node);371}372/**373 * Returns the last Node that's before node in tree order, or null if node is374 * the first Node.375 */376function previousNode(node) {377 if (node.previousSibling) {378 node = node.previousSibling;379 while (node.hasChildNodes()) {380 node = node.lastChild;381 }382 return node;383 }384 return node.parentNode;385}386/**387 * Returns the next Node that's after node and all its descendants in tree388 * order, or null if node is the last Node or an ancestor of it.389 */390function nextNodeDescendants(node) {391 while (node && !node.nextSibling) {392 node = node.parentNode;393 }394 if (!node) {395 return null;396 }397 return node.nextSibling;398}399/**400 * Returns the ownerDocument of the Node, or the Node itself if it's a401 * Document.402 */403function ownerDocument(node) {404 return node.nodeType == Node.DOCUMENT_NODE405 ? node406 : node.ownerDocument;407}408/**409 * Returns true if ancestor is an ancestor of descendant, false otherwise.410 */411function isAncestor(ancestor, descendant) {412 if (!ancestor || !descendant) {413 return false;414 }415 while (descendant && descendant != ancestor) {416 descendant = descendant.parentNode;417 }418 return descendant == ancestor;419}420/**421 * Returns true if ancestor is an inclusive ancestor of descendant, false422 * otherwise.423 */424function isInclusiveAncestor(ancestor, descendant) {425 return ancestor === descendant || isAncestor(ancestor, descendant);426}427/**428 * Returns true if descendant is a descendant of ancestor, false otherwise.429 */430function isDescendant(descendant, ancestor) {431 return isAncestor(ancestor, descendant);432}433/**434 * Returns true if descendant is an inclusive descendant of ancestor, false435 * otherwise.436 */437function isInclusiveDescendant(descendant, ancestor) {438 return descendant === ancestor || isDescendant(descendant, ancestor);439}440/**441 * The position of two boundary points relative to one another, as defined by442 * the spec.443 */444function getPosition(nodeA, offsetA, nodeB, offsetB) {445 // "If node A is the same as node B, return equal if offset A equals offset446 // B, before if offset A is less than offset B, and after if offset A is447 // greater than offset B."448 if (nodeA == nodeB) {449 if (offsetA == offsetB) {450 return "equal";451 }452 if (offsetA < offsetB) {453 return "before";454 }455 if (offsetA > offsetB) {456 return "after";457 }458 }459 // "If node A is after node B in tree order, compute the position of (node460 // B, offset B) relative to (node A, offset A). If it is before, return461 // after. If it is after, return before."462 if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_FOLLOWING) {463 var pos = getPosition(nodeB, offsetB, nodeA, offsetA);464 if (pos == "before") {465 return "after";466 }467 if (pos == "after") {468 return "before";469 }470 }471 // "If node A is an ancestor of node B:"472 if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_CONTAINS) {473 // "Let child equal node B."474 var child = nodeB;475 // "While child is not a child of node A, set child to its parent."476 while (child.parentNode != nodeA) {477 child = child.parentNode;478 }479 // "If the index of child is less than offset A, return after."480 if (indexOf(child) < offsetA) {481 return "after";482 }483 }484 // "Return before."485 return "before";486}487/**488 * "contained" as defined by DOM Range: "A Node node is contained in a range489 * range if node's furthest ancestor is the same as range's root, and (node, 0)490 * is after range's start, and (node, length of node) is before range's end."491 */492function isContained(node, range) {493 var pos1 = getPosition(node, 0, range.startContainer, range.startOffset);494 var pos2 = getPosition(node, nodeLength(node), range.endContainer, range.endOffset);495 return furthestAncestor(node) == furthestAncestor(range.startContainer)496 && pos1 == "after"497 && pos2 == "before";498}499/**500 * "partially contained" as defined by DOM Range: "A Node is partially501 * contained in a range if it is an ancestor container of the range's start but502 * not its end, or vice versa."503 */504function isPartiallyContained(node, range) {505 var cond1 = isAncestorContainer(node, range.startContainer);506 var cond2 = isAncestorContainer(node, range.endContainer);507 return (cond1 && !cond2) || (cond2 && !cond1);508}509/**510 * Index of a node as defined by the spec.511 */512function indexOf(node) {513 if (!node.parentNode) {514 // No preceding sibling nodes, right?515 return 0;516 }517 var i = 0;518 while (node != node.parentNode.childNodes[i]) {519 i++;520 }521 return i;522}523/**524 * extractContents() implementation, following the spec. If an exception is525 * supposed to be thrown, will return a string with the name (e.g.,526 * "HIERARCHY_REQUEST_ERR") instead of a document fragment. It might also527 * return an arbitrary human-readable string if a condition is hit that implies528 * a spec bug.529 */530function myExtractContents(range) {531 // "Let frag be a new DocumentFragment whose ownerDocument is the same as532 // the ownerDocument of the context object's start node."533 var ownerDoc = range.startContainer.nodeType == Node.DOCUMENT_NODE534 ? range.startContainer535 : range.startContainer.ownerDocument;536 var frag = ownerDoc.createDocumentFragment();537 // "If the context object's start and end are the same, abort this method,538 // returning frag."539 if (range.startContainer == range.endContainer540 && range.startOffset == range.endOffset) {541 return frag;542 }543 // "Let original start node, original start offset, original end node, and544 // original end offset be the context object's start and end nodes and545 // offsets, respectively."546 var originalStartNode = range.startContainer;547 var originalStartOffset = range.startOffset;548 var originalEndNode = range.endContainer;549 var originalEndOffset = range.endOffset;550 // "If original start node is original end node, and they are a Text,551 // ProcessingInstruction, or Comment node:"552 if (range.startContainer == range.endContainer553 && (range.startContainer.nodeType == Node.TEXT_NODE554 || range.startContainer.nodeType == Node.PROCESSING_INSTRUCTION_NODE555 || range.startContainer.nodeType == Node.COMMENT_NODE)) {556 // "Let clone be the result of calling cloneNode(false) on original557 // start node."558 var clone = originalStartNode.cloneNode(false);559 // "Set the data of clone to the result of calling560 // substringData(original start offset, original end offset â original561 // start offset) on original start node."562 clone.data = originalStartNode.substringData(originalStartOffset,563 originalEndOffset - originalStartOffset);564 // "Append clone as the last child of frag."565 frag.appendChild(clone);566 // "Call deleteData(original start offset, original end offset â567 // original start offset) on original start node."568 originalStartNode.deleteData(originalStartOffset,569 originalEndOffset - originalStartOffset);570 // "Abort this method, returning frag."571 return frag;572 }573 // "Let common ancestor equal original start node."574 var commonAncestor = originalStartNode;575 // "While common ancestor is not an ancestor container of original end576 // node, set common ancestor to its own parent."577 while (!isAncestorContainer(commonAncestor, originalEndNode)) {578 commonAncestor = commonAncestor.parentNode;579 }580 // "If original start node is an ancestor container of original end node,581 // let first partially contained child be null."582 var firstPartiallyContainedChild;583 if (isAncestorContainer(originalStartNode, originalEndNode)) {584 firstPartiallyContainedChild = null;585 // "Otherwise, let first partially contained child be the first child of586 // common ancestor that is partially contained in the context object."587 } else {588 for (var i = 0; i < commonAncestor.childNodes.length; i++) {589 if (isPartiallyContained(commonAncestor.childNodes[i], range)) {590 firstPartiallyContainedChild = commonAncestor.childNodes[i];591 break;592 }593 }594 if (!firstPartiallyContainedChild) {595 throw "Spec bug: no first partially contained child!";596 }597 }598 // "If original end node is an ancestor container of original start node,599 // let last partially contained child be null."600 var lastPartiallyContainedChild;601 if (isAncestorContainer(originalEndNode, originalStartNode)) {602 lastPartiallyContainedChild = null;603 // "Otherwise, let last partially contained child be the last child of604 // common ancestor that is partially contained in the context object."605 } else {606 for (var i = commonAncestor.childNodes.length - 1; i >= 0; i--) {607 if (isPartiallyContained(commonAncestor.childNodes[i], range)) {608 lastPartiallyContainedChild = commonAncestor.childNodes[i];609 break;610 }611 }612 if (!lastPartiallyContainedChild) {613 throw "Spec bug: no last partially contained child!";614 }615 }616 // "Let contained children be a list of all children of common ancestor617 // that are contained in the context object, in tree order."618 //619 // "If any member of contained children is a DocumentType, raise a620 // HIERARCHY_REQUEST_ERR exception and abort these steps."621 var containedChildren = [];622 for (var i = 0; i < commonAncestor.childNodes.length; i++) {623 if (isContained(commonAncestor.childNodes[i], range)) {624 if (commonAncestor.childNodes[i].nodeType625 == Node.DOCUMENT_TYPE_NODE) {626 return "HIERARCHY_REQUEST_ERR";627 }628 containedChildren.push(commonAncestor.childNodes[i]);629 }630 }631 // "If original start node is an ancestor container of original end node,632 // set new node to original start node and new offset to original start633 // offset."634 var newNode, newOffset;635 if (isAncestorContainer(originalStartNode, originalEndNode)) {636 newNode = originalStartNode;637 newOffset = originalStartOffset;638 // "Otherwise:"639 } else {640 // "Let reference node equal original start node."641 var referenceNode = originalStartNode;642 // "While reference node's parent is not null and is not an ancestor643 // container of original end node, set reference node to its parent."644 while (referenceNode.parentNode645 && !isAncestorContainer(referenceNode.parentNode, originalEndNode)) {646 referenceNode = referenceNode.parentNode;647 }648 // "Set new node to the parent of reference node, and new offset to one649 // plus the index of reference node."650 newNode = referenceNode.parentNode;651 newOffset = 1 + indexOf(referenceNode);652 }653 // "If first partially contained child is a Text, ProcessingInstruction, or654 // Comment node:"655 if (firstPartiallyContainedChild656 && (firstPartiallyContainedChild.nodeType == Node.TEXT_NODE657 || firstPartiallyContainedChild.nodeType == Node.PROCESSING_INSTRUCTION_NODE658 || firstPartiallyContainedChild.nodeType == Node.COMMENT_NODE)) {659 // "Let clone be the result of calling cloneNode(false) on original...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var isAncestorContainer = wptoolkit.isAncestorContainer;3var container = document.getElementById('container');4var node = document.getElementById('node');5var wptoolkit = require('wptoolkit');6var isAncestorContainer = wptoolkit.isAncestorContainer;7var container = document.getElementById('container');8var node = document.getElementById('node');9var wptoolkit = require('wptoolkit');10var isAncestorContainer = wptoolkit.isAncestorContainer;11var container = document.getElementById('container');12var node = document.getElementById('node');13var wptoolkit = require('wptoolkit');14var isAncestorContainer = wptoolkit.isAncestorContainer;15var container = document.getElementById('container');16var node = document.getElementById('node');17var wptoolkit = require('wptoolkit');18var isAncestorContainer = wptoolkit.isAncestorContainer;19var container = document.getElementById('container');20var node = document.getElementById('node');21var wptoolkit = require('wptoolkit');22var isAncestorContainer = wptoolkit.isAncestorContainer;23var container = document.getElementById('container');24var node = document.getElementById('node');25var wptoolkit = require('wptoolkit');26var isAncestorContainer = wptoolkit.isAncestorContainer;27var container = document.getElementById('
Using AI Code Generation
1var doc = document.implementation.createHTMLDocument("test");2var div = doc.createElement("div");3var p = doc.createElement("p");4div.appendChild(p);5var text = doc.createTextNode("test");6p.appendChild(text);7var range = doc.createRange();8range.selectNode(p);9var range2 = doc.createRange();10range2.selectNode(text);11var result = range.isAncestorContainer(range2);12console.log(result);
Using AI Code Generation
1var range = document.selection.createRange();2var element = document.getElementById("someElement");3var isAncestor = range.isAncestorContainer(element);4var range = document.selection.createRange();5range.select();6var range = document.selection.createRange();7var element = document.getElementById("someElement");8range.selectNode(element);9var range = document.selection.createRange();10var element = document.getElementById("someElement");11range.selectNodeContents(element);12var range = document.selection.createRange();13var endPoint = "StartToStart";14var element = document.getElementById("someElement");15range.setEndPoint(endPoint, element);16var range = document.selection.createRange();17var endPoint = "StartToStart";18var element = document.getElementById("someElement");19var offset = 0;20range.setEndPoint(endPoint, element, offset);21var range = document.selection.createRange();22var startContainer = range.startContainer;23var range = document.selection.createRange();24var startOffset = range.startOffset;25var range = document.selection.createRange();26var text = range.toString();27var range = document.selection.createRange();28var endPoint = "StartToStart";29var element = document.getElementById("someElement");30var compare = range.compareEndPoints(endPoint, element);31var range = document.selection.createRange();32var endPoint = "StartToStart";33var element = document.getElementById("someElement");34var offset = 0;35var compare = range.compareEndPoints(endPoint, element, offset);36var range = document.selection.createRange();37var element = document.getElementById("someElement");38var compare = range.isEqual(element);39var range = document.selection.createRange();40var unit = "character";
Using AI Code Generation
1var range = document.createRange();2var textNode = document.createTextNode("Hello World");3document.body.appendChild(textNode);4range.selectNode(textNode);5var wptextRange = new WPTextrange(range);6var isAncestorContainer = wptextRange.isAncestorContainer();7console.log(isAncestorContainer);8var range = document.createRange();9var textNode = document.createTextNode("Hello World");10document.body.appendChild(textNode);11range.selectNode(textNode);12var wptextRange = new WPTextrange(range);13var isCollapsed = wptextRange.isCollapsed();14console.log(isCollapsed);15var range = document.createRange();16var textNode = document.createTextNode("Hello World");17document.body.appendChild(textNode);18range.selectNode(textNode);19var wptextRange = new WPTextrange(range);20var isCollapsed = wptextRange.isEqual();21console.log(isCollapsed);22var range = document.createRange();23var textNode = document.createTextNode("Hello World");24document.body.appendChild(textNode);25range.selectNode(textNode);26var wptextRange = new WPTextrange(range
Using AI Code Generation
1var range = document.selection.createRange();2var parent = range.parentElement();3var ancestor = range.isAncestorContainer(parent);4if (ancestor)5{6 alert("The parent is an ancestor of the range");7}8{9 alert("The parent is not an ancestor of the range");10}
Using AI Code Generation
1var range = document.createRange();2var textNode = document.createTextNode("Hello World!");3var element = document.createElement("div");4element.appendChild(textNode);5document.body.appendChild(element);6range.selectNode(textNode);7var result = range.isAncestorContainer(element);8var range = document.createRange();9var textNode = document.createTextNode("Hello World!");10var element = document.createElement("div");11element.appendChild(textNode);12document.body.appendChild(element);13range.selectNode(textNode);14var result = range.isPointInRange(element, 5);15var range = document.createRange();16var textNode = document.createTextNode("Hello World!");17var element = document.createElement("div");18element.appendChild(textNode);19document.body.appendChild(element);20range.selectNodeContents(element);21var result = range.toString();22var range = document.createRange();23var textNode = document.createTextNode("Hello World!");24var element = document.createElement("div");25element.appendChild(textNode);26document.body.appendChild(element);27range.selectNodeContents(element);28range.surroundContents(document.createElement("div"));29var range = document.createRange();30var textNode = document.createTextNode("Hello World!");31var element = document.createElement("div");32element.appendChild(textNode);33document.body.appendChild(element);34range.selectNodeContents(element);35var range2 = document.createRange();36range2.selectNodeContents(element);37var result = range.compareBoundaryPoints(range2.START_TO_START, range2);38var range = document.createRange();39var textNode = document.createTextNode("Hello World!");40var element = document.createElement("div");41element.appendChild(textNode);42document.body.appendChild(element);43range.selectNodeContents(element);44var result = range.cloneContents();45var range = document.createRange();46var result = range.cloneRange();47var range = document.createRange();
Using AI Code Generation
1var range = document.createRange();2var selection = document.getSelection();3range.selectNodeContents(document.body);4selection.removeAllRanges();5selection.addRange(range);6var textRange = range.duplicate();7var ancestor = textRange.parentElement();8var isAncestorContainer = textRange.isAncestorContainer(ancestor);9alert(isAncestorContainer);10> + var range = document.createRange();11> + var selection = document.getSelection();12> + range.selectNodeContents(document.body);13> + selection.removeAllRanges();14> + selection.addRange(range);15> + var textRange = range.duplicate();16> + var ancestor = textRange.parentElement();17> + var isAncestorContainer = textRange.isAncestorContainer(ancestor);18> + shouldBe("isAncestorContainer", "true");19> + }20> +}21> + var range = document.createRange();22> + var selection = document.getSelection();23> + range.selectNodeContents(document.body);24> + selection.removeAllRanges();25> + selection.addRange(range);26> + var textRange = range.duplicate();27> + var ancestor = textRange.parentElement();28> + var isAncestorContainer = textRange.isAncestorContainer(ancestor);29> + shouldBe("isAncestorContainer", "true");30> + }31> +}32> + var range = document.createRange();33> + var selection = document.getSelection();
Using AI Code Generation
1function isAncestorContainer(wptextRange) {2 if (wptextRange.startContainer) {3 if (wptextRange.startContainer.contains(wptextRange.endContainer)) {4 return true;5 }6 }7 return false;8}9function isAncestorContainer(wptextRange) {10 if (wptextRange.startContainer) {11 if (wptextRange.startContainer.contains(wptextRange.endContainer)) {12 return true;13 }14 }15 return false;16}17function isAncestorContainer(wptextRange) {18 if (wptextRange.startContainer) {19 if (wptextRange.startContainer.contains(wptextRange.endContainer)) {20 return true;21 }22 }23 return false;24}25function isAncestorContainer(wptextRange) {26 if (wptextRange.startContainer) {27 if (wptextRange.startContainer.contains(wptextRange.endContainer)) {28 return true;29 }30 }31 return false;32}
Using AI Code Generation
1var ele = document.body;2var child = document.firstChild;3var result = wptoolkit.isAncestorContainer(ele, child);4var ele = document.body;5var child = document.body;6var result = wptoolkit.isAncestorContainer(ele, child);7var ele = document.body;8var child = document.firstChild;9var result = wptoolkit.isAncestorContainer(ele, child);10var ele = document.body;11var child = document.body;12var result = wptoolkit.isAncestorContainer(ele, child);13var ele = document.body;14var child = document.firstChild;15var result = wptoolkit.isAncestorContainer(ele, child);16var ele = document.body;17var child = document.body;18var result = wptoolkit.isAncestorContainer(ele, child);19var ele = document.body;20var child = document.firstChild;21var result = wptoolkit.isAncestorContainer(ele, child);22var ele = document.body;23var child = document.body;24var result = wptoolkit.isAncestorContainer(ele, child);25var ele = document.body;26var child = document.firstChild;27var result = wptoolkit.isAncestorContainer(ele, child);28var ele = document.body;29var child = document.body;
Using AI Code Generation
1var selection = document.getSelection();2var range = selection.getRangeAt(0);3var startContainer = range.startContainer;4var body = document.body;5var isAncestor = wpteditor.isAncestorContainer(body, startContainer);6if(isAncestor) {7 var text = range.toString();8 if(text == "") {9 var parent = startContainer.parentElement;10 if(parent.tagName == "P") {11 text = parent.innerHTML;12 }13 }14 alert(text);15}16var selection = document.getSelection();17var range = selection.getRangeAt(0);18var startContainer = range.startContainer;19var element = document.getElementById("element");20var isAncestor = wpteditor.isAncestorContainer(element, startContainer);21if(isAncestor) {22 var text = range.toString();23 if(text == "") {24 var parent = startContainer.parentElement;25 if(parent.tagName == "P") {26 text = parent.innerHTML;27 }28 }29 alert(text);30}
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!!