Best JavaScript code snippet using wpt
implementation.js
Source:implementation.js
...258 node = nextNode(node);259 }260 return nodeList;261}262function getAllContainedNodes(range, condition) {263 if (typeof condition == "undefined") {264 condition = function() { return true };265 }266 var node = range.startContainer;267 if (node.hasChildNodes()268 && range.startOffset < node.childNodes.length) {269 270 node = node.childNodes[range.startOffset];271 } else if (range.startOffset == getNodeLength(node)) {272 273 node = nextNodeDescendants(node);274 } else {275 276 node = nextNode(node);277 }278 var stop = range.endContainer;279 if (stop.hasChildNodes()280 && range.endOffset < stop.childNodes.length) {281 282 stop = stop.childNodes[range.endOffset];283 } else {284 285 stop = nextNodeDescendants(stop);286 }287 var nodeList = [];288 while (isBefore(node, stop)) {289 if (isContained(node, range)290 && condition(node)) {291 nodeList.push(node);292 }293 node = nextNode(node);294 }295 return nodeList;296}297function normalizeColor(color) {298 if (color.toLowerCase() == "currentcolor") {299 return null;300 }301 if (normalizeColor.resultCache === undefined) {302 normalizeColor.resultCache = {};303 }304 if (normalizeColor.resultCache[color] !== undefined) {305 return normalizeColor.resultCache[color];306 }307 var originalColor = color;308 var outerSpan = document.createElement("span");309 document.body.appendChild(outerSpan);310 outerSpan.style.color = "black";311 var innerSpan = document.createElement("span");312 outerSpan.appendChild(innerSpan);313 innerSpan.style.color = color;314 color = getComputedStyle(innerSpan).color;315 if (color == "rgb(0, 0, 0)") {316 317 outerSpan.color = "white";318 color = getComputedStyle(innerSpan).color;319 if (color != "rgb(0, 0, 0)") {320 return normalizeColor.resultCache[originalColor] = null;321 }322 }323 document.body.removeChild(outerSpan);324 325 326 327 if (/^rgba\([0-9]+, [0-9]+, [0-9]+, 1\)$/.test(color)) {328 329 return normalizeColor.resultCache[originalColor] =330 color.replace("rgba", "rgb").replace(", 1)", ")");331 }332 if (color == "transparent") {333 334 335 return normalizeColor.resultCache[originalColor] =336 "rgba(0, 0, 0, 0)";337 }338 339 340 color = color.replace(/, 0.496094\)$/, ", 0.5)");341 return normalizeColor.resultCache[originalColor] = color;342}343function parseSimpleColor(color) {344 color = normalizeColor(color);345 var matches = /^rgb\(([0-9]+), ([0-9]+), ([0-9]+)\)$/.exec(color);346 if (matches) {347 return "#"348 + parseInt(matches[1]).toString(16).replace(/^.$/, "0$&")349 + parseInt(matches[2]).toString(16).replace(/^.$/, "0$&")350 + parseInt(matches[3]).toString(16).replace(/^.$/, "0$&");351 }352 return null;353}354var executionStackDepth = 0;355function editCommandMethod(command, range, callback) {356 357 if (executionStackDepth == 0 && typeof range != "undefined") {358 globalRange = range;359 } else if (executionStackDepth == 0) {360 globalRange = null;361 globalRange = getActiveRange();362 }363 executionStackDepth++;364 try {365 var ret = callback();366 } catch(e) {367 executionStackDepth--;368 throw e;369 }370 executionStackDepth--;371 return ret;372}373function myExecCommand(command, showUi, value, range) {374 375 376 command = command.toLowerCase();377 378 379 380 381 if (arguments.length == 1382 || (arguments.length >=4 && typeof showUi == "undefined")) {383 showUi = false;384 }385 386 387 if (arguments.length <= 2388 || (arguments.length >=4 && typeof value == "undefined")) {389 value = "";390 }391 return editCommandMethod(command, range, (function(command, showUi, value) { return function() {392 393 if (!(command in commands) || !myQueryCommandEnabled(command)) {394 return false;395 }396 397 398 var ret = commands[command].action(value);399 400 if (ret !== true && ret !== false) {401 throw "execCommand() didn't return true or false: " + ret;402 }403 404 if (ret === false) {405 return false;406 }407 408 return true;409 }})(command, showUi, value));410}411function myQueryCommandEnabled(command, range) {412 413 414 command = command.toLowerCase();415 return editCommandMethod(command, range, (function(command) { return function() {416 417 418 if (!(command in commands)) {419 return false;420 }421 422 423 424 425 426 427 428 return ["copy", "defaultparagraphseparator", "selectall", "stylewithcss",429 "usecss"].indexOf(command) != -1430 || (431 getActiveRange() !== null432 && (isEditable(getActiveRange().startContainer) || isEditingHost(getActiveRange().startContainer))433 && (isEditable(getActiveRange().endContainer) || isEditingHost(getActiveRange().endContainer))434 && (getInclusiveAncestors(getActiveRange().commonAncestorContainer).some(isEditingHost))435 );436 }})(command));437}438function myQueryCommandIndeterm(command, range) {439 440 441 command = command.toLowerCase();442 return editCommandMethod(command, range, (function(command) { return function() {443 444 if (!(command in commands) || !("indeterm" in commands[command])) {445 return false;446 }447 448 return commands[command].indeterm();449 }})(command));450}451function myQueryCommandState(command, range) {452 453 454 command = command.toLowerCase();455 return editCommandMethod(command, range, (function(command) { return function() {456 457 if (!(command in commands) || !("state" in commands[command])) {458 return false;459 }460 461 if (typeof getStateOverride(command) != "undefined") {462 return getStateOverride(command);463 }464 465 return commands[command].state();466 }})(command));467}468function myQueryCommandSupported(command) {469 470 471 command = command.toLowerCase();472 return command in commands;473}474function myQueryCommandValue(command, range) {475 476 477 command = command.toLowerCase();478 return editCommandMethod(command, range, function() {479 480 if (!(command in commands) || !("value" in commands[command])) {481 return "";482 }483 484 485 486 if (command == "fontsize"487 && getValueOverride("fontsize") !== undefined) {488 return getLegacyFontSize(getValueOverride("fontsize"));489 }490 491 if (typeof getValueOverride(command) != "undefined") {492 return getValueOverride(command);493 }494 495 return commands[command].value();496 });497}498function isHtmlElement(node, tags) {499 if (typeof tags == "string") {500 tags = [tags];501 }502 if (typeof tags == "object") {503 tags = tags.map(function(tag) { return tag.toUpperCase() });504 }505 return node506 && node.nodeType == Node.ELEMENT_NODE507 && isHtmlNamespace(node.namespaceURI)508 && (typeof tags == "undefined" || tags.indexOf(node.tagName) != -1);509}510var prohibitedParagraphChildNames = ["address", "article", "aside",511 "blockquote", "caption", "center", "col", "colgroup", "dd", "details",512 "dir", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer",513 "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "li",514 "listing", "menu", "nav", "ol", "p", "plaintext", "pre", "section",515 "summary", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "ul",516 "xmp"];517function isProhibitedParagraphChild(node) {518 return isHtmlElement(node, prohibitedParagraphChildNames);519}520function isBlockNode(node) {521 return node522 && ((node.nodeType == Node.ELEMENT_NODE && ["inline", "inline-block", "inline-table", "none"].indexOf(getComputedStyle(node).display) == -1)523 || node.nodeType == Node.DOCUMENT_NODE524 || node.nodeType == Node.DOCUMENT_FRAGMENT_NODE);525}526function isInlineNode(node) {527 return node && !isBlockNode(node);528}529function isEditingHost(node) {530 return node531 && isHtmlElement(node)532 && (node.contentEditable == "true"533 || (node.parentNode534 && node.parentNode.nodeType == Node.DOCUMENT_NODE535 && node.parentNode.designMode == "on"));536}537function isEditable(node) {538 return node539 && !isEditingHost(node)540 && (node.nodeType != Node.ELEMENT_NODE || node.contentEditable != "false")541 && (isEditingHost(node.parentNode) || isEditable(node.parentNode))542 && (isHtmlElement(node)543 || (node.nodeType == Node.ELEMENT_NODE && node.namespaceURI == "http://www.w3.org/2000/svg" && node.localName == "svg")544 || (node.nodeType == Node.ELEMENT_NODE && node.namespaceURI == "http://www.w3.org/1998/Math/MathML" && node.localName == "math")545 || (node.nodeType != Node.ELEMENT_NODE && isHtmlElement(node.parentNode)));546}547function hasEditableDescendants(node) {548 for (var i = 0; i < node.childNodes.length; i++) {549 if (isEditable(node.childNodes[i])550 || hasEditableDescendants(node.childNodes[i])) {551 return true;552 }553 }554 return false;555}556function getEditingHostOf(node) {557 if (isEditingHost(node)) {558 return node;559 } else if (isEditable(node)) {560 var ancestor = node.parentNode;561 while (!isEditingHost(ancestor)) {562 ancestor = ancestor.parentNode;563 }564 return ancestor;565 } else {566 return null;567 }568}569function inSameEditingHost(node1, node2) {570 return getEditingHostOf(node1)571 && getEditingHostOf(node1) == getEditingHostOf(node2);572}573function isCollapsedLineBreak(br) {574 if (!isHtmlElement(br, "br")) {575 return false;576 }577 578 579 580 var ref = br.parentNode;581 while (getComputedStyle(ref).display == "inline") {582 ref = ref.parentNode;583 }584 var refStyle = ref.hasAttribute("style") ? ref.getAttribute("style") : null;585 ref.style.height = "auto";586 ref.style.maxHeight = "none";587 ref.style.minHeight = "0";588 var space = document.createTextNode("\u200b");589 var origHeight = ref.offsetHeight;590 if (origHeight == 0) {591 throw "isCollapsedLineBreak: original height is zero, bug?";592 }593 br.parentNode.insertBefore(space, br.nextSibling);594 var finalHeight = ref.offsetHeight;595 space.parentNode.removeChild(space);596 if (refStyle === null) {597 598 599 ref.setAttribute("style", "");600 ref.removeAttribute("style");601 } else {602 ref.setAttribute("style", refStyle);603 }604 605 606 607 return origHeight < finalHeight - 5;608}609function isExtraneousLineBreak(br) {610 if (!isHtmlElement(br, "br")) {611 return false;612 }613 if (isHtmlElement(br.parentNode, "li")614 && br.parentNode.childNodes.length == 1) {615 return false;616 }617 618 619 620 621 var ref = br.parentNode;622 while (getComputedStyle(ref).display == "inline") {623 ref = ref.parentNode;624 }625 var refStyle = ref.hasAttribute("style") ? ref.getAttribute("style") : null;626 ref.style.height = "auto";627 ref.style.maxHeight = "none";628 ref.style.minHeight = "0";629 var brStyle = br.hasAttribute("style") ? br.getAttribute("style") : null;630 var origHeight = ref.offsetHeight;631 if (origHeight == 0) {632 throw "isExtraneousLineBreak: original height is zero, bug?";633 }634 br.setAttribute("style", "display:none");635 var finalHeight = ref.offsetHeight;636 if (refStyle === null) {637 638 639 ref.setAttribute("style", "");640 ref.removeAttribute("style");641 } else {642 ref.setAttribute("style", refStyle);643 }644 if (brStyle === null) {645 br.removeAttribute("style");646 } else {647 br.setAttribute("style", brStyle);648 }649 return origHeight == finalHeight;650}651function isWhitespaceNode(node) {652 return node653 && node.nodeType == Node.TEXT_NODE654 && (node.data == ""655 || (656 /^[\t\n\r ]+$/.test(node.data)657 && node.parentNode658 && node.parentNode.nodeType == Node.ELEMENT_NODE659 && ["normal", "nowrap"].indexOf(getComputedStyle(node.parentNode).whiteSpace) != -1660 ) || (661 /^[\t\r ]+$/.test(node.data)662 && node.parentNode663 && node.parentNode.nodeType == Node.ELEMENT_NODE664 && getComputedStyle(node.parentNode).whiteSpace == "pre-line"665 ));666}667function isCollapsedWhitespaceNode(node) {668 669 if (!isWhitespaceNode(node)) {670 return false;671 }672 673 if (node.data == "") {674 return true;675 }676 677 var ancestor = node.parentNode;678 679 if (!ancestor) {680 return true;681 }682 683 684 if (getAncestors(node).some(function(ancestor) {685 return ancestor.nodeType == Node.ELEMENT_NODE686 && getComputedStyle(ancestor).display == "none";687 })) {688 return true;689 }690 691 692 while (!isBlockNode(ancestor)693 && ancestor.parentNode) {694 ancestor = ancestor.parentNode;695 }696 697 var reference = node;698 699 while (reference != ancestor) {700 701 reference = previousNode(reference);702 703 if (isBlockNode(reference)704 || isHtmlElement(reference, "br")) {705 return true;706 }707 708 709 if ((reference.nodeType == Node.TEXT_NODE && !isWhitespaceNode(reference))710 || isHtmlElement(reference, "img")) {711 break;712 }713 }714 715 reference = node;716 717 var stop = nextNodeDescendants(ancestor);718 while (reference != stop) {719 720 721 reference = nextNode(reference);722 723 if (isBlockNode(reference)724 || isHtmlElement(reference, "br")) {725 return true;726 }727 728 729 if ((reference && reference.nodeType == Node.TEXT_NODE && !isWhitespaceNode(reference))730 || isHtmlElement(reference, "img")) {731 break;732 }733 }734 735 return false;736}737function isVisible(node) {738 if (!node) {739 return false;740 }741 if (getAncestors(node).concat(node)742 .filter(function(node) { return node.nodeType == Node.ELEMENT_NODE })743 .some(function(node) { return getComputedStyle(node).display == "none" })) {744 return false;745 }746 if (isBlockNode(node)747 || (node.nodeType == Node.TEXT_NODE && !isCollapsedWhitespaceNode(node))748 || isHtmlElement(node, "img")749 || (isHtmlElement(node, "br") && !isExtraneousLineBreak(node))) {750 return true;751 }752 for (var i = 0; i < node.childNodes.length; i++) {753 if (isVisible(node.childNodes[i])) {754 return true;755 }756 }757 return false;758}759function isInvisible(node) {760 return node && !isVisible(node);761}762function isCollapsedBlockProp(node) {763 if (isCollapsedLineBreak(node)764 && !isExtraneousLineBreak(node)) {765 return true;766 }767 if (!isInlineNode(node)768 || node.nodeType != Node.ELEMENT_NODE) {769 return false;770 }771 var hasCollapsedBlockPropChild = false;772 for (var i = 0; i < node.childNodes.length; i++) {773 if (!isInvisible(node.childNodes[i])774 && !isCollapsedBlockProp(node.childNodes[i])) {775 return false;776 }777 if (isCollapsedBlockProp(node.childNodes[i])) {778 hasCollapsedBlockPropChild = true;779 }780 }781 return hasCollapsedBlockPropChild;782}783function getActiveRange() {784 var ret;785 if (globalRange) {786 ret = globalRange;787 } else if (getSelection().rangeCount) {788 ret = getSelection().getRangeAt(0);789 } else {790 return null;791 }792 if ([Node.TEXT_NODE, Node.ELEMENT_NODE].indexOf(ret.startContainer.nodeType) == -1793 || [Node.TEXT_NODE, Node.ELEMENT_NODE].indexOf(ret.endContainer.nodeType) == -1794 || !ret.startContainer.ownerDocument795 || !ret.endContainer.ownerDocument796 || !isDescendant(ret.startContainer, ret.startContainer.ownerDocument)797 || !isDescendant(ret.endContainer, ret.endContainer.ownerDocument)) {798 throw "Invalid active range; test bug?";799 }800 return ret;801}802var getStateOverride, setStateOverride, unsetStateOverride,803 getValueOverride, setValueOverride, unsetValueOverride;804(function() {805 var stateOverrides = {};806 var valueOverrides = {};807 var storedRange = null;808 function resetOverrides() {809 if (!storedRange810 || storedRange.startContainer != getActiveRange().startContainer811 || storedRange.endContainer != getActiveRange().endContainer812 || storedRange.startOffset != getActiveRange().startOffset813 || storedRange.endOffset != getActiveRange().endOffset) {814 stateOverrides = {};815 valueOverrides = {};816 storedRange = getActiveRange().cloneRange();817 }818 }819 getStateOverride = function(command) {820 resetOverrides();821 return stateOverrides[command];822 };823 setStateOverride = function(command, newState) {824 resetOverrides();825 stateOverrides[command] = newState;826 };827 unsetStateOverride = function(command) {828 resetOverrides();829 delete stateOverrides[command];830 }831 getValueOverride = function(command) {832 resetOverrides();833 return valueOverrides[command];834 }835 836 837 838 setValueOverride = function(command, newValue) {839 resetOverrides();840 valueOverrides[command] = newValue;841 if (command == "backcolor") {842 valueOverrides.hilitecolor = newValue;843 } else if (command == "hilitecolor") {844 valueOverrides.backcolor = newValue;845 }846 }847 unsetValueOverride = function(command) {848 resetOverrides();849 delete valueOverrides[command];850 if (command == "backcolor") {851 delete valueOverrides.hilitecolor;852 } else if (command == "hilitecolor") {853 delete valueOverrides.backcolor;854 }855 }856})();857var extraRanges = [];858function movePreservingRanges(node, newParent, newIndex) {859 860 if (newIndex == -1) {861 newIndex = newParent.childNodes.length;862 }863 864 865 866 867 868 869 870 var oldParent = node.parentNode;871 var oldIndex = getNodeIndex(node);872 873 874 875 var ranges = [globalRange].concat(extraRanges);876 for (var i = 0; i < getSelection().rangeCount; i++) {877 ranges.push(getSelection().getRangeAt(i));878 }879 var boundaryPoints = [];880 ranges.forEach(function(range) {881 boundaryPoints.push([range.startContainer, range.startOffset]);882 boundaryPoints.push([range.endContainer, range.endOffset]);883 });884 boundaryPoints.forEach(function(boundaryPoint) {885 886 887 888 889 890 891 if (boundaryPoint[0] == newParent892 && boundaryPoint[1] > newIndex) {893 boundaryPoint[1]++;894 }895 896 897 898 if (boundaryPoint[0] == oldParent899 && (boundaryPoint[1] == oldIndex900 || boundaryPoint[1] == oldIndex + 1)) {901 boundaryPoint[0] = newParent;902 boundaryPoint[1] += newIndex - oldIndex;903 }904 905 906 if (boundaryPoint[0] == oldParent907 && boundaryPoint[1] > oldIndex + 1) {908 boundaryPoint[1]--;909 }910 });911 912 if (newParent.childNodes.length == newIndex) {913 newParent.appendChild(node);914 } else {915 newParent.insertBefore(node, newParent.childNodes[newIndex]);916 }917 globalRange.setStart(boundaryPoints[0][0], boundaryPoints[0][1]);918 globalRange.setEnd(boundaryPoints[1][0], boundaryPoints[1][1]);919 for (var i = 0; i < extraRanges.length; i++) {920 extraRanges[i].setStart(boundaryPoints[2*i + 2][0], boundaryPoints[2*i + 2][1]);921 extraRanges[i].setEnd(boundaryPoints[2*i + 3][0], boundaryPoints[2*i + 3][1]);922 }923 getSelection().removeAllRanges();924 for (var i = 1 + extraRanges.length; i < ranges.length; i++) {925 var newRange = document.createRange();926 newRange.setStart(boundaryPoints[2*i][0], boundaryPoints[2*i][1]);927 newRange.setEnd(boundaryPoints[2*i + 1][0], boundaryPoints[2*i + 1][1]);928 getSelection().addRange(newRange);929 }930}931function setTagName(element, newName) {932 933 934 if (isHtmlElement(element, newName.toUpperCase())) {935 return element;936 }937 938 if (!element.parentNode) {939 return element;940 }941 942 943 var replacementElement = element.ownerDocument.createElement(newName);944 945 946 element.parentNode.insertBefore(replacementElement, element);947 948 for (var i = 0; i < element.attributes.length; i++) {949 replacementElement.setAttributeNS(element.attributes[i].namespaceURI, element.attributes[i].name, element.attributes[i].value);950 }951 952 953 while (element.childNodes.length) {954 movePreservingRanges(element.firstChild, replacementElement, replacementElement.childNodes.length);955 }956 957 element.parentNode.removeChild(element);958 959 return replacementElement;960}961function removeExtraneousLineBreaksBefore(node) {962 963 var ref = node.previousSibling;964 965 if (!ref) {966 return;967 }968 969 while (ref.hasChildNodes()) {970 ref = ref.lastChild;971 }972 973 974 while (isInvisible(ref)975 && !isExtraneousLineBreak(ref)976 && ref != node.parentNode) {977 ref = previousNode(ref);978 }979 980 981 if (isEditable(ref)982 && isExtraneousLineBreak(ref)) {983 ref.parentNode.removeChild(ref);984 }985}986function removeExtraneousLineBreaksAtTheEndOf(node) {987 988 var ref = node;989 990 while (ref.hasChildNodes()) {991 ref = ref.lastChild;992 }993 994 995 while (isInvisible(ref)996 && !isExtraneousLineBreak(ref)997 && ref != node) {998 ref = previousNode(ref);999 }1000 1001 if (isEditable(ref)1002 && isExtraneousLineBreak(ref)) {1003 1004 1005 while (isEditable(ref.parentNode)1006 && isInvisible(ref.parentNode)) {1007 ref = ref.parentNode;1008 }1009 1010 ref.parentNode.removeChild(ref);1011 }1012}1013function removeExtraneousLineBreaksFrom(node) {1014 removeExtraneousLineBreaksBefore(node);1015 removeExtraneousLineBreaksAtTheEndOf(node);1016}1017function wrap(nodeList, siblingCriteria, newParentInstructions) {1018 1019 1020 if (typeof siblingCriteria == "undefined") {1021 siblingCriteria = function() { return false };1022 }1023 if (typeof newParentInstructions == "undefined") {1024 newParentInstructions = function() { return null };1025 }1026 1027 1028 if (nodeList.every(isInvisible)1029 && !nodeList.some(function(node) { return isHtmlElement(node, "br") })) {1030 return null;1031 }1032 1033 1034 if (!nodeList[0].parentNode) {1035 return null;1036 }1037 1038 1039 if (isInlineNode(nodeList[nodeList.length - 1])1040 && !isHtmlElement(nodeList[nodeList.length - 1], "br")1041 && isHtmlElement(nodeList[nodeList.length - 1].nextSibling, "br")) {1042 nodeList.push(nodeList[nodeList.length - 1].nextSibling);1043 }1044 1045 1046 while (isInvisible(nodeList[0].previousSibling)) {1047 nodeList.unshift(nodeList[0].previousSibling);1048 }1049 1050 1051 while (isInvisible(nodeList[nodeList.length - 1].nextSibling)) {1052 nodeList.push(nodeList[nodeList.length - 1].nextSibling);1053 }1054 1055 1056 1057 var newParent;1058 if (isEditable(nodeList[0].previousSibling)1059 && siblingCriteria(nodeList[0].previousSibling)) {1060 newParent = nodeList[0].previousSibling;1061 1062 1063 1064 } else if (isEditable(nodeList[nodeList.length - 1].nextSibling)1065 && siblingCriteria(nodeList[nodeList.length - 1].nextSibling)) {1066 newParent = nodeList[nodeList.length - 1].nextSibling;1067 1068 1069 } else {1070 newParent = newParentInstructions();1071 }1072 1073 if (!newParent) {1074 return null;1075 }1076 1077 if (!newParent.parentNode) {1078 1079 1080 nodeList[0].parentNode.insertBefore(newParent, nodeList[0]);1081 1082 1083 1084 1085 1086 if (globalRange.startContainer == newParent.parentNode1087 && globalRange.startOffset == getNodeIndex(newParent)) {1088 globalRange.setStart(globalRange.startContainer, globalRange.startOffset + 1);1089 }1090 if (globalRange.endContainer == newParent.parentNode1091 && globalRange.endOffset == getNodeIndex(newParent)) {1092 globalRange.setEnd(globalRange.endContainer, globalRange.endOffset + 1);1093 }1094 }1095 1096 var originalParent = nodeList[0].parentNode;1097 1098 if (isBefore(newParent, nodeList[0])) {1099 1100 1101 1102 1103 1104 if (!isInlineNode(newParent)1105 && isInlineNode([].filter.call(newParent.childNodes, isVisible).slice(-1)[0])1106 && isInlineNode(nodeList.filter(isVisible)[0])1107 && !isHtmlElement(newParent.lastChild, "BR")) {1108 newParent.appendChild(newParent.ownerDocument.createElement("br"));1109 }1110 1111 1112 for (var i = 0; i < nodeList.length; i++) {1113 movePreservingRanges(nodeList[i], newParent, -1);1114 }1115 1116 } else {1117 1118 1119 1120 1121 1122 if (!isInlineNode(newParent)1123 && isInlineNode([].filter.call(newParent.childNodes, isVisible)[0])1124 && isInlineNode(nodeList.filter(isVisible).slice(-1)[0])1125 && !isHtmlElement(nodeList[nodeList.length - 1], "BR")) {1126 newParent.insertBefore(newParent.ownerDocument.createElement("br"), newParent.firstChild);1127 }1128 1129 1130 for (var i = nodeList.length - 1; i >= 0; i--) {1131 movePreservingRanges(nodeList[i], newParent, 0);1132 }1133 }1134 1135 1136 if (isEditable(originalParent) && !originalParent.hasChildNodes()) {1137 originalParent.parentNode.removeChild(originalParent);1138 }1139 1140 1141 if (isEditable(newParent.nextSibling)1142 && siblingCriteria(newParent.nextSibling)) {1143 1144 1145 1146 1147 1148 if (!isInlineNode(newParent)1149 && isInlineNode(newParent.lastChild)1150 && isInlineNode(newParent.nextSibling.firstChild)1151 && !isHtmlElement(newParent.lastChild, "BR")) {1152 newParent.appendChild(newParent.ownerDocument.createElement("br"));1153 }1154 1155 1156 while (newParent.nextSibling.hasChildNodes()) {1157 movePreservingRanges(newParent.nextSibling.firstChild, newParent, -1);1158 }1159 1160 newParent.parentNode.removeChild(newParent.nextSibling);1161 }1162 1163 removeExtraneousLineBreaksFrom(newParent);1164 1165 return newParent;1166}1167var namesOfElementsWithInlineContents = ["a", "abbr", "b", "bdi", "bdo",1168 "cite", "code", "dfn", "em", "h1", "h2", "h3", "h4", "h5", "h6", "i",1169 "kbd", "mark", "p", "pre", "q", "rp", "rt", "ruby", "s", "samp", "small",1170 "span", "strong", "sub", "sup", "u", "var", "acronym", "listing", "strike",1171 "xmp", "big", "blink", "font", "marquee", "nobr", "tt"];1172function isElementWithInlineContents(node) {1173 return isHtmlElement(node, namesOfElementsWithInlineContents);1174}1175function isAllowedChild(child, parent_) {1176 1177 1178 1179 1180 if ((["colgroup", "table", "tbody", "tfoot", "thead", "tr"].indexOf(parent_) != -11181 || isHtmlElement(parent_, ["colgroup", "table", "tbody", "tfoot", "thead", "tr"]))1182 && typeof child == "object"1183 && child.nodeType == Node.TEXT_NODE1184 && !/^[ \t\n\f\r]*$/.test(child.data)) {1185 return false;1186 }1187 1188 1189 1190 if ((["script", "style", "plaintext", "xmp"].indexOf(parent_) != -11191 || isHtmlElement(parent_, ["script", "style", "plaintext", "xmp"]))1192 && (typeof child != "object" || child.nodeType != Node.TEXT_NODE)) {1193 return false;1194 }1195 1196 1197 if (typeof child == "object"1198 && (child.nodeType == Node.DOCUMENT_NODE1199 || child.nodeType == Node.DOCUMENT_FRAGMENT_NODE1200 || child.nodeType == Node.DOCUMENT_TYPE_NODE)) {1201 return false;1202 }1203 1204 if (isHtmlElement(child)) {1205 child = child.tagName.toLowerCase();1206 }1207 1208 if (typeof child != "string") {1209 return true;1210 }1211 1212 if (isHtmlElement(parent_)) {1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 var ancestor = parent_;1224 while (ancestor) {1225 if (child == "a" && isHtmlElement(ancestor, "a")) {1226 return false;1227 }1228 if (prohibitedParagraphChildNames.indexOf(child) != -11229 && isElementWithInlineContents(ancestor)) {1230 return false;1231 }1232 if (/^h[1-6]$/.test(child)1233 && isHtmlElement(ancestor)1234 && /^H[1-6]$/.test(ancestor.tagName)) {1235 return false;1236 }1237 ancestor = ancestor.parentNode;1238 }1239 1240 parent_ = parent_.tagName.toLowerCase();1241 }1242 1243 if (typeof parent_ == "object"1244 && (parent_.nodeType == Node.ELEMENT_NODE1245 || parent_.nodeType == Node.DOCUMENT_FRAGMENT_NODE)) {1246 return true;1247 }1248 1249 if (typeof parent_ != "string") {1250 return false;1251 }1252 1253 1254 1255 switch (parent_) {1256 case "colgroup":1257 return child == "col";1258 case "table":1259 return ["caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"].indexOf(child) != -1;1260 case "tbody":1261 case "thead":1262 case "tfoot":1263 return ["td", "th", "tr"].indexOf(child) != -1;1264 case "tr":1265 return ["td", "th"].indexOf(child) != -1;1266 case "dl":1267 return ["dt", "dd"].indexOf(child) != -1;1268 case "dir":1269 case "ol":1270 case "ul":1271 return ["dir", "li", "ol", "ul"].indexOf(child) != -1;1272 case "hgroup":1273 return /^h[1-6]$/.test(child);1274 }1275 1276 1277 1278 if (["body", "caption", "col", "colgroup", "frame", "frameset", "head",1279 "html", "tbody", "td", "tfoot", "th", "thead", "tr"].indexOf(child) != -1) {1280 return false;1281 }1282 1283 if (["dd", "dt"].indexOf(child) != -11284 && parent_ != "dl") {1285 return false;1286 }1287 1288 if (child == "li"1289 && parent_ != "ol"1290 && parent_ != "ul") {1291 return false;1292 }1293 1294 1295 var table = [1296 [["a"], ["a"]],1297 [["dd", "dt"], ["dd", "dt"]],1298 [["h1", "h2", "h3", "h4", "h5", "h6"], ["h1", "h2", "h3", "h4", "h5", "h6"]],1299 [["li"], ["li"]],1300 [["nobr"], ["nobr"]],1301 [namesOfElementsWithInlineContents, prohibitedParagraphChildNames],1302 [["td", "th"], ["caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"]],1303 ];1304 for (var i = 0; i < table.length; i++) {1305 if (table[i][0].indexOf(parent_) != -11306 && table[i][1].indexOf(child) != -1) {1307 return false;1308 }1309 }1310 1311 return true;1312}1313function isEffectivelyContained(node, range) {1314 if (range.collapsed) {1315 return false;1316 }1317 1318 if (isContained(node, range)) {1319 return true;1320 }1321 1322 1323 if (node == range.startContainer1324 && node.nodeType == Node.TEXT_NODE1325 && getNodeLength(node) != range.startOffset) {1326 return true;1327 }1328 1329 1330 if (node == range.endContainer1331 && node.nodeType == Node.TEXT_NODE1332 && range.endOffset != 0) {1333 return true;1334 }1335 1336 1337 1338 1339 1340 if (node.hasChildNodes()1341 && [].every.call(node.childNodes, function(child) { return isEffectivelyContained(child, range) })1342 && (!isDescendant(range.startContainer, node)1343 || range.startContainer.nodeType != Node.TEXT_NODE1344 || range.startOffset == 0)1345 && (!isDescendant(range.endContainer, node)1346 || range.endContainer.nodeType != Node.TEXT_NODE1347 || range.endOffset == getNodeLength(range.endContainer))) {1348 return true;1349 }1350 return false;1351}1352function getEffectivelyContainedNodes(range, condition) {1353 if (typeof condition == "undefined") {1354 condition = function() { return true };1355 }1356 var node = range.startContainer;1357 while (isEffectivelyContained(node.parentNode, range)) {1358 node = node.parentNode;1359 }1360 var stop = nextNodeDescendants(range.endContainer);1361 var nodeList = [];1362 while (isBefore(node, stop)) {1363 if (isEffectivelyContained(node, range)1364 && condition(node)) {1365 nodeList.push(node);1366 node = nextNodeDescendants(node);1367 continue;1368 }1369 node = nextNode(node);1370 }1371 return nodeList;1372}1373function getAllEffectivelyContainedNodes(range, condition) {1374 if (typeof condition == "undefined") {1375 condition = function() { return true };1376 }1377 var node = range.startContainer;1378 while (isEffectivelyContained(node.parentNode, range)) {1379 node = node.parentNode;1380 }1381 var stop = nextNodeDescendants(range.endContainer);1382 var nodeList = [];1383 while (isBefore(node, stop)) {1384 if (isEffectivelyContained(node, range)1385 && condition(node)) {1386 nodeList.push(node);1387 }1388 node = nextNode(node);1389 }1390 return nodeList;1391}1392function isModifiableElement(node) {1393 if (!isHtmlElement(node)) {1394 return false;1395 }1396 if (["B", "EM", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"].indexOf(node.tagName) != -1) {1397 if (node.attributes.length == 0) {1398 return true;1399 }1400 if (node.attributes.length == 11401 && node.hasAttribute("style")) {1402 return true;1403 }1404 }1405 if (node.tagName == "FONT" || node.tagName == "A") {1406 var numAttrs = node.attributes.length;1407 if (node.hasAttribute("style")) {1408 numAttrs--;1409 }1410 if (node.tagName == "FONT") {1411 if (node.hasAttribute("color")) {1412 numAttrs--;1413 }1414 if (node.hasAttribute("face")) {1415 numAttrs--;1416 }1417 if (node.hasAttribute("size")) {1418 numAttrs--;1419 }1420 }1421 if (node.tagName == "A"1422 && node.hasAttribute("href")) {1423 numAttrs--;1424 }1425 if (numAttrs == 0) {1426 return true;1427 }1428 }1429 return false;1430}1431function isSimpleModifiableElement(node) {1432 1433 1434 if (!isHtmlElement(node)) {1435 return false;1436 }1437 1438 if (["A", "B", "EM", "FONT", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"].indexOf(node.tagName) == -1) {1439 return false;1440 }1441 1442 1443 if (node.attributes.length == 0) {1444 return true;1445 }1446 1447 if (node.attributes.length > 1) {1448 return false;1449 }1450 1451 1452 1453 1454 1455 if (node.hasAttribute("style")1456 && node.style.length == 0) {1457 return true;1458 }1459 1460 if (node.tagName == "A"1461 && node.hasAttribute("href")) {1462 return true;1463 }1464 1465 1466 if (node.tagName == "FONT"1467 && (node.hasAttribute("color")1468 || node.hasAttribute("face")1469 || node.hasAttribute("size")1470 )) {1471 return true;1472 }1473 1474 1475 1476 if ((node.tagName == "B" || node.tagName == "STRONG")1477 && node.hasAttribute("style")1478 && node.style.length == 11479 && node.style.fontWeight != "") {1480 return true;1481 }1482 1483 1484 1485 if ((node.tagName == "I" || node.tagName == "EM")1486 && node.hasAttribute("style")1487 && node.style.length == 11488 && node.style.fontStyle != "") {1489 return true;1490 }1491 1492 1493 1494 1495 if ((node.tagName == "A" || node.tagName == "FONT" || node.tagName == "SPAN")1496 && node.hasAttribute("style")1497 && node.style.length == 11498 && node.style.textDecoration == "") {1499 return true;1500 }1501 1502 1503 1504 1505 1506 1507 1508 1509 if (["A", "FONT", "S", "SPAN", "STRIKE", "U"].indexOf(node.tagName) != -11510 && node.hasAttribute("style")1511 && (node.style.length == 11512 || (node.style.length == 41513 && "MozTextBlink" in node.style1514 && "textDecorationColor" in node.style1515 && "textDecorationLine" in node.style1516 && "textDecorationStyle" in node.style)1517 )1518 && (node.style.textDecoration == "line-through"1519 || node.style.textDecoration == "underline"1520 || node.style.textDecoration == "overline"1521 || node.style.textDecoration == "none")) {1522 return true;1523 }1524 return false;1525}1526function isFormattableNode(node) {1527 return isEditable(node)1528 && isVisible(node)1529 && (node.nodeType == Node.TEXT_NODE1530 || isHtmlElement(node, ["img", "br"]));1531}1532function areEquivalentValues(command, val1, val2) {1533 if (val1 === null && val2 === null) {1534 return true;1535 }1536 if (typeof val1 == "string"1537 && typeof val2 == "string"1538 && val1 == val21539 && !("equivalentValues" in commands[command])) {1540 return true;1541 }1542 if (typeof val1 == "string"1543 && typeof val2 == "string"1544 && "equivalentValues" in commands[command]1545 && commands[command].equivalentValues(val1, val2)) {1546 return true;1547 }1548 return false;1549}1550function areLooselyEquivalentValues(command, val1, val2) {1551 if (areEquivalentValues(command, val1, val2)) {1552 return true;1553 }1554 if (command != "fontsize"1555 || typeof val1 != "string"1556 || typeof val2 != "string") {1557 return false;1558 }1559 1560 var callee = areLooselyEquivalentValues;1561 if (callee.sizeMap === undefined) {1562 callee.sizeMap = {};1563 var font = document.createElement("font");1564 document.body.appendChild(font);1565 ["x-small", "small", "medium", "large", "x-large", "xx-large",1566 "xxx-large"].forEach(function(keyword) {1567 font.size = cssSizeToLegacy(keyword);1568 callee.sizeMap[keyword] = getComputedStyle(font).fontSize;1569 });1570 document.body.removeChild(font);1571 }1572 return val1 === callee.sizeMap[val2]1573 || val2 === callee.sizeMap[val1];1574}1575function getEffectiveCommandValue(node, command) {1576 1577 if (node.nodeType != Node.ELEMENT_NODE1578 && (!node.parentNode || node.parentNode.nodeType != Node.ELEMENT_NODE)) {1579 return null;1580 }1581 1582 1583 if (node.nodeType != Node.ELEMENT_NODE) {1584 return getEffectiveCommandValue(node.parentNode, command);1585 }1586 1587 if (command == "createlink" || command == "unlink") {1588 1589 1590 while (node1591 && (!isHtmlElement(node)1592 || node.tagName != "A"1593 || !node.hasAttribute("href"))) {1594 node = node.parentNode;1595 }1596 1597 if (!node) {1598 return null;1599 }1600 1601 return node.getAttribute("href");1602 }1603 1604 if (command == "backcolor"1605 || command == "hilitecolor") {1606 1607 1608 1609 1610 1611 while ((getComputedStyle(node).backgroundColor == "rgba(0, 0, 0, 0)"1612 || getComputedStyle(node).backgroundColor === ""1613 || getComputedStyle(node).backgroundColor == "transparent")1614 && node.parentNode1615 && node.parentNode.nodeType == Node.ELEMENT_NODE) {1616 node = node.parentNode;1617 }1618 1619 return getComputedStyle(node).backgroundColor;1620 }1621 1622 if (command == "subscript" || command == "superscript") {1623 1624 1625 var affectedBySubscript = false;1626 var affectedBySuperscript = false;1627 1628 while (isInlineNode(node)) {1629 var verticalAlign = getComputedStyle(node).verticalAlign;1630 1631 if (isHtmlElement(node, "sub")) {1632 affectedBySubscript = true;1633 1634 1635 } else if (isHtmlElement(node, "sup")) {1636 affectedBySuperscript = true;1637 }1638 1639 node = node.parentNode;1640 }1641 1642 1643 if (affectedBySubscript && affectedBySuperscript) {1644 return "mixed";1645 }1646 1647 if (affectedBySubscript) {1648 return "subscript";1649 }1650 1651 if (affectedBySuperscript) {1652 return "superscript";1653 }1654 1655 return null;1656 }1657 1658 1659 1660 if (command == "strikethrough") {1661 do {1662 if (getComputedStyle(node).textDecoration.indexOf("line-through") != -1) {1663 return "line-through";1664 }1665 node = node.parentNode;1666 } while (node && node.nodeType == Node.ELEMENT_NODE);1667 return null;1668 }1669 1670 1671 1672 if (command == "underline") {1673 do {1674 if (getComputedStyle(node).textDecoration.indexOf("underline") != -1) {1675 return "underline";1676 }1677 node = node.parentNode;1678 } while (node && node.nodeType == Node.ELEMENT_NODE);1679 return null;1680 }1681 if (!("relevantCssProperty" in commands[command])) {1682 throw "Bug: no relevantCssProperty for " + command + " in getEffectiveCommandValue";1683 }1684 1685 1686 return getComputedStyle(node)[commands[command].relevantCssProperty];1687}1688function getSpecifiedCommandValue(element, command) {1689 1690 1691 if ((command == "backcolor" || command == "hilitecolor")1692 && getComputedStyle(element).display != "inline") {1693 return null;1694 }1695 1696 if (command == "createlink" || command == "unlink") {1697 1698 1699 if (isHtmlElement(element)1700 && element.tagName == "A"1701 && element.hasAttribute("href")) {1702 return element.getAttribute("href");1703 }1704 1705 return null;1706 }1707 1708 if (command == "subscript" || command == "superscript") {1709 1710 if (isHtmlElement(element, "sup")) {1711 return "superscript";1712 }1713 1714 if (isHtmlElement(element, "sub")) {1715 return "subscript";1716 }1717 1718 return null;1719 }1720 1721 1722 if (command == "strikethrough"1723 && element.style.textDecoration != "") {1724 1725 1726 if (element.style.textDecoration.indexOf("line-through") != -1) {1727 return "line-through";1728 }1729 1730 return null;1731 }1732 1733 1734 if (command == "strikethrough"1735 && isHtmlElement(element, ["S", "STRIKE"])) {1736 return "line-through";1737 }1738 1739 1740 if (command == "underline"1741 && element.style.textDecoration != "") {1742 1743 1744 if (element.style.textDecoration.indexOf("underline") != -1) {1745 return "underline";1746 }1747 1748 return null;1749 }1750 1751 1752 if (command == "underline"1753 && isHtmlElement(element, "U")) {1754 return "underline";1755 }1756 1757 var property = commands[command].relevantCssProperty;1758 1759 if (property === null) {1760 return null;1761 }1762 1763 1764 if (element.style[property] != "") {1765 return element.style[property];1766 }1767 1768 1769 1770 1771 if (isHtmlNamespace(element.namespaceURI)1772 && element.tagName == "FONT") {1773 if (property == "color" && element.hasAttribute("color")) {1774 return element.color;1775 }1776 if (property == "fontFamily" && element.hasAttribute("face")) {1777 return element.face;1778 }1779 if (property == "fontSize" && element.hasAttribute("size")) {1780 1781 var size = parseInt(element.size);1782 if (size < 1) {1783 size = 1;1784 }1785 if (size > 7) {1786 size = 7;1787 }1788 return {1789 1: "x-small",1790 2: "small",1791 3: "medium",1792 4: "large",1793 5: "x-large",1794 6: "xx-large",1795 7: "xxx-large"1796 }[size];1797 }1798 }1799 1800 1801 1802 1803 if (property == "fontWeight"1804 && (element.tagName == "B" || element.tagName == "STRONG")) {1805 return "bold";1806 }1807 if (property == "fontStyle"1808 && (element.tagName == "I" || element.tagName == "EM")) {1809 return "italic";1810 }1811 1812 return null;1813}1814function reorderModifiableDescendants(node, command, newValue) {1815 1816 var candidate = node;1817 1818 1819 1820 1821 while (isModifiableElement(candidate)1822 && candidate.childNodes.length == 11823 && isModifiableElement(candidate.firstChild)1824 && (!isSimpleModifiableElement(candidate)1825 || !areEquivalentValues(command, getSpecifiedCommandValue(candidate, command), newValue))) {1826 candidate = candidate.firstChild;1827 }1828 1829 1830 1831 1832 if (candidate == node1833 || !isSimpleModifiableElement(candidate)1834 || !areEquivalentValues(command, getSpecifiedCommandValue(candidate, command), newValue)1835 || !areLooselyEquivalentValues(command, getEffectiveCommandValue(candidate, command), newValue)) {1836 return;1837 }1838 1839 1840 while (candidate.hasChildNodes()) {1841 movePreservingRanges(candidate.firstChild, candidate.parentNode, getNodeIndex(candidate));1842 }1843 1844 node.parentNode.insertBefore(candidate, node.nextSibling);1845 1846 movePreservingRanges(node, candidate, -1);1847}1848function recordValues(nodeList) {1849 1850 1851 var values = [];1852 1853 1854 1855 nodeList.forEach(function(node) {1856 ["subscript", "bold", "fontname", "fontsize", "forecolor",1857 "hilitecolor", "italic", "strikethrough", "underline"].forEach(function(command) {1858 1859 var ancestor = node;1860 1861 if (ancestor.nodeType != Node.ELEMENT_NODE) {1862 ancestor = ancestor.parentNode;1863 }1864 1865 1866 while (ancestor1867 && ancestor.nodeType == Node.ELEMENT_NODE1868 && getSpecifiedCommandValue(ancestor, command) === null) {1869 ancestor = ancestor.parentNode;1870 }1871 1872 1873 1874 if (ancestor && ancestor.nodeType == Node.ELEMENT_NODE) {1875 values.push([node, command, getSpecifiedCommandValue(ancestor, command)]);1876 } else {1877 values.push([node, command, null]);1878 }1879 });1880 });1881 1882 return values;1883}1884function restoreValues(values) {1885 1886 values.forEach(function(triple) {1887 var node = triple[0];1888 var command = triple[1];1889 var value = triple[2];1890 1891 var ancestor = node;1892 1893 if (!ancestor || ancestor.nodeType != Node.ELEMENT_NODE) {1894 ancestor = ancestor.parentNode;1895 }1896 1897 1898 while (ancestor1899 && ancestor.nodeType == Node.ELEMENT_NODE1900 && getSpecifiedCommandValue(ancestor, command) === null) {1901 ancestor = ancestor.parentNode;1902 }1903 1904 1905 if (value === null1906 && ancestor1907 && ancestor.nodeType == Node.ELEMENT_NODE) {1908 pushDownValues(node, command, null);1909 1910 1911 1912 1913 } else if ((ancestor1914 && ancestor.nodeType == Node.ELEMENT_NODE1915 && !areEquivalentValues(command, getSpecifiedCommandValue(ancestor, command), value))1916 || ((!ancestor || ancestor.nodeType != Node.ELEMENT_NODE)1917 && value !== null)) {1918 forceValue(node, command, value);1919 }1920 });1921}1922function clearValue(element, command) {1923 1924 if (!isEditable(element)) {1925 return [];1926 }1927 1928 1929 if (getSpecifiedCommandValue(element, command) === null) {1930 return [];1931 }1932 1933 if (isSimpleModifiableElement(element)) {1934 1935 var children = Array.prototype.slice.call(element.childNodes);1936 1937 1938 for (var i = 0; i < children.length; i++) {1939 movePreservingRanges(children[i], element.parentNode, getNodeIndex(element));1940 }1941 1942 element.parentNode.removeChild(element);1943 1944 return children;1945 }1946 1947 1948 1949 if (command == "strikethrough"1950 && element.style.textDecoration.indexOf("line-through") != -1) {1951 if (element.style.textDecoration == "line-through") {1952 element.style.textDecoration = "";1953 } else {1954 element.style.textDecoration = element.style.textDecoration.replace("line-through", "");1955 }1956 if (element.getAttribute("style") == "") {1957 element.removeAttribute("style");1958 }1959 }1960 1961 1962 1963 if (command == "underline"1964 && element.style.textDecoration.indexOf("underline") != -1) {1965 if (element.style.textDecoration == "underline") {1966 element.style.textDecoration = "";1967 } else {1968 element.style.textDecoration = element.style.textDecoration.replace("underline", "");1969 }1970 if (element.getAttribute("style") == "") {1971 element.removeAttribute("style");1972 }1973 }1974 1975 1976 if (commands[command].relevantCssProperty !== null) {1977 element.style[commands[command].relevantCssProperty] = '';1978 if (element.getAttribute("style") == "") {1979 element.removeAttribute("style");1980 }1981 }1982 1983 if (isHtmlNamespace(element.namespaceURI) && element.tagName == "FONT") {1984 1985 if (command == "forecolor") {1986 element.removeAttribute("color");1987 }1988 1989 if (command == "fontname") {1990 element.removeAttribute("face");1991 }1992 1993 if (command == "fontsize") {1994 element.removeAttribute("size");1995 }1996 }1997 1998 1999 if (isHtmlElement(element, "A")2000 && (command == "createlink" || command == "unlink")) {2001 element.removeAttribute("href");2002 }2003 2004 2005 if (getSpecifiedCommandValue(element, command) === null) {2006 return [];2007 }2008 2009 2010 return [setTagName(element, "span")];2011}2012function pushDownValues(node, command, newValue) {2013 2014 if (!node.parentNode2015 || node.parentNode.nodeType != Node.ELEMENT_NODE) {2016 return;2017 }2018 2019 2020 if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) {2021 return;2022 }2023 2024 var currentAncestor = node.parentNode;2025 2026 var ancestorList = [];2027 2028 2029 2030 2031 while (isEditable(currentAncestor)2032 && currentAncestor.nodeType == Node.ELEMENT_NODE2033 && !areLooselyEquivalentValues(command, getEffectiveCommandValue(currentAncestor, command), newValue)) {2034 ancestorList.push(currentAncestor);2035 currentAncestor = currentAncestor.parentNode;2036 }2037 2038 if (!ancestorList.length) {2039 return;2040 }2041 2042 2043 var propagatedValue = getSpecifiedCommandValue(ancestorList[ancestorList.length - 1], command);2044 2045 2046 if (propagatedValue === null && propagatedValue != newValue) {2047 return;2048 }2049 2050 2051 2052 if (newValue !== null2053 && !areLooselyEquivalentValues(command, getEffectiveCommandValue(ancestorList[ancestorList.length - 1].parentNode, command), newValue)) {2054 return;2055 }2056 2057 while (ancestorList.length) {2058 2059 2060 var currentAncestor = ancestorList.pop();2061 2062 2063 if (getSpecifiedCommandValue(currentAncestor, command) !== null) {2064 propagatedValue = getSpecifiedCommandValue(currentAncestor, command);2065 }2066 2067 var children = Array.prototype.slice.call(currentAncestor.childNodes);2068 2069 2070 if (getSpecifiedCommandValue(currentAncestor, command) !== null) {2071 clearValue(currentAncestor, command);2072 }2073 2074 for (var i = 0; i < children.length; i++) {2075 var child = children[i];2076 2077 if (child == node) {2078 continue;2079 }2080 2081 2082 2083 if (child.nodeType == Node.ELEMENT_NODE2084 && getSpecifiedCommandValue(child, command) !== null2085 && !areEquivalentValues(command, propagatedValue, getSpecifiedCommandValue(child, command))) {2086 continue;2087 }2088 2089 2090 if (child == ancestorList[ancestorList.length - 1]) {2091 continue;2092 }2093 2094 2095 forceValue(child, command, propagatedValue);2096 }2097 }2098}2099function forceValue(node, command, newValue) {2100 2101 if (!node.parentNode) {2102 return;2103 }2104 2105 if (newValue === null) {2106 return;2107 }2108 2109 if (isAllowedChild(node, "span")) {2110 2111 reorderModifiableDescendants(node.previousSibling, command, newValue);2112 2113 reorderModifiableDescendants(node.nextSibling, command, newValue);2114 2115 2116 2117 2118 2119 wrap([node],2120 function(node) {2121 return isSimpleModifiableElement(node)2122 && areEquivalentValues(command, getSpecifiedCommandValue(node, command), newValue)2123 && areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue);2124 },2125 function() { return null }2126 );2127 }2128 2129 if (isInvisible(node)) {2130 return;2131 }2132 2133 2134 if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) {2135 return;2136 }2137 2138 if (!isAllowedChild(node, "span")) {2139 2140 2141 2142 var children = [];2143 for (var i = 0; i < node.childNodes.length; i++) {2144 if (node.childNodes[i].nodeType == Node.ELEMENT_NODE) {2145 var specifiedValue = getSpecifiedCommandValue(node.childNodes[i], command);2146 if (specifiedValue !== null2147 && !areEquivalentValues(command, newValue, specifiedValue)) {2148 continue;2149 }2150 }2151 children.push(node.childNodes[i]);2152 }2153 2154 2155 for (var i = 0; i < children.length; i++) {2156 forceValue(children[i], command, newValue);2157 }2158 2159 return;2160 }2161 2162 2163 if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) {2164 return;2165 }2166 2167 var newParent = null;2168 2169 if (!cssStylingFlag) {2170 2171 2172 if (command == "bold" && (newValue == "bold" || newValue == "700")) {2173 newParent = node.ownerDocument.createElement("b");2174 }2175 2176 2177 2178 if (command == "italic" && newValue == "italic") {2179 newParent = node.ownerDocument.createElement("i");2180 }2181 2182 2183 2184 if (command == "strikethrough" && newValue == "line-through") {2185 newParent = node.ownerDocument.createElement("s");2186 }2187 2188 2189 2190 if (command == "underline" && newValue == "underline") {2191 newParent = node.ownerDocument.createElement("u");2192 }2193 2194 2195 if (command == "forecolor" && parseSimpleColor(newValue)) {2196 2197 2198 newParent = node.ownerDocument.createElement("font");2199 2200 2201 2202 newParent.setAttribute("color", parseSimpleColor(newValue));2203 }2204 2205 2206 2207 if (command == "fontname") {2208 newParent = node.ownerDocument.createElement("font");2209 newParent.face = newValue;2210 }2211 }2212 2213 if (command == "createlink" || command == "unlink") {2214 2215 2216 newParent = node.ownerDocument.createElement("a");2217 2218 newParent.setAttribute("href", newValue);2219 2220 var ancestor = node.parentNode;2221 2222 while (ancestor) {2223 2224 2225 if (isHtmlElement(ancestor, "A")) {2226 ancestor = setTagName(ancestor, "span");2227 }2228 2229 ancestor = ancestor.parentNode;2230 }2231 }2232 2233 2234 2235 2236 2237 2238 if (command == "fontsize"2239 && ["x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"].indexOf(newValue) != -12240 && (!cssStylingFlag || newValue == "xxx-large")) {2241 newParent = node.ownerDocument.createElement("font");2242 newParent.size = cssSizeToLegacy(newValue);2243 }2244 2245 2246 2247 if ((command == "subscript" || command == "superscript")2248 && newValue == "subscript") {2249 newParent = node.ownerDocument.createElement("sub");2250 }2251 2252 2253 2254 if ((command == "subscript" || command == "superscript")2255 && newValue == "superscript") {2256 newParent = node.ownerDocument.createElement("sup");2257 }2258 2259 2260 if (!newParent) {2261 newParent = node.ownerDocument.createElement("span");2262 }2263 2264 node.parentNode.insertBefore(newParent, node);2265 2266 2267 2268 2269 var property = commands[command].relevantCssProperty;2270 if (property !== null2271 && !areLooselyEquivalentValues(command, getEffectiveCommandValue(newParent, command), newValue)) {2272 newParent.style[property] = newValue;2273 }2274 2275 2276 2277 2278 if (command == "strikethrough"2279 && newValue == "line-through"2280 && getEffectiveCommandValue(newParent, "strikethrough") != "line-through") {2281 newParent.style.textDecoration = "line-through";2282 }2283 2284 2285 2286 2287 if (command == "underline"2288 && newValue == "underline"2289 && getEffectiveCommandValue(newParent, "underline") != "underline") {2290 newParent.style.textDecoration = "underline";2291 }2292 2293 movePreservingRanges(node, newParent, newParent.childNodes.length);2294 2295 2296 if (node.nodeType == Node.ELEMENT_NODE2297 && !areEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) {2298 2299 2300 movePreservingRanges(node, newParent.parentNode, getNodeIndex(newParent));2301 2302 newParent.parentNode.removeChild(newParent);2303 2304 2305 2306 var children = [];2307 for (var i = 0; i < node.childNodes.length; i++) {2308 if (node.childNodes[i].nodeType == Node.ELEMENT_NODE) {2309 var specifiedValue = getSpecifiedCommandValue(node.childNodes[i], command);2310 if (specifiedValue !== null2311 && !areEquivalentValues(command, newValue, specifiedValue)) {2312 continue;2313 }2314 }2315 children.push(node.childNodes[i]);2316 }2317 2318 2319 for (var i = 0; i < children.length; i++) {2320 forceValue(children[i], command, newValue);2321 }2322 }2323}2324function setSelectionValue(command, newValue) {2325 2326 2327 if (!getAllEffectivelyContainedNodes(getActiveRange())2328 .some(isFormattableNode)) {2329 2330 2331 if ("inlineCommandActivatedValues" in commands[command]) {2332 setStateOverride(command, commands[command].inlineCommandActivatedValues2333 .indexOf(newValue) != -1);2334 }2335 2336 2337 if (command == "subscript") {2338 unsetStateOverride("superscript");2339 }2340 2341 2342 if (command == "superscript") {2343 unsetStateOverride("subscript");2344 }2345 2346 if (newValue === null) {2347 unsetValueOverride(command);2348 2349 2350 } else if (command == "createlink" || "value" in commands[command]) {2351 setValueOverride(command, newValue);2352 }2353 2354 return;2355 }2356 2357 2358 2359 2360 2361 if (isEditable(getActiveRange().startContainer)2362 && getActiveRange().startContainer.nodeType == Node.TEXT_NODE2363 && getActiveRange().startOffset != 02364 && getActiveRange().startOffset != getNodeLength(getActiveRange().startContainer)) {2365 2366 var newActiveRange = document.createRange();2367 var newNode;2368 if (getActiveRange().startContainer == getActiveRange().endContainer) {2369 var newEndOffset = getActiveRange().endOffset - getActiveRange().startOffset;2370 newNode = getActiveRange().startContainer.splitText(getActiveRange().startOffset);2371 newActiveRange.setEnd(newNode, newEndOffset);2372 getActiveRange().setEnd(newNode, newEndOffset);2373 } else {2374 newNode = getActiveRange().startContainer.splitText(getActiveRange().startOffset);2375 }2376 newActiveRange.setStart(newNode, 0);2377 getSelection().removeAllRanges();2378 getSelection().addRange(newActiveRange);2379 getActiveRange().setStart(newNode, 0);2380 }2381 2382 2383 2384 2385 if (isEditable(getActiveRange().endContainer)2386 && getActiveRange().endContainer.nodeType == Node.TEXT_NODE2387 && getActiveRange().endOffset != 02388 && getActiveRange().endOffset != getNodeLength(getActiveRange().endContainer)) {2389 2390 2391 2392 2393 var activeRange = getActiveRange();2394 var newStart = [activeRange.startContainer, activeRange.startOffset];2395 var newEnd = [activeRange.endContainer, activeRange.endOffset];2396 activeRange.endContainer.splitText(activeRange.endOffset);2397 activeRange.setStart(newStart[0], newStart[1]);2398 activeRange.setEnd(newEnd[0], newEnd[1]);2399 getSelection().removeAllRanges();2400 getSelection().addRange(activeRange);2401 }2402 2403 2404 2405 2406 getAllEffectivelyContainedNodes(getActiveRange(), function(node) {2407 return isEditable(node) && node.nodeType == Node.ELEMENT_NODE;2408 }).forEach(function(element) {2409 clearValue(element, command);2410 });2411 2412 2413 2414 2415 getAllEffectivelyContainedNodes(getActiveRange(), isEditable).forEach(function(node) {2416 2417 pushDownValues(node, command, newValue);2418 2419 if (isAllowedChild(node, "span")) {2420 forceValue(node, command, newValue);2421 }2422 });2423}2424commands.backcolor = {2425 2426 action: function(value) {2427 2428 2429 2430 2431 2432 2433 2434 if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) {2435 value = "#" + value;2436 }2437 if (!/^(rgba?|hsla?)\(.*\)$/.test(value)2438 && !parseSimpleColor(value)2439 && value.toLowerCase() != "transparent") {2440 return false;2441 }2442 2443 setSelectionValue("backcolor", value);2444 2445 return true;2446 }, standardInlineValueCommand: true, relevantCssProperty: "backgroundColor",2447 equivalentValues: function(val1, val2) {2448 2449 2450 2451 return normalizeColor(val1) === normalizeColor(val2);2452 },2453};2454commands.bold = {2455 action: function() {2456 2457 2458 2459 if (myQueryCommandState("bold")) {2460 setSelectionValue("bold", "normal");2461 } else {2462 setSelectionValue("bold", "bold");2463 }2464 return true;2465 }, inlineCommandActivatedValues: ["bold", "600", "700", "800", "900"],2466 relevantCssProperty: "fontWeight",2467 equivalentValues: function(val1, val2) {2468 2469 2470 return val1 == val22471 || (val1 == "bold" && val2 == "700")2472 || (val1 == "700" && val2 == "bold")2473 || (val1 == "normal" && val2 == "400")2474 || (val1 == "400" && val2 == "normal");2475 },2476};2477commands.createlink = {2478 action: function(value) {2479 2480 if (value === "") {2481 return false;2482 }2483 2484 2485 2486 2487 2488 2489 getAllEffectivelyContainedNodes(getActiveRange()).forEach(function(node) {2490 getAncestors(node).forEach(function(ancestor) {2491 if (isEditable(ancestor)2492 && isHtmlElement(ancestor, "a")2493 && ancestor.hasAttribute("href")) {2494 ancestor.setAttribute("href", value);2495 }2496 });2497 });2498 2499 setSelectionValue("createlink", value);2500 2501 return true;2502 }2503};2504commands.fontname = {2505 action: function(value) {2506 2507 setSelectionValue("fontname", value);2508 return true;2509 }, standardInlineValueCommand: true, relevantCssProperty: "fontFamily"2510};2511function normalizeFontSize(value) {2512 2513 2514 2515 value = value.trim();2516 2517 2518 2519 if (!/^[-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?$/.test(value)) {2520 return null;2521 }2522 var mode;2523 2524 2525 if (value[0] == "+") {2526 value = value.slice(1);2527 mode = "relative-plus";2528 2529 2530 } else if (value[0] == "-") {2531 value = value.slice(1);2532 mode = "relative-minus";2533 2534 } else {2535 mode = "absolute";2536 }2537 2538 2539 2540 2541 var num = parseInt(value);2542 2543 if (mode == "relative-plus") {2544 num += 3;2545 }2546 2547 if (mode == "relative-minus") {2548 num = 3 - num;2549 }2550 2551 if (num < 1) {2552 num = 1;2553 }2554 2555 if (num > 7) {2556 num = 7;2557 }2558 2559 value = {2560 1: "x-small",2561 2: "small",2562 3: "medium",2563 4: "large",2564 5: "x-large",2565 6: "xx-large",2566 7: "xxx-large"2567 }[num];2568 return value;2569}2570commands.fontsize = {2571 action: function(value) {2572 value = normalizeFontSize(value);2573 if (value === null) {2574 return false;2575 }2576 2577 setSelectionValue("fontsize", value);2578 2579 return true;2580 }, indeterm: function() {2581 2582 2583 2584 return getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode)2585 .map(function(node) {2586 return getEffectiveCommandValue(node, "fontsize");2587 }).filter(function(value, i, arr) {2588 return arr.slice(0, i).indexOf(value) == -1;2589 }).length >= 2;2590 }, value: function() {2591 2592 if (!getActiveRange()) {2593 return "";2594 }2595 2596 2597 2598 2599 2600 var node = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode)[0];2601 if (node === undefined) {2602 node = getActiveRange().startContainer;2603 }2604 var pixelSize = getEffectiveCommandValue(node, "fontsize");2605 2606 return getLegacyFontSize(pixelSize);2607 }, relevantCssProperty: "fontSize"2608};2609function getLegacyFontSize(size) {2610 if (getLegacyFontSize.resultCache === undefined) {2611 getLegacyFontSize.resultCache = {};2612 }2613 if (getLegacyFontSize.resultCache[size] !== undefined) {2614 return getLegacyFontSize.resultCache[size];2615 }2616 2617 2618 2619 if (normalizeFontSize(size) !== null) {2620 return getLegacyFontSize.resultCache[size] = cssSizeToLegacy(normalizeFontSize(size));2621 }2622 if (["x-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"].indexOf(size) == -12623 && !/^[0-9]+(\.[0-9]+)?(cm|mm|in|pt|pc|px)$/.test(size)) {2624 2625 return getLegacyFontSize.resultCache[size] = null;2626 }2627 var font = document.createElement("font");2628 document.body.appendChild(font);2629 if (size == "xxx-large") {2630 font.size = 7;2631 } else {2632 font.style.fontSize = size;2633 }2634 var pixelSize = parseInt(getComputedStyle(font).fontSize);2635 document.body.removeChild(font);2636 2637 var returnedSize = 1;2638 2639 while (returnedSize < 7) {2640 2641 2642 var font = document.createElement("font");2643 font.size = returnedSize;2644 document.body.appendChild(font);2645 var lowerBound = parseInt(getComputedStyle(font).fontSize);2646 2647 2648 2649 font.size = 1 + returnedSize;2650 var upperBound = parseInt(getComputedStyle(font).fontSize);2651 document.body.removeChild(font);2652 2653 var average = (upperBound + lowerBound)/2;2654 2655 2656 if (pixelSize < average) {2657 return getLegacyFontSize.resultCache[size] = String(returnedSize);2658 }2659 2660 returnedSize++;2661 }2662 2663 return getLegacyFontSize.resultCache[size] = "7";2664}2665commands.forecolor = {2666 action: function(value) {2667 2668 2669 2670 2671 2672 2673 2674 if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) {2675 value = "#" + value;2676 }2677 if (!/^(rgba?|hsla?)\(.*\)$/.test(value)2678 && !parseSimpleColor(value)2679 && value.toLowerCase() != "transparent") {2680 return false;2681 }2682 2683 setSelectionValue("forecolor", value);2684 2685 return true;2686 }, standardInlineValueCommand: true, relevantCssProperty: "color",2687 equivalentValues: function(val1, val2) {2688 2689 2690 2691 return normalizeColor(val1) === normalizeColor(val2);2692 },2693};2694commands.hilitecolor = {2695 2696 action: function(value) {2697 2698 2699 2700 2701 2702 2703 2704 if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) {2705 value = "#" + value;2706 }2707 if (!/^(rgba?|hsla?)\(.*\)$/.test(value)2708 && !parseSimpleColor(value)2709 && value.toLowerCase() != "transparent") {2710 return false;2711 }2712 2713 setSelectionValue("hilitecolor", value);2714 2715 return true;2716 }, indeterm: function() {2717 2718 2719 2720 return getAllEffectivelyContainedNodes(getActiveRange(), function(node) {2721 return isEditable(node) && node.nodeType == Node.TEXT_NODE;2722 }).map(function(node) {2723 return getEffectiveCommandValue(node, "hilitecolor");2724 }).filter(function(value, i, arr) {2725 return arr.slice(0, i).indexOf(value) == -1;2726 }).length >= 2;2727 }, standardInlineValueCommand: true, relevantCssProperty: "backgroundColor",2728 equivalentValues: function(val1, val2) {2729 2730 2731 2732 return normalizeColor(val1) === normalizeColor(val2);2733 },2734};2735commands.italic = {2736 action: function() {2737 2738 2739 2740 if (myQueryCommandState("italic")) {2741 setSelectionValue("italic", "normal");2742 } else {2743 setSelectionValue("italic", "italic");2744 }2745 return true;2746 }, inlineCommandActivatedValues: ["italic", "oblique"],2747 relevantCssProperty: "fontStyle"2748};2749commands.removeformat = {2750 action: function() {2751 2752 2753 2754 2755 2756 function isRemoveFormatCandidate(node) {2757 return isEditable(node)2758 && isHtmlElement(node, ["abbr", "acronym", "b", "bdi", "bdo",2759 "big", "blink", "cite", "code", "dfn", "em", "font", "i",2760 "ins", "kbd", "mark", "nobr", "q", "s", "samp", "small",2761 "span", "strike", "strong", "sub", "sup", "tt", "u", "var"]);2762 }2763 2764 2765 var elementsToRemove = getAllEffectivelyContainedNodes(getActiveRange(), isRemoveFormatCandidate);2766 2767 elementsToRemove.forEach(function(element) {2768 2769 2770 2771 while (element.hasChildNodes()) {2772 movePreservingRanges(element.firstChild, element.parentNode, getNodeIndex(element));2773 }2774 2775 element.parentNode.removeChild(element);2776 });2777 2778 2779 2780 2781 2782 if (isEditable(getActiveRange().startContainer)2783 && getActiveRange().startContainer.nodeType == Node.TEXT_NODE2784 && getActiveRange().startOffset != 02785 && getActiveRange().startOffset != getNodeLength(getActiveRange().startContainer)) {2786 2787 if (getActiveRange().startContainer == getActiveRange().endContainer) {2788 var newEnd = getActiveRange().endOffset - getActiveRange().startOffset;2789 var newNode = getActiveRange().startContainer.splitText(getActiveRange().startOffset);2790 getActiveRange().setStart(newNode, 0);2791 getActiveRange().setEnd(newNode, newEnd);2792 } else {2793 getActiveRange().setStart(getActiveRange().startContainer.splitText(getActiveRange().startOffset), 0);2794 }2795 }2796 2797 2798 2799 2800 if (isEditable(getActiveRange().endContainer)2801 && getActiveRange().endContainer.nodeType == Node.TEXT_NODE2802 && getActiveRange().endOffset != 02803 && getActiveRange().endOffset != getNodeLength(getActiveRange().endContainer)) {2804 2805 2806 2807 2808 2809 var newStart = [getActiveRange().startContainer, getActiveRange().startOffset];2810 var newEnd = [getActiveRange().endContainer, getActiveRange().endOffset];2811 getActiveRange().setEnd(document.documentElement, 0);2812 newEnd[0].splitText(newEnd[1]);2813 getActiveRange().setStart(newStart[0], newStart[1]);2814 getActiveRange().setEnd(newEnd[0], newEnd[1]);2815 }2816 2817 2818 2819 2820 2821 2822 getAllEffectivelyContainedNodes(getActiveRange(), isEditable).forEach(function(node) {2823 while (isRemoveFormatCandidate(node.parentNode)2824 && inSameEditingHost(node.parentNode, node)) {2825 splitParent([node]);2826 }2827 });2828 2829 2830 [2831 "subscript",2832 "bold",2833 "fontname",2834 "fontsize",2835 "forecolor",2836 "hilitecolor",2837 "italic",2838 "strikethrough",2839 "underline",2840 ].forEach(function(command) {2841 setSelectionValue(command, null);2842 });2843 2844 return true;2845 }2846};2847commands.strikethrough = {2848 action: function() {2849 2850 2851 2852 if (myQueryCommandState("strikethrough")) {2853 setSelectionValue("strikethrough", null);2854 } else {2855 setSelectionValue("strikethrough", "line-through");2856 }2857 return true;2858 }, inlineCommandActivatedValues: ["line-through"]2859};2860commands.subscript = {2861 action: function() {2862 2863 var state = myQueryCommandState("subscript");2864 2865 setSelectionValue("subscript", null);2866 2867 if (!state) {2868 setSelectionValue("subscript", "subscript");2869 }2870 2871 return true;2872 }, indeterm: function() {2873 2874 2875 2876 2877 2878 2879 var nodes = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode);2880 return (nodes.some(function(node) { return getEffectiveCommandValue(node, "subscript") == "subscript" })2881 && nodes.some(function(node) { return getEffectiveCommandValue(node, "subscript") != "subscript" }))2882 || nodes.some(function(node) { return getEffectiveCommandValue(node, "subscript") == "mixed" });2883 }, inlineCommandActivatedValues: ["subscript"],2884};2885commands.superscript = {2886 action: function() {2887 2888 2889 var state = myQueryCommandState("superscript");2890 2891 setSelectionValue("superscript", null);2892 2893 if (!state) {2894 setSelectionValue("superscript", "superscript");2895 }2896 2897 return true;2898 }, indeterm: function() {2899 2900 2901 2902 2903 2904 2905 var nodes = getAllEffectivelyContainedNodes(getActiveRange(), isFormattableNode);2906 return (nodes.some(function(node) { return getEffectiveCommandValue(node, "superscript") == "superscript" })2907 && nodes.some(function(node) { return getEffectiveCommandValue(node, "superscript") != "superscript" }))2908 || nodes.some(function(node) { return getEffectiveCommandValue(node, "superscript") == "mixed" });2909 }, inlineCommandActivatedValues: ["superscript"],2910};2911commands.underline = {2912 action: function() {2913 2914 2915 2916 if (myQueryCommandState("underline")) {2917 setSelectionValue("underline", null);2918 } else {2919 setSelectionValue("underline", "underline");2920 }2921 return true;2922 }, inlineCommandActivatedValues: ["underline"]2923};2924commands.unlink = {2925 action: function() {2926 2927 2928 2929 2930 2931 2932 var range = getActiveRange();2933 var hyperlinks = [];2934 for (2935 var node = range.startContainer;2936 node;2937 node = node.parentNode2938 ) {2939 if (isHtmlElement(node, "A")2940 && node.hasAttribute("href")) {2941 hyperlinks.unshift(node);2942 }2943 }2944 for (2945 var node = range.startContainer;2946 node != nextNodeDescendants(range.endContainer);2947 node = nextNode(node)2948 ) {2949 if (isHtmlElement(node, "A")2950 && node.hasAttribute("href")2951 && (isContained(node, range)2952 || isAncestor(node, range.endContainer)2953 || node == range.endContainer)) {2954 hyperlinks.push(node);2955 }2956 }2957 2958 for (var i = 0; i < hyperlinks.length; i++) {2959 clearValue(hyperlinks[i], "unlink");2960 }2961 2962 return true;2963 }2964};2965function isIndentationElement(node) {2966 if (!isHtmlElement(node)) {2967 return false;2968 }2969 if (node.tagName == "BLOCKQUOTE") {2970 return true;2971 }2972 if (node.tagName != "DIV") {2973 return false;2974 }2975 for (var i = 0; i < node.style.length; i++) {2976 2977 if (/^(-[a-z]+-)?margin/.test(node.style[i])) {2978 return true;2979 }2980 }2981 return false;2982}2983function isSimpleIndentationElement(node) {2984 if (!isIndentationElement(node)) {2985 return false;2986 }2987 for (var i = 0; i < node.attributes.length; i++) {2988 if (!isHtmlNamespace(node.attributes[i].namespaceURI)2989 || ["style", "dir"].indexOf(node.attributes[i].name) == -1) {2990 return false;2991 }2992 }2993 for (var i = 0; i < node.style.length; i++) {2994 2995 if (!/^(-[a-z]+-)?(margin|border|padding)/.test(node.style[i])) {2996 return false;2997 }2998 }2999 return true;3000}3001function isNonListSingleLineContainer(node) {3002 return isHtmlElement(node, ["address", "div", "h1", "h2", "h3", "h4", "h5",3003 "h6", "listing", "p", "pre", "xmp"]);3004}3005function isSingleLineContainer(node) {3006 return isNonListSingleLineContainer(node)3007 || isHtmlElement(node, ["li", "dt", "dd"]);3008}3009function getBlockNodeOf(node) {3010 3011 while (isInlineNode(node)) {3012 node = node.parentNode;3013 }3014 3015 return node;3016}3017function fixDisallowedAncestors(node) {3018 3019 if (!isEditable(node)) {3020 return;3021 }3022 3023 3024 if (getAncestors(node).every(function(ancestor) {3025 return !inSameEditingHost(node, ancestor)3026 || !isAllowedChild(node, ancestor)3027 })) {3028 3029 3030 3031 3032 3033 if (isHtmlElement(node, ["dd", "dt"])) {3034 wrap([node],3035 function(sibling) { return isHtmlElement(sibling, "dl") && !sibling.attributes.length },3036 function() { return document.createElement("dl") });3037 return;3038 }3039 3040 3041 if (!isAllowedChild("p", getEditingHostOf(node))) {3042 return;3043 }3044 3045 if (!isProhibitedParagraphChild(node)) {3046 return;3047 }3048 3049 3050 node = setTagName(node, defaultSingleLineContainerName);3051 3052 fixDisallowedAncestors(node);3053 3054 var children = [].slice.call(node.childNodes);3055 3056 3057 children.filter(isProhibitedParagraphChild)3058 .forEach(function(child) {3059 3060 3061 var values = recordValues([child]);3062 3063 splitParent([child]);3064 3065 restoreValues(values);3066 });3067 3068 return;3069 }3070 3071 3072 var values = recordValues([node]);3073 3074 3075 while (!isAllowedChild(node, node.parentNode)) {3076 splitParent([node]);3077 }3078 3079 restoreValues(values);3080}3081function normalizeSublists(item) {3082 3083 3084 if (!isHtmlElement(item, "LI")3085 || !isEditable(item)3086 || !isEditable(item.parentNode)) {3087 return;3088 }3089 3090 var newItem = null;3091 3092 while ([].some.call(item.childNodes, function (node) { return isHtmlElement(node, ["OL", "UL"]) })) {3093 3094 var child = item.lastChild;3095 3096 3097 if (isHtmlElement(child, ["OL", "UL"])3098 || (!newItem && child.nodeType == Node.TEXT_NODE && /^[ \t\n\f\r]*$/.test(child.data))) {3099 3100 newItem = null;3101 3102 3103 movePreservingRanges(child, item.parentNode, 1 + getNodeIndex(item));3104 3105 } else {3106 3107 3108 3109 if (!newItem) {3110 newItem = item.ownerDocument.createElement("li");3111 item.parentNode.insertBefore(newItem, item.nextSibling);3112 }3113 3114 3115 movePreservingRanges(child, newItem, 0);3116 }3117 }3118}3119function getSelectionListState() {3120 3121 if (!getActiveRange()) {3122 return "none";3123 }3124 3125 var newRange = blockExtend(getActiveRange());3126 3127 3128 3129 3130 3131 3132 var nodeList = getContainedNodes(newRange, function(node) {3133 return isEditable(node)3134 && !isIndentationElement(node)3135 && (isHtmlElement(node, ["ol", "ul"])3136 || isHtmlElement(node.parentNode, ["ol", "ul"])3137 || isAllowedChild(node, "li"));3138 });3139 3140 if (!nodeList.length) {3141 return "none";3142 }3143 3144 3145 3146 if (nodeList.every(function(node) {3147 return isHtmlElement(node, "ol")3148 || isHtmlElement(node.parentNode, "ol")3149 || (isHtmlElement(node.parentNode, "li") && isHtmlElement(node.parentNode.parentNode, "ol"));3150 })3151 && !nodeList.some(function(node) { return isHtmlElement(node, "ul") || ("querySelector" in node && node.querySelector("ul")) })) {3152 return "ol";3153 }3154 3155 3156 3157 if (nodeList.every(function(node) {3158 return isHtmlElement(node, "ul")3159 || isHtmlElement(node.parentNode, "ul")3160 || (isHtmlElement(node.parentNode, "li") && isHtmlElement(node.parentNode.parentNode, "ul"));3161 })3162 && !nodeList.some(function(node) { return isHtmlElement(node, "ol") || ("querySelector" in node && node.querySelector("ol")) })) {3163 return "ul";3164 }3165 var hasOl = nodeList.some(function(node) {3166 return isHtmlElement(node, "ol")3167 || isHtmlElement(node.parentNode, "ol")3168 || ("querySelector" in node && node.querySelector("ol"))3169 || (isHtmlElement(node.parentNode, "li") && isHtmlElement(node.parentNode.parentNode, "ol"));3170 });3171 var hasUl = nodeList.some(function(node) {3172 return isHtmlElement(node, "ul")3173 || isHtmlElement(node.parentNode, "ul")3174 || ("querySelector" in node && node.querySelector("ul"))3175 || (isHtmlElement(node.parentNode, "li") && isHtmlElement(node.parentNode.parentNode, "ul"));3176 });3177 3178 3179 3180 3181 if (hasOl && hasUl) {3182 return "mixed";3183 }3184 3185 3186 if (hasOl) {3187 return "mixed ol";3188 }3189 3190 3191 if (hasUl) {3192 return "mixed ul";3193 }3194 3195 return "none";3196}3197function getAlignmentValue(node) {3198 3199 3200 3201 while ((node && node.nodeType != Node.ELEMENT_NODE)3202 || (node.nodeType == Node.ELEMENT_NODE3203 && ["inline", "none"].indexOf(getComputedStyle(node).display) != -1)) {3204 node = node.parentNode;3205 }3206 3207 if (!node || node.nodeType != Node.ELEMENT_NODE) {3208 return "left";3209 }3210 var resolvedValue = getComputedStyle(node).textAlign3211 3212 .replace(/^-(moz|webkit)-/, "")3213 .replace(/^auto$/, "start");3214 3215 3216 if (resolvedValue == "start") {3217 return getDirectionality(node) == "ltr" ? "left" : "right";3218 }3219 3220 3221 if (resolvedValue == "end") {3222 return getDirectionality(node) == "ltr" ? "right" : "left";3223 }3224 3225 3226 if (["center", "justify", "left", "right"].indexOf(resolvedValue) != -1) {3227 return resolvedValue;3228 }3229 3230 return "left";3231}3232function getNextEquivalentPoint(node, offset) {3233 3234 if (getNodeLength(node) == 0) {3235 return null;3236 }3237 3238 3239 if (offset == getNodeLength(node)3240 && node.parentNode3241 && isInlineNode(node)) {3242 return [node.parentNode, 1 + getNodeIndex(node)];3243 }3244 3245 3246 if (0 <= offset3247 && offset < node.childNodes.length3248 && getNodeLength(node.childNodes[offset]) != 03249 && isInlineNode(node.childNodes[offset])) {3250 return [node.childNodes[offset], 0];3251 }3252 3253 return null;3254}3255function getPreviousEquivalentPoint(node, offset) {3256 3257 if (getNodeLength(node) == 0) {3258 return null;3259 }3260 3261 3262 if (offset == 03263 && node.parentNode3264 && isInlineNode(node)) {3265 return [node.parentNode, getNodeIndex(node)];3266 }3267 3268 3269 3270 if (0 <= offset - 13271 && offset - 1 < node.childNodes.length3272 && getNodeLength(node.childNodes[offset - 1]) != 03273 && isInlineNode(node.childNodes[offset - 1])) {3274 return [node.childNodes[offset - 1], getNodeLength(node.childNodes[offset - 1])];3275 }3276 3277 return null;3278}3279function getFirstEquivalentPoint(node, offset) {3280 3281 3282 var prev;3283 while (prev = getPreviousEquivalentPoint(node, offset)) {3284 node = prev[0];3285 offset = prev[1];3286 }3287 3288 return [node, offset];3289}3290function getLastEquivalentPoint(node, offset) {3291 3292 3293 var next;3294 while (next = getNextEquivalentPoint(node, offset)) {3295 node = next[0];3296 offset = next[1];3297 }3298 3299 return [node, offset];3300}3301function isBlockStartPoint(node, offset) {3302 return (!node.parentNode && offset == 0)3303 || (0 <= offset - 13304 && offset - 1 < node.childNodes.length3305 && isVisible(node.childNodes[offset - 1])3306 && (isBlockNode(node.childNodes[offset - 1])3307 || isHtmlElement(node.childNodes[offset - 1], "br")));3308}3309function isBlockEndPoint(node, offset) {3310 return (!node.parentNode && offset == getNodeLength(node))3311 || (offset < node.childNodes.length3312 && isVisible(node.childNodes[offset])3313 && isBlockNode(node.childNodes[offset]));3314}3315function isBlockBoundaryPoint(node, offset) {3316 return isBlockStartPoint(node, offset)3317 || isBlockEndPoint(node, offset);3318}3319function blockExtend(range) {3320 3321 3322 var startNode = range.startContainer;3323 var startOffset = range.startOffset;3324 var endNode = range.endContainer;3325 var endOffset = range.endOffset;3326 3327 3328 3329 var liAncestors = getAncestors(startNode).concat(startNode)3330 .filter(function(ancestor) { return isHtmlElement(ancestor, "li") })3331 .slice(-1);3332 if (liAncestors.length) {3333 startOffset = getNodeIndex(liAncestors[0]);3334 startNode = liAncestors[0].parentNode;3335 }3336 3337 3338 if (!isBlockStartPoint(startNode, startOffset)) do {3339 3340 3341 if (startOffset == 0) {3342 startOffset = getNodeIndex(startNode);3343 startNode = startNode.parentNode;3344 3345 } else {3346 startOffset--;3347 }3348 3349 3350 } while (!isBlockBoundaryPoint(startNode, startOffset));3351 3352 3353 while (startOffset == 03354 && startNode.parentNode) {3355 startOffset = getNodeIndex(startNode);3356 startNode = startNode.parentNode;3357 }3358 3359 3360 3361 var liAncestors = getAncestors(endNode).concat(endNode)3362 .filter(function(ancestor) { return isHtmlElement(ancestor, "li") })3363 .slice(-1);3364 if (liAncestors.length) {3365 endOffset = 1 + getNodeIndex(liAncestors[0]);3366 endNode = liAncestors[0].parentNode;3367 }3368 3369 3370 if (!isBlockEndPoint(endNode, endOffset)) do {3371 3372 3373 if (endOffset == getNodeLength(endNode)) {3374 endOffset = 1 + getNodeIndex(endNode);3375 endNode = endNode.parentNode;3376 3377 } else {3378 endOffset++;3379 }3380 3381 3382 } while (!isBlockBoundaryPoint(endNode, endOffset));3383 3384 3385 3386 while (endOffset == getNodeLength(endNode)3387 && endNode.parentNode) {3388 endOffset = 1 + getNodeIndex(endNode);3389 endNode = endNode.parentNode;3390 }3391 3392 3393 var newRange = startNode.ownerDocument.createRange();3394 newRange.setStart(startNode, startOffset);3395 newRange.setEnd(endNode, endOffset);3396 3397 return newRange;3398}3399function followsLineBreak(node) {3400 3401 var offset = 0;3402 3403 while (!isBlockBoundaryPoint(node, offset)) {3404 3405 3406 if (0 <= offset - 13407 && offset - 1 < node.childNodes.length3408 && isVisible(node.childNodes[offset - 1])) {3409 return false;3410 }3411 3412 3413 if (offset == 03414 || !node.hasChildNodes()) {3415 offset = getNodeIndex(node);3416 node = node.parentNode;3417 3418 3419 } else {3420 node = node.childNodes[offset - 1];3421 offset = getNodeLength(node);3422 }3423 }3424 3425 return true;3426}3427function precedesLineBreak(node) {3428 3429 var offset = getNodeLength(node);3430 3431 while (!isBlockBoundaryPoint(node, offset)) {3432 3433 if (offset < node.childNodes.length3434 && isVisible(node.childNodes[offset])) {3435 return false;3436 }3437 3438 3439 if (offset == getNodeLength(node)3440 || !node.hasChildNodes()) {3441 offset = 1 + getNodeIndex(node);3442 node = node.parentNode;3443 3444 3445 } else {3446 node = node.childNodes[offset];3447 offset = 0;3448 }3449 }3450 3451 return true;3452}3453function recordCurrentOverrides() {3454 3455 3456 var overrides = [];3457 3458 3459 if (getValueOverride("createlink") !== undefined) {3460 overrides.push(["createlink", getValueOverride("createlink")]);3461 }3462 3463 3464 3465 3466 ["bold", "italic", "strikethrough", "subscript", "superscript",3467 "underline"].forEach(function(command) {3468 if (getStateOverride(command) !== undefined) {3469 overrides.push([command, getStateOverride(command)]);3470 }3471 });3472 3473 3474 3475 ["fontname", "fontsize", "forecolor",3476 "hilitecolor"].forEach(function(command) {3477 if (getValueOverride(command) !== undefined) {3478 overrides.push([command, getValueOverride(command)]);3479 }3480 });3481 3482 return overrides;3483}3484function recordCurrentStatesAndValues() {3485 3486 3487 var overrides = [];3488 3489 3490 var node = getAllEffectivelyContainedNodes(getActiveRange())3491 .filter(isFormattableNode)[0];3492 3493 if (!node) {3494 return overrides;3495 }3496 3497 3498 overrides.push(["createlink", getEffectiveCommandValue(node, "createlink")]);3499 3500 3501 3502 3503 3504 ["bold", "italic", "strikethrough", "subscript", "superscript",3505 "underline"].forEach(function(command) {3506 if (commands[command].inlineCommandActivatedValues3507 .indexOf(getEffectiveCommandValue(node, command)) != -1) {3508 overrides.push([command, true]);3509 } else {3510 overrides.push([command, false]);3511 }3512 });3513 3514 3515 ["fontname", "fontsize", "forecolor", "hilitecolor"].forEach(function(command) {3516 overrides.push([command, commands[command].value()]);3517 });3518 3519 3520 overrides.push(["fontsize", getEffectiveCommandValue(node, "fontsize")]);3521 3522 return overrides;3523}3524function restoreStatesAndValues(overrides) {3525 3526 3527 var node = getAllEffectivelyContainedNodes(getActiveRange())3528 .filter(isFormattableNode)[0];3529 3530 3531 if (node) {3532 for (var i = 0; i < overrides.length; i++) {3533 var command = overrides[i][0];3534 var override = overrides[i][1];3535 3536 3537 3538 if (typeof override == "boolean"3539 && myQueryCommandState(command) != override) {3540 commands[command].action("");3541 3542 3543 3544 3545 } else if (typeof override == "string"3546 && command != "createlink"3547 && command != "fontsize"3548 && !areEquivalentValues(command, myQueryCommandValue(command), override)) {3549 commands[command].action(override);3550 3551 3552 3553 3554 3555 3556 } else if (typeof override == "string"3557 && command == "createlink"3558 && (3559 (3560 getValueOverride("createlink") !== undefined3561 && getValueOverride("createlink") !== override3562 ) || (3563 getValueOverride("createlink") === undefined3564 && getEffectiveCommandValue(node, "createlink") !== override3565 )3566 )) {3567 commands.createlink.action(override);3568 3569 3570 3571 3572 3573 } else if (typeof override == "string"3574 && command == "fontsize"3575 && (3576 (3577 getValueOverride("fontsize") !== undefined3578 && getValueOverride("fontsize") !== override3579 ) || (3580 getValueOverride("fontsize") === undefined3581 && !areLooselyEquivalentValues(command, getEffectiveCommandValue(node, "fontsize"), override)3582 )3583 )) {3584 3585 3586 override = getLegacyFontSize(override);3587 3588 3589 commands.fontsize.action(override);3590 3591 } else {3592 continue;3593 }3594 3595 3596 node = getAllEffectivelyContainedNodes(getActiveRange())3597 .filter(isFormattableNode)[0]3598 || node;3599 }3600 3601 } else {3602 for (var i = 0; i < overrides.length; i++) {3603 var command = overrides[i][0];3604 var override = overrides[i][1];3605 3606 3607 if (typeof override == "boolean") {3608 setStateOverride(command, override);3609 }3610 3611 3612 if (typeof override == "string") {3613 setValueOverride(command, override);3614 }3615 }3616 }3617}3618function deleteSelection(flags) {3619 if (flags === undefined) {3620 flags = {};3621 }3622 var blockMerging = "blockMerging" in flags ? Boolean(flags.blockMerging) : true;3623 var stripWrappers = "stripWrappers" in flags ? Boolean(flags.stripWrappers) : true;3624 var direction = "direction" in flags ? flags.direction : "forward";3625 3626 if (!getActiveRange()) {3627 return;3628 }3629 3630 canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset);3631 3632 canonicalizeWhitespace(getActiveRange().endContainer, getActiveRange().endOffset);3633 3634 3635 var start = getLastEquivalentPoint(getActiveRange().startContainer, getActiveRange().startOffset);3636 var startNode = start[0];3637 var startOffset = start[1];3638 3639 3640 var end = getFirstEquivalentPoint(getActiveRange().endContainer, getActiveRange().endOffset);3641 var endNode = end[0];3642 var endOffset = end[1];3643 3644 if (getPosition(endNode, endOffset, startNode, startOffset) !== "after") {3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 if (direction == "forward") {3656 if (getSelection().rangeCount) {3657 getSelection().collapseToStart();3658 }3659 getActiveRange().collapse(true);3660 3661 } else {3662 getSelection().collapseToEnd();3663 getActiveRange().collapse(false);3664 }3665 3666 return;3667 }3668 3669 3670 if (startNode.nodeType == Node.TEXT_NODE3671 && startOffset == 0) {3672 startOffset = getNodeIndex(startNode);3673 startNode = startNode.parentNode;3674 }3675 3676 3677 if (endNode.nodeType == Node.TEXT_NODE3678 && endOffset == getNodeLength(endNode)) {3679 endOffset = 1 + getNodeIndex(endNode);3680 endNode = endNode.parentNode;3681 }3682 3683 3684 getSelection().collapse(startNode, startOffset);3685 getActiveRange().setStart(startNode, startOffset);3686 3687 getSelection().extend(endNode, endOffset);3688 getActiveRange().setEnd(endNode, endOffset);3689 3690 var startBlock = getActiveRange().startContainer;3691 3692 3693 while (inSameEditingHost(startBlock, startBlock.parentNode)3694 && isInlineNode(startBlock)) {3695 startBlock = startBlock.parentNode;3696 }3697 3698 3699 3700 if ((!isBlockNode(startBlock) && !isEditingHost(startBlock))3701 || !isAllowedChild("span", startBlock)3702 || isHtmlElement(startBlock, ["td", "th"])) {3703 startBlock = null;3704 }3705 3706 var endBlock = getActiveRange().endContainer;3707 3708 3709 while (inSameEditingHost(endBlock, endBlock.parentNode)3710 && isInlineNode(endBlock)) {3711 endBlock = endBlock.parentNode;3712 }3713 3714 3715 3716 if ((!isBlockNode(endBlock) && !isEditingHost(endBlock))3717 || !isAllowedChild("span", endBlock)3718 || isHtmlElement(endBlock, ["td", "th"])) {3719 endBlock = null;3720 }3721 3722 var overrides = recordCurrentStatesAndValues();3723 3724 3725 if (startNode == endNode3726 && isEditable(startNode)3727 && startNode.nodeType == Node.TEXT_NODE) {3728 3729 3730 startNode.deleteData(startOffset, endOffset - startOffset);3731 3732 3733 canonicalizeWhitespace(startNode, startOffset, false);3734 3735 3736 if (direction == "forward") {3737 if (getSelection().rangeCount) {3738 getSelection().collapseToStart();3739 }3740 getActiveRange().collapse(true);3741 3742 } else {3743 getSelection().collapseToEnd();3744 getActiveRange().collapse(false);3745 }3746 3747 restoreStatesAndValues(overrides);3748 3749 return;3750 }3751 3752 3753 3754 if (isEditable(startNode)3755 && startNode.nodeType == Node.TEXT_NODE) {3756 startNode.deleteData(startOffset, getNodeLength(startNode) - startOffset);3757 }3758 3759 3760 3761 3762 3763 var nodeList = getContainedNodes(getActiveRange(),3764 function(node) {3765 return isEditable(node)3766 && !isHtmlElement(node, ["thead", "tbody", "tfoot", "tr", "th", "td"]);3767 }3768 );3769 3770 for (var i = 0; i < nodeList.length; i++) {3771 var node = nodeList[i];3772 3773 var parent_ = node.parentNode;3774 3775 parent_.removeChild(node);3776 3777 3778 3779 if (![].some.call(getBlockNodeOf(parent_).childNodes, isVisible)3780 && (isEditable(parent_) || isEditingHost(parent_))) {3781 parent_.appendChild(document.createElement("br"));3782 }3783 3784 3785 3786 3787 if (stripWrappers3788 || (!isAncestor(parent_, startNode) && parent_ != startNode)) {3789 while (isEditable(parent_)3790 && isInlineNode(parent_)3791 && getNodeLength(parent_) == 0) {3792 var grandparent = parent_.parentNode;3793 grandparent.removeChild(parent_);3794 parent_ = grandparent;3795 }3796 }3797 }3798 3799 3800 if (isEditable(endNode)3801 && endNode.nodeType == Node.TEXT_NODE) {3802 endNode.deleteData(0, endOffset);3803 }3804 3805 3806 canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset, false);3807 3808 3809 canonicalizeWhitespace(getActiveRange().endContainer, getActiveRange().endOffset, false);3810 3811 3812 3813 if (!blockMerging3814 || !startBlock3815 || !endBlock3816 || !inSameEditingHost(startBlock, endBlock)3817 || startBlock == endBlock) {3818 3819 3820 if (direction == "forward") {3821 if (getSelection().rangeCount) {3822 getSelection().collapseToStart();3823 }3824 getActiveRange().collapse(true);3825 3826 } else {3827 if (getSelection().rangeCount) {3828 getSelection().collapseToEnd();3829 }3830 getActiveRange().collapse(false);3831 }3832 3833 restoreStatesAndValues(overrides);3834 3835 return;3836 }3837 3838 3839 if (startBlock.children.length == 13840 && isCollapsedBlockProp(startBlock.firstChild)) {3841 startBlock.removeChild(startBlock.firstChild);3842 }3843 3844 if (isAncestor(startBlock, endBlock)) {3845 3846 var referenceNode = endBlock;3847 3848 3849 while (referenceNode.parentNode != startBlock) {3850 referenceNode = referenceNode.parentNode;3851 }3852 3853 3854 3855 getSelection().collapse(startBlock, getNodeIndex(referenceNode));3856 getActiveRange().setStart(startBlock, getNodeIndex(referenceNode));3857 getActiveRange().collapse(true);3858 3859 if (!endBlock.hasChildNodes()) {3860 3861 3862 3863 3864 while (isEditable(endBlock)3865 && endBlock.parentNode.childNodes.length == 13866 && endBlock.parentNode != startBlock) {3867 var parent_ = endBlock;3868 parent_.removeChild(endBlock);3869 endBlock = parent_;3870 }3871 3872 3873 3874 3875 if (isEditable(endBlock)3876 && !isInlineNode(endBlock)3877 && isInlineNode(endBlock.previousSibling)3878 && isInlineNode(endBlock.nextSibling)) {3879 endBlock.parentNode.insertBefore(document.createElement("br"), endBlock.nextSibling);3880 }3881 3882 if (isEditable(endBlock)) {3883 endBlock.parentNode.removeChild(endBlock);3884 }3885 3886 restoreStatesAndValues(overrides);3887 3888 return;3889 }3890 3891 3892 if (!isInlineNode(endBlock.firstChild)) {3893 restoreStatesAndValues(overrides);3894 return;3895 }3896 3897 var children = [];3898 3899 children.push(endBlock.firstChild);3900 3901 3902 3903 while (!isHtmlElement(children[children.length - 1], "br")3904 && isInlineNode(children[children.length - 1].nextSibling)) {3905 children.push(children[children.length - 1].nextSibling);3906 }3907 3908 var values = recordValues(children);3909 3910 3911 while (children[0].parentNode != startBlock) {3912 splitParent(children);3913 }3914 3915 3916 if (isEditable(children[0].previousSibling)3917 && isHtmlElement(children[0].previousSibling, "br")) {3918 children[0].parentNode.removeChild(children[0].previousSibling);3919 }3920 3921 } else if (isDescendant(startBlock, endBlock)) {3922 3923 3924 getSelection().collapse(startBlock, getNodeLength(startBlock));3925 getActiveRange().setStart(startBlock, getNodeLength(startBlock));3926 getActiveRange().collapse(true);3927 3928 var referenceNode = startBlock;3929 3930 3931 while (referenceNode.parentNode != endBlock) {3932 referenceNode = referenceNode.parentNode;3933 }3934 3935 3936 if (isInlineNode(referenceNode.nextSibling)3937 && isHtmlElement(startBlock.lastChild, "br")) {3938 startBlock.removeChild(startBlock.lastChild);3939 }3940 3941 var nodesToMove = [];3942 3943 3944 if (referenceNode.nextSibling3945 && !isBlockNode(referenceNode.nextSibling)) {3946 nodesToMove.push(referenceNode.nextSibling);3947 }3948 3949 3950 3951 if (nodesToMove.length3952 && !isHtmlElement(nodesToMove[nodesToMove.length - 1], "br")3953 && nodesToMove[nodesToMove.length - 1].nextSibling3954 && !isBlockNode(nodesToMove[nodesToMove.length - 1].nextSibling)) {3955 nodesToMove.push(nodesToMove[nodesToMove.length - 1].nextSibling);3956 }3957 3958 var values = recordValues(nodesToMove);3959 3960 3961 nodesToMove.forEach(function(node) {3962 movePreservingRanges(node, startBlock, -1);3963 });3964 3965 } else {3966 3967 3968 getSelection().collapse(startBlock, getNodeLength(startBlock));3969 getActiveRange().setStart(startBlock, getNodeLength(startBlock));3970 getActiveRange().collapse(true);3971 3972 3973 if (isInlineNode(endBlock.firstChild)3974 && isHtmlElement(startBlock.lastChild, "br")) {3975 startBlock.removeChild(startBlock.lastChild);3976 }3977 3978 3979 var values = recordValues([].slice.call(endBlock.childNodes));3980 3981 3982 while (endBlock.hasChildNodes()) {3983 movePreservingRanges(endBlock.firstChild, startBlock, -1);3984 }3985 3986 3987 3988 while (!endBlock.hasChildNodes()) {3989 var parent_ = endBlock.parentNode;3990 parent_.removeChild(endBlock);3991 endBlock = parent_;3992 }3993 }3994 3995 var ancestor = startBlock;3996 3997 3998 3999 4000 while (getInclusiveAncestors(ancestor).some(function(node) {4001 return inSameEditingHost(ancestor, node)4002 && (4003 (isHtmlElement(node, "ol") && isHtmlElement(node.nextSibling, "ol"))4004 || (isHtmlElement(node, "ul") && isHtmlElement(node.nextSibling, "ul"))4005 ) && inSameEditingHost(ancestor, node.nextSibling);4006 })) {4007 4008 4009 4010 while (!(4011 isHtmlElement(ancestor, "ol")4012 && isHtmlElement(ancestor.nextSibling, "ol")4013 && inSameEditingHost(ancestor, ancestor.nextSibling)4014 ) && !(4015 isHtmlElement(ancestor, "ul")4016 && isHtmlElement(ancestor.nextSibling, "ul")4017 && inSameEditingHost(ancestor, ancestor.nextSibling)4018 )) {4019 ancestor = ancestor.parentNode;4020 }4021 4022 4023 4024 while (ancestor.nextSibling.hasChildNodes()) {4025 movePreservingRanges(ancestor.nextSibling.firstChild, ancestor, -1);4026 }4027 4028 ancestor.parentNode.removeChild(ancestor.nextSibling);4029 }4030 4031 restoreValues(values);4032 4033 4034 if (!startBlock.hasChildNodes()) {4035 startBlock.appendChild(document.createElement("br"));4036 }4037 4038 removeExtraneousLineBreaksAtTheEndOf(startBlock);4039 4040 restoreStatesAndValues(overrides);4041}4042function splitParent(nodeList) {4043 4044 var originalParent = nodeList[0].parentNode;4045 4046 4047 if (!isEditable(originalParent)4048 || !originalParent.parentNode) {4049 return;4050 }4051 4052 4053 if (nodeList.indexOf(originalParent.firstChild) != -1) {4054 removeExtraneousLineBreaksBefore(originalParent);4055 }4056 4057 4058 4059 var followsLineBreak_ = nodeList.indexOf(originalParent.firstChild) != -14060 && followsLineBreak(originalParent);4061 4062 4063 4064 var precedesLineBreak_ = nodeList.indexOf(originalParent.lastChild) != -14065 && precedesLineBreak(originalParent);4066 4067 4068 if (nodeList.indexOf(originalParent.firstChild) == -14069 && nodeList.indexOf(originalParent.lastChild) != -1) {4070 4071 4072 4073 for (var i = nodeList.length - 1; i >= 0; i--) {4074 movePreservingRanges(nodeList[i], originalParent.parentNode, 1 + getNodeIndex(originalParent));4075 }4076 4077 4078 4079 4080 if (precedesLineBreak_4081 && !precedesLineBreak(nodeList[nodeList.length - 1])) {4082 nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling);4083 }4084 4085 removeExtraneousLineBreaksAtTheEndOf(originalParent);4086 4087 return;4088 }4089 4090 if (nodeList.indexOf(originalParent.firstChild) == -1) {4091 4092 4093 var clonedParent = originalParent.cloneNode(false);4094 4095 originalParent.removeAttribute("id");4096 4097 4098 originalParent.parentNode.insertBefore(clonedParent, originalParent);4099 4100 4101 4102 while (nodeList[0].previousSibling) {4103 movePreservingRanges(originalParent.firstChild, clonedParent, clonedParent.childNodes.length);4104 }4105 }4106 4107 4108 for (var i = 0; i < nodeList.length; i++) {4109 movePreservingRanges(nodeList[i], originalParent.parentNode, getNodeIndex(originalParent));4110 }4111 4112 4113 4114 if (followsLineBreak_4115 && !followsLineBreak(nodeList[0])) {4116 nodeList[0].parentNode.insertBefore(document.createElement("br"), nodeList[0]);4117 }4118 4119 4120 4121 4122 if (isInlineNode(nodeList[nodeList.length - 1])4123 && !isHtmlElement(nodeList[nodeList.length - 1], "br")4124 && isHtmlElement(originalParent.firstChild, "br")4125 && !isInlineNode(originalParent)) {4126 originalParent.removeChild(originalParent.firstChild);4127 }4128 4129 if (!originalParent.hasChildNodes()) {4130 4131 originalParent.parentNode.removeChild(originalParent);4132 4133 4134 4135 4136 if (precedesLineBreak_4137 && !precedesLineBreak(nodeList[nodeList.length - 1])) {4138 nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling);4139 }4140 4141 } else {4142 removeExtraneousLineBreaksBefore(originalParent);4143 }4144 4145 4146 4147 if (!nodeList[nodeList.length - 1].nextSibling4148 && nodeList[nodeList.length - 1].parentNode) {4149 removeExtraneousLineBreaksAtTheEndOf(nodeList[nodeList.length - 1].parentNode);4150 }4151}4152function removePreservingDescendants(node) {4153 if (node.hasChildNodes()) {4154 splitParent([].slice.call(node.childNodes));4155 } else {4156 node.parentNode.removeChild(node);4157 }4158}4159function canonicalSpaceSequence(n, nonBreakingStart, nonBreakingEnd) {4160 4161 if (n == 0) {4162 return "";4163 }4164 4165 4166 if (n == 1 && !nonBreakingStart && !nonBreakingEnd) {4167 return " ";4168 }4169 4170 if (n == 1) {4171 return "\xa0";4172 }4173 4174 var buffer = "";4175 4176 4177 var repeatedPair;4178 if (nonBreakingStart) {4179 repeatedPair = "\xa0 ";4180 } else {4181 repeatedPair = " \xa0";4182 }4183 4184 4185 while (n > 3) {4186 buffer += repeatedPair;4187 n -= 2;4188 }4189 4190 4191 if (n == 3) {4192 buffer +=4193 !nonBreakingStart && !nonBreakingEnd ? " \xa0 "4194 : nonBreakingStart && !nonBreakingEnd ? "\xa0\xa0 "4195 : !nonBreakingStart && nonBreakingEnd ? " \xa0\xa0"4196 : nonBreakingStart && nonBreakingEnd ? "\xa0 \xa0"4197 : "impossible";4198 4199 4200 } else {4201 buffer +=4202 !nonBreakingStart && !nonBreakingEnd ? "\xa0 "4203 : nonBreakingStart && !nonBreakingEnd ? "\xa0 "4204 : !nonBreakingStart && nonBreakingEnd ? " \xa0"4205 : nonBreakingStart && nonBreakingEnd ? "\xa0\xa0"4206 : "impossible";4207 }4208 4209 return buffer;4210}4211function canonicalizeWhitespace(node, offset, fixCollapsedSpace) {4212 if (fixCollapsedSpace === undefined) {4213 4214 4215 fixCollapsedSpace = true;4216 }4217 4218 if (!isEditable(node) && !isEditingHost(node)) {4219 return;4220 }4221 4222 var startNode = node;4223 var startOffset = offset;4224 4225 while (true) {4226 4227 4228 4229 if (0 <= startOffset - 14230 && inSameEditingHost(startNode, startNode.childNodes[startOffset - 1])) {4231 startNode = startNode.childNodes[startOffset - 1];4232 startOffset = getNodeLength(startNode);4233 4234 4235 4236 4237 } else if (startOffset == 04238 && !followsLineBreak(startNode)4239 && inSameEditingHost(startNode, startNode.parentNode)) {4240 startOffset = getNodeIndex(startNode);4241 startNode = startNode.parentNode;4242 4243 4244 4245 4246 4247 } else if (startNode.nodeType == Node.TEXT_NODE4248 && ["pre", "pre-wrap"].indexOf(getComputedStyle(startNode.parentNode).whiteSpace) == -14249 && startOffset != 04250 && /[ \xa0]/.test(startNode.data[startOffset - 1])) {4251 startOffset--;4252 4253 } else {4254 break;4255 }4256 }4257 4258 var endNode = startNode;4259 var endOffset = startOffset;4260 4261 var length = 0;4262 4263 4264 var collapseSpaces = startOffset == 0 && followsLineBreak(startNode);4265 4266 while (true) {4267 4268 4269 if (endOffset < endNode.childNodes.length4270 && inSameEditingHost(endNode, endNode.childNodes[endOffset])) {4271 endNode = endNode.childNodes[endOffset];4272 endOffset = 0;4273 4274 4275 4276 4277 } else if (endOffset == getNodeLength(endNode)4278 && !precedesLineBreak(endNode)4279 && inSameEditingHost(endNode, endNode.parentNode)) {4280 endOffset = 1 + getNodeIndex(endNode);4281 endNode = endNode.parentNode;4282 4283 4284 4285 4286 } else if (endNode.nodeType == Node.TEXT_NODE4287 && ["pre", "pre-wrap"].indexOf(getComputedStyle(endNode.parentNode).whiteSpace) == -14288 && endOffset != getNodeLength(endNode)4289 && /[ \xa0]/.test(endNode.data[endOffset])) {4290 4291 4292 4293 4294 if (fixCollapsedSpace4295 && collapseSpaces4296 && " " == endNode.data[endOffset]) {4297 endNode.deleteData(endOffset, 1);4298 continue;4299 }4300 4301 4302 collapseSpaces = " " == endNode.data[endOffset];4303 4304 endOffset++;4305 4306 length++;4307 4308 } else {4309 break;4310 }4311 }4312 4313 4314 if (fixCollapsedSpace) {4315 while (getPosition(startNode, startOffset, endNode, endOffset) == "before") {4316 4317 4318 4319 if (0 <= endOffset - 14320 && endOffset - 1 < endNode.childNodes.length4321 && inSameEditingHost(endNode, endNode.childNodes[endOffset - 1])) {4322 endNode = endNode.childNodes[endOffset - 1];4323 endOffset = getNodeLength(endNode);4324 4325 4326 4327 } else if (endOffset == 04328 && inSameEditingHost(endNode, endNode.parentNode)) {4329 endOffset = getNodeIndex(endNode);4330 endNode = endNode.parentNode;4331 4332 4333 4334 4335 } else if (endNode.nodeType == Node.TEXT_NODE4336 && ["pre", "pre-wrap"].indexOf(getComputedStyle(endNode.parentNode).whiteSpace) == -14337 && endOffset == getNodeLength(endNode)4338 && endNode.data[endNode.data.length - 1] == " "4339 && precedesLineBreak(endNode)) {4340 4341 endOffset--;4342 4343 length--;4344 4345 endNode.deleteData(endOffset, 1);4346 4347 } else {4348 break;4349 }4350 }4351 }4352 4353 4354 4355 4356 4357 var replacementWhitespace = canonicalSpaceSequence(length,4358 startOffset == 0 && followsLineBreak(startNode),4359 endOffset == getNodeLength(endNode) && precedesLineBreak(endNode));4360 4361 while (getPosition(startNode, startOffset, endNode, endOffset) == "before") {4362 4363 4364 if (startOffset < startNode.childNodes.length) {4365 startNode = startNode.childNodes[startOffset];4366 startOffset = 0;4367 4368 4369 4370 } else if (startNode.nodeType != Node.TEXT_NODE4371 || startOffset == getNodeLength(startNode)) {4372 startOffset = 1 + getNodeIndex(startNode);4373 startNode = startNode.parentNode;4374 4375 } else {4376 4377 4378 var element = replacementWhitespace[0];4379 replacementWhitespace = replacementWhitespace.slice(1);4380 4381 4382 if (element != startNode.data[startOffset]) {4383 4384 startNode.insertData(startOffset, element);4385 4386 startNode.deleteData(startOffset + 1, 1);4387 }4388 4389 startOffset++;4390 }4391 }4392}4393function indentNodes(nodeList) {4394 4395 if (!nodeList.length) {4396 return;4397 }4398 4399 var firstNode = nodeList[0];4400 4401 if (isHtmlElement(firstNode.parentNode, ["OL", "UL"])) {4402 4403 var tag = firstNode.parentNode.tagName;4404 4405 4406 4407 4408 wrap(nodeList,4409 function(node) { return isHtmlElement(node, tag) },4410 function() { return firstNode.ownerDocument.createElement(tag) });4411 4412 return;4413 }4414 4415 4416 4417 4418 var newParent = wrap(nodeList,4419 function(node) { return isSimpleIndentationElement(node) },4420 function() { return firstNode.ownerDocument.createElement("blockquote") });4421 4422 fixDisallowedAncestors(newParent);4423}4424function outdentNode(node) {4425 4426 if (!isEditable(node)) {4427 return;4428 }4429 4430 4431 if (isSimpleIndentationElement(node)) {4432 removePreservingDescendants(node);4433 return;4434 }4435 4436 if (isIndentationElement(node)) {4437 4438 node.removeAttribute("dir");4439 4440 node.style.margin = "";4441 node.style.padding = "";4442 node.style.border = "";4443 if (node.getAttribute("style") == ""4444 4445 || node.getAttribute("style") == "border-width: initial; border-color: initial; ") {4446 node.removeAttribute("style");4447 }4448 4449 setTagName(node, "div");4450 4451 return;4452 }4453 4454 var currentAncestor = node.parentNode;4455 4456 var ancestorList = [];4457 4458 4459 4460 while (isEditable(currentAncestor)4461 && currentAncestor.nodeType == Node.ELEMENT_NODE4462 && !isSimpleIndentationElement(currentAncestor)4463 && !isHtmlElement(currentAncestor, ["ol", "ul"])) {4464 ancestorList.push(currentAncestor);4465 currentAncestor = currentAncestor.parentNode;4466 }4467 4468 if (!isEditable(currentAncestor)4469 || !isSimpleIndentationElement(currentAncestor)) {4470 4471 currentAncestor = node.parentNode;4472 4473 ancestorList = [];4474 4475 4476 4477 while (isEditable(currentAncestor)4478 && currentAncestor.nodeType == Node.ELEMENT_NODE4479 && !isIndentationElement(currentAncestor)4480 && !isHtmlElement(currentAncestor, ["ol", "ul"])) {4481 ancestorList.push(currentAncestor);4482 currentAncestor = currentAncestor.parentNode;4483 }4484 }4485 4486 4487 if (isHtmlElement(node, ["OL", "UL"])4488 && (!isEditable(currentAncestor)4489 || !isIndentationElement(currentAncestor))) {4490 4491 4492 node.removeAttribute("reversed");4493 node.removeAttribute("start");4494 node.removeAttribute("type");4495 4496 var children = [].slice.call(node.childNodes);4497 4498 4499 if (node.attributes.length4500 && !isHtmlElement(node.parentNode, ["OL", "UL"])) {4501 setTagName(node, "div");4502 4503 } else {4504 4505 4506 var values = recordValues([].slice.call(node.childNodes));4507 4508 removePreservingDescendants(node);4509 4510 restoreValues(values);4511 }4512 4513 for (var i = 0; i < children.length; i++) {4514 fixDisallowedAncestors(children[i]);4515 }4516 4517 return;4518 }4519 4520 4521 if (!isEditable(currentAncestor)4522 || !isIndentationElement(currentAncestor)) {4523 return;4524 }4525 4526 ancestorList.push(currentAncestor);4527 4528 var originalAncestor = currentAncestor;4529 4530 while (ancestorList.length) {4531 4532 4533 4534 currentAncestor = ancestorList.pop();4535 4536 4537 var target = node.parentNode == currentAncestor4538 ? node4539 : ancestorList[ancestorList.length - 1];4540 4541 4542 if (isInlineNode(target)4543 && !isHtmlElement(target, "BR")4544 && isHtmlElement(target.nextSibling, "BR")) {4545 target.parentNode.removeChild(target.nextSibling);4546 }4547 4548 4549 var precedingSiblings = [].slice.call(currentAncestor.childNodes, 0, getNodeIndex(target));4550 var followingSiblings = [].slice.call(currentAncestor.childNodes, 1 + getNodeIndex(target));4551 4552 indentNodes(precedingSiblings);4553 4554 indentNodes(followingSiblings);4555 }4556 4557 outdentNode(originalAncestor);4558}4559function toggleLists(tagName) {4560 4561 4562 var mode = getSelectionListState() == tagName ? "disable" : "enable";4563 var range = getActiveRange();4564 tagName = tagName.toUpperCase();4565 4566 4567 var otherTagName = tagName == "OL" ? "UL" : "OL";4568 4569 4570 4571 4572 4573 var items = [];4574 (function(){4575 for (4576 var ancestorContainer = range.endContainer;4577 ancestorContainer != range.commonAncestorContainer;4578 ancestorContainer = ancestorContainer.parentNode4579 ) {4580 if (isHtmlElement(ancestorContainer, "li")) {4581 items.unshift(ancestorContainer);4582 }4583 }4584 for (4585 var ancestorContainer = range.startContainer;4586 ancestorContainer;4587 ancestorContainer = ancestorContainer.parentNode4588 ) {4589 if (isHtmlElement(ancestorContainer, "li")) {4590 items.unshift(ancestorContainer);4591 }4592 }4593 })();4594 4595 items.forEach(normalizeSublists);4596 4597 var newRange = blockExtend(range);4598 4599 4600 4601 if (mode == "enable") {4602 getAllContainedNodes(newRange, function(node) {4603 return isEditable(node)4604 && isHtmlElement(node, otherTagName);4605 }).forEach(function(list) {4606 4607 4608 if ((isEditable(list.previousSibling) && isHtmlElement(list.previousSibling, tagName))4609 || (isEditable(list.nextSibling) && isHtmlElement(list.nextSibling, tagName))) {4610 4611 var children = [].slice.call(list.childNodes);4612 4613 4614 var values = recordValues(children);4615 4616 splitParent(children);4617 4618 4619 wrap(children, function(node) { return isHtmlElement(node, tagName) });4620 4621 restoreValues(values);4622 4623 } else {4624 setTagName(list, tagName);4625 }4626 });4627 }4628 4629 4630 4631 4632 4633 4634 4635 var nodeList = getContainedNodes(newRange, function(node) {4636 return isEditable(node)4637 && !isIndentationElement(node)4638 && (isHtmlElement(node, ["OL", "UL"])4639 || isHtmlElement(node.parentNode, ["OL", "UL"])4640 || isAllowedChild(node, "li"));4641 });4642 4643 4644 if (mode == "enable") {4645 nodeList = nodeList.filter(function(node) {4646 return !isHtmlElement(node, ["ol", "ul"])4647 || isHtmlElement(node.parentNode, ["ol", "ul"]);4648 });4649 }4650 4651 if (mode == "disable") {4652 while (nodeList.length) {4653 4654 var sublist = [];4655 4656 4657 sublist.push(nodeList.shift());4658 4659 4660 4661 if (isHtmlElement(sublist[0], tagName)) {4662 outdentNode(sublist[0]);4663 continue;4664 }4665 4666 4667 4668 4669 while (nodeList.length4670 && nodeList[0] == sublist[sublist.length - 1].nextSibling4671 && !isHtmlElement(nodeList[0], tagName)) {4672 sublist.push(nodeList.shift());4673 }4674 4675 var values = recordValues(sublist);4676 4677 splitParent(sublist);4678 4679 for (var i = 0; i < sublist.length; i++) {4680 fixDisallowedAncestors(sublist[i]);4681 }4682 4683 restoreValues(values);4684 }4685 4686 } else {4687 while (nodeList.length) {4688 4689 var sublist = [];4690 4691 4692 while (!sublist.length4693 || (nodeList.length4694 && nodeList[0] == sublist[sublist.length - 1].nextSibling)) {4695 4696 4697 4698 if (isHtmlElement(nodeList[0], ["p", "div"])) {4699 sublist.push(setTagName(nodeList[0], "li"));4700 nodeList.shift();4701 4702 4703 } else if (isHtmlElement(nodeList[0], ["li", "ol", "ul"])) {4704 sublist.push(nodeList.shift());4705 4706 } else {4707 4708 var nodesToWrap = [];4709 4710 4711 4712 4713 4714 4715 while (!nodesToWrap.length4716 || (nodeList.length4717 && nodeList[0] == nodesToWrap[nodesToWrap.length - 1].nextSibling4718 && isInlineNode(nodeList[0])4719 && isInlineNode(nodesToWrap[nodesToWrap.length - 1])4720 && !isHtmlElement(nodesToWrap[nodesToWrap.length - 1], "br"))) {4721 nodesToWrap.push(nodeList.shift());4722 }4723 4724 4725 4726 sublist.push(wrap(nodesToWrap,4727 undefined,4728 function() { return document.createElement("li") }));4729 }4730 }4731 4732 4733 4734 if (isHtmlElement(sublist[0].parentNode, tagName)4735 || sublist.every(function(node) { return isHtmlElement(node, ["ol", "ul"]) })) {4736 continue;4737 }4738 4739 4740 if (isHtmlElement(sublist[0].parentNode, otherTagName)) {4741 4742 4743 var values = recordValues(sublist);4744 4745 splitParent(sublist);4746 4747 4748 4749 4750 wrap(sublist,4751 function(node) { return isHtmlElement(node, tagName) },4752 function() { return document.createElement(tagName) });4753 4754 restoreValues(values);4755 4756 continue;4757 }4758 4759 4760 4761 4762 4763 fixDisallowedAncestors(wrap(sublist,4764 function(node) { return isHtmlElement(node, tagName) },4765 function() {4766 4767 4768 4769 4770 4771 if (!isEditable(sublist[0].parentNode)4772 || !isSimpleIndentationElement(sublist[0].parentNode)4773 || !isEditable(sublist[0].parentNode.previousSibling)4774 || !isHtmlElement(sublist[0].parentNode.previousSibling, tagName)) {4775 return document.createElement(tagName);4776 }4777 4778 4779 var list = sublist[0].parentNode.previousSibling;4780 4781 normalizeSublists(list.lastChild);4782 4783 4784 4785 4786 if (!isEditable(list.lastChild)4787 || !isHtmlElement(list.lastChild, tagName)) {4788 list.appendChild(document.createElement(tagName));4789 }4790 4791 return list.lastChild;4792 }4793 ));4794 }4795 }4796}4797function justifySelection(alignment) {4798 4799 var newRange = blockExtend(globalRange);4800 4801 4802 4803 4804 var elementList = getAllContainedNodes(newRange, function(node) {4805 return node.nodeType == Node.ELEMENT_NODE4806 && isEditable(node)4807 4808 && (4809 node.hasAttribute("align")4810 || node.style.textAlign != ""4811 || isHtmlElement(node, "center")4812 );4813 });4814 4815 for (var i = 0; i < elementList.length; i++) {4816 var element = elementList[i];4817 4818 4819 element.removeAttribute("align");4820 4821 4822 element.style.textAlign = "";4823 if (element.getAttribute("style") == "") {4824 element.removeAttribute("style");4825 }4826 4827 4828 if (isHtmlElement(element, ["div", "span", "center"])4829 && !element.attributes.length) {4830 removePreservingDescendants(element);4831 }4832 4833 4834 if (isHtmlElement(element, "center")4835 && element.attributes.length) {4836 setTagName(element, "div");4837 }4838 }4839 4840 newRange = blockExtend(globalRange);4841 4842 var nodeList = [];4843 4844 4845 4846 4847 nodeList = getContainedNodes(newRange, function(node) {4848 return isEditable(node)4849 && isAllowedChild(node, "div")4850 && getAlignmentValue(node) != alignment;4851 });4852 4853 while (nodeList.length) {4854 4855 var sublist = [];4856 4857 sublist.push(nodeList.shift());4858 4859 4860 4861 while (nodeList.length4862 && nodeList[0] == sublist[sublist.length - 1].nextSibling) {4863 sublist.push(nodeList.shift());4864 }4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 wrap(sublist,4879 function(node) {4880 return isHtmlElement(node, "div")4881 && [].every.call(node.attributes, function(attr) {4882 return (attr.name == "align" && attr.value.toLowerCase() == alignment)4883 || (attr.name == "style" && node.style.length == 1 && node.style.textAlign == alignment);4884 });4885 },4886 function() {4887 var newParent = document.createElement("div");4888 newParent.setAttribute("style", "text-align: " + alignment);4889 return newParent;4890 }4891 );4892 }4893}4894var autolinkableUrlRegexp =4895 4896 4897 4898 4899 4900 "([a-zA-Z][a-zA-Z0-9+.-]*://|mailto:)"4901 4902 + "[^ \t\n\f\r]*"4903 4904 + "[^!\"'(),\\-.:;<>[\\]`{}]";4905var validEmailRegexp =4906 "[a-zA-Z0-9!#$%&'*+\\-/=?^_`{|}~.]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*";4907function autolink(node, endOffset) {4908 4909 4910 while (getPreviousEquivalentPoint(node, endOffset)) {4911 var prev = getPreviousEquivalentPoint(node, endOffset);4912 node = prev[0];4913 endOffset = prev[1];4914 }4915 4916 4917 if (node.nodeType != Node.TEXT_NODE4918 || getAncestors(node).some(function(ancestor) { return isHtmlElement(ancestor, "a") })) {4919 return;4920 }4921 4922 4923 var search = /[^ \t\n\f\r]*$/.exec(node.substringData(0, endOffset))[0];4924 4925 if (new RegExp(autolinkableUrlRegexp).test(search)) {4926 4927 4928 while (!(new RegExp(autolinkableUrlRegexp + "$").test(node.substringData(0, endOffset)))) {4929 endOffset--;4930 }4931 4932 4933 var startOffset = new RegExp(autolinkableUrlRegexp + "$").exec(node.substringData(0, endOffset)).index;4934 4935 4936 var href = node.substringData(startOffset, endOffset - startOffset);4937 4938 } else if (new RegExp(validEmailRegexp).test(search)) {4939 4940 4941 while (!(new RegExp(validEmailRegexp + "$").test(node.substringData(0, endOffset)))) {4942 endOffset--;4943 }4944 4945 4946 var startOffset = new RegExp(validEmailRegexp + "$").exec(node.substringData(0, endOffset)).index;4947 4948 4949 var href = "mailto:" + node.substringData(startOffset, endOffset - startOffset);4950 4951 } else {4952 return;4953 }4954 4955 var originalRange = getActiveRange();4956 4957 4958 var newRange = document.createRange();4959 newRange.setStart(node, startOffset);4960 newRange.setEnd(node, endOffset);4961 getSelection().removeAllRanges();4962 getSelection().addRange(newRange);4963 globalRange = newRange;4964 4965 commands.createlink.action(href);4966 4967 getSelection().removeAllRanges();4968 getSelection().addRange(originalRange);4969 globalRange = originalRange;4970}4971commands["delete"] = {4972 preservesOverrides: true,4973 action: function() {4974 4975 4976 if (!getActiveRange().collapsed) {4977 deleteSelection();4978 return true;4979 }4980 4981 canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset);4982 4983 var node = getActiveRange().startContainer;4984 var offset = getActiveRange().startOffset;4985 4986 while (true) {4987 4988 4989 if (offset == 04990 && isEditable(node.previousSibling)4991 && isInvisible(node.previousSibling)) {4992 node.parentNode.removeChild(node.previousSibling);4993 4994 4995 4996 } else if (0 <= offset - 14997 && offset - 1 < node.childNodes.length4998 && isEditable(node.childNodes[offset - 1])4999 && isInvisible(node.childNodes[offset - 1])) {5000 node.removeChild(node.childNodes[offset - 1]);5001 offset--;5002 5003 5004 5005 } else if ((offset == 05006 && isInlineNode(node))5007 || isInvisible(node)) {5008 offset = getNodeIndex(node);5009 node = node.parentNode;5010 5011 5012 5013 } else if (0 <= offset - 15014 && offset - 1 < node.childNodes.length5015 && isEditable(node.childNodes[offset - 1])5016 && isHtmlElement(node.childNodes[offset - 1], "a")) {5017 removePreservingDescendants(node.childNodes[offset - 1]);5018 return true;5019 5020 5021 5022 } else if (0 <= offset - 15023 && offset - 1 < node.childNodes.length5024 && !isBlockNode(node.childNodes[offset - 1])5025 && !isHtmlElement(node.childNodes[offset - 1], ["br", "img"])) {5026 node = node.childNodes[offset - 1];5027 offset = getNodeLength(node);5028 5029 } else {5030 break;5031 }5032 }5033 5034 5035 5036 if ((node.nodeType == Node.TEXT_NODE5037 && offset != 0)5038 || (isBlockNode(node)5039 && 0 <= offset - 15040 && offset - 1 < node.childNodes.length5041 && isHtmlElement(node.childNodes[offset - 1], ["br", "hr", "img"]))) {5042 5043 getSelection().collapse(node, offset);5044 getActiveRange().setEnd(node, offset);5045 5046 5047 getSelection().extend(node, offset - 1);5048 getActiveRange().setStart(node, offset - 1);5049 5050 deleteSelection();5051 5052 return true;5053 }5054 5055 if (isInlineNode(node)) {5056 return true;5057 }5058 5059 5060 if (isHtmlElement(node, ["li", "dt", "dd"])5061 && node == node.parentNode.firstChild5062 && offset == 0) {5063 5064 5065 5066 var items = [];5067 for (var ancestor = node.parentNode; ancestor; ancestor = ancestor.parentNode) {5068 if (isHtmlElement(ancestor, "li")) {5069 items.unshift(ancestor);5070 }5071 }5072 5073 for (var i = 0; i < items.length; i++) {5074 normalizeSublists(items[i]);5075 }5076 5077 5078 var values = recordValues([node]);5079 5080 splitParent([node]);5081 5082 restoreValues(values);5083 5084 5085 5086 5087 if (isHtmlElement(node, ["dd", "dt"])5088 && getAncestors(node).every(function(ancestor) {5089 return !inSameEditingHost(node, ancestor)5090 || !isAllowedChild(node, ancestor)5091 })) {5092 node = setTagName(node, defaultSingleLineContainerName);5093 }5094 5095 fixDisallowedAncestors(node);5096 5097 return true;5098 }5099 5100 var startNode = node;5101 var startOffset = offset;5102 5103 while (true) {5104 5105 5106 if (startOffset == 0) {5107 startOffset = getNodeIndex(startNode);5108 startNode = startNode.parentNode;5109 5110 5111 5112 } else if (0 <= startOffset - 15113 && startOffset - 1 < startNode.childNodes.length5114 && isEditable(startNode.childNodes[startOffset - 1])5115 && isInvisible(startNode.childNodes[startOffset - 1])) {5116 startNode.removeChild(startNode.childNodes[startOffset - 1]);5117 startOffset--;5118 5119 } else {5120 break;5121 }5122 }5123 5124 5125 if (offset == 05126 && getAncestors(node).concat(node).filter(function(ancestor) {5127 return isEditable(ancestor)5128 && inSameEditingHost(ancestor, node)5129 && isIndentationElement(ancestor);5130 }).length) {5131 5132 5133 var newRange = document.createRange();5134 newRange.setStart(node, 0);5135 newRange = blockExtend(newRange);5136 5137 5138 5139 5140 5141 5142 var nodeList = getContainedNodes(newRange, function(currentNode) {5143 return isEditable(currentNode)5144 && !hasEditableDescendants(currentNode);5145 });5146 5147 for (var i = 0; i < nodeList.length; i++) {5148 outdentNode(nodeList[i]);5149 }5150 5151 return true;5152 }5153 5154 5155 if (isHtmlElement(startNode.childNodes[startOffset], "table")) {5156 return true;5157 }5158 5159 5160 if (0 <= startOffset - 15161 && startOffset - 1 < startNode.childNodes.length5162 && isHtmlElement(startNode.childNodes[startOffset - 1], "table")) {5163 5164 5165 getSelection().collapse(startNode, startOffset - 1);5166 getActiveRange().setStart(startNode, startOffset - 1);5167 5168 5169 getSelection().extend(startNode, startOffset);5170 getActiveRange().setEnd(startNode, startOffset);5171 5172 return true;5173 }5174 5175 5176 5177 if (offset == 05178 && (isHtmlElement(startNode.childNodes[startOffset - 1], "hr")5179 || (5180 isHtmlElement(startNode.childNodes[startOffset - 1], "br")5181 && (5182 isHtmlElement(startNode.childNodes[startOffset - 1].previousSibling, "br")5183 || !isInlineNode(startNode.childNodes[startOffset - 1].previousSibling)5184 )5185 )5186 )) {5187 5188 5189 getSelection().collapse(startNode, startOffset - 1);5190 getActiveRange().setStart(startNode, startOffset - 1);5191 5192 5193 getSelection().extend(startNode, startOffset);5194 getActiveRange().setEnd(startNode, startOffset);5195 5196 deleteSelection();5197 5198 getSelection().collapse(node, offset);5199 getActiveRange().setStart(node, offset);5200 getActiveRange().collapse(true);5201 5202 return true;5203 }5204 5205 5206 5207 if (isHtmlElement(startNode.childNodes[startOffset], ["li", "dt", "dd"])5208 && isInlineNode(startNode.childNodes[startOffset].firstChild)5209 && startOffset != 0) {5210 5211 5212 var previousItem = startNode.childNodes[startOffset - 1];5213 5214 5215 5216 if (isInlineNode(previousItem.lastChild)5217 && !isHtmlElement(previousItem.lastChild, "br")) {5218 previousItem.appendChild(document.createElement("br"));5219 }5220 5221 5222 5223 if (isInlineNode(previousItem.lastChild)) {5224 previousItem.appendChild(document.createElement("br"));5225 }5226 }5227 5228 5229 if (isHtmlElement(startNode.childNodes[startOffset], ["li", "dt", "dd"])5230 && isHtmlElement(startNode.childNodes[startOffset].previousSibling, ["li", "dt", "dd"])) {5231 5232 5233 5234 5235 5236 var originalRange = getActiveRange().cloneRange();5237 extraRanges.push(originalRange);5238 5239 startNode = startNode.childNodes[startOffset - 1];5240 5241 startOffset = getNodeLength(startNode);5242 5243 node = startNode.nextSibling;5244 5245 5246 getSelection().collapse(startNode, startOffset);5247 getActiveRange().setStart(startNode, startOffset);5248 5249 getSelection().extend(node, 0);5250 getActiveRange().setEnd(node, 0);5251 5252 deleteSelection();5253 5254 getSelection().removeAllRanges();5255 5256 5257 getSelection().addRange(originalRange);5258 getActiveRange().setStart(originalRange.startContainer, originalRange.startOffset);5259 getActiveRange().setEnd(originalRange.endContainer, originalRange.endOffset);5260 5261 extraRanges.pop();5262 return true;5263 }5264 5265 while (0 <= startOffset - 15266 && startOffset - 1 < startNode.childNodes.length) {5267 5268 5269 5270 if (isEditable(startNode.childNodes[startOffset - 1])5271 && isInvisible(startNode.childNodes[startOffset - 1])) {5272 startNode.removeChild(startNode.childNodes[startOffset - 1]);5273 startOffset--;5274 5275 5276 } else {5277 startNode = startNode.childNodes[startOffset - 1];5278 startOffset = getNodeLength(startNode);5279 }5280 }5281 5282 5283 getSelection().collapse(startNode, startOffset);5284 getActiveRange().setStart(startNode, startOffset);5285 5286 getSelection().extend(node, offset);5287 getActiveRange().setEnd(node, offset);5288 5289 deleteSelection({direction: "backward"});5290 5291 return true;5292 }5293};5294var formattableBlockNames = ["address", "dd", "div", "dt", "h1", "h2", "h3",5295 "h4", "h5", "h6", "p", "pre"];5296commands.formatblock = {5297 preservesOverrides: true,5298 action: function(value) {5299 5300 5301 if (/^<.*>$/.test(value)) {5302 value = value.slice(1, -1);5303 }5304 5305 value = value.toLowerCase();5306 5307 if (formattableBlockNames.indexOf(value) == -1) {5308 return false;5309 }5310 5311 var newRange = blockExtend(getActiveRange());5312 5313 5314 5315 5316 5317 5318 5319 var nodeList = getContainedNodes(newRange, function(node) {5320 return isEditable(node)5321 && (isNonListSingleLineContainer(node)5322 || isAllowedChild(node, "p")5323 || isHtmlElement(node, ["dd", "dt"]))5324 && !getDescendants(node).some(isProhibitedParagraphChild);5325 });5326 5327 var values = recordValues(nodeList);5328 5329 5330 5331 5332 5333 for (var i = 0; i < nodeList.length; i++) {5334 var node = nodeList[i];5335 while (getAncestors(node).some(function(ancestor) {5336 return isEditable(ancestor)5337 && inSameEditingHost(ancestor, node)5338 && isHtmlElement(ancestor, formattableBlockNames)5339 && !getDescendants(ancestor).some(isProhibitedParagraphChild);5340 })) {5341 splitParent([node]);5342 }5343 }5344 5345 restoreValues(values);5346 5347 while (nodeList.length) {5348 var sublist;5349 5350 5351 if (isSingleLineContainer(nodeList[0])) {5352 5353 5354 sublist = [].slice.call(nodeList[0].childNodes);5355 5356 5357 var values = recordValues(sublist);5358 5359 5360 removePreservingDescendants(nodeList[0]);5361 5362 restoreValues(values);5363 5364 nodeList.shift();5365 5366 } else {5367 5368 sublist = [];5369 5370 5371 sublist.push(nodeList.shift());5372 5373 5374 5375 5376 5377 5378 while (nodeList.length5379 && nodeList[0] == sublist[sublist.length - 1].nextSibling5380 && !isSingleLineContainer(nodeList[0])5381 && !isHtmlElement(sublist[sublist.length - 1], "BR")) {5382 sublist.push(nodeList.shift());5383 }5384 }5385 5386 5387 5388 5389 5390 5391 fixDisallowedAncestors(wrap(sublist,5392 ["div", "p"].indexOf(value) == - 15393 ? function(node) { return isHtmlElement(node, value) && !node.attributes.length }5394 : function() { return false },5395 function() { return document.createElement(value) }));5396 }5397 5398 return true;5399 }, indeterm: function() {5400 5401 if (!getActiveRange()) {5402 return false;5403 }5404 5405 var newRange = blockExtend(getActiveRange());5406 5407 5408 var nodeList = getAllContainedNodes(newRange, function(node) {5409 return isVisible(node)5410 && isEditable(node)5411 && !node.hasChildNodes();5412 });5413 5414 if (!nodeList.length) {5415 return false;5416 }5417 5418 var type = null;5419 5420 for (var i = 0; i < nodeList.length; i++) {5421 var node = nodeList[i];5422 5423 5424 5425 while (isEditable(node.parentNode)5426 && inSameEditingHost(node, node.parentNode)5427 && !isHtmlElement(node, formattableBlockNames)) {5428 node = node.parentNode;5429 }5430 5431 var currentType = "";5432 5433 5434 5435 5436 if (isEditable(node)5437 && isHtmlElement(node, formattableBlockNames)5438 && !getDescendants(node).some(isProhibitedParagraphChild)) {5439 currentType = node.tagName;5440 }5441 5442 if (type === null) {5443 type = currentType;5444 5445 } else if (type != currentType) {5446 return true;5447 }5448 }5449 5450 return false;5451 }, value: function() {5452 5453 if (!getActiveRange()) {5454 return "";5455 }5456 5457 var newRange = blockExtend(getActiveRange());5458 5459 5460 5461 var nodes = getAllContainedNodes(newRange, function(node) {5462 return isVisible(node)5463 && isEditable(node)5464 && !node.hasChildNodes();5465 });5466 if (!nodes.length) {5467 return "";5468 }5469 var node = nodes[0];5470 5471 5472 5473 while (isEditable(node.parentNode)5474 && inSameEditingHost(node, node.parentNode)5475 && !isHtmlElement(node, formattableBlockNames)) {5476 node = node.parentNode;5477 }5478 5479 5480 5481 5482 if (isEditable(node)5483 && isHtmlElement(node, formattableBlockNames)5484 && !getDescendants(node).some(isProhibitedParagraphChild)) {5485 return node.tagName.toLowerCase();5486 }5487 5488 return "";5489 }5490};5491commands.forwarddelete = {5492 preservesOverrides: true,5493 action: function() {5494 5495 5496 if (!getActiveRange().collapsed) {5497 deleteSelection();5498 return true;5499 }5500 5501 canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset);5502 5503 var node = getActiveRange().startContainer;5504 var offset = getActiveRange().startOffset;5505 5506 while (true) {5507 5508 5509 5510 if (offset == getNodeLength(node)5511 && isEditable(node.nextSibling)5512 && isInvisible(node.nextSibling)) {5513 node.parentNode.removeChild(node.nextSibling);5514 5515 5516 } else if (offset < node.childNodes.length5517 && isEditable(node.childNodes[offset])5518 && isInvisible(node.childNodes[offset])) {5519 node.removeChild(node.childNodes[offset]);5520 5521 5522 5523 } else if ((offset == getNodeLength(node)5524 && isInlineNode(node))5525 || isInvisible(node)) {5526 offset = 1 + getNodeIndex(node);5527 node = node.parentNode;5528 5529 5530 5531 } else if (offset < node.childNodes.length5532 && !isBlockNode(node.childNodes[offset])5533 && !isHtmlElement(node.childNodes[offset], ["br", "img"])5534 && !isCollapsedBlockProp(node.childNodes[offset])) {5535 node = node.childNodes[offset];5536 offset = 0;5537 5538 } else {5539 break;5540 }5541 }5542 5543 if (node.nodeType == Node.TEXT_NODE5544 && offset != getNodeLength(node)) {5545 5546 var endOffset = offset + 1;5547 5548 5549 5550 5551 5552 5553 5554 5555 while (endOffset != node.length5556 && /^[\u0300-\u036f\u0591-\u05bd\u05c1\u05c2]$/.test(node.data[endOffset])) {5557 endOffset++;5558 }5559 5560 getSelection().collapse(node, offset);5561 getActiveRange().setStart(node, offset);5562 5563 5564 getSelection().extend(node, endOffset);5565 getActiveRange().setEnd(node, endOffset);5566 5567 deleteSelection();5568 5569 return true;5570 }5571 5572 if (isInlineNode(node)) {5573 return true;5574 }5575 5576 5577 if (offset < node.childNodes.length5578 && isHtmlElement(node.childNodes[offset], ["br", "hr", "img"])5579 && !isCollapsedBlockProp(node.childNodes[offset])) {5580 5581 getSelection().collapse(node, offset);5582 getActiveRange().setStart(node, offset);5583 5584 5585 getSelection().extend(node, offset + 1);5586 getActiveRange().setEnd(node, offset + 1);5587 5588 deleteSelection();5589 5590 return true;5591 }5592 5593 var endNode = node;5594 var endOffset = offset;5595 5596 5597 if (endOffset < endNode.childNodes.length5598 && isCollapsedBlockProp(endNode.childNodes[endOffset])) {5599 endOffset++;5600 }5601 5602 while (true) {5603 5604 5605 if (endOffset == getNodeLength(endNode)) {5606 endOffset = 1 + getNodeIndex(endNode);5607 endNode = endNode.parentNode;5608 5609 5610 } else if (endOffset < endNode.childNodes.length5611 && isEditable(endNode.childNodes[endOffset])5612 && isInvisible(endNode.childNodes[endOffset])) {5613 endNode.removeChild(endNode.childNodes[endOffset]);5614 5615 } else {5616 break;5617 }5618 }5619 5620 5621 if (isHtmlElement(endNode.childNodes[endOffset - 1], "table")) {5622 return true;5623 }5624 5625 if (isHtmlElement(endNode.childNodes[endOffset], "table")) {5626 5627 5628 getSelection().collapse(endNode, endOffset);5629 getActiveRange().setStart(endNode, endOffset);5630 5631 5632 getSelection().extend(endNode, endOffset + 1);5633 getActiveRange().setEnd(endNode, endOffset + 1);5634 5635 return true;5636 }5637 5638 5639 if (offset == getNodeLength(node)5640 && isHtmlElement(endNode.childNodes[endOffset], ["br", "hr"])) {5641 5642 5643 getSelection().collapse(endNode, endOffset);5644 getActiveRange().setStart(endNode, endOffset);5645 5646 5647 getSelection().extend(endNode, endOffset + 1);5648 getActiveRange().setEnd(endNode, endOffset + 1);5649 5650 deleteSelection();5651 5652 getSelection().collapse(node, offset);5653 getActiveRange().setStart(node, offset);5654 getActiveRange().collapse(true);5655 5656 return true;5657 }5658 5659 while (endOffset < endNode.childNodes.length) {5660 5661 5662 if (isEditable(endNode.childNodes[endOffset])5663 && isInvisible(endNode.childNodes[endOffset])) {5664 endNode.removeChild(endNode.childNodes[endOffset]);5665 5666 5667 } else {5668 endNode = endNode.childNodes[endOffset];5669 endOffset = 0;5670 }5671 }5672 5673 getSelection().collapse(node, offset);5674 getActiveRange().setStart(node, offset);5675 5676 5677 getSelection().extend(endNode, endOffset);5678 getActiveRange().setEnd(endNode, endOffset);5679 5680 deleteSelection();5681 5682 return true;5683 }5684};5685commands.indent = {5686 preservesOverrides: true,5687 action: function() {5688 5689 5690 5691 5692 var items = [];5693 for (var node = getActiveRange().endContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) {5694 if (isHtmlElement(node, "LI")) {5695 items.unshift(node);5696 }5697 }5698 for (var node = getActiveRange().startContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) {5699 if (isHtmlElement(node, "LI")) {5700 items.unshift(node);5701 }5702 }5703 for (var node = getActiveRange().commonAncestorContainer; node; node = node.parentNode) {5704 if (isHtmlElement(node, "LI")) {5705 items.unshift(node);5706 }5707 }5708 5709 for (var i = 0; i < items.length; i++) {5710 normalizeSublists(items[i]);5711 }5712 5713 var newRange = blockExtend(getActiveRange());5714 5715 var nodeList = [];5716 5717 5718 5719 nodeList = getContainedNodes(newRange, function(node) {5720 return isEditable(node)5721 && (isAllowedChild(node, "div")5722 || isAllowedChild(node, "ol"));5723 });5724 5725 5726 if (isHtmlElement(nodeList.filter(isVisible)[0], "li")5727 && isHtmlElement(nodeList.filter(isVisible)[0].parentNode, ["ol", "ul"])) {5728 5729 5730 var sibling = nodeList.filter(isVisible)[0].previousSibling;5731 5732 5733 while (isInvisible(sibling)) {5734 sibling = sibling.previousSibling;5735 }5736 5737 if (isHtmlElement(sibling, "li")) {5738 normalizeSublists(sibling);5739 }5740 }5741 5742 while (nodeList.length) {5743 5744 var sublist = [];5745 5746 sublist.push(nodeList.shift());5747 5748 5749 5750 while (nodeList.length5751 && nodeList[0] == sublist[sublist.length - 1].nextSibling) {5752 sublist.push(nodeList.shift());5753 }5754 5755 indentNodes(sublist);5756 }5757 5758 return true;5759 }5760};5761commands.inserthorizontalrule = {5762 preservesOverrides: true,5763 action: function() {5764 5765 5766 var startNode = getActiveRange().startContainer;5767 var startOffset = getActiveRange().startOffset;5768 var endNode = getActiveRange().endContainer;5769 var endOffset = getActiveRange().endOffset;5770 5771 5772 5773 while (startOffset == 05774 && startNode.parentNode) {5775 startOffset = getNodeIndex(startNode);5776 startNode = startNode.parentNode;5777 }5778 5779 5780 5781 while (endOffset == getNodeLength(endNode)5782 && endNode.parentNode) {5783 endOffset = 1 + getNodeIndex(endNode);5784 endNode = endNode.parentNode;5785 }5786 5787 5788 getSelection().collapse(startNode, startOffset);5789 getActiveRange().setStart(startNode, startOffset);5790 5791 5792 getSelection().extend(endNode, endOffset);5793 getActiveRange().setEnd(endNode, endOffset);5794 5795 deleteSelection({blockMerging: false});5796 5797 5798 if (!isEditable(getActiveRange().startContainer)5799 && !isEditingHost(getActiveRange().startContainer)) {5800 return true;5801 }5802 5803 5804 5805 5806 if (getActiveRange().startContainer.nodeType == Node.TEXT_NODE5807 && getActiveRange().startOffset == 0) {5808 var newNode = getActiveRange().startContainer.parentNode;5809 var newOffset = getNodeIndex(getActiveRange().startContainer);5810 getSelection().collapse(newNode, newOffset);5811 getActiveRange().setStart(newNode, newOffset);5812 getActiveRange().collapse(true);5813 }5814 5815 5816 5817 5818 5819 if (getActiveRange().startContainer.nodeType == Node.TEXT_NODE5820 && getActiveRange().startOffset == getNodeLength(getActiveRange().startContainer)) {5821 var newNode = getActiveRange().startContainer.parentNode;5822 var newOffset = 1 + getNodeIndex(getActiveRange().startContainer);5823 getSelection().collapse(newNode, newOffset);5824 getActiveRange().setStart(newNode, newOffset);5825 getActiveRange().collapse(true);5826 }5827 5828 5829 var hr = document.createElement("hr");5830 5831 getActiveRange().insertNode(hr);5832 5833 fixDisallowedAncestors(hr);5834 5835 5836 5837 getSelection().collapse(hr.parentNode, 1 + getNodeIndex(hr));5838 getActiveRange().setStart(hr.parentNode, 1 + getNodeIndex(hr));5839 getActiveRange().collapse(true);5840 5841 return true;5842 }5843};5844commands.inserthtml = {5845 preservesOverrides: true,5846 action: function(value) {5847 5848 deleteSelection();5849 5850 5851 if (!isEditable(getActiveRange().startContainer)5852 && !isEditingHost(getActiveRange().startContainer)) {5853 return true;5854 }5855 5856 5857 var frag = getActiveRange().createContextualFragment(value);5858 5859 var lastChild = frag.lastChild;5860 5861 if (!lastChild) {5862 return true;5863 }5864 5865 var descendants = getDescendants(frag);5866 5867 if (isBlockNode(getActiveRange().startContainer)) {5868 5869 5870 5871 5872 5873 5874 [].filter.call(getActiveRange().startContainer.childNodes, function(node) {5875 return isEditable(node)5876 && isCollapsedBlockProp(node)5877 && getNodeIndex(node) >= getActiveRange().startOffset;5878 }).forEach(function(node) {5879 node.parentNode.removeChild(node);5880 });5881 }5882 5883 getActiveRange().insertNode(frag);5884 5885 5886 5887 if (isBlockNode(getActiveRange().startContainer)5888 && ![].some.call(getActiveRange().startContainer.childNodes, isVisible)) {5889 getActiveRange().startContainer.appendChild(document.createElement("br"));5890 }5891 5892 5893 5894 getActiveRange().setStart(lastChild.parentNode, 1 + getNodeIndex(lastChild));5895 getActiveRange().setEnd(lastChild.parentNode, 1 + getNodeIndex(lastChild));5896 5897 for (var i = 0; i < descendants.length; i++) {5898 fixDisallowedAncestors(descendants[i]);5899 }5900 5901 return true;5902 }5903};5904commands.insertimage = {5905 preservesOverrides: true,5906 action: function(value) {5907 5908 if (value === "") {5909 return false;5910 }5911 5912 deleteSelection({stripWrappers: false});5913 5914 var range = getActiveRange();5915 5916 5917 if (!isEditable(getActiveRange().startContainer)5918 && !isEditingHost(getActiveRange().startContainer)) {5919 return true;5920 }5921 5922 5923 if (isBlockNode(range.startContainer)5924 && range.startContainer.childNodes.length == 15925 && isHtmlElement(range.startContainer.firstChild, "br")5926 && range.startOffset == 0) {5927 range.startContainer.removeChild(range.startContainer.firstChild);5928 }5929 5930 5931 var img = document.createElement("img");5932 5933 img.setAttribute("src", value);5934 5935 range.insertNode(img);5936 5937 5938 5939 5940 5941 5942 5943 range.setStart(img.parentNode, 1 + getNodeIndex(img));5944 range.setEnd(img.parentNode, 1 + getNodeIndex(img));5945 getSelection().removeAllRanges();5946 getSelection().addRange(range);5947 5948 5949 img.removeAttribute("width");5950 img.removeAttribute("height");5951 5952 return true;5953 }5954};5955commands.insertlinebreak = {5956 preservesOverrides: true,5957 action: function(value) {5958 5959 deleteSelection({stripWrappers: false});5960 5961 5962 if (!isEditable(getActiveRange().startContainer)5963 && !isEditingHost(getActiveRange().startContainer)) {5964 return true;5965 }5966 5967 5968 if (getActiveRange().startContainer.nodeType == Node.ELEMENT_NODE5969 && !isAllowedChild("br", getActiveRange().startContainer)) {5970 return true;5971 }5972 5973 5974 5975 if (getActiveRange().startContainer.nodeType != Node.ELEMENT_NODE5976 && !isAllowedChild("br", getActiveRange().startContainer.parentNode)) {5977 return true;5978 }5979 5980 5981 5982 5983 if (getActiveRange().startContainer.nodeType == Node.TEXT_NODE5984 && getActiveRange().startOffset == 0) {5985 var newNode = getActiveRange().startContainer.parentNode;5986 var newOffset = getNodeIndex(getActiveRange().startContainer);5987 getSelection().collapse(newNode, newOffset);5988 getActiveRange().setStart(newNode, newOffset);5989 getActiveRange().setEnd(newNode, newOffset);5990 }5991 5992 5993 5994 5995 5996 if (getActiveRange().startContainer.nodeType == Node.TEXT_NODE5997 && getActiveRange().startOffset == getNodeLength(getActiveRange().startContainer)) {5998 var newNode = getActiveRange().startContainer.parentNode;5999 var newOffset = 1 + getNodeIndex(getActiveRange().startContainer);6000 getSelection().collapse(newNode, newOffset);6001 getActiveRange().setStart(newNode, newOffset);6002 getActiveRange().setEnd(newNode, newOffset);6003 }6004 6005 6006 var br = document.createElement("br");6007 6008 getActiveRange().insertNode(br);6009 6010 6011 6012 getSelection().collapse(br.parentNode, 1 + getNodeIndex(br));6013 getActiveRange().setStart(br.parentNode, 1 + getNodeIndex(br));6014 getActiveRange().setEnd(br.parentNode, 1 + getNodeIndex(br));6015 6016 6017 6018 if (isCollapsedLineBreak(br)) {6019 getActiveRange().insertNode(document.createElement("br"));6020 6021 getSelection().collapse(br.parentNode, 1 + getNodeIndex(br));6022 getActiveRange().setStart(br.parentNode, 1 + getNodeIndex(br));6023 getActiveRange().setEnd(br.parentNode, 1 + getNodeIndex(br));6024 }6025 6026 return true;6027 }6028};6029commands.insertorderedlist = {6030 preservesOverrides: true,6031 6032 action: function() { toggleLists("ol"); return true },6033 6034 6035 indeterm: function() { return /^mixed( ol)?$/.test(getSelectionListState()) },6036 6037 state: function() { return getSelectionListState() == "ol" },6038};6039commands.insertparagraph = {6040 preservesOverrides: true,6041 action: function() {6042 6043 deleteSelection();6044 6045 6046 if (!isEditable(getActiveRange().startContainer)6047 && !isEditingHost(getActiveRange().startContainer)) {6048 return true;6049 }6050 6051 var node = getActiveRange().startContainer;6052 var offset = getActiveRange().startOffset;6053 6054 6055 if (node.nodeType == Node.TEXT_NODE6056 && offset != 06057 && offset != getNodeLength(node)) {6058 node.splitText(offset);6059 }6060 6061 6062 if (node.nodeType == Node.TEXT_NODE6063 && offset == getNodeLength(node)) {6064 offset = 1 + getNodeIndex(node);6065 node = node.parentNode;6066 }6067 6068 6069 if (node.nodeType == Node.TEXT_NODE6070 || node.nodeType == Node.COMMENT_NODE) {6071 offset = getNodeIndex(node);6072 node = node.parentNode;6073 }6074 6075 getSelection().collapse(node, offset);6076 getActiveRange().setStart(node, offset);6077 getActiveRange().setEnd(node, offset);6078 6079 var container = node;6080 6081 6082 6083 while (!isSingleLineContainer(container)6084 && isEditable(container.parentNode)6085 && inSameEditingHost(node, container.parentNode)) {6086 container = container.parentNode;6087 }6088 6089 6090 if (isEditable(container)6091 && isSingleLineContainer(container)6092 && inSameEditingHost(node, container.parentNode)6093 && (container.tagName == "P" || container.tagName == "DIV")) {6094 6095 var outerContainer = container;6096 6097 6098 6099 while (!isHtmlElement(outerContainer, ["dd", "dt", "li"])6100 && isEditable(outerContainer.parentNode)) {6101 outerContainer = outerContainer.parentNode;6102 }6103 6104 6105 if (isHtmlElement(outerContainer, ["dd", "dt", "li"])) {6106 container = outerContainer;6107 }6108 }6109 6110 6111 if (!isEditable(container)6112 || !inSameEditingHost(container, node)6113 || !isSingleLineContainer(container)) {6114 6115 var tag = defaultSingleLineContainerName;6116 6117 6118 var newRange = blockExtend(getActiveRange());6119 6120 6121 6122 6123 var nodeList = getContainedNodes(newRange, function(node) { return isAllowedChild(node, "p") })6124 .slice(0, 1);6125 6126 if (!nodeList.length) {6127 6128 6129 if (!isAllowedChild(tag, getActiveRange().startContainer)) {6130 return true;6131 }6132 6133 6134 container = document.createElement(tag);6135 6136 getActiveRange().insertNode(container);6137 6138 6139 container.appendChild(document.createElement("br"));6140 6141 6142 getSelection().collapse(container, 0);6143 getActiveRange().setStart(container, 0);6144 getActiveRange().setEnd(container, 0);6145 6146 return true;6147 }6148 6149 6150 while (nodeList[nodeList.length - 1].nextSibling6151 && isAllowedChild(nodeList[nodeList.length - 1].nextSibling, "p")) {6152 nodeList.push(nodeList[nodeList.length - 1].nextSibling);6153 }6154 6155 6156 6157 6158 container = wrap(nodeList,6159 function() { return false },6160 function() { return document.createElement(tag) }6161 );6162 }6163 6164 if (container.tagName == "ADDRESS"6165 || container.tagName == "LISTING"6166 || container.tagName == "PRE") {6167 6168 6169 var br = document.createElement("br");6170 6171 getActiveRange().insertNode(br);6172 6173 6174 getSelection().collapse(node, offset + 1);6175 getActiveRange().setStart(node, offset + 1);6176 getActiveRange().setEnd(node, offset + 1);6177 6178 6179 6180 6181 6182 6183 if (!isDescendant(nextNode(br), container)) {6184 getActiveRange().insertNode(document.createElement("br"));6185 getSelection().collapse(node, offset + 1);6186 getActiveRange().setEnd(node, offset + 1);6187 }6188 6189 return true;6190 }6191 6192 6193 if (["LI", "DT", "DD"].indexOf(container.tagName) != -16194 && (!container.hasChildNodes()6195 || (container.childNodes.length == 16196 && isHtmlElement(container.firstChild, "br")))) {6197 6198 splitParent([container]);6199 6200 6201 6202 if (!container.hasChildNodes()) {6203 container.appendChild(document.createElement("br"));6204 }6205 6206 6207 6208 6209 if (isHtmlElement(container, ["dd", "dt"])6210 && getAncestors(container).every(function(ancestor) {6211 return !inSameEditingHost(container, ancestor)6212 || !isAllowedChild(container, ancestor)6213 })) {6214 container = setTagName(container, defaultSingleLineContainerName);6215 }6216 6217 fixDisallowedAncestors(container);6218 6219 return true;6220 }6221 6222 6223 6224 var newLineRange = document.createRange();6225 newLineRange.setStart(getActiveRange().startContainer, getActiveRange().startOffset);6226 newLineRange.setEnd(container, getNodeLength(container));6227 6228 6229 6230 while (newLineRange.startOffset == 06231 && !isProhibitedParagraphChild(newLineRange.startContainer)) {6232 newLineRange.setStart(newLineRange.startContainer.parentNode, getNodeIndex(newLineRange.startContainer));6233 }6234 6235 6236 6237 while (newLineRange.startOffset == getNodeLength(newLineRange.startContainer)6238 && !isProhibitedParagraphChild(newLineRange.startContainer)) {6239 newLineRange.setStart(newLineRange.startContainer.parentNode, 1 + getNodeIndex(newLineRange.startContainer));6240 }6241 6242 6243 var containedInNewLineRange = getContainedNodes(newLineRange);6244 var endOfLine = !containedInNewLineRange.length6245 || (containedInNewLineRange.length == 16246 && isHtmlElement(containedInNewLineRange[0], "br"));6247 6248 6249 6250 var newContainerName;6251 if (/^H[1-6]$/.test(container.tagName)6252 && endOfLine) {6253 newContainerName = defaultSingleLineContainerName;6254 6255 6256 } else if (container.tagName == "DT"6257 && endOfLine) {6258 newContainerName = "dd";6259 6260 6261 } else if (container.tagName == "DD"6262 && endOfLine) {6263 newContainerName = "dt";6264 6265 } else {6266 newContainerName = container.tagName.toLowerCase();6267 }6268 6269 6270 var newContainer = document.createElement(newContainerName);6271 6272 for (var i = 0; i < container.attributes.length; i++) {6273 newContainer.setAttributeNS(container.attributes[i].namespaceURI, container.attributes[i].name, container.attributes[i].value);6274 }6275 6276 newContainer.removeAttribute("id");6277 6278 6279 container.parentNode.insertBefore(newContainer, container.nextSibling);6280 6281 var containedNodes = getAllContainedNodes(newLineRange);6282 6283 6284 var frag = newLineRange.extractContents();6285 6286 6287 var descendants = getDescendants(frag);6288 for (var i = 0; i < descendants.length; i++) {6289 if (descendants[i].nodeType == Node.ELEMENT_NODE6290 && containedNodes.indexOf(descendants[i]) == -1) {6291 descendants[i].removeAttribute("id");6292 }6293 }6294 6295 newContainer.appendChild(frag);6296 6297 6298 while (isProhibitedParagraphChild(container.lastChild)) {6299 container = container.lastChild;6300 }6301 6302 6303 while (isProhibitedParagraphChild(newContainer.lastChild)) {6304 newContainer = newContainer.lastChild;6305 }6306 6307 6308 6309 if (![].some.call(container.childNodes, isVisible)) {6310 container.appendChild(document.createElement("br"));6311 }6312 6313 6314 6315 if (![].some.call(newContainer.childNodes, isVisible)) {6316 newContainer.appendChild(document.createElement("br"));6317 }6318 6319 getSelection().collapse(newContainer, 0);6320 getActiveRange().setStart(newContainer, 0);6321 getActiveRange().setEnd(newContainer, 0);6322 6323 return true;6324 }6325};6326commands.inserttext = {6327 action: function(value) {6328 6329 deleteSelection({stripWrappers: false});6330 6331 6332 if (!isEditable(getActiveRange().startContainer)6333 && !isEditingHost(getActiveRange().startContainer)) {6334 return true;6335 }6336 6337 if (value.length > 1) {6338 6339 6340 for (var i = 0; i < value.length; i++) {6341 commands.inserttext.action(value[i]);6342 }6343 6344 return true;6345 }6346 6347 if (value == "") {6348 return true;6349 }6350 6351 6352 if (value == "\n") {6353 commands.insertparagraph.action();6354 return true;6355 }6356 6357 var node = getActiveRange().startContainer;6358 var offset = getActiveRange().startOffset;6359 6360 6361 6362 if (0 <= offset - 16363 && offset - 1 < node.childNodes.length6364 && node.childNodes[offset - 1].nodeType == Node.TEXT_NODE) {6365 node = node.childNodes[offset - 1];6366 offset = getNodeLength(node);6367 }6368 6369 6370 if (0 <= offset6371 && offset < node.childNodes.length6372 && node.childNodes[offset].nodeType == Node.TEXT_NODE) {6373 node = node.childNodes[offset];6374 offset = 0;6375 }6376 6377 var overrides = recordCurrentOverrides();6378 6379 getSelection().collapse(node, offset);6380 getActiveRange().setStart(node, offset);6381 getActiveRange().setEnd(node, offset);6382 6383 canonicalizeWhitespace(node, offset);6384 6385 node = getActiveRange().startContainer;6386 offset = getActiveRange().startOffset;6387 6388 if (node.nodeType == Node.TEXT_NODE) {6389 6390 node.insertData(offset, value);6391 6392 getSelection().collapse(node, offset);6393 getActiveRange().setStart(node, offset);6394 6395 6396 6397 6398 6399 try { getSelection().extend(node, offset + 1); } catch(e) {}6400 getActiveRange().setEnd(node, offset + 1);6401 6402 } else {6403 6404 6405 6406 6407 6408 if (node.childNodes.length == 16409 && isCollapsedLineBreak(node.firstChild)) {6410 node.removeChild(node.firstChild);6411 }6412 6413 6414 var text = document.createTextNode(value);6415 6416 getActiveRange().insertNode(text);6417 6418 getSelection().collapse(text, 0);6419 getActiveRange().setStart(text, 0);6420 6421 getSelection().extend(text, 1);6422 getActiveRange().setEnd(text, 1);6423 }6424 6425 restoreStatesAndValues(overrides);6426 6427 6428 canonicalizeWhitespace(getActiveRange().startContainer, getActiveRange().startOffset, false);6429 6430 6431 canonicalizeWhitespace(getActiveRange().endContainer, getActiveRange().endOffset, false);6432 6433 if (/^[ \t\n\f\r]$/.test(value)) {6434 autolink(getActiveRange().startContainer, getActiveRange().startOffset);6435 }6436 6437 6438 6439 6440 try { getSelection().collapseToEnd(); } catch(e) {}6441 getActiveRange().collapse(false);6442 6443 return true;6444 }6445};6446commands.insertunorderedlist = {6447 preservesOverrides: true,6448 6449 action: function() { toggleLists("ul"); return true },6450 6451 6452 indeterm: function() { return /^mixed( ul)?$/.test(getSelectionListState()) },6453 6454 state: function() { return getSelectionListState() == "ul" },6455};6456commands.justifycenter = {6457 preservesOverrides: true,6458 6459 action: function() { justifySelection("center"); return true },6460 indeterm: function() {6461 6462 6463 6464 6465 6466 if (!getActiveRange()) {6467 return false;6468 }6469 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6470 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6471 });6472 return nodes.some(function(node) { return getAlignmentValue(node) == "center" })6473 && nodes.some(function(node) { return getAlignmentValue(node) != "center" });6474 }, state: function() {6475 6476 6477 6478 6479 6480 if (!getActiveRange()) {6481 return false;6482 }6483 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6484 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6485 });6486 return nodes.length6487 && nodes.every(function(node) { return getAlignmentValue(node) == "center" });6488 }, value: function() {6489 6490 6491 6492 6493 if (!getActiveRange()) {6494 return "";6495 }6496 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6497 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6498 });6499 if (nodes.length) {6500 return getAlignmentValue(nodes[0]);6501 } else {6502 return "left";6503 }6504 },6505};6506commands.justifyfull = {6507 preservesOverrides: true,6508 6509 action: function() { justifySelection("justify"); return true },6510 indeterm: function() {6511 6512 6513 6514 6515 6516 if (!getActiveRange()) {6517 return false;6518 }6519 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6520 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6521 });6522 return nodes.some(function(node) { return getAlignmentValue(node) == "justify" })6523 && nodes.some(function(node) { return getAlignmentValue(node) != "justify" });6524 }, state: function() {6525 6526 6527 6528 6529 6530 if (!getActiveRange()) {6531 return false;6532 }6533 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6534 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6535 });6536 return nodes.length6537 && nodes.every(function(node) { return getAlignmentValue(node) == "justify" });6538 }, value: function() {6539 6540 6541 6542 6543 if (!getActiveRange()) {6544 return "";6545 }6546 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6547 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6548 });6549 if (nodes.length) {6550 return getAlignmentValue(nodes[0]);6551 } else {6552 return "left";6553 }6554 },6555};6556commands.justifyleft = {6557 preservesOverrides: true,6558 6559 action: function() { justifySelection("left"); return true },6560 indeterm: function() {6561 6562 6563 6564 6565 6566 if (!getActiveRange()) {6567 return false;6568 }6569 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6570 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6571 });6572 return nodes.some(function(node) { return getAlignmentValue(node) == "left" })6573 && nodes.some(function(node) { return getAlignmentValue(node) != "left" });6574 }, state: function() {6575 6576 6577 6578 6579 6580 if (!getActiveRange()) {6581 return false;6582 }6583 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6584 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6585 });6586 return nodes.length6587 && nodes.every(function(node) { return getAlignmentValue(node) == "left" });6588 }, value: function() {6589 6590 6591 6592 6593 if (!getActiveRange()) {6594 return "";6595 }6596 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6597 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6598 });6599 if (nodes.length) {6600 return getAlignmentValue(nodes[0]);6601 } else {6602 return "left";6603 }6604 },6605};6606commands.justifyright = {6607 preservesOverrides: true,6608 6609 action: function() { justifySelection("right"); return true },6610 indeterm: function() {6611 6612 6613 6614 6615 6616 if (!getActiveRange()) {6617 return false;6618 }6619 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6620 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6621 });6622 return nodes.some(function(node) { return getAlignmentValue(node) == "right" })6623 && nodes.some(function(node) { return getAlignmentValue(node) != "right" });6624 }, state: function() {6625 6626 6627 6628 6629 6630 if (!getActiveRange()) {6631 return false;6632 }6633 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6634 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6635 });6636 return nodes.length6637 && nodes.every(function(node) { return getAlignmentValue(node) == "right" });6638 }, value: function() {6639 6640 6641 6642 6643 if (!getActiveRange()) {6644 return "";6645 }6646 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function(node) {6647 return isEditable(node) && isVisible(node) && !node.hasChildNodes();6648 });6649 if (nodes.length) {6650 return getAlignmentValue(nodes[0]);6651 } else {6652 return "left";6653 }6654 },6655};6656commands.outdent = {6657 preservesOverrides: true,6658 action: function() {6659 6660 ...
d549ba877ef2abdd028ad56f175ed0eccb0425c1_1_46.js
Source:d549ba877ef2abdd028ad56f175ed0eccb0425c1_1_46.js
...3 var newRange = blockExtend(getActiveRange());4 // "Let node be the first visible editable node that is contained in5 // new range and has no children. If there is no such node, return the6 // empty string."7 var nodes = getAllContainedNodes(newRange, function(node) {8 return isVisible(node)9 && isEditable(node)10 && !node.hasChildNodes();11 });12 if (!nodes.length) {13 return "";14 }15 var node = nodes[0];16 // "While node's parent is editable and in the same editing host as17 // node, and node is not an HTML element whose local name is a18 // formattable block name, set node to its parent."19 while (isEditable(node.parentNode)20 && inSameEditingHost(node, node.parentNode)21 && !isHtmlElement(node, formattableBlockNames)) {...
f822bc1960c4e2f4c779dc8bffed556320f0dca2_0_3.js
Source:f822bc1960c4e2f4c779dc8bffed556320f0dca2_0_3.js
...3 var newRange = blockExtend(range);4 // "Let node be the first visible editable node that is contained in5 // new range and has no children. If there is no such node, return the6 // empty string."7 var nodes = getAllContainedNodes(newRange, function(node) {8 return isVisible(node)9 && isEditable(node)10 && !node.hasChildNodes();11 });12 if (!nodes.length) {13 return "";14 }15 var node = nodes[0];16 // "While node's parent is editable and in the same editing host as17 // node, and node is not an HTML element whose local name is a18 // formattable block name, set node to its parent."19 while (isEditable(node.parentNode)20 && inSameEditingHost(node, node.parentNode)21 && !isHtmlElement(node, formattableBlockNames)) {...
Using AI Code Generation
1var wptree = require('wptree');2var tree = new wptree();3var node = tree.createNode('root');4var child1 = node.createChild('child1');5var child2 = node.createChild('child2');6var child3 = node.createChild('child3');7var child4 = node.createChild('child4');8var child5 = node.createChild('child5');9var child6 = node.createChild('child6');10var child7 = node.createChild('child7');11var child8 = node.createChild('child8');12var child9 = node.createChild('child9');13var child10 = node.createChild('child10');14var child11 = node.createChild('child11');15var child12 = node.createChild('child12');16var child13 = node.createChild('child13');17var child14 = node.createChild('child14');18var child15 = node.createChild('child15');19var child16 = node.createChild('child16');20var child17 = node.createChild('child17');21var child18 = node.createChild('child18');22var child19 = node.createChild('child19');23var child20 = node.createChild('child20');24var child21 = node.createChild('child21');25var child22 = node.createChild('child22');26var child23 = node.createChild('child23');27var child24 = node.createChild('child24');28var child25 = node.createChild('child25');29var child26 = node.createChild('child26');30var child27 = node.createChild('child27');31var child28 = node.createChild('child28');32var child29 = node.createChild('child29');33var child30 = node.createChild('child30');34var child31 = node.createChild('child31');35var child32 = node.createChild('child32');36var child33 = node.createChild('child33');37var child34 = node.createChild('child34');38var child35 = node.createChild('child35');39var child36 = node.createChild('child36');40var child37 = node.createChild('child37');41var child38 = node.createChild('child38');42var child39 = node.createChild('child39');43var child40 = node.createChild('child40');44var child41 = node.createChild('child41');45var child42 = node.createChild('child42');
Using AI Code Generation
1var tree = new WebProtege.Tree();2var nodes = tree.getAllContainedNodes();3for (var i = 0; i < nodes.length; i++) {4 console.log(nodes[i].getBrowserText());5}6var tree = new WebProtege.Tree();7var root = tree.getRoot();8console.log(root.getBrowserText());9var tree = new WebProtege.Tree();10var node = tree.getSelected();11console.log(node.getBrowserText());12var tree = new WebProtege.Tree();13var node = tree.getSelected();14var parent = node.getParent();15console.log(parent.getBrowserText());
Using AI Code Generation
1var wptree = require('./wptree');2var tree = new wptree.WPTree();3var node1 = tree.addNode(null, "1");4var node2 = tree.addNode(node1, "2");5var node3 = tree.addNode(node1, "3");6var node4 = tree.addNode(node1, "4");7var node5 = tree.addNode(node2, "5");8var node6 = tree.addNode(node2, "6");9var node7 = tree.addNode(node5, "7");10var node8 = tree.addNode(node5, "8");11var node9 = tree.addNode(node5, "9");12var node10 = tree.addNode(node3, "10");13var node11 = tree.addNode(node3, "11");14var node12 = tree.addNode(node3, "12");15var node13 = tree.addNode(node3, "13");16var node14 = tree.addNode(node3, "14");17var node15 = tree.addNode(node4, "15");18var node16 = tree.addNode(node4, "16");19var node17 = tree.addNode(node4, "17");20var node18 = tree.addNode(node4, "18");21var node19 = tree.addNode(node4, "19");22var node20 = tree.addNode(node4, "20");23var node21 = tree.addNode(node4, "21");24var node22 = tree.addNode(node4, "22");25var node23 = tree.addNode(node4, "23");26var node24 = tree.addNode(node4, "24");27var node25 = tree.addNode(node4, "25");28var node26 = tree.addNode(node4, "26");29var node27 = tree.addNode(node4, "27");30var node28 = tree.addNode(node4, "28");31var node29 = tree.addNode(node4, "29");32var node30 = tree.addNode(node4, "30");33var node31 = tree.addNode(node4, "31");34var node32 = tree.addNode(node4, "32");35var node33 = tree.addNode(node4, "33");36var node34 = tree.addNode(node4, "34");37var node35 = tree.addNode(node4, "35");38var node36 = tree.addNode(node4, "36");39var node37 = tree.addNode(node4, "37");40var node38 = tree.addNode(node4, "38");41var node39 = tree.addNode(node4, "39");42var node40 = tree.addNode(node4
Using AI Code Generation
1var wptree = require('./wptree.js');2var tree = new wptree();3var root = tree.createNode(1);4var node1 = tree.createNode(1, root);5var node2 = tree.createNode(1, root);6var node3 = tree.createNode(1, node1);7var node4 = tree.createNode(1, node1);8var node5 = tree.createNode(1, node2);9var node6 = tree.createNode(1, node2);10var node7 = tree.createNode(1, node3);11var node8 = tree.createNode(1, node3);12var node9 = tree.createNode(1, node4);13var node10 = tree.createNode(1, node4);14var node11 = tree.createNode(1, node5);15var node12 = tree.createNode(1, node5);16var node13 = tree.createNode(1, node6);17var node14 = tree.createNode(1, node6);18var node15 = tree.createNode(1, node7);19var node16 = tree.createNode(1, node7);20var node17 = tree.createNode(1, node8);21var node18 = tree.createNode(1, node8);22var node19 = tree.createNode(1, node9);23var node20 = tree.createNode(1, node9);24var node21 = tree.createNode(1, node10);25var node22 = tree.createNode(1, node10);26var node23 = tree.createNode(1, node11);27var node24 = tree.createNode(1, node11);28var node25 = tree.createNode(1, node12);29var node26 = tree.createNode(1, node12);30var node27 = tree.createNode(1, node13);31var node28 = tree.createNode(1, node13);32var node29 = tree.createNode(1, node14);33var node30 = tree.createNode(1, node14);34var node31 = tree.createNode(1, node15);35var node32 = tree.createNode(1, node15);36var node33 = tree.createNode(1, node16);37var node34 = tree.createNode(1, node16);38var node35 = tree.createNode(1, node17);39var node36 = tree.createNode(1, node17);
Using AI Code Generation
1var fs = require('fs');2var wptree = require('./wptree');3var wptree = new wptree();4var json = fs.readFileSync('test.json');5var data = JSON.parse(json);6wptree.loadTree(data);7var nodes = wptree.getAllContainedNodes(1);8console.log(nodes);9{10 {11 {12 {13 {14 {
Using AI Code Generation
1var wptree = require('./wptree.js');2var tree = new wptree();3tree.init();4var node1 = tree.addRootNode('node1');5var node2 = tree.addChildNode('node2', node1);6var node3 = tree.addChildNode('node3', node2);7var node4 = tree.addChildNode('node4', node3);8var node5 = tree.addChildNode('node5', node4);9var node6 = tree.addChildNode('node6', node5);10var node7 = tree.addChildNode('node7', node6);11var node8 = tree.addChildNode('node8', node7);12var node9 = tree.addChildNode('node9', node8);13var node10 = tree.addChildNode('node10', node9);14var node11 = tree.addChildNode('node11', node10);15var node12 = tree.addChildNode('node12', node11);16var node13 = tree.addChildNode('node13', node12);17var node14 = tree.addChildNode('node14', node13);18var node15 = tree.addChildNode('node15', node14);19var node16 = tree.addChildNode('node16', node15);20var node17 = tree.addChildNode('node17', node16);21var node18 = tree.addChildNode('node18', node17);22var node19 = tree.addChildNode('node19', node18);23var node20 = tree.addChildNode('node20', node19);24var node21 = tree.addChildNode('node21', node20);25var node22 = tree.addChildNode('node22', node21);26var node23 = tree.addChildNode('node23', node22);27var node24 = tree.addChildNode('node24', node23);28var node25 = tree.addChildNode('node25', node24);29var node26 = tree.addChildNode('node26', node25);30var node27 = tree.addChildNode('node27', node26);31var node28 = tree.addChildNode('node28', node27);32var node29 = tree.addChildNode('node29', node28);33var node30 = tree.addChildNode('node30', node29);34var node31 = tree.addChildNode('node31', node30);35var node32 = tree.addChildNode('node32', node31);36var node33 = tree.addChildNode('node33', node32);37var node34 = tree.addChildNode('node34', node33
Using AI Code Generation
1var wptree = require('wptree');2var tree = new wptree();3var nodes = tree.getAllContainedNodes();4console.log("Nodes: "+nodes);5var wptree = module.exports = function() {6 this.getAllContainedNodes = function() {7 return "All contained nodes";8 }9}10var select = $('#mySelect');11select.val('1');12var obj = new Object();13obj.name = "John";14obj.age = 23;15var obj = {name: "John", age: 23};16var select = $('#mySelect');17select.val('1');18var select = $('#mySelect');19select.val('1');
Using AI Code Generation
1var wptree = require("./wptree.js");2var fs = require("fs");3var tree = new wptree.WPTree();4var data = fs.readFileSync("sample.json", "utf8");5var json = JSON.parse(data);6tree.populateTree(json);7var nodes = tree.getAllContainedNodes();8console.log(nodes);9var nodes = tree.getAllContainedNodes("1");10console.log(nodes);11var nodes = tree.getAllContainedNodes("2");12console.log(nodes);13var nodes = tree.getAllContainedNodes("3");14console.log(nodes);15var nodes = tree.getAllContainedNodes("4");16console.log(nodes);17var nodes = tree.getAllContainedNodes("5");18console.log(nodes);19var nodes = tree.getAllContainedNodes("6");20console.log(nodes);21var nodes = tree.getAllContainedNodes("7");22console.log(nodes);23var nodes = tree.getAllContainedNodes("8");24console.log(nodes);25var nodes = tree.getAllContainedNodes("9");26console.log(nodes);27var nodes = tree.getAllContainedNodes("10");28console.log(nodes);29var nodes = tree.getAllContainedNodes("11");30console.log(nodes);31var nodes = tree.getAllContainedNodes("12");32console.log(nodes);33var nodes = tree.getAllContainedNodes("13");34console.log(nodes);35var nodes = tree.getAllContainedNodes("14");36console.log(nodes);
Using AI Code Generation
1var tree = new WPTree("tree");2var nodes = tree.getAllContainedNodes(1);3var tree = new WPTree("tree");4var nodes = tree.getContainedNodes(1);5var tree = new WPTree("tree");6var nodes = tree.getContainedNodes(1);7var tree = new WPTree("tree");8var nodes = tree.getContainedNodes(1);9var tree = new WPTree("tree");10var nodes = tree.getContainedNodes(1);11var tree = new WPTree("tree");12var nodes = tree.getContainedNodes(1);13var tree = new WPTree("tree");
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!!