Best JavaScript code snippet using wpt
Selection.ts
Source:Selection.ts
...131 replacementElement.setAttributeNS(element.attributes[i].namespaceURI, element.attributes[i].name, element.attributes[i].value);132 }133 // å½å
ç´ å
·æåå
ç´ æ¶ï¼å°å
ç´ ç第ä¸ä¸ªåå
ç´ è¿½å 为æ¿æ¢å
ç´ çæåä¸ä¸ªåå
ç´ ï¼å¹¶ä¿çèå´ã134 while (element.childNodes.length) {135 movePreservingRanges(element.firstChild, replacementElement, replacementElement.childNodes.length);136 }137 // ä»å
¶ç¶å
ç´ ä¸å é¤å
ç´ ã138 element.parentNode.removeChild(element);139 // è¿åæ¿æ¢å
ç´ 140 return replacementElement;141 }142 // å¨èç¹ä¹åå é¤å¤ä½çæ¢è¡ç¬¦143 const removeExtraneousLineBreaksBefore = (node) => {144 // 令ref为èç¹çpreviousSiblingã145 let ref = node.previousSibling;146 // å¦æref为nullï¼è¯·ä¸æ¢è¿äºæ¥éª¤ã147 if (!ref) {148 return;149 }150 // å½refæå©åæ¶ï¼è¯·å°ref设置为å
¶lastChildã151 while (ref.hasChildNodes()) {152 ref = ref.lastChild;153 }154 // è½ç¶refæ¯ä¸å¯è§çï¼ä½ä¸æ¯å¤ä½çæ¢è¡ç¬¦ï¼å¹¶ä¸refä¸çäºèç¹çç¶èç¹ï¼ä½è¯·ææ 顺åºå°ref设置为该èç¹ä¹åçèç¹ã155 while (utils.isInvisible(ref)156 && !utils.isExtraneousLineBreak(ref)157 && ref != node.parentNode) {158 ref = utils.previousNode(ref);159 }160 // å¦ærefæ¯å¯ç¼è¾çå¤é¨æ¢è¡ç¬¦ï¼è¯·å°å
¶ä»å
¶ç¶çº§ä¸å é¤161 if (utils.isEditable(ref)162 && utils.isExtraneousLineBreak(ref)) {163 ref.parentNode.removeChild(ref);164 }165 }166 // å é¤èç¹æ«ç«¯çå¤ä½æ¢è¡ç¬¦167 const removeExtraneousLineBreaksAtTheEndOf = (node) => {168 // 让 ref = node169 let ref = node;170 // å½ ref æå©åæ¶ï¼è¯·å° ref 设置为å
¶ lastChildã171 while (ref.hasChildNodes()) {172 ref = ref.lastChild;173 }174 // è½ç¶ ref æ¯ä¸å¯è§çï¼ä½ä¸æ¯å¤ä½çæ¢è¡ç¬¦ï¼è ref æ¯ä¸ç¸ççèç¹ï¼è¯·ä»¥æ 顺åºå°ref设置为该èç¹ä¹åçèç¹ã175 while (utils.isInvisible(ref)176 && !utils.isExtraneousLineBreak(ref)177 && ref != node) {178 ref = utils.previousNode(ref);179 }180 // å¦æ ref æ¯å¯ç¼è¾çå¤é¨æ¢è¡ç¬¦181 if (utils.isEditable(ref)182 && utils.isExtraneousLineBreak(ref)) {183 // å½refçç¶çº§æ¯å¯ç¼è¾ä¸ä¸å¯è§çæ¶ï¼è¯·å°ref设置为å
¶ç¶çº§ã184 while (utils.isEditable(ref.parentNode)185 && utils.isInvisible(ref.parentNode)) {186 ref = ref.parentNode;187 }188 // ä»å
¶ç¶çº§ç§»é¤refã189 ref.parentNode.removeChild(ref);190 }191 }192 // è¦ä»èç¹ä¸å é¤å¤ä½çæ¢è¡ç¬¦ï¼è¯·å
å é¤è¯¥èç¹ä¹åçå¤ä½çæ¢è¡ç¬¦ï¼ç¶ååå¨å
¶æ«å°¾å é¤å¤ä½çæ¢è¡ç¬¦ã193 const removeExtraneousLineBreaksFrom = (node) => {194 removeExtraneousLineBreaksBefore(node);195 removeExtraneousLineBreaksAtTheEndOf(node);196 }197 // å
è£
èç¹å表198 // siblingCriteriaï¼ å
å¼å§å¦¹199 // newParentInstructionsï¼ æ°ç¶èç¹200 const wrap = (nodeList, siblingCriteria, newParentInstructions) => {201 // å¦ææªæä¾ï¼åå级æ¡ä»¶è¿å falseï¼èæ°çç¶æ令è¿ånull202 if (typeof siblingCriteria == "undefined") {203 siblingCriteria = function () { return false };204 }205 if (typeof newParentInstructions == "undefined") {206 newParentInstructions = function () { return null };207 }208 // å¦æèç¹å表çæ¯ä¸ªæåé½ä¸å¯è§ï¼å¹¶ä¸é½ä¸æ¯brï¼åè¿ånull并ä¸æ¢è¿äºæ¥éª¤ã209 if (nodeList.every(utils.isInvisible)210 && !nodeList.some(function (node) { return utils.isHtmlElement(node, "br") })) {211 return null;212 }213 // å¦æèç¹å表ç第ä¸ä¸ªæåçç¶çº§ä¸ºnullï¼åè¿ånull并ä¸æ¢è¿äºæ¥éª¤ã214 if (!nodeList[0].parentNode) {215 return null;216 }217 // âå¦æèç¹å表çæåä¸ä¸ªæåæ¯ä¸æ¯brçå
èèç¹ï¼å¹¶ä¸èç¹å表çæåä¸ä¸ªæåçnextSiblingæ¯brï¼åå°è¯¥bréå å°èç¹å表ä¸ãâ218 if (utils.isInlineNode(nodeList[nodeList.length - 1])219 && !utils.isHtmlElement(nodeList[nodeList.length - 1], "br")220 && utils.isHtmlElement(nodeList[nodeList.length - 1].nextSibling, "br")) {221 nodeList.push(nodeList[nodeList.length - 1].nextSibling);222 }223 // è½ç¶èç¹å表ç第ä¸ä¸ªæåçä¸ä¸ä¸ªå级å
å¼æ¯ä¸å¯è§çï¼ä½å¨åé¢å®å°èç¹å表224 while (utils.isInvisible(nodeList[0].previousSibling)) {225 nodeList.unshift(nodeList[0].previousSibling);226 }227 // å½èç¹å表çæåä¸ä¸ªæåçnextSiblingä¸å¯è§æ¶ï¼è¯·å°å
¶è¿½å å°èç¹å表ä¸ã228 while (utils.isInvisible(nodeList[nodeList.length - 1].nextSibling)) {229 nodeList.push(nodeList[nodeList.length - 1].nextSibling);230 }231 // å¦æèç¹å表ç第ä¸ä¸ªæåçpreviousSiblingæ¯å¯ç¼è¾çï¼å¹¶ä¸å¨å
¶ä¸è¿è¡å级æ¡ä»¶å°è¿åtrueï¼232 // 让æ°çç¶çº§æ为èç¹å表ç第ä¸ä¸ªæåçpreviousSibling233 let newParent;234 if (utils.isEditable(nodeList[0].previousSibling)235 && siblingCriteria(nodeList[0].previousSibling)) {236 newParent = nodeList[0].previousSibling;237 // å¦åï¼å¦æèç¹å表çæåä¸ä¸ªæåçnextSiblingæ¯å¯ç¼è¾çï¼å¹¶ä¸å¨å
¶ä¸è¿è¡çå
å¼å§å¦¹æ¡ä»¶è¿åtrueï¼238 // å让æ°çç¶èç¹æ为èç¹å表çæåä¸ä¸ªæåçnextSiblingã239 } else if (utils.isEditable(nodeList[nodeList.length - 1].nextSibling)240 && siblingCriteria(nodeList[nodeList.length - 1].nextSibling)) {241 newParent = nodeList[nodeList.length - 1].nextSibling;242 // å¦åï¼è¯·è¿è¡æ°çç¶ä»£æ令ï¼å¹¶è®©æ°çç¶ä»£æ为ç»æã243 } else {244 newParent = newParentInstructions();245 }246 // å¦ænew parent为nullï¼è¯·ä¸æ¢è¿äºæ¥éª¤å¹¶è¿ånullã247 if (!newParent) {248 return null;249 }250 // å¦ææ°ç¶èç¹çç¶èç¹ä¸ºç©ºï¼251 if (!newParent.parentNode) {252 // å¨èç¹å表ç第ä¸ä¸ªæåä¹åï¼å°æ°çç¶èç¹æå
¥å°èç¹å表ç第ä¸ä¸ªæåçç¶èç¹ä¸253 nodeList[0].parentNode.insertBefore(newParent, nodeList[0]);254 // å¦æä»»ä½èå´çè¾¹çç¹çèç¹çäºæ°ç¶çº§çç¶çº§ï¼èå移éçäºæ°ç¶çº§çç´¢å¼ï¼åå¨è¯¥è¾¹çç¹çå移éä¸å ä¸ä¸ªã255 // ä»
å°è¯ä¿®å¤å
¨å±Range256 if (internalRange.startContainer == newParent.parentNode257 && internalRange.startOffset == utils.getNodeIndex(newParent)) {258 internalRange.setStart(internalRange.startContainer, internalRange.startOffset + 1);259 }260 if (internalRange.endContainer == newParent.parentNode261 && internalRange.endOffset == utils.getNodeIndex(newParent)) {262 internalRange.setEnd(internalRange.endContainer, internalRange.endOffset + 1);263 }264 }265 // 令åå§ç¶èç¹ä¸ºèç¹å表ç第ä¸ä¸ªæåçç¶èç¹ã266 let originalParent = nodeList[0].parentNode;267 // å¦ææ°çç¶çº§ææ 顺åºå¨èç¹å表ç第ä¸ä¸ªæåä¹å268 if (utils.isBefore(newParent, nodeList[0])) {269 // å¦ææ°ç¶çº§ä¸æ¯å
èèç¹ï¼ä½æ¯æ°ç¶çº§çæåä¸ä¸ªå¯è§å级åèç¹å表ç第ä¸ä¸ªå¯è§æåé½æ¯å
èèç¹ï¼270 // 并ä¸æ°ç¶çº§çæåä¸ä¸ªå级ä¸æ¯ brï¼è¯·å¨ä»¥ä¸ä½ç½®è°ç¨createElementï¼â brâï¼271 // æ°ç¶çº§ç ownerDocument 并å°ç»æéå 为æ°ç¶çº§çæåä¸ä¸ªå级ã272 if (!utils.isInlineNode(newParent)273 && utils.isInlineNode([].filter.call(newParent.childNodes, utils.isVisible).slice(-1)[0])274 && utils.isInlineNode(nodeList.filter(utils.isVisible)[0])275 && !utils.isHtmlElement(newParent.lastChild, "BR")) {276 newParent.appendChild(newParent.ownerDocument.createElement("br"));277 }278 // 对äºèç¹å表ä¸çæ¯ä¸ªèç¹ï¼å°èç¹è¿½å 为æ°ç¶èç¹çæåä¸ä¸ªåèç¹ï¼ä»¥ä¿çèå´ã279 for (let i = 0; i < nodeList.length; i++) {280 movePreservingRanges(nodeList[i], newParent, -1);281 }282 // å
¶ä»æ
åµ283 } else {284 // âå¦ææ°çç¶çº§ä¸æ¯å
èèç¹ï¼ä½æ¯æ°çç¶çº§ç第ä¸ä¸ªå¯è§å级åèç¹å表çæåä¸ä¸ªå¯è§æåé½æ¯å
èèç¹ï¼285 // 并ä¸èç¹å表çæåä¸ä¸ªæåä¸æ¯brï¼è¯·è°ç¨createElementï¼â brâï¼ å¨æ°ç¶é¡¹çownerDocumentä¸ï¼å¹¶å°ç»æä½ä¸ºæ°ç¶é¡¹ç第ä¸ä¸ªå项æå
¥ãâ286 if (!utils.isInlineNode(newParent)287 && utils.isInlineNode([].filter.call(newParent.childNodes, utils.isVisible)[0])288 && utils.isInlineNode(nodeList.filter(utils.isVisible).slice(-1)[0])289 && !utils.isHtmlElement(nodeList[nodeList.length - 1], "BR")) {290 newParent.insertBefore(newParent.ownerDocument.createElement("br"), newParent.firstChild);291 }292 // 对äºèç¹å表ä¸çæ¯ä¸ªèç¹ï¼ä»¥ç¸åç顺åºï¼å°èç¹æå
¥ä¸ºæ°ç¶èç¹ç第ä¸ä¸ªåèç¹ï¼å¹¶ä¿çèå´ã293 for (let i = nodeList.length - 1; i >= 0; i--) {294 movePreservingRanges(nodeList[i], newParent, 0);295 }296 }297 // å¦æåå§ç¶çº§æ¯å¯ç¼è¾ç并ä¸æ²¡æå级ï¼åå°å
¶ä»å
¶ç¶çº§ä¸å é¤298 if (utils.isEditable(originalParent) && !originalParent.hasChildNodes()) {299 originalParent.parentNode.removeChild(originalParent);300 }301 // å¦ææ°ç¶çº§èç¹ç nextSibling æ¯å¯ç¼è¾çï¼å¹¶ä¸å¨å
¶ä¸è¿è¡å
å¼å§å¦¹æ¡ä»¶ï¼åè¿åtrue302 if (utils.isEditable(newParent.nextSibling)303 && siblingCriteria(newParent.nextSibling)) {304 // å¦ææ°ç¶æ¯ä¸æ¯å
èèç¹ï¼ä½æ¯æ°ç¶æ¯çæåä¸ä¸ªå©ååæ°ç¶æ¯çnextSiblingç第ä¸ä¸ªå©åé½æ¯å
èèç¹ï¼èæ°ç¶æ¯çæåä¸ä¸ªå©åä¹ä¸æ¯brï¼è¯·å¨æ°ç¶æ¯çownerDocumentä¸è°ç¨createElementï¼â brâï¼ å°ç»æéå 为æ°ç¶æ¯çæåä¸ä¸ªå©åã305 if (!utils.isInlineNode(newParent)306 && utils.isInlineNode(newParent.lastChild)307 && utils.isInlineNode(newParent.nextSibling.firstChild)308 && !utils.isHtmlElement(newParent.lastChild, "BR")) {309 newParent.appendChild(newParent.ownerDocument.createElement("br"));310 }311 // âå°½ç®¡æ° parent ç nextSibling æ childï¼ä½å°å
¶ç¬¬ä¸ä¸ª child 追å 为 new parent çæåä¸ä¸ª childï¼ä»¥ä¿çèå´312 while (newParent.nextSibling.hasChildNodes()) {313 movePreservingRanges(newParent.nextSibling.firstChild, newParent, -1);314 }315 // ä»å
¶ç¶çº§ä¸å é¤æ°ç¶çº§ç nextSibling316 newParent.parentNode.removeChild(newParent.nextSibling);317 }318 // ä»æ°çç¶çº§ä¸å é¤å¤ä½çæ¢è¡ç¬¦319 removeExtraneousLineBreaksFrom(newParent);320 // Return new parent321 return newParent;322 }323 const exports = {324 get range() {325 return internalRange326 },327 selection: getSelection(),...
fa9097bb40a169ef85e63c7e26d08a8aaf7f0f1f_0_1.js
Source:fa9097bb40a169ef85e63c7e26d08a8aaf7f0f1f_0_1.js
...30 // parent of original parent immediately after original parent,31 // preserving ranges."32 for (var i = nodeList.length - 1; i >= 0; i--) {33 if(isHtmlElement(originalParent.parentNode, ["UL", "OL", "LI"])){34 movePreservingRanges(nodeList[i], originalParent.parentNode, 1 + getNodeIndex(originalParent), range);35 }36 }37 // "If precedes line break is true, and the last member of node list38 // does not precede a line break, call createElement("br") on the39 // context object and insert the result immediately after the last40 // member of node list."41 if (precedesLineBreak_42 && !precedesLineBreak(nodeList[nodeList.length - 1])) {43 nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling);44 }45 // "Remove extraneous line breaks at the end of original parent."46 removeExtraneousLineBreaksAtTheEndOf(originalParent);47 // "Abort these steps."48 return;49 }50 // "If the first child of original parent is not in node list:"51 if ($_(nodeList).indexOf(originalParent.firstChild) == -1) {52 // "Let cloned parent be the result of calling cloneNode(false) on53 // original parent."54 var clonedParent = originalParent.cloneNode(false);55 // "If original parent has an id attribute, unset it."56 originalParent.removeAttribute("id");57 // "Insert cloned parent into the parent of original parent immediately58 // before original parent."59 originalParent.parentNode.insertBefore(clonedParent, originalParent);60 // "While the previousSibling of the first member of node list is not61 // null, append the first child of original parent as the last child of62 // cloned parent, preserving ranges."63 while (nodeList[0].previousSibling) {64 movePreservingRanges(originalParent.firstChild, clonedParent, clonedParent.childNodes.length, range);65 }66 }67 // "For each node in node list, insert node into the parent of original68 // parent immediately before original parent, preserving ranges."69 for (var i = 0; i < nodeList.length; i++) {70 if(isHtmlElement(originalParent.parentNode, ["LI"])){71 var newParent = originalParent.parentNode.parentNode;72 var newIndex = getNodeIndex(originalParent.parentNode) + 1;73 movePreservingRanges(nodeList[i], newParent, newIndex, range);74 movePreservingRanges(originalParent, nodeList[i], -1, range);75 } else {76 movePreservingRanges(nodeList[i], originalParent.parentNode, getNodeIndex(originalParent), range);77 }78 }79 // "If follows line break is true, and the first member of node list does80 // not follow a line break, call createElement("br") on the context object81 // and insert the result immediately before the first member of node list."82 if (followsLineBreak_83 && !followsLineBreak(nodeList[0])) {84 nodeList[0].parentNode.insertBefore(document.createElement("br"), nodeList[0]);85 }86 // "If the last member of node list is an inline node other than a br, and87 // the first child of original parent is a br, and original parent is not88 // an inline node, remove the first child of original parent from original89 // parent."90 if (isInlineNode(nodeList[nodeList.length - 1])...
e3164882d3c469b3a098126313ebca646aed5d7f_0_1.js
Source:e3164882d3c469b3a098126313ebca646aed5d7f_0_1.js
...28 // "For each node in node list, in reverse order, insert node into the29 // parent of original parent immediately after original parent,30 // preserving ranges."31 for (var i = nodeList.length - 1; i >= 0; i--) {32 movePreservingRanges(nodeList[i], originalParent.parentNode, 1 + getNodeIndex(originalParent), range);33 }34 // "If precedes line break is true, and the last member of node list35 // does not precede a line break, call createElement("br") on the36 // context object and insert the result immediately after the last37 // member of node list."38 if (precedesLineBreak_39 && !precedesLineBreak(nodeList[nodeList.length - 1])) {40 nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling);41 }42 // "Remove extraneous line breaks at the end of original parent."43 removeExtraneousLineBreaksAtTheEndOf(originalParent);44 // "Abort these steps."45 return;46 }47 // "If the first child of original parent is not in node list:"48 if (!firstChildInNodeList) {49 // "Let cloned parent be the result of calling cloneNode(false) on50 // original parent."51 var clonedParent = originalParent.cloneNode(false);52 // "If original parent has an id attribute, unset it."53 originalParent.removeAttribute("id");54 // "Insert cloned parent into the parent of original parent immediately55 // before original parent."56 originalParent.parentNode.insertBefore(clonedParent, originalParent);57 // "While the previousSibling of the first member of node list is not58 // null, append the first child of original parent as the last child of59 // cloned parent, preserving ranges."60 while (nodeList[0].previousSibling) {61 movePreservingRanges(originalParent.firstChild, clonedParent, clonedParent.childNodes.length, range);62 }63 }64 // "For each node in node list, insert node into the parent of original65 // parent immediately before original parent, preserving ranges."66 for (var i = 0; i < nodeList.length; i++) {67 if(isHtmlElementInArray(originalParent.parentNode, ["LI"])){68 var newParent = originalParent.parentNode.parentNode;69 var newIndex = getNodeIndex(originalParent.parentNode) + 1;70 movePreservingRanges(nodeList[i], newParent, newIndex, range);71 movePreservingRanges(originalParent, nodeList[i], -1, range);72 } else {73 movePreservingRanges(nodeList[i], originalParent.parentNode, getNodeIndex(originalParent), range);74 }75 }76 // "If follows line break is true, and the first member of node list does77 // not follow a line break, call createElement("br") on the context object78 // and insert the result immediately before the first member of node list."79 if (followsLineBreak_80 && !followsLineBreak(nodeList[0])) {81 nodeList[0].parentNode.insertBefore(document.createElement("br"), nodeList[0]);82 }83 // "If the last member of node list is an inline node other than a br, and84 // the first child of original parent is a br, and original parent is not85 // an inline node, remove the first child of original parent from original86 // parent."87 if (isInlineNode(nodeList[nodeList.length - 1])...
Using AI Code Generation
1var selection = document.getSelection();2var range = selection.getRangeAt(0);3var startNode = range.startContainer;4var endNode = range.endContainer;5var startOffset = range.startOffset;6var endOffset = range.endOffset;7var startId = startNode.id;8var endId = endNode.id;9var startNode = document.getElementById(startId);10var endNode = document.getElementById(endId);11var range = document.createRange();12range.setStart(startNode, startOffset);13range.setEnd(endNode, endOffset);14var selection = document.getSelection();15selection.removeAllRanges();16selection.addRange(range);17var range = selection.getRangeAt(0);18var startNode = range.startContainer;19var endNode = range.endContainer;20var startOffset = range.startOffset;21var endOffset = range.endOffset;22var startId = startNode.id;23var endId = endNode.id;24var startNode = document.getElementById(startId);25var endNode = document.getElementById(endId);26var range = document.createRange();27range.setStart(startNode, startOffset);28range.setEnd(endNode, endOffset);29var selection = document.getSelection();30selection.removeAllRanges();31selection.addRange(range);32var range = selection.getRangeAt(0);33var startNode = range.startContainer;34var endNode = range.endContainer;35var startOffset = range.startOffset;36var endOffset = range.endOffset;37var startId = startNode.id;38var endId = endNode.id;39var startNode = document.getElementById(startId);40var endNode = document.getElementById(endId);41var range = document.createRange();42range.setStart(startNode, startOffset);43range.setEnd(endNode, endOffset);44var selection = document.getSelection();45selection.removeAllRanges();46selection.addRange(range);47var range = selection.getRangeAt(0);48var startNode = range.startContainer;49var endNode = range.endContainer;50var startOffset = range.startOffset;51var endOffset = range.endOffset;52var startId = startNode.id;53var endId = endNode.id;54var startNode = document.getElementById(startId);55var endNode = document.getElementById(endId);56var range = document.createRange();57range.setStart(startNode, startOffset);58range.setEnd(endNode, endOffset);59var selection = document.getSelection();60selection.removeAllRanges();61selection.addRange(range);62var range = selection.getRangeAt(0);63var startNode = range.startContainer;
Using AI Code Generation
1CKEDITOR.replace( 'editor1', {2 on: {3 instanceReady: function( evt ) {4 var editor = evt.editor;5 editor.focus();6 editor.movePreservingRanges( 1, 1 );7 }8 }9} );
Using AI Code Generation
1var win = new Window('dialog', 'Test');2var tf = win.add('edittext', undefined, 'Hello World');3var btn = win.add('button', undefined, 'Move');4btn.onClick = function() {5 tf.movePreservingRanges(0, 2);6}7win.show();8var win = new Window('dialog', 'Test');9var tf = win.add('edittext', undefined, 'Hello World');10var btn = win.add('button', undefined, 'Move');11btn.onClick = function() {12 tf.movePreservingRanges(0, 2);13}14win.show();15var win = new Window('dialog', 'Test');16var tf = win.add('edittext', undefined, 'Hello World');17var btn = win.add('button', undefined, 'Move');18btn.onClick = function() {19 tf.movePreservingRanges(tf.text.length, tf.text.length);20}21win.show();22I have a text field with a number in it, and I want to move the cursor to the beginning of the number. I can do this with tf.move(0, 2) but this also selects the
Using AI Code Generation
1var selection = window.getSelection();2var range = selection.getRangeAt(0);3var textNode = document.createTextNode('hello world');4var parent = range.commonAncestorContainer;5parent.appendChild(textNode);6var newRange = document.createRange();7newRange.selectNode(textNode);8selection.removeAllRanges();9selection.addRange(newRange);10var newRange = document.createRange();11newRange.selectNode(textNode);12selection.removeAllRanges();13selection.addRange(newRange);14selection.movePreservingRanges(textNode, 2);15> + for (uint32_t i = 0; i < mRanges.Length(); i++) {16> + nsRange* range = mRanges[i];17> + range->SetStart(textNode, range->StartOffset() + offset);18> + range->SetEnd(textNode, range->EndOffset() + offset); 19Attachment #8671670 - Flags: review?(bzbarsky) → review+
Using AI Code Generation
1var { assert } = require("chai");2var { movePreservingRanges } = require("sdk/editor/selection");3var { getMostRecentBrowserWindow } = require("sdk/window/utils");4var browserWindow = getMostRecentBrowserWindow();5var document = browserWindow.document;6var content = document.getElementById("content");7var editor = content.getHTMLEditor();8var selection = editor.selection;9var rangeCount = selection.rangeCount;10var range = selection.getRangeAt(0);11var startOffset = range.startOffset;12var endOffset = range.endOffset;13var startNode = range.startContainer;14var endNode = range.endContainer;15var text = startNode.data;16var textBeforeSelection = text.substring(0, startOffset);17var textAfterSelection = text.substring(endOffset);18var textInSelection = text.substring(startOffset, endOffset);19movePreservingRanges(selection, editor, 1, 1);20var rangeCountAfter = selection.rangeCount;21var rangeAfter = selection.getRangeAt(0);22var startOffsetAfter = rangeAfter.startOffset;23var endOffsetAfter = rangeAfter.endOffset;24var startNodeAfter = rangeAfter.startContainer;25var endNodeAfter = rangeAfter.endContainer;26var textAfter = startNodeAfter.data;27var textBeforeSelectionAfter = textAfter.substring(0, startOffsetAfter);28var textAfterSelectionAfter = textAfter.substring(endOffsetAfter);
Using AI Code Generation
1var editor = new wpTextEditor();2editor.init();3var range = editor.getRange();4editor.movePreservingRanges(range, 10);5editor.movePreservingRanges(range, -10);6wpTextEditor.prototype.movePreservingRanges = function(range, offset) {7 if (range.collapsed) {8 this.movePreservingRangesCollapsed(range, offset);9 } else {10 this.movePreservingRangesExpanded(range, offset);11 }12 this.setSelection(range);13};14wpTextEditor.prototype.movePreservingRangesCollapsed = function(range, offset) {15 range.move(offset);16};17wpTextEditor.prototype.movePreservingRangesExpanded = function(range, offset) {18 if (offset > 0) {19 range.end.move(offset);20 } else if (offset < 0) {21 range.start.move(offset);22 }23};24wpTextEditor.prototype.getRange = function() {25 return this.selection.getRangeAt(0);26};27wpTextEditor.prototype.setSelection = function(range) {28 this.selection.removeAllRanges();29 this.selection.addRange(range);30};31function Range() {32 this.start = new Position();33 this.end = new Position();34 this.collapsed = false;35}36function Position() {37 this.node = null;38 this.offset = 0;39}40Position.prototype.move = function(offset) {41 this.offset += offset;42};43function Selection() {44 this.range = new Range();45}46Selection.prototype.getRangeAt = function() {47 return this.range;48};49Selection.prototype.removeAllRanges = function() {50 this.range = null;51};52Selection.prototype.addRange = function(range) {53 this.range = range;54};55function wpTextEditor() {56 this.selection = new Selection();57}58wpTextEditor.prototype.init = function() {59 this.selection.range.start.node = document.getElementById("test");60 this.selection.range.end.node = document.getElementById("test");61};
Using AI Code Generation
1var wptextarea = document.getElementById('wpTextbox1');2var cursorPos = wptextarea.selectionStart;3var text = wptextarea.value;4var textBefore = text.substring(0, cursorPos);5var textAfter = text.substring(cursorPos, text.length);6if (textBefore.lastIndexOf('7') == textBefore.length - 1) {8 wptextarea.movePreservingRanges(cursorPos + 1);9}10if (textAfter.indexOf('11') == 0) {12 wptextarea.movePreservingRanges(cursorPos - 1);13}14if (textBefore.lastIndexOf('15') == -1) {16 wptextarea.movePreservingRanges(cursorPos - textBefore.length);17}18if (textAfter.indexOf('19') == -1) {20 wptextarea.movePreservingRanges(cursorPos + textAfter.length);21}22if (textBefore.lastIndexOf('23 && textAfter.indexOf('24') == 0) {
Using AI Code Generation
1var editor = document.getElementById("editor");2editor.movePreservingRanges(0, 10, 0, 0);3movePreservingRanges: function (aFromLine, aFromCol, aToLine, aToCol) {4 var range = this.editor.selection.getRangeAt(0);5 var newRange = this.editor.document.createRange();6 newRange.setStart(this.editor.document.getElementsByTagName("pre")[0].childNodes[aToLine], aToCol);7 newRange.setEnd(this.editor.document.getElementsByTagName("pre")[0].childNodes[aToLine], aToCol);8 this.editor.selection.removeAllRanges();9 this.editor.selection.addRange(newRange);10}11> (In reply to comment #3 ) > > (In reply to comment #2 ) > > > I'm
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!!