Best JavaScript code snippet using wpt
Utils.ts
Source:Utils.ts
...236 let node = range.startContainer;237 while (isEffectivelyContained(node.parentNode, range)) {238 node = node.parentNode;239 }240 let stop = nextNodeDescendants(range.endContainer);241 let nodeList = [];242 while (isBefore(node, stop)) {243 if (isEffectivelyContained(node, range)244 && condition(node)) {245 nodeList.push(node);246 node = nextNodeDescendants(node);247 continue;248 }249 node = nextNode(node);250 }251 return nodeList;252}253// è·å¾ææææå
å«çèç¹ list254function getAllEffectivelyContainedNodes(range: Range, condition: Function) {255 if (typeof condition == "undefined") {256 condition = function () { return true };257 }258 let node = range.startContainer;259 while (isEffectivelyContained(node.parentNode, range)) {260 node = node.parentNode;261 }262 let stop = nextNodeDescendants(range.endContainer);263 let nodeList = [];264 while (isBefore(node, stop)) {265 if (isEffectivelyContained(node, range)266 && condition(node)) {267 nodeList.push(node);268 }269 node = nextNode(node);270 }271 return nodeList;272}273// ä¸ä¸ªå¯ä»¥è¢«ä¿®æ¹çå
ç´ 274// å¦ææ¯ "B", "EM", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U" , é¤äºæ ·å¼æ²¡æä»»ä½å
¶ä»çå±æ§275// é¤äº Font å
ç´ ç style color face size ç276// é¤äº A å
ç´ ç href277// å
¶ä»é½ä¸å¯ä»¥278function isModifiableElement(node): boolean {279 if (!isHtmlElement(node)) {280 return false;281 }282 if (["B", "EM", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"].includes(node.tagName)) {283 if (node.attributes.length == 0) {284 return true;285 }286 if (node.attributes.length == 1287 && node.hasAttribute("style")) {288 return true;289 }290 }291 if (node.tagName == "FONT" || node.tagName == "A") {292 let numAttrs = node.attributes.length;293 if (node.hasAttribute("style")) {294 numAttrs--;295 }296 if (node.tagName == "FONT") {297 if (node.hasAttribute("color")) {298 numAttrs--;299 }300 if (node.hasAttribute("face")) {301 numAttrs--;302 }303 if (node.hasAttribute("size")) {304 numAttrs--;305 }306 }307 if (node.tagName == "A"308 && node.hasAttribute("href")) {309 numAttrs--;310 }311 if (numAttrs == 0) {312 return true;313 }314 }315 return false;316}317// ä¸ä¸ªç®åçå¯ä»¥è¢«ä¿®æ¹çå
ç´ 318function isSimpleModifiableElement(node) {319 // ä¸ä¸ªç®åçå¯ä¿®æ¹å
ç´ æ¯HTMLå
ç´ ï¼å
¶è³å°å
·æ以ä¸ä¸é¡¹320 if (!isHtmlElement(node)) {321 return false;322 }323 // åªæè¿äºå
ç´ å¯è½æ¯ç®åçå¯ä¿®æ¹å
ç´ ã324 if (["A", "B", "EM", "FONT", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"].includes(node.tagName)) {325 return false;326 }327 // å¦ææ¯ä¸ä¸ª a, b, em, font, i, s, span, strike, strong, sub, sup, u å
ç´ ï¼å¹¶ä¸æ²¡æå
ç´ å±æ§328 if (node.attributes.length == 0) {329 return true;330 }331 // å¦æå®å
·æå¤ä¸ªå±æ§ï¼åæ¤ä¹åçæææä½é½ä¼å¤±è´¥ã332 if (node.attributes.length > 1) {333 return false;334 }335 // å®æ¯ä¸ä¸ªa , b, em, font, i, s, span, str, strike, strong, sub, sup æ uå
ç´ ï¼336 // å
¶å
ç´ æ°å¥½å
·æä¸ä¸ª style å±æ§, ä½æ¯æ²¡æ设置设置CSSå±æ§ï¼å
æ¬æ æææ æ³è¯å«çå±æ§ï¼ã337 // 请å¿å°è¯æ æææ æ³è¯å«çå
容ã338 if (node.hasAttribute("style")339 && node.style.length == 0) {340 return true;341 }342 // å¦ææ¯ä¸ä¸ª A å
ç´ ï¼ å¹¶ä¸åªæ href å±æ§ä¹ç®æ¯343 if (node.tagName == "A"344 && node.hasAttribute("href")) {345 return true;346 }347 // å¦ææ¯ font å
ç´ å¹¶ä¸åªæä¸ä¸ªå±æ§ï¼color | face | size348 if (node.tagName == "FONT"349 && (node.hasAttribute("color")350 || node.hasAttribute("face")351 || node.hasAttribute("size")352 )) {353 return true;354 }355 // å¦ææ¯ä¸ä¸ª b | strong å
ç´ å¹¶ä¸åªæä¸ä¸ªå±æ§ style 并ä¸åªæä¸ä¸ªæ ·å¼å±æ§ font-weight356 if ((node.tagName == "B" || node.tagName == "STRONG")357 && node.hasAttribute("style")358 && node.style.length == 1359 && node.style.fontWeight != "") {360 return true;361 }362 // å¦ææ¯ä¸ä¸ª i | em å
ç´ å¹¶ä¸åªæä¸ä¸ªå±æ§ style 并ä¸åªæä¸ä¸ªæ ·å¼å±æ§ font-style363 if ((node.tagName == "I" || node.tagName == "EM")364 && node.hasAttribute("style")365 && node.style.length == 1366 && node.style.fontStyle != "") {367 return true;368 }369 // å¦ææ¯ a | font | span 并ä¸åªæä¸ä¸ªå±æ§ styleï¼å¹¶ä¸å®ç text-decoration 为空370 if ((node.tagName == "A" || node.tagName == "FONT" || node.tagName == "SPAN")371 && node.hasAttribute("style")372 && node.style.length == 1373 && node.style.textDecoration == "") {374 return true;375 }376 // å¦ææ¯ä¸ä¸ª "A", "FONT", "S", "SPAN", "STRIKE", "U" å
ç´ ï¼å¹¶ä¸æ style å±æ§377 // 并ä¸ä»
åªè®¾ç½®äºä¸ä¸ªæ ·å¼ "line-through", "underline", "overline", "none" ä¸çä¹ä¸378 // å¥æªç length æ£æ¥æ¯ä¸ºäºé对 Firefox åçè°æ´ï¼èµ° 8.02a å¼å§ççæ¬379 if (["A", "FONT", "S", "SPAN", "STRIKE", "U"].indexOf(node.tagName) != -1380 && node.hasAttribute("style")381 && (node.style.length == 1382 || (node.style.length == 4383 && "MozTextBlink" in node.style384 && "MozTextDecorationColor" in node.style385 && "MozTextDecorationLine" in node.style386 && "MozTextDecorationStyle" in node.style)387 || (node.style.length == 4388 && "MozTextBlink" in node.style389 && "textDecorationColor" in node.style390 && "textDecorationLine" in node.style391 && "textDecorationStyle" in node.style)392 )393 && (node.style.textDecoration == "line-through"394 || node.style.textDecoration == "underline"395 || node.style.textDecoration == "overline"396 || node.style.textDecoration == "none"397 )) {398 return true;399 }400}401// æ¯å¦æ¯ä¸ä¸ªå¯ç¼è¾å¯ä»¥æ ¼å¼åçå¯è§èç¹ï¼å®å¯ä»¥æ¯ TextNodeï¼ImgNode æ brNode402function isFormattableNode(node) {403 return isEditable(node)404 && isVisible(node)405 && (node.nodeType == Node.TEXT_NODE406 || isHtmlElement(node, ["img", "br"]));407}408//409/**410 * @description æ¯å级å
ç´ å411 * @param node412 * @returns boolean413 */414function isBlockNode(node) {415 const verify = ['inline', 'inline-block', 'inline-table', 'none']416 return node417 && ((node.nodeType === Node.ELEMENT_NODE && verify.includes(getComputedStyle(node).display) === false)418 || node.nodeType === Node.DOCUMENT_NODE419 || node.nodeType === Node.DOCUMENT_FRAGMENT_NODE)420}421/**422 * @description å
èèç¹æ¯ä¸æ¯åèç¹çèç¹423 * @param node424 * @returns425 */426function isInlineNode(node) {427 return node && !isBlockNode(node);428}429/**430 * @description æ¯å¦å¯ç¼è¾å
ç´ 431 * 1.ä¸ä¸ªç¨æ·å¯ç¼è¾çå
ç´ ï¼ä¾å¦ä¸ä¸ªä½¿ç¨ contenteditable çHTMLå
ç´ ï¼ææ¯å¨å¯ç¨äº designMode ç Document çåå
ç´ ï¼432 * @param node433 * @returns boolean434 *435 */436function isEditingHost(node) {437 return node438 && isHtmlElement(node)439 && (node.contentEditable === 'true'440 || (node.parentNode441 && node.parentNode.nodeType === Node.DOCUMENT_NODE442 && node.parentNode.designMode === 'on'));443}444/**445 * @description æ¯å¦å¯ä»¥è¢«ç¼è¾446 * @param node447 * @returns boolean448 * 1.å¦æå®æ¯ä¸ä¸ªå¯ç¼è¾çèç¹ï¼ä½ä¸æ¯å¯ç¼è¾ç主è¦æ ¹èç¹449 * 2.å®ç¡®å®æ²¡æå° contenteditable å±æ§è®¾ç½®ä¸º false ç¶æ450 * 3.å
¶ç¶çº§æ¯ç¼è¾æ ¹èç¹æå¯ç¼è¾ç451 * 4.并ä¸å®æ¯ä¸ä¸ª HTML å
ç´ ï¼æè
æ¯ä¸ä¸ª svg æ math å
ç´ ï¼æè
å®ä¸æ¯ Element ä¸å
¶ç¶å
ç´ æ¯ HTML å
ç´ ã452 */453function isEditable(node): boolean {454 return node455 && !isEditingHost(node)456 && (node.nodeType !== Node.ELEMENT_NODE || node.contentEditable !== 'false')457 && (isEditingHost(node.parentNode) || isEditable(node.parentNode))458 && (isHtmlElement(node)459 || (node.nodeType === Node.ELEMENT_NODE && node.namespaceURI === 'http://www.w3.org/2000/svg' && node.localName === 'svg')460 || (node.nodeType === Node.ELEMENT_NODE && node.namespaceURI === 'http://www.w3.org/1998/Math/MathML' && node.localName === 'math')461 || (node.nodeType !== Node.ELEMENT_NODE && isHtmlElement(node.parentNode)));462}463/**464 * @description æ¯å¦æå¯ç¼è¾çå代èç¹465 * @param node466 * @returns467 */468function hasEditableDescendants(node) {469 for (let i = 0; i < node.childNodes.length; i++) {470 if (isEditable(node.childNodes[i])471 || hasEditableDescendants(node.childNodes[i])) {472 return true;473 }474 }475 return false;476}477/**478 * @description479 * 1. å¦æèç¹ä¸å¯ç¼è¾ï¼é£ä¹ç¼è¾æ ¹èç¹ä¸º null480 * 2. å¦æèç¹æ¯ç¼è¾æ ¹èç¹ï¼åè¿åèç¹æ¬èº«481 * 3. å¦æèç¹æ¯å¯ç¼è¾çï¼åå¯ç¼è¾çæ ¹èç¹ä¸ºè¯¥èç¹çæè¿ç¥å
ã482 * @param node483 * @returns484 */485function getEditingHostOf(node) {486 if (isEditingHost(node)) {487 return node;488 } else if (isEditable(node)) {489 let ancestor = node.parentNode;490 while (!isEditingHost(ancestor)) {491 ancestor = ancestor.parentNode;492 }493 return ancestor;494 } else {495 return null;496 }497}498/**499 * @description æ¯å¦æ¥æå
±åçå¯ç¼è¾æ ¹èç¹ç¥å
500 * @param node1501 * @param node2502 * @returns503 */504function inSameEditingHost(node1, node2) {505 return getEditingHostOf(node1)506 && getEditingHostOf(node1) == getEditingHostOf(node2);507}508/**509 * @description æ¯å¦æ¯æå çç©ºæ ¼510 * @param br511 * @returns512 */513function isCollapsedLineBreak(br) {514 if (!isHtmlElement(br, "br")) {515 return false;516 }517 // å¨å®åé¢æ·»å ä¸ä¸ªzwsp(é¶å®½ç©ºæ ¼)ï¼ççè¿æ¯å¦ä¼æ¹åæè¿çéå
èç¶çº§çé«åº¦ã518 // 注æï¼è¿å®é
ä¸æ¯ä¸å¯é çï¼å 为ç¶çº§å¯è½æåºå®çé«åº¦æå
¶ä»ä¸è¥¿ã519 let ref = br.parentNode;520 while (getComputedStyle(ref).display == "inline") {521 ref = ref.parentNode;522 }523 let refStyle = ref.hasAttribute("style") ? ref.getAttribute("style") : null;524 ref.style.height = "auto";525 ref.style.maxHeight = "none";526 ref.style.minHeight = "0";527 let space = document.createTextNode("\u200b");528 let origHeight = ref.offsetHeight;529 if (origHeight == 0) {530 throw "isCollapsedLineBreak: original height is zero, bug?";531 }532 br.parentNode.insertBefore(space, br.nextSibling);533 let finalHeight = ref.offsetHeight;534 space.parentNode.removeChild(space);535 if (refStyle === null) {536 ref.setAttribute("style", "");537 ref.removeAttribute("style");538 } else {539 ref.setAttribute("style", refStyle);540 }541 // å¦æzwsp(é¶å®½ç©ºæ ¼)没æå建ä¸æ¡å
¨æ°çç«çº¿ caretï¼èåªæ¯ä½¿ä¸æ¡ç°æçcaretç¨å¾®é«ä¸ç¹ï¼é£ä¹å¯ä»¥å
许ã542 // firefox6.0 å¨ç¬¬ä¸è¡æ¯ç²ä½æ¶æ¾ç¤ºè¿ç§è¡ä¸ºã543 return origHeight < finalHeight - 5;544}545/**546 * @description æ¯å¦æ¯å¤ä½æ æç br547 * å¤ä½çæ¢è¡ç¬¦æ¯ä¸å
·æè§è§ææçbrï¼å 为ä»DOMä¸å°å
¶å é¤ä¸ä¼æ¹åå¸å±ï¼é¤äºæ¯ä½ä¸ºliçå¯ä¸å代çbr548 * @param br549 * @returns550 */551function isExtraneousLineBreak(br) {552 if (!isHtmlElement(br, "br")) {553 return false;554 }555 if (isHtmlElement(br.parentNode, "li")556 && br.parentNode.childNodes.length == 1) {557 return false;558 }559 // 使æ¢è¡ç¬¦æ¶å¤±ï¼ççè¿æ¯å¦ä¼æ¹ååçé«åº¦ã560 // æ¯çï¼è¿æ¯ä¸ä¸ª css hackãæ们å¿
é¡»éç½®åèèç¹ä¸çé«åº¦çï¼å 为å¦åå®çé«åº¦ä¸ä¼æ¹åï¼å¦æå®ä¸æ¯èªå¨çã561 let ref = br.parentNode;562 while (getComputedStyle(ref).display == "inline") {563 ref = ref.parentNode;564 }565 let refStyle = ref.hasAttribute("style") ? ref.getAttribute("style") : null;566 ref.style.height = "auto";567 ref.style.maxHeight = "none";568 ref.style.minHeight = "0";569 let brStyle = br.hasAttribute("style") ? br.getAttribute("style") : null;570 let origHeight = ref.offsetHeight;571 if (origHeight == 0) {572 throw "isExtraneousLineBreak: original height is zero, bug?";573 }574 br.setAttribute("style", "display:none");575 let finalHeight = ref.offsetHeight;576 if (refStyle === null) {577 ref.setAttribute("style", "");578 ref.removeAttribute("style");579 } else {580 ref.setAttribute("style", refStyle);581 }582 if (brStyle === null) {583 br.removeAttribute("style");584 } else {585 br.setAttribute("style", brStyle);586 }587 return origHeight == finalHeight;588}589/**590 * @description æ¯å¦æ¯ç©ºç½èç¹?591 * 空ç½èç¹æ¯å
¶æ°æ®ä¸ºç©ºå符串çTextèç¹; æå
¶æ°æ®ä»
ç±ä¸ä¸ªæå¤ä¸ªå¶è¡¨ç¬¦ï¼0x0009ï¼ï¼æ¢è¡ç¬¦ï¼0x000Aï¼ï¼å车符ï¼0x000Dï¼å/æ ç©ºæ ¼ï¼0x0020ï¼ï¼å¹¶ä¸å
¶ç¶çº§æ¯ä¸ä¸ªå
ç´ ï¼å
¶ 'white-space' ç解æå¼ä¸º 'normal' æâ nowrapâï¼æè
å
¶æ°æ®ä»
ç±ä¸ä¸ªæå¤ä¸ªå¶è¡¨ç¬¦ï¼0x0009ï¼ç»æçTextèç¹ï¼å车符ï¼0x000D ï¼å/æç©ºæ ¼ï¼0x0020ï¼ï¼å¹¶ä¸å
¶ç¶é¡¹æ¯Elementï¼å
¶å¯¹ 'white-space' ç解æå¼ä¸º 'pre-line'592 * @param node593 * @returns594 */595function isWhitespaceNode(node) {596 return node597 && node.nodeType == Node.TEXT_NODE598 && (node.data == ""599 || (600 /^[\t\n\r ]+$/.test(node.data)601 && node.parentNode602 && node.parentNode.nodeType == Node.ELEMENT_NODE603 && ["normal", "nowrap"].indexOf(getComputedStyle(node.parentNode).whiteSpace) != -1)604 || (605 /^[\t\r ]+$/.test(node.data)606 && node.parentNode607 && node.parentNode.nodeType == Node.ELEMENT_NODE608 && getComputedStyle(node.parentNode).whiteSpace == "pre-line"609 ));610}611/**612 * @description å¦æ以ä¸ç®æ³è¿åtrueï¼åèç¹æ¯æå ç空ç½èç¹613 * @param node614 * @returns615 *616 */617function isCollapsedWhitespaceNode(node) {618 // å¦æä¸æ¯ç©ºç½èç¹è¿å619 if (!isWhitespaceNode(node)) {620 return false;621 }622 // å¦æ node ç data 为空ï¼è¿å true623 if (node.data == "") {624 return true;625 }626 // æ¥çç¥å
èç¹627 let ancestor = node.parentNode;628 // å¦æç¥å
èç¹ä¹æ¯ç©ºçï¼è¿å ture629 if (!ancestor) {630 return true;631 }632 // å¦æç¥å
èç¹çå±æ§ display æ¾ç¤ºä¸º none, è¿å ture633 if (getAncestors(node).some(function (ancestor) {634 return ancestor.nodeType == Node.ELEMENT_NODE635 && getComputedStyle(ancestor).display == "none";636 })) {637 return true;638 }639 // å½ç¥å
ä¸æ¯åèç¹ä¸å
¶ç¶èç¹ä¸ä¸ºç©ºæ¶ï¼å°ç¥å
设置为å
¶ç¶èç¹640 while (!isBlockNode(ancestor)641 && ancestor.parentNode) {642 ancestor = ancestor.parentNode;643 }644 // å建å¼ç¨èç¹645 let reference = node;646 // è reference æ¯ç¥å
çå代: ååæ¥è¯¢647 while (reference != ancestor) {648 // 让 reference ææ 顺åºä½ä¸ºå
¶åé¢çèç¹.649 reference = previousNode(reference);650 // å¦ææ¯å级èç¹æè
br è¿å true651 if (isBlockNode(reference)652 || isHtmlElement(reference, "br")) {653 return true;654 }655 // å¦æ reference æ¯ä¸æ¯ç©ºæ ¼èç¹ç Text èç¹ æ imgï¼è¯·éåºæ¤å¾ªç¯656 if ((reference.nodeType == Node.TEXT_NODE && !isWhitespaceNode(reference))657 || isHtmlElement(reference, "img")) {658 break;659 }660 }661 // reference = node662 reference = node;663 // reference æ¯ç¥å
çå代 ååæ¥è¯¢664 let stop = nextNodeDescendants(ancestor);665 while (reference != stop) {666 // 令 reference 为æ åçèç¹ï¼å¦æ没æè¿æ ·çèç¹ï¼å为nullã667 reference = nextNode(reference);668 // å¦æ reference æ¯åèç¹æ brï¼åè¿åtrue669 if (isBlockNode(reference)670 || isHtmlElement(reference, "br")) {671 return true;672 }673 // å¦æ reference æ¯ä¸æ¯ç©ºæ ¼èç¹çTextèç¹æimgï¼è¯·éåºæ¤å¾ªç¯ã674 if ((reference && reference.nodeType == Node.TEXT_NODE && !isWhitespaceNode(reference))675 || isHtmlElement(reference, "img")) {676 break;677 }678 }679 // é½ä¸æ»¡è¶³è¿å false680 return false;681}682/**683 * @description å¤æèç¹æ¯å¯è§çï¼å¦ææ个èç¹æ¯åèç¹ï¼æä¸æ¯æå ç©ºæ ¼èç¹çææ¬èç¹ï¼æimgï¼æä¸æ¯æ å
³æ¢è¡ç¬¦çbrï¼æä»»ä½å
·æå¯è§å代çèç¹ï¼å该èç¹æ¯å¯è§çï¼æé¤å
·æç¥å
容å¨å
ç´ ä¸å
¶ 'display' å±æ§è§£æå¼ä¸º 'none' çä»»ä½èç¹684 * @param node685 * @returns boolean686 */687function isVisible(node) {688 if (!node) {689 return false;690 }691 if (getAncestors(node).concat(node)692 .filter(function (node) { return node.nodeType == Node.ELEMENT_NODE })693 .some(function (node) { return getComputedStyle(node).display == "none" })) {694 return false;695 }696 if (isBlockNode(node)697 || (node.nodeType == Node.TEXT_NODE && !isCollapsedWhitespaceNode(node))698 || isHtmlElement(node, "img")699 || (isHtmlElement(node, "br") && !isExtraneousLineBreak(node))) {700 return true;701 }702 for (let i = 0; i < node.childNodes.length; i++) {703 if (isVisible(node.childNodes[i])) {704 return true;705 }706 }707 return false;708}709/**710 * @description èç¹æ¯ä¸å¯è§ç711 * @param node712 * @returns713 */714function isInvisible(node) {715 return node && !isVisible(node);716}717/**718 * @description æ¯æå çå级å±æ§719 * æå çå级å±æ§å¯ä»¥æ¯ä¸æ¯æ å
³çæ¢è¡ç¬¦çæå æ¢è¡ç¬¦ï¼ä¹å¯ä»¥æ¯ä½ä¸ºå
èèç¹çå
ç´ ï¼å
¶åå
ç´ é½æ¯ä¸å¯è§çææå çå级å±æ§ï¼å¹¶ä¸è³å°æä¸ä¸ªåå
ç´ æ¯æå çå级å±æ§ã720 * @param node721 * @returns722 */723function isCollapsedBlockProp(node) {724 if (isCollapsedLineBreak(node)725 && !isExtraneousLineBreak(node)) {726 return true;727 }728 if (!isInlineNode(node)729 || node.nodeType != Node.ELEMENT_NODE) {730 return false;731 }732 let hasCollapsedBlockPropChild = false;733 for (let i = 0; i < node.childNodes.length; i++) {734 if (!isInvisible(node.childNodes[i])735 && !isCollapsedBlockProp(node.childNodes[i])) {736 return false;737 }738 if (isCollapsedBlockProp(node.childNodes[i])) {739 hasCollapsedBlockPropChild = true;740 }741 }742 return hasCollapsedBlockPropChild;743}744/**745 * @description è·å¾ç¸é»çä¸ä¸ªæè¿ç node èç¹746 * @param node747 * @returns748 */749function nextNode(node: Node) {750 if (node.hasChildNodes()) {751 return node.firstChild;752 }753 return nextNodeDescendants(node);754}755/**756 * @description ä¸ä¸ªèç¹çå代èç¹757 * @param node758 * @returns759 */760function nextNodeDescendants(node: Node) {761 while (node && !node.nextSibling) {762 node = node.parentNode;763 }764 if (!node) {765 return null;766 }767 return node.nextSibling;768}769/**770 * @description åä¸ä¸ªèç¹çå代èç¹771 * @param node772 * @returns773 */774function previousNode(node) {775 if (node.previousSibling) {776 node = node.previousSibling;777 while (node.hasChildNodes()) {778 node = node.lastChild;779 }780 return node;781 }782 if (node.parentNode783 && node.parentNode.nodeType == Node.ELEMENT_NODE) {784 return node.parentNode;785 }786 return null;787}788/**789 * @description å¦æç¥å
æ¯å代çç¥å
ï¼åè¿åtrueï¼å¦åè¿åfalseã790 */791function isAncestor(ancestor, descendant) {792 return ancestor793 && descendant794 && Boolean(ancestor.compareDocumentPosition(descendant) & Node.DOCUMENT_POSITION_CONTAINED_BY);795}796/**797 * @description æ¯å¦æ¯å代çç¥å
798 * @param ancestor799 * @param descendant800 * @returns801 */802function isAncestorContainer(ancestor, descendant) {803 return (ancestor || descendant)804 && (ancestor == descendant || isAncestor(ancestor, descendant));805}806/**807 * @description æ¯å¦æ¯å代èç¹ï¼å
¶ä»æ
åµè¿å false808 * @param descendant809 * @param ancestor810 * @returns811 */812function isDescendant(descendant, ancestor) {813 return ancestor814 && descendant815 && Boolean(ancestor.compareDocumentPosition(descendant) & Node.DOCUMENT_POSITION_CONTAINED_BY);816}817/**818 * @description æ¯å¦å¨ node1 å¨ node2 ä¹å è¿å ture819 * @param node1820 * @param node2821 * @returns822 */823function isBefore(node1, node2) {824 return Boolean(node1.compareDocumentPosition(node2) & Node.DOCUMENT_POSITION_FOLLOWING);825}826/**827 * @description æ¯å¦å¨ node1 å¨ node2 ä¹å è¿å ture828 * @param node1829 * @param node2830 * @returns831 */832function isAfter(node1, node2) {833 return Boolean(node1.compareDocumentPosition(node2) & Node.DOCUMENT_POSITION_PRECEDING);834}835/**836 * @description è·å¾ææçç¥å
837 * @param node838 * @returns839 */840function getAncestors(node) {841 const ancestors = [];842 while (node.parentNode) {843 ancestors.unshift(node.parentNode);844 node = node.parentNode;845 }846 return ancestors;847}848/**849 * @description è·å¾å
å«èªå·±çææç¥å
850 * @param node851 * @returns852 */853function getInclusiveAncestors(node) {854 return getAncestors(node).concat(node);855}856/**857 * @description è·å¾ææå代èç¹858 * @param node859 * @returns860 */861function getDescendants(node) {862 const descendants = [];863 const stop = nextNodeDescendants(node);864 while ((node = nextNode(node)) && node != stop) {865 descendants.push(node);866 }867 return descendants;868}869/**870 * @description è·å¾å
å«èªå·±çææå代èç¹871 * @param node872 * @returns873 */874function getInclusiveDescendants(node) {875 return [node].concat(getDescendants(node));876}877// 转æ¢ç¹å«çå±æ§878function convertProperty(property) {879 // Special-case for now880 const map = {881 'fontFamily': 'font-family',882 'fontSize': 'font-size',883 'fontStyle': 'font-style',884 'fontWeight': 'font-weight',885 'textDecoration': 'text-decoration',886 };887 if (typeof map[property] != 'undefined') {888 return map[property];889 }890 return property;891}892// è¿åç»å®CSS大å°ç<font size = X>å¼ï¼å¦æåå¨ï¼åè¿åundefined没æã893function cssSizeToLegacy(cssVal) {894 return {895 "x-small": 1,896 "small": 2,897 "medium": 3,898 "large": 4,899 "x-large": 5,900 "xx-large": 6,901 "xxx-large": 7902 }[cssVal];903}904// æ ¹æ®ç»å®çæ§å¤§å°è¿åCSS大å°ã905function legacySizeToCss(legacyVal) {906 return {907 1: "x-small",908 2: "small",909 3: "medium",910 4: "large",911 5: "x-large",912 6: "xx-large",913 7: "xxx-large",914 }[legacyVal];915}916// HTMLä¸ç âæ¹åæ§âã æä¸ç¨æ
å¿éHTMLå
ç´ ã917// âå
ç´ çæ¹åæ§æ¯'ltr'æ'rtl'ï¼å¹¶ä¸æ ¹æ®ä»¥ä¸éå½çæ¥éª¤ç¡®å®â918function getDirectionality(element) {919 // å¦æå
ç´ çæçæ¹åæ§æ¯ ltr é£ä¹ç»æä¹æ¯ ltr920 if (element.dir == "ltr") {921 return "ltr";922 }923 // å¦æå
ç´ çæçæ¹åæ§æ¯ rtl é£ä¹ç»æä¹æ¯ rtl924 if (element.dir == "rtl") {925 return "rtl";926 }927 // å¦æå
ç´ çæ ¹èç¹æ¯ htmlï¼é£ä¹ç´æ¥è·³è¿å¤æï¼è¿åé»è®¤å¼928 if (!isHtmlElement(element.parentNode)) {929 return "ltr";930 }931 // å¦æ没æå®ä¹ææ¬çæåæ¹åï¼å¯»æ¾ç¶èç¹ï¼åèç¹é»è®¤éæç¶èç¹çæ¹åæ§932 return getDirectionality(element.parentNode);933}934// è·å¾ node å¨å½åèç¹å±çº§ç index935function getNodeIndex(node) {936 let ret = 0;937 while (node.previousSibling) {938 ret++;939 node = node.previousSibling;940 }941 return ret;942}943/**944 * @description è·å¾ node ç length945 * [ProcessingInstruction, DocumentType] é常è¿å 0946 * [Text, Comment] è¿åèªèº«ç length947 * [anthor] è¿å node's childNodes's length948 */949function getNodeLength(node) {950 switch (node.nodeType) {951 case Node.PROCESSING_INSTRUCTION_NODE:952 case Node.DOCUMENT_TYPE_NODE:953 return 0;954 case Node.TEXT_NODE:955 case Node.COMMENT_NODE:956 return node.length;957 default:958 return node.childNodes.length;959 }960}961/**962 * @description å¤æç±DOM Rangeå®ä¹ç两个边çç¹å½¼æ¤ç¸å¯¹çä½ç½®ã963 * @param nodeA964 * @param offsetA965 * @param nodeB966 * @param offsetB967 *968 */969function getPosition(nodeA, offsetA, nodeB, offsetB) {970 // å¦æèç¹Aä¸èç¹Bç¸åï¼971 // åå¦æå移éAçäºå移éBï¼åè¿åç¸ç972 // å¦æå移éAå°äºå移éBï¼åè¿åä¹å973 // å¦æå移éA大äºå移éBï¼åè¿åä¹å974 if (nodeA == nodeB) {975 if (offsetA == offsetB) {976 return "equal";977 }978 if (offsetA < offsetB) {979 return "before";980 }981 if (offsetA > offsetB) {982 return "after";983 }984 }985 // å¦æèç¹A以æ 顺åºå¨èç¹Bä¹åï¼è®¡ç®ï¼èç¹Bï¼å移éBï¼ç¸å¯¹äºï¼èç¹Aï¼å移éAï¼çä½ç½®ã986 // å¦æå®å¨ä¹åï¼åå¨è¿å afterãå¦æå¨ä¹åï¼åè¿å before987 if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_FOLLOWING) {988 let pos = getPosition(nodeB, offsetB, nodeA, offsetA);989 if (pos == "before") {990 return "after";991 }992 if (pos == "after") {993 return "before";994 }995 }996 // å¦æ nodeA æ¯ nodeB çç¥å
997 if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_CONTAINS) {998 // 计ç®åèç¹ï¼nodeB æ¯ nodeA çåèç¹999 let child = nodeB;1000 // å½ child ä¸æ¯èç¹Açå代æ¶ï¼å° child 设置为å
¶ç¶ä»£ã1001 while (child.parentNode != nodeA) {1002 child = child.parentNode;1003 }1004 // å¦æå项çç´¢å¼å°äºå移éAï¼åè¿å after1005 if (getNodeIndex(child) < offsetA) {1006 return "after";1007 }1008 }1009 return "before";1010}1011/**1012 * @description è¿åç±DOM Rangeå®ä¹ç nodeçæè¿ç¥å
ã1013 */1014function getFurthestAncestor(node) {1015 let root = node;1016 while (root.parentNode != null) {1017 root = root.parentNode;1018 }1019 return root;1020}1021/**1022 * ç±DOMèå´å®ä¹ä¸ºâå
å«â: âå¦æèç¹çæè¿ç¥å
ä¸èå´çæ ¹ç¸åï¼å¹¶ä¸ node 0 çèµ·å§ä½ç½®å¨ Range.startContainer ä¹åï¼ç»æèç¹å¨ Range.endContainer ä¹åï¼é£ä¹è¿ä¸ªèç¹è¢«èå´å
å«1023 */1024function isContained(node, range: Range) {1025 let pos1 = getPosition(node, 0, range.startContainer, range.startOffset);1026 let pos2 = getPosition(node, getNodeLength(node), range.endContainer, range.endOffset);1027 return getFurthestAncestor(node) == getFurthestAncestor(range.startContainer)1028 && pos1 == "after"1029 && pos2 == "before";1030}1031// è·å¾éåºèå´å
å
å«çææèç¹, 忽ç¥å
裹çç¥å
èç¹1032function getContainedNodes(range, condition) {1033 if (typeof condition == "undefined") {1034 condition = function () { return true };1035 }1036 let node = range.startContainer;1037 if (node.hasChildNodes()1038 && range.startOffset < node.childNodes.length) {1039 // ä¸ä¸ªåèç¹è¢«å
裹1040 node = node.childNodes[range.startOffset];1041 } else if (range.startOffset == getNodeLength(node)) {1042 // ä¸è½å
å«ä»»ä½å代1043 node = nextNodeDescendants(node);1044 } else {1045 // å¦æ没æåèç¹, 该èç¹ä¹ä¸å¯ä»¥è¢«å
å«1046 node = nextNode(node);1047 }1048 let stop = range.endContainer;1049 if (stop.hasChildNodes()1050 && range.endOffset < stop.childNodes.length) {1051 // æåä¸ä¸ªå
å«çèç¹ä¹åçèç¹æ¯åèç¹1052 stop = stop.childNodes[range.endOffset];1053 } else {1054 // å¯è½å
å«æ¤èç¹å/æå
¶æäºåèç¹1055 stop = nextNodeDescendants(stop);1056 }1057 let nodeList = [];1058 while (isBefore(node, stop)) {1059 if (isContained(node, range)1060 && condition(node)) {1061 nodeList.push(node);1062 node = nextNodeDescendants(node);1063 continue;1064 }1065 node = nextNode(node);1066 }1067 return nodeList;1068}1069// è·å¾éåºèå´å
å
å«çææèç¹, ä¸å¿½ç¥å
裹çç¥å
èç¹1070function getAllContainedNodes(range, condition) {1071 if (typeof condition == "undefined") {1072 condition = function () { return true };1073 }1074 let node = range.startContainer;1075 if (node.hasChildNodes()1076 && range.startOffset < node.childNodes.length) {1077 // ä¸ä¸ªåèç¹è¢«å
裹1078 node = node.childNodes[range.startOffset];1079 } else if (range.startOffset == getNodeLength(node)) {1080 // ä¸è½å
å«ä»»ä½å代1081 node = nextNodeDescendants(node);1082 } else {1083 // å¦æ没æåèç¹, 该èç¹ä¹ä¸å¯ä»¥è¢«å
å«1084 node = nextNode(node);1085 }1086 let stop = range.endContainer;1087 if (stop.hasChildNodes()1088 && range.endOffset < stop.childNodes.length) {1089 // æåä¸ä¸ªå
å«çèç¹ä¹åçèç¹æ¯åèç¹1090 stop = stop.childNodes[range.endOffset];1091 } else {1092 // å¯è½å
å«æ¤èç¹å/æå
¶æäºåèç¹1093 stop = nextNodeDescendants(stop);1094 }1095 let nodeList = [];1096 while (isBefore(node, stop)) {1097 if (isContained(node, range)1098 && condition(node)) {1099 nodeList.push(node);1100 }1101 node = nextNode(node);1102 }1103 return nodeList;1104}1105function normalizeColor(color) {1106 let resultCache = {}1107 if (color.toLowerCase() == "currentcolor") {...
dom-utils.ts
Source:dom-utils.ts
...220 break221 }222 }223 reference = node224 let stop = nextNodeDescendants(ancestor)225 while (reference != stop) {226 reference = nextNode(reference)227 if (isBlockNode(reference)228 || isHtmlElement(reference, 'br')) {229 return true230 }231 if (reference232 && reference.nodeType == Node.TEXT_NODE233 && !isWhitespaceNode(reference)234 || isHtmlElement(reference, 'img')235 ) {236 break237 }238 }239 return false240}241export function isVisible(node) {242 if (!node) return false243 if (244 getAncestors(node)245 .concat(node)246 .filter(function (node) {247 return node.nodeType == Node.ELEMENT_NODE248 })249 .some(function (node) {250 return getComputedStyle(node).display == 'none'251 })252 ) {253 return false254 }255 if (isBlockNode(node)256 || (node.nodeType == Node.TEXT_NODE && !isCollapsedWhitespaceNode(node))257 || isHtmlElement(node, 'img')258 || (isHtmlElement(node, 'br') && !isExtraneousLineBreak(node))) {259 return true260 }261 for (let i = 0; i < node.childNodes.length; i++) {262 if (isVisible(node.childNodes[i])) return true263 }264 return false265}266export function isInvisible(node) {267 return node && !isVisible(node)268}269export function isCollapsedBlockProp(node) {270 if (isCollapsedLineBreak(node) && !isExtraneousLineBreak(node)) {271 return true272 }273 if (!isInlineNode(node) || node.nodeType != Node.ELEMENT_NODE) {274 return false275 }276 let hasCollapsedBlockPropChild = false277 for (let i = 0; i < node.childNodes.length; i++) {278 if (!isInvisible(node.childNodes[i])279 && !isCollapsedBlockProp(node.childNodes[i])280 ) {281 return false282 }283 if (isCollapsedBlockProp(node.childNodes[i])) {284 hasCollapsedBlockPropChild = true285 }286 }287 return hasCollapsedBlockPropChild288}289export function isHtmlNamespace(ns) {290 return ns === null || ns === htmlNamespace291}292export function isHtmlElement(node, tags?) {293 if (typeof tags == 'string') {294 tags = [tags]295 }296 if (typeof tags == 'object') {297 tags = tags.map(function (tag) {298 return tag.toUpperCase()299 })300 }301 return node302 && node.nodeType == Node.ELEMENT_NODE303 && isHtmlNamespace(node.namespaceURI)304 && (typeof tags == 'undefined' || tags.indexOf(node.tagName) != -1)305}306export function nextNode(node) {307 if (node.hasChildNodes()) {308 return node.firstChild309 }310 return nextNodeDescendants(node)311}312export function nextNodeDescendants(node) {313 while (node && !node.nextSibling) {314 node = node.parentNode315 }316 if (!node) {317 return null318 }319 return node.nextSibling320}321export function previousNode(node) {322 if (node.previousSibling) {323 node = node.previousSibling324 while (node.hasChildNodes()) {325 node = node.lastChild326 }327 return node328 }329 if (node.parentNode330 && node.parentNode.nodeType == Node.ELEMENT_NODE) {331 return node.parentNode332 }333 return null334}335export function isAncestor(ancestor, descendant) {336 return ancestor337 && descendant338 && Boolean(ancestor.compareDocumentPosition(descendant) & Node.DOCUMENT_POSITION_CONTAINED_BY)339}340export function isAncestorContainer(ancestor, descendant) {341 return (ancestor || descendant)342 && (ancestor == descendant || isAncestor(ancestor, descendant))343}344export function isDescendant(descendant, ancestor) {345 return ancestor346 && descendant347 && Boolean(ancestor.compareDocumentPosition(descendant) & Node.DOCUMENT_POSITION_CONTAINED_BY)348}349export function isBefore(node1: Node, node2: Node) {350 return Boolean(node1.compareDocumentPosition(node2) & Node.DOCUMENT_POSITION_FOLLOWING)351}352export function isAfter(node1: Node, node2: Node) {353 return Boolean(node1.compareDocumentPosition(node2) & Node.DOCUMENT_POSITION_PRECEDING)354}355export function getAncestors(node, condition?: Function | null) {356 const ancestors = []357 if (!condition) condition = function (node) { return true }358 while (node.parentNode && condition(node)) {359 ancestors.unshift(node.parentNode)360 node = node.parentNode361 }362 return ancestors363}364export function getInclusiveAncestors(node: Node, condition?: Function) {365 return getAncestors(node, condition).concat(node)366}367export function getDescendants(node: Node) {368 const descendants = []369 let stop = nextNodeDescendants(node)370 while ((node = nextNode(node)) && node != stop) {371 descendants.push(node)372 }373 return descendants374}375export function getInclusiveDescendants(node) {376 return [node].concat(getDescendants(node))377}378export function getDirectionality(element) {379 if (element.dir == 'ltr') {380 return 'ltr'381 }382 if (element.dir == 'rtl') {383 return 'rtl'384 }385 if (!isHtmlElement(element.parentNode)) {386 return 'ltr'387 }388 return getDirectionality(element.parentNode)389}390export function getBlockNodeOf(node) {391 while (isInlineNode(node)) {392 node = node.parentNode;393 }394 return node;395}396export function getNodeIndex(node) {397 let ret = 0398 while (node.previousSibling) {399 ret++400 node = node.previousSibling;401 }402 return ret403}404export function getNodeLength(node) {405 switch (node.nodeType) {406 case Node.PROCESSING_INSTRUCTION_NODE:407 case Node.DOCUMENT_TYPE_NODE:408 return 0;409 case Node.TEXT_NODE:410 case Node.COMMENT_NODE:411 return node.length;412 default:413 return node.childNodes.length;414 }415}416function getPosition(nodeA: Node, offsetA: number, nodeB: Node, offsetB: number): string {417 if (nodeA == nodeB) {418 if (offsetA == offsetB) return 'equal'419 if (offsetA < offsetB) return 'before'420 if (offsetA > offsetB) return 'after'421 }422 if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_FOLLOWING) {423 const pos = getPosition(nodeB, offsetB, nodeA, offsetA);424 if (pos == 'before') return 'after'425 if (pos == 'after') 'before'426 }427 if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_CONTAINS) {428 let child = nodeB;429 while (child.parentNode != nodeA) child = child.parentNode430 if (getNodeIndex(child) < offsetA) return 'after';431 }432 return 'before';433}434// è·å¾æè¿çç¥å
èç¹435function getFurthestAncestor(node) {436 let root = node437 while (root.parentNode) {438 root = root.parentNode;439 }440 return root;441}442// 被å
å«å®ä¹ä¸ºå¨å¼å§èç¹ä¹åï¼å¨ç»æèç¹ä¹åï¼å°±è®¤ä¸ºè¢«å
å«443function isContained(node: Node, range: Range) {444 const startPosition = getPosition(node, 0, range.startContainer, range.startOffset);445 const endPosition = getPosition(node, getNodeLength(node), range.endContainer, range.endOffset);446 return getFurthestAncestor(node) == getFurthestAncestor(range.startContainer)447 && startPosition == 'after'448 && endPosition == 'before';449}450export function isContainedNode(node: Node, otherNode: Node) {451 return node.contains(otherNode)452}453// æ¯å¦ææ被å
å«454function isEffectivelyContained(node: Node, range: Range) {455 if (range.collapsed) return false;456 if (isContained(node, range)) return true;457 if (node == range.startContainer458 && node.nodeType == Node.TEXT_NODE459 && getNodeLength(node) != range.startOffset) {460 return true;461 }462 if (node == range.endContainer463 && node.nodeType == Node.TEXT_NODE464 && range.endOffset != 0) {465 return true;466 }467 if (node.hasChildNodes()468 && [].every.call(node.childNodes, (child: Node) => isEffectivelyContained(child, range))469 && (!isDescendant(range.startContainer, node)470 || range.startContainer.nodeType != Node.TEXT_NODE471 || range.startOffset == 0)472 && (!isDescendant(range.endContainer, node)473 || range.endContainer.nodeType != Node.TEXT_NODE474 || range.endOffset == getNodeLength(range.endContainer)475 )476 ) {477 return true;478 }479 return false;480}481// ä¸ get(All)ContainedNodes() 类似ï¼ä½ç¨äºææå
å«çèç¹ ä¸å
å« TextNode482export function getEffectivelyContainedNodes(range: Range, condition?: Function) {483 if (typeof condition == 'undefined') {484 condition = function() { return true };485 }486 let node = range.startContainer487 while (isEffectivelyContained(node.parentNode, range)) {488 node = node.parentNode489 }490 // è·å¾éåºç»æçä¸ä¸ªèç¹491 let stop = nextNodeDescendants(range.endContainer);492 const nodeList = []493 while (isBefore(node, stop)) {494 if (isEffectivelyContained(node, range)495 && condition(node)) {496 nodeList.push(node)497 node = nextNodeDescendants(node)498 continue;499 }500 node = nextNode(node);501 }502 return nodeList;503}504export function getAllEffectivelyContainedNodes(range: Range, condition?: Function) {505 if (typeof condition == 'undefined') {506 condition = function() { return true };507 }508 // è·å¾å¼å§èç¹çæ顶å±è¢«å
裹å¨éåºèå´å
çèç¹509 let node = range.startContainer;510 while (isEffectivelyContained(node.parentNode, range)) {511 node = node.parentNode;512 }513 // è·å¾éåºç»æçä¸ä¸ªèç¹514 let stop = nextNodeDescendants(range.endContainer);515 // å¼å§å¯¹èç¹è¿è¡è¯»åï¼è¿è¡æ 深度ååºéåå¾å°éä¸ç list516 const nodeList = [];517 while (isBefore(node, stop)) {518 // å¤ææ¯å¦å½åèç¹è¢«ææå
å«ï¼å 为ä¼åå¨èå´æº¢åºçæ
åµ519 // condition 对å½åèç¹è¿è¡å¤æï¼é»è®¤ä¼éä¸ææèç¹520 if (isEffectivelyContained(node, range)521 && condition(node)) {522 nodeList.push(node);523 }524 node = nextNode(node);525 }526 return nodeList;...
index.js
Source:index.js
...9export function nextNode(node) {10 if (node.hasChildNodes()) {11 return node.firstChild12 }13 return nextNodeDescendants(node)14}15export function nextNodeDescendants(node) {16 while (node && !node.nextSibling) {17 node = node.parentNode18 }19 if (!node) {20 return null21 }22 return node.nextSibling23}24// è·å¾å½å node çindex25export function getNodeIndex(node) {26 let ret = 027 while (node.previousSibling) {28 ret++29 node = node.previousSibling;30 }31 return ret32}33// è·å¾èç¹çå
·ä½é¿åº¦34export function getNodeLength(node) {35 switch (node.nodeType) {36 case Node.PROCESSING_INSTRUCTION_NODE:37 case Node.DOCUMENT_TYPE_NODE:38 return 0;39 case Node.TEXT_NODE:40 case Node.COMMENT_NODE:41 return node.length;42 default:43 return node.childNodes.length;44 }45}46// è·å¾èç¹ä½ç½®47export function getPosition(nodeA, offsetA, nodeB, offsetB) {48 if (nodeA == nodeB) {49 if (offsetA == offsetB) return 'equal'50 if (offsetA < offsetB) return 'before'51 if (offsetA > offsetB) return 'after'52 }53 if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_FOLLOWING) {54 const pos = getPosition(nodeB, offsetB, nodeA, offsetA);55 if (pos == 'before') return 'after'56 if (pos == 'after') 'before'57 }58 if (nodeB.compareDocumentPosition(nodeA) & Node.DOCUMENT_POSITION_CONTAINS) {59 let child = nodeB;60 while (child.parentNode != nodeA) child = child.parentNode61 if (getNodeIndex(child) < offsetA) return 'after';62 }63 return 'before';64}65// è·å¾æè¿çç¥å
èç¹66export function getFurthestAncestor(node) {67 let root = node68 while (root.parentNode) {69 root = root.parentNode;70 }71 return root;72}73// 被å
å«å®ä¹ä¸ºå¨å¼å§èç¹ä¹åï¼å¨ç»æèç¹ä¹åï¼å°±è®¤ä¸ºè¢«å
å«74export function isContained(node, range) {75 const startPosition = getPosition(node, 0, range.startContainer, range.startOffset);76 const endPosition = getPosition(node, getNodeLength(node), range.endContainer, range.endOffset);77 return getFurthestAncestor(node) == getFurthestAncestor(range.startContainer)78 && startPosition == 'after'79 && endPosition == 'before';80}81// æ¯å¦ææ被å
å«82export function isEffectivelyContained(node, range) {83 if (range.collapsed) return false;84 if (isContained(node, range)) return true;85 if (node == range.startContainer86 && node.nodeType == Node.TEXT_NODE87 && getNodeLength(node) != range.startOffset) {88 return true;89 }90 if (node == range.endContainer91 && node.nodeType == Node.TEXT_NODE92 && range.endOffset != 0) {93 return true;94 }95 if (node.hasChildNodes()96 && [].every.call(node.childNodes, (child) => isEffectivelyContained(child, range))97 && (!isDescendant(range.startContainer, node)98 || range.startContainer.nodeType != Node.TEXT_NODE99 || range.startOffset == 0)100 && (!isDescendant(range.endContainer, node)101 || range.endContainer.nodeType != Node.TEXT_NODE102 || range.endOffset == getNodeLength(range.endContainer)103 )104 ) {105 return true;106 }107 return false;108}109// è·å¾ææ被å
å«çèç¹110export function getAllEffectivelyContainedNodes(range, condition) {111 if (typeof condition == 'undefined') {112 condition = function() { return true };113 }114 // è·å¾å¼å§èç¹çæ顶å±è¢«å
裹å¨éåºèå´å
çèç¹115 let node = range.startContainer;116 while (isEffectivelyContained(node.parentNode, range)) {117 node = node.parentNode;118 }119 // è·å¾éåºç»æçä¸ä¸ªèç¹120 let stop = nextNodeDescendants(range.endContainer);121 // å¼å§å¯¹èç¹è¿è¡è¯»åï¼è¿è¡æ 深度ååºéåå¾å°éä¸ç list122 const nodeList = [];123 while (isBefore(node, stop)) {124 // å¤ææ¯å¦å½åèç¹è¢«ææå
å«ï¼å 为ä¼åå¨èå´æº¢åºçæ
åµ125 // condition 对å½åèç¹è¿è¡å¤æï¼é»è®¤ä¼éä¸ææèç¹126 if (isEffectivelyContained(node, range)127 && condition(node)) {128 nodeList.push(node);129 }130 node = nextNode(node);131 }132 return nodeList;133}134export function isContainedNode(node, otherNode) {...
Using AI Code Generation
1var tree = new WebFXTree('Root');2var node1 = tree.add(new WebFXTreeItem('Node 1'));3var node2 = tree.add(new WebFXTreeItem('Node 2'));4var node3 = tree.add(new WebFXTreeItem('Node 3'));5var node4 = tree.add(new WebFXTreeItem('Node 4'));6var node5 = tree.add(new WebFXTreeItem('Node 5'));7var node6 = node1.add(new WebFXTreeItem('Node 6'));8var node7 = node1.add(new WebFXTreeItem('Node 7'));9var node8 = node2.add(new WebFXTreeItem('Node 8'));10var node9 = node2.add(new WebFXTreeItem('Node 9'));11var node10 = node3.add(new WebFXTreeItem('Node 10'));12var node11 = node3.add(new WebFXTreeItem('Node 11'));13var node12 = node4.add(new WebFXTreeItem('Node 12'));14var node13 = node4.add(new WebFXTreeItem('Node 13'));15var node14 = node5.add(new WebFXTreeItem('Node 14'));16var node15 = node5.add(new WebFXTreeItem('Node 15'));17var node16 = node6.add(new WebFXTreeItem('Node 16'));18var node17 = node6.add(new WebFXTreeItem('Node 17'));19var node18 = node7.add(new WebFXTreeItem('Node 18'));20var node19 = node7.add(new WebFXTreeItem('Node 19'));21var node20 = node8.add(new WebFXTreeItem('Node 20'));22var node21 = node8.add(new WebFXTreeItem('Node 21'));23var node22 = node9.add(new WebFXTreeItem('Node 22'));24var node23 = node9.add(new WebFXTreeItem('Node 23'));25var node24 = node10.add(new WebFXTreeItem('Node 24'));26var node25 = node10.add(new WebFXTreeItem('Node 25'));27var node26 = node11.add(new WebFXTreeItem('Node 26'));28var node27 = node11.add(new WebFXTreeItem('Node 27'));29var node28 = node12.add(new WebFXTreeItem('Node 28'));30var node29 = node12.add(new WebFXTreeItem('Node 29
Using AI Code Generation
1var tree = new WebFXTree('Root');2var node1 = tree.add(new WebFXTreeItem('Node 1'));3var node2 = tree.add(new WebFXTreeItem('Node 2'));4var node3 = tree.add(new WebFXTreeItem('Node 3'));5var node4 = tree.add(new WebFXTreeItem('Node 4'));6var node5 = tree.add(new WebFXTreeItem('Node 5'));7var node6 = node1.add(new WebFXTreeItem('Node 6'));8var node7 = node1.add(new WebFXTreeItem('Node 7'));9var node8 = node2.add(new WebFXTreeItem('Node 8'));10var node9 = node2.add(new WebFXTreeItem('Node 9'));11var node10 = node3.add(new WebFXTreeItem('Node 10'));12var node11 = node3.add(new WebFXTreeItem('Node 11'));13var node12 = node4.add(new WebFXTreeItem('Node 12'));14var node13 = node4.add(new WebFXTreeItem('Node 13'));15var node14 = node5.add(new WebFXTreeItem('Node 14'));16var node15 = node5.add(new WebFXTreeItem('Node 15'));17var node16 = node6.add(new WebFXTreeItem('Node 16'));18var node17 = node6.add(new WebFXTreeItem('Node 17'));19var node18 = node7.add(new WebFXTreeItem('Node 18'));20var node19 = node7.add(new WebFXTreeItem('Node 19'));21var node20 = node8.add(new WebFXTreeItem('Node 20'));22var node21 = node8.add(new WebFXTreeItem('Node 21'));23var node22 = node9.add(new WebFXTreeItem('Node 22'));24var node23 = node9.add(new WebFXTreeItem('Node 23'));25var node24 = node10.add(new WebFXTreeItem('Node 24'));26var node25 = node10.add(new WebFXTreeItem('Node 25'));27var node26 = node11.add(new WebFXTreeItem('Node 26'));28var node27 = node11.add(new WebFXTreeItem('Node 27'));29var node28 = node12.add(new WebFXTreeItem('Node 28'));30var node29 = node12.add(new WebFXTreeItem('Node 29
Using AI Code Generation
1var tree = new WPTree();2var node1 = new WPNode();3node1.name = "node1";4var node2 = new WPNode();5node2.name = "node2";6var node3 = new WPNode();7node3.name = "node3";8var node4 = new WPNode();9node4.name = "node4";10var node5 = new WPNode();11node5.name = "node5";12var node6 = new WPNode();13node6.name = "node6";14var node7 = new WPNode();15node7.name = "node7";16var node8 = new WPNode();17node8.name = "node8";18var node9 = new WPNode();19node9.name = "node9";20var node10 = new WPNode();21node10.name = "node10";22var node11 = new WPNode();23node11.name = "node11";24var node12 = new WPNode();25node12.name = "node12";26var node13 = new WPNode();27node13.name = "node13";28var node14 = new WPNode();29node14.name = "node14";30var node15 = new WPNode();31node15.name = "node15";32var node16 = new WPNode();33node16.name = "node16";34tree.root = node1;35node1.addChild(node2);36node1.addChild(node3);37node1.addChild(node4);38node1.addChild(node5);39node1.addChild(node6);40node1.addChild(node7);41node1.addChild(node8);42node1.addChild(node9);43node1.addChild(node10);44node1.addChild(node11);45node1.addChild(node12);46node1.addChild(node13);47node1.addChild(node14);48node1.addChild(node15);49node1.addChild(node16);50var nodeArray = tree.nextNodeDescendants(node1, 3);51for (var i = 0; i < nodeArray.length; i++) {52 console.log(nodeArray[i].name);53}54var tree = new WPTree();55var node1 = new WPNode();56node1.name = "node1";57var node2 = new WPNode();58node2.name = "node2";59var node3 = new WPNode();60node3.name = "node3";
Using AI Code Generation
1var tree = new WPTree();2tree.root = new WPNode("root");3var child1 = new WPNode("child1");4var child2 = new WPNode("child2");5var child3 = new WPNode("child3");6var child4 = new WPNode("child4");7var child5 = new WPNode("child5");8var child6 = new WPNode("child6");9var child7 = new WPNode("child7");10var child8 = tew WPNree("child8");11var child9 = new WPNode("child9");12var child10 = new WPNode("child10");13var child11 = new WPNode("child11");14var child12 = new WPNode("child12");15var child13 = new WPNode("child13");16var child14 = new WPNode("child14");17var child15 = new WPNode("child15");18var child16 = new WPNode("child16");19var child17 = new WPNode("child17");20var child18 = new WPNode("child18");21var child19 = new WPNode("child19");22var child20 = new WPNode("child20");23var child21 = new WPNode("child21");24var child22 = new WPNode("child22");25var child23 = new WPNode("child23");26var child24 = new WPNode("child24");27var child25 = new WPNode("child25");28var child26 = new WPNode("child26");29var child27 = new WPNode("child27");30var child28 = new WPNode("child28");31var child29 = new WPNode("child29");32var child30 = new WPNode("child30");33var child31 = new WPNode("child31");34var child32 = new WPNode("child32");35var child33 = new WPNode("child33");36var child34 = new WPNode("child34");37var child35 = new WPNode("child35");38var child36 = new WPNode("child36");39var child37 = new WPNode("child37");40var child38 = new WPNode("child38");41var child39 = new WPNode("child39");42var child40 = new WPNode("child40");43var child41 = new WPNode("child41");44var child42 = new WPNode("child42");45var child43 = new WPNode("child43");
Using AI Code Generation
1var node = new WPTree();2tree.root = new WPNode("root");3var child1 = new WPNode("child1");4var child2 = new WPNode("child2");5var child3 = new WPNode("child3");6var child4 = new WPNode("child4");7var child5 = new WPNode("child5");8var child6 = new WPNode("child6");9var child7 = new WPNode("child7");10var child8 = new WPNode("child8");11var child9 = new WPNode("child9");12var child10 = new WPNode("child10");13var child11 = new WPNode("child11");14var child12 = new WPNode("child12");15var child13 = new WPNode("child13");16var child14 = new WPNode("child14");17var child15 = new WPNode("child15");18var child16 = new WPNode("child16");19var child17 = new WPNode("child17");20var child18 = new WPNode("child18");21var child19 = new WPNode("child19");22var child20 = new WPNode("child20");23var child21 = new WPNode("child21");24var child22 = new WPNode("child22");25var child23 = new WPNode("child23");26var child24 = new WPNode("child24");27var child25 = new WPNode("child25");28var child26 = new WPNode("child26");29var child27 = new WPNode("child27");30var child28 = new WPNode("child28");31var child29 = new WPNode("child29");32var child30 = new WPNode("child30");33var child31 = new WPNode("child31");34var child32 = new WPNode("child32");35var child33 = new WPNode("child33");36var child34 = new WPNode("child34");37var child35 = new WPNode("child35");38var child36 = new WPNode("child36");39var child37 = new WPNode("child37");40var child38 = new WPNode("child38");41var child39 = new WPNode("child39");42var child40 = new WPNode("child40");43var child41 = new WPNode("child41");44var child42 = new WPNode("child42");45var child43 = new WPNode("child43");
Using AI Code Generation
1var node = tree.getNodeById(1);2var descendants = node.nextNodeDescendants();3var i = 0;4while (i < descendants.length) {5 alert(descendants[i].id);6 i++;7}8var node = tree.getNodeById(1);9var nextDescendant = node.nextNode();10alert(nextDescendant.id);11var node = tree.getNodeById(1);12var descendants = node.previousNodeDescendants();13var i = 0;14while (i < descendants.length) {15 alert(descendants[i].id);16 i++;17}hildren
Using AI Code Generation
1var tree = wptree.getTree("tree1");2var node = tree.getNode("node1");3var nextNodeDescendants = node.nextNodeDescendants();4var tree = wptree.getTree("tree1");5var node = tree.getNode("node1");6var nextNodeDescendants = node.nextNodeDescendants("tag1");7var tree = wptree.getTree("tree1");8var node = tree.getNode("node1");9var nextNodeDescendants = node.nextNodeDescendants("tag1","value1");10var tree = wptree.getTree("tree1");11var noe = tee.getNode("nod1");12var extNodeDescendants = node.nextNodeDescendants("tag1","value1");13var tree = wptree.getTree("tree1");14var node = tree.getNode("node1");15var nextNodeDescendants = node.nextNodeDescendants("tag1","value1");16var tree = wptree.getTree("tree1");17var node = tree.getNode("node1");18var nextNodeDescendants = node.nextNodeDescendants("tag1","value1");19var tree = wptree.getTree("tree1");20var node = tree.getNode("node1");21var nextNodeDescendants = node.nextNodeDescendants("tag1","value1");22var tree = wptree.getTree("tree1");23var node = tree.getNode("node1");24var nextNodeDescendants = node.nextNodeDescendants("tag1","value1");25var node = tree.getNodeById(1);26var previousDescendant = node.previousNode();27alert(previousDescendant.id);28var node = tree.getNodeById(1);29alert(node.isLast());30var node = tree.getNodeById(1);31alert(node.isFirst());32var node = tree.getNodeById(1);33alert(node.isLeaf());34var node = tree.getNodeById(1);35alert(node.isRoot());36var node = tree.getNodeById(1);37var node2 = tree.getNodeById(2);38alert(node.isDescendantOf(node2));
Using AI Code Generation
1var treeView = document.getElementById("myTreeView");2var treeItem = treeView.selectedItem;3var treeItemDescendants = treeView.nextNodeDescendants(treeItem);4for (var i=0; i<treeItemDescendants.lengt; i++) {5 alert(treeItemDescendants[i].getAttrbute("labe"));6}7var node = tree.getNodeById(1);8var node2 = tree.getNodeById(2);9alert(node.isAncestorOf(node2));10var node = tree.getNodeById(1);11var node2 = tree.getNodeById(2);12alert(node.isParentOf(node2));13var node = tree.getNodeById(1);14var node2 = tree.getNodeById(
Using AI Code Generation
1var treeView = new WpTreeView("treeView", "treeViewRoot");2treeView.nextNodeDescendants();3WpTreeView.prototype.nextNodeDescendants = function() {4 var treeView = this;5 var treeViewRoot = treeView.treeViewRoot;6 var treeViewRootChildren = treeViewRoot.children;7 var treeViewRootChildrenLength = treeViewRootChildren.length;8 var treeViewRootChildrenLengthMinusOne = treeViewRootChildrenLength - 1;9 for (var i = 0; i < treeViewRootChildrenLength; i++) {10 var treeViewRootChild = treeViewRootChildren[i];11 var treeViewRootChildChildren = treeViewRootChild.children;12 var treeViewRootChildChildrenLength = treeViewRootChildChildren.length;13 var treeViewRootChildChildrenLengthMinusOne = treeViewRootChildChildrenLength - 1;14 for (var j = 0; j < treeViewRootChildChildrenLength; j++) {15 var treeViewRootChildChild = treeViewRootChildChildren[j];16 var treeViewRootChildChildChildren = treeViewRootChildChild.children;17 var treeViewRootChildChildChildrenLength = treeViewRootChildChildChildren.length;18 var treeViewRootChildChildChildrenLengthMinusOne = treeViewRootChildChildChildrenLength - 1;19 for (var k = 0; k < treeViewRootChildChildChildrenLength; k++) {20 var treeViewRootChildChildChild = treeViewRootChildChildChildren[k];21 var treeViewRootChildChildChildChildren = treeViewRootChildChildChild.children;22 var treeViewRootChildChildChildChildrenLength = treeViewRootChildChildChildChildren.length;23 var treeViewRootChildChildChildChildrenLengthMinusOne = treeViewRootChildChildChildChildrenLength - 1;24 for (var l = 0; l < treeViewRootChildChildChildChildrenLength; l++) {25 var treeViewRootChildChildChildChild = treeViewRootChildChildChildChildren[l];26 var treeViewRootChildChildChildChildChildren = treeViewRootChildChildChildChild.children;27 var treeViewRootChildChildChildChildChildrenLength = treeViewRootChildChildChildChildChildren.length;
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!!