How to use myQueryCommandState method in wpt

Best JavaScript code snippet using wpt

implementation.js

Source:implementation.js Github

copy

Full Screen

...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 ...

Full Screen

Full Screen

d549ba877ef2abdd028ad56f175ed0eccb0425c1_1_38.js

Source:d549ba877ef2abdd028ad56f175ed0eccb0425c1_1_38.js Github

copy

Full Screen

...12 // "If override is a boolean, and queryCommandState(command)13 // returns something different from override, call14 // execCommand(command)."15 if (typeof override == "boolean"16 && myQueryCommandState(command) != override) {17 myExecCommand(command);18 // "Otherwise, if override is a string, and command is not19 // "fontSize", and queryCommandValue(command) returns something not20 // equivalent to override, call execCommand(command, false,21 // override)."22 } else if (typeof override == "string"23 && command != "fontsize"24 && !areEquivalentValues(command, myQueryCommandValue(command), override)) {25 myExecCommand(command, false, override);26 // "Otherwise, if override is a string; and command is "fontSize";27 // and either there is a value override for "fontSize" that is not28 // equal to override, or there is no value override for "fontSize"29 // and node's effective command value for "fontSize" is not loosely30 // equivalent to override: call execCommand("fontSize", false,...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1CKEDITOR.instances.editor1.execCommand( 'myQueryCommandState' );2CKEDITOR.plugins.add( 'wptextpattern', {3 init: function( editor ) {4 editor.addCommand( 'myQueryCommandState', {5 exec: function( editor ) {6 console.log( editor.getCommand( 'wptextpattern' ).state );7 }8 });9 }10});11CKEDITOR.replace( 'editor1', {12 { start: ':)', format: 'smiley' }13});

Full Screen

Using AI Code Generation

copy

Full Screen

1var state = wptbEditor.myQueryCommandState('bold');2console.log(state);3var value = wptbEditor.myQueryCommandValue('fontname');4console.log(value);5var indeterm = wptbEditor.myQueryCommandIndeterm('fontname');6console.log(indeterm);7var supported = wptbEditor.myQueryCommandSupported('fontname');8console.log(supported);9var enabled = wptbEditor.myQueryCommandEnabled('fontname');10console.log(enabled);11wptbEditor.myExecCommand('bold', false, null);12wptbEditor.myExecCommandShowUI('fontname', false, null);13wptbEditor.myExecCommandValueArgument('fontname', false, 'Arial');14wptbEditor.myDocumentExecCommand('bold', false, null);15wptbEditor.myDocumentExecCommandShowUI('fontname', false, null);

Full Screen

Using AI Code Generation

copy

Full Screen

1var state = document.queryCommandState('myQueryCommandState');2var value = document.queryCommandValue('myQueryCommandValue');3var enabled = document.queryCommandEnabled('myQueryCommandEnabled');4var indeterm = document.queryCommandIndeterm('myQueryCommandIndeterm');5var text = document.queryCommandText('myQueryCommandText');6var supported = document.queryCommandSupported('myQueryCommandSupported');7var default = document.queryCommandDefault('myQueryCommandDefault');8var help = document.queryCommandHelp('myQueryCommandHelp');9var state = document.queryCommandState('myQueryCommandState', false, 'myParam');10var value = document.queryCommandValue('myQueryCommandValue', false, 'myParam');11var enabled = document.queryCommandEnabled('myQueryCommandEnabled', false, 'myParam');

Full Screen

Using AI Code Generation

copy

Full Screen

1function myQueryCommandState(cmd) {2 var state = false;3 try {4 state = document.queryCommandState(cmd);5 } catch (e) { }6 return state;7}8function myQueryCommandValue(cmd) {9 var value = "";10 try {11 value = document.queryCommandValue(cmd);12 } catch (e) { }13 return value;14}15function myQueryCommandIndeterm(cmd) {16 var indeterm = false;17 try {18 indeterm = document.queryCommandIndeterm(cmd);19 } catch (e) { }20 return indeterm;21}22function myQueryCommandSupported(cmd) {23 var supported = false;24 try {25 supported = document.queryCommandSupported(cmd);26 } catch (e) { }27 return supported;28}29function myQueryCommandEnabled(cmd) {30 var enabled = false;31 try {32 enabled = document.queryCommandEnabled(cmd);33 } catch (e) { }34 return enabled;35}36function myQueryCommandText(cmd) {37 var text = "";38 try {39 text = document.queryCommandText(cmd);40 } catch (e) { }41 return text;42}43function myQueryCommandLabel(cmd) {44 var label = "";45 try {46 label = document.queryCommandLabel(cmd);47 } catch (e) { }48 return label;49}50function myQueryCommandDefault(cmd) {51 var def = "";52 try {53 def = document.queryCommandDefault(cmd);54 } catch (e) { }55 return def;56}

Full Screen

Using AI Code Generation

copy

Full Screen

1var editor = CKEDITOR.instances.editor1;2var command = editor.getCommand('wptextpattern');3if (command) {4 var state = command.state;5 alert(state);6}7var editor = CKEDITOR.instances.editor1;8var command = editor.getCommand('wptextpattern');9if (command) {10 var value = command.value;11 alert(value);12}13var editor = CKEDITOR.instances.editor1;14var command = editor.getCommand('wptextpattern');15if (command) {16 var enabled = command.enabled;17 alert(enabled);18}19var editor = CKEDITOR.instances.editor1;20var command = editor.getCommand('wptextpattern');21if (command) {22 var state = command.state;23 alert(state);24}25var editor = CKEDITOR.instances.editor1;26var command = editor.getCommand('wptextpattern');27if (command) {28 var value = command.value;29 alert(value);30}31var editor = CKEDITOR.instances.editor1;32var command = editor.getCommand('wptextpattern');33if (command) {34 var enabled = command.enabled;35 alert(enabled);36}37var editor = CKEDITOR.instances.editor1;38var command = editor.getCommand('wptextpattern');39if (command) {40 var state = command.state;41 alert(state);42}43var editor = CKEDITOR.instances.editor1;44var command = editor.getCommand('wptextpattern');45if (command) {46 var value = command.value;47 alert(value);48}49var editor = CKEDITOR.instances.editor1;

Full Screen

Using AI Code Generation

copy

Full Screen

1wptbControls.myQueryCommandState( 'bold' );2wptbControls.myQueryCommandValue( 'bold' );3wptbControls.myQueryCommandSupported( 'bold' );4wptbControls.myQueryCommandEnabled( 'bold' );5wptbControls.myQueryCommandIndeterm( 'bold' );6wptbControls.myQueryCommandText( 'bold' );7wptbControls.myQueryCommandDefaultText( 'bold' );8wptbControls.myQueryCommandDefaultIndeterm( 'bold' );9wptbControls.myQueryCommandValueText( 'bold' );10wptbControls.myQueryCommandStateText( 'bold' );11wptbControls.myQueryCommandSupportedText( 'bold' );

Full Screen

Using AI Code Generation

copy

Full Screen

1var state = new wptbTableState();2var result = state.myQueryCommandState('bold');3console.log(result);4var wptbTableState = function() {5 this.myQueryCommandState = function(command) {6 var result = document.queryCommandState(command);7 return result;8 }9}

Full Screen

Using AI Code Generation

copy

Full Screen

1var tableState = new wptbTableState();2var tableStateResult = tableState.myQueryCommandState( 'bold' );3wptbTableState.prototype.myQueryCommandState = function( commandName ) {4 var table = document.querySelector( '.wptb-preview-table' );5 var selection = document.getSelection();6 var selectionRange = selection.getRangeAt( 0 );7 var selectionText = selectionRange.toString();8 var selectionTextLength = selectionText.length;9 var selectionTextNodes = selectionRange.cloneContents().childNodes;10 var selectionTextNodesLength = selectionTextNodes.length;11 var selectedTextNodes = [];12 if( selectionTextNodesLength > 0 ) {13 for( var i = 0; i < selectionTextNodesLength; i++ ) {14 if( selectionTextNodes[i].nodeName == '#text' && selectionTextNodes[i].textContent.trim() != '' ) {15 selectedTextNodes.push( selectionTextNodes[i] );16 }17 }18 }19 if( table && selectionTextLength > 0 && selectedTextNodes.length > 0 ) {20 var tableStateResult = table.querySelector( '.wptb-table-state' );21 if( tableStateResult ) {22 tableStateResult.remove();23 }24 table.insertAdjacentHTML( 'beforeend', '<div class="wptb-table-state"></div>' );25 tableStateResult = table.querySelector( '.wptb-table-state' );26 var selectedTextNodesLength = selectedTextNodes.length;27 for( var i = 0; i < selectedTextNodesLength; i++ ) {28 var selectedTextNode = selectedTextNodes[i];29 var selectedTextNodeParent = selectedTextNode.parentElement;30 var selectedTextNodeParentStyle = window.getComputedStyle( selectedTextNodeParent );31 var selectedTextNodeParentTagName = selectedTextNodeParent.tagName.toLowerCase();32 var selectedTextNodeParentTagNameClass = '';33 if( selectedTextNodeParentTagName ) {34 selectedTextNodeParentTagNameClass = 'wptb-table-state-' + selectedTextNodeParentTagName;35 }36 var selectedTextNodeParentStyleDisplay = selectedTextNodeParentStyle.display;37 var selectedTextNodeParentStyleTextAlign = selectedTextNodeParentStyle.textAlign;38 var selectedTextNodeParentStyleVerticalAlign = selectedTextNodeParentStyle.verticalAlign;

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptbTableState = new wptbElementTable();2wptbTableState.myQueryCommandState();3var wptbTableState = new wptbElementTable();4wptbElementTable.prototype.myQueryCommandState = function () {5 console.log('myQueryCommandState');6}

Full Screen

Using AI Code Generation

copy

Full Screen

1var state = window.external.myQueryCommandState("bold");2if (state == 1)3{4alert("bold is enabled");5}6{7alert("bold is disabled");8}9var myQueryCommandState = function(command)10{11var state = 0;12var commandId = -1;13switch(command)14{15commandId = 1;16break;17commandId = 2;18break;19commandId = 3;20break;21commandId = 4;22break;23commandId = 5;24break;25commandId = 6;26break;27commandId = 7;28break;29commandId = 8;30break;31commandId = 9;32break;33commandId = 10;34break;35commandId = 11;36break;37commandId = 12;38break;39commandId = 13;40break;41commandId = 14;42break;43commandId = 15;44break;45commandId = 16;46break;47commandId = 17;48break;49commandId = 18;50break;51commandId = 19;52break;53commandId = 20;54break;55commandId = 21;56break;57commandId = 22;58break;59commandId = 23;60break;61commandId = 24;62break;63commandId = 25;64break;65commandId = 26;66break;67commandId = 27;68break;69commandId = 28;70break;71commandId = 29;72break;73commandId = 30;74break;75commandId = 31;76break;

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful