1 define(['aloha/core', 'aloha/ecma5shims', 'util/maps', 'jquery'], function (Aloha, $_, Maps, jQuery) { 2 "use strict"; 3 4 function hasAttribute(obj, attr) { 5 var native_method = obj.hasAttribute; 6 if (native_method) { 7 return obj.hasAttribute(attr); 8 } 9 return (typeof obj.attributes[attr] != "undefined"); 10 } 11 12 var htmlNamespace = "http://www.w3.org/1999/xhtml"; 13 14 var cssStylingFlag = false; 15 16 // This is bad :( 17 var globalRange = null; 18 19 // Commands are stored in a dictionary where we call their actions and such 20 var commands = {}; 21 22 /////////////////////////////////////////////////////////////////////////////// 23 ////////////////////////////// Utility functions ////////////////////////////// 24 /////////////////////////////////////////////////////////////////////////////// 25 //@{ 26 27 // Opera 11 puts HTML elements in the null namespace, it seems. 28 function isHtmlNamespace(ns) { 29 return ns === null || !ns || ns === htmlNamespace; 30 } 31 32 // "An HTML element is an Element whose namespace is the HTML namespace." 33 // 34 // I allow an extra argument to more easily check whether something is a 35 // particular HTML element, like isNamedHtmlElement(node, 'OL'). It accepts arrays 36 // too, like isHtmlElementInArray(node, ["OL", "UL"]) to check if it's an ol or ul. 37 // TODO This function was prominent during profiling. Remove it 38 // and replace with calls to isAnyHtmlElement, isNamedHtmlElement 39 // and is isMappedHtmlElement. 40 function isHtmlElement_obsolete(node, tags) { 41 if (typeof tags == "string") { 42 tags = [tags]; 43 } 44 if (typeof tags == "object") { 45 tags = $_(tags).map(function (tag) { 46 return tag.toUpperCase(); 47 }); 48 } 49 return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI) && (typeof tags == "undefined" || $_(tags).indexOf(node.tagName) != -1); 50 } 51 52 function isAnyHtmlElement(node) { 53 return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI); 54 } 55 56 // name should be uppercase 57 function isNamedHtmlElement(node, name) { 58 return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI) 59 // This function is passed in a mix of upper and lower case names 60 && name.toUpperCase() === node.nodeName; 61 } 62 63 // TODO remove when isHtmlElementInArray is removed 64 function arrayContainsInsensitive(array, str) { 65 var i, len; 66 str = str.toUpperCase(); 67 for (i = 0, len = array.length; i < len; i++) { 68 if (array[i].toUpperCase() === str) { 69 return true; 70 } 71 } 72 return false; 73 } 74 // TODO replace calls to this function with calls to isMappedHtmlElement() 75 function isHtmlElementInArray(node, array) { 76 return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI) 77 // This function is passed in a mix of upper and lower case names 78 && arrayContainsInsensitive(array, node.nodeName); 79 } 80 81 // map must have all-uppercase keys 82 function isMappedHtmlElement(node, map) { 83 return node && node.nodeType == 1 && isHtmlNamespace(node.namespaceURI) && map[node.nodeName]; 84 } 85 86 /** 87 * Method to count the number of styles in the given style 88 */ 89 function getStyleLength(node) { 90 var s; 91 var styleLength = 0; 92 93 if (!node) { 94 return 0; 95 } 96 97 if (!node.style) { 98 return 0; 99 } 100 101 // some browsers support .length on styles 102 if (typeof node.style.length !== 'undefined') { 103 return node.style.length; 104 } 105 106 /*jslint forin: true*/ //not sure whether node.style.hasOwnProperty is valid 107 for (s in node.style) { 108 if (node.style[s] && node.style[s] !== 0 && node.style[s] !== 'false') { 109 styleLength++; 110 } 111 } 112 /*jslint forin: false*/ 113 114 return styleLength; 115 } 116 117 function toArray(obj) { 118 if (!obj) { 119 return null; 120 } 121 var array = [], 122 i, 123 l = obj.length; 124 // iterate backwards ensuring that length is an UInt32 125 i = l >>> 0; 126 while (i--) { 127 array[i] = obj[i]; 128 } 129 return array; 130 } 131 132 function nextNodeDescendants(node) { 133 while (node && !node.nextSibling) { 134 node = node.parentNode; 135 } 136 if (!node) { 137 return null; 138 } 139 return node.nextSibling; 140 } 141 142 function nextNode(node) { 143 if (node.hasChildNodes()) { 144 return node.firstChild; 145 } 146 return nextNodeDescendants(node); 147 } 148 149 function previousNode(node) { 150 if (node.previousSibling) { 151 node = node.previousSibling; 152 while (node.hasChildNodes()) { 153 node = node.lastChild; 154 } 155 return node; 156 } 157 if (node.parentNode && node.parentNode.nodeType == $_.Node.ELEMENT_NODE) { 158 return node.parentNode; 159 } 160 return null; 161 } 162 163 /** 164 * Returns true if ancestor is an ancestor of descendant, false otherwise. 165 */ 166 function isAncestor(ancestor, descendant) { 167 return ancestor && descendant && Boolean($_.compareDocumentPosition(ancestor, descendant) & $_.Node.DOCUMENT_POSITION_CONTAINED_BY); 168 } 169 170 /** 171 * Returns true if ancestor is an ancestor of or equal to descendant, false 172 * otherwise. 173 */ 174 function isAncestorContainer(ancestor, descendant) { 175 return (ancestor || descendant) && (ancestor == descendant || isAncestor(ancestor, descendant)); 176 } 177 178 /** 179 * Returns true if descendant is a descendant of ancestor, false otherwise. 180 */ 181 function isDescendant(descendant, ancestor) { 182 return ancestor && descendant && Boolean($_.compareDocumentPosition(ancestor, descendant) & $_.Node.DOCUMENT_POSITION_CONTAINED_BY); 183 } 184 185 /** 186 * Returns true if node1 is before node2 in tree order, false otherwise. 187 */ 188 function isBefore(node1, node2) { 189 return Boolean($_.compareDocumentPosition(node1, node2) & $_.Node.DOCUMENT_POSITION_FOLLOWING); 190 } 191 192 /** 193 * Returns true if node1 is after node2 in tree order, false otherwise. 194 */ 195 function isAfter(node1, node2) { 196 return Boolean($_.compareDocumentPosition(node1, node2) & $_.Node.DOCUMENT_POSITION_PRECEDING); 197 } 198 199 function getAncestors(node) { 200 var ancestors = []; 201 while (node.parentNode) { 202 ancestors.unshift(node.parentNode); 203 node = node.parentNode; 204 } 205 return ancestors; 206 } 207 208 function getDescendants(node) { 209 var descendants = []; 210 var stop = nextNodeDescendants(node); 211 while (null != (node = nextNode(node)) && node != stop) { 212 descendants.push(node); 213 } 214 return descendants; 215 } 216 217 function convertProperty(property) { 218 // Special-case for now 219 var map = { 220 "fontFamily": "font-family", 221 "fontSize": "font-size", 222 "fontStyle": "font-style", 223 "fontWeight": "font-weight", 224 "textDecoration": "text-decoration" 225 }; 226 if (typeof map[property] != "undefined") { 227 return map[property]; 228 } 229 230 return property; 231 } 232 233 // Return the <font size=X> value for the given CSS size, or undefined if there 234 // is none. 235 function cssSizeToLegacy(cssVal) { 236 return { 237 "xx-small": 1, 238 "small": 2, 239 "medium": 3, 240 "large": 4, 241 "x-large": 5, 242 "xx-large": 6, 243 "xxx-large": 7 244 }[cssVal]; 245 } 246 247 // Return the CSS size given a legacy size. 248 function legacySizeToCss(legacyVal) { 249 return { 250 1: "xx-small", 251 2: "small", 252 3: "medium", 253 4: "large", 254 5: "x-large", 255 6: "xx-large", 256 7: "xxx-large" 257 }[legacyVal]; 258 } 259 260 // "the directionality" from HTML. I don't bother caring about non-HTML 261 // elements. 262 // 263 // "The directionality of an element is either 'ltr' or 'rtl', and is 264 // determined as per the first appropriate set of steps from the following 265 // list:" 266 function getDirectionality(element) { 267 // "If the element's dir attribute is in the ltr state 268 // The directionality of the element is 'ltr'." 269 if (element.dir == "ltr") { 270 return "ltr"; 271 } 272 273 // "If the element's dir attribute is in the rtl state 274 // The directionality of the element is 'rtl'." 275 if (element.dir == "rtl") { 276 return "rtl"; 277 } 278 279 // "If the element's dir attribute is in the auto state 280 // "If the element is a bdi element and the dir attribute is not in a 281 // defined state (i.e. it is not present or has an invalid value) 282 // [lots of complicated stuff] 283 // 284 // Skip this, since no browser implements it anyway. 285 286 // "If the element is a root element and the dir attribute is not in a 287 // defined state (i.e. it is not present or has an invalid value) 288 // The directionality of the element is 'ltr'." 289 if (!isAnyHtmlElement(element.parentNode)) { 290 return "ltr"; 291 } 292 293 // "If the element has a parent element and the dir attribute is not in a 294 // defined state (i.e. it is not present or has an invalid value) 295 // The directionality of the element is the same as the element's 296 // parent element's directionality." 297 return getDirectionality(element.parentNode); 298 } 299 300 //@} 301 302 /////////////////////////////////////////////////////////////////////////////// 303 ///////////////////////////// DOM Range functions ///////////////////////////// 304 /////////////////////////////////////////////////////////////////////////////// 305 //@{ 306 307 function getNodeIndex(node) { 308 var ret = 0; 309 while (node.previousSibling) { 310 ret++; 311 node = node.previousSibling; 312 } 313 return ret; 314 } 315 316 // "The length of a Node node is the following, depending on node: 317 // 318 // ProcessingInstruction 319 // DocumentType 320 // Always 0. 321 // Text 322 // Comment 323 // node's length. 324 // Any other node 325 // node's childNodes's length." 326 function getNodeLength(node) { 327 switch (node.nodeType) { 328 case $_.Node.PROCESSING_INSTRUCTION_NODE: 329 case $_.Node.DOCUMENT_TYPE_NODE: 330 return 0; 331 332 case $_.Node.TEXT_NODE: 333 case $_.Node.COMMENT_NODE: 334 return node.length; 335 336 default: 337 return node.childNodes.length; 338 } 339 } 340 341 /** 342 * The position of two boundary points relative to one another, as defined by 343 * DOM Range. 344 */ 345 function getPosition(nodeA, offsetA, nodeB, offsetB) { 346 // "If node A is the same as node B, return equal if offset A equals offset 347 // B, before if offset A is less than offset B, and after if offset A is 348 // greater than offset B." 349 if (nodeA == nodeB) { 350 if (offsetA == offsetB) { 351 return "equal"; 352 } 353 if (offsetA < offsetB) { 354 return "before"; 355 } 356 if (offsetA > offsetB) { 357 return "after"; 358 } 359 } 360 361 var documentPosition = $_.compareDocumentPosition(nodeB, nodeA); 362 // "If node A is after node B in tree order, compute the position of (node 363 // B, offset B) relative to (node A, offset A). If it is before, return 364 // after. If it is after, return before." 365 if (documentPosition & $_.Node.DOCUMENT_POSITION_FOLLOWING) { 366 var pos = getPosition(nodeB, offsetB, nodeA, offsetA); 367 if (pos == "before") { 368 return "after"; 369 } 370 if (pos == "after") { 371 return "before"; 372 } 373 } 374 375 // "If node A is an ancestor of node B:" 376 if (documentPosition & $_.Node.DOCUMENT_POSITION_CONTAINS) { 377 // "Let child equal node B." 378 var child = nodeB; 379 380 // "While child is not a child of node A, set child to its parent." 381 while (child.parentNode != nodeA) { 382 child = child.parentNode; 383 } 384 385 // "If the index of child is less than offset A, return after." 386 if (getNodeIndex(child) < offsetA) { 387 return "after"; 388 } 389 } 390 391 // "Return before." 392 return "before"; 393 } 394 395 /** 396 * Returns the furthest ancestor of a Node as defined by DOM Range. 397 */ 398 function getFurthestAncestor(node) { 399 var root = node; 400 while (root.parentNode != null) { 401 root = root.parentNode; 402 } 403 return root; 404 } 405 406 /** 407 * "contained" as defined by DOM Range: "A Node node is contained in a range 408 * range if node's furthest ancestor is the same as range's root, and (node, 0) 409 * is after range's start, and (node, length of node) is before range's end." 410 */ 411 function isContained(node, range) { 412 var pos1 = getPosition(node, 0, range.startContainer, range.startOffset); 413 if (pos1 !== "after") { 414 return false; 415 } 416 var pos2 = getPosition(node, getNodeLength(node), range.endContainer, range.endOffset); 417 if (pos2 !== "before") { 418 return false; 419 } 420 return getFurthestAncestor(node) == getFurthestAncestor(range.startContainer); 421 } 422 423 /** 424 * Return all nodes contained in range that the provided function returns true 425 * for, omitting any with an ancestor already being returned. 426 */ 427 function getContainedNodes(range, condition) { 428 if (typeof condition == "undefined") { 429 condition = function () { 430 return true; 431 }; 432 } 433 var node = range.startContainer; 434 if (node.hasChildNodes() && range.startOffset < node.childNodes.length) { 435 // A child is contained 436 node = node.childNodes[range.startOffset]; 437 } else if (range.startOffset == getNodeLength(node)) { 438 // No descendant can be contained 439 node = nextNodeDescendants(node); 440 } else { 441 // No children; this node at least can't be contained 442 node = nextNode(node); 443 } 444 445 var stop = range.endContainer; 446 if (stop.hasChildNodes() && range.endOffset < stop.childNodes.length) { 447 // The node after the last contained node is a child 448 stop = stop.childNodes[range.endOffset]; 449 } else { 450 // This node and/or some of its children might be contained 451 stop = nextNodeDescendants(stop); 452 } 453 454 var nodeList = []; 455 while (isBefore(node, stop)) { 456 if (isContained(node, range) && condition(node)) { 457 nodeList.push(node); 458 node = nextNodeDescendants(node); 459 continue; 460 } 461 node = nextNode(node); 462 } 463 return nodeList; 464 } 465 466 /** 467 * As above, but includes nodes with an ancestor that's already been returned. 468 */ 469 function getAllContainedNodes(range, condition) { 470 if (typeof condition == "undefined") { 471 condition = function () { 472 return true; 473 }; 474 } 475 var node = range.startContainer; 476 if (node.hasChildNodes() && range.startOffset < node.childNodes.length) { 477 // A child is contained 478 node = node.childNodes[range.startOffset]; 479 } else if (range.startOffset == getNodeLength(node)) { 480 // No descendant can be contained 481 node = nextNodeDescendants(node); 482 } else { 483 // No children; this node at least can't be contained 484 node = nextNode(node); 485 } 486 487 var stop = range.endContainer; 488 if (stop.hasChildNodes() && range.endOffset < stop.childNodes.length) { 489 // The node after the last contained node is a child 490 stop = stop.childNodes[range.endOffset]; 491 } else { 492 // This node and/or some of its children might be contained 493 stop = nextNodeDescendants(stop); 494 } 495 496 var nodeList = []; 497 while (isBefore(node, stop)) { 498 if (isContained(node, range) && condition(node)) { 499 nodeList.push(node); 500 } 501 node = nextNode(node); 502 } 503 return nodeList; 504 } 505 506 // Returns either null, or something of the form rgb(x, y, z), or something of 507 // the form rgb(x, y, z, w) with w != 0. 508 function normalizeColor(color) { 509 if (color.toLowerCase() == "currentcolor") { 510 return null; 511 } 512 513 var outerSpan = document.createElement("span"); 514 document.body.appendChild(outerSpan); 515 outerSpan.style.color = "black"; 516 517 var innerSpan = document.createElement("span"); 518 outerSpan.appendChild(innerSpan); 519 innerSpan.style.color = color; 520 color = $_.getComputedStyle(innerSpan).color; 521 522 if (color == "rgb(0, 0, 0)") { 523 // Maybe it's really black, maybe it's invalid. 524 outerSpan.color = "white"; 525 color = $_.getComputedStyle(innerSpan).color; 526 if (color != "rgb(0, 0, 0)") { 527 return null; 528 } 529 } 530 531 document.body.removeChild(outerSpan); 532 533 // I rely on the fact that browsers generally provide consistent syntax for 534 // getComputedStyle(), although it's not standardized. There are only two 535 // exceptions I found: 536 if (/^rgba\([0-9]+, [0-9]+, [0-9]+, 1\)$/.test(color)) { 537 // IE10PP2 seems to do this sometimes. 538 return color.replace("rgba", "rgb").replace(", 1)", ")"); 539 } 540 if (color == "transparent") { 541 // IE10PP2, Firefox 7.0a2, and Opera 11.50 all return "transparent" if 542 // the specified value is "transparent". 543 return "rgba(0, 0, 0, 0)"; 544 } 545 return color; 546 } 547 548 // Returns either null, or something of the form #xxxxxx, or the color itself 549 // if it's a valid keyword. 550 function parseSimpleColor(color) { 551 color = color.toLowerCase(); 552 if ($_(["aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige", "bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown", "burlywood", "cadetblue", "chartreuse", "chocolate", "coral", "cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan", "darkgoldenrod", "darkgray", "darkgreen", "darkgrey", "darkkhaki", "darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred", "darksalmon", "darkseagreen", "darkslateblue", "darkslategray", "darkslategrey", "darkturquoise", "darkviolet", "deeppink", "deepskyblue", "dimgray", "dimgrey", "dodgerblue", "firebrick", "floralwhite", "forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod", "gray", "green", "greenyellow", "grey", "honeydew", "hotpink", "indianred", "indigo", "ivory", "khaki", "lavender", "lavenderblush", "lawngreen", "lemonchiffon", "lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow", "lightgray", "lightgreen", "lightgrey", "lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray", "lightslategrey", "lightsteelblue", "lightyellow", "lime", "limegreen", "linen", "magenta", "maroon", "mediumaquamarine", "mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", "mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin", "navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange", "orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise", "palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum", "powderblue", "purple", "red", "rosybrown", "royalblue", "saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna", "silver", "skyblue", "slateblue", "slategray", "slategrey", "snow", "springgreen", "steelblue", "tan", "teal", "thistle", "tomato", "turquoise", "violet", "wheat", "white", "whitesmoke", "yellow", "yellowgreen"]).indexOf(color) != -1) { 553 return color; 554 } 555 556 color = normalizeColor(color); 557 var matches = /^rgb\(([0-9]+), ([0-9]+), ([0-9]+)\)$/.exec(color); 558 if (matches) { 559 return "#" + parseInt(matches[1], 10).toString(16).replace(/^.$/, "0$&") + parseInt(matches[2], 10).toString(16).replace(/^.$/, "0$&") + parseInt(matches[3], 10).toString(16).replace(/^.$/, "0$&"); 560 } else if (/^#[abcdef0123456789]+$/i.exec(color)) { 561 // return hexadecimal color values (as returned by IE 7/8) 562 return color; 563 } 564 return null; 565 } 566 567 //@} 568 569 ////////////////////////////////////////////////////////////////////////////// 570 /////////////////////////// Edit command functions /////////////////////////// 571 ////////////////////////////////////////////////////////////////////////////// 572 573 ///////////////////////////////////////////////// 574 ///// Methods of the HTMLDocument interface ///// 575 ///////////////////////////////////////////////// 576 //@{ 577 578 var getStateOverride, 579 setStateOverride, 580 resetOverrides, 581 unsetStateOverride, 582 getValueOverride, 583 setValueOverride, 584 unsetValueOverride; 585 586 var executionStackDepth = 0; 587 588 // Helper function for fontSize's action plus queryOutputHelper. It's just the 589 // middle of fontSize's action, ripped out into its own function. 590 function normalizeFontSize(value) { 591 // "Strip leading and trailing whitespace from value." 592 // 593 // Cheap hack, not following the actual algorithm. 594 value = $_(value).trim(); 595 596 // "If value is a valid floating point number, or would be a valid 597 // floating point number if a single leading "+" character were 598 // stripped:" 599 if (/^[\-+]?[0-9]+(\.[0-9]+)?([eE][\-+]?[0-9]+)?$/.test(value)) { 600 var mode; 601 602 // "If the first character of value is "+", delete the character 603 // and let mode be "relative-plus"." 604 if (value[0] == "+") { 605 value = value.slice(1); 606 mode = "relative-plus"; 607 // "Otherwise, if the first character of value is "-", delete the 608 // character and let mode be "relative-minus"." 609 } else if (value[0] == "-") { 610 value = value.slice(1); 611 mode = "relative-minus"; 612 // "Otherwise, let mode be "absolute"." 613 } else { 614 mode = "absolute"; 615 } 616 617 // "Apply the rules for parsing non-negative integers to value, and 618 // let number be the result." 619 // 620 // Another cheap hack. 621 var num = parseInt(value, 10); 622 623 // "If mode is "relative-plus", add three to number." 624 if (mode == "relative-plus") { 625 num += 3; 626 } 627 628 // "If mode is "relative-minus", negate number, then add three to 629 // it." 630 if (mode == "relative-minus") { 631 num = 3 - num; 632 } 633 634 // "If number is less than one, let number equal 1." 635 636 if (num < 1) { 637 num = 1; 638 } 639 640 // "If number is greater than seven, let number equal 7." 641 if (num > 7) { 642 num = 7; 643 } 644 645 // "Set value to the string here corresponding to number:" [table 646 // omitted] 647 value = { 648 1: "xx-small", 649 2: "small", 650 3: "medium", 651 4: "large", 652 5: "x-large", 653 6: "xx-large", 654 7: "xxx-large" 655 }[num]; 656 } 657 658 return value; 659 } 660 661 function getLegacyFontSize(size) { 662 // For convenience in other places in my code, I handle all sizes, not just 663 // pixel sizes as the spec says. This means pixel sizes have to be passed 664 // in suffixed with "px", not as plain numbers. 665 size = normalizeFontSize(size); 666 667 if (jQuery.inArray(size, ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"]) == -1 && !/^[0-9]+(\.[0-9]+)?(cm|mm|in|pt|pc|px)$/.test(size)) { 668 // There is no sensible legacy size for things like "2em". 669 return null; 670 } 671 672 var font = document.createElement("font"); 673 document.body.appendChild(font); 674 if (size == "xxx-large") { 675 font.size = 7; 676 } else { 677 font.style.fontSize = size; 678 } 679 var pixelSize = parseInt($_.getComputedStyle(font).fontSize, 10); 680 document.body.removeChild(font); 681 682 // "Let returned size be 1." 683 var returnedSize = 1; 684 685 // "While returned size is less than 7:" 686 while (returnedSize < 7) { 687 // "Let lower bound be the resolved value of "font-size" in pixels 688 // of a font element whose size attribute is set to returned size." 689 font = document.createElement("font"); 690 font.size = returnedSize; 691 document.body.appendChild(font); 692 var lowerBound = parseInt($_.getComputedStyle(font).fontSize, 10); 693 694 // "Let upper bound be the resolved value of "font-size" in pixels 695 // of a font element whose size attribute is set to one plus 696 // returned size." 697 font.size = 1 + returnedSize; 698 var upperBound = parseInt($_.getComputedStyle(font).fontSize, 10); 699 document.body.removeChild(font); 700 701 // "Let average be the average of upper bound and lower bound." 702 var average = (upperBound + lowerBound) / 2; 703 704 // "If pixel size is less than average, return the one-element 705 // string consisting of the digit returned size." 706 if (pixelSize < average) { 707 return String(returnedSize); 708 } 709 710 // "Add one to returned size." 711 returnedSize++; 712 } 713 714 // "Return "7"." 715 return "7"; 716 } 717 718 // Helper function for common behavior. 719 function editCommandMethod(command, prop, range, callback) { 720 var ret; 721 722 // Set up our global range magic, but only if we're the outermost function 723 if (executionStackDepth == 0 && typeof range != "undefined") { 724 globalRange = range; 725 } else if (executionStackDepth == 0) { 726 globalRange = null; 727 globalRange = range; 728 } 729 730 executionStackDepth++; 731 try { 732 ret = callback(); 733 } catch (e) { 734 executionStackDepth--; 735 throw e; 736 } 737 executionStackDepth--; 738 return ret; 739 } 740 741 function myQueryCommandEnabled(command, range) { 742 // "All of these methods must treat their command argument ASCII 743 // case-insensitively." 744 command = command.toLowerCase(); 745 746 // "If command is not supported, raise a NOT_SUPPORTED_ERR exception." 747 return editCommandMethod(command, "action", range, (function (command) { 748 return function () { 749 // "Among commands defined in this specification, those listed in 750 // Miscellaneous commands are always enabled. The other commands defined 751 // here are enabled if the active range is not null, and disabled 752 // otherwise." 753 return jQuery.inArray(command, ["copy", "cut", "paste", "selectall", "stylewithcss", "usecss"]) != -1 || range !== null; 754 }; 755 }(command))); 756 } 757 758 function setActiveRange(range) { 759 var rangeObject = new window.GENTICS.Utils.RangeObject(); 760 761 rangeObject.startContainer = range.startContainer; 762 rangeObject.startOffset = range.startOffset; 763 rangeObject.endContainer = range.endContainer; 764 rangeObject.endOffset = range.endOffset; 765 766 rangeObject.select(); 767 } 768 769 function myExecCommand(commandArg, showUiArg, valueArg, range) { 770 // "All of these methods must treat their command argument ASCII 771 // case-insensitively." 772 var command = commandArg.toLowerCase(); 773 var showUi = showUiArg; 774 var value = valueArg; 775 776 // "If only one argument was provided, let show UI be false." 777 // 778 // If range was passed, I can't actually detect how many args were passed 779 // . . . 780 if (arguments.length == 1 || (arguments.length >= 4 && typeof showUi == "undefined")) { 781 showUi = false; 782 } 783 784 // "If only one or two arguments were provided, let value be the empty 785 // string." 786 if (arguments.length <= 2 || (arguments.length >= 4 && typeof value == "undefined")) { 787 value = ""; 788 } 789 790 // "If command is not supported, raise a NOT_SUPPORTED_ERR exception." 791 // 792 // "If command has no action, raise an INVALID_ACCESS_ERR exception." 793 return editCommandMethod(command, "action", range, (function (command, showUi, value) { 794 return function () { 795 // "If command is not enabled, return false." 796 if (!myQueryCommandEnabled(command)) { 797 return false; 798 } 799 800 // "Take the action for command, passing value to the instructions as an 801 // argument." 802 commands[command].action(value, range); 803 804 // always fix the range after the command is complete 805 setActiveRange(range); 806 807 // "Return true." 808 return true; 809 }; 810 }(command, showUi, value))); 811 } 812 813 function myQueryCommandIndeterm(command, range) { 814 // "All of these methods must treat their command argument ASCII 815 // case-insensitively." 816 command = command.toLowerCase(); 817 818 // "If command is not supported, raise a NOT_SUPPORTED_ERR exception." 819 // 820 // "If command has no indeterminacy, raise an INVALID_ACCESS_ERR 821 // exception." 822 return editCommandMethod(command, "indeterm", range, (function (command) { 823 return function () { 824 // "If command is not enabled, return false." 825 if (!myQueryCommandEnabled(command, range)) { 826 return false; 827 } 828 829 // "Return true if command is indeterminate, otherwise false." 830 return commands[command].indeterm(range); 831 }; 832 }(command))); 833 } 834 835 function myQueryCommandState(command, range) { 836 // "All of these methods must treat their command argument ASCII 837 // case-insensitively." 838 command = command.toLowerCase(); 839 840 // "If command is not supported, raise a NOT_SUPPORTED_ERR exception." 841 // 842 // "If command has no state, raise an INVALID_ACCESS_ERR exception." 843 return editCommandMethod(command, "state", range, (function (command) { 844 return function () { 845 // "If command is not enabled, return false." 846 if (!myQueryCommandEnabled(command, range)) { 847 return false; 848 } 849 850 // "If the state override for command is set, return it." 851 if (typeof getStateOverride(command, range) != "undefined") { 852 return getStateOverride(command, range); 853 } 854 855 // "Return true if command's state is true, otherwise false." 856 return commands[command].state(range); 857 }; 858 }(command))); 859 } 860 861 // "When the queryCommandSupported(command) method on the HTMLDocument 862 // interface is invoked, the user agent must return true if command is 863 // supported, and false otherwise." 864 function myQueryCommandSupported(command) { 865 // "All of these methods must treat their command argument ASCII 866 // case-insensitively." 867 command = command.toLowerCase(); 868 869 return commands.hasOwnProperty(command); 870 } 871 872 function myQueryCommandValue(command, range) { 873 // "All of these methods must treat their command argument ASCII 874 // case-insensitively." 875 command = command.toLowerCase(); 876 877 return editCommandMethod(command, "value", range, function () { 878 // "If command is not supported or has no value, return the empty string." 879 if (!commands.hasOwnProperty(command) || !commands[command].hasOwnProperty("value")) { 880 return ""; 881 } 882 883 // "If command is "fontSize" and its value override is set, convert the 884 // value override to an integer number of pixels and return the legacy 885 // font size for the result." 886 if (command == "fontsize" && getValueOverride("fontsize", range) !== undefined) { 887 return getLegacyFontSize(getValueOverride("fontsize", range)); 888 } 889 890 // "If the value override for command is set, return it." 891 if (typeof getValueOverride(command, range) != "undefined") { 892 return getValueOverride(command, range); 893 } 894 895 // "Return command's value." 896 return commands[command].value(range); 897 }); 898 } 899 //@} 900 901 ////////////////////////////// 902 ///// Common definitions ///// 903 ////////////////////////////// 904 //@{ 905 906 // "A prohibited paragraph child name is "address", "article", "aside", 907 // "blockquote", "caption", "center", "col", "colgroup", "dd", "details", 908 // "dir", "div", "dl", "dt", "fieldset", "figcaption", "figure", "footer", 909 // "form", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "li", 910 // "listing", "menu", "nav", "ol", "p", "plaintext", "pre", "section", 911 // "summary", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "ul", or 912 // "xmp"." 913 var prohibitedParagraphChildNamesMap = { 914 "ADDRESS": true, 915 "ARTICLE": true, 916 "ASIDE": true, 917 "BLOCKQUOTE": true, 918 919 "CAPTION": true, 920 "CENTER": true, 921 "COL": true, 922 "COLGROUP": true, 923 "DD": true, 924 "DETAILS": true, 925 "DIR": true, 926 "DIV": true, 927 "DL": true, 928 "DT": true, 929 "FIELDSET": true, 930 "FIGCAPTION": true, 931 "FIGURE": true, 932 "FOOTER": true, 933 "FORM": true, 934 "H1": true, 935 "H2": true, 936 "H3": true, 937 "H4": true, 938 "H5": true, 939 "H6": true, 940 "HEADER": true, 941 "HGROUP": true, 942 "HR": true, 943 "LI": true, 944 "LISTING": true, 945 "MENU": true, 946 "NAV": true, 947 "OL": true, 948 "P": true, 949 "PLAINTEXT": true, 950 "PRE": true, 951 "SECTION": true, 952 "SUMMARY": true, 953 "TABLE": true, 954 "TBODY": true, 955 "TD": true, 956 "TFOOT": true, 957 "TH": true, 958 "THEAD": true, 959 "TR": true, 960 "UL": true, 961 "XMP": true 962 }; 963 964 // "A prohibited paragraph child is an HTML element whose local name is a 965 // prohibited paragraph child name." 966 function isProhibitedParagraphChild(node) { 967 return isMappedHtmlElement(node, prohibitedParagraphChildNamesMap); 968 } 969 970 var nonBlockDisplayValuesMap = { 971 "inline": true, 972 "inline-block": true, 973 "inline-table": true, 974 "none": true 975 }; 976 977 // "A block node is either an Element whose "display" property does not have 978 // resolved value "inline" or "inline-block" or "inline-table" or "none", or a 979 // Document, or a DocumentFragment." 980 function isBlockNode(node) { 981 return node && ((node.nodeType == $_.Node.ELEMENT_NODE && !nonBlockDisplayValuesMap[$_.getComputedStyle(node).display]) || node.nodeType == $_.Node.DOCUMENT_NODE || node.nodeType == $_.Node.DOCUMENT_FRAGMENT_NODE); 982 } 983 984 // "An inline node is a node that is not a block node." 985 function isInlineNode(node) { 986 return node && !isBlockNode(node); 987 } 988 989 // "An editing host is a node that is either an Element with a contenteditable 990 // attribute set to the true state, or the Element child of a Document whose 991 // designMode is enabled." 992 function isEditingHost(node) { 993 return node && node.nodeType == $_.Node.ELEMENT_NODE && (node.contentEditable == "true" || (node.parentNode && node.parentNode.nodeType == $_.Node.DOCUMENT_NODE && node.parentNode.designMode == "on")); 994 } 995 996 // "Something is editable if it is a node which is not an editing host, does 997 // not have a contenteditable attribute set to the false state, and whose 998 // parent is an editing host or editable." 999 function isEditable(node) { 1000 // This is slightly a lie, because we're excluding non-HTML elements with 1001 // contentEditable attributes. 1002 return node && !isEditingHost(node) && (node.nodeType != $_.Node.ELEMENT_NODE || node.contentEditable != "false" || jQuery(node).hasClass('aloha-table-wrapper')) && (isEditingHost(node.parentNode) || isEditable(node.parentNode)); 1003 } 1004 1005 // Helper function, not defined in the spec 1006 function hasEditableDescendants(node) { 1007 var i; 1008 for (i = 0; i < node.childNodes.length; i++) { 1009 if (isEditable(node.childNodes[i]) || hasEditableDescendants(node.childNodes[i])) { 1010 return true; 1011 } 1012 } 1013 return false; 1014 } 1015 1016 // "The editing host of node is null if node is neither editable nor an editing 1017 // host; node itself, if node is an editing host; or the nearest ancestor of 1018 // node that is an editing host, if node is editable." 1019 function getEditingHostOf(node) { 1020 if (isEditingHost(node)) { 1021 return node; 1022 } 1023 if (isEditable(node)) { 1024 var ancestor = node.parentNode; 1025 while (!isEditingHost(ancestor)) { 1026 ancestor = ancestor.parentNode; 1027 } 1028 return ancestor; 1029 } 1030 return null; 1031 } 1032 1033 // "Two nodes are in the same editing host if the editing host of the first is 1034 // non-null and the same as the editing host of the second." 1035 function inSameEditingHost(node1, node2) { 1036 return getEditingHostOf(node1) && getEditingHostOf(node1) == getEditingHostOf(node2); 1037 } 1038 1039 // "A collapsed line break is a br that begins a line box which has nothing 1040 // else in it, and therefore has zero height." 1041 function isCollapsedLineBreak(br) { 1042 if (!isNamedHtmlElement(br, 'br')) { 1043 return false; 1044 } 1045 1046 // Add a zwsp after it and see if that changes the height of the nearest 1047 // non-inline parent. Note: this is not actually reliable, because the 1048 // parent might have a fixed height or something. 1049 var ref = br.parentNode; 1050 while ($_.getComputedStyle(ref).display == "inline") { 1051 ref = ref.parentNode; 1052 } 1053 1054 var origStyle = { 1055 height: ref.style.height, 1056 maxHeight: ref.style.maxHeight, 1057 minHeight: ref.style.minHeight 1058 }; 1059 1060 ref.style.height = 'auto'; 1061 ref.style.maxHeight = 'none'; 1062 if (!(jQuery.browser.msie && jQuery.browser.version < 8)) { 1063 ref.style.minHeight = '0'; 1064 } 1065 var space = document.createTextNode('\u200b'); 1066 var origHeight = ref.offsetHeight; 1067 if (origHeight == 0) { 1068 throw 'isCollapsedLineBreak: original height is zero, bug?'; 1069 } 1070 br.parentNode.insertBefore(space, br.nextSibling); 1071 var finalHeight = ref.offsetHeight; 1072 space.parentNode.removeChild(space); 1073 1074 ref.style.height = origStyle.height; 1075 ref.style.maxHeight = origStyle.maxHeight; 1076 if (!(jQuery.browser.msie && jQuery.browser.version < 8)) { 1077 ref.style.minHeight = origStyle.minHeight; 1078 } 1079 1080 // Allow some leeway in case the zwsp didn't create a whole new line, but 1081 // only made an existing line slightly higher. Firefox 6.0a2 shows this 1082 // behavior when the first line is bold. 1083 return origHeight < finalHeight - 5; 1084 } 1085 1086 // "An extraneous line break is a br that has no visual effect, in that 1087 // removing it from the DOM would not change layout, except that a br that is 1088 // the sole child of an li is not extraneous." 1089 function isExtraneousLineBreak(br) { 1090 1091 if (!isNamedHtmlElement(br, 'br')) { 1092 return false; 1093 } 1094 1095 if (isNamedHtmlElement(br.parentNode, "li") && br.parentNode.childNodes.length == 1) { 1096 return false; 1097 } 1098 1099 // Make the line break disappear and see if that changes the block's 1100 // height. Yes, this is an absurd hack. We have to reset height etc. on 1101 // the reference node because otherwise its height won't change if it's not 1102 // auto. 1103 var ref = br.parentNode; 1104 while ($_.getComputedStyle(ref).display == "inline") { 1105 ref = ref.parentNode; 1106 } 1107 1108 var origStyle = { 1109 height: ref.style.height, 1110 maxHeight: ref.style.maxHeight, 1111 minHeight: ref.style.minHeight, 1112 contentEditable: ref.contentEditable 1113 }; 1114 1115 ref.style.height = 'auto'; 1116 ref.style.maxHeight = 'none'; 1117 ref.style.minHeight = '0'; 1118 // IE7 would ignore display:none in contentEditable, so we temporarily set it to false 1119 if (jQuery.browser.msie && jQuery.browser.version <= 7) { 1120 ref.contentEditable = 'false'; 1121 } 1122 1123 var origHeight = ref.offsetHeight; 1124 if (origHeight == 0) { 1125 throw "isExtraneousLineBreak: original height is zero, bug?"; 1126 } 1127 1128 var origBrDisplay = br.style.display; 1129 br.style.display = 'none'; 1130 var finalHeight = ref.offsetHeight; 1131 1132 // Restore original styles to the touched elements. 1133 ref.style.height = origStyle.height; 1134 ref.style.maxHeight = origStyle.maxHeight; 1135 ref.style.minHeight = origStyle.minHeight; 1136 // reset contentEditable for IE7 1137 if (jQuery.browser.msie && jQuery.browser.version <= 7) { 1138 ref.contentEditable = origStyle.contentEditable; 1139 } 1140 1141 br.style.display = origBrDisplay; 1142 1143 // https://github.com/alohaeditor/Aloha-Editor/issues/516 1144 // look like it works in msie > 7 1145 /* if (jQuery.browser.msie && jQuery.browser.version < 8) { 1146 br.removeAttribute("style"); 1147 ref.removeAttribute("style"); 1148 } */ 1149 1150 return origHeight == finalHeight; 1151 } 1152 1153 // "A whitespace node is either a Text node whose data is the empty string; or 1154 // a Text node whose data consists only of one or more tabs (0x0009), line 1155 // feeds (0x000A), carriage returns (0x000D), and/or spaces (0x0020), and whose 1156 // parent is an Element whose resolved value for "white-space" is "normal" or 1157 // "nowrap"; or a Text node whose data consists only of one or more tabs 1158 // (0x0009), carriage returns (0x000D), and/or spaces (0x0020), and whose 1159 // parent is an Element whose resolved value for "white-space" is "pre-line"." 1160 function isWhitespaceNode(node) { 1161 return node && node.nodeType == $_.Node.TEXT_NODE && (node.data == "" || (/^[\t\n\r ]+$/.test(node.data) && node.parentNode && node.parentNode.nodeType == $_.Node.ELEMENT_NODE && jQuery.inArray($_.getComputedStyle(node.parentNode).whiteSpace, ["normal", "nowrap"]) != -1) || (/^[\t\r ]+$/.test(node.data) && node.parentNode && node.parentNode.nodeType == $_.Node.ELEMENT_NODE && $_.getComputedStyle(node.parentNode).whiteSpace == "pre-line") || (/^[\t\n\r ]+$/.test(node.data) && node.parentNode && node.parentNode.nodeType == $_.Node.DOCUMENT_FRAGMENT_NODE)); 1162 } 1163 1164 /** 1165 * Collapse sequences of ignorable whitespace (tab (0x0009), line feed (0x000A), carriage return (0x000D), space (0x0020)) to only one space. 1166 * Preserve the given range if necessary. 1167 * @param node text node 1168 * @param range range 1169 */ 1170 function collapseWhitespace(node, range) { 1171 // "If node is neither editable nor an editing host, abort these steps." 1172 if (!isEditable(node) && !isEditingHost(node)) { 1173 return; 1174 } 1175 1176 // if the given node is not a text node, return 1177 if (!node || node.nodeType !== $_.Node.TEXT_NODE) { 1178 return; 1179 } 1180 1181 // if the node is in a pre or pre-wrap node, return 1182 if (jQuery.inArray($_.getComputedStyle(node.parentNode).whiteSpace, ["pre", "pre-wrap"]) != -1) { 1183 return; 1184 } 1185 1186 // if the given node does not contain sequences of at least two consecutive ignorable whitespace characters, return 1187 if (!/[\t\n\r ]{2,}/.test(node.data)) { 1188 return; 1189 } 1190 1191 var newData = ''; 1192 var correctStart = range.startContainer == node; 1193 var correctEnd = range.endContainer == node; 1194 var wsFound = false; 1195 var i; 1196 1197 // iterate through the node data 1198 for (i = 0; i < node.data.length; ++i) { 1199 if (/[\t\n\r ]/.test(node.data.substr(i, 1))) { 1200 // found a whitespace 1201 if (!wsFound) { 1202 // this is the first whitespace in the current sequence 1203 // add a whitespace to the new data sequence 1204 newData += ' '; 1205 // remember that we found a whitespace 1206 wsFound = true; 1207 } else { 1208 // this is not the first whitespace in the sequence, so omit this character 1209 if (correctStart && newData.length < range.startOffset) { 1210 range.startOffset--; 1211 } 1212 if (correctEnd && newData.length < range.endOffset) { 1213 range.endOffset--; 1214 } 1215 } 1216 } else { 1217 newData += node.data.substr(i, 1); 1218 wsFound = false; 1219 } 1220 } 1221 1222 // set the new data 1223 node.data = newData; 1224 } 1225 1226 // "node is a collapsed whitespace node if the following algorithm returns 1227 // true:" 1228 function isCollapsedWhitespaceNode(node) { 1229 // "If node is not a whitespace node, return false." 1230 if (!isWhitespaceNode(node)) { 1231 return false; 1232 } 1233 1234 // "If node's data is the empty string, return true." 1235 if (node.data == "") { 1236 return true; 1237 } 1238 1239 // "Let ancestor be node's parent." 1240 var ancestor = node.parentNode; 1241 1242 // "If ancestor is null, return true." 1243 if (!ancestor) { 1244 return true; 1245 } 1246 1247 // "If the "display" property of some ancestor of node has resolved value 1248 // "none", return true." 1249 if ($_(getAncestors(node)).some(function (ancestor) { return ancestor.nodeType == $_.Node.ELEMENT_NODE && $_.getComputedStyle(ancestor).display == "none"; })) { 1250 return true; 1251 } 1252 1253 // "While ancestor is not a block node and its parent is not null, set 1254 // ancestor to its parent." 1255 while (!isBlockNode(ancestor) && ancestor.parentNode) { 1256 ancestor = ancestor.parentNode; 1257 } 1258 1259 // "Let reference be node." 1260 var reference = node; 1261 1262 // "While reference is a descendant of ancestor:" 1263 while (reference != ancestor) { 1264 // "Let reference be the node before it in tree order." 1265 reference = previousNode(reference); 1266 1267 // "If reference is a block node or a br, return true." 1268 if (isBlockNode(reference) || isNamedHtmlElement(reference, 'br')) { 1269 return true; 1270 } 1271 1272 // "If reference is a Text node that is not a whitespace node, or is an 1273 // img, break from this loop." 1274 if ((reference.nodeType == $_.Node.TEXT_NODE && !isWhitespaceNode(reference)) || isNamedHtmlElement(reference, 'img')) { 1275 break; 1276 } 1277 } 1278 1279 // "Let reference be node." 1280 reference = node; 1281 1282 // "While reference is a descendant of ancestor:" 1283 var stop = nextNodeDescendants(ancestor); 1284 while (reference != stop) { 1285 // "Let reference be the node after it in tree order, or null if there 1286 // is no such node." 1287 reference = nextNode(reference); 1288 1289 // "If reference is a block node or a br, return true." 1290 if (isBlockNode(reference) || isNamedHtmlElement(reference, 'br')) { 1291 return true; 1292 } 1293 1294 // "If reference is a Text node that is not a whitespace node, or is an 1295 // img, break from this loop." 1296 if ((reference && reference.nodeType == $_.Node.TEXT_NODE && !isWhitespaceNode(reference)) || isNamedHtmlElement(reference, 'img')) { 1297 break; 1298 } 1299 } 1300 1301 // "Return false." 1302 return false; 1303 } 1304 1305 // "Something is visible if it is a node that either is a block node, or a Text 1306 // node that is not a collapsed whitespace node, or an img, or a br that is not 1307 // an extraneous line break, or any node with a visible descendant; excluding 1308 // any node with an ancestor container Element whose "display" property has 1309 // resolved value "none"." 1310 function isVisible(node) { 1311 var i; 1312 1313 if (!node) { 1314 return false; 1315 } 1316 1317 if ($_(getAncestors(node).concat(node)) 1318 .filter(function (node) { return node.nodeType == $_.Node.ELEMENT_NODE; }, true) 1319 .some(function (node) { return $_.getComputedStyle(node).display == "none"; })) { 1320 return false; 1321 } 1322 1323 if (isBlockNode(node) || (node.nodeType == $_.Node.TEXT_NODE && !isCollapsedWhitespaceNode(node)) || isNamedHtmlElement(node, 'img') || (isNamedHtmlElement(node, 'br') && !isExtraneousLineBreak(node))) { 1324 return true; 1325 } 1326 1327 for (i = 0; i < node.childNodes.length; i++) { 1328 if (isVisible(node.childNodes[i])) { 1329 return true; 1330 } 1331 } 1332 1333 return false; 1334 } 1335 1336 // "Something is invisible if it is a node that is not visible." 1337 function isInvisible(node) { 1338 return node && !isVisible(node); 1339 } 1340 1341 // "A collapsed block prop is either a collapsed line break that is not an 1342 // extraneous line break, or an Element that is an inline node and whose 1343 // children are all either invisible or collapsed block props and that has at 1344 // least one child that is a collapsed block prop." 1345 function isCollapsedBlockProp(node) { 1346 var i; 1347 1348 if (isCollapsedLineBreak(node) && !isExtraneousLineBreak(node)) { 1349 return true; 1350 } 1351 1352 if (!isInlineNode(node) || node.nodeType != $_.Node.ELEMENT_NODE) { 1353 return false; 1354 } 1355 1356 var hasCollapsedBlockPropChild = false; 1357 for (i = 0; i < node.childNodes.length; i++) { 1358 if (!isInvisible(node.childNodes[i]) && !isCollapsedBlockProp(node.childNodes[i])) { 1359 return false; 1360 } 1361 if (isCollapsedBlockProp(node.childNodes[i])) { 1362 hasCollapsedBlockPropChild = true; 1363 } 1364 } 1365 1366 return hasCollapsedBlockPropChild; 1367 } 1368 1369 // Please note: This method is deprecated and will be removed. 1370 // Every command should use the value and range parameter. 1371 // 1372 // "The active range is the first range in the Selection given by calling 1373 // getSelection() on the context object, or null if there is no such range." 1374 // 1375 // We cheat and return globalRange if that's defined. We also ensure that the 1376 // active range meets the requirements that selection boundary points are 1377 // supposed to meet, i.e., that the nodes are both Text or Element nodes that 1378 // descend from a Document. 1379 function getActiveRange() { 1380 var ret; 1381 if (globalRange) { 1382 ret = globalRange; 1383 } else if (Aloha.getSelection().rangeCount) { 1384 ret = Aloha.getSelection().getRangeAt(0); 1385 } else { 1386 return null; 1387 } 1388 if (jQuery.inArray(ret.startContainer.nodeType, [$_.Node.TEXT_NODE, $_.Node.ELEMENT_NODE]) == -1 || jQuery.inArray(ret.endContainer.nodeType, [$_.Node.TEXT_NODE, $_.Node.ELEMENT_NODE]) == -1 || !ret.startContainer.ownerDocument || !ret.endContainer.ownerDocument || !isDescendant(ret.startContainer, ret.startContainer.ownerDocument) || !isDescendant(ret.endContainer, ret.endContainer.ownerDocument)) { 1389 throw "Invalid active range; test bug?"; 1390 } 1391 return ret; 1392 } 1393 1394 // "For some commands, each HTMLDocument must have a boolean state override 1395 // and/or a string value override. These do not change the command's state or 1396 // value, but change the way some algorithms behave, as specified in those 1397 // algorithms' definitions. Initially, both must be unset for every command. 1398 // Whenever the number of ranges in the Selection changes to something 1399 // different, and whenever a boundary point of the range at a given index in 1400 // the Selection changes to something different, the state override and value 1401 // override must be unset for every command." 1402 // 1403 // We implement this crudely by using setters and getters. To verify that the 1404 // selection hasn't changed, we copy the active range and just check the 1405 // endpoints match. This isn't really correct, but it's good enough for us. 1406 // Unset state/value overrides are undefined. We put everything in a function 1407 // so no one can access anything except via the provided functions, since 1408 // otherwise callers might mistakenly use outdated overrides (if the selection 1409 // has changed). 1410 (function () { 1411 var stateOverrides = {}; 1412 var valueOverrides = {}; 1413 var storedRange = null; 1414 1415 resetOverrides = function (range) { 1416 if (!storedRange 1417 || storedRange.startContainer != range.startContainer 1418 || storedRange.endContainer != range.endContainer 1419 || storedRange.startOffset != range.startOffset 1420 || storedRange.endOffset != range.endOffset) { 1421 storedRange = { 1422 startContainer: range.startContainer, 1423 endContainer: range.endContainer, 1424 startOffset: range.startOffset, 1425 endOffset: range.endOffset 1426 }; 1427 if (!Maps.isEmpty(stateOverrides) || !Maps.isEmpty(valueOverrides)) { 1428 stateOverrides = {}; 1429 valueOverrides = {}; 1430 return true; 1431 } 1432 } 1433 return false; 1434 }; 1435 1436 getStateOverride = function (command, range) { 1437 resetOverrides(range); 1438 return stateOverrides[command]; 1439 }; 1440 1441 setStateOverride = function (command, newState, range) { 1442 resetOverrides(range); 1443 stateOverrides[command] = newState; 1444 }; 1445 1446 unsetStateOverride = function (command, range) { 1447 resetOverrides(range); 1448 delete stateOverrides[command]; 1449 }; 1450 1451 getValueOverride = function (command, range) { 1452 resetOverrides(range); 1453 return valueOverrides[command]; 1454 }; 1455 1456 // "The value override for the backColor command must be the same as the 1457 // value override for the hiliteColor command, such that setting one sets 1458 // the other to the same thing and unsetting one unsets the other." 1459 setValueOverride = function (command, newValue, range) { 1460 resetOverrides(range); 1461 valueOverrides[command] = newValue; 1462 if (command == "backcolor") { 1463 valueOverrides.hilitecolor = newValue; 1464 } else if (command == "hilitecolor") { 1465 valueOverrides.backcolor = newValue; 1466 } 1467 }; 1468 1469 unsetValueOverride = function (command, range) { 1470 resetOverrides(range); 1471 delete valueOverrides[command]; 1472 if (command == "backcolor") { 1473 delete valueOverrides.hilitecolor; 1474 } else if (command == "hilitecolor") { 1475 delete valueOverrides.backcolor; 1476 } 1477 }; 1478 }()); 1479 1480 //@} 1481 1482 ///////////////////////////// 1483 ///// Common algorithms ///// 1484 ///////////////////////////// 1485 1486 ///// Assorted common algorithms ///// 1487 //@{ 1488 1489 function movePreservingRanges(node, newParent, newIndex, range) { 1490 // For convenience, I allow newIndex to be -1 to mean "insert at the end". 1491 if (newIndex == -1) { 1492 newIndex = newParent.childNodes.length; 1493 } 1494 1495 // "When the user agent is to move a Node to a new location, preserving 1496 // ranges, it must remove the Node from its original parent (if any), then 1497 // insert it in the new location. In doing so, however, it must ignore the 1498 // regular range mutation rules, and instead follow these rules:" 1499 1500 // "Let node be the moved Node, old parent and old index be the old parent 1501 // (which may be null) and index, and new parent and new index be the new 1502 // parent and index." 1503 var oldParent = node.parentNode; 1504 var oldIndex = getNodeIndex(node); 1505 var i; 1506 1507 // We only even attempt to preserve the global range object and the ranges 1508 // in the selection, not every range out there (the latter is probably 1509 // impossible). 1510 var ranges = [range]; 1511 for (i = 0; i < Aloha.getSelection().rangeCount; i++) { 1512 ranges.push(Aloha.getSelection().getRangeAt(i)); 1513 } 1514 var boundaryPoints = []; 1515 $_(ranges).forEach(function (range) { 1516 boundaryPoints.push([range.startContainer, range.startOffset]); 1517 boundaryPoints.push([range.endContainer, range.endOffset]); 1518 }); 1519 1520 $_(boundaryPoints).forEach(function (boundaryPoint) { 1521 // "If a boundary point's node is the same as or a descendant of node, 1522 // leave it unchanged, so it moves to the new location." 1523 // 1524 // No modifications necessary. 1525 1526 // "If a boundary point's node is new parent and its offset is greater 1527 // than new index, add one to its offset." 1528 if (boundaryPoint[0] == newParent && boundaryPoint[1] > newIndex) { 1529 boundaryPoint[1]++; 1530 } 1531 1532 // "If a boundary point's node is old parent and its offset is old index or 1533 // old index + 1, set its node to new parent and add new index − old index 1534 // to its offset." 1535 if (boundaryPoint[0] == oldParent && (boundaryPoint[1] == oldIndex || boundaryPoint[1] == oldIndex + 1)) { 1536 boundaryPoint[0] = newParent; 1537 boundaryPoint[1] += newIndex - oldIndex; 1538 } 1539 1540 // "If a boundary point's node is old parent and its offset is greater than 1541 // old index + 1, subtract one from its offset." 1542 if (boundaryPoint[0] == oldParent && boundaryPoint[1] > oldIndex + 1) { 1543 boundaryPoint[1]--; 1544 } 1545 }); 1546 1547 // Now actually move it and preserve the ranges. 1548 if (newParent.childNodes.length == newIndex) { 1549 newParent.appendChild(node); 1550 } else { 1551 newParent.insertBefore(node, newParent.childNodes[newIndex]); 1552 } 1553 1554 // if we're off actual node boundaries this implies that the move was 1555 // part of a deletion process (backspace). If that's the case we 1556 // attempt to fix this by restoring the range to the first index of 1557 // the node that has been moved 1558 var newRange = null; 1559 if (boundaryPoints[0][1] > boundaryPoints[0][0].childNodes.length && boundaryPoints[1][1] > boundaryPoints[1][0].childNodes.length) { 1560 range.setStart(node, 0); 1561 range.setEnd(node, 0); 1562 } else { 1563 range.setStart(boundaryPoints[0][0], boundaryPoints[0][1]); 1564 range.setEnd(boundaryPoints[1][0], boundaryPoints[1][1]); 1565 1566 Aloha.getSelection().removeAllRanges(); 1567 for (i = 1; i < ranges.length; i++) { 1568 newRange = Aloha.createRange(); 1569 newRange.setStart(boundaryPoints[2 * i][0], boundaryPoints[2 * i][1]); 1570 newRange.setEnd(boundaryPoints[2 * i + 1][0], boundaryPoints[2 * i + 1][1]); 1571 Aloha.getSelection().addRange(newRange); 1572 } 1573 if (newRange) { 1574 range = newRange; 1575 } 1576 } 1577 } 1578 1579 /** 1580 * Copy all non empty attributes from an existing to a new element 1581 * 1582 * @param {dom} element The source DOM element 1583 * @param {dom} newElement The new DOM element which will get the attributes of the source DOM element 1584 * @return void 1585 */ 1586 function copyAttributes(element, newElement) { 1587 1588 // This is an IE7 workaround. We identified three places that were connected 1589 // to the mysterious ie7 crash: 1590 // 1. Add attribute to dom element (Initialization of jquery-ui sortable) 1591 // 2. Access the jquery expando attribute. Just reading the name is 1592 // sufficient to make the browser vulnerable for the crash (Press enter) 1593 // 3. On editable blur the Aloha.editables[0].getContents(); gets invoked. 1594 // This invokation somehow crashes the ie7. We assume that the access of 1595 // shared expando attribute updates internal references which are not 1596 // correclty handled during clone(); 1597 if (jQuery.browser.msie && jQuery.browser.version >= 7 && typeof element.attributes[jQuery.expando] !== 'undefined') { 1598 jQuery(element).removeAttr(jQuery.expando); 1599 } 1600 1601 var attrs = element.attributes; 1602 var i; 1603 for (i = 0; i < attrs.length; i++) { 1604 var attr = attrs[i]; 1605 // attr.specified is an IE specific check to exclude attributes that were never really set. 1606 if (typeof attr.specified === "undefined" || attr.specified) { 1607 if (typeof newElement.setAttributeNS === 'function') { 1608 newElement.setAttributeNS(attr.namespaceURI, attr.name, attr.value); 1609 } else { 1610 // fixes https://github.com/alohaeditor/Aloha-Editor/issues/515 1611 newElement.setAttribute(attr.name, attr.value); 1612 } 1613 } 1614 } 1615 } 1616 1617 function setTagName(element, newName, range) { 1618 // "If element is an HTML element with local name equal to new name, return 1619 // element." 1620 if (isNamedHtmlElement(element, newName)) { 1621 return element; 1622 } 1623 1624 // "If element's parent is null, return element." 1625 if (!element.parentNode) { 1626 return element; 1627 } 1628 1629 // "Let replacement element be the result of calling createElement(new 1630 // name) on the ownerDocument of element." 1631 var replacementElement = element.ownerDocument.createElement(newName); 1632 1633 // "Insert replacement element into element's parent immediately before 1634 // element." 1635 element.parentNode.insertBefore(replacementElement, element); 1636 1637 // "Copy all attributes of element to replacement element, in order." 1638 copyAttributes(element, replacementElement); 1639 1640 // "While element has children, append the first child of element as the 1641 // last child of replacement element, preserving ranges." 1642 while (element.childNodes.length) { 1643 movePreservingRanges(element.firstChild, replacementElement, replacementElement.childNodes.length, range); 1644 } 1645 1646 // "Remove element from its parent." 1647 element.parentNode.removeChild(element); 1648 1649 // if the range still uses the old element, we modify it to the new one 1650 if (range.startContainer === element) { 1651 range.startContainer = replacementElement; 1652 } 1653 if (range.endContainer === element) { 1654 range.endContainer = replacementElement; 1655 } 1656 1657 // "Return replacement element." 1658 return replacementElement; 1659 } 1660 1661 function removeExtraneousLineBreaksBefore(node) { 1662 // "Let ref be the previousSibling of node." 1663 var ref = node.previousSibling; 1664 1665 // "If ref is null, abort these steps." 1666 if (!ref) { 1667 return; 1668 } 1669 1670 // "While ref has children, set ref to its lastChild." 1671 while (ref.hasChildNodes()) { 1672 ref = ref.lastChild; 1673 } 1674 1675 // "While ref is invisible but not an extraneous line break, and ref does 1676 // not equal node's parent, set ref to the node before it in tree order." 1677 while (isInvisible(ref) && !isExtraneousLineBreak(ref) && ref != node.parentNode) { 1678 ref = previousNode(ref); 1679 } 1680 1681 // "If ref is an editable extraneous line break, remove it from its 1682 // parent." 1683 if (isEditable(ref) && isExtraneousLineBreak(ref)) { 1684 ref.parentNode.removeChild(ref); 1685 } 1686 } 1687 1688 function removeExtraneousLineBreaksAtTheEndOf(node) { 1689 // "Let ref be node." 1690 var ref = node; 1691 1692 // "While ref has children, set ref to its lastChild." 1693 while (ref.hasChildNodes()) { 1694 ref = ref.lastChild; 1695 } 1696 1697 // "While ref is invisible but not an extraneous line break, and ref does 1698 // not equal node, set ref to the node before it in tree order." 1699 while (isInvisible(ref) && !isExtraneousLineBreak(ref) && ref != node) { 1700 ref = previousNode(ref); 1701 } 1702 1703 // "If ref is an editable extraneous line break, remove it from its 1704 // parent." 1705 if (isEditable(ref) && isExtraneousLineBreak(ref)) { 1706 ref.parentNode.removeChild(ref); 1707 } 1708 } 1709 1710 // "To remove extraneous line breaks from a node, first remove extraneous line 1711 // breaks before it, then remove extraneous line breaks at the end of it." 1712 function removeExtraneousLineBreaksFrom(node) { 1713 removeExtraneousLineBreaksBefore(node); 1714 removeExtraneousLineBreaksAtTheEndOf(node); 1715 } 1716 1717 //@} 1718 ///// Wrapping a list of nodes ///// 1719 //@{ 1720 1721 function wrap(nodeList, siblingCriteria, newParentInstructions, range) { 1722 var i; 1723 1724 // "If not provided, sibling criteria returns false and new parent 1725 // instructions returns null." 1726 if (typeof siblingCriteria == "undefined") { 1727 siblingCriteria = function () { 1728 return false; 1729 }; 1730 } 1731 if (typeof newParentInstructions == "undefined") { 1732 newParentInstructions = function () { 1733 return null; 1734 }; 1735 } 1736 1737 // "If node list is empty, or the first member of node list is not 1738 // editable, return null and abort these steps." 1739 if (!nodeList.length || !isEditable(nodeList[0])) { 1740 return null; 1741 } 1742 1743 // "If node list's last member is an inline node that's not a br, and node 1744 // list's last member's nextSibling is a br, append that br to node list." 1745 if (isInlineNode(nodeList[nodeList.length - 1]) && !isNamedHtmlElement(nodeList[nodeList.length - 1], "br") && isNamedHtmlElement(nodeList[nodeList.length - 1].nextSibling, "br")) { 1746 nodeList.push(nodeList[nodeList.length - 1].nextSibling); 1747 } 1748 1749 // "If the previousSibling of the first member of node list is editable and 1750 // running sibling criteria on it returns true, let new parent be the 1751 // previousSibling of the first member of node list." 1752 var newParent; 1753 if (isEditable(nodeList[0].previousSibling) && siblingCriteria(nodeList[0].previousSibling)) { 1754 newParent = nodeList[0].previousSibling; 1755 1756 // "Otherwise, if the nextSibling of the last member of node list is 1757 // editable and running sibling criteria on it returns true, let new parent 1758 // be the nextSibling of the last member of node list." 1759 } else if (isEditable(nodeList[nodeList.length - 1].nextSibling) && siblingCriteria(nodeList[nodeList.length - 1].nextSibling)) { 1760 newParent = nodeList[nodeList.length - 1].nextSibling; 1761 1762 // "Otherwise, run new parent instructions, and let new parent be the 1763 // result." 1764 } else { 1765 newParent = newParentInstructions(); 1766 } 1767 1768 // "If new parent is null, abort these steps and return null." 1769 if (!newParent) { 1770 return null; 1771 } 1772 1773 // "If new parent's parent is null:" 1774 if (!newParent.parentNode) { 1775 // "Insert new parent into the parent of the first member of node list 1776 // immediately before the first member of node list." 1777 nodeList[0].parentNode.insertBefore(newParent, nodeList[0]); 1778 1779 // "If any range has a boundary point with node equal to the parent of 1780 // new parent and offset equal to the index of new parent, add one to 1781 // that boundary point's offset." 1782 // 1783 // Try to fix range 1784 var startContainer = range.startContainer, 1785 startOffset = range.startOffset, 1786 endContainer = range.endContainer, 1787 endOffset = range.endOffset; 1788 if (startContainer == newParent.parentNode && startOffset >= getNodeIndex(newParent)) { 1789 range.setStart(startContainer, startOffset + 1); 1790 } 1791 if (endContainer == newParent.parentNode && endOffset >= getNodeIndex(newParent)) { 1792 range.setEnd(endContainer, endOffset + 1); 1793 } 1794 1795 // Only try to fix the global range. TODO remove globalRange here 1796 if (globalRange && globalRange !== range) { 1797 startContainer = globalRange.startContainer; 1798 startOffset = globalRange.startOffset; 1799 endContainer = globalRange.endContainer; 1800 endOffset = globalRange.endOffset; 1801 if (startContainer == newParent.parentNode && startOffset >= getNodeIndex(newParent)) { 1802 globalRange.setStart(startContainer, startOffset + 1); 1803 } 1804 if (endContainer == newParent.parentNode && endOffset >= getNodeIndex(newParent)) { 1805 globalRange.setEnd(endContainer, endOffset + 1); 1806 } 1807 } 1808 } 1809 1810 // "Let original parent be the parent of the first member of node list." 1811 var originalParent = nodeList[0].parentNode; 1812 1813 // "If new parent is before the first member of node list in tree order:" 1814 if (isBefore(newParent, nodeList[0])) { 1815 // "If new parent is not an inline node, but the last child of new 1816 // parent and the first member of node list are both inline nodes, and 1817 // the last child of new parent is not a br, call createElement("br") 1818 // on the ownerDocument of new parent and append the result as the last 1819 // child of new parent." 1820 if (!isInlineNode(newParent) && isInlineNode(newParent.lastChild) && isInlineNode(nodeList[0]) && !isNamedHtmlElement(newParent.lastChild, "BR")) { 1821 newParent.appendChild(newParent.ownerDocument.createElement("br")); 1822 } 1823 1824 // "For each node in node list, append node as the last child of new 1825 // parent, preserving ranges." 1826 for (i = 0; i < nodeList.length; i++) { 1827 movePreservingRanges(nodeList[i], newParent, -1, range); 1828 } 1829 1830 // "Otherwise:" 1831 } else { 1832 // "If new parent is not an inline node, but the first child of new 1833 // parent and the last member of node list are both inline nodes, and 1834 // the last member of node list is not a br, call createElement("br") 1835 // on the ownerDocument of new parent and insert the result as the 1836 // first child of new parent." 1837 if (!isInlineNode(newParent) && isInlineNode(newParent.firstChild) && isInlineNode(nodeList[nodeList.length - 1]) && !isNamedHtmlElement(nodeList[nodeList.length - 1], "BR")) { 1838 newParent.insertBefore(newParent.ownerDocument.createElement("br"), newParent.firstChild); 1839 } 1840 1841 // "For each node in node list, in reverse order, insert node as the 1842 // first child of new parent, preserving ranges." 1843 for (i = nodeList.length - 1; i >= 0; i--) { 1844 movePreservingRanges(nodeList[i], newParent, 0, range); 1845 } 1846 } 1847 1848 // "If original parent is editable and has no children, remove it from its 1849 // parent." 1850 if (isEditable(originalParent) && !originalParent.hasChildNodes()) { 1851 originalParent.parentNode.removeChild(originalParent); 1852 } 1853 1854 // "If new parent's nextSibling is editable and running sibling criteria on 1855 // it returns true:" 1856 if (isEditable(newParent.nextSibling) && siblingCriteria(newParent.nextSibling)) { 1857 // "If new parent is not an inline node, but new parent's last child 1858 // and new parent's nextSibling's first child are both inline nodes, 1859 // and new parent's last child is not a br, call createElement("br") on 1860 // the ownerDocument of new parent and append the result as the last 1861 // child of new parent." 1862 if (!isInlineNode(newParent) && isInlineNode(newParent.lastChild) && isInlineNode(newParent.nextSibling.firstChild) && !isNamedHtmlElement(newParent.lastChild, "BR")) { 1863 newParent.appendChild(newParent.ownerDocument.createElement("br")); 1864 } 1865 1866 // "While new parent's nextSibling has children, append its first child 1867 // as the last child of new parent, preserving ranges." 1868 while (newParent.nextSibling.hasChildNodes()) { 1869 movePreservingRanges(newParent.nextSibling.firstChild, newParent, -1, range); 1870 } 1871 1872 // "Remove new parent's nextSibling from its parent." 1873 newParent.parentNode.removeChild(newParent.nextSibling); 1874 } 1875 1876 // "Remove extraneous line breaks from new parent." 1877 removeExtraneousLineBreaksFrom(newParent); 1878 1879 // "Return new parent." 1880 return newParent; 1881 } 1882 1883 1884 //@} 1885 ///// Allowed children ///// 1886 //@{ 1887 1888 // "A name of an element with inline contents is "a", "abbr", "b", "bdi", 1889 // "bdo", "cite", "code", "dfn", "em", "h1", "h2", "h3", "h4", "h5", "h6", "i", 1890 // "kbd", "mark", "p", "pre", "q", "rp", "rt", "ruby", "s", "samp", "small", 1891 // "span", "strong", "sub", "sup", "u", "var", "acronym", "listing", "strike", 1892 // "xmp", "big", "blink", "font", "marquee", "nobr", or "tt"." 1893 var namesOfElementsWithInlineContentsMap = { 1894 "A": true, 1895 "ABBR": true, 1896 "B": true, 1897 "BDI": true, 1898 "BDO": true, 1899 "CITE": true, 1900 "CODE": true, 1901 "DFN": true, 1902 "EM": true, 1903 "H1": true, 1904 "H2": true, 1905 "H3": true, 1906 "H4": true, 1907 "H5": true, 1908 "H6": true, 1909 "I": true, 1910 "KBD": true, 1911 "MARK": true, 1912 "P": true, 1913 "PRE": true, 1914 "Q": true, 1915 "RP": true, 1916 "RT": true, 1917 "RUBY": true, 1918 "S": true, 1919 "SAMP": true, 1920 "SMALL": true, 1921 "SPAN": true, 1922 "STRONG": true, 1923 "SUB": true, 1924 "SUP": true, 1925 "U": true, 1926 "VAR": true, 1927 "ACRONYM": true, 1928 "LISTING": true, 1929 "STRIKE": true, 1930 "XMP": true, 1931 "BIG": true, 1932 1933 "BLINK": true, 1934 "FONT": true, 1935 "MARQUEE": true, 1936 "NOBR": true, 1937 "TT": true 1938 }; 1939 1940 1941 var tableRelatedElements = { 1942 "colgroup": true, 1943 "table": true, 1944 "tbody": true, 1945 "tfoot": true, 1946 "thead": true, 1947 "tr": true 1948 }; 1949 1950 var scriptRelatedElements = { 1951 "script": true, 1952 "style": true, 1953 "plaintext": true, 1954 "xmp": true 1955 }; 1956 1957 var prohibitedHeadingNestingMap = jQuery.extend({ 1958 "H1": true, 1959 "H2": true, 1960 "H3": true, 1961 "H4": true, 1962 "H5": true, 1963 "H6": true 1964 }, prohibitedParagraphChildNamesMap); 1965 var prohibitedTableNestingMap = { 1966 "CAPTION": true, 1967 "COL": true, 1968 "COLGROUP": true, 1969 "TBODY": true, 1970 "TD": true, 1971 "TFOOT": true, 1972 "TH": true, 1973 "THEAD": true, 1974 "TR": true 1975 }; 1976 var prohibitedDefNestingMap = { 1977 "DD": true, 1978 "DT": true 1979 }; 1980 var prohibitedNestingCombinationsMap = { 1981 "A": jQuery.extend({ 1982 "A": true 1983 }, prohibitedParagraphChildNamesMap), 1984 "DD": prohibitedDefNestingMap, 1985 "DT": prohibitedDefNestingMap, 1986 "LI": { 1987 "LI": true 1988 }, 1989 "NOBR": jQuery.extend({ 1990 "NOBR": true 1991 }, prohibitedParagraphChildNamesMap), 1992 "H1": prohibitedHeadingNestingMap, 1993 "H2": prohibitedHeadingNestingMap, 1994 "H3": prohibitedHeadingNestingMap, 1995 "H4": prohibitedHeadingNestingMap, 1996 "H5": prohibitedHeadingNestingMap, 1997 "H6": prohibitedHeadingNestingMap, 1998 "TD": prohibitedTableNestingMap, 1999 "TH": prohibitedTableNestingMap, 2000 // this is the same as namesOfElementsWithInlineContentsMap excluding a and h1-h6 elements above 2001 "ABBR": prohibitedParagraphChildNamesMap, 2002 "B": prohibitedParagraphChildNamesMap, 2003 "BDI": prohibitedParagraphChildNamesMap, 2004 "BDO": prohibitedParagraphChildNamesMap, 2005 "CITE": prohibitedParagraphChildNamesMap, 2006 "CODE": prohibitedParagraphChildNamesMap, 2007 "DFN": prohibitedParagraphChildNamesMap, 2008 "EM": prohibitedParagraphChildNamesMap, 2009 "I": prohibitedParagraphChildNamesMap, 2010 "KBD": prohibitedParagraphChildNamesMap, 2011 "MARK": prohibitedParagraphChildNamesMap, 2012 "P": prohibitedParagraphChildNamesMap, 2013 "PRE": prohibitedParagraphChildNamesMap, 2014 "Q": prohibitedParagraphChildNamesMap, 2015 "RP": prohibitedParagraphChildNamesMap, 2016 "RT": prohibitedParagraphChildNamesMap, 2017 "RUBY": prohibitedParagraphChildNamesMap, 2018 "S": prohibitedParagraphChildNamesMap, 2019 "SAMP": prohibitedParagraphChildNamesMap, 2020 "SMALL": prohibitedParagraphChildNamesMap, 2021 "SPAN": prohibitedParagraphChildNamesMap, 2022 "STRONG": prohibitedParagraphChildNamesMap, 2023 "SUB": prohibitedParagraphChildNamesMap, 2024 "SUP": prohibitedParagraphChildNamesMap, 2025 "U": prohibitedParagraphChildNamesMap, 2026 "VAR": prohibitedParagraphChildNamesMap, 2027 "ACRONYM": prohibitedParagraphChildNamesMap, 2028 "LISTING": prohibitedParagraphChildNamesMap, 2029 "STRIKE": prohibitedParagraphChildNamesMap, 2030 "XMP": prohibitedParagraphChildNamesMap, 2031 "BIG": prohibitedParagraphChildNamesMap, 2032 "BLINK": prohibitedParagraphChildNamesMap, 2033 "FONT": prohibitedParagraphChildNamesMap, 2034 "MARQUEE": prohibitedParagraphChildNamesMap, 2035 "TT": prohibitedParagraphChildNamesMap 2036 }; 2037 2038 // "An element with inline contents is an HTML element whose local name is a 2039 // name of an element with inline contents." 2040 function isElementWithInlineContents(node) { 2041 return isMappedHtmlElement(node, namesOfElementsWithInlineContentsMap); 2042 } 2043 2044 function isAllowedChild(child, parent_) { 2045 // "If parent is "colgroup", "table", "tbody", "tfoot", "thead", "tr", or 2046 // an HTML element with local name equal to one of those, and child is a 2047 // Text node whose data does not consist solely of space characters, return 2048 // false." 2049 if ((tableRelatedElements[parent_] || isHtmlElementInArray(parent_, ["colgroup", "table", "tbody", "tfoot", "thead", "tr"])) && typeof child == "object" && child.nodeType == $_.Node.TEXT_NODE && !/^[ \t\n\f\r]*$/.test(child.data)) { 2050 return false; 2051 } 2052 2053 // "If parent is "script", "style", "plaintext", or "xmp", or an HTML 2054 // element with local name equal to one of those, and child is not a Text 2055 // node, return false." 2056 if ((scriptRelatedElements[parent_] || isHtmlElementInArray(parent_, ["script", "style", "plaintext", "xmp"])) && (typeof child != "object" || child.nodeType != $_.Node.TEXT_NODE)) { 2057 return false; 2058 } 2059 2060 // "If child is a Document, DocumentFragment, or DocumentType, return 2061 // false." 2062 if (typeof child == "object" && (child.nodeType == $_.Node.DOCUMENT_NODE || child.nodeType == $_.Node.DOCUMENT_FRAGMENT_NODE || child.nodeType == $_.Node.DOCUMENT_TYPE_NODE)) { 2063 return false; 2064 } 2065 2066 // "If child is an HTML element, set child to the local name of child." 2067 if (isAnyHtmlElement(child)) { 2068 child = child.tagName.toLowerCase(); 2069 } 2070 2071 // "If child is not a string, return true." 2072 if (typeof child != "string") { 2073 return true; 2074 } 2075 2076 // "If parent is an HTML element:" 2077 if (isAnyHtmlElement(parent_)) { 2078 // "If child is "a", and parent or some ancestor of parent is an a, 2079 // return false." 2080 // 2081 // "If child is a prohibited paragraph child name and parent or some 2082 // ancestor of parent is an element with inline contents, return 2083 // false." 2084 // 2085 // "If child is "h1", "h2", "h3", "h4", "h5", or "h6", and parent or 2086 // some ancestor of parent is an HTML element with local name "h1", 2087 // "h2", "h3", "h4", "h5", or "h6", return false." 2088 var ancestor = parent_; 2089 while (ancestor) { 2090 if (child == "a" && isNamedHtmlElement(ancestor, 'a')) { 2091 return false; 2092 } 2093 if (prohibitedParagraphChildNamesMap[child.toUpperCase()] && isElementWithInlineContents(ancestor)) { 2094 return false; 2095 } 2096 if (/^h[1-6]$/.test(child) && isAnyHtmlElement(ancestor) && /^H[1-6]$/.test(ancestor.tagName)) { 2097 return false; 2098 } 2099 ancestor = ancestor.parentNode; 2100 } 2101 2102 // "Let parent be the local name of parent." 2103 parent_ = parent_.tagName.toLowerCase(); 2104 } 2105 2106 // "If parent is an Element or DocumentFragment, return true." 2107 if (typeof parent_ == "object" && (parent_.nodeType == $_.Node.ELEMENT_NODE || parent_.nodeType == $_.Node.DOCUMENT_FRAGMENT_NODE)) { 2108 return true; 2109 } 2110 2111 // "If parent is not a string, return false." 2112 if (typeof parent_ != "string") { 2113 return false; 2114 } 2115 2116 // "If parent is on the left-hand side of an entry on the following list, 2117 // then return true if child is listed on the right-hand side of that 2118 // entry, and false otherwise." 2119 switch (parent_) { 2120 case "colgroup": 2121 return child == "col"; 2122 case "table": 2123 return jQuery.inArray(child, ["caption", "col", "colgroup", "tbody", "td", "tfoot", "th", "thead", "tr"]) != -1; 2124 case "tbody": 2125 case "thead": 2126 case "tfoot": 2127 return jQuery.inArray(child, ["td", "th", "tr"]) != -1; 2128 case "tr": 2129 return jQuery.inArray(child, ["td", "th"]) != -1; 2130 case "dl": 2131 return jQuery.inArray(child, ["dt", "dd"]) != -1; 2132 case "dir": 2133 case "ol": 2134 case "ul": 2135 return jQuery.inArray(child, ["dir", "li", "ol", "ul"]) != -1; 2136 case "hgroup": 2137 return (/^h[1-6]$/).test(child); 2138 } 2139 2140 // "If child is "body", "caption", "col", "colgroup", "frame", "frameset", 2141 // "head", "html", "tbody", "td", "tfoot", "th", "thead", or "tr", return 2142 // false." 2143 if (jQuery.inArray(child, ["body", "caption", "col", "colgroup", "frame", "frameset", "head", "html", "tbody", "td", "tfoot", "th", "thead", "tr"]) != -1) { 2144 return false; 2145 } 2146 2147 // "If child is "dd" or "dt" and parent is not "dl", return false." 2148 if (jQuery.inArray(child, ["dd", "dt"]) != -1 && parent_ != "dl") { 2149 return false; 2150 } 2151 2152 // "If child is "li" and parent is not "ol" or "ul", return false." 2153 if (child == "li" && parent_ != "ol" && parent_ != "ul") { 2154 return false; 2155 } 2156 2157 // "If parent is on the left-hand side of an entry on the following list 2158 // and child is listed on the right-hand side of that entry, return false." 2159 var leftSide = prohibitedNestingCombinationsMap[parent_.toUpperCase()]; 2160 if (leftSide) { 2161 var rightSide = leftSide[child.toUpperCase()]; 2162 if (rightSide) { 2163 return false; 2164 } 2165 } 2166 2167 // "Return true." 2168 return true; 2169 } 2170 2171 2172 //@} 2173 2174 ////////////////////////////////////// 2175 ///// Inline formatting commands ///// 2176 ////////////////////////////////////// 2177 2178 ///// Inline formatting command definitions ///// 2179 //@{ 2180 2181 // "A node node is effectively contained in a range range if range is not 2182 // collapsed, and at least one of the following holds:" 2183 function isEffectivelyContained(node, range) { 2184 if (range.collapsed) { 2185 return false; 2186 } 2187 2188 // "node is contained in range." 2189 if (isContained(node, range)) { 2190 return true; 2191 } 2192 2193 // "node is range's start node, it is a Text node, and its length is 2194 // different from range's start offset." 2195 if (node == range.startContainer && node.nodeType == $_.Node.TEXT_NODE && getNodeLength(node) != range.startOffset) { 2196 return true; 2197 } 2198 2199 // "node is range's end node, it is a Text node, and range's end offset is 2200 // not 0." 2201 if (node == range.endContainer && node.nodeType == $_.Node.TEXT_NODE && range.endOffset != 0) { 2202 return true; 2203 } 2204 2205 // "node has at least one child; and all its children are effectively 2206 // contained in range; and either range's start node is not a descendant of 2207 // node or is not a Text node or range's start offset is zero; and either 2208 // range's end node is not a descendant of node or is not a Text node or 2209 // range's end offset is its end node's length." 2210 if (node.hasChildNodes() && $_(node.childNodes).every(function (child) { return isEffectivelyContained(child, range); }) 2211 && (!isDescendant(range.startContainer, node) 2212 || range.startContainer.nodeType != $_.Node.TEXT_NODE 2213 || range.startOffset == 0) 2214 && (!isDescendant(range.endContainer, node) 2215 || range.endContainer.nodeType != $_.Node.TEXT_NODE 2216 || range.endOffset == getNodeLength(range.endContainer))) { 2217 return true; 2218 } 2219 2220 return false; 2221 } 2222 2223 // Like get(All)ContainedNodes(), but for effectively contained nodes. 2224 function getEffectivelyContainedNodes(range, condition) { 2225 if (typeof condition == "undefined") { 2226 condition = function () { 2227 return true; 2228 }; 2229 } 2230 var node = range.startContainer; 2231 while (isEffectivelyContained(node.parentNode, range)) { 2232 node = node.parentNode; 2233 } 2234 2235 var stop = nextNodeDescendants(range.endContainer); 2236 2237 var nodeList = []; 2238 while (isBefore(node, stop)) { 2239 if (isEffectivelyContained(node, range) && condition(node)) { 2240 nodeList.push(node); 2241 node = nextNodeDescendants(node); 2242 continue; 2243 } 2244 node = nextNode(node); 2245 } 2246 return nodeList; 2247 } 2248 2249 function getAllEffectivelyContainedNodes(range, condition) { 2250 if (typeof condition == "undefined") { 2251 condition = function () { 2252 return true; 2253 }; 2254 } 2255 var node = range.startContainer; 2256 while (isEffectivelyContained(node.parentNode, range)) { 2257 node = node.parentNode; 2258 } 2259 2260 var stop = nextNodeDescendants(range.endContainer); 2261 2262 var nodeList = []; 2263 while (isBefore(node, stop)) { 2264 if (isEffectivelyContained(node, range) && condition(node)) { 2265 nodeList.push(node); 2266 } 2267 node = nextNode(node); 2268 } 2269 return nodeList; 2270 } 2271 2272 // "A modifiable element is a b, em, i, s, span, strong, sub, sup, or u element 2273 // with no attributes except possibly style; or a font element with no 2274 // attributes except possibly style, color, face, and/or size; or an a element 2275 // with no attributes except possibly style and/or href." 2276 function isModifiableElement(node) { 2277 if (!isAnyHtmlElement(node)) { 2278 return false; 2279 } 2280 2281 if (jQuery.inArray(node.tagName, ["B", "EM", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"]) != -1) { 2282 if (node.attributes.length == 0) { 2283 return true; 2284 } 2285 2286 if (node.attributes.length == 1 && hasAttribute(node, "style")) { 2287 return true; 2288 } 2289 } 2290 2291 if (node.tagName == "FONT" || node.tagName == "A") { 2292 var numAttrs = node.attributes.length; 2293 2294 if (hasAttribute(node, "style")) { 2295 numAttrs--; 2296 } 2297 2298 if (node.tagName == "FONT") { 2299 if (hasAttribute(node, "color")) { 2300 numAttrs--; 2301 } 2302 2303 if (hasAttribute(node, "face")) { 2304 numAttrs--; 2305 } 2306 2307 if (hasAttribute(node, "size")) { 2308 numAttrs--; 2309 } 2310 } 2311 2312 if (node.tagName == "A" && hasAttribute(node, "href")) { 2313 numAttrs--; 2314 } 2315 2316 if (numAttrs == 0) { 2317 return true; 2318 } 2319 } 2320 2321 return false; 2322 } 2323 2324 function isSimpleModifiableElement(node) { 2325 // "A simple modifiable element is an HTML element for which at least one 2326 // of the following holds:" 2327 if (!isAnyHtmlElement(node)) { 2328 return false; 2329 } 2330 2331 // Only these elements can possibly be a simple modifiable element. 2332 if (jQuery.inArray(node.tagName, ["A", "B", "EM", "FONT", "I", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U"]) == -1) { 2333 return false; 2334 } 2335 2336 // "It is an a, b, em, font, i, s, span, strike, strong, sub, sup, or u 2337 // element with no attributes." 2338 if (node.attributes.length == 0) { 2339 return true; 2340 } 2341 2342 // If it's got more than one attribute, everything after this fails. 2343 if (node.attributes.length > 1) { 2344 return false; 2345 } 2346 2347 // "It is an a, b, em, font, i, s, span, strike, strong, sub, sup, or u 2348 // element with exactly one attribute, which is style, which sets no CSS 2349 // properties (including invalid or unrecognized properties)." 2350 // 2351 // Not gonna try for invalid or unrecognized. 2352 if (hasAttribute(node, "style") && getStyleLength(node) == 0) { 2353 return true; 2354 } 2355 2356 2357 // "It is an a element with exactly one attribute, which is href." 2358 if (node.tagName == "A" && hasAttribute(node, "href")) { 2359 return true; 2360 } 2361 2362 // "It is a font element with exactly one attribute, which is either color, 2363 // face, or size." 2364 if (node.tagName == "FONT" && (hasAttribute(node, "color") || hasAttribute(node, "face") || hasAttribute(node, "size"))) { 2365 return true; 2366 } 2367 2368 // "It is a b or strong element with exactly one attribute, which is style, 2369 // and the style attribute sets exactly one CSS property (including invalid 2370 // or unrecognized properties), which is "font-weight"." 2371 if ((node.tagName == "B" || node.tagName == "STRONG") && hasAttribute(node, "style") && getStyleLength(node) == 1 && node.style.fontWeight != "") { 2372 return true; 2373 } 2374 2375 // "It is an i or em element with exactly one attribute, which is style, 2376 // and the style attribute sets exactly one CSS property (including invalid 2377 // or unrecognized properties), which is "font-style"." 2378 if ((node.tagName == "I" || node.tagName == "EM") && hasAttribute(node, "style") && getStyleLength(node) == 1 && node.style.fontStyle != "") { 2379 return true; 2380 } 2381 2382 // "It is an a, font, or span element with exactly one attribute, which is 2383 // style, and the style attribute sets exactly one CSS property (including 2384 // invalid or unrecognized properties), and that property is not 2385 // "text-decoration"." 2386 if ((node.tagName == "A" || node.tagName == "FONT" || node.tagName == "SPAN") && hasAttribute(node, "style") && getStyleLength(node) == 1 && node.style.textDecoration == "") { 2387 return true; 2388 } 2389 2390 // "It is an a, font, s, span, strike, or u element with exactly one 2391 // attribute, which is style, and the style attribute sets exactly one CSS 2392 // property (including invalid or unrecognized properties), which is 2393 // "text-decoration", which is set to "line-through" or "underline" or 2394 // "overline" or "none"." 2395 if (jQuery.inArray(node.tagName, ["A", "FONT", "S", "SPAN", "STRIKE", "U"]) != -1 && hasAttribute(node, "style") && getStyleLength(node) == 1 && (node.style.textDecoration == "line-through" || node.style.textDecoration == "underline" || node.style.textDecoration == "overline" || node.style.textDecoration == "none")) { 2396 return true; 2397 } 2398 2399 return false; 2400 } 2401 2402 // "Two quantities are equivalent values for a command if either both are null, 2403 // or both are strings and they're equal and the command does not define any 2404 // equivalent values, or both are strings and the command defines equivalent 2405 // values and they match the definition." 2406 function areEquivalentValues(command, val1, val2) { 2407 if (val1 === null && val2 === null) { 2408 return true; 2409 } 2410 2411 if (typeof val1 == "string" && typeof val2 == "string" && val1 == val2 && !(commands[command].hasOwnProperty("equivalentValues"))) { 2412 return true; 2413 } 2414 2415 if (typeof val1 == "string" && typeof val2 == "string" && commands[command].hasOwnProperty("equivalentValues") && commands[command].equivalentValues(val1, val2)) { 2416 return true; 2417 } 2418 2419 return false; 2420 } 2421 2422 // "Two quantities are loosely equivalent values for a command if either they 2423 // are equivalent values for the command, or if the command is the fontSize 2424 // command; one of the quantities is one of "xx-small", "small", "medium", 2425 // "large", "x-large", "xx-large", or "xxx-large"; and the other quantity is 2426 // the resolved value of "font-size" on a font element whose size attribute has 2427 // the corresponding value set ("1" through "7" respectively)." 2428 function areLooselyEquivalentValues(command, val1, val2) { 2429 if (areEquivalentValues(command, val1, val2)) { 2430 return true; 2431 } 2432 2433 if (command != "fontsize" || typeof val1 != "string" || typeof val2 != "string") { 2434 return false; 2435 } 2436 2437 // Static variables in JavaScript? 2438 var callee = areLooselyEquivalentValues; 2439 if (callee.sizeMap === undefined) { 2440 callee.sizeMap = {}; 2441 var font = document.createElement("font"); 2442 document.body.appendChild(font); 2443 $_(["xx-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"]).forEach(function (keyword) { 2444 font.size = cssSizeToLegacy(keyword); 2445 callee.sizeMap[keyword] = $_.getComputedStyle(font).fontSize; 2446 }); 2447 document.body.removeChild(font); 2448 } 2449 2450 return val1 === callee.sizeMap[val2] || val2 === callee.sizeMap[val1]; 2451 } 2452 2453 //@} 2454 ///// Assorted inline formatting command algorithms ///// 2455 //@{ 2456 2457 function getEffectiveCommandValue(node, command) { 2458 // "If neither node nor its parent is an Element, return null." 2459 if (node.nodeType != $_.Node.ELEMENT_NODE && (!node.parentNode || node.parentNode.nodeType != $_.Node.ELEMENT_NODE)) { 2460 return null; 2461 } 2462 2463 // "If node is not an Element, return the effective command value of its 2464 // parent for command." 2465 if (node.nodeType != $_.Node.ELEMENT_NODE) { 2466 return getEffectiveCommandValue(node.parentNode, command); 2467 } 2468 2469 // "If command is "createLink" or "unlink":" 2470 if (command == "createlink" || command == "unlink") { 2471 // "While node is not null, and is not an a element that has an href 2472 // attribute, set node to its parent." 2473 while (node && (!isAnyHtmlElement(node) || node.tagName != "A" || !hasAttribute(node, "href"))) { 2474 node = node.parentNode; 2475 } 2476 2477 // "If node is null, return null." 2478 if (!node) { 2479 return null; 2480 } 2481 2482 // "Return the value of node's href attribute." 2483 return node.getAttribute("href"); 2484 } 2485 2486 // "If command is "backColor" or "hiliteColor":" 2487 if (command == "backcolor" || command == "hilitecolor") { 2488 // "While the resolved value of "background-color" on node is any 2489 // fully transparent value, and node's parent is an Element, set 2490 // node to its parent." 2491 // 2492 // Another lame hack to avoid flawed APIs. 2493 while (($_.getComputedStyle(node).backgroundColor == "rgba(0, 0, 0, 0)" || $_.getComputedStyle(node).backgroundColor === "" || $_.getComputedStyle(node).backgroundColor == "transparent") && node.parentNode && node.parentNode.nodeType == $_.Node.ELEMENT_NODE) { 2494 node = node.parentNode; 2495 } 2496 2497 // "If the resolved value of "background-color" on node is a fully 2498 // transparent value, return "rgb(255, 255, 255)"." 2499 if ($_.getComputedStyle(node).backgroundColor == "rgba(0, 0, 0, 0)" || $_.getComputedStyle(node).backgroundColor === "" || $_.getComputedStyle(node).backgroundColor == "transparent") { 2500 return "rgb(255, 255, 255)"; 2501 } 2502 2503 // "Otherwise, return the resolved value of "background-color" for 2504 // node." 2505 return $_.getComputedStyle(node).backgroundColor; 2506 } 2507 2508 // "If command is "subscript" or "superscript":" 2509 if (command == "subscript" || command == "superscript") { 2510 // "Let affected by subscript and affected by superscript be two 2511 // boolean variables, both initially false." 2512 var affectedBySubscript = false; 2513 var affectedBySuperscript = false; 2514 2515 // "While node is an inline node:" 2516 while (isInlineNode(node)) { 2517 var verticalAlign = $_.getComputedStyle(node).verticalAlign; 2518 2519 // "If node is a sub, set affected by subscript to true." 2520 if (isNamedHtmlElement(node, 'sub')) { 2521 affectedBySubscript = true; 2522 // "Otherwise, if node is a sup, set affected by superscript to 2523 // true." 2524 } else if (isNamedHtmlElement(node, 'sup')) { 2525 affectedBySuperscript = true; 2526 } 2527 2528 // "Set node to its parent." 2529 node = node.parentNode; 2530 } 2531 2532 // "If affected by subscript and affected by superscript are both true, 2533 // return the string "mixed"." 2534 if (affectedBySubscript && affectedBySuperscript) { 2535 return "mixed"; 2536 } 2537 2538 // "If affected by subscript is true, return "subscript"." 2539 if (affectedBySubscript) { 2540 return "subscript"; 2541 } 2542 2543 // "If affected by superscript is true, return "superscript"." 2544 if (affectedBySuperscript) { 2545 return "superscript"; 2546 } 2547 2548 // "Return null." 2549 return null; 2550 } 2551 2552 // "If command is "strikethrough", and the "text-decoration" property of 2553 // node or any of its ancestors has resolved value containing 2554 // "line-through", return "line-through". Otherwise, return null." 2555 if (command == "strikethrough") { 2556 do { 2557 if ($_.getComputedStyle(node).textDecoration.indexOf("line-through") != -1) { 2558 return "line-through"; 2559 } 2560 node = node.parentNode; 2561 } while (node && node.nodeType == $_.Node.ELEMENT_NODE); 2562 return null; 2563 } 2564 2565 // "If command is "underline", and the "text-decoration" property of node 2566 // or any of its ancestors has resolved value containing "underline", 2567 // return "underline". Otherwise, return null." 2568 if (command == "underline") { 2569 do { 2570 if ($_.getComputedStyle(node).textDecoration.indexOf("underline") != -1) { 2571 return "underline"; 2572 } 2573 node = node.parentNode; 2574 } while (node && node.nodeType == $_.Node.ELEMENT_NODE); 2575 return null; 2576 } 2577 2578 if (!commands[command].hasOwnProperty("relevantCssProperty")) { 2579 throw "Bug: no relevantCssProperty for " + command + " in getEffectiveCommandValue"; 2580 } 2581 2582 // "Return the resolved value for node of the relevant CSS property for 2583 // command." 2584 return $_.getComputedStyle(node)[commands[command].relevantCssProperty].toString(); 2585 } 2586 2587 function getSpecifiedCommandValue(element, command) { 2588 // "If command is "backColor" or "hiliteColor" and element's display 2589 // property does not have resolved value "inline", return null." 2590 if ((command == "backcolor" || command == "hilitecolor") && $_.getComputedStyle(element).display != "inline") { 2591 return null; 2592 } 2593 2594 // "If command is "createLink" or "unlink":" 2595 if (command == "createlink" || command == "unlink") { 2596 // "If element is an a element and has an href attribute, return the 2597 // value of that attribute." 2598 if (isAnyHtmlElement(element) && element.tagName == "A" && hasAttribute(element, "href")) { 2599 return element.getAttribute("href"); 2600 } 2601 2602 // "Return null." 2603 return null; 2604 } 2605 2606 // "If command is "subscript" or "superscript":" 2607 if (command == "subscript" || command == "superscript") { 2608 // "If element is a sup, return "superscript"." 2609 if (isNamedHtmlElement(element, 'sup')) { 2610 return "superscript"; 2611 } 2612 2613 // "If element is a sub, return "subscript"." 2614 if (isNamedHtmlElement(element, 'sub')) { 2615 return "subscript"; 2616 } 2617 2618 // "Return null." 2619 return null; 2620 } 2621 2622 // "If command is "strikethrough", and element has a style attribute set, 2623 // and that attribute sets "text-decoration":" 2624 if (command == "strikethrough" && element.style.textDecoration != "") { 2625 // "If element's style attribute sets "text-decoration" to a value 2626 // containing "line-through", return "line-through"." 2627 if (element.style.textDecoration.indexOf("line-through") != -1) { 2628 return "line-through"; 2629 } 2630 2631 // "Return null." 2632 return null; 2633 } 2634 2635 // "If command is "strikethrough" and element is a s or strike element, 2636 // return "line-through"." 2637 if (command == "strikethrough" && isHtmlElementInArray(element, ["S", "STRIKE"])) { 2638 return "line-through"; 2639 } 2640 2641 // "If command is "underline", and element has a style attribute set, and 2642 // that attribute sets "text-decoration":" 2643 if (command == "underline" && element.style.textDecoration != "") { 2644 // "If element's style attribute sets "text-decoration" to a value 2645 // containing "underline", return "underline"." 2646 if (element.style.textDecoration.indexOf("underline") != -1) { 2647 return "underline"; 2648 } 2649 2650 // "Return null." 2651 return null; 2652 } 2653 2654 // "If command is "underline" and element is a u element, return 2655 // "underline"." 2656 if (command == "underline" && isNamedHtmlElement(element, 'U')) { 2657 return "underline"; 2658 } 2659 2660 // "Let property be the relevant CSS property for command." 2661 var property = commands[command].relevantCssProperty; 2662 2663 // "If property is null, return null." 2664 if (property === null) { 2665 return null; 2666 } 2667 2668 // "If element has a style attribute set, and that attribute has the 2669 // effect of setting property, return the value that it sets property to." 2670 if (element.style[property] != "") { 2671 return element.style[property]; 2672 } 2673 2674 // "If element is a font element that has an attribute whose effect is 2675 // to create a presentational hint for property, return the value that the 2676 // hint sets property to. (For a size of 7, this will be the non-CSS value 2677 // "xxx-large".)" 2678 if (isHtmlNamespace(element.namespaceURI) && element.tagName == "FONT") { 2679 if (property == "color" && hasAttribute(element, "color")) { 2680 return element.color; 2681 } 2682 if (property == "fontFamily" && hasAttribute(element, "face")) { 2683 return element.face; 2684 } 2685 if (property == "fontSize" && hasAttribute(element, "size")) { 2686 // This is not even close to correct in general. 2687 var size = parseInt(element.size, 10); 2688 if (size < 1) { 2689 size = 1; 2690 } 2691 if (size > 7) { 2692 size = 7; 2693 } 2694 return { 2695 1: "xx-small", 2696 2: "small", 2697 3: "medium", 2698 4: "large", 2699 5: "x-large", 2700 6: "xx-large", 2701 7: "xxx-large" 2702 }[size]; 2703 } 2704 } 2705 2706 // "If element is in the following list, and property is equal to the 2707 // CSS property name listed for it, return the string listed for it." 2708 // 2709 // A list follows, whose meaning is copied here. 2710 if (property == "fontWeight" && (element.tagName == "B" || element.tagName == "STRONG")) { 2711 return "bold"; 2712 } 2713 if (property == "fontStyle" && (element.tagName == "I" || element.tagName == "EM")) { 2714 return "italic"; 2715 } 2716 2717 // "Return null." 2718 return null; 2719 } 2720 2721 function reorderModifiableDescendants(node, command, newValue, range) { 2722 // "Let candidate equal node." 2723 var candidate = node; 2724 2725 // "While candidate is a modifiable element, and candidate has exactly one 2726 // child, and that child is also a modifiable element, and candidate is not 2727 // a simple modifiable element or candidate's specified command value for 2728 // command is not equivalent to new value, set candidate to its child." 2729 while (isModifiableElement(candidate) && candidate.childNodes.length == 1 && isModifiableElement(candidate.firstChild) && (!isSimpleModifiableElement(candidate) || !areEquivalentValues(command, getSpecifiedCommandValue(candidate, command), newValue))) { 2730 candidate = candidate.firstChild; 2731 } 2732 2733 // "If candidate is node, or is not a simple modifiable element, or its 2734 // specified command value is not equivalent to new value, or its effective 2735 // command value is not loosely equivalent to new value, abort these 2736 // steps." 2737 if (candidate == node || !isSimpleModifiableElement(candidate) || !areEquivalentValues(command, getSpecifiedCommandValue(candidate, command), newValue) || !areLooselyEquivalentValues(command, getEffectiveCommandValue(candidate, command), newValue)) { 2738 return; 2739 } 2740 2741 // "While candidate has children, insert the first child of candidate into 2742 // candidate's parent immediately before candidate, preserving ranges." 2743 while (candidate.hasChildNodes()) { 2744 movePreservingRanges(candidate.firstChild, candidate.parentNode, getNodeIndex(candidate), range); 2745 } 2746 2747 // "Insert candidate into node's parent immediately after node." 2748 node.parentNode.insertBefore(candidate, node.nextSibling); 2749 2750 // "Append the node as the last child of candidate, preserving ranges." 2751 movePreservingRanges(node, candidate, -1, range); 2752 } 2753 2754 var recordValuesCommands = ["subscript", "bold", "fontname", "fontsize", "forecolor", "hilitecolor", "italic", "strikethrough", "underline"]; 2755 2756 function recordValues(nodeList) { 2757 // "Let values be a list of (node, command, specified command value) 2758 // triples, initially empty." 2759 var values = []; 2760 2761 // "For each node in node list, for each command in the list "subscript", 2762 // "bold", "fontName", "fontSize", "foreColor", "hiliteColor", "italic", 2763 // "strikethrough", and "underline" in that order:" 2764 2765 // Ensure we have a plain array to avoid the potential performance 2766 // overhead of a NodeList 2767 var nodes = jQuery.makeArray(nodeList); 2768 var i, j; 2769 var node; 2770 var command; 2771 var ancestor; 2772 var specifiedCommandValue; 2773 for (i = 0; i < nodes.length; i++) { 2774 node = nodes[i]; 2775 for (j = 0; j < recordValuesCommands.length; j++) { 2776 command = recordValuesCommands[j]; 2777 2778 // "Let ancestor equal node." 2779 ancestor = node; 2780 2781 // "If ancestor is not an Element, set it to its parent." 2782 if (ancestor.nodeType != 1) { 2783 ancestor = ancestor.parentNode; 2784 } 2785 2786 // "While ancestor is an Element and its specified command value 2787 // for command is null, set it to its parent." 2788 specifiedCommandValue = null; 2789 while (ancestor && ancestor.nodeType == 1 && (specifiedCommandValue = getSpecifiedCommandValue(ancestor, command)) === null) { 2790 ancestor = ancestor.parentNode; 2791 } 2792 2793 // "If ancestor is an Element, add (node, command, ancestor's 2794 // specified command value for command) to values. Otherwise add 2795 // (node, command, null) to values." 2796 values.push([node, command, specifiedCommandValue]); 2797 } 2798 } 2799 2800 // "Return values." 2801 return values; 2802 } 2803 2804 //@} 2805 ///// Clearing an element's value ///// 2806 //@{ 2807 2808 function clearValue(element, command, range) { 2809 // "If element is not editable, return the empty list." 2810 if (!isEditable(element)) { 2811 return []; 2812 } 2813 2814 // "If element's specified command value for command is null, return the 2815 // empty list." 2816 if (getSpecifiedCommandValue(element, command) === null) { 2817 return []; 2818 } 2819 2820 // "If element is a simple modifiable element:" 2821 if (isSimpleModifiableElement(element)) { 2822 // "Let children be the children of element." 2823 var children = Array.prototype.slice.call(toArray(element.childNodes)); 2824 2825 // "For each child in children, insert child into element's parent 2826 // immediately before element, preserving ranges." 2827 var i; 2828 for (i = 0; i < children.length; i++) { 2829 movePreservingRanges(children[i], element.parentNode, getNodeIndex(element), range); 2830 } 2831 2832 // "Remove element from its parent." 2833 element.parentNode.removeChild(element); 2834 2835 // "Return children." 2836 return children; 2837 } 2838 2839 // "If command is "strikethrough", and element has a style attribute that 2840 // sets "text-decoration" to some value containing "line-through", delete 2841 // "line-through" from the value." 2842 if (command == "strikethrough" && element.style.textDecoration.indexOf("line-through") != -1) { 2843 if (element.style.textDecoration == "line-through") { 2844 element.style.textDecoration = ""; 2845 } else { 2846 element.style.textDecoration = element.style.textDecoration.replace("line-through", ""); 2847 } 2848 if (element.getAttribute("style") == "") { 2849 element.removeAttribute("style"); 2850 } 2851 } 2852 2853 // "If command is "underline", and element has a style attribute that sets 2854 // "text-decoration" to some value containing "underline", delete 2855 // "underline" from the value." 2856 if (command == "underline" && element.style.textDecoration.indexOf("underline") != -1) { 2857 if (element.style.textDecoration == "underline") { 2858 element.style.textDecoration = ""; 2859 } else { 2860 element.style.textDecoration = element.style.textDecoration.replace("underline", ""); 2861 } 2862 if (element.getAttribute("style") == "") { 2863 element.removeAttribute("style"); 2864 } 2865 } 2866 2867 // "If the relevant CSS property for command is not null, unset the CSS 2868 // property property of element." 2869 if (commands[command].relevantCssProperty !== null) { 2870 element.style[commands[command].relevantCssProperty] = ''; 2871 if (element.getAttribute("style") == "") { 2872 element.removeAttribute("style"); 2873 } 2874 } 2875 2876 // "If element is a font element:" 2877 if (isHtmlNamespace(element.namespaceURI) && element.tagName == "FONT") { 2878 // "If command is "foreColor", unset element's color attribute, if set." 2879 if (command == "forecolor") { 2880 element.removeAttribute("color"); 2881 } 2882 2883 // "If command is "fontName", unset element's face attribute, if set." 2884 if (command == "fontname") { 2885 element.removeAttribute("face"); 2886 } 2887 2888 // "If command is "fontSize", unset element's size attribute, if set." 2889 if (command == "fontsize") { 2890 element.removeAttribute("size"); 2891 } 2892 } 2893 2894 // "If element is an a element and command is "createLink" or "unlink", 2895 // unset the href property of element." 2896 if (isNamedHtmlElement(element, 'A') && (command == "createlink" || command == "unlink")) { 2897 element.removeAttribute("href"); 2898 } 2899 2900 // "If element's specified command value for command is null, return the 2901 // empty list." 2902 if (getSpecifiedCommandValue(element, command) === null) { 2903 return []; 2904 } 2905 2906 // "Set the tag name of element to "span", and return the one-node list 2907 // consisting of the result." 2908 return [setTagName(element, "span", range)]; 2909 } 2910 2911 //@} 2912 ///// Forcing the value of a node ///// 2913 //@{ 2914 2915 function forceValue(node, command, newValue, range) { 2916 var children = []; 2917 var i; 2918 var specifiedValue; 2919 2920 // "If node's parent is null, abort this algorithm." 2921 if (!node.parentNode) { 2922 return; 2923 } 2924 2925 // "If new value is null, abort this algorithm." 2926 if (newValue === null) { 2927 return; 2928 } 2929 2930 // "If node is an allowed child of "span":" 2931 if (isAllowedChild(node, "span")) { 2932 // "Reorder modifiable descendants of node's previousSibling." 2933 reorderModifiableDescendants(node.previousSibling, command, newValue, range); 2934 2935 // "Reorder modifiable descendants of node's nextSibling." 2936 reorderModifiableDescendants(node.nextSibling, command, newValue, range); 2937 2938 // "Wrap the one-node list consisting of node, with sibling criteria 2939 // returning true for a simple modifiable element whose specified 2940 // command value is equivalent to new value and whose effective command 2941 // value is loosely equivalent to new value and false otherwise, and 2942 // with new parent instructions returning null." 2943 wrap( 2944 [node], 2945 function (node) { 2946 return isSimpleModifiableElement(node) && areEquivalentValues(command, getSpecifiedCommandValue(node, command), newValue) && areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue); 2947 }, 2948 function () { 2949 return null; 2950 }, 2951 range 2952 ); 2953 } 2954 2955 // "If the effective command value of command is loosely equivalent to new 2956 // value on node, abort this algorithm." 2957 if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) { 2958 return; 2959 } 2960 2961 // "If node is not an allowed child of "span":" 2962 if (!isAllowedChild(node, "span")) { 2963 // "Let children be all children of node, omitting any that are 2964 // Elements whose specified command value for command is neither null 2965 // nor equivalent to new value." 2966 for (i = 0; i < node.childNodes.length; i++) { 2967 if (node.childNodes[i].nodeType == $_.Node.ELEMENT_NODE) { 2968 specifiedValue = getSpecifiedCommandValue(node.childNodes[i], command); 2969 2970 if (specifiedValue !== null && !areEquivalentValues(command, newValue, specifiedValue)) { 2971 continue; 2972 } 2973 } 2974 children.push(node.childNodes[i]); 2975 } 2976 2977 // "Force the value of each Node in children, with command and new 2978 // value as in this invocation of the algorithm." 2979 for (i = 0; i < children.length; i++) { 2980 forceValue(children[i], command, newValue, range); 2981 } 2982 2983 // "Abort this algorithm." 2984 return; 2985 } 2986 2987 // "If the effective command value of command is loosely equivalent to new 2988 // value on node, abort this algorithm." 2989 if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) { 2990 return; 2991 } 2992 2993 // "Let new parent be null." 2994 var newParent = null; 2995 2996 // "If the CSS styling flag is false:" 2997 if (!cssStylingFlag) { 2998 // "If command is "bold" and new value is "bold", let new parent be the 2999 // result of calling createElement("b") on the ownerDocument of node." 3000 if (command == "bold" && (newValue == "bold" || newValue == "700")) { 3001 newParent = node.ownerDocument.createElement("b"); 3002 } 3003 3004 // "If command is "italic" and new value is "italic", let new parent be 3005 // the result of calling createElement("i") on the ownerDocument of 3006 // node." 3007 if (command == "italic" && newValue == "italic") { 3008 newParent = node.ownerDocument.createElement("i"); 3009 } 3010 3011 // "If command is "strikethrough" and new value is "line-through", let 3012 // new parent be the result of calling createElement("s") on the 3013 // ownerDocument of node." 3014 if (command == "strikethrough" && newValue == "line-through") { 3015 newParent = node.ownerDocument.createElement("s"); 3016 } 3017 3018 // "If command is "underline" and new value is "underline", let new 3019 // parent be the result of calling createElement("u") on the 3020 // ownerDocument of node." 3021 if (command == "underline" && newValue == "underline") { 3022 newParent = node.ownerDocument.createElement("u"); 3023 } 3024 3025 // "If command is "foreColor", and new value is fully opaque with red, 3026 // green, and blue components in the range 0 to 255:" 3027 if (command == "forecolor" && parseSimpleColor(newValue)) { 3028 // "Let new parent be the result of calling createElement("span") 3029 // on the ownerDocument of node." 3030 // NOTE: modified this process to create span elements with style attributes 3031 // instead of oldschool font tags with color attributes 3032 newParent = node.ownerDocument.createElement("span"); 3033 3034 // "If new value is an extended color keyword, set the color 3035 // attribute of new parent to new value." 3036 // 3037 // "Otherwise, set the color attribute of new parent to the result 3038 // of applying the rules for serializing simple color values to new 3039 // value (interpreted as a simple color)." 3040 jQuery(newParent).css('color', parseSimpleColor(newValue)); 3041 } 3042 3043 // "If command is "fontName", let new parent be the result of calling 3044 // createElement("font") on the ownerDocument of node, then set the 3045 // face attribute of new parent to new value." 3046 if (command == "fontname") { 3047 newParent = node.ownerDocument.createElement("font"); 3048 newParent.face = newValue; 3049 } 3050 } 3051 3052 // "If command is "createLink" or "unlink":" 3053 if (command == "createlink" || command == "unlink") { 3054 // "Let new parent be the result of calling createElement("a") on the 3055 // ownerDocument of node." 3056 newParent = node.ownerDocument.createElement("a"); 3057 3058 // "Set the href attribute of new parent to new value." 3059 newParent.setAttribute("href", newValue); 3060 3061 // "Let ancestor be node's parent." 3062 var ancestor = node.parentNode; 3063 3064 // "While ancestor is not null:" 3065 while (ancestor) { 3066 // "If ancestor is an a, set the tag name of ancestor to "span", 3067 // and let ancestor be the result." 3068 if (isNamedHtmlElement(ancestor, 'A')) { 3069 ancestor = setTagName(ancestor, "span", range); 3070 } 3071 3072 // "Set ancestor to its parent." 3073 ancestor = ancestor.parentNode; 3074 } 3075 } 3076 3077 // "If command is "fontSize"; and new value is one of "xx-small", "small", 3078 // "medium", "large", "x-large", "xx-large", or "xxx-large"; and either the 3079 // CSS styling flag is false, or new value is "xxx-large": let new parent 3080 // be the result of calling createElement("font") on the ownerDocument of 3081 // node, then set the size attribute of new parent to the number from the 3082 // following table based on new value: [table omitted]" 3083 if (command == "fontsize" && jQuery.inArray(newValue, ["xx-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"]) != -1 && (!cssStylingFlag || newValue == "xxx-large")) { 3084 newParent = node.ownerDocument.createElement("font"); 3085 newParent.size = cssSizeToLegacy(newValue); 3086 } 3087 3088 // "If command is "subscript" or "superscript" and new value is 3089 // "subscript", let new parent be the result of calling 3090 // createElement("sub") on the ownerDocument of node." 3091 if ((command == "subscript" || command == "superscript") && newValue == "subscript") { 3092 newParent = node.ownerDocument.createElement("sub"); 3093 } 3094 3095 // "If command is "subscript" or "superscript" and new value is 3096 // "superscript", let new parent be the result of calling 3097 // createElement("sup") on the ownerDocument of node." 3098 if ((command == "subscript" || command == "superscript") && newValue == "superscript") { 3099 newParent = node.ownerDocument.createElement("sup"); 3100 } 3101 3102 // "If new parent is null, let new parent be the result of calling 3103 // createElement("span") on the ownerDocument of node." 3104 if (!newParent) { 3105 newParent = node.ownerDocument.createElement("span"); 3106 } 3107 3108 // "Insert new parent in node's parent before node." 3109 node.parentNode.insertBefore(newParent, node); 3110 3111 // "If the effective command value of command for new parent is not loosely 3112 // equivalent to new value, and the relevant CSS property for command is 3113 // not null, set that CSS property of new parent to new value (if the new 3114 // value would be valid)." 3115 var property = commands[command].relevantCssProperty; 3116 if (property !== null && !areLooselyEquivalentValues(command, getEffectiveCommandValue(newParent, command), newValue)) { 3117 newParent.style[property] = newValue; 3118 } 3119 3120 // "If command is "strikethrough", and new value is "line-through", and the 3121 // effective command value of "strikethrough" for new parent is not 3122 // "line-through", set the "text-decoration" property of new parent to 3123 // "line-through"." 3124 if (command == "strikethrough" && newValue == "line-through" && getEffectiveCommandValue(newParent, "strikethrough") != "line-through") { 3125 newParent.style.textDecoration = "line-through"; 3126 } 3127 3128 // "If command is "underline", and new value is "underline", and the 3129 // effective command value of "underline" for new parent is not 3130 // "underline", set the "text-decoration" property of new parent to 3131 // "underline"." 3132 if (command == "underline" && newValue == "underline" && getEffectiveCommandValue(newParent, "underline") != "underline") { 3133 newParent.style.textDecoration = "underline"; 3134 } 3135 3136 // "Append node to new parent as its last child, preserving ranges." 3137 movePreservingRanges(node, newParent, newParent.childNodes.length, range); 3138 3139 // "If node is an Element and the effective command value of command for 3140 // node is not loosely equivalent to new value:" 3141 if (node.nodeType == $_.Node.ELEMENT_NODE && !areEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) { 3142 // "Insert node into the parent of new parent before new parent, 3143 3144 // preserving ranges." 3145 movePreservingRanges(node, newParent.parentNode, getNodeIndex(newParent), range); 3146 3147 // "Remove new parent from its parent." 3148 newParent.parentNode.removeChild(newParent); 3149 3150 // "Let children be all children of node, omitting any that are 3151 // Elements whose specified command value for command is neither null 3152 // nor equivalent to new value." 3153 children = []; 3154 for (i = 0; i < node.childNodes.length; i++) { 3155 if (node.childNodes[i].nodeType == $_.Node.ELEMENT_NODE) { 3156 specifiedValue = getSpecifiedCommandValue(node.childNodes[i], command); 3157 3158 if (specifiedValue !== null && !areEquivalentValues(command, newValue, specifiedValue)) { 3159 continue; 3160 } 3161 } 3162 children.push(node.childNodes[i]); 3163 } 3164 3165 // "Force the value of each Node in children, with command and new 3166 // value as in this invocation of the algorithm." 3167 for (i = 0; i < children.length; i++) { 3168 forceValue(children[i], command, newValue, range); 3169 } 3170 } 3171 } 3172 3173 //@} 3174 ///// Pushing down values ///// 3175 //@{ 3176 3177 function pushDownValues(node, command, newValue, range) { 3178 // "If node's parent is not an Element, abort this algorithm." 3179 if (!node.parentNode || node.parentNode.nodeType != $_.Node.ELEMENT_NODE) { 3180 return; 3181 } 3182 3183 // "If the effective command value of command is loosely equivalent to new 3184 // value on node, abort this algorithm." 3185 if (areLooselyEquivalentValues(command, getEffectiveCommandValue(node, command), newValue)) { 3186 return; 3187 } 3188 3189 // "Let current ancestor be node's parent." 3190 var currentAncestor = node.parentNode; 3191 3192 // "Let ancestor list be a list of Nodes, initially empty." 3193 var ancestorList = []; 3194 3195 // "While current ancestor is an editable Element and the effective command 3196 // value of command is not loosely equivalent to new value on it, append 3197 // current ancestor to ancestor list, then set current ancestor to its 3198 // parent." 3199 while (isEditable(currentAncestor) && currentAncestor.nodeType == $_.Node.ELEMENT_NODE && !areLooselyEquivalentValues(command, getEffectiveCommandValue(currentAncestor, command), newValue)) { 3200 ancestorList.push(currentAncestor); 3201 currentAncestor = currentAncestor.parentNode; 3202 } 3203 3204 // "If ancestor list is empty, abort this algorithm." 3205 if (!ancestorList.length) { 3206 return; 3207 } 3208 3209 // "Let propagated value be the specified command value of command on the 3210 // last member of ancestor list." 3211 var propagatedValue = getSpecifiedCommandValue(ancestorList[ancestorList.length - 1], command); 3212 3213 // "If propagated value is null and is not equal to new value, abort this 3214 // algorithm." 3215 if (propagatedValue === null && propagatedValue != newValue) { 3216 return; 3217 } 3218 3219 // "If the effective command value for the parent of the last member of 3220 // ancestor list is not loosely equivalent to new value, and new value is 3221 // not null, abort this algorithm." 3222 if (newValue !== null && !areLooselyEquivalentValues(command, getEffectiveCommandValue(ancestorList[ancestorList.length - 1].parentNode, command), newValue)) { 3223 return; 3224 } 3225 3226 // "While ancestor list is not empty:" 3227 while (ancestorList.length) { 3228 // "Let current ancestor be the last member of ancestor list." 3229 // "Remove the last member from ancestor list." 3230 currentAncestor = ancestorList.pop(); 3231 3232 // "If the specified command value of current ancestor for command is 3233 // not null, set propagated value to that value." 3234 if (getSpecifiedCommandValue(currentAncestor, command) !== null) { 3235 propagatedValue = getSpecifiedCommandValue(currentAncestor, command); 3236 } 3237 3238 // "Let children be the children of current ancestor." 3239 var children = Array.prototype.slice.call(toArray(currentAncestor.childNodes)); 3240 3241 // "If the specified command value of current ancestor for command is 3242 // not null, clear the value of current ancestor." 3243 if (getSpecifiedCommandValue(currentAncestor, command) !== null) { 3244 clearValue(currentAncestor, command, range); 3245 } 3246 3247 // "For every child in children:" 3248 var i; 3249 for (i = 0; i < children.length; i++) { 3250 var child = children[i]; 3251 3252 // "If child is node, continue with the next child." 3253 if (child == node) { 3254 continue; 3255 } 3256 3257 // "If child is an Element whose specified command value for 3258 // command is neither null nor equivalent to propagated value, 3259 // continue with the next child." 3260 if (child.nodeType == $_.Node.ELEMENT_NODE && getSpecifiedCommandValue(child, command) !== null && !areEquivalentValues(command, propagatedValue, getSpecifiedCommandValue(child, command))) { 3261 continue; 3262 } 3263 3264 // "If child is the last member of ancestor list, continue with the 3265 // next child." 3266 if (child == ancestorList[ancestorList.length - 1]) { 3267 continue; 3268 } 3269 3270 // "Force the value of child, with command as in this algorithm 3271 // and new value equal to propagated value." 3272 forceValue(child, command, propagatedValue, range); 3273 } 3274 } 3275 } 3276 3277 3278 function restoreValues(values, range) { 3279 // "For each (node, command, value) triple in values:" 3280 $_(values).forEach(function (triple) { 3281 var node = triple[0]; 3282 var command = triple[1]; 3283 var value = triple[2]; 3284 3285 // "Let ancestor equal node." 3286 var ancestor = node; 3287 3288 // "If ancestor is not an Element, set it to its parent." 3289 if (!ancestor || ancestor.nodeType != $_.Node.ELEMENT_NODE) { 3290 ancestor = ancestor.parentNode; 3291 } 3292 3293 // "While ancestor is an Element and its specified command value for 3294 // command is null, set it to its parent." 3295 while (ancestor && ancestor.nodeType == $_.Node.ELEMENT_NODE && getSpecifiedCommandValue(ancestor, command) === null) { 3296 ancestor = ancestor.parentNode; 3297 } 3298 3299 // "If value is null and ancestor is an Element, push down values on 3300 // node for command, with new value null." 3301 if (value === null && ancestor && ancestor.nodeType == $_.Node.ELEMENT_NODE) { 3302 pushDownValues(node, command, null, range); 3303 3304 // "Otherwise, if ancestor is an Element and its specified command 3305 // value for command is not equivalent to value, or if ancestor is not 3306 // an Element and value is not null, force the value of command to 3307 // value on node." 3308 } else if ((ancestor && ancestor.nodeType == $_.Node.ELEMENT_NODE && !areEquivalentValues(command, getSpecifiedCommandValue(ancestor, command), value)) || ((!ancestor || ancestor.nodeType != $_.Node.ELEMENT_NODE) && value !== null)) { 3309 forceValue(node, command, value, range); 3310 } 3311 }); 3312 } 3313 3314 //@} 3315 ///// Setting the selection's value ///// 3316 //@{ 3317 3318 function setSelectionValue(command, newValue, range) { 3319 3320 // Use current selected range if no range passed 3321 range = range || getActiveRange(); 3322 3323 // "If there is no editable text node effectively contained in the active 3324 // range:" 3325 if (!$_(getAllEffectivelyContainedNodes(range)).filter(function (node) { return node.nodeType == $_.Node.TEXT_NODE; }, true).some(isEditable)) { 3326 // "If command has inline command activated values, set the state 3327 // override to true if new value is among them and false if it's not." 3328 if (commands[command].hasOwnProperty("inlineCommandActivatedValues")) { 3329 setStateOverride( 3330 command, 3331 $_(commands[command].inlineCommandActivatedValues).indexOf(newValue) != -1, 3332 range 3333 ); 3334 } 3335 3336 // "If command is "subscript", unset the state override for 3337 // "superscript"." 3338 if (command == "subscript") { 3339 unsetStateOverride("superscript", range); 3340 } 3341 3342 // "If command is "superscript", unset the state override for 3343 // "subscript"." 3344 if (command == "superscript") { 3345 unsetStateOverride("subscript", range); 3346 } 3347 3348 // "If new value is null, unset the value override (if any)." 3349 if (newValue === null) { 3350 unsetValueOverride(command, range); 3351 3352 // "Otherwise, if command has a value specified, set the value override 3353 // to new value." 3354 } else if (commands[command].hasOwnProperty("value")) { 3355 setValueOverride(command, newValue, range); 3356 } 3357 3358 // "Abort these steps." 3359 return; 3360 } 3361 3362 // "If the active range's start node is an editable Text node, and its 3363 // start offset is neither zero nor its start node's length, call 3364 // splitText() on the active range's start node, with argument equal to the 3365 // active range's start offset. Then set the active range's start node to 3366 // the result, and its start offset to zero." 3367 if (isEditable(range.startContainer) && range.startContainer.nodeType == $_.Node.TEXT_NODE && range.startOffset != 0 && range.startOffset != getNodeLength(range.startContainer)) { 3368 // Account for browsers not following range mutation rules 3369 var newNode = range.startContainer.splitText(range.startOffset); 3370 var newActiveRange = Aloha.createRange(); 3371 if (range.startContainer == range.endContainer) { 3372 var newEndOffset = range.endOffset - range.startOffset; 3373 newActiveRange.setEnd(newNode, newEndOffset); 3374 range.setEnd(newNode, newEndOffset); 3375 } 3376 newActiveRange.setStart(newNode, 0); 3377 Aloha.getSelection().removeAllRanges(); 3378 Aloha.getSelection().addRange(newActiveRange); 3379 3380 range.setStart(newNode, 0); 3381 } 3382 3383 // "If the active range's end node is an editable Text node, and its end 3384 // offset is neither zero nor its end node's length, call splitText() on 3385 // the active range's end node, with argument equal to the active range's 3386 // end offset." 3387 if (isEditable(range.endContainer) && range.endContainer.nodeType == $_.Node.TEXT_NODE && range.endOffset != 0 && range.endOffset != getNodeLength(range.endContainer)) { 3388 // IE seems to mutate the range incorrectly here, so we need correction 3389 // here as well. The active range will be temporarily in orphaned 3390 // nodes, so calling getActiveRange() after splitText() but before 3391 // fixing the range will throw an exception. 3392 // TODO: check if this is still neccessary 3393 var activeRange = range; 3394 var newStart = [activeRange.startContainer, activeRange.startOffset]; 3395 var newEnd = [activeRange.endContainer, activeRange.endOffset]; 3396 activeRange.endContainer.splitText(activeRange.endOffset); 3397 activeRange.setStart(newStart[0], newStart[1]); 3398 activeRange.setEnd(newEnd[0], newEnd[1]); 3399 3400 Aloha.getSelection().removeAllRanges(); 3401 Aloha.getSelection().addRange(activeRange); 3402 } 3403 3404 // "Let element list be all editable Elements effectively contained in the 3405 // active range. 3406 // 3407 // "For each element in element list, clear the value of element." 3408 $_(getAllEffectivelyContainedNodes(getActiveRange(), function (node) { 3409 return isEditable(node) && node.nodeType == $_.Node.ELEMENT_NODE; 3410 })).forEach(function (element) { 3411 clearValue(element, command, range); 3412 }); 3413 3414 // "Let node list be all editable nodes effectively contained in the active 3415 // range. 3416 3417 // 3418 // "For each node in node list:" 3419 $_(getAllEffectivelyContainedNodes(range, isEditable)).forEach(function (node) { 3420 // "Push down values on node." 3421 pushDownValues(node, command, newValue, range); 3422 3423 // "Force the value of node." 3424 forceValue(node, command, newValue, range); 3425 }); 3426 } 3427 3428 /** 3429 * attempt to retrieve a block like a table or an Aloha Block 3430 * which is located one step right of the current caret position. 3431 * If an appropriate element is found it will be returned or 3432 * false otherwise 3433 * 3434 * @param {element} node current node we're in 3435 * @param {number} offset current offset within that node 3436 * 3437 * @return the dom node if found or false if no appropriate 3438 * element was found 3439 */ 3440 function getBlockAtNextPosition(node, offset) { 3441 var i; 3442 3443 // if we're inside a text node we first have to check 3444 // if there is nothing but tabs, newlines or the like 3445 // after our current cursor position 3446 if (node.nodeType === $_.Node.TEXT_NODE && offset < node.length) { 3447 for (i = offset; i < node.length; i++) { 3448 if ((node.data.charAt(i) !== '\t' && node.data.charAt(i) !== '\r' && node.data.charAt(i) !== '\n') || node.data.charCodeAt(i) === 160) { // 3449 // this is a character that has to be deleted first 3450 return false; 3451 } 3452 } 3453 } 3454 3455 // try the most simple approach first: the next sibling 3456 // is a table 3457 if (node.nextSibling && node.nextSibling.className && node.nextSibling.className.indexOf("aloha-table-wrapper") >= 0) { 3458 return node.nextSibling; 3459 } 3460 3461 // since we got only ignorable whitespace here determine if 3462 // our nodes parents next sibling is a table 3463 if (node.parentNode && node.parentNode.nextSibling && node.parentNode.nextSibling.className && node.parentNode.nextSibling.className.indexOf("aloha-table-wrapper") >= 0) { 3464 return node.parentNode.nextSibling; 3465 } 3466 3467 // our parents nextsibling is a pure whitespace node such as 3468 // generated by sourcecode indentation so we'll check for 3469 // the next next sibling 3470 if (node.parentNode && node.parentNode.nextSibling && isWhitespaceNode(node.parentNode.nextSibling) && node.parentNode.nextSibling.nextSibling && node.parentNode.nextSibling.nextSibling.className && node.parentNode.nextSibling.nextSibling.className.indexOf("aloha-table-wrapper") >= 0) { 3471 return node.parentNode.nextSibling.nextSibling; 3472 } 3473 3474 // Note: the search above works for tables, since they cannot be 3475 // nested deeply in paragraphs and other formatting tags. If this code 3476 // is extended to work also for other blocks, the search probably needs to be adapted 3477 } 3478 3479 /** 3480 * Attempt to retrieve a block like a table or an Aloha Block 3481 * which is located right before the current position. 3482 * If an appropriate element is found, it will be returned or 3483 * false otherwise 3484 * 3485 * @param {element} node current node 3486 * @param {offset} offset current offset 3487 * 3488 * @return dom node of found or false if no appropriate 3489 * element was found 3490 */ 3491 function getBlockAtPreviousPosition(node, offset) { 3492 var i; 3493 3494 if (node.nodeType === $_.Node.TEXT_NODE && offset > 0) { 3495 for (i = offset - 1; i >= 0; i--) { 3496 if ((node.data.charAt(i) !== '\t' && node.data.charAt(i) !== '\r' && node.data.charAt(i) !== '\n') || node.data.charCodeAt(i) === 160) { // 3497 // this is a character that has to be deleted first 3498 return false; 3499 } 3500 } 3501 3502 } 3503 3504 // try the previous sibling 3505 if (node.previousSibling && node.previousSibling.className && node.previousSibling.className.indexOf("aloha-table-wrapper") >= 0) { 3506 return node.previousSibling; 3507 } 3508 3509 // try the parent's previous sibling 3510 if (node.parentNode && node.parentNode.previousSibling && node.parentNode.previousSibling.className && node.parentNode.previousSibling.className.indexOf("aloha-table-wrapper") >= 0) { 3511 return node.parentNode.previousSibling; 3512 } 3513 3514 // the parent's previous sibling might be a whitespace node 3515 if (node.parentNode && node.parentNode.previousSibling && isWhitespaceNode(node.parentNode.previousSibling) && node.parentNode.previousSibling.previousSibling && node.parentNode.previousSibling.previousSibling.className && node.parentNode.previousSibling.previousSibling.className.indexOf('aloha-table-wrapper') >= 0) { 3516 return node.parentNode.previousSibling.previousSibling; 3517 } 3518 3519 // Note: the search above works for tables, since they cannot be 3520 // nested deeply in paragraphs and other formatting tags. If this code 3521 // is extended to work also for other blocks, the search probably needs to be adapted 3522 3523 return false; 3524 } 3525 3526 // "A boundary point (node, offset) is a block start point if either node's 3527 // parent is null and offset is zero; or node has a child with index offset − 3528 // 1, and that child is either a visible block node or a visible br." 3529 function isBlockStartPoint(node, offset) { 3530 return (!node.parentNode && offset == 0) || (0 <= offset - 1 && offset - 1 < node.childNodes.length && isVisible(node.childNodes[offset - 1]) && (isBlockNode(node.childNodes[offset - 1]) || isNamedHtmlElement(node.childNodes[offset - 1], "br"))); 3531 } 3532 3533 // "A boundary point (node, offset) is a block end point if either node's 3534 // parent is null and offset is node's length; or node has a child with index 3535 // offset, and that child is a visible block node." 3536 function isBlockEndPoint(node, offset) { 3537 return (!node.parentNode && offset == getNodeLength(node)) || (offset < node.childNodes.length && isVisible(node.childNodes[offset]) && isBlockNode(node.childNodes[offset])); 3538 } 3539 3540 // "A boundary point is a block boundary point if it is either a block start 3541 // point or a block end point." 3542 function isBlockBoundaryPoint(node, offset) { 3543 return isBlockStartPoint(node, offset) || isBlockEndPoint(node, offset); 3544 } 3545 3546 function followsLineBreak(node) { 3547 // "Let offset be zero." 3548 var offset = 0; 3549 3550 // "While (node, offset) is not a block boundary point:" 3551 while (!isBlockBoundaryPoint(node, offset)) { 3552 // "If node has a visible child with index offset minus one, return 3553 // false." 3554 if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isVisible(node.childNodes[offset - 1])) { 3555 return false; 3556 } 3557 3558 // "If offset is zero or node has no children, set offset to node's 3559 // index, then set node to its parent." 3560 if (offset == 0 || !node.hasChildNodes()) { 3561 offset = getNodeIndex(node); 3562 node = node.parentNode; 3563 3564 // "Otherwise, set node to its child with index offset minus one, then 3565 // set offset to node's length." 3566 } else { 3567 node = node.childNodes[offset - 1]; 3568 offset = getNodeLength(node); 3569 } 3570 } 3571 3572 // "Return true." 3573 return true; 3574 } 3575 3576 function precedesLineBreak(node) { 3577 // "Let offset be node's length." 3578 var offset = getNodeLength(node); 3579 3580 // "While (node, offset) is not a block boundary point:" 3581 while (!isBlockBoundaryPoint(node, offset)) { 3582 // "If node has a visible child with index offset, return false." 3583 if (offset < node.childNodes.length && isVisible(node.childNodes[offset])) { 3584 return false; 3585 } 3586 3587 // "If offset is node's length or node has no children, set offset to 3588 // one plus node's index, then set node to its parent." 3589 if (offset == getNodeLength(node) || !node.hasChildNodes()) { 3590 offset = 1 + getNodeIndex(node); 3591 node = node.parentNode; 3592 3593 // "Otherwise, set node to its child with index offset and set offset 3594 // to zero." 3595 } else { 3596 node = node.childNodes[offset]; 3597 offset = 0; 3598 } 3599 } 3600 3601 // "Return true." 3602 return true; 3603 } 3604 3605 //@} 3606 ///// Splitting a node list's parent ///// 3607 //@{ 3608 3609 function splitParent(nodeList, range) { 3610 var i; 3611 3612 // "Let original parent be the parent of the first member of node list." 3613 var originalParent = nodeList[0].parentNode; 3614 3615 // "If original parent is not editable or its parent is null, do nothing 3616 // and abort these steps." 3617 if (!isEditable(originalParent) || !originalParent.parentNode) { 3618 return; 3619 3620 } 3621 3622 // "If the first child of original parent is in node list, remove 3623 // extraneous line breaks before original parent." 3624 if (jQuery.inArray(originalParent.firstChild, nodeList) != -1) { 3625 removeExtraneousLineBreaksBefore(originalParent); 3626 } 3627 3628 var firstChildInNodeList = jQuery.inArray(originalParent.firstChild, nodeList) != -1; 3629 var lastChildInNodeList = jQuery.inArray(originalParent.lastChild, nodeList) != -1; 3630 3631 // "If the first child of original parent is in node list, and original 3632 // parent follows a line break, set follows line break to true. Otherwise, 3633 // set follows line break to false." 3634 var followsLineBreak_ = firstChildInNodeList && followsLineBreak(originalParent); 3635 3636 // "If the last child of original parent is in node list, and original 3637 // parent precedes a line break, set precedes line break to true. 3638 // Otherwise, set precedes line break to false." 3639 var precedesLineBreak_ = lastChildInNodeList && precedesLineBreak(originalParent); 3640 3641 // "If the first child of original parent is not in node list, but its last 3642 // child is:" 3643 if (!firstChildInNodeList && lastChildInNodeList) { 3644 // "For each node in node list, in reverse order, insert node into the 3645 // parent of original parent immediately after original parent, 3646 // preserving ranges." 3647 for (i = nodeList.length - 1; i >= 0; i--) { 3648 movePreservingRanges(nodeList[i], originalParent.parentNode, 1 + getNodeIndex(originalParent), range); 3649 } 3650 3651 // "If precedes line break is true, and the last member of node list 3652 // does not precede a line break, call createElement("br") on the 3653 // context object and insert the result immediately after the last 3654 // member of node list." 3655 if (precedesLineBreak_ && !precedesLineBreak(nodeList[nodeList.length - 1])) { 3656 nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling); 3657 } 3658 3659 // "Remove extraneous line breaks at the end of original parent." 3660 removeExtraneousLineBreaksAtTheEndOf(originalParent); 3661 3662 // "Abort these steps." 3663 return; 3664 } 3665 3666 // "If the first child of original parent is not in node list:" 3667 if (!firstChildInNodeList) { 3668 // "Let cloned parent be the result of calling cloneNode(false) on 3669 // original parent." 3670 var clonedParent = originalParent.cloneNode(false); 3671 3672 // "If original parent has an id attribute, unset it." 3673 originalParent.removeAttribute("id"); 3674 3675 // "Insert cloned parent into the parent of original parent immediately 3676 // before original parent." 3677 originalParent.parentNode.insertBefore(clonedParent, originalParent); 3678 3679 // "While the previousSibling of the first member of node list is not 3680 // null, append the first child of original parent as the last child of 3681 // cloned parent, preserving ranges." 3682 while (nodeList[0].previousSibling) { 3683 movePreservingRanges(originalParent.firstChild, clonedParent, clonedParent.childNodes.length, range); 3684 } 3685 } 3686 3687 // "For each node in node list, insert node into the parent of original 3688 // parent immediately before original parent, preserving ranges." 3689 for (i = 0; i < nodeList.length; i++) { 3690 movePreservingRanges(nodeList[i], originalParent.parentNode, getNodeIndex(originalParent), range); 3691 } 3692 3693 // "If follows line break is true, and the first member of node list does 3694 // not follow a line break, call createElement("br") on the context object 3695 // and insert the result immediately before the first member of node list." 3696 if (followsLineBreak_ && !followsLineBreak(nodeList[0])) { 3697 nodeList[0].parentNode.insertBefore(document.createElement("br"), nodeList[0]); 3698 } 3699 3700 // "If the last member of node list is an inline node other than a br, and 3701 // the first child of original parent is a br, and original parent is not 3702 // an inline node, remove the first child of original parent from original 3703 // parent." 3704 if (isInlineNode(nodeList[nodeList.length - 1]) && !isNamedHtmlElement(nodeList[nodeList.length - 1], "br") && isNamedHtmlElement(originalParent.firstChild, "br") && !isInlineNode(originalParent)) { 3705 originalParent.removeChild(originalParent.firstChild); 3706 } 3707 3708 // "If original parent has no children:" 3709 if (!originalParent.hasChildNodes()) { 3710 // if the current range is collapsed and at the end of the originalParent.parentNode 3711 // the offset will not be available anymore after the next step (remove child) 3712 // that's why we need to fix the range to prevent a bogus offset 3713 if (originalParent.parentNode === range.startContainer && originalParent.parentNode === range.endContainer && range.startContainer === range.endContainer && range.startOffset === range.endOffset && originalParent.parentNode.childNodes.length === range.startOffset) { 3714 range.startOffset = originalParent.parentNode.childNodes.length - 1; 3715 range.endOffset = range.startOffset; 3716 } 3717 3718 // "Remove original parent from its parent." 3719 originalParent.parentNode.removeChild(originalParent); 3720 3721 // "If precedes line break is true, and the last member of node list 3722 // does not precede a line break, call createElement("br") on the 3723 // context object and insert the result immediately after the last 3724 // member of node list." 3725 if (precedesLineBreak_ && !precedesLineBreak(nodeList[nodeList.length - 1])) { 3726 nodeList[nodeList.length - 1].parentNode.insertBefore(document.createElement("br"), nodeList[nodeList.length - 1].nextSibling); 3727 } 3728 3729 // "Otherwise, remove extraneous line breaks before original parent." 3730 } else { 3731 removeExtraneousLineBreaksBefore(originalParent); 3732 } 3733 3734 // "If node list's last member's nextSibling is null, but its parent is not 3735 // null, remove extraneous line breaks at the end of node list's last 3736 // member's parent." 3737 if (!nodeList[nodeList.length - 1].nextSibling && nodeList[nodeList.length - 1].parentNode) { 3738 removeExtraneousLineBreaksAtTheEndOf(nodeList[nodeList.length - 1].parentNode); 3739 } 3740 } 3741 3742 //@} 3743 ///// The backColor command ///// 3744 //@{ 3745 commands.backcolor = { 3746 // Copy-pasted, same as hiliteColor 3747 action: function (value, range) { 3748 // Action is further copy-pasted, same as foreColor 3749 3750 // "If value is not a valid CSS color, prepend "#" to it." 3751 // 3752 // "If value is still not a valid CSS color, or if it is currentColor, 3753 // abort these steps and do nothing." 3754 // 3755 // Cheap hack for testing, no attempt to be comprehensive. 3756 if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) { 3757 value = "#" + value; 3758 } 3759 if (!/^(rgba?|hsla?)\(.*\)$/.test(value) && !parseSimpleColor(value) && value.toLowerCase() != "transparent") { 3760 return; 3761 } 3762 3763 // "Set the selection's value to value." 3764 setSelectionValue("backcolor", value, range); 3765 }, 3766 standardInlineValueCommand: true, 3767 relevantCssProperty: "backgroundColor", 3768 equivalentValues: function (val1, val2) { 3769 // "Either both strings are valid CSS colors and have the same red, 3770 // green, blue, and alpha components, or neither string is a valid CSS 3771 // color." 3772 return normalizeColor(val1) === normalizeColor(val2); 3773 } 3774 }; 3775 3776 //@} 3777 ///// The bold command ///// 3778 //@{ 3779 commands.bold = { 3780 action: function (value, range) { 3781 // "If queryCommandState("bold") returns true, set the selection's 3782 // value to "normal". Otherwise set the selection's value to "bold"." 3783 if (myQueryCommandState("bold", range)) { 3784 setSelectionValue("bold", "normal", range); 3785 } else { 3786 setSelectionValue("bold", "bold", range); 3787 } 3788 }, 3789 inlineCommandActivatedValues: ["bold", "600", "700", "800", "900"], 3790 relevantCssProperty: "fontWeight", 3791 equivalentValues: function (val1, val2) { 3792 // "Either the two strings are equal, or one is "bold" and the other is 3793 // "700", or one is "normal" and the other is "400"." 3794 return val1 == val2 || (val1 == "bold" && val2 == "700") || (val1 == "700" && val2 == "bold") || (val1 == "normal" && val2 == "400") || (val1 == "400" && val2 == "normal"); 3795 } 3796 }; 3797 3798 //@} 3799 ///// The createLink command ///// 3800 //@{ 3801 commands.createlink = { 3802 action: function (value, range) { 3803 // "If value is the empty string, abort these steps and do nothing." 3804 if (value === "") { 3805 return; 3806 } 3807 3808 // "For each editable a element that has an href attribute and is an 3809 // ancestor of some node effectively contained in the active range, set 3810 // that a element's href attribute to value." 3811 // 3812 // TODO: We don't actually do this in tree order, not that it matters 3813 // unless you're spying with mutation events. 3814 3815 $_(getAllEffectivelyContainedNodes(getActiveRange())).forEach(function (node) { 3816 $_(getAncestors(node)).forEach(function (ancestor) { 3817 if (isEditable(ancestor) && isNamedHtmlElement(ancestor, 'a') && hasAttribute(ancestor, "href")) { 3818 ancestor.setAttribute("href", value); 3819 } 3820 }); 3821 }); 3822 3823 // "Set the selection's value to value." 3824 setSelectionValue("createlink", value, range); 3825 }, 3826 standardInlineValueCommand: true 3827 }; 3828 3829 //@} 3830 ///// The fontName command ///// 3831 //@{ 3832 commands.fontname = { 3833 action: function (value, range) { 3834 // "Set the selection's value to value." 3835 setSelectionValue("fontname", value, range); 3836 }, 3837 standardInlineValueCommand: true, 3838 relevantCssProperty: "fontFamily" 3839 }; 3840 3841 //@} 3842 ///// The fontSize command ///// 3843 //@{ 3844 3845 commands.fontsize = { 3846 action: function (value, range) { 3847 // "If value is the empty string, abort these steps and do nothing." 3848 if (value === "") { 3849 return; 3850 } 3851 3852 value = normalizeFontSize(value); 3853 3854 // "If value is not one of the strings "xx-small", "x-small", "small", 3855 // "medium", "large", "x-large", "xx-large", "xxx-large", and is not a 3856 // valid CSS absolute length, then abort these steps and do nothing." 3857 // 3858 // More cheap hacks to skip valid CSS absolute length checks. 3859 if (jQuery.inArray(value, ["xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"]) == -1 && !/^[0-9]+(\.[0-9]+)?(cm|mm|in|pt|pc)$/.test(value)) { 3860 return; 3861 } 3862 3863 // "Set the selection's value to value." 3864 setSelectionValue("fontsize", value, range); 3865 }, 3866 3867 indeterm: function () { 3868 // "True if among editable Text nodes that are effectively contained in 3869 // the active range, there are two that have distinct effective command 3870 // values. Otherwise false." 3871 return $_(getAllEffectivelyContainedNodes(getActiveRange(), function (node) { 3872 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 3873 })).map(function (node) { 3874 return getEffectiveCommandValue(node, "fontsize"); 3875 }, true).filter(function (value, i, arr) { 3876 return $_(arr.slice(0, i)).indexOf(value) == -1; 3877 }).length >= 2; 3878 }, 3879 value: function (range) { 3880 // "Let pixel size be the effective command value of the first editable 3881 // Text node that is effectively contained in the active range, or if 3882 // there is no such node, the effective command value of the active 3883 // range's start node, in either case interpreted as a number of 3884 // pixels." 3885 var node = getAllEffectivelyContainedNodes(range, function (node) { 3886 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 3887 })[0]; 3888 if (node === undefined) { 3889 node = range.startContainer; 3890 } 3891 var pixelSize = getEffectiveCommandValue(node, "fontsize"); 3892 3893 // "Return the legacy font size for pixel size." 3894 return getLegacyFontSize(pixelSize); 3895 }, 3896 relevantCssProperty: "fontSize" 3897 }; 3898 3899 //@} 3900 ///// The foreColor command ///// 3901 //@{ 3902 commands.forecolor = { 3903 action: function (value, range) { 3904 // Copy-pasted, same as backColor and hiliteColor 3905 3906 // "If value is not a valid CSS color, prepend "#" to it." 3907 // 3908 // "If value is still not a valid CSS color, or if it is currentColor, 3909 // abort these steps and do nothing." 3910 // 3911 // Cheap hack for testing, no attempt to be comprehensive. 3912 if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) { 3913 value = "#" + value; 3914 } 3915 if (!/^(rgba?|hsla?)\(.*\)$/.test(value) && !parseSimpleColor(value) && value.toLowerCase() != "transparent") { 3916 3917 return; 3918 } 3919 3920 // "Set the selection's value to value." 3921 setSelectionValue("forecolor", value, range); 3922 }, 3923 standardInlineValueCommand: true, 3924 relevantCssProperty: "color", 3925 equivalentValues: function (val1, val2) { 3926 // "Either both strings are valid CSS colors and have the same red, 3927 // green, blue, and alpha components, or neither string is a valid CSS 3928 // color." 3929 return normalizeColor(val1) === normalizeColor(val2); 3930 } 3931 }; 3932 3933 //@} 3934 ///// The hiliteColor command ///// 3935 //@{ 3936 commands.hilitecolor = { 3937 // Copy-pasted, same as backColor 3938 action: function (value, range) { 3939 // Action is further copy-pasted, same as foreColor 3940 3941 // "If value is not a valid CSS color, prepend "#" to it." 3942 // 3943 // "If value is still not a valid CSS color, or if it is currentColor, 3944 // abort these steps and do nothing." 3945 // 3946 // Cheap hack for testing, no attempt to be comprehensive. 3947 if (/^([0-9a-fA-F]{3}){1,2}$/.test(value)) { 3948 value = "#" + value; 3949 } 3950 if (!/^(rgba?|hsla?)\(.*\)$/.test(value) && !parseSimpleColor(value) && value.toLowerCase() != "transparent") { 3951 return; 3952 } 3953 3954 // "Set the selection's value to value." 3955 setSelectionValue("hilitecolor", value, range); 3956 }, 3957 indeterm: function () { 3958 // "True if among editable Text nodes that are effectively contained in 3959 // the active range, there are two that have distinct effective command 3960 // values. Otherwise false." 3961 return $_(getAllEffectivelyContainedNodes(getActiveRange(), function (node) { 3962 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 3963 })).map(function (node) { 3964 return getEffectiveCommandValue(node, "hilitecolor"); 3965 }, true).filter(function (value, i, arr) { 3966 return $_(arr.slice(0, i)).indexOf(value) == -1; 3967 }).length >= 2; 3968 }, 3969 standardInlineValueCommand: true, 3970 relevantCssProperty: "backgroundColor", 3971 equivalentValues: function (val1, val2) { 3972 // "Either both strings are valid CSS colors and have the same red, 3973 // green, blue, and alpha components, or neither string is a valid CSS 3974 // color." 3975 return normalizeColor(val1) === normalizeColor(val2); 3976 } 3977 }; 3978 3979 //@} 3980 ///// The italic command ///// 3981 //@{ 3982 commands.italic = { 3983 action: function (value, range) { 3984 // "If queryCommandState("italic") returns true, set the selection's 3985 // value to "normal". Otherwise set the selection's value to "italic"." 3986 if (myQueryCommandState("italic", range)) { 3987 setSelectionValue("italic", "normal", range); 3988 } else { 3989 setSelectionValue("italic", "italic", range); 3990 } 3991 }, 3992 inlineCommandActivatedValues: ["italic", "oblique"], 3993 relevantCssProperty: "fontStyle" 3994 }; 3995 3996 //@} 3997 ///// The removeFormat command ///// 3998 //@{ 3999 commands.removeformat = { 4000 action: function (value, range) { 4001 var newEnd, newStart, newNode; 4002 4003 // "A removeFormat candidate is an editable HTML element with local 4004 // name "abbr", "acronym", "b", "bdi", "bdo", "big", "blink", "cite", 4005 // "code", "dfn", "em", "font", "i", "ins", "kbd", "mark", "nobr", "q", 4006 // "s", "samp", "small", "span", "strike", "strong", "sub", "sup", 4007 // "tt", "u", or "var"." 4008 function isRemoveFormatCandidate(node) { 4009 return isEditable(node) && isHtmlElementInArray(node, ["abbr", "acronym", "b", "bdi", "bdo", "big", "blink", "cite", "code", "dfn", "em", "font", "i", "ins", "kbd", "mark", "nobr", "q", "s", "samp", "small", "span", "strike", "strong", "sub", "sup", "tt", "u", "var"]); 4010 } 4011 4012 // "Let elements to remove be a list of every removeFormat candidate 4013 // effectively contained in the active range." 4014 var elementsToRemove = getAllEffectivelyContainedNodes(getActiveRange(), isRemoveFormatCandidate); 4015 4016 // "For each element in elements to remove:" 4017 $_(elementsToRemove).forEach(function (element) { 4018 // "While element has children, insert the first child of element 4019 // into the parent of element immediately before element, 4020 // preserving ranges." 4021 while (element.hasChildNodes()) { 4022 movePreservingRanges(element.firstChild, element.parentNode, getNodeIndex(element), getActiveRange()); 4023 } 4024 4025 // "Remove element from its parent." 4026 element.parentNode.removeChild(element); 4027 }); 4028 4029 // "If the active range's start node is an editable Text node, and its 4030 // start offset is neither zero nor its start node's length, call 4031 // splitText() on the active range's start node, with argument equal to 4032 // the active range's start offset. Then set the active range's start 4033 // node to the result, and its start offset to zero." 4034 if (isEditable(getActiveRange().startContainer) && getActiveRange().startContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().startOffset != 0 && getActiveRange().startOffset != getNodeLength(getActiveRange().startContainer)) { 4035 // Account for browsers not following range mutation rules 4036 if (getActiveRange().startContainer == getActiveRange().endContainer) { 4037 newEnd = getActiveRange().endOffset - getActiveRange().startOffset; 4038 newNode = getActiveRange().startContainer.splitText(getActiveRange().startOffset); 4039 getActiveRange().setStart(newNode, 0); 4040 getActiveRange().setEnd(newNode, newEnd); 4041 } else { 4042 getActiveRange().setStart(getActiveRange().startContainer.splitText(getActiveRange().startOffset), 0); 4043 } 4044 } 4045 4046 // "If the active range's end node is an editable Text node, and its 4047 // end offset is neither zero nor its end node's length, call 4048 // splitText() on the active range's end node, with argument equal to 4049 // the active range's end offset." 4050 if (isEditable(getActiveRange().endContainer) && getActiveRange().endContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().endOffset != 0 && getActiveRange().endOffset != getNodeLength(getActiveRange().endContainer)) { 4051 // IE seems to mutate the range incorrectly here, so we need 4052 // correction here as well. Have to be careful to set the range to 4053 // something not including the text node so that getActiveRange() 4054 // doesn't throw an exception due to a temporarily detached 4055 // endpoint. 4056 newStart = [getActiveRange().startContainer, getActiveRange().startOffset]; 4057 newEnd = [getActiveRange().endContainer, getActiveRange().endOffset]; 4058 getActiveRange().setEnd(document.documentElement, 0); 4059 newEnd[0].splitText(newEnd[1]); 4060 getActiveRange().setStart(newStart[0], newStart[1]); 4061 getActiveRange().setEnd(newEnd[0], newEnd[1]); 4062 } 4063 4064 // "Let node list consist of all editable nodes effectively contained 4065 // in the active range." 4066 // 4067 // "For each node in node list, while node's parent is a removeFormat 4068 // candidate in the same editing host as node, split the parent of the 4069 // one-node list consisting of node." 4070 $_(getAllEffectivelyContainedNodes(getActiveRange(), isEditable)).forEach(function (node) { 4071 while (isRemoveFormatCandidate(node.parentNode) && inSameEditingHost(node.parentNode, node)) { 4072 splitParent([node], getActiveRange()); 4073 } 4074 }); 4075 4076 // "For each of the entries in the following list, in the given order, 4077 // set the selection's value to null, with command as given." 4078 $_(["subscript", "bold", "fontname", "fontsize", "forecolor", "hilitecolor", "italic", "strikethrough", "underline"]).forEach(function (command) { 4079 setSelectionValue(command, null, range); 4080 }); 4081 } 4082 }; 4083 4084 //@} 4085 ///// The strikethrough command ///// 4086 //@{ 4087 commands.strikethrough = { 4088 action: function (value, range) { 4089 // "If queryCommandState("strikethrough") returns true, set the 4090 // selection's value to null. Otherwise set the selection's value to 4091 // "line-through"." 4092 if (myQueryCommandState("strikethrough", range)) { 4093 setSelectionValue("strikethrough", null, range); 4094 } else { 4095 setSelectionValue("strikethrough", "line-through", range); 4096 } 4097 }, 4098 inlineCommandActivatedValues: ["line-through"] 4099 }; 4100 4101 //@} 4102 ///// The subscript command ///// 4103 //@{ 4104 commands.subscript = { 4105 action: function (value, range) { 4106 // "Call queryCommandState("subscript"), and let state be the result." 4107 var state = myQueryCommandState("subscript", range); 4108 4109 // "Set the selection's value to null." 4110 setSelectionValue("subscript", null, range); 4111 4112 // "If state is false, set the selection's value to "subscript"." 4113 if (!state) { 4114 setSelectionValue("subscript", "subscript", range); 4115 } 4116 }, 4117 indeterm: function () { 4118 // "True if either among editable Text nodes that are effectively 4119 // contained in the active range, there is at least one with effective 4120 // command value "subscript" and at least one with some other effective 4121 // command value; or if there is some editable Text node effectively 4122 // contained in the active range with effective command value "mixed". 4123 // Otherwise false." 4124 var nodes = getAllEffectivelyContainedNodes(getActiveRange(), function (node) { 4125 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 4126 }); 4127 return (($_(nodes).some(function (node) { return getEffectiveCommandValue(node, "subscript") == "subscript"; }) 4128 && $_(nodes).some(function (node) { return getEffectiveCommandValue(node, "subscript") != "subscript"; })) 4129 || $_(nodes).some(function (node) { return getEffectiveCommandValue(node, "subscript") == "mixed"; })); 4130 }, 4131 inlineCommandActivatedValues: ["subscript"] 4132 }; 4133 4134 //@} 4135 ///// The superscript command ///// 4136 //@{ 4137 commands.superscript = { 4138 action: function (value, range) { 4139 // "Call queryCommandState("superscript"), and let state be the 4140 // result." 4141 var state = myQueryCommandState("superscript", range); 4142 4143 // "Set the selection's value to null." 4144 setSelectionValue("superscript", null, range); 4145 4146 // "If state is false, set the selection's value to "superscript"." 4147 if (!state) { 4148 setSelectionValue("superscript", "superscript", range); 4149 } 4150 }, 4151 indeterm: function () { 4152 // "True if either among editable Text nodes that are effectively 4153 // contained in the active range, there is at least one with effective 4154 // command value "superscript" and at least one with some other 4155 // effective command value; or if there is some editable Text node 4156 // effectively contained in the active range with effective command 4157 // value "mixed". Otherwise false." 4158 var nodes = getAllEffectivelyContainedNodes( 4159 getActiveRange(), 4160 function (node) { 4161 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 4162 } 4163 ); 4164 return (($_(nodes).some(function (node) { return getEffectiveCommandValue(node, "superscript") == "superscript"; }) 4165 && $_(nodes).some(function (node) { return getEffectiveCommandValue(node, "superscript") != "superscript"; })) 4166 || $_(nodes).some(function (node) { return getEffectiveCommandValue(node, "superscript") == "mixed"; })); 4167 }, 4168 inlineCommandActivatedValues: ["superscript"] 4169 }; 4170 4171 //@} 4172 ///// The underline command ///// 4173 //@{ 4174 commands.underline = { 4175 action: function (value, range) { 4176 // "If queryCommandState("underline") returns true, set the selection's 4177 // value to null. Otherwise set the selection's value to "underline"." 4178 if (myQueryCommandState("underline", range)) { 4179 setSelectionValue("underline", null, range); 4180 } else { 4181 setSelectionValue("underline", "underline", range); 4182 } 4183 }, 4184 inlineCommandActivatedValues: ["underline"] 4185 }; 4186 4187 //@} 4188 ///// The unlink command ///// 4189 //@{ 4190 commands.unlink = { 4191 action: function () { 4192 // "Let hyperlinks be a list of every a element that has an href 4193 // attribute and is contained in the active range or is an ancestor of 4194 // one of its boundary points." 4195 // 4196 // As usual, take care to ensure it's tree order. The correctness of 4197 // the following is left as an exercise for the reader. 4198 var range = getActiveRange(); 4199 var hyperlinks = []; 4200 var node; 4201 for (node = range.startContainer; node; node = node.parentNode) { 4202 if (isNamedHtmlElement(node, 'A') && hasAttribute(node, "href")) { 4203 hyperlinks.unshift(node); 4204 } 4205 } 4206 for (node = range.startContainer; node != nextNodeDescendants(range.endContainer); node = nextNode(node)) { 4207 if (isNamedHtmlElement(node, 'A') && hasAttribute(node, "href") && (isContained(node, range) || isAncestor(node, range.endContainer) || node == range.endContainer)) { 4208 hyperlinks.push(node); 4209 } 4210 } 4211 4212 // "Clear the value of each member of hyperlinks." 4213 var i; 4214 for (i = 0; i < hyperlinks.length; i++) { 4215 clearValue(hyperlinks[i], "unlink", range); 4216 } 4217 }, 4218 standardInlineValueCommand: true 4219 }; 4220 4221 //@} 4222 4223 ///////////////////////////////////// 4224 ///// Block formatting commands ///// 4225 ///////////////////////////////////// 4226 4227 ///// Block formatting command definitions ///// 4228 //@{ 4229 4230 // "An indentation element is either a blockquote, or a div that has a style 4231 // attribute that sets "margin" or some subproperty of it." 4232 function isIndentationElement(node) { 4233 if (!isAnyHtmlElement(node)) { 4234 return false; 4235 } 4236 4237 if (node.tagName == "BLOCKQUOTE") { 4238 return true; 4239 } 4240 4241 if (node.tagName != "DIV") { 4242 return false; 4243 } 4244 4245 if (typeof node.style.length !== 'undefined') { 4246 var i; 4247 for (i = 0; i < node.style.length; i++) { 4248 // Approximate check 4249 if (/^(-[a-z]+-)?margin/.test(node.style[i])) { 4250 return true; 4251 } 4252 } 4253 } else { 4254 var s; 4255 /*jslint forin: true*/ //not sure whether node.style.hasOwnProperty is valid 4256 for (s in node.style) { 4257 if (/^(-[a-z]+-)?margin/.test(s) && node.style[s] && node.style[s] !== 0) { 4258 return true; 4259 } 4260 } 4261 /*jslint forin: false*/ 4262 } 4263 4264 return false; 4265 } 4266 4267 // "A simple indentation element is an indentation element that has no 4268 // attributes other than one or more of 4269 // 4270 // * "a style attribute that sets no properties other than "margin", "border", 4271 // "padding", or subproperties of those; 4272 // * "a class attribute; 4273 // * "a dir attribute." 4274 function isSimpleIndentationElement(node) { 4275 if (!isIndentationElement(node)) { 4276 return false; 4277 } 4278 4279 if (node.tagName != "BLOCKQUOTE" && node.tagName != "DIV") { 4280 return false; 4281 } 4282 4283 var i; 4284 for (i = 0; i < node.attributes.length; i++) { 4285 if (!isHtmlNamespace(node.attributes[i].namespaceURI) || jQuery.inArray(node.attributes[i].name, ["style", "class", "dir"]) == -1) { 4286 return false; 4287 } 4288 } 4289 4290 if (typeof node.style.length !== 'undefined') { 4291 for (i = 0; i < node.style.length; i++) { 4292 // This is approximate, but it works well enough for my purposes. 4293 if (!/^(-[a-z]+-)?(margin|border|padding)/.test(node.style[i])) { 4294 return false; 4295 } 4296 } 4297 } else { 4298 var s; 4299 /*jslint forin: true*/ //not sure whether node.style.hasOwnProperty is valid 4300 for (s in node.style) { 4301 // This is approximate, but it works well enough for my purposes. 4302 if (!/^(-[a-z]+-)?(margin|border|padding)/.test(s) && node.style[s] && node.style[s] !== 0 && node.style[s] !== 'false') { 4303 return false; 4304 } 4305 } 4306 /*jslint forin: false*/ 4307 } 4308 4309 return true; 4310 } 4311 4312 // "A non-list single-line container is an HTML element with local name 4313 // "address", "div", "h1", "h2", "h3", "h4", "h5", "h6", "listing", "p", "pre", 4314 // or "xmp"." 4315 function isNonListSingleLineContainer(node) { 4316 return isHtmlElementInArray(node, ["address", "div", "h1", "h2", "h3", "h4", "h5", "h6", "listing", "p", "pre", "xmp"]); 4317 } 4318 4319 // "A single-line container is either a non-list single-line container, or an 4320 // HTML element with local name "li", "dt", or "dd"." 4321 function isSingleLineContainer(node) { 4322 return isNonListSingleLineContainer(node) || isHtmlElementInArray(node, ["li", "dt", "dd"]); 4323 } 4324 4325 // "The default single-line container name is "p"." 4326 var defaultSingleLineContainerName = "p"; 4327 4328 //@} 4329 ///// Check whether the given element is an end break ///// 4330 //@{ 4331 function isEndBreak(element) { 4332 return (isNamedHtmlElement(element, 'br') && element.parentNode.lastChild === element); 4333 } 4334 4335 //@} 4336 ///// Create an end break ///// 4337 //@{ 4338 function createEndBreak() { 4339 return document.createElement("br"); 4340 } 4341 4342 /** 4343 * Ensure the container is editable 4344 * E.g. when called for an empty paragraph or header, and the browser is not IE, 4345 * we need to append a br (marked with class aloha-end-br) 4346 * For IE7, there is a special behaviour that will append zero-width whitespace 4347 * @param {DOMNode} container 4348 */ 4349 function ensureContainerEditable(container) { 4350 if (!container) { 4351 return; 4352 } 4353 4354 if (isNamedHtmlElement(container.lastChild, "br")) { 4355 return; 4356 } 4357 4358 if ($_(container.childNodes).some(isVisible)) { 4359 return; 4360 } 4361 4362 if (!jQuery.browser.msie) { 4363 // for normal browsers, the end-br will do 4364 container.appendChild(createEndBreak()); 4365 } else if (jQuery.browser.msie && jQuery.browser.version <= 7 && isHtmlElementInArray(container, ["p", "h1", "h2", "h3", "h4", "h5", "h6", "pre", "blockquote"])) { 4366 // for IE7, we need to insert a text node containing a single zero-width whitespace character 4367 if (!container.firstChild) { 4368 container.appendChild(document.createTextNode('\u200b')); 4369 } 4370 } 4371 } 4372 4373 //@} 4374 ///// Assorted block formatting command algorithms ///// 4375 //@{ 4376 4377 function fixDisallowedAncestors(node, range) { 4378 var i; 4379 4380 // "If node is not editable, abort these steps." 4381 if (!isEditable(node)) { 4382 return; 4383 } 4384 4385 // "If node is not an allowed child of any of its ancestors in the same 4386 // editing host, and is not an HTML element with local name equal to the 4387 // default single-line container name:" 4388 if ($_(getAncestors(node)).every(function (ancestor) { return !inSameEditingHost(node, ancestor) || !isAllowedChild(node, ancestor); }) 4389 && !isHtmlElement_obsolete(node, defaultSingleLineContainerName)) { 4390 // "If node is a dd or dt, wrap the one-node list consisting of node, 4391 // with sibling criteria returning true for any dl with no attributes 4392 // and false otherwise, and new parent instructions returning the 4393 // result of calling createElement("dl") on the context object. Then 4394 // abort these steps." 4395 if (isHtmlElementInArray(node, ["dd", "dt"])) { 4396 wrap( 4397 [node], 4398 function (sibling) { 4399 return isNamedHtmlElement(sibling, 'dl') && !sibling.attributes.length; 4400 }, 4401 function () { 4402 return document.createElement("dl"); 4403 }, 4404 range 4405 ); 4406 return; 4407 } 4408 4409 // "If node is not a prohibited paragraph child, abort these steps." 4410 if (!isProhibitedParagraphChild(node)) { 4411 return; 4412 } 4413 4414 // "Set the tag name of node to the default single-line container name, 4415 // and let node be the result." 4416 node = setTagName(node, defaultSingleLineContainerName, range); 4417 4418 ensureContainerEditable(node); 4419 4420 // "Fix disallowed ancestors of node." 4421 fixDisallowedAncestors(node, range); 4422 4423 // "Let descendants be all descendants of node." 4424 var descendants = getDescendants(node); 4425 4426 // "Fix disallowed ancestors of each member of descendants." 4427 for (i = 0; i < descendants.length; i++) { 4428 fixDisallowedAncestors(descendants[i], range); 4429 } 4430 4431 // "Abort these steps." 4432 return; 4433 } 4434 4435 // "Record the values of the one-node list consisting of node, and let 4436 // values be the result." 4437 var values = recordValues([node]); 4438 var newStartOffset, newEndOffset; 4439 4440 // "While node is not an allowed child of its parent, split the parent of 4441 // the one-node list consisting of node." 4442 while (!isAllowedChild(node, node.parentNode)) { 4443 // If the parent contains only this node and possibly empty text nodes, we rather want to unwrap the node, instead of splitting. 4444 // With splitting, we would get empty nodes, like: 4445 // split: <p><p>foo</p></p> -> <p></p><p>foo</p> (bad) 4446 // unwrap: <p><p>foo</p></p> -> <p>foo</p> (good) 4447 4448 // First remove empty text nodes that are children of the parent and correct the range if necessary 4449 // we do this to have the node being the only child of its parent, so that we can replace the parent with the node 4450 for (i = node.parentNode.childNodes.length - 1; i >= 0; --i) { 4451 if (node.parentNode.childNodes[i].nodeType == 3 && node.parentNode.childNodes[i].data.length == 0) { 4452 // we remove the empty text node 4453 node.parentNode.removeChild(node.parentNode.childNodes[i]); 4454 4455 // if the range points to somewhere behind the removed text node, we reduce the offset 4456 if (range.startContainer == node.parentNode && range.startOffset > i) { 4457 range.startOffset--; 4458 } 4459 if (range.endContainer == node.parentNode && range.endOffset > i) { 4460 range.endOffset--; 4461 } 4462 } 4463 } 4464 4465 // now that the parent has only the node as child (because we 4466 // removed any existing empty text nodes), we can safely unwrap the 4467 // node's contents, and correct the range if necessary 4468 if (node.parentNode.childNodes.length == 1) { 4469 newStartOffset = range.startOffset; 4470 newEndOffset = range.endOffset; 4471 4472 if (range.startContainer === node.parentNode && range.startOffset > getNodeIndex(node)) { 4473 // the node (1 element) will be replaced by its contents (contents().length elements) 4474 newStartOffset = range.startOffset + (jQuery(node).contents().length - 1); 4475 } 4476 if (range.endContainer === node.parentNode && range.endOffset > getNodeIndex(node)) { 4477 // the node (1 element) will be replaced by its contents (contents().length elements) 4478 newEndOffset = range.endOffset + (jQuery(node).contents().length - 1); 4479 } 4480 jQuery(node).contents().unwrap(); 4481 range.startOffset = newStartOffset; 4482 range.endOffset = newEndOffset; 4483 // after unwrapping, we are done 4484 break; 4485 } else { 4486 // store the original parent 4487 var originalParent = node.parentNode; 4488 splitParent([node], range); 4489 // check whether the parent did not change, so the split did not work, e.g. 4490 // because we already reached the editing host itself. 4491 // this situation can occur, e.g. when we insert a paragraph into an contenteditable span 4492 // in such cases, we just unwrap the contents of the paragraph 4493 if (originalParent === node.parentNode) { 4494 // so we unwrap now 4495 newStartOffset = range.startOffset; 4496 newEndOffset = range.endOffset; 4497 4498 if (range.startContainer === node.parentNode && range.startOffset > getNodeIndex(node)) { 4499 // the node (1 element) will be replaced by its contents (contents().length elements) 4500 newStartOffset = range.startOffset + (jQuery(node).contents().length - 1); 4501 } 4502 if (range.endContainer === node.parentNode && range.endOffset > getNodeIndex(node)) { 4503 // the node (1 element) will be replaced by its contents (contents().length elements) 4504 newEndOffset = range.endOffset + (jQuery(node).contents().length - 1); 4505 } 4506 jQuery(node).contents().unwrap(); 4507 range.startOffset = newStartOffset; 4508 range.endOffset = newEndOffset; 4509 // after unwrapping, we are done 4510 break; 4511 } 4512 } 4513 } 4514 4515 // "Restore the values from values." 4516 restoreValues(values, range); 4517 } 4518 4519 /** 4520 * This method "normalizes" sublists of the given item (which is supposed to be a LI): 4521 * If sublists are found in the LI element, they are moved directly into the outer list. 4522 * @param item item 4523 * @param range range, which will be modified if necessary 4524 */ 4525 function normalizeSublists(item, range) { 4526 // "If item is not an li or it is not editable or its parent is not 4527 // editable, abort these steps." 4528 if (!isNamedHtmlElement(item, 'LI') || !isEditable(item) || !isEditable(item.parentNode)) { 4529 return; 4530 } 4531 4532 // "Let new item be null." 4533 var newItem = null; 4534 4535 function isOlUl(node) { 4536 return isHtmlElementInArray(node, ["OL", "UL"]); 4537 } 4538 4539 // "While item has an ol or ul child:" 4540 while ($_(item.childNodes).some(isOlUl)) { 4541 // "Let child be the last child of item." 4542 var child = item.lastChild; 4543 4544 // "If child is an ol or ul, or new item is null and child is a Text 4545 // node whose data consists of zero of more space characters:" 4546 if (isHtmlElementInArray(child, ["OL", "UL"]) || (!newItem && child.nodeType == $_.Node.TEXT_NODE && /^[ \t\n\f\r]*$/.test(child.data))) { 4547 // "Set new item to null." 4548 newItem = null; 4549 4550 // "Insert child into the parent of item immediately following 4551 // item, preserving ranges." 4552 movePreservingRanges(child, item.parentNode, 1 + getNodeIndex(item), range); 4553 4554 // "Otherwise:" 4555 } else { 4556 // "If new item is null, let new item be the result of calling 4557 // createElement("li") on the ownerDocument of item, then insert 4558 // new item into the parent of item immediately after item." 4559 if (!newItem) { 4560 newItem = item.ownerDocument.createElement("li"); 4561 item.parentNode.insertBefore(newItem, item.nextSibling); 4562 } 4563 4564 // "Insert child into new item as its first child, preserving 4565 // ranges." 4566 movePreservingRanges(child, newItem, 0, range); 4567 } 4568 } 4569 } 4570 4571 /** 4572 * This method is the exact opposite of normalizeSublists. 4573 * List nodes directly nested into each other are corrected to be nested in li elements (so that the resulting lists conform the html5 specification) 4574 * @param item list node 4575 * @param range range, which is preserved when modifying the list 4576 */ 4577 function unNormalizeSublists(item, range) { 4578 // "If item is not an ol or ol or it is not editable or its parent is not 4579 // editable, abort these steps." 4580 if (!isHtmlElementInArray(item, ["OL", "UL"]) || !isEditable(item)) { 4581 return; 4582 } 4583 4584 var $list = jQuery(item); 4585 $list.children("ol,ul").each(function (index, sublist) { 4586 if (isNamedHtmlElement(sublist.previousSibling, "LI")) { 4587 // move the sublist into the LI 4588 movePreservingRanges(sublist, sublist.previousSibling, sublist.previousSibling.childNodes.length, range); 4589 } 4590 }); 4591 } 4592 4593 //@} 4594 ///// Block-extending a range ///// 4595 //@{ 4596 4597 function blockExtend(range) { 4598 // "Let start node, start offset, end node, and end offset be the start 4599 // and end nodes and offsets of the range." 4600 var startNode = range.startContainer; 4601 var startOffset = range.startOffset; 4602 var endNode = range.endContainer; 4603 var endOffset = range.endOffset; 4604 4605 // "If some ancestor container of start node is an li, set start offset to 4606 // the index of the last such li in tree order, and set start node to that 4607 // li's parent." 4608 var liAncestors = $_(getAncestors(startNode).concat(startNode)).filter(function (ancestor) { return isNamedHtmlElement(ancestor, 'li'); }).slice(-1); 4609 if (liAncestors.length) { 4610 startOffset = getNodeIndex(liAncestors[0]); 4611 startNode = liAncestors[0].parentNode; 4612 } 4613 4614 // "If (start node, start offset) is not a block start point, repeat the 4615 // following steps:" 4616 if (!isBlockStartPoint(startNode, startOffset)) { 4617 do { 4618 // "If start offset is zero, set it to start node's index, then set 4619 // start node to its parent." 4620 if (startOffset == 0) { 4621 startOffset = getNodeIndex(startNode); 4622 startNode = startNode.parentNode; 4623 4624 // "Otherwise, subtract one from start offset." 4625 } else { 4626 startOffset--; 4627 } 4628 4629 // "If (start node, start offset) is a block boundary point, break from 4630 // this loop." 4631 } while (!isBlockBoundaryPoint(startNode, startOffset)); 4632 } 4633 4634 // "While start offset is zero and start node's parent is not null, set 4635 // start offset to start node's index, then set start node to its parent." 4636 while (startOffset == 0 && startNode.parentNode) { 4637 startOffset = getNodeIndex(startNode); 4638 startNode = startNode.parentNode; 4639 } 4640 4641 // "If some ancestor container of end node is an li, set end offset to one 4642 // plus the index of the last such li in tree order, and set end node to 4643 // that li's parent." 4644 liAncestors = $_(getAncestors(endNode).concat(endNode)).filter(function (ancestor) { return isNamedHtmlElement(ancestor, 'li'); }).slice(-1); 4645 if (liAncestors.length) { 4646 endOffset = 1 + getNodeIndex(liAncestors[0]); 4647 endNode = liAncestors[0].parentNode; 4648 } 4649 4650 // "If (end node, end offset) is not a block end point, repeat the 4651 // following steps:" 4652 if (!isBlockEndPoint(endNode, endOffset)) { 4653 do { 4654 // "If end offset is end node's length, set it to one plus end node's 4655 // index, then set end node to its parent." 4656 if (endOffset == getNodeLength(endNode)) { 4657 endOffset = 1 + getNodeIndex(endNode); 4658 endNode = endNode.parentNode; 4659 4660 // "Otherwise, add one to end offset. 4661 } else { 4662 endOffset++; 4663 } 4664 4665 // "If (end node, end offset) is a block boundary point, break from 4666 // this loop." 4667 } while (!isBlockBoundaryPoint(endNode, endOffset)); 4668 } 4669 4670 // "While end offset is end node's length and end node's parent is not 4671 // null, set end offset to one plus end node's index, then set end node to 4672 // its parent." 4673 while (endOffset == getNodeLength(endNode) && endNode.parentNode) { 4674 endOffset = 1 + getNodeIndex(endNode); 4675 endNode = endNode.parentNode; 4676 } 4677 4678 // "Let new range be a new range whose start and end nodes and offsets 4679 // are start node, start offset, end node, and end offset." 4680 var newRange = Aloha.createRange(); 4681 newRange.setStart(startNode, startOffset); 4682 newRange.setEnd(endNode, endOffset); 4683 4684 // "Return new range." 4685 return newRange; 4686 } 4687 4688 function getSelectionListState() { 4689 // "Block-extend the active range, and let new range be the result." 4690 4691 var newRange = blockExtend(getActiveRange()); 4692 4693 // "Let node list be a list of nodes, initially empty." 4694 // 4695 // "For each node contained in new range, append node to node list if the 4696 // last member of node list (if any) is not an ancestor of node; node is 4697 // editable; node is not an indentation element; and node is either an ol 4698 // or ul, or the child of an ol or ul, or an allowed child of "li"." 4699 var nodeList = getContainedNodes(newRange, function (node) { 4700 return isEditable(node) && !isIndentationElement(node) && (isHtmlElementInArray(node, ["ol", "ul"]) || isHtmlElementInArray(node.parentNode, ["ol", "ul"]) || isAllowedChild(node, "li")); 4701 }); 4702 4703 // "If node list is empty, return "none"." 4704 if (!nodeList.length) { 4705 return "none"; 4706 } 4707 4708 // "If every member of node list is either an ol or the child of an ol or 4709 // the child of an li child of an ol, and none is a ul or an ancestor of a 4710 // ul, return "ol"." 4711 if ($_(nodeList).every(function (node) { return (isNamedHtmlElement(node, 'ol') 4712 || isNamedHtmlElement(node.parentNode, "ol") 4713 || (isNamedHtmlElement(node.parentNode, "li") 4714 && isNamedHtmlElement(node.parentNode.parentNode, "ol"))); }) 4715 && !$_(nodeList).some(function (node) { return isNamedHtmlElement(node, 'ul') || (node.querySelector && node.querySelector("ul")); })) { 4716 return "ol"; 4717 } 4718 4719 // "If every member of node list is either a ul or the child of a ul or the 4720 // child of an li child of a ul, and none is an ol or an ancestor of an ol, 4721 // return "ul"." 4722 if ($_(nodeList).every(function (node) { return (isNamedHtmlElement(node, 'ul') 4723 || isNamedHtmlElement(node.parentNode, "ul") 4724 || (isNamedHtmlElement(node.parentNode, "li") 4725 && isNamedHtmlElement(node.parentNode.parentNode, "ul"))); }) 4726 && !$_(nodeList).some(function (node) { return isNamedHtmlElement(node, 'ol') || (node.querySelector && node.querySelector("ol")); })) { 4727 return "ul"; 4728 } 4729 4730 var hasOl = $_(nodeList).some(function (node) { 4731 return (isNamedHtmlElement(node, 'ol') 4732 || isNamedHtmlElement(node.parentNode, "ol") 4733 || (node.querySelector && node.querySelector("ol")) 4734 || (isNamedHtmlElement(node.parentNode, "li") 4735 && isNamedHtmlElement(node.parentNode.parentNode, "ol"))); 4736 }); 4737 var hasUl = $_(nodeList).some(function (node) { 4738 return (isNamedHtmlElement(node, 'ul') 4739 || isNamedHtmlElement(node.parentNode, "ul") 4740 || (node.querySelector && node.querySelector("ul")) 4741 || (isNamedHtmlElement(node.parentNode, "li") 4742 && isNamedHtmlElement(node.parentNode.parentNode, "ul"))); 4743 }); 4744 // "If some member of node list is either an ol or the child or ancestor of 4745 // an ol or the child of an li child of an ol, and some member of node list 4746 // is either a ul or the child or ancestor of a ul or the child of an li 4747 // child of a ul, return "mixed"." 4748 if (hasOl && hasUl) { 4749 return "mixed"; 4750 } 4751 4752 // "If some member of node list is either an ol or the child or ancestor of 4753 // an ol or the child of an li child of an ol, return "mixed ol"." 4754 if (hasOl) { 4755 return "mixed ol"; 4756 } 4757 4758 // "If some member of node list is either a ul or the child or ancestor of 4759 // a ul or the child of an li child of a ul, return "mixed ul"." 4760 if (hasUl) { 4761 return "mixed ul"; 4762 } 4763 4764 // "Return "none"." 4765 return "none"; 4766 } 4767 4768 function getAlignmentValue(node) { 4769 // "While node is neither null nor an Element, or it is an Element but its 4770 // "display" property has resolved value "inline" or "none", set node to 4771 // its parent." 4772 while ((node && node.nodeType != $_.Node.ELEMENT_NODE) || (node.nodeType == $_.Node.ELEMENT_NODE && jQuery.inArray($_.getComputedStyle(node).display, ["inline", "none"]) != -1)) { 4773 node = node.parentNode; 4774 } 4775 4776 // "If node is not an Element, return "left"." 4777 if (!node || node.nodeType != $_.Node.ELEMENT_NODE) { 4778 return "left"; 4779 } 4780 4781 var resolvedValue = $_.getComputedStyle(node).textAlign 4782 // Hack around browser non-standardness 4783 .replace(/^-(moz|webkit)-/, "").replace(/^auto$/, "start"); 4784 4785 // "If node's "text-align" property has resolved value "start", return 4786 // "left" if the directionality of node is "ltr", "right" if it is "rtl"." 4787 if (resolvedValue == "start") { 4788 return getDirectionality(node) == "ltr" ? "left" : "right"; 4789 } 4790 4791 // "If node's "text-align" property has resolved value "end", return 4792 // "right" if the directionality of node is "ltr", "left" if it is "rtl"." 4793 if (resolvedValue == "end") { 4794 return getDirectionality(node) == "ltr" ? "right" : "left"; 4795 } 4796 4797 // "If node's "text-align" property has resolved value "center", "justify", 4798 // "left", or "right", return that value." 4799 if (jQuery.inArray(resolvedValue, ["center", "justify", "left", "right"]) != -1) { 4800 return resolvedValue; 4801 } 4802 4803 // "Return "left"." 4804 return "left"; 4805 } 4806 4807 //@} 4808 ///// Recording and restoring overrides ///// 4809 //@{ 4810 4811 function recordCurrentOverrides(range) { 4812 // "Let overrides be a list of (string, string or boolean) ordered pairs, 4813 // initially empty." 4814 var overrides = []; 4815 4816 // "If there is a value override for "createLink", add ("createLink", value 4817 // override for "createLink") to overrides." 4818 if (getValueOverride("createlink", range) !== undefined) { 4819 overrides.push(["createlink", getValueOverride("createlink", range)]); 4820 } 4821 4822 // "For each command in the list "bold", "italic", "strikethrough", 4823 // "subscript", "superscript", "underline", in order: if there is a state 4824 // override for command, add (command, command's state override) to 4825 // overrides." 4826 $_(["bold", "italic", "strikethrough", "subscript", "superscript", "underline"]).forEach(function (command) { 4827 if (getStateOverride(command, range) !== undefined) { 4828 overrides.push([command, getStateOverride(command, range)]); 4829 } 4830 }); 4831 4832 // "For each command in the list "fontName", "fontSize", "foreColor", 4833 // "hiliteColor", in order: if there is a value override for command, add 4834 // (command, command's value override) to overrides." 4835 $_(["fontname", "fontsize", "forecolor", "hilitecolor"]).forEach(function (command) { 4836 if (getValueOverride(command, range) !== undefined) { 4837 overrides.push([command, getValueOverride(command, range)]); 4838 } 4839 }); 4840 4841 // "Return overrides." 4842 return overrides; 4843 } 4844 4845 function recordCurrentStatesAndValues(range) { 4846 // "Let overrides be a list of (string, string or boolean) ordered pairs, 4847 // initially empty." 4848 var overrides = []; 4849 4850 // "Let node be the first editable Text node effectively contained in the 4851 // active range, or null if there is none." 4852 var node = $_(getAllEffectivelyContainedNodes(range)).filter(function (node) { 4853 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 4854 })[0]; 4855 4856 // "If node is null, return overrides." 4857 if (!node) { 4858 return overrides; 4859 } 4860 4861 // "Add ("createLink", value for "createLink") to overrides." 4862 overrides.push(["createlink", commands.createlink.value(range)]); 4863 4864 // "For each command in the list "bold", "italic", "strikethrough", 4865 // "subscript", "superscript", "underline", in order: if node's effective 4866 // command value for command is one of its inline command activated values, 4867 // add (command, true) to overrides, and otherwise add (command, false) to 4868 // overrides." 4869 $_(["bold", "italic", "strikethrough", "subscript", "superscript", "underline"]).forEach(function (command) { 4870 if ($_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(node, command)) != -1) { 4871 overrides.push([command, true]); 4872 } else { 4873 overrides.push([command, false]); 4874 } 4875 }); 4876 4877 // "For each command in the list "fontName", "foreColor", "hiliteColor", in 4878 // order: add (command, command's value) to overrides." 4879 4880 $_(["fontname", "fontsize", "forecolor", "hilitecolor"]).forEach(function (command) { 4881 overrides.push([command, commands[command].value(range)]); 4882 }); 4883 4884 // "Add ("fontSize", node's effective command value for "fontSize") to 4885 // overrides." 4886 overrides.push(["fontsize", getEffectiveCommandValue(node, "fontsize")]); 4887 4888 // "Return overrides." 4889 return overrides; 4890 } 4891 4892 function restoreStatesAndValues(overrides, range) { 4893 var i; 4894 var command; 4895 var override; 4896 // "Let node be the first editable Text node effectively contained in the 4897 // active range, or null if there is none." 4898 var node = $_(getAllEffectivelyContainedNodes(range)).filter(function (node) { 4899 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 4900 })[0]; 4901 4902 function isEditableTextNode(node) { 4903 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 4904 } 4905 4906 // "If node is not null, then for each (command, override) pair in 4907 // overrides, in order:" 4908 if (node) { 4909 4910 for (i = 0; i < overrides.length; i++) { 4911 command = overrides[i][0]; 4912 override = overrides[i][1]; 4913 4914 4915 // "If override is a boolean, and queryCommandState(command) 4916 // returns something different from override, call 4917 // execCommand(command)." 4918 if (typeof override == "boolean" && myQueryCommandState(command, range) != override) { 4919 myExecCommand(command, false, override, range); 4920 4921 // "Otherwise, if override is a string, and command is not 4922 // "fontSize", and queryCommandValue(command) returns something not 4923 // equivalent to override, call execCommand(command, false, 4924 // override)." 4925 } else if (typeof override == "string" && command != "fontsize" && !areEquivalentValues(command, myQueryCommandValue(command, range), override)) { 4926 myExecCommand(command, false, override, range); 4927 4928 // "Otherwise, if override is a string; and command is "fontSize"; 4929 // and either there is a value override for "fontSize" that is not 4930 // equal to override, or there is no value override for "fontSize" 4931 // and node's effective command value for "fontSize" is not loosely 4932 // equivalent to override: call execCommand("fontSize", false, 4933 // override)." 4934 } else if (typeof override == "string" 4935 && command == "fontsize" 4936 && ((getValueOverride("fontsize", range) !== undefined 4937 && getValueOverride("fontsize", range) !== override) 4938 || (getValueOverride("fontsize", range) === undefined 4939 && !areLooselyEquivalentValues(command, getEffectiveCommandValue(node, "fontsize"), override)))) { 4940 myExecCommand("fontsize", false, override, range); 4941 4942 // "Otherwise, continue this loop from the beginning." 4943 } else { 4944 continue; 4945 } 4946 4947 // "Set node to the first editable Text node effectively contained 4948 // in the active range, if there is one." 4949 node = $_(getAllEffectivelyContainedNodes(range)).filter(isEditableTextNode)[0] || node; 4950 } 4951 4952 // "Otherwise, for each (command, override) pair in overrides, in order:" 4953 } else { 4954 for (i = 0; i < overrides.length; i++) { 4955 command = overrides[i][0]; 4956 override = overrides[i][1]; 4957 4958 // "If override is a boolean, set the state override for command to 4959 // override." 4960 if (typeof override == "boolean") { 4961 setStateOverride(command, override, range); 4962 } 4963 4964 // "If override is a string, set the value override for command to 4965 // override." 4966 if (typeof override == "string") { 4967 setValueOverride(command, override, range); 4968 } 4969 } 4970 } 4971 } 4972 4973 //@} 4974 ///// Canonical space sequences ///// 4975 //@{ 4976 4977 function canonicalSpaceSequence(n, nonBreakingStart, nonBreakingEnd) { 4978 // "If n is zero, return the empty string." 4979 if (n == 0) { 4980 return ""; 4981 } 4982 4983 // "If n is one and both non-breaking start and non-breaking end are false, 4984 // return a single space (U+0020)." 4985 if (n == 1 && !nonBreakingStart && !nonBreakingEnd) { 4986 return " "; 4987 } 4988 4989 // "If n is one, return a single non-breaking space (U+00A0)." 4990 if (n == 1) { 4991 return "\xa0"; 4992 } 4993 4994 // "Let buffer be the empty string." 4995 var buffer = ""; 4996 4997 // "If non-breaking start is true, let repeated pair be U+00A0 U+0020. 4998 // Otherwise, let it be U+0020 U+00A0." 4999 var repeatedPair; 5000 if (nonBreakingStart) { 5001 repeatedPair = "\xa0 "; 5002 } else { 5003 repeatedPair = " \xa0"; 5004 } 5005 5006 // "While n is greater than three, append repeated pair to buffer and 5007 // subtract two from n." 5008 while (n > 3) { 5009 buffer += repeatedPair; 5010 n -= 2; 5011 } 5012 5013 // "If n is three, append a three-element string to buffer depending on 5014 // non-breaking start and non-breaking end:" 5015 if (n == 3) { 5016 5017 buffer += !nonBreakingStart && !nonBreakingEnd ? " \xa0 " : nonBreakingStart && !nonBreakingEnd ? "\xa0\xa0 " : !nonBreakingStart && nonBreakingEnd ? " \xa0\xa0" : nonBreakingStart && nonBreakingEnd ? "\xa0 \xa0" : "impossible"; 5018 5019 // "Otherwise, append a two-element string to buffer depending on 5020 // non-breaking start and non-breaking end:" 5021 } else { 5022 buffer += !nonBreakingStart && !nonBreakingEnd ? "\xa0 " : nonBreakingStart && !nonBreakingEnd ? "\xa0 " : !nonBreakingStart && nonBreakingEnd ? " \xa0" : nonBreakingStart && nonBreakingEnd ? "\xa0\xa0" : "impossible"; 5023 } 5024 5025 // "Return buffer." 5026 return buffer; 5027 } 5028 5029 function canonicalizeWhitespace(node, offset) { 5030 // "If node is neither editable nor an editing host, abort these steps." 5031 if (!isEditable(node) && !isEditingHost(node)) { 5032 return; 5033 } 5034 5035 // "Let start node equal node and let start offset equal offset." 5036 var startNode = node; 5037 var startOffset = offset; 5038 5039 // "Repeat the following steps:" 5040 while (true) { 5041 // "If start node has a child in the same editing host with index start 5042 // offset minus one, set start node to that child, then set start 5043 // offset to start node's length." 5044 if (0 <= startOffset - 1 && inSameEditingHost(startNode, startNode.childNodes[startOffset - 1])) { 5045 startNode = startNode.childNodes[startOffset - 1]; 5046 startOffset = getNodeLength(startNode); 5047 5048 // "Otherwise, if start offset is zero and start node does not follow a 5049 // line break and start node's parent is in the same editing host, set 5050 // start offset to start node's index, then set start node to its 5051 // parent." 5052 } else if (startOffset == 0 && !followsLineBreak(startNode) && inSameEditingHost(startNode, startNode.parentNode)) { 5053 startOffset = getNodeIndex(startNode); 5054 startNode = startNode.parentNode; 5055 5056 // "Otherwise, if start node is a Text node and its parent's resolved 5057 // value for "white-space" is neither "pre" nor "pre-wrap" and start 5058 // offset is not zero and the (start offset − 1)st element of start 5059 // node's data is a space (0x0020) or non-breaking space (0x00A0), 5060 // subtract one from start offset." 5061 } else if (startNode.nodeType == $_.Node.TEXT_NODE && jQuery.inArray($_.getComputedStyle(startNode.parentNode).whiteSpace, ["pre", "pre-wrap"]) == -1 && startOffset != 0 && /[ \xa0]/.test(startNode.data[startOffset - 1])) { 5062 startOffset--; 5063 5064 // "Otherwise, break from this loop." 5065 } else { 5066 break; 5067 } 5068 } 5069 5070 // "Let end node equal start node and end offset equal start offset." 5071 var endNode = startNode; 5072 var endOffset = startOffset; 5073 5074 // "Let length equal zero." 5075 var length = 0; 5076 5077 // "Let follows space be false." 5078 var followsSpace = false; 5079 5080 // "Repeat the following steps:" 5081 while (true) { 5082 // "If end node has a child in the same editing host with index end 5083 // offset, set end node to that child, then set end offset to zero." 5084 if (endOffset < endNode.childNodes.length && inSameEditingHost(endNode, endNode.childNodes[endOffset])) { 5085 endNode = endNode.childNodes[endOffset]; 5086 endOffset = 0; 5087 5088 // "Otherwise, if end offset is end node's length and end node does not 5089 // precede a line break and end node's parent is in the same editing 5090 // host, set end offset to one plus end node's index, then set end node 5091 // to its parent." 5092 } else if (endOffset == getNodeLength(endNode) && !precedesLineBreak(endNode) && inSameEditingHost(endNode, endNode.parentNode)) { 5093 endOffset = 1 + getNodeIndex(endNode); 5094 endNode = endNode.parentNode; 5095 5096 // "Otherwise, if end node is a Text node and its parent's resolved 5097 // value for "white-space" is neither "pre" nor "pre-wrap" and end 5098 // offset is not end node's length and the end offsetth element of 5099 // end node's data is a space (0x0020) or non-breaking space (0x00A0):" 5100 } else if (endNode.nodeType == $_.Node.TEXT_NODE && jQuery.inArray($_.getComputedStyle(endNode.parentNode).whiteSpace, ["pre", "pre-wrap"]) == -1 && endOffset != getNodeLength(endNode) && /[ \xa0]/.test(endNode.data[endOffset])) { 5101 // "If follows space is true and the end offsetth element of end 5102 // node's data is a space (0x0020), call deleteData(end offset, 1) 5103 // on end node, then continue this loop from the beginning." 5104 if (followsSpace && " " == endNode.data[endOffset]) { 5105 endNode.deleteData(endOffset, 1); 5106 continue; 5107 } 5108 5109 // "Set follows space to true if the end offsetth element of end 5110 // node's data is a space (0x0020), false otherwise." 5111 followsSpace = " " == endNode.data[endOffset]; 5112 5113 // "Add one to end offset." 5114 endOffset++; 5115 5116 // "Add one to length." 5117 length++; 5118 5119 // "Otherwise, break from this loop." 5120 } else { 5121 break; 5122 } 5123 } 5124 5125 // "Let replacement whitespace be the canonical space sequence of length 5126 // length. non-breaking start is true if start offset is zero and start 5127 // node follows a line break, and false otherwise. non-breaking end is true 5128 // if end offset is end node's length and end node precedes a line break, 5129 // and false otherwise." 5130 var replacementWhitespace = canonicalSpaceSequence(length, startOffset == 0 && followsLineBreak(startNode), endOffset == getNodeLength(endNode) && precedesLineBreak(endNode)); 5131 5132 // "While (start node, start offset) is before (end node, end offset):" 5133 while (getPosition(startNode, startOffset, endNode, endOffset) == "before") { 5134 // "If start node has a child with index start offset, set start node 5135 // to that child, then set start offset to zero." 5136 if (startOffset < startNode.childNodes.length) { 5137 startNode = startNode.childNodes[startOffset]; 5138 startOffset = 0; 5139 5140 // "Otherwise, if start node is not a Text node or if start offset is 5141 // start node's length, set start offset to one plus start node's 5142 // index, then set start node to its parent." 5143 } else if (startNode.nodeType != $_.Node.TEXT_NODE || startOffset == getNodeLength(startNode)) { 5144 startOffset = 1 + getNodeIndex(startNode); 5145 startNode = startNode.parentNode; 5146 5147 // "Otherwise:" 5148 } else { 5149 // "Remove the first element from replacement whitespace, and let 5150 // element be that element." 5151 var element = replacementWhitespace[0]; 5152 replacementWhitespace = replacementWhitespace.slice(1); 5153 5154 // "If element is not the same as the start offsetth element of 5155 // start node's data:" 5156 if (element != startNode.data[startOffset]) { 5157 // "Call insertData(start offset, element) on start node." 5158 startNode.insertData(startOffset, element); 5159 5160 // "Call deleteData(start offset + 1, 1) on start node." 5161 startNode.deleteData(startOffset + 1, 1); 5162 } 5163 5164 // "Add one to start offset." 5165 startOffset++; 5166 } 5167 } 5168 } 5169 5170 //@} 5171 ///// Deleting the contents of a range ///// 5172 //@{ 5173 5174 function deleteContents(arg1, arg2, arg3, arg4, arg5) { 5175 // We accept several different calling conventions: 5176 // 5177 // 1) A single argument, which is a range. 5178 // 5179 // 2) Two arguments, the first being a range and the second flags. 5180 // 5181 // 3) Four arguments, the start and end of a range. 5182 // 5183 // 4) Five arguments, the start and end of a range plus flags. 5184 // 5185 // The flags argument is a dictionary that can have up to two keys, 5186 // blockMerging and stripWrappers, whose corresponding values are 5187 // interpreted as boolean. E.g., {stripWrappers: false}. 5188 var range; 5189 var flags = {}; 5190 var i; 5191 5192 if (arguments.length < 3) { 5193 range = arg1; 5194 } else { 5195 range = Aloha.createRange(); 5196 range.setStart(arg1, arg2); 5197 range.setEnd(arg3, arg4); 5198 } 5199 if (arguments.length == 2) { 5200 flags = arg2; 5201 } 5202 if (arguments.length == 5) { 5203 flags = arg5; 5204 } 5205 5206 var blockMerging = null != flags.blockMerging ? !!flags.blockMerging : true; 5207 var stripWrappers = null != flags.stripWrappers ? !!flags.stripWrappers : true; 5208 5209 // "If range is null, abort these steps and do nothing." 5210 if (!range) { 5211 return; 5212 } 5213 5214 // "Let start node, start offset, end node, and end offset be range's start 5215 // and end nodes and offsets." 5216 var startNode = range.startContainer; 5217 var startOffset = range.startOffset; 5218 var endNode = range.endContainer; 5219 var endOffset = range.endOffset; 5220 var referenceNode; 5221 5222 // "While start node has at least one child:" 5223 while (startNode.hasChildNodes()) { 5224 // "If start offset is start node's length, and start node's parent is 5225 // in the same editing host, and start node is an inline node, set 5226 // start offset to one plus the index of start node, then set start 5227 // node to its parent and continue this loop from the beginning." 5228 if (startOffset == getNodeLength(startNode) && inSameEditingHost(startNode, startNode.parentNode) && isInlineNode(startNode)) { 5229 startOffset = 1 + getNodeIndex(startNode); 5230 startNode = startNode.parentNode; 5231 continue; 5232 } 5233 5234 // "If start offset is start node's length, break from this loop." 5235 if (startOffset == getNodeLength(startNode)) { 5236 break; 5237 } 5238 5239 // "Let reference node be the child of start node with index equal to 5240 // start offset." 5241 referenceNode = startNode.childNodes[startOffset]; 5242 5243 // "If reference node is a block node or an Element with no children, 5244 // or is neither an Element nor a Text node, break from this loop." 5245 if (isBlockNode(referenceNode) || (referenceNode.nodeType == $_.Node.ELEMENT_NODE && !referenceNode.hasChildNodes()) || (referenceNode.nodeType != $_.Node.ELEMENT_NODE && referenceNode.nodeType != $_.Node.TEXT_NODE)) { 5246 break; 5247 } 5248 5249 // "Set start node to reference node and start offset to 0." 5250 startNode = referenceNode; 5251 startOffset = 0; 5252 } 5253 5254 // "While end node has at least one child:" 5255 while (endNode.hasChildNodes()) { 5256 // "If end offset is 0, and end node's parent is in the same editing 5257 // host, and end node is an inline node, set end offset to the index of 5258 // end node, then set end node to its parent and continue this loop 5259 // from the beginning." 5260 if (endOffset == 0 && inSameEditingHost(endNode, endNode.parentNode) && isInlineNode(endNode)) { 5261 endOffset = getNodeIndex(endNode); 5262 endNode = endNode.parentNode; 5263 continue; 5264 } 5265 5266 // "If end offset is 0, break from this loop." 5267 if (endOffset == 0) { 5268 break; 5269 } 5270 5271 // "Let reference node be the child of end node with index equal to end 5272 // offset minus one." 5273 referenceNode = endNode.childNodes[endOffset - 1]; 5274 5275 // "If reference node is a block node or an Element with no children, 5276 // or is neither an Element nor a Text node, break from this loop." 5277 if (isBlockNode(referenceNode) || (referenceNode.nodeType == $_.Node.ELEMENT_NODE && !referenceNode.hasChildNodes()) || (referenceNode.nodeType != $_.Node.ELEMENT_NODE && referenceNode.nodeType != $_.Node.TEXT_NODE)) { 5278 break; 5279 } 5280 5281 // "Set end node to reference node and end offset to the length of 5282 // reference node." 5283 endNode = referenceNode; 5284 endOffset = getNodeLength(referenceNode); 5285 } 5286 5287 // "If (end node, end offset) is not after (start node, start offset), set 5288 // range's end to its start and abort these steps." 5289 if (getPosition(endNode, endOffset, startNode, startOffset) !== "after") { 5290 range.setEnd(range.startContainer, range.startOffset); 5291 return; 5292 } 5293 5294 // "If start node is a Text node and start offset is 0, set start offset to 5295 // the index of start node, then set start node to its parent." 5296 // Commented out for unknown reason 5297 //if (startNode.nodeType == $_.Node.TEXT_NODE && startOffset == 0 && startNode != endNode) { 5298 // startOffset = getNodeIndex(startNode); 5299 // startNode = startNode.parentNode; 5300 //} 5301 5302 // "If end node is a Text node and end offset is its length, set end offset 5303 // to one plus the index of end node, then set end node to its parent." 5304 if (endNode.nodeType == $_.Node.TEXT_NODE && endOffset == getNodeLength(endNode) && startNode != endNode) { 5305 endOffset = 1 + getNodeIndex(endNode); 5306 endNode = endNode.parentNode; 5307 } 5308 5309 // "Set range's start to (start node, start offset) and its end to (end 5310 // node, end offset)." 5311 range.setStart(startNode, startOffset); 5312 range.setEnd(endNode, endOffset); 5313 5314 // "Let start block be the start node of range." 5315 var startBlock = range.startContainer; 5316 5317 // "While start block's parent is in the same editing host and start block 5318 // is an inline node, set start block to its parent." 5319 while (inSameEditingHost(startBlock, startBlock.parentNode) && isInlineNode(startBlock)) { 5320 startBlock = startBlock.parentNode; 5321 } 5322 5323 // "If start block is neither a block node nor an editing host, or "span" 5324 // is not an allowed child of start block, or start block is a td or th, 5325 // set start block to null." 5326 if ((!isBlockNode(startBlock) && !isEditingHost(startBlock)) || !isAllowedChild("span", startBlock) || isHtmlElementInArray(startBlock, ["td", "th"])) { 5327 startBlock = null; 5328 } 5329 5330 // "Let end block be the end node of range." 5331 var endBlock = range.endContainer; 5332 5333 // "While end block's parent is in the same editing host and end block is 5334 // an inline node, set end block to its parent." 5335 while (inSameEditingHost(endBlock, endBlock.parentNode) && isInlineNode(endBlock)) { 5336 endBlock = endBlock.parentNode; 5337 } 5338 5339 // "If end block is neither a block node nor an editing host, or "span" is 5340 // not an allowed child of end block, or end block is a td or th, set end 5341 // block to null." 5342 if ((!isBlockNode(endBlock) && !isEditingHost(endBlock)) || !isAllowedChild("span", endBlock) || isHtmlElementInArray(endBlock, ["td", "th"])) { 5343 endBlock = null; 5344 } 5345 5346 // "Record current states and values, and let overrides be the result." 5347 var overrides = recordCurrentStatesAndValues(range); 5348 var parent_; 5349 // "If start node and end node are the same, and start node is an editable 5350 // Text node:" 5351 if (startNode == endNode && isEditable(startNode) && startNode.nodeType == $_.Node.TEXT_NODE) { 5352 // "Let parent be the parent of node." 5353 parent_ = startNode.parentNode; 5354 5355 // "Call deleteData(start offset, end offset − start offset) on start 5356 // node." 5357 startNode.deleteData(startOffset, endOffset - startOffset); 5358 5359 // if deleting the text moved two spaces together, we replace the left one by a , which makes the two spaces a visible 5360 // two space sequence 5361 if (startOffset > 0 && startNode.data.substr(startOffset - 1, 1) === ' ' && startOffset < startNode.data.length && startNode.data.substr(startOffset, 1) === ' ') { 5362 startNode.replaceData(startOffset - 1, 1, '\xa0'); 5363 } 5364 5365 // "Canonicalize whitespace at (start node, start offset)." 5366 canonicalizeWhitespace(startNode, startOffset); 5367 5368 // "Set range's end to its start." 5369 // Ok, also set the range's start to its start, because modifying the text 5370 // might have somehow corrupted the range 5371 range.setStart(range.startContainer, range.startOffset); 5372 range.setEnd(range.startContainer, range.startOffset); 5373 5374 // "Restore states and values from overrides." 5375 restoreStatesAndValues(overrides, range); 5376 5377 // "If parent is editable or an editing host, is not an inline node, 5378 // and has no children, call createElement("br") on the context object 5379 // and append the result as the last child of parent." 5380 // only do this, if the offsetHeight is 0 5381 if ((isEditable(parent_) || isEditingHost(parent_)) && !isInlineNode(parent_)) { 5382 // TODO is this always correct? 5383 ensureContainerEditable(parent_); 5384 } 5385 5386 // "Abort these steps." 5387 return; 5388 } 5389 5390 // "If start node is an editable Text node, call deleteData() on it, with 5391 // start offset as the first argument and (length of start node − start 5392 // offset) as the second argument." 5393 if (isEditable(startNode) && startNode.nodeType == $_.Node.TEXT_NODE) { 5394 startNode.deleteData(startOffset, getNodeLength(startNode) - startOffset); 5395 } 5396 5397 // "Let node list be a list of nodes, initially empty." 5398 // 5399 // "For each node contained in range, append node to node list if the last 5400 // member of node list (if any) is not an ancestor of node; node is 5401 // editable; and node is not a thead, tbody, tfoot, tr, th, or td." 5402 var nodeList = getContainedNodes( 5403 range, 5404 function (node) { 5405 return isEditable(node) && !isHtmlElementInArray(node, ["thead", "tbody", "tfoot", "tr", "th", "td"]); 5406 } 5407 ); 5408 5409 // "For each node in node list:" 5410 for (i = 0; i < nodeList.length; i++) { 5411 var node = nodeList[i]; 5412 5413 // "Let parent be the parent of node." 5414 parent_ = node.parentNode; 5415 5416 // "Remove node from parent." 5417 parent_.removeChild(node); 5418 5419 // "If strip wrappers is true or parent is not an ancestor container of 5420 // start node, while parent is an editable inline node with length 0, 5421 // let grandparent be the parent of parent, then remove parent from 5422 // grandparent, then set parent to grandparent." 5423 if (stripWrappers || (!isAncestor(parent_, startNode) && parent_ != startNode)) { 5424 while (isEditable(parent_) && isInlineNode(parent_) && getNodeLength(parent_) == 0) { 5425 var grandparent = parent_.parentNode; 5426 grandparent.removeChild(parent_); 5427 parent_ = grandparent; 5428 } 5429 } 5430 5431 // "If parent is editable or an editing host, is not an inline node, 5432 // and has no children, call createElement("br") on the context object 5433 // and append the result as the last child of parent." 5434 // only do this, if the offsetHeight is 0 5435 if ((isEditable(parent_) || isEditingHost(parent_)) && !isInlineNode(parent_)) { 5436 ensureContainerEditable(parent_); 5437 } 5438 } 5439 5440 // "If end node is an editable Text node, call deleteData(0, end offset) on 5441 // it." 5442 if (isEditable(endNode) && endNode.nodeType == $_.Node.TEXT_NODE) { 5443 endNode.deleteData(0, endOffset); 5444 } 5445 5446 // "Canonicalize whitespace at range's start." 5447 canonicalizeWhitespace(range.startContainer, range.startOffset); 5448 5449 // "Canonicalize whitespace at range's end." 5450 canonicalizeWhitespace(range.endContainer, range.endOffset); 5451 5452 // "If block merging is false, or start block or end block is null, or 5453 // start block is not in the same editing host as end block, or start block 5454 // and end block are the same:" 5455 if (!blockMerging || !startBlock || !endBlock || !inSameEditingHost(startBlock, endBlock) || startBlock == endBlock) { 5456 // "Set range's end to its start." 5457 range.setEnd(range.startContainer, range.startOffset); 5458 5459 // "Restore states and values from overrides." 5460 restoreStatesAndValues(overrides, range); 5461 5462 // "Abort these steps." 5463 return; 5464 } 5465 5466 // "If start block has one child, which is a collapsed block prop, remove 5467 // its child from it." 5468 if (startBlock.children.length == 1 && isCollapsedBlockProp(startBlock.firstChild)) { 5469 startBlock.removeChild(startBlock.firstChild); 5470 } 5471 5472 // "If end block has one child, which is a collapsed block prop, remove its 5473 // child from it." 5474 if (endBlock.children.length == 1 && isCollapsedBlockProp(endBlock.firstChild)) { 5475 endBlock.removeChild(endBlock.firstChild); 5476 } 5477 5478 var values; 5479 // "If start block is an ancestor of end block:" 5480 if (isAncestor(startBlock, endBlock)) { 5481 // "Let reference node be end block." 5482 referenceNode = endBlock; 5483 5484 // "While reference node is not a child of start block, set reference 5485 // node to its parent." 5486 while (referenceNode.parentNode != startBlock) { 5487 referenceNode = referenceNode.parentNode; 5488 } 5489 5490 // "Set the start and end of range to (start block, index of reference 5491 // node)." 5492 range.setStart(startBlock, getNodeIndex(referenceNode)); 5493 range.setEnd(startBlock, getNodeIndex(referenceNode)); 5494 5495 // "If end block has no children:" 5496 if (!endBlock.hasChildNodes()) { 5497 // "While end block is editable and is the only child of its parent 5498 // and is not a child of start block, let parent equal end block, 5499 // then remove end block from parent, then set end block to 5500 // parent." 5501 while (isEditable(endBlock) && endBlock.parentNode.childNodes.length == 1 && endBlock.parentNode != startBlock) { 5502 parent_ = endBlock; 5503 parent_.removeChild(endBlock); 5504 endBlock = parent_; 5505 } 5506 5507 // "If end block is editable and is not an inline node, and its 5508 // previousSibling and nextSibling are both inline nodes, call 5509 // createElement("br") on the context object and insert it into end 5510 // block's parent immediately after end block." 5511 5512 if (isEditable(endBlock) && !isInlineNode(endBlock) && isInlineNode(endBlock.previousSibling) && isInlineNode(endBlock.nextSibling)) { 5513 endBlock.parentNode.insertBefore(document.createElement("br"), endBlock.nextSibling); 5514 } 5515 5516 // "If end block is editable, remove it from its parent." 5517 if (isEditable(endBlock)) { 5518 endBlock.parentNode.removeChild(endBlock); 5519 } 5520 5521 // "Restore states and values from overrides." 5522 restoreStatesAndValues(overrides, range); 5523 5524 // "Abort these steps." 5525 return; 5526 } 5527 5528 // "If end block's firstChild is not an inline node, restore states and 5529 // values from overrides, then abort these steps." 5530 if (!isInlineNode(endBlock.firstChild)) { 5531 restoreStatesAndValues(overrides, range); 5532 return; 5533 } 5534 5535 // "Let children be a list of nodes, initially empty." 5536 5537 var children = []; 5538 5539 // "Append the first child of end block to children." 5540 children.push(endBlock.firstChild); 5541 5542 // "While children's last member is not a br, and children's last 5543 // member's nextSibling is an inline node, append children's last 5544 // member's nextSibling to children." 5545 while (!isNamedHtmlElement(children[children.length - 1], "br") && isInlineNode(children[children.length - 1].nextSibling)) { 5546 children.push(children[children.length - 1].nextSibling); 5547 } 5548 5549 // "Record the values of children, and let values be the result." 5550 values = recordValues(children); 5551 5552 // "While children's first member's parent is not start block, split 5553 // the parent of children." 5554 while (children[0].parentNode != startBlock) { 5555 splitParent(children, range); 5556 } 5557 5558 // "If children's first member's previousSibling is an editable br, 5559 // remove that br from its parent." 5560 if (isEditable(children[0].previousSibling) && isNamedHtmlElement(children[0].previousSibling, "br")) { 5561 children[0].parentNode.removeChild(children[0].previousSibling); 5562 } 5563 5564 // "Otherwise, if start block is a descendant of end block:" 5565 } else if (isDescendant(startBlock, endBlock)) { 5566 // "Set the start and end of range to (start block, length of start 5567 // block)." 5568 range.setStart(startBlock, getNodeLength(startBlock)); 5569 range.setEnd(startBlock, getNodeLength(startBlock)); 5570 5571 // "Let reference node be start block." 5572 referenceNode = startBlock; 5573 5574 // "While reference node is not a child of end block, set reference 5575 // node to its parent." 5576 while (referenceNode.parentNode != endBlock) { 5577 referenceNode = referenceNode.parentNode; 5578 } 5579 5580 // "If reference node's nextSibling is an inline node and start block's 5581 // lastChild is a br, remove start block's lastChild from it." 5582 if (isInlineNode(referenceNode.nextSibling) && isNamedHtmlElement(startBlock.lastChild, "br")) { 5583 startBlock.removeChild(startBlock.lastChild); 5584 } 5585 5586 // "Let nodes to move be a list of nodes, initially empty." 5587 var nodesToMove = []; 5588 5589 5590 // "If reference node's nextSibling is neither null nor a br nor a 5591 // block node, append it to nodes to move." 5592 if (referenceNode.nextSibling && !isNamedHtmlElement(referenceNode.nextSibling, "br") && !isBlockNode(referenceNode.nextSibling)) { 5593 nodesToMove.push(referenceNode.nextSibling); 5594 } 5595 5596 // "While nodes to move is nonempty and its last member's nextSibling 5597 // is neither null nor a br nor a block node, append it to nodes to 5598 // move." 5599 if (nodesToMove.length && nodesToMove[nodesToMove.length - 1].nextSibling && !isNamedHtmlElement(nodesToMove[nodesToMove.length - 1].nextSibling, "br") && !isBlockNode(nodesToMove[nodesToMove.length - 1].nextSibling)) { 5600 nodesToMove.push(nodesToMove[nodesToMove.length - 1].nextSibling); 5601 } 5602 5603 // "Record the values of nodes to move, and let values be the result." 5604 values = recordValues(nodesToMove); 5605 5606 // "For each node in nodes to move, append node as the last child of 5607 // start block, preserving ranges." 5608 $_(nodesToMove).forEach(function (node) { 5609 5610 movePreservingRanges(node, startBlock, -1, range); 5611 }); 5612 5613 // "If the nextSibling of reference node is a br, remove it from its 5614 // parent." 5615 if (isNamedHtmlElement(referenceNode.nextSibling, "br")) { 5616 referenceNode.parentNode.removeChild(referenceNode.nextSibling); 5617 } 5618 5619 // "Otherwise:" 5620 } else { 5621 // "Set the start and end of range to (start block, length of start 5622 // block)." 5623 range.setStart(startBlock, getNodeLength(startBlock)); 5624 range.setEnd(startBlock, getNodeLength(startBlock)); 5625 5626 // "If end block's firstChild is an inline node and start block's 5627 // lastChild is a br, remove start block's lastChild from it." 5628 if (isInlineNode(endBlock.firstChild) && isNamedHtmlElement(startBlock.lastChild, "br")) { 5629 startBlock.removeChild(startBlock.lastChild); 5630 } 5631 5632 // "Record the values of end block's children, and let values be the 5633 // result." 5634 values = recordValues([].slice.call(toArray(endBlock.childNodes))); 5635 5636 // "While end block has children, append the first child of end block 5637 // to start block, preserving ranges." 5638 while (endBlock.hasChildNodes()) { 5639 movePreservingRanges(endBlock.firstChild, startBlock, -1, range); 5640 } 5641 5642 // "While end block has no children, let parent be the parent of end 5643 // block, then remove end block from parent, then set end block to 5644 // parent." 5645 while (!endBlock.hasChildNodes()) { 5646 parent_ = endBlock.parentNode; 5647 parent_.removeChild(endBlock); 5648 endBlock = parent_; 5649 } 5650 } 5651 5652 // "Restore the values from values." 5653 restoreValues(values, range); 5654 5655 // "If start block has no children, call createElement("br") on the context 5656 // object and append the result as the last child of start block." 5657 ensureContainerEditable(startBlock); 5658 5659 // "Restore states and values from overrides." 5660 restoreStatesAndValues(overrides, range); 5661 } 5662 5663 // "To remove a node node while preserving its descendants, split the parent of 5664 // node's children if it has any. If it has no children, instead remove it from 5665 // its parent." 5666 function removePreservingDescendants(node, range) { 5667 if (node.hasChildNodes()) { 5668 splitParent([].slice.call(toArray(node.childNodes)), range); 5669 } else { 5670 node.parentNode.removeChild(node); 5671 } 5672 } 5673 5674 //@} 5675 ///// Indenting and outdenting ///// 5676 //@{ 5677 5678 function cleanLists(node, range) { 5679 // remove any whitespace nodes around list nodes 5680 if (node) { 5681 jQuery(node).find('ul,ol,li').each(function () { 5682 jQuery(this).contents().each(function () { 5683 if (isWhitespaceNode(this)) { 5684 var index = getNodeIndex(this); 5685 5686 // if the range points to somewhere behind the removed text node, we reduce the offset 5687 if (range.startContainer === this.parentNode && range.startOffset > index) { 5688 range.startOffset--; 5689 } else if (range.startContainer === this) { 5690 // the range starts in the removed text node, let it start right before 5691 range.startContainer = this.parentNode; 5692 range.startOffset = index; 5693 } 5694 // same thing for end of the range 5695 if (range.endContainer === this.parentNode && range.endOffset > index) { 5696 range.endOffset--; 5697 } else if (range.endContainer === this) { 5698 range.endContainer = this.parentNode; 5699 range.endOffset = index; 5700 } 5701 // finally remove the whitespace node 5702 jQuery(this).remove(); 5703 } 5704 }); 5705 }); 5706 } 5707 } 5708 5709 5710 //@} 5711 ///// Indenting and outdenting ///// 5712 //@{ 5713 5714 function indentNodes(nodeList, range) { 5715 // "If node list is empty, do nothing and abort these steps." 5716 if (!nodeList.length) { 5717 return; 5718 } 5719 5720 // "Let first node be the first member of node list." 5721 var firstNode = nodeList[0]; 5722 5723 // "If first node's parent is an ol or ul:" 5724 if (isHtmlElementInArray(firstNode.parentNode, ["OL", "UL"])) { 5725 // "Let tag be the local name of the parent of first node." 5726 var tag = firstNode.parentNode.tagName; 5727 5728 // "Wrap node list, with sibling criteria returning true for an HTML 5729 // element with local name tag and false otherwise, and new parent 5730 // instructions returning the result of calling createElement(tag) on 5731 // the ownerDocument of first node." 5732 wrap( 5733 nodeList, 5734 function (node) { 5735 return isHtmlElement_obsolete(node, tag); 5736 }, 5737 function () { 5738 return firstNode.ownerDocument.createElement(tag); 5739 }, 5740 range 5741 ); 5742 5743 // "Abort these steps." 5744 return; 5745 } 5746 5747 // "Wrap node list, with sibling criteria returning true for a simple 5748 // indentation element and false otherwise, and new parent instructions 5749 // returning the result of calling createElement("blockquote") on the 5750 // ownerDocument of first node. Let new parent be the result." 5751 var newParent = wrap( 5752 nodeList, 5753 function (node) { 5754 return isSimpleIndentationElement(node); 5755 }, 5756 function () { 5757 return firstNode.ownerDocument.createElement("blockquote"); 5758 }, 5759 range 5760 ); 5761 5762 // "Fix disallowed ancestors of new parent." 5763 fixDisallowedAncestors(newParent, range); 5764 } 5765 5766 function outdentNode(node, range) { 5767 // "If node is not editable, abort these steps." 5768 if (!isEditable(node)) { 5769 return; 5770 } 5771 5772 // "If node is a simple indentation element, remove node, preserving its 5773 // descendants. Then abort these steps." 5774 if (isSimpleIndentationElement(node)) { 5775 removePreservingDescendants(node, range); 5776 return; 5777 } 5778 5779 // "If node is an indentation element:" 5780 if (isIndentationElement(node)) { 5781 // "Unset the class and dir attributes of node, if any." 5782 node.removeAttribute("class"); 5783 node.removeAttribute("dir"); 5784 5785 // "Unset the margin, padding, and border CSS properties of node." 5786 node.style.margin = ""; 5787 node.style.padding = ""; 5788 node.style.border = ""; 5789 if (node.getAttribute("style") == "") { 5790 node.removeAttribute("style"); 5791 } 5792 5793 // "Set the tag name of node to "div"." 5794 setTagName(node, "div", range); 5795 5796 // "Abort these steps." 5797 return; 5798 } 5799 5800 // "Let current ancestor be node's parent." 5801 var currentAncestor = node.parentNode; 5802 5803 // "Let ancestor list be a list of nodes, initially empty." 5804 var ancestorList = []; 5805 5806 // "While current ancestor is an editable Element that is neither a simple 5807 // indentation element nor an ol nor a ul, append current ancestor to 5808 // ancestor list and then set current ancestor to its parent." 5809 while (isEditable(currentAncestor) && currentAncestor.nodeType == $_.Node.ELEMENT_NODE && !isSimpleIndentationElement(currentAncestor) && !isHtmlElementInArray(currentAncestor, ["ol", "ul"])) { 5810 ancestorList.push(currentAncestor); 5811 currentAncestor = currentAncestor.parentNode; 5812 } 5813 5814 // "If current ancestor is not an editable simple indentation element:" 5815 if (!isEditable(currentAncestor) || !isSimpleIndentationElement(currentAncestor)) { 5816 // "Let current ancestor be node's parent." 5817 currentAncestor = node.parentNode; 5818 5819 // "Let ancestor list be the empty list." 5820 ancestorList = []; 5821 5822 // "While current ancestor is an editable Element that is neither an 5823 // indentation element nor an ol nor a ul, append current ancestor to 5824 // ancestor list and then set current ancestor to its parent." 5825 while (isEditable(currentAncestor) && currentAncestor.nodeType == $_.Node.ELEMENT_NODE && !isIndentationElement(currentAncestor) && !isHtmlElementInArray(currentAncestor, ["ol", "ul"])) { 5826 ancestorList.push(currentAncestor); 5827 currentAncestor = currentAncestor.parentNode; 5828 } 5829 } 5830 5831 // "If node is an ol or ul and current ancestor is not an editable 5832 // indentation element:" 5833 if (isHtmlElementInArray(node, ["OL", "UL"]) && (!isEditable(currentAncestor) || !isIndentationElement(currentAncestor))) { 5834 // "Unset the reversed, start, and type attributes of node, if any are 5835 // set." 5836 node.removeAttribute("reversed"); 5837 node.removeAttribute("start"); 5838 node.removeAttribute("type"); 5839 5840 // "Let children be the children of node." 5841 var children = [].slice.call(toArray(node.childNodes)); 5842 5843 // "If node has attributes, and its parent is not an ol or ul, set the 5844 // tag name of node to "div"." 5845 if (node.attributes.length && !isHtmlElementInArray(node.parentNode, ["OL", "UL"])) { 5846 setTagName(node, "div", range); 5847 5848 // "Otherwise:" 5849 } else { 5850 // "Record the values of node's children, and let values be the 5851 // result." 5852 var values = recordValues([].slice.call(toArray(node.childNodes))); 5853 5854 // "Remove node, preserving its descendants." 5855 removePreservingDescendants(node, range); 5856 5857 // "Restore the values from values." 5858 restoreValues(values, range); 5859 } 5860 5861 // "Fix disallowed ancestors of each member of children." 5862 var i; 5863 for (i = 0; i < children.length; i++) { 5864 fixDisallowedAncestors(children[i], range); 5865 } 5866 5867 // "Abort these steps." 5868 return; 5869 } 5870 5871 // "If current ancestor is not an editable indentation element, abort these 5872 // steps." 5873 if (!isEditable(currentAncestor) || !isIndentationElement(currentAncestor)) { 5874 return; 5875 } 5876 5877 // "Append current ancestor to ancestor list." 5878 ancestorList.push(currentAncestor); 5879 5880 // "Let original ancestor be current ancestor." 5881 var originalAncestor = currentAncestor; 5882 5883 // "While ancestor list is not empty:" 5884 while (ancestorList.length) { 5885 // "Let current ancestor be the last member of ancestor list." 5886 // 5887 // "Remove the last member of ancestor list." 5888 currentAncestor = ancestorList.pop(); 5889 5890 // "Let target be the child of current ancestor that is equal to either 5891 // node or the last member of ancestor list." 5892 var target = node.parentNode == currentAncestor ? node : ancestorList[ancestorList.length - 1]; 5893 5894 // "If target is an inline node that is not a br, and its nextSibling 5895 // is a br, remove target's nextSibling from its parent." 5896 if (isInlineNode(target) && !isNamedHtmlElement(target, 'BR') && isNamedHtmlElement(target.nextSibling, "BR")) { 5897 target.parentNode.removeChild(target.nextSibling); 5898 } 5899 5900 // "Let preceding siblings be the preceding siblings of target, and let 5901 // following siblings be the following siblings of target." 5902 var precedingSiblings = [].slice.call(toArray(currentAncestor.childNodes), 0, getNodeIndex(target)); 5903 var followingSiblings = [].slice.call(toArray(currentAncestor.childNodes), 1 + getNodeIndex(target)); 5904 5905 // "Indent preceding siblings." 5906 indentNodes(precedingSiblings, range); 5907 5908 // "Indent following siblings." 5909 indentNodes(followingSiblings, range); 5910 } 5911 5912 // "Outdent original ancestor." 5913 outdentNode(originalAncestor, range); 5914 } 5915 5916 5917 //@} 5918 ///// Toggling lists ///// 5919 //@{ 5920 5921 function toggleLists(tagName, range) { 5922 // "Let mode be "disable" if the selection's list state is tag name, and 5923 // "enable" otherwise." 5924 var mode = getSelectionListState() == tagName ? "disable" : "enable"; 5925 5926 tagName = tagName.toUpperCase(); 5927 5928 // "Let other tag name be "ol" if tag name is "ul", and "ul" if tag name is 5929 // "ol"." 5930 var otherTagName = tagName == "OL" ? "UL" : "OL"; 5931 5932 // "Let items be a list of all lis that are ancestor containers of the 5933 // range's start and/or end node." 5934 // 5935 // It's annoying to get this in tree order using functional stuff without 5936 // doing getDescendants(document), which is slow, so I do it imperatively. 5937 var items = []; 5938 (function () { 5939 var ancestorContainer; 5940 for (ancestorContainer = range.endContainer; 5941 ancestorContainer != range.commonAncestorContainer; 5942 ancestorContainer = ancestorContainer.parentNode) { 5943 if (isNamedHtmlElement(ancestorContainer, "li")) { 5944 items.unshift(ancestorContainer); 5945 } 5946 } 5947 for (ancestorContainer = range.startContainer; 5948 ancestorContainer; 5949 ancestorContainer = ancestorContainer.parentNode) { 5950 if (isNamedHtmlElement(ancestorContainer, "li")) { 5951 items.unshift(ancestorContainer); 5952 } 5953 } 5954 }()); 5955 5956 // "For each item in items, normalize sublists of item." 5957 $_(items).forEach(function (thisArg) { 5958 normalizeSublists(thisArg, range); 5959 }); 5960 5961 // "Block-extend the range, and let new range be the result." 5962 var newRange = blockExtend(range); 5963 5964 // "If mode is "enable", then let lists to convert consist of every 5965 // editable HTML element with local name other tag name that is contained 5966 // in new range, and for every list in lists to convert:" 5967 if (mode == "enable") { 5968 $_(getAllContainedNodes(newRange, function (node) { 5969 return isEditable(node) && isHtmlElement_obsolete(node, otherTagName); 5970 })).forEach(function (list) { 5971 // "If list's previousSibling or nextSibling is an editable HTML 5972 // element with local name tag name:" 5973 if ((isEditable(list.previousSibling) && isHtmlElement_obsolete(list.previousSibling, tagName)) || (isEditable(list.nextSibling) && isHtmlElement_obsolete(list.nextSibling, tagName))) { 5974 // "Let children be list's children." 5975 var children = [].slice.call(toArray(list.childNodes)); 5976 5977 // "Record the values of children, and let values be the 5978 // result." 5979 var values = recordValues(children); 5980 5981 // "Split the parent of children." 5982 splitParent(children, range); 5983 5984 // "Wrap children, with sibling criteria returning true for an 5985 // HTML element with local name tag name and false otherwise." 5986 wrap( 5987 children, 5988 function (node) { 5989 return isHtmlElement_obsolete(node, tagName); 5990 }, 5991 function () { 5992 return null; 5993 }, 5994 range 5995 ); 5996 5997 // "Restore the values from values." 5998 restoreValues(values, range); 5999 6000 // "Otherwise, set the tag name of list to tag name." 6001 } else { 6002 setTagName(list, tagName, range); 6003 } 6004 }); 6005 } 6006 6007 // "Let node list be a list of nodes, initially empty." 6008 // 6009 // "For each node node contained in new range, if node is editable; the 6010 6011 // last member of node list (if any) is not an ancestor of node; node 6012 // is not an indentation element; and either node is an ol or ul, or its 6013 // parent is an ol or ul, or it is an allowed child of "li"; then append 6014 // node to node list." 6015 var nodeList = getContainedNodes(newRange, function (node) { 6016 return isEditable(node) && !isIndentationElement(node) && (isHtmlElementInArray(node, ["OL", "UL"]) || isHtmlElementInArray(node.parentNode, ["OL", "UL"]) || isAllowedChild(node, "li")); 6017 }); 6018 6019 // "If mode is "enable", remove from node list any ol or ul whose parent is 6020 // not also an ol or ul." 6021 if (mode == "enable") { 6022 nodeList = $_(nodeList).filter(function (node) { 6023 return !isHtmlElementInArray(node, ["ol", "ul"]) || isHtmlElementInArray(node.parentNode, ["ol", "ul"]); 6024 }); 6025 } 6026 6027 // "If mode is "disable", then while node list is not empty:" 6028 var sublist, values; 6029 6030 function createLi() { 6031 return document.createElement("li"); 6032 } 6033 6034 function isOlUl(node) { 6035 return isHtmlElementInArray(node, ["ol", "ul"]); 6036 } 6037 6038 function makeIsElementPred(tagName) { 6039 return function (node) { 6040 return isHtmlElement_obsolete(node, tagName); 6041 }; 6042 } 6043 6044 function makeCreateElement(tagName) { 6045 return function () { 6046 return document.createElement(tagName); 6047 }; 6048 } 6049 6050 function makeCreateElementSublist(tagName, sublist, range) { 6051 return function () { 6052 // "If sublist's first member's parent is not an editable 6053 // simple indentation element, or sublist's first member's 6054 // parent's previousSibling is not an editable HTML element 6055 // with local name tag name, call createElement(tag name) 6056 // on the context object and return the result." 6057 if (!isEditable(sublist[0].parentNode) || !isSimpleIndentationElement(sublist[0].parentNode) || !isEditable(sublist[0].parentNode.previousSibling) || !isHtmlElement_obsolete(sublist[0].parentNode.previousSibling, tagName)) { 6058 return document.createElement(tagName); 6059 } 6060 6061 // "Let list be sublist's first member's parent's 6062 // previousSibling." 6063 var list = sublist[0].parentNode.previousSibling; 6064 6065 // "Normalize sublists of list's lastChild." 6066 normalizeSublists(list.lastChild, range); 6067 6068 // "If list's lastChild is not an editable HTML element 6069 // with local name tag name, call createElement(tag name) 6070 // on the context object, and append the result as the last 6071 // child of list." 6072 if (!isEditable(list.lastChild) || !isHtmlElement_obsolete(list.lastChild, tagName)) { 6073 list.appendChild(document.createElement(tagName)); 6074 } 6075 6076 // "Return the last child of list." 6077 return list.lastChild; 6078 }; 6079 } 6080 6081 if (mode == "disable") { 6082 while (nodeList.length) { 6083 // "Let sublist be an empty list of nodes." 6084 sublist = []; 6085 6086 // "Remove the first member from node list and append it to 6087 // sublist." 6088 sublist.push(nodeList.shift()); 6089 6090 // "If the first member of sublist is an HTML element with local 6091 // name tag name, outdent it and continue this loop from the 6092 // beginning." 6093 if (isHtmlElement_obsolete(sublist[0], tagName)) { 6094 outdentNode(sublist[0], range); 6095 continue; 6096 } 6097 6098 // "While node list is not empty, and the first member of node list 6099 // is the nextSibling of the last member of sublist and is not an 6100 // HTML element with local name tag name, remove the first member 6101 // from node list and append it to sublist." 6102 while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isHtmlElement_obsolete(nodeList[0], tagName)) { 6103 sublist.push(nodeList.shift()); 6104 } 6105 6106 // "Record the values of sublist, and let values be the result." 6107 values = recordValues(sublist); 6108 6109 // "Split the parent of sublist." 6110 splitParent(sublist, range); 6111 6112 // "Fix disallowed ancestors of each member of sublist." 6113 var i; 6114 for (i = 0; i < sublist.length; i++) { 6115 fixDisallowedAncestors(sublist[i], range); 6116 } 6117 6118 // "Restore the values from values." 6119 restoreValues(values, range); 6120 } 6121 6122 // "Otherwise, while node list is not empty:" 6123 } else { 6124 while (nodeList.length) { 6125 // "Let sublist be an empty list of nodes." 6126 sublist = []; 6127 6128 // "While either sublist is empty, or node list is not empty and 6129 // its first member is the nextSibling of sublist's last member:" 6130 while (!sublist.length || (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling)) { 6131 // "If node list's first member is a p or div, set the tag name 6132 // of node list's first member to "li", and append the result 6133 // to sublist. Remove the first member from node list." 6134 if (isHtmlElementInArray(nodeList[0], ["p", "div"])) { 6135 sublist.push(setTagName(nodeList[0], "li", range)); 6136 nodeList.shift(); 6137 6138 // "Otherwise, if the first member of node list is an li or ol 6139 // or ul, remove it from node list and append it to sublist." 6140 } else if (isHtmlElementInArray(nodeList[0], ["li", "ol", "ul"])) { 6141 sublist.push(nodeList.shift()); 6142 6143 // "Otherwise:" 6144 } else { 6145 // "Let nodes to wrap be a list of nodes, initially empty." 6146 var nodesToWrap = []; 6147 6148 // "While nodes to wrap is empty, or node list is not empty 6149 // and its first member is the nextSibling of nodes to 6150 // wrap's last member and the first member of node list is 6151 // an inline node and the last member of nodes to wrap is 6152 // an inline node other than a br, remove the first member 6153 // from node list and append it to nodes to wrap." 6154 while (!nodesToWrap.length || (nodeList.length && nodeList[0] == nodesToWrap[nodesToWrap.length - 1].nextSibling && isInlineNode(nodeList[0]) && isInlineNode(nodesToWrap[nodesToWrap.length - 1]) && !isNamedHtmlElement(nodesToWrap[nodesToWrap.length - 1], "br"))) { 6155 nodesToWrap.push(nodeList.shift()); 6156 } 6157 6158 // "Wrap nodes to wrap, with new parent instructions 6159 // returning the result of calling createElement("li") on 6160 // the context object. Append the result to sublist." 6161 sublist.push(wrap( 6162 nodesToWrap, 6163 undefined, 6164 createLi, 6165 range 6166 )); 6167 } 6168 } 6169 6170 // "If sublist's first member's parent is an HTML element with 6171 // local name tag name, or if every member of sublist is an ol or 6172 // ul, continue this loop from the beginning." 6173 if (isHtmlElement_obsolete(sublist[0].parentNode, tagName) || $_(sublist).every(isOlUl)) { 6174 continue; 6175 } 6176 6177 // "If sublist's first member's parent is an HTML element with 6178 // local name other tag name:" 6179 if (isHtmlElement_obsolete(sublist[0].parentNode, otherTagName)) { 6180 // "Record the values of sublist, and let values be the 6181 // result." 6182 values = recordValues(sublist); 6183 6184 // "Split the parent of sublist." 6185 splitParent(sublist, range); 6186 6187 // "Wrap sublist, with sibling criteria returning true for an 6188 // HTML element with local name tag name and false otherwise, 6189 // and new parent instructions returning the result of calling 6190 // createElement(tag name) on the context object." 6191 wrap( 6192 sublist, 6193 makeIsElementPred(tagName), 6194 makeCreateElement(tagName), 6195 range 6196 ); 6197 6198 // "Restore the values from values." 6199 restoreValues(values, range); 6200 6201 // "Continue this loop from the beginning." 6202 continue; 6203 } 6204 6205 // "Wrap sublist, with sibling criteria returning true for an HTML 6206 // element with local name tag name and false otherwise, and new 6207 // parent instructions being the following:" 6208 // . . . 6209 // "Fix disallowed ancestors of the previous step's result." 6210 fixDisallowedAncestors(wrap( 6211 sublist, 6212 makeIsElementPred(tagName), 6213 makeCreateElementSublist(tagName, sublist, range), 6214 range 6215 ), range); 6216 } 6217 } 6218 } 6219 6220 6221 //@} 6222 ///// Justifying the selection ///// 6223 //@{ 6224 6225 function justifySelection(alignment, range) { 6226 6227 // "Block-extend the active range, and let new range be the result." 6228 var newRange = blockExtend(range); 6229 6230 // "Let element list be a list of all editable Elements contained in new 6231 // range that either has an attribute in the HTML namespace whose local 6232 // name is "align", or has a style attribute that sets "text-align", or is 6233 // a center." 6234 var elementList = getAllContainedNodes(newRange, function (node) { 6235 return node.nodeType == $_.Node.ELEMENT_NODE && isEditable(node) 6236 // Ignoring namespaces here 6237 && (hasAttribute(node, "align") || node.style.textAlign != "" || isNamedHtmlElement(node, 'center')); 6238 }); 6239 6240 // "For each element in element list:" 6241 var i; 6242 for (i = 0; i < elementList.length; i++) { 6243 var element = elementList[i]; 6244 6245 // "If element has an attribute in the HTML namespace whose local name 6246 // is "align", remove that attribute." 6247 element.removeAttribute("align"); 6248 6249 // "Unset the CSS property "text-align" on element, if it's set by a 6250 // style attribute." 6251 element.style.textAlign = ""; 6252 if (element.getAttribute("style") == "") { 6253 element.removeAttribute("style"); 6254 } 6255 6256 // "If element is a div or span or center with no attributes, remove 6257 // it, preserving its descendants." 6258 if (isHtmlElementInArray(element, ["div", "span", "center"]) && !element.attributes.length) { 6259 removePreservingDescendants(element, range); 6260 } 6261 6262 // "If element is a center with one or more attributes, set the tag 6263 // name of element to "div"." 6264 if (isNamedHtmlElement(element, 'center') && element.attributes.length) { 6265 setTagName(element, "div", range); 6266 } 6267 } 6268 6269 // "Block-extend the active range, and let new range be the result." 6270 newRange = blockExtend(globalRange); 6271 6272 // "Let node list be a list of nodes, initially empty." 6273 var nodeList = []; 6274 6275 // "For each node node contained in new range, append node to node list if 6276 // the last member of node list (if any) is not an ancestor of node; node 6277 // is editable; node is an allowed child of "div"; and node's alignment 6278 // value is not alignment." 6279 nodeList = getContainedNodes(newRange, function (node) { 6280 return isEditable(node) && isAllowedChild(node, "div") && getAlignmentValue(node) != alignment; 6281 }); 6282 6283 function makeIsAlignedDiv(alignment) { 6284 return function (node) { 6285 return isNamedHtmlElement(node, 'div') && $_(node.attributes).every(function (attr) { 6286 return (attr.name == "align" && attr.value.toLowerCase() == alignment) || (attr.name == "style" && getStyleLength(node) == 1 && node.style.textAlign == alignment); 6287 }); 6288 }; 6289 } 6290 6291 function makeCreateAlignedDiv(alignment) { 6292 return function () { 6293 var newParent = document.createElement("div"); 6294 newParent.setAttribute("style", "text-align: " + alignment); 6295 return newParent; 6296 }; 6297 } 6298 6299 // "While node list is not empty:" 6300 while (nodeList.length) { 6301 // "Let sublist be a list of nodes, initially empty." 6302 var sublist = []; 6303 6304 // "Remove the first member of node list and append it to sublist." 6305 sublist.push(nodeList.shift()); 6306 6307 // "While node list is not empty, and the first member of node list is 6308 // the nextSibling of the last member of sublist, remove the first 6309 // member of node list and append it to sublist." 6310 while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling) { 6311 sublist.push(nodeList.shift()); 6312 } 6313 6314 // "Wrap sublist. Sibling criteria returns true for any div that has 6315 // one or both of the following two attributes and no other attributes, 6316 // and false otherwise:" 6317 // 6318 // * "An align attribute whose value is an ASCII case-insensitive 6319 // match for alignment. 6320 // * "A style attribute which sets exactly one CSS property 6321 // (including unrecognized or invalid attributes), which is 6322 // "text-align", which is set to alignment. 6323 // 6324 // "New parent instructions are to call createElement("div") on the 6325 // context object, then set its CSS property "text-align" to alignment 6326 // and return the result." 6327 wrap( 6328 sublist, 6329 makeIsAlignedDiv(alignment), 6330 makeCreateAlignedDiv(alignment), 6331 range 6332 ); 6333 } 6334 } 6335 6336 //@} 6337 ///// Move the given collapsed range over adjacent zero-width whitespace characters. 6338 ///// The range is 6339 //@{ 6340 /** 6341 * Move the given collapsed range over adjacent zero-width whitespace characters. 6342 * If the range is not collapsed or is not contained in a text node, it is not modified 6343 * @param range range to modify 6344 * @param forward {Boolean} true to move forward, false to move backward 6345 */ 6346 function moveOverZWSP(range, forward) { 6347 var offset; 6348 if (!range.collapsed) { 6349 return; 6350 } 6351 6352 offset = range.startOffset; 6353 6354 if (forward) { 6355 // check whether the range starts in a text node 6356 if (range.startContainer && range.startContainer.nodeType === $_.Node.TEXT_NODE) { 6357 // move forward (i.e. increase offset) as long as we stay in the text node and have zwsp characters to the right 6358 while (offset < range.startContainer.data.length && range.startContainer.data.charAt(offset) === '\u200b') { 6359 offset++; 6360 } 6361 } 6362 } else { 6363 // check whether the range starts in a text node 6364 if (range.startContainer && range.startContainer.nodeType === $_.Node.TEXT_NODE) { 6365 // move backward (i.e. decrease offset) as long as we stay in the text node and have zwsp characters to the left 6366 while (offset > 0 && range.startContainer.data.charAt(offset - 1) === '\u200b') { 6367 offset--; 6368 } 6369 } 6370 } 6371 6372 // if the offset was changed, set it back to the collapsed range 6373 if (offset !== range.startOffset) { 6374 range.setStart(range.startContainer, offset); 6375 range.setEnd(range.startContainer, offset); 6376 } 6377 } 6378 6379 /** 6380 * implementation of the delete command 6381 * will attempt to delete contents within range if non-collapsed 6382 * or delete the character left of the cursor position if range 6383 * is collapsed. Is used to define the behaviour of the backspace 6384 * button. 6385 * 6386 * @param value is just there for compatibility with the commands api. parameter is ignored. 6387 * @param range the range to execute the delete command for 6388 * @return void 6389 */ 6390 commands["delete"] = { 6391 action: function (value, range) { 6392 var i; 6393 6394 // special behaviour for skipping zero-width whitespaces in IE7 6395 if (jQuery.browser.msie && jQuery.browser.version <= 7) { 6396 moveOverZWSP(range, false); 6397 } 6398 6399 // "If the active range is not collapsed, delete the contents of the 6400 // active range and abort these steps." 6401 if (!range.collapsed) { 6402 deleteContents(range); 6403 return; 6404 } 6405 6406 // "Canonicalize whitespace at (active range's start node, active 6407 // range's start offset)." 6408 canonicalizeWhitespace(range.startContainer, range.startOffset); 6409 6410 // "Let node and offset be the active range's start node and offset." 6411 var node = range.startContainer; 6412 var offset = range.startOffset; 6413 var isBr = false; 6414 var isHr = false; 6415 6416 // "Repeat the following steps:" 6417 while (true) { 6418 // we need to reset isBr and isHr on every interation of the loop 6419 if (offset > 0) { 6420 isBr = isNamedHtmlElement(node.childNodes[offset - 1], "br") || false; 6421 isHr = isNamedHtmlElement(node.childNodes[offset - 1], "hr") || false; 6422 } 6423 // "If offset is zero and node's previousSibling is an editable 6424 // invisible node, remove node's previousSibling from its parent." 6425 if (offset == 0 && isEditable(node.previousSibling) && isInvisible(node.previousSibling)) { 6426 node.parentNode.removeChild(node.previousSibling); 6427 continue; 6428 } 6429 // "Otherwise, if node has a child with index offset − 1 and that 6430 // child is an editable invisible node, remove that child from 6431 // node, then subtract one from offset." 6432 if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isEditable(node.childNodes[offset - 1]) && (isInvisible(node.childNodes[offset - 1]) || isBr || isHr)) { 6433 node.removeChild(node.childNodes[offset - 1]); 6434 offset--; 6435 if (isBr || isHr) { 6436 range.setStart(node, offset); 6437 range.setEnd(node, offset); 6438 return; 6439 } 6440 continue; 6441 6442 } 6443 // "Otherwise, if offset is zero and node is an inline node, or if 6444 // node is an invisible node, set offset to the index of node, then 6445 // set node to its parent." 6446 if ((offset == 0 && isInlineNode(node)) || isInvisible(node)) { 6447 offset = getNodeIndex(node); 6448 node = node.parentNode; 6449 continue; 6450 } 6451 // "Otherwise, if node has a child with index offset − 1 and that 6452 // child is an editable a, remove that child from node, preserving 6453 // its descendants. Then abort these steps." 6454 if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isEditable(node.childNodes[offset - 1]) && isNamedHtmlElement(node.childNodes[offset - 1], "a")) { 6455 removePreservingDescendants(node.childNodes[offset - 1], range); 6456 return; 6457 6458 } 6459 // "Otherwise, if node has a child with index offset − 1 and that 6460 // child is not a block node or a br or an img, set node to that 6461 // child, then set offset to the length of node." 6462 if (0 <= offset - 1 && offset - 1 < node.childNodes.length && !isBlockNode(node.childNodes[offset - 1]) && !isHtmlElementInArray(node.childNodes[offset - 1], ["br", "img"])) { 6463 node = node.childNodes[offset - 1]; 6464 offset = getNodeLength(node); 6465 continue; 6466 } 6467 // "Otherwise, break from this loop." 6468 // brk is a quick and dirty jslint workaround since I don't want to rewrite this loop 6469 var brk = true; 6470 if (brk) { 6471 break; 6472 } 6473 } 6474 6475 // if the previous node is an aloha-table we want to delete it 6476 var delBlock = getBlockAtPreviousPosition(node, offset); 6477 if (delBlock) { 6478 delBlock.parentNode.removeChild(delBlock); 6479 return; 6480 } 6481 6482 // "If node is a Text node and offset is not zero, call collapse(node, 6483 // offset) on the Selection. Then delete the contents of the range with 6484 // start (node, offset − 1) and end (node, offset) and abort these 6485 // steps." 6486 if (node.nodeType == $_.Node.TEXT_NODE && offset != 0) { 6487 range.setStart(node, offset - 1); 6488 range.setEnd(node, offset - 1); 6489 deleteContents(node, offset - 1, node, offset); 6490 return; 6491 } 6492 6493 // @iebug 6494 // when inserting a special char via the plugin 6495 // there where problems deleting them again with backspace after insertation 6496 // see https://github.com/alohaeditor/Aloha-Editor/issues/517 6497 if (node.nodeType == $_.Node.TEXT_NODE && offset == 0 && jQuery.browser.msie) { 6498 offset = 1; 6499 range.setStart(node, offset); 6500 range.setEnd(node, offset); 6501 range.startOffset = 0; 6502 deleteContents(range); 6503 return; 6504 } 6505 6506 // "If node is an inline node, abort these steps." 6507 if (isInlineNode(node)) { 6508 return; 6509 } 6510 6511 // "If node has a child with index offset − 1 and that child is a br or 6512 // hr or img, call collapse(node, offset) on the Selection. Then delete 6513 // the contents of the range with start (node, offset − 1) and end 6514 // (node, offset) and abort these steps." 6515 if (0 <= offset - 1 && offset - 1 < node.childNodes.length && isHtmlElementInArray(node.childNodes[offset - 1], ["br", "hr", "img"])) { 6516 range.setStart(node, offset); 6517 range.setEnd(node, offset); 6518 deleteContents(range); 6519 return; 6520 6521 } 6522 6523 // "If node is an li or dt or dd and is the first child of its parent, 6524 // and offset is zero:" 6525 if (isHtmlElementInArray(node, ["li", "dt", "dd"]) && node == node.parentNode.firstChild && offset == 0) { 6526 // "Let items be a list of all lis that are ancestors of node." 6527 // 6528 // Remember, must be in tree order. 6529 var items = []; 6530 var ancestor; 6531 for (ancestor = node.parentNode; ancestor; ancestor = ancestor.parentNode) { 6532 if (isNamedHtmlElement(ancestor, 'li')) { 6533 items.unshift(ancestor); 6534 } 6535 } 6536 6537 // "Normalize sublists of each item in items." 6538 for (i = 0; i < items.length; i++) { 6539 normalizeSublists(items[i], range); 6540 } 6541 6542 // "Record the values of the one-node list consisting of node, and 6543 // let values be the result." 6544 var values = recordValues([node]); 6545 6546 // "Split the parent of the one-node list consisting of node." 6547 splitParent([node], range); 6548 6549 // "Restore the values from values." 6550 restoreValues(values, range); 6551 6552 // "If node is a dd or dt, and it is not an allowed child of any of 6553 // its ancestors in the same editing host, set the tag name of node 6554 // to the default single-line container name and let node be the 6555 // result." 6556 if (isHtmlElementInArray(node, ["dd", "dt"]) && $_(getAncestors(node)).every(function (ancestor) { return !inSameEditingHost(node, ancestor) || !isAllowedChild(node, ancestor); })) { 6557 node = setTagName(node, defaultSingleLineContainerName, range); 6558 } 6559 6560 // "Fix disallowed ancestors of node." 6561 fixDisallowedAncestors(node, range); 6562 6563 // fix the lists to be html5 conformant 6564 for (i = 0; i < items.length; i++) { 6565 unNormalizeSublists(items[i].parentNode, range); 6566 } 6567 6568 // "Abort these steps." 6569 return; 6570 } 6571 6572 // "Let start node equal node and let start offset equal offset." 6573 var startNode = node; 6574 var startOffset = offset; 6575 6576 // "Repeat the following steps:" 6577 while (true) { 6578 // "If start offset is zero, set start offset to the index of start 6579 // node and then set start node to its parent." 6580 if (startOffset == 0) { 6581 startOffset = getNodeIndex(startNode); 6582 startNode = startNode.parentNode; 6583 6584 // "Otherwise, if start node has an editable invisible child with 6585 // index start offset minus one, remove it from start node and 6586 // subtract one from start offset." 6587 } else if (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length && isEditable(startNode.childNodes[startOffset - 1]) && isInvisible(startNode.childNodes[startOffset - 1])) { 6588 startNode.removeChild(startNode.childNodes[startOffset - 1]); 6589 startOffset--; 6590 6591 // "Otherwise, break from this loop." 6592 } else { 6593 break; 6594 } 6595 } 6596 6597 // "If offset is zero, and node has an editable ancestor container in 6598 // the same editing host that's an indentation element:" 6599 if (offset == 0 && $_(getAncestors(node).concat(node)).filter(function (ancestor) { return isEditable(ancestor) && inSameEditingHost(ancestor, node) && isIndentationElement(ancestor); }).length) { 6600 // "Block-extend the range whose start and end are both (node, 0), 6601 // and let new range be the result." 6602 var newRange = Aloha.createRange(); 6603 newRange.setStart(node, 0); 6604 newRange.setEnd(node, 0); 6605 newRange = blockExtend(newRange); 6606 6607 // "Let node list be a list of nodes, initially empty." 6608 // 6609 // "For each node current node contained in new range, append 6610 // current node to node list if the last member of node list (if 6611 // any) is not an ancestor of current node, and current node is 6612 // editable but has no editable descendants." 6613 var nodeList = getContainedNodes(newRange, function (currentNode) { 6614 return isEditable(currentNode) && !hasEditableDescendants(currentNode); 6615 }); 6616 6617 // "Outdent each node in node list." 6618 for (i = 0; i < nodeList.length; i++) { 6619 outdentNode(nodeList[i], range); 6620 } 6621 6622 // "Abort these steps." 6623 return; 6624 } 6625 6626 // "If the child of start node with index start offset is a table, 6627 // abort these steps." 6628 if (isNamedHtmlElement(startNode.childNodes[startOffset], "table")) { 6629 return; 6630 } 6631 6632 // "If start node has a child with index start offset − 1, and that 6633 // child is a table:" 6634 if (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length && isNamedHtmlElement(startNode.childNodes[startOffset - 1], "table")) { 6635 // "Call collapse(start node, start offset − 1) on the context 6636 // object's Selection." 6637 range.setStart(startNode, startOffset - 1); 6638 6639 // "Call extend(start node, start offset) on the context object's 6640 // Selection." 6641 range.setEnd(startNode, startOffset); 6642 6643 // "Abort these steps." 6644 return; 6645 } 6646 6647 // "If offset is zero; and either the child of start node with index 6648 // start offset minus one is an hr, or the child is a br whose 6649 // previousSibling is either a br or not an inline node:" 6650 if (offset == 0 6651 && (isNamedHtmlElement(startNode.childNodes[startOffset - 1], "hr") 6652 6653 || (isNamedHtmlElement(startNode.childNodes[startOffset - 1], "br") 6654 && (isNamedHtmlElement(startNode.childNodes[startOffset - 1].previousSibling, "br") 6655 || !isInlineNode(startNode.childNodes[startOffset - 1].previousSibling))))) { 6656 // "Call collapse(node, offset) on the Selection." 6657 6658 range.setStart(node, offset); 6659 range.setEnd(node, offset); 6660 6661 // "Delete the contents of the range with start (start node, start 6662 // offset − 1) and end (start node, start offset)." 6663 deleteContents(startNode, startOffset - 1, startNode, startOffset); 6664 6665 // "Abort these steps." 6666 return; 6667 } 6668 6669 // "If the child of start node with index start offset is an li or dt 6670 // or dd, and that child's firstChild is an inline node, and start 6671 // offset is not zero:" 6672 if (isHtmlElementInArray(startNode.childNodes[startOffset], ["li", "dt", "dd"]) && isInlineNode(startNode.childNodes[startOffset].firstChild) && startOffset != 0) { 6673 // "Let previous item be the child of start node with index start 6674 // offset minus one." 6675 var previousItem = startNode.childNodes[startOffset - 1]; 6676 6677 // "If previous item's lastChild is an inline node other than a br, 6678 // call createElement("br") on the context object and append the 6679 // result as the last child of previous item." 6680 if (isInlineNode(previousItem.lastChild) && !isNamedHtmlElement(previousItem.lastChild, "br")) { 6681 previousItem.appendChild(document.createElement("br")); 6682 } 6683 6684 // "If previous item's lastChild is an inline node, call 6685 // createElement("br") on the context object and append the result 6686 // as the last child of previous item." 6687 if (isInlineNode(previousItem.lastChild)) { 6688 previousItem.appendChild(document.createElement("br")); 6689 } 6690 } 6691 6692 // "If the child of start node with index start offset is an li or dt 6693 // or dd, and its previousSibling is also an li or dt or dd, set start 6694 // node to its child with index start offset − 1, then set start offset 6695 // to start node's length, then set node to start node's nextSibling, 6696 // then set offset to 0." 6697 if (isHtmlElementInArray(startNode.childNodes[startOffset], ["li", "dt", "dd"]) && isHtmlElementInArray(startNode.childNodes[startOffset - 1], ["li", "dt", "dd"])) { 6698 startNode = startNode.childNodes[startOffset - 1]; 6699 startOffset = getNodeLength(startNode); 6700 node = startNode.nextSibling; 6701 offset = 0; 6702 6703 // "Otherwise, while start node has a child with index start offset 6704 // minus one:" 6705 } else { 6706 while (0 <= startOffset - 1 && startOffset - 1 < startNode.childNodes.length) { 6707 // "If start node's child with index start offset minus one is 6708 // editable and invisible, remove it from start node, then 6709 // subtract one from start offset." 6710 if (isEditable(startNode.childNodes[startOffset - 1]) && isInvisible(startNode.childNodes[startOffset - 1])) { 6711 startNode.removeChild(startNode.childNodes[startOffset - 1]); 6712 startOffset--; 6713 6714 // "Otherwise, set start node to its child with index start 6715 // offset minus one, then set start offset to the length of 6716 // start node." 6717 } else { 6718 startNode = startNode.childNodes[startOffset - 1]; 6719 startOffset = getNodeLength(startNode); 6720 } 6721 } 6722 } 6723 6724 // "Delete the contents of the range with start (start node, start 6725 // offset) and end (node, offset)." 6726 var delRange = Aloha.createRange(); 6727 delRange.setStart(startNode, startOffset); 6728 delRange.setEnd(node, offset); 6729 deleteContents(delRange); 6730 6731 if (!isAncestorContainer(document.body, range.startContainer)) { 6732 if (delRange.startContainer.hasChildNodes() || delRange.startContainer.nodeType == $_.Node.TEXT_NODE) { 6733 range.setStart(delRange.startContainer, delRange.startOffset); 6734 range.setEnd(delRange.startContainer, delRange.startOffset); 6735 } else { 6736 range.setStart(delRange.startContainer.parentNode, getNodeIndex(delRange.startContainer)); 6737 range.setEnd(delRange.startContainer.parentNode, getNodeIndex(delRange.startContainer)); 6738 } 6739 } 6740 } 6741 }; 6742 6743 //@} 6744 ///// The formatBlock command ///// 6745 //@{ 6746 // "A formattable block name is "address", "dd", "div", "dt", "h1", "h2", "h3", 6747 // "h4", "h5", "h6", "p", or "pre"." 6748 var formattableBlockNames = ["address", "dd", "div", "dt", "h1", "h2", "h3", "h4", "h5", "h6", "p", "pre"]; 6749 6750 commands.formatblock = { 6751 action: function (value) { 6752 var i; 6753 6754 // "If value begins with a "<" character and ends with a ">" character, 6755 // remove the first and last characters from it." 6756 if (/^<.*>$/.test(value)) { 6757 value = value.slice(1, -1); 6758 } 6759 6760 // "Let value be converted to ASCII lowercase." 6761 value = value.toLowerCase(); 6762 6763 // "If value is not a formattable block name, abort these steps and do 6764 // nothing." 6765 if ($_(formattableBlockNames).indexOf(value) == -1) { 6766 return; 6767 } 6768 6769 // "Block-extend the active range, and let new range be the result." 6770 var newRange = blockExtend(getActiveRange()); 6771 6772 // "Let node list be an empty list of nodes." 6773 // 6774 // "For each node node contained in new range, append node to node list 6775 // if it is editable, the last member of original node list (if any) is 6776 // not an ancestor of node, node is either a non-list single-line 6777 // container or an allowed child of "p" or a dd or dt, and node is not 6778 // the ancestor of a prohibited paragraph child." 6779 var nodeList = getContainedNodes(newRange, function (node) { 6780 return isEditable(node) && (isNonListSingleLineContainer(node) || isAllowedChild(node, "p") || isHtmlElementInArray(node, ["dd", "dt"])) && !$_(getDescendants(node)).some(isProhibitedParagraphChild); 6781 }); 6782 6783 // "Record the values of node list, and let values be the result." 6784 var values = recordValues(nodeList); 6785 6786 function makeIsEditableElementInSameEditingHostDoesNotContainProhibitedParagraphChildren(node) { 6787 return function (ancestor) { 6788 return (isEditable(ancestor) 6789 && inSameEditingHost(ancestor, node) 6790 && isHtmlElement_obsolete(ancestor, formattableBlockNames) 6791 && !$_(getDescendants(ancestor)).some(isProhibitedParagraphChild)); 6792 }; 6793 } 6794 6795 function makeIsElementWithoutAttributes(value) { 6796 return function (node) { 6797 return isHtmlElement_obsolete(node, value) && !node.attributes.length; 6798 }; 6799 } 6800 6801 function returnFalse() { 6802 return false; 6803 } 6804 6805 function makeCreateElement(value) { 6806 return function () { 6807 return document.createElement(value); 6808 }; 6809 } 6810 6811 // "For each node in node list, while node is the descendant of an 6812 // editable HTML element in the same editing host, whose local name is 6813 // a formattable block name, and which is not the ancestor of a 6814 // prohibited paragraph child, split the parent of the one-node list 6815 // consisting of node." 6816 for (i = 0; i < nodeList.length; i++) { 6817 var node = nodeList[i]; 6818 while ($_(getAncestors(node)).some(makeIsEditableElementInSameEditingHostDoesNotContainProhibitedParagraphChildren(node))) { 6819 splitParent([node], newRange); 6820 } 6821 } 6822 6823 // "Restore the values from values." 6824 restoreValues(values, newRange); 6825 6826 // "While node list is not empty:" 6827 while (nodeList.length) { 6828 var sublist; 6829 6830 // "If the first member of node list is a single-line 6831 // container:" 6832 if (isSingleLineContainer(nodeList[0])) { 6833 // "Let sublist be the children of the first member of node 6834 // list." 6835 sublist = [].slice.call(toArray(nodeList[0].childNodes)); 6836 6837 // "Record the values of sublist, and let values be the 6838 // result." 6839 values = recordValues(sublist); 6840 6841 // "Remove the first member of node list from its parent, 6842 // preserving its descendants." 6843 removePreservingDescendants(nodeList[0], newRange); 6844 6845 // "Restore the values from values." 6846 restoreValues(values, newRange); 6847 6848 // "Remove the first member from node list." 6849 nodeList.shift(); 6850 6851 // "Otherwise:" 6852 } else { 6853 // "Let sublist be an empty list of nodes." 6854 sublist = []; 6855 6856 // "Remove the first member of node list and append it to 6857 // sublist." 6858 sublist.push(nodeList.shift()); 6859 6860 // "While node list is not empty, and the first member of 6861 // node list is the nextSibling of the last member of 6862 // sublist, and the first member of node list is not a 6863 // single-line container, and the last member of sublist is 6864 // not a br, remove the first member of node list and 6865 // append it to sublist." 6866 while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isSingleLineContainer(nodeList[0]) && !isNamedHtmlElement(sublist[sublist.length - 1], "BR")) { 6867 sublist.push(nodeList.shift()); 6868 } 6869 } 6870 6871 // "Wrap sublist. If value is "div" or "p", sibling criteria 6872 // returns false; otherwise it returns true for an HTML element 6873 // with local name value and no attributes, and false otherwise. 6874 // New parent instructions return the result of running 6875 // createElement(value) on the context object. Then fix disallowed 6876 // ancestors of the result." 6877 fixDisallowedAncestors(wrap( 6878 sublist, 6879 jQuery.inArray(value, ["div", "p"]) == -1 ? makeIsElementWithoutAttributes(value) : returnFalse, 6880 makeCreateElement(value), 6881 newRange 6882 ), newRange); 6883 } 6884 }, 6885 indeterm: function () { 6886 // "Block-extend the active range, and let new range be the result." 6887 var newRange = blockExtend(getActiveRange()); 6888 6889 // "Let node list be all visible editable nodes that are contained in 6890 // new range and have no children." 6891 var nodeList = getAllContainedNodes(newRange, function (node) { 6892 return isVisible(node) && isEditable(node) && !node.hasChildNodes(); 6893 }); 6894 6895 // "If node list is empty, return false." 6896 if (!nodeList.length) { 6897 return false; 6898 } 6899 6900 // "Let type be null." 6901 var type = null; 6902 6903 // "For each node in node list:" 6904 var i; 6905 for (i = 0; i < nodeList.length; i++) { 6906 var node = nodeList[i]; 6907 6908 // "While node's parent is editable and in the same editing host as 6909 // node, and node is not an HTML element whose local name is a 6910 // formattable block name, set node to its parent." 6911 while (isEditable(node.parentNode) && inSameEditingHost(node, node.parentNode) && !isHtmlElement_obsolete(node, formattableBlockNames)) { 6912 node = node.parentNode; 6913 } 6914 6915 // "Let current type be the empty string." 6916 var currentType = ""; 6917 6918 // "If node is an editable HTML element whose local name is a 6919 // formattable block name, and node is not the ancestor of a 6920 // prohibited paragraph child, set current type to node's local 6921 // name." 6922 if (isEditable(node) && isHtmlElement_obsolete(node, formattableBlockNames) && !$_(getDescendants(node)).some(isProhibitedParagraphChild)) { 6923 currentType = node.tagName; 6924 } 6925 6926 // "If type is null, set type to current type." 6927 if (type === null) { 6928 type = currentType; 6929 6930 // "Otherwise, if type does not equal current type, return true." 6931 } else if (type != currentType) { 6932 return true; 6933 } 6934 } 6935 6936 // "Return false." 6937 return false; 6938 }, 6939 value: function () { 6940 // "Block-extend the active range, and let new range be the result." 6941 var newRange = blockExtend(getActiveRange()); 6942 6943 // "Let node be the first visible editable node that is contained in 6944 // new range and has no children. If there is no such node, return the 6945 // empty string." 6946 var nodes = getAllContainedNodes(newRange, function (node) { 6947 return isVisible(node) && isEditable(node) && !node.hasChildNodes(); 6948 }); 6949 if (!nodes.length) { 6950 return ""; 6951 } 6952 var node = nodes[0]; 6953 6954 // "While node's parent is editable and in the same editing host as 6955 // node, and node is not an HTML element whose local name is a 6956 // formattable block name, set node to its parent." 6957 while (isEditable(node.parentNode) && inSameEditingHost(node, node.parentNode) && !isHtmlElement_obsolete(node, formattableBlockNames)) { 6958 node = node.parentNode; 6959 } 6960 6961 // "If node is an editable HTML element whose local name is a 6962 // formattable block name, and node is not the ancestor of a prohibited 6963 // paragraph child, return node's local name, converted to ASCII 6964 // lowercase." 6965 if (isEditable(node) && isHtmlElement_obsolete(node, formattableBlockNames) && !$_(getDescendants(node)).some(isProhibitedParagraphChild)) { 6966 return node.tagName.toLowerCase(); 6967 } 6968 6969 // "Return the empty string." 6970 return ""; 6971 } 6972 }; 6973 6974 //@} 6975 ///// The forwardDelete command ///// 6976 //@{ 6977 commands.forwarddelete = { 6978 action: function (value, range) { 6979 // special behaviour for skipping zero-width whitespaces in IE7 6980 if (jQuery.browser.msie && jQuery.browser.version <= 7) { 6981 moveOverZWSP(range, true); 6982 } 6983 6984 // "If the active range is not collapsed, delete the contents of the 6985 // active range and abort these steps." 6986 if (!range.collapsed) { 6987 deleteContents(range); 6988 return; 6989 } 6990 6991 // "Canonicalize whitespace at (active range's start node, active 6992 // range's start offset)." 6993 canonicalizeWhitespace(range.startContainer, range.startOffset); 6994 6995 // "Let node and offset be the active range's start node and offset." 6996 var node = range.startContainer; 6997 var offset = range.startOffset; 6998 var isBr = false; 6999 var isHr = false; 7000 7001 // "Repeat the following steps:" 7002 while (true) { 7003 // check whether the next element is a br or hr 7004 // Commented out for unknown reason. 7005 //if (offset < node.childNodes.length) { 7006 // isBr = isHtmlElement_obsolete(node.childNodes[offset], "br") || false; 7007 // isHr = isHtmlElement_obsolete(node.childNodes[offset], "hr") || false; 7008 //} 7009 7010 // "If offset is the length of node and node's nextSibling is an 7011 // editable invisible node, remove node's nextSibling from its 7012 // parent." 7013 if (offset == getNodeLength(node) && isEditable(node.nextSibling) && isInvisible(node.nextSibling)) { 7014 node.parentNode.removeChild(node.nextSibling); 7015 7016 // "Otherwise, if node has a child with index offset and that child 7017 // is an editable invisible node, remove that child from node." 7018 } else if (offset < node.childNodes.length && isEditable(node.childNodes[offset]) && (isInvisible(node.childNodes[offset]) || isBr || isHr)) { 7019 node.removeChild(node.childNodes[offset]); 7020 if (isBr || isHr) { 7021 ensureContainerEditable(node); 7022 range.setStart(node, offset); 7023 range.setEnd(node, offset); 7024 return; 7025 } 7026 7027 // "Otherwise, if node has a child with index offset and that child 7028 // is a collapsed block prop, add one to offset." 7029 } else if (offset < node.childNodes.length && isCollapsedBlockProp(node.childNodes[offset])) { 7030 offset++; 7031 7032 // "Otherwise, if offset is the length of node and node is an 7033 // inline node, or if node is invisible, set offset to one plus the 7034 // index of node, then set node to its parent." 7035 } else if ((offset == getNodeLength(node) && isInlineNode(node)) || isInvisible(node)) { 7036 offset = 1 + getNodeIndex(node); 7037 node = node.parentNode; 7038 7039 // "Otherwise, if node has a child with index offset and that child 7040 // is not a block node or a br or an img, set node to that child, 7041 // then set offset to zero." 7042 } else if (offset < node.childNodes.length && !isBlockNode(node.childNodes[offset]) && !isHtmlElementInArray(node.childNodes[offset], ["br", "img"])) { 7043 node = node.childNodes[offset]; 7044 offset = 0; 7045 7046 // "Otherwise, break from this loop." 7047 } else { 7048 break; 7049 } 7050 } 7051 7052 // collapse whitespace in the node, if it is a text node 7053 canonicalizeWhitespace(range.startContainer, range.startOffset); 7054 7055 // if the next node is an aloha-table we want to delete it 7056 var delBlock = getBlockAtNextPosition(node, offset); 7057 if (delBlock) { 7058 delBlock.parentNode.removeChild(delBlock); 7059 return; 7060 } 7061 7062 var endOffset; 7063 // "If node is a Text node and offset is not node's length:" 7064 if (node.nodeType == $_.Node.TEXT_NODE && offset != getNodeLength(node)) { 7065 // "Call collapse(node, offset) on the Selection." 7066 range.setStart(node, offset); 7067 range.setEnd(node, offset); 7068 7069 // "Let end offset be offset plus one." 7070 7071 endOffset = offset + 1; 7072 7073 // "While end offset is not node's length and the end offsetth 7074 // element of node's data has general category M when interpreted 7075 // as a Unicode code point, add one to end offset." 7076 7077 // 7078 // TODO: Not even going to try handling anything beyond the most 7079 // basic combining marks, since I couldn't find a good list. I 7080 // special-case a few Hebrew diacritics too to test basic coverage 7081 // of non-Latin stuff. 7082 while (endOffset != node.length && /^[\u0300-\u036f\u0591-\u05bd\u05c1\u05c2]$/.test(node.data[endOffset])) { 7083 endOffset++; 7084 } 7085 7086 // "Delete the contents of the range with start (node, offset) and 7087 // end (node, end offset)." 7088 deleteContents(node, offset, node, endOffset); 7089 7090 // "Abort these steps." 7091 return; 7092 } 7093 7094 // "If node is an inline node, abort these steps." 7095 if (isInlineNode(node)) { 7096 return; 7097 } 7098 7099 // "If node has a child with index offset and that child is a br or hr 7100 // or img, call collapse(node, offset) on the Selection. Then delete 7101 // the contents of the range with start (node, offset) and end (node, 7102 // offset + 1) and abort these steps." 7103 if (offset < node.childNodes.length && isHtmlElementInArray(node.childNodes[offset], ["br", "hr", "img"])) { 7104 range.setStart(node, offset); 7105 range.setEnd(node, offset); 7106 deleteContents(node, offset, node, offset + 1); 7107 return; 7108 } 7109 7110 // "Let end node equal node and let end offset equal offset." 7111 var endNode = node; 7112 endOffset = offset; 7113 7114 // "Repeat the following steps:" 7115 while (true) { 7116 // "If end offset is the length of end node, set end offset to one 7117 // plus the index of end node and then set end node to its parent." 7118 if (endOffset == getNodeLength(endNode)) { 7119 endOffset = 1 + getNodeIndex(endNode); 7120 endNode = endNode.parentNode; 7121 7122 // "Otherwise, if end node has a an editable invisible child with 7123 // index end offset, remove it from end node." 7124 } else if (endOffset < endNode.childNodes.length && isEditable(endNode.childNodes[endOffset]) && isInvisible(endNode.childNodes[endOffset])) { 7125 endNode.removeChild(endNode.childNodes[endOffset]); 7126 7127 // "Otherwise, break from this loop." 7128 } else { 7129 break; 7130 } 7131 } 7132 7133 // "If the child of end node with index end offset minus one is a 7134 // table, abort these steps." 7135 if (isNamedHtmlElement(endNode.childNodes[endOffset - 1], "table")) { 7136 return; 7137 } 7138 7139 // "If the child of end node with index end offset is a table:" 7140 if (isNamedHtmlElement(endNode.childNodes[endOffset], "table")) { 7141 // "Call collapse(end node, end offset) on the context object's 7142 // Selection." 7143 range.setStart(endNode, endOffset); 7144 7145 // "Call extend(end node, end offset + 1) on the context object's 7146 // Selection." 7147 range.setEnd(endNode, endOffset + 1); 7148 7149 // "Abort these steps." 7150 return; 7151 } 7152 7153 // "If offset is the length of node, and the child of end node with 7154 // index end offset is an hr or br:" 7155 7156 if (offset == getNodeLength(node) && isHtmlElementInArray(endNode.childNodes[endOffset], ["br", "hr"])) { 7157 // "Call collapse(node, offset) on the Selection." 7158 range.setStart(node, offset); 7159 range.setEnd(node, offset); 7160 7161 // "Delete the contents of the range with end (end node, end 7162 // offset) and end (end node, end offset + 1)." 7163 deleteContents(endNode, endOffset, endNode, endOffset + 1); 7164 7165 // "Abort these steps." 7166 return; 7167 } 7168 7169 // "While end node has a child with index end offset:" 7170 while (endOffset < endNode.childNodes.length) { 7171 // "If end node's child with index end offset is editable and 7172 // invisible, remove it from end node." 7173 if (isEditable(endNode.childNodes[endOffset]) && isInvisible(endNode.childNodes[endOffset])) { 7174 endNode.removeChild(endNode.childNodes[endOffset]); 7175 7176 // "Otherwise, set end node to its child with index end offset and 7177 // set end offset to zero." 7178 } else { 7179 endNode = endNode.childNodes[endOffset]; 7180 endOffset = 0; 7181 } 7182 } 7183 7184 // "Delete the contents of the range with start (node, offset) and end 7185 // (end node, end offset)." 7186 deleteContents(node, offset, endNode, endOffset); 7187 } 7188 }; 7189 7190 //@} 7191 ///// The indent command ///// 7192 //@{ 7193 commands.indent = { 7194 action: function () { 7195 // "Let items be a list of all lis that are ancestor containers of the 7196 // active range's start and/or end node." 7197 // 7198 // Has to be in tree order, remember! 7199 var items = []; 7200 var node; 7201 for (node = getActiveRange().endContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) { 7202 if (isNamedHtmlElement(node, "LI")) { 7203 items.unshift(node); 7204 } 7205 } 7206 for (node = getActiveRange().startContainer; node != getActiveRange().commonAncestorContainer; node = node.parentNode) { 7207 if (isNamedHtmlElement(node, "LI")) { 7208 items.unshift(node); 7209 } 7210 } 7211 for (node = getActiveRange().commonAncestorContainer; node; node = node.parentNode) { 7212 if (isNamedHtmlElement(node, "LI")) { 7213 items.unshift(node); 7214 } 7215 } 7216 7217 // "For each item in items, normalize sublists of item." 7218 var i; 7219 for (i = 0; i < items.length; i++) { 7220 normalizeSublists(items[i], getActiveRange()); 7221 } 7222 7223 // "Block-extend the active range, and let new range be the result." 7224 var newRange = blockExtend(getActiveRange()); 7225 7226 // "Let node list be a list of nodes, initially empty." 7227 var nodeList = []; 7228 7229 // "For each node node contained in new range, if node is editable and 7230 // is an allowed child of "div" or "ol" and if the last member of node 7231 // list (if any) is not an ancestor of node, append node to node list." 7232 nodeList = getContainedNodes(newRange, function (node) { 7233 return isEditable(node) && (isAllowedChild(node, "div") || isAllowedChild(node, "ol")); 7234 }); 7235 7236 // "If the first member of node list is an li whose parent is an ol or 7237 // ul, and its previousSibling is an li as well, normalize sublists of 7238 // its previousSibling." 7239 if (nodeList.length && isNamedHtmlElement(nodeList[0], "LI") && isHtmlElementInArray(nodeList[0].parentNode, ["OL", "UL"]) && isNamedHtmlElement(nodeList[0].previousSibling, "LI")) { 7240 normalizeSublists(nodeList[0].previousSibling, newRange); 7241 } 7242 7243 // "While node list is not empty:" 7244 while (nodeList.length) { 7245 // "Let sublist be a list of nodes, initially empty." 7246 var sublist = []; 7247 7248 // "Remove the first member of node list and append it to sublist." 7249 sublist.push(nodeList.shift()); 7250 7251 // "While the first member of node list is the nextSibling of the 7252 // last member of sublist, remove the first member of node list and 7253 // append it to sublist." 7254 while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling) { 7255 sublist.push(nodeList.shift()); 7256 } 7257 7258 // "Indent sublist." 7259 indentNodes(sublist, newRange); 7260 } 7261 } 7262 }; 7263 7264 //@} 7265 ///// The insertHorizontalRule command ///// 7266 //@{ 7267 commands.inserthorizontalrule = { 7268 action: function (value, range) { 7269 7270 // "While range's start offset is 0 and its start node's parent is not 7271 // null, set range's start to (parent of start node, index of start 7272 // node)." 7273 while (range.startOffset == 0 && range.startContainer.parentNode) { 7274 range.setStart(range.startContainer.parentNode, getNodeIndex(range.startContainer)); 7275 } 7276 7277 // "While range's end offset is the length of its end node, and its end 7278 // node's parent is not null, set range's end to (parent of end node, 1 7279 // + index of start node)." 7280 while (range.endOffset == getNodeLength(range.endContainer) && range.endContainer.parentNode) { 7281 range.setEnd(range.endContainer.parentNode, 1 + getNodeIndex(range.endContainer)); 7282 } 7283 7284 // "Delete the contents of range, with block merging false." 7285 deleteContents(range, { 7286 blockMerging: false 7287 }); 7288 7289 // "If the active range's start node is neither editable nor an editing 7290 // host, abort these steps." 7291 if (!isEditable(getActiveRange().startContainer) && !isEditingHost(getActiveRange().startContainer)) { 7292 return; 7293 } 7294 7295 // "If the active range's start node is a Text node and its start 7296 // offset is zero, set the active range's start and end to (parent of 7297 // start node, index of start node)." 7298 if (getActiveRange().startContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().startOffset == 0) { 7299 getActiveRange().setStart(getActiveRange().startContainer.parentNode, getNodeIndex(getActiveRange().startContainer)); 7300 getActiveRange().collapse(true); 7301 } 7302 7303 // "If the active range's start node is a Text node and its start 7304 // offset is the length of its start node, set the active range's start 7305 // and end to (parent of start node, 1 + index of start node)." 7306 if (getActiveRange().startContainer.nodeType == $_.Node.TEXT_NODE && getActiveRange().startOffset == getNodeLength(getActiveRange().startContainer)) { 7307 getActiveRange().setStart(getActiveRange().startContainer.parentNode, 1 + getNodeIndex(getActiveRange().startContainer)); 7308 getActiveRange().collapse(true); 7309 } 7310 7311 // "Let hr be the result of calling createElement("hr") on the 7312 // context object." 7313 var hr = document.createElement("hr"); 7314 7315 // "Run insertNode(hr) on the range." 7316 range.insertNode(hr); 7317 7318 // "Fix disallowed ancestors of hr." 7319 fixDisallowedAncestors(hr, range); 7320 7321 // "Run collapse() on the Selection, with first argument equal to the 7322 // parent of hr and the second argument equal to one plus the index of 7323 // hr." 7324 // 7325 // Not everyone actually supports collapse(), so we do it manually 7326 // instead. Also, we need to modify the actual range we're given as 7327 // well, for the sake of autoimplementation.html's range-filling-in. 7328 range.setStart(hr.parentNode, 1 + getNodeIndex(hr)); 7329 range.setEnd(hr.parentNode, 1 + getNodeIndex(hr)); 7330 Aloha.getSelection().removeAllRanges(); 7331 Aloha.getSelection().addRange(range); 7332 } 7333 }; 7334 7335 //@} 7336 ///// The insertHTML command ///// 7337 //@{ 7338 commands.inserthtml = { 7339 action: function (value, range) { 7340 7341 7342 // "Delete the contents of the active range." 7343 deleteContents(range); 7344 7345 // "If the active range's start node is neither editable nor an editing 7346 // host, abort these steps." 7347 if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) { 7348 return; 7349 } 7350 7351 // "Let frag be the result of calling createContextualFragment(value) 7352 // on the active range." 7353 var frag = range.createContextualFragment(value); 7354 7355 // "Let last child be the lastChild of frag." 7356 var lastChild = frag.lastChild; 7357 7358 // "If last child is null, abort these steps." 7359 if (!lastChild) { 7360 return; 7361 } 7362 7363 // "Let descendants be all descendants of frag." 7364 var descendants = getDescendants(frag); 7365 7366 // "If the active range's start node is a block node:" 7367 if (isBlockNode(range.startContainer)) { 7368 // "Let collapsed block props be all editable collapsed block prop 7369 // children of the active range's start node that have index 7370 // greater than or equal to the active range's start offset." 7371 // 7372 // "For each node in collapsed block props, remove node from its 7373 // parent." 7374 $_(range.startContainer.childNodes).filter(function (node, range) { 7375 return isEditable(node) && isCollapsedBlockProp(node) && getNodeIndex(node) >= range.startOffset; 7376 }, true).forEach(function (node) { 7377 node.parentNode.removeChild(node); 7378 }); 7379 } 7380 7381 // "Call insertNode(frag) on the active range." 7382 range.insertNode(frag); 7383 7384 // "If the active range's start node is a block node with no visible 7385 // children, call createElement("br") on the context object and append 7386 // the result as the last child of the active range's start node." 7387 if (isBlockNode(range.startContainer)) { 7388 ensureContainerEditable(range.startContainer); 7389 } 7390 7391 // "Call collapse() on the context object's Selection, with last 7392 // child's parent as the first argument and one plus its index as the 7393 // second." 7394 range.setStart(lastChild.parentNode, 1 + getNodeIndex(lastChild)); 7395 range.setEnd(lastChild.parentNode, 1 + getNodeIndex(lastChild)); 7396 7397 // "Fix disallowed ancestors of each member of descendants." 7398 var i; 7399 for (i = 0; i < descendants.length; i++) { 7400 fixDisallowedAncestors(descendants[i], range); 7401 } 7402 7403 setActiveRange(range); 7404 } 7405 }; 7406 7407 //@} 7408 ///// The insertImage command ///// 7409 //@{ 7410 commands.insertimage = { 7411 action: function (value) { 7412 // "If value is the empty string, abort these steps and do nothing." 7413 if (value === "") { 7414 return; 7415 } 7416 7417 // "Let range be the active range." 7418 var range = getActiveRange(); 7419 7420 // "Delete the contents of range, with strip wrappers false." 7421 deleteContents(range, { 7422 stripWrappers: false 7423 }); 7424 7425 // "If the active range's start node is neither editable nor an editing 7426 // host, abort these steps." 7427 if (!isEditable(getActiveRange().startContainer) && !isEditingHost(getActiveRange().startContainer)) { 7428 return; 7429 } 7430 7431 // "If range's start node is a block node whose sole child is a br, and 7432 // its start offset is 0, remove its start node's child from it." 7433 if (isBlockNode(range.startContainer) && range.startContainer.childNodes.length == 1 && isNamedHtmlElement(range.startContainer.firstChild, "br") && range.startOffset == 0) { 7434 range.startContainer.removeChild(range.startContainer.firstChild); 7435 } 7436 7437 // "Let img be the result of calling createElement("img") on the 7438 // context object." 7439 var img = document.createElement("img"); 7440 7441 // "Run setAttribute("src", value) on img." 7442 img.setAttribute("src", value); 7443 7444 // "Run insertNode(img) on the range." 7445 range.insertNode(img); 7446 7447 // "Run collapse() on the Selection, with first argument equal to the 7448 // parent of img and the second argument equal to one plus the index of 7449 // img." 7450 // 7451 // Not everyone actually supports collapse(), so we do it manually 7452 // instead. Also, we need to modify the actual range we're given as 7453 // well, for the sake of autoimplementation.html's range-filling-in. 7454 range.setStart(img.parentNode, 1 + getNodeIndex(img)); 7455 range.setEnd(img.parentNode, 1 + getNodeIndex(img)); 7456 Aloha.getSelection().removeAllRanges(); 7457 Aloha.getSelection().addRange(range); 7458 7459 // IE adds width and height attributes for some reason, so remove those 7460 // to actually do what the spec says. 7461 img.removeAttribute("width"); 7462 img.removeAttribute("height"); 7463 } 7464 }; 7465 7466 //@} 7467 ///// The insertLineBreak command ///// 7468 //@{ 7469 commands.insertlinebreak = { 7470 action: function (value, range) { 7471 // "Delete the contents of the active range, with strip wrappers false." 7472 deleteContents(range, { 7473 stripWrappers: false 7474 }); 7475 7476 // "If the active range's start node is neither editable nor an editing 7477 // host, abort these steps." 7478 if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) { 7479 return; 7480 } 7481 7482 // "If the active range's start node is an Element, and "br" is not an 7483 // allowed child of it, abort these steps." 7484 if (range.startContainer.nodeType == $_.Node.ELEMENT_NODE && !isAllowedChild("br", range.startContainer)) { 7485 return; 7486 } 7487 7488 // "If the active range's start node is not an Element, and "br" is not 7489 // an allowed child of the active range's start node's parent, abort 7490 // these steps." 7491 if (range.startContainer.nodeType != $_.Node.ELEMENT_NODE && !isAllowedChild("br", range.startContainer.parentNode)) { 7492 return; 7493 } 7494 7495 // "If the active range's start node is a Text node and its start 7496 // offset is zero, call collapse() on the context object's Selection, 7497 // with first argument equal to the active range's start node's parent 7498 // and second argument equal to the active range's start node's index." 7499 var newNode, newOffset; 7500 if (range.startContainer.nodeType == $_.Node.TEXT_NODE && range.startOffset == 0) { 7501 newNode = range.startContainer.parentNode; 7502 newOffset = getNodeIndex(range.startContainer); 7503 Aloha.getSelection().collapse(newNode, newOffset); 7504 range.setStart(newNode, newOffset); 7505 range.setEnd(newNode, newOffset); 7506 } 7507 7508 // "If the active range's start node is a Text node and its start 7509 // offset is the length of its start node, call collapse() on the 7510 // context object's Selection, with first argument equal to the active 7511 // range's start node's parent and second argument equal to one plus 7512 // the active range's start node's index." 7513 if (range.startContainer.nodeType == $_.Node.TEXT_NODE && range.startOffset == getNodeLength(range.startContainer)) { 7514 newNode = range.startContainer.parentNode; 7515 newOffset = 1 + getNodeIndex(range.startContainer); 7516 Aloha.getSelection().collapse(newNode, newOffset); 7517 range.setStart(newNode, newOffset); 7518 range.setEnd(newNode, newOffset); 7519 } 7520 7521 // "Let br be the result of calling createElement("br") on the context 7522 // object." 7523 var br = document.createElement("br"); 7524 7525 // "Call insertNode(br) on the active range." 7526 range.insertNode(br); 7527 7528 // "Call collapse() on the context object's Selection, with br's parent 7529 // as the first argument and one plus br's index as the second 7530 // argument." 7531 Aloha.getSelection().collapse(br.parentNode, 1 + getNodeIndex(br)); 7532 range.setStart(br.parentNode, 1 + getNodeIndex(br)); 7533 range.setEnd(br.parentNode, 1 + getNodeIndex(br)); 7534 7535 // "If br is a collapsed line break, call createElement("br") on the 7536 // context object and let extra br be the result, then call 7537 // insertNode(extra br) on the active range." 7538 if (isCollapsedLineBreak(br)) { 7539 // TODO 7540 range.insertNode(createEndBreak()); 7541 7542 // Compensate for nonstandard implementations of insertNode 7543 Aloha.getSelection().collapse(br.parentNode, 1 + getNodeIndex(br)); 7544 range.setStart(br.parentNode, 1 + getNodeIndex(br)); 7545 range.setEnd(br.parentNode, 1 + getNodeIndex(br)); 7546 } 7547 7548 // IE7 is adding this styles: height: auto; min-height: 0px; max-height: none; 7549 // with that there is the ugly "IE-editable-outline" 7550 if (jQuery.browser.msie && jQuery.browser.version < 8) { 7551 br.parentNode.removeAttribute("style"); 7552 } 7553 } 7554 }; 7555 7556 //@} 7557 ///// The insertOrderedList command ///// 7558 //@{ 7559 commands.insertorderedlist = { 7560 // "Toggle lists with tag name "ol"." 7561 action: function (value, range) { 7562 toggleLists("ol", range); 7563 }, 7564 // "True if the selection's list state is "mixed" or "mixed ol", false 7565 // otherwise." 7566 indeterm: function () { 7567 return (/^mixed( ol)?$/).test(getSelectionListState()); 7568 }, 7569 // "True if the selection's list state is "ol", false otherwise." 7570 state: function () { 7571 return getSelectionListState() == "ol"; 7572 } 7573 }; 7574 7575 var listRelatedElements = { 7576 "LI": true, 7577 "DT": true, 7578 "DD": true 7579 }; 7580 7581 //@} 7582 ///// The insertParagraph command ///// 7583 //@{ 7584 commands.insertparagraph = { 7585 action: function (value, range) { 7586 var i; 7587 7588 // "Delete the contents of the active range." 7589 deleteContents(range); 7590 7591 // clean lists in the editing host, this will remove any whitespace nodes around lists 7592 // because the following algorithm is not prepared to deal with them 7593 cleanLists(getEditingHostOf(range.startContainer), range); 7594 7595 // "If the active range's start node is neither editable nor an editing 7596 // host, abort these steps." 7597 if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) { 7598 return; 7599 } 7600 7601 // "Let node and offset be the active range's start node and offset." 7602 var node = range.startContainer; 7603 var offset = range.startOffset; 7604 7605 // "If node is a Text node, and offset is neither 0 nor the length of 7606 // node, call splitText(offset) on node." 7607 if (node.nodeType == $_.Node.TEXT_NODE && offset != 0 && offset != getNodeLength(node)) { 7608 node.splitText(offset); 7609 } 7610 7611 // "If node is a Text node and offset is its length, set offset to one 7612 // plus the index of node, then set node to its parent." 7613 if (node.nodeType == $_.Node.TEXT_NODE && offset == getNodeLength(node)) { 7614 offset = 1 + getNodeIndex(node); 7615 node = node.parentNode; 7616 } 7617 7618 // "If node is a Text or Comment node, set offset to the index of node, 7619 // then set node to its parent." 7620 if (node.nodeType == $_.Node.TEXT_NODE || node.nodeType == $_.Node.COMMENT_NODE) { 7621 offset = getNodeIndex(node); 7622 node = node.parentNode; 7623 } 7624 7625 // "Call collapse(node, offset) on the context object's Selection." 7626 Aloha.getSelection().collapse(node, offset); 7627 range.setStart(node, offset); 7628 range.setEnd(node, offset); 7629 7630 // "Let container equal node." 7631 var container = node; 7632 7633 // "While container is not a single-line container, and container's 7634 // parent is editable and in the same editing host as node, set 7635 // container to its parent." 7636 while (!isSingleLineContainer(container) && isEditable(container.parentNode) && inSameEditingHost(node, container.parentNode)) { 7637 container = container.parentNode; 7638 } 7639 7640 // "If container is not editable or not in the same editing host as 7641 // node or is not a single-line container:" 7642 if (!isEditable(container) || !inSameEditingHost(container, node) || !isSingleLineContainer(container)) { 7643 // "Let tag be the default single-line container name." 7644 var tag = defaultSingleLineContainerName; 7645 7646 // "Block-extend the active range, and let new range be the 7647 // result." 7648 var newRange = blockExtend(range); 7649 7650 // "Let node list be a list of nodes, initially empty." 7651 // 7652 // "Append to node list the first node in tree order that is 7653 // contained in new range and is an allowed child of "p", if any." 7654 var nodeList = getContainedNodes(newRange, function (node) { 7655 return isAllowedChild(node, "p"); 7656 }).slice(0, 1); 7657 7658 // "If node list is empty:" 7659 if (!nodeList.length) { 7660 // "If tag is not an allowed child of the active range's start 7661 // node, abort these steps." 7662 if (!isAllowedChild(tag, range.startContainer)) { 7663 return; 7664 } 7665 7666 // "Set container to the result of calling createElement(tag) 7667 // on the context object." 7668 container = document.createElement(tag); 7669 7670 // "Call insertNode(container) on the active range." 7671 range.insertNode(container); 7672 7673 // "Call createElement("br") on the context object, and append 7674 // the result as the last child of container." 7675 // TODO not always 7676 container.appendChild(createEndBreak()); 7677 7678 // "Call collapse(container, 0) on the context object's 7679 // Selection." 7680 // TODO: remove selection from command 7681 Aloha.getSelection().collapse(container, 0); 7682 range.setStart(container, 0); 7683 range.setEnd(container, 0); 7684 7685 // "Abort these steps." 7686 return; 7687 } 7688 7689 // "While the nextSibling of the last member of node list is not 7690 // null and is an allowed child of "p", append it to node list." 7691 while (nodeList[nodeList.length - 1].nextSibling && isAllowedChild(nodeList[nodeList.length - 1].nextSibling, "p")) { 7692 nodeList.push(nodeList[nodeList.length - 1].nextSibling); 7693 } 7694 7695 // "Wrap node list, with sibling criteria returning false and new 7696 // parent instructions returning the result of calling 7697 // createElement(tag) on the context object. Set container to the 7698 // result." 7699 container = wrap( 7700 nodeList, 7701 function () { 7702 return false; 7703 }, 7704 function () { 7705 return document.createElement(tag); 7706 }, 7707 range 7708 ); 7709 } 7710 7711 // "If container's local name is "address", "listing", or "pre":" 7712 var oldHeight, newHeight; 7713 if (container.tagName == "ADDRESS" || container.tagName == "LISTING" || container.tagName == "PRE") { 7714 // "Let br be the result of calling createElement("br") on the 7715 // context object." 7716 var br = document.createElement("br"); 7717 7718 // remember the old height 7719 oldHeight = container.offsetHeight; 7720 7721 // "Call insertNode(br) on the active range." 7722 range.insertNode(br); 7723 7724 // determine the new height 7725 newHeight = container.offsetHeight; 7726 7727 // "Call collapse(node, offset + 1) on the context object's 7728 // Selection." 7729 Aloha.getSelection().collapse(node, offset + 1); 7730 range.setStart(node, offset + 1); 7731 range.setEnd(node, offset + 1); 7732 7733 // "If br is the last descendant of container, let br be the result 7734 // of calling createElement("br") on the context object, then call 7735 // insertNode(br) on the active range." (Fix: only do this, if the container height did not change by inserting a single <br/>) 7736 // 7737 // Work around browser bugs: some browsers select the 7738 // newly-inserted node, not per spec. 7739 if (oldHeight == newHeight && !isDescendant(nextNode(br), container)) { 7740 // TODO check 7741 range.insertNode(createEndBreak()); 7742 Aloha.getSelection().collapse(node, offset + 1); 7743 range.setEnd(node, offset + 1); 7744 } 7745 7746 7747 // "Abort these steps." 7748 return; 7749 } 7750 7751 // "If container's local name is "li", "dt", or "dd"; and either it has 7752 // no children or it has a single child and that child is a br:" 7753 if (listRelatedElements[container.tagName] && (!container.hasChildNodes() || (container.childNodes.length == 1 && isNamedHtmlElement(container.firstChild, "br")))) { 7754 // "Split the parent of the one-node list consisting of container." 7755 splitParent([container], range); 7756 7757 // "If container has no children, call createElement("br") on the 7758 // context object and append the result as the last child of 7759 // container." 7760 // only do this, if inserting the br does NOT modify the offset height of the container 7761 // if (!container.hasChildNodes()) { 7762 // var oldHeight = container.offsetHeight, endBr = createEndBreak(); 7763 // container.appendChild(endBr); 7764 // if (container.offsetHeight !== oldHeight) { 7765 // container.removeChild(endBr); 7766 // } 7767 // } 7768 7769 // "If container is a dd or dt, and it is not an allowed child of 7770 // any of its ancestors in the same editing host, set the tag name 7771 // of container to the default single-line container name and let 7772 // container be the result." 7773 if (isHtmlElementInArray(container, ["dd", "dt"]) && $_(getAncestors(container)).every(function (ancestor) { return !inSameEditingHost(container, ancestor) || !isAllowedChild(container, ancestor); })) { 7774 container = setTagName(container, defaultSingleLineContainerName, range); 7775 } 7776 7777 // "Fix disallowed ancestors of container." 7778 fixDisallowedAncestors(container, range); 7779 7780 // fix invalid nested lists 7781 if (isNamedHtmlElement(container, 'li') && isNamedHtmlElement(container.nextSibling, "li") && isHtmlElementInArray(container.nextSibling.firstChild, ["ol", "ul"])) { 7782 // we found a li containing only a br followed by a li containing a list as first element: merge the two li's 7783 var listParent = container.nextSibling, 7784 length = container.nextSibling.childNodes.length; 7785 for (i = 0; i < length; i++) { 7786 // we always move the first child into the container 7787 container.appendChild(listParent.childNodes[0]); 7788 } 7789 listParent.parentNode.removeChild(listParent); 7790 } 7791 7792 // "Abort these steps." 7793 return; 7794 } 7795 7796 // "Let new line range be a new range whose start is the same as 7797 // the active range's, and whose end is (container, length of 7798 // container)." 7799 var newLineRange = Aloha.createRange(); 7800 newLineRange.setStart(range.startContainer, range.startOffset); 7801 newLineRange.setEnd(container, getNodeLength(container)); 7802 7803 // "While new line range's start offset is zero and its start node is 7804 // not container, set its start to (parent of start node, index of 7805 // start node)." 7806 while (newLineRange.startOffset == 0 && newLineRange.startContainer != container) { 7807 newLineRange.setStart(newLineRange.startContainer.parentNode, getNodeIndex(newLineRange.startContainer)); 7808 } 7809 7810 // "While new line range's start offset is the length of its start node 7811 // and its start node is not container, set its start to (parent of 7812 // start node, 1 + index of start node)." 7813 while (newLineRange.startOffset == getNodeLength(newLineRange.startContainer) && newLineRange.startContainer != container) { 7814 newLineRange.setStart(newLineRange.startContainer.parentNode, 1 + getNodeIndex(newLineRange.startContainer)); 7815 } 7816 7817 // "Let end of line be true if new line range contains either nothing 7818 // or a single br, and false otherwise." 7819 var containedInNewLineRange = getContainedNodes(newLineRange); 7820 var endOfLine = !containedInNewLineRange.length || (containedInNewLineRange.length == 1 && isNamedHtmlElement(containedInNewLineRange[0], "br")); 7821 7822 // "If the local name of container is "h1", "h2", "h3", "h4", "h5", or 7823 // "h6", and end of line is true, let new container name be the default 7824 // single-line container name." 7825 var newContainerName; 7826 if (/^H[1-6]$/.test(container.tagName) && endOfLine) { 7827 newContainerName = defaultSingleLineContainerName; 7828 7829 // "Otherwise, if the local name of container is "dt" and end of line 7830 // is true, let new container name be "dd"." 7831 } else if (container.tagName == "DT" && endOfLine) { 7832 newContainerName = "dd"; 7833 7834 // "Otherwise, if the local name of container is "dd" and end of line 7835 // is true, let new container name be "dt"." 7836 } else if (container.tagName == "DD" && endOfLine) { 7837 newContainerName = "dt"; 7838 7839 // "Otherwise, let new container name be the local name of container." 7840 } else { 7841 newContainerName = container.tagName.toLowerCase(); 7842 } 7843 7844 // "Let new container be the result of calling createElement(new 7845 // container name) on the context object." 7846 var newContainer = document.createElement(newContainerName); 7847 7848 // "Copy all non empty attributes of the container to new container." 7849 copyAttributes(container, newContainer); 7850 7851 // "If new container has an id attribute, unset it." 7852 newContainer.removeAttribute("id"); 7853 7854 // "Insert new container into the parent of container immediately after 7855 // container." 7856 container.parentNode.insertBefore(newContainer, container.nextSibling); 7857 7858 // "Let contained nodes be all nodes contained in new line range." 7859 var containedNodes = getAllContainedNodes(newLineRange); 7860 7861 // "Let frag be the result of calling extractContents() on new line 7862 // range." 7863 var frag = newLineRange.extractContents(); 7864 7865 // "Unset the id attribute (if any) of each Element descendant of frag 7866 // that is not in contained nodes." 7867 var descendants = getDescendants(frag); 7868 for (i = 0; i < descendants.length; i++) { 7869 if (descendants[i].nodeType == $_.Node.ELEMENT_NODE && $_(containedNodes).indexOf(descendants[i]) == -1) { 7870 descendants[i].removeAttribute("id"); 7871 } 7872 } 7873 7874 var fragChildren = [], 7875 fragChild = frag.firstChild; 7876 if (fragChild) { 7877 do { 7878 if (!isWhitespaceNode(fragChild)) { 7879 fragChildren.push(fragChild); 7880 } 7881 } while (null != (fragChild = fragChild.nextSibling)); 7882 } 7883 7884 // if newContainer is a li and frag contains only a list, we add a br in the li (but only if the height would not change) 7885 if (isNamedHtmlElement(newContainer, 'li') && fragChildren.length && isHtmlElementInArray(fragChildren[0], ["ul", "ol"])) { 7886 oldHeight = newContainer.offsetHeight; 7887 var endBr = createEndBreak(); 7888 newContainer.appendChild(endBr); 7889 newHeight = newContainer.offsetHeight; 7890 if (oldHeight !== newHeight) { 7891 newContainer.removeChild(endBr); 7892 } 7893 } 7894 7895 // "Call appendChild(frag) on new container." 7896 newContainer.appendChild(frag); 7897 7898 // "If container has no visible children, call createElement("br") on 7899 // the context object, and append the result as the last child of 7900 // container." 7901 ensureContainerEditable(container); 7902 7903 // "If new container has no visible children, call createElement("br") 7904 // on the context object, and append the result as the last child of 7905 // new container." 7906 ensureContainerEditable(newContainer); 7907 7908 // "Call collapse(new container, 0) on the context object's Selection." 7909 Aloha.getSelection().collapse(newContainer, 0); 7910 range.setStart(newContainer, 0); 7911 range.setEnd(newContainer, 0); 7912 } 7913 }; 7914 7915 //@} 7916 ///// The insertText command ///// 7917 //@{ 7918 commands.inserttext = { 7919 action: function (value, range) { 7920 var i; 7921 7922 // "Delete the contents of the active range, with strip wrappers 7923 // false." 7924 deleteContents(range, { 7925 7926 stripWrappers: false 7927 }); 7928 7929 // "If the active range's start node is neither editable nor an editing 7930 // host, abort these steps." 7931 if (!isEditable(range.startContainer) && !isEditingHost(range.startContainer)) { 7932 return; 7933 } 7934 7935 // "If value's length is greater than one:" 7936 if (value.length > 1) { 7937 // "For each element el in value, take the action for the 7938 // insertText command, with value equal to el." 7939 for (i = 0; i < value.length; i++) { 7940 commands.inserttext.action(value[i], range); 7941 } 7942 7943 // "Abort these steps." 7944 return; 7945 } 7946 7947 // "If value is the empty string, abort these steps." 7948 if (value == "") { 7949 return; 7950 } 7951 7952 // "If value is a newline (U+00A0), take the action for the 7953 // insertParagraph command and abort these steps." 7954 if (value == "\n") { 7955 commands.insertparagraph.action('', range); 7956 return; 7957 } 7958 7959 // "Let node and offset be the active range's start node and offset." 7960 var node = range.startContainer; 7961 var offset = range.startOffset; 7962 7963 // "If node has a child whose index is offset − 1, and that child is a 7964 // Text node, set node to that child, then set offset to node's 7965 // length." 7966 if (0 <= offset - 1 && offset - 1 < node.childNodes.length && node.childNodes[offset - 1].nodeType == $_.Node.TEXT_NODE) { 7967 node = node.childNodes[offset - 1]; 7968 offset = getNodeLength(node); 7969 } 7970 7971 // "If node has a child whose index is offset, and that child is a Text 7972 // node, set node to that child, then set offset to zero." 7973 if (0 <= offset && offset < node.childNodes.length && node.childNodes[offset].nodeType == $_.Node.TEXT_NODE) { 7974 node = node.childNodes[offset]; 7975 offset = 0; 7976 } 7977 7978 // "If value is a space (U+0020), and either node is an Element whose 7979 // resolved value for "white-space" is neither "pre" nor "pre-wrap" or 7980 // node is not an Element but its parent is an Element whose resolved 7981 // value for "white-space" is neither "pre" nor "pre-wrap", set value 7982 // to a non-breaking space (U+00A0)." 7983 var refElement = node.nodeType == $_.Node.ELEMENT_NODE ? node : node.parentNode; 7984 if (value == " " && refElement.nodeType == $_.Node.ELEMENT_NODE && jQuery.inArray($_.getComputedStyle(refElement).whiteSpace, ["pre", "pre-wrap"]) == -1) { 7985 value = "\xa0"; 7986 } 7987 7988 // "Record current overrides, and let overrides be the result." 7989 var overrides = recordCurrentOverrides(range); 7990 7991 // "If node is a Text node:" 7992 if (node.nodeType == $_.Node.TEXT_NODE) { 7993 // "Call insertData(offset, value) on node." 7994 node.insertData(offset, value); 7995 7996 // "Call collapse(node, offset) on the context object's Selection." 7997 Aloha.getSelection().collapse(node, offset); 7998 range.setStart(node, offset); 7999 8000 // "Call extend(node, offset + 1) on the context object's 8001 // Selection." 8002 Aloha.getSelection().extend(node, offset + 1); 8003 range.setEnd(node, offset + 1); 8004 8005 // "Otherwise:" 8006 } else { 8007 // "If node has only one child, which is a collapsed line break, 8008 // remove its child from it." 8009 // 8010 // FIXME: IE incorrectly returns false here instead of true 8011 // sometimes? 8012 if (node.childNodes.length == 1 && isCollapsedLineBreak(node.firstChild)) { 8013 node.removeChild(node.firstChild); 8014 } 8015 8016 // "Let text be the result of calling createTextNode(value) on the 8017 // context object." 8018 var text = document.createTextNode(value); 8019 8020 // "Call insertNode(text) on the active range." 8021 range.insertNode(text); 8022 8023 // "Call collapse(text, 0) on the context object's Selection." 8024 Aloha.getSelection().collapse(text, 0); 8025 range.setStart(text, 0); 8026 8027 // "Call extend(text, 1) on the context object's Selection." 8028 Aloha.getSelection().extend(text, 1); 8029 range.setEnd(text, 1); 8030 } 8031 8032 // "Restore states and values from overrides." 8033 restoreStatesAndValues(overrides, range); 8034 8035 // "Canonicalize whitespace at the active range's start." 8036 canonicalizeWhitespace(range.startContainer, range.startOffset); 8037 8038 // "Canonicalize whitespace at the active range's end." 8039 canonicalizeWhitespace(range.endContainer, range.endOffset); 8040 8041 // "Call collapseToEnd() on the context object's Selection." 8042 Aloha.getSelection().collapseToEnd(); 8043 range.collapse(false); 8044 } 8045 }; 8046 8047 //@} 8048 ///// The insertUnorderedList command ///// 8049 //@{ 8050 commands.insertunorderedlist = { 8051 // "Toggle lists with tag name "ul"." 8052 action: function (value, range) { 8053 toggleLists("ul", range); 8054 }, 8055 // "True if the selection's list state is "mixed" or "mixed ul", false 8056 // otherwise." 8057 indeterm: function () { 8058 return (/^mixed( ul)?$/).test(getSelectionListState()); 8059 }, 8060 // "True if the selection's list state is "ul", false otherwise." 8061 state: function () { 8062 return getSelectionListState() == "ul"; 8063 } 8064 }; 8065 8066 //@} 8067 ///// The justifyCenter command ///// 8068 //@{ 8069 commands.justifycenter = { 8070 // "Justify the selection with alignment "center"." 8071 action: function (value, range) { 8072 justifySelection("center", range); 8073 }, 8074 indeterm: function () { 8075 // "Block-extend the active range. Return true if among visible 8076 // editable nodes that are contained in the result and have no 8077 // children, at least one has alignment value "center" and at least one 8078 // does not. Otherwise return false." 8079 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8080 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8081 }); 8082 return $_(nodes).some(function (node) { return getAlignmentValue(node) == "center"; }) 8083 && $_(nodes).some(function (node) { return getAlignmentValue(node) != "center"; }); 8084 }, 8085 state: function () { 8086 // "Block-extend the active range. Return true if there is at least one 8087 // visible editable node that is contained in the result and has no 8088 // children, and all such nodes have alignment value "center". 8089 // Otherwise return false." 8090 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8091 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8092 }); 8093 return nodes.length && $_(nodes).every(function (node) { 8094 return getAlignmentValue(node) == "center"; 8095 }); 8096 }, 8097 value: function () { 8098 // "Block-extend the active range, and return the alignment value of 8099 // the first visible editable node that is contained in the result and 8100 // has no children. If there is no such node, return "left"." 8101 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8102 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8103 }); 8104 if (nodes.length) { 8105 return getAlignmentValue(nodes[0]); 8106 } 8107 return "left"; 8108 } 8109 }; 8110 8111 //@} 8112 ///// The justifyFull command ///// 8113 //@{ 8114 commands.justifyfull = { 8115 // "Justify the selection with alignment "justify"." 8116 action: function (value, range) { 8117 justifySelection("justify", range); 8118 }, 8119 indeterm: function () { 8120 // "Block-extend the active range. Return true if among visible 8121 // editable nodes that are contained in the result and have no 8122 // children, at least one has alignment value "justify" and at least 8123 // one does not. Otherwise return false." 8124 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8125 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8126 }); 8127 return $_(nodes).some(function (node) { return getAlignmentValue(node) == "justify"; }) 8128 && $_(nodes).some(function (node) { return getAlignmentValue(node) != "justify"; }); 8129 }, 8130 state: function () { 8131 // "Block-extend the active range. Return true if there is at least one 8132 // visible editable node that is contained in the result and has no 8133 // children, and all such nodes have alignment value "justify". 8134 // Otherwise return false." 8135 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8136 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8137 }); 8138 return nodes.length && $_(nodes).every(function (node) { 8139 return getAlignmentValue(node) == "justify"; 8140 }); 8141 }, 8142 value: function () { 8143 // "Block-extend the active range, and return the alignment value of 8144 // the first visible editable node that is contained in the result and 8145 // has no children. If there is no such node, return "left"." 8146 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8147 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8148 }); 8149 if (nodes.length) { 8150 return getAlignmentValue(nodes[0]); 8151 } 8152 return "left"; 8153 } 8154 }; 8155 8156 //@} 8157 ///// The justifyLeft command ///// 8158 //@{ 8159 commands.justifyleft = { 8160 // "Justify the selection with alignment "left"." 8161 action: function (value, range) { 8162 justifySelection("left", range); 8163 }, 8164 indeterm: function () { 8165 // "Block-extend the active range. Return true if among visible 8166 // editable nodes that are contained in the result and have no 8167 // children, at least one has alignment value "left" and at least one 8168 // does not. Otherwise return false." 8169 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8170 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8171 }); 8172 return $_(nodes).some(function (node) { return getAlignmentValue(node) == "left"; }) 8173 && $_(nodes).some(function (node) { return getAlignmentValue(node) != "left"; }); 8174 }, 8175 state: function () { 8176 // "Block-extend the active range. Return true if there is at least one 8177 // visible editable node that is contained in the result and has no 8178 // children, and all such nodes have alignment value "left". Otherwise 8179 // return false." 8180 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8181 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8182 }); 8183 return nodes.length && $_(nodes).every(function (node) { 8184 return getAlignmentValue(node) == "left"; 8185 }); 8186 }, 8187 value: function () { 8188 // "Block-extend the active range, and return the alignment value of 8189 // the first visible editable node that is contained in the result and 8190 // has no children. If there is no such node, return "left"." 8191 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8192 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8193 }); 8194 if (nodes.length) { 8195 return getAlignmentValue(nodes[0]); 8196 } 8197 return "left"; 8198 } 8199 }; 8200 8201 //@} 8202 ///// The justifyRight command ///// 8203 //@{ 8204 commands.justifyright = { 8205 // "Justify the selection with alignment "right"." 8206 action: function (value, range) { 8207 justifySelection("right", range); 8208 }, 8209 indeterm: function () { 8210 // "Block-extend the active range. Return true if among visible 8211 // editable nodes that are contained in the result and have no 8212 // children, at least one has alignment value "right" and at least one 8213 // does not. Otherwise return false." 8214 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8215 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8216 }); 8217 return $_(nodes).some(function (node) { return getAlignmentValue(node) == "right"; }) 8218 && $_(nodes).some(function (node) { return getAlignmentValue(node) != "right"; }); 8219 }, 8220 state: function () { 8221 // "Block-extend the active range. Return true if there is at least one 8222 // visible editable node that is contained in the result and has no 8223 // children, and all such nodes have alignment value "right". 8224 // Otherwise return false." 8225 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8226 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8227 }); 8228 return nodes.length && $_(nodes).every(function (node) { 8229 return getAlignmentValue(node) == "right"; 8230 }); 8231 }, 8232 value: function () { 8233 // "Block-extend the active range, and return the alignment value of 8234 8235 // the first visible editable node that is contained in the result and 8236 // has no children. If there is no such node, return "left"." 8237 var nodes = getAllContainedNodes(blockExtend(getActiveRange()), function (node) { 8238 return isEditable(node) && isVisible(node) && !node.hasChildNodes(); 8239 }); 8240 if (nodes.length) { 8241 return getAlignmentValue(nodes[0]); 8242 } 8243 return "left"; 8244 } 8245 }; 8246 8247 //@} 8248 ///// The outdent command ///// 8249 //@{ 8250 commands.outdent = { 8251 action: function () { 8252 // "Let items be a list of all lis that are ancestor containers of the 8253 8254 // range's start and/or end node." 8255 // 8256 // It's annoying to get this in tree order using functional stuff 8257 // without doing getDescendants(document), which is slow, so I do it 8258 // imperatively. 8259 var items = []; 8260 (function () { 8261 var ancestorContainer; 8262 for (ancestorContainer = getActiveRange().endContainer; 8263 ancestorContainer != getActiveRange().commonAncestorContainer; 8264 ancestorContainer = ancestorContainer.parentNode) { 8265 if (isNamedHtmlElement(ancestorContainer, "li")) { 8266 items.unshift(ancestorContainer); 8267 } 8268 } 8269 for (ancestorContainer = getActiveRange().startContainer; 8270 ancestorContainer; 8271 ancestorContainer = ancestorContainer.parentNode) { 8272 if (isNamedHtmlElement(ancestorContainer, "li")) { 8273 items.unshift(ancestorContainer); 8274 } 8275 } 8276 }()); 8277 8278 // "For each item in items, normalize sublists of item." 8279 $_(items).forEach(function (thisArg) { 8280 normalizeSublists(thisArg, getActiveRange()); 8281 }); 8282 8283 // "Block-extend the active range, and let new range be the result." 8284 var newRange = blockExtend(getActiveRange()); 8285 8286 // "Let node list be a list of nodes, initially empty." 8287 // 8288 // "For each node node contained in new range, append node to node list 8289 // if the last member of node list (if any) is not an ancestor of node; 8290 // node is editable; and either node has no editable descendants, or is 8291 // an ol or ul, or is an li whose parent is an ol or ul." 8292 var nodeList = getContainedNodes(newRange, function (node) { 8293 return isEditable(node) && (!$_(getDescendants(node)).some(isEditable) || isHtmlElementInArray(node, ["ol", "ul"]) || (isNamedHtmlElement(node, 'li') && isHtmlElementInArray(node.parentNode, ["ol", "ul"]))); 8294 }); 8295 8296 // "While node list is not empty:" 8297 while (nodeList.length) { 8298 // "While the first member of node list is an ol or ul or is not 8299 // the child of an ol or ul, outdent it and remove it from node 8300 // list." 8301 while (nodeList.length && (isHtmlElementInArray(nodeList[0], ["OL", "UL"]) || !isHtmlElementInArray(nodeList[0].parentNode, ["OL", "UL"]))) { 8302 outdentNode(nodeList.shift(), newRange); 8303 } 8304 8305 // "If node list is empty, break from these substeps." 8306 if (!nodeList.length) { 8307 break; 8308 } 8309 8310 // "Let sublist be a list of nodes, initially empty." 8311 var sublist = []; 8312 8313 // "Remove the first member of node list and append it to sublist." 8314 sublist.push(nodeList.shift()); 8315 8316 // "While the first member of node list is the nextSibling of the 8317 // last member of sublist, and the first member of node list is not 8318 // an ol or ul, remove the first member of node list and append it 8319 // to sublist." 8320 while (nodeList.length && nodeList[0] == sublist[sublist.length - 1].nextSibling && !isHtmlElementInArray(nodeList[0], ["OL", "UL"])) { 8321 sublist.push(nodeList.shift()); 8322 } 8323 8324 // "Record the values of sublist, and let values be the result." 8325 var values = recordValues(sublist); 8326 8327 // "Split the parent of sublist, with new parent null." 8328 splitParent(sublist, newRange); 8329 8330 // "Fix disallowed ancestors of each member of sublist." 8331 $_(sublist).forEach(fixDisallowedAncestors); 8332 8333 // "Restore the values from values." 8334 restoreValues(values, newRange); 8335 } 8336 } 8337 }; 8338 8339 //@} 8340 8341 ////////////////////////////////// 8342 ///// Miscellaneous commands ///// 8343 ////////////////////////////////// 8344 8345 ///// The selectAll command ///// 8346 //@{ 8347 commands.selectall = { 8348 // Note, this ignores the whole globalRange/getActiveRange() thing and 8349 // works with actual selections. Not suitable for autoimplementation.html. 8350 action: function () { 8351 // "Let target be the body element of the context object." 8352 var target = document.body; 8353 8354 // "If target is null, let target be the context object's 8355 // documentElement." 8356 if (!target) { 8357 target = document.documentElement; 8358 } 8359 8360 // "If target is null, call getSelection() on the context object, and 8361 // call removeAllRanges() on the result." 8362 if (!target) { 8363 Aloha.getSelection().removeAllRanges(); 8364 8365 // "Otherwise, call getSelection() on the context object, and call 8366 // selectAllChildren(target) on the result." 8367 } else { 8368 Aloha.getSelection().selectAllChildren(target); 8369 } 8370 } 8371 }; 8372 8373 //@} 8374 ///// The styleWithCSS command ///// 8375 //@{ 8376 commands.stylewithcss = { 8377 action: function (value) { 8378 // "If value is an ASCII case-insensitive match for the string 8379 // "false", set the CSS styling flag to false. Otherwise, set the 8380 // CSS styling flag to true." 8381 cssStylingFlag = String(value).toLowerCase() != "false"; 8382 }, 8383 state: function () { 8384 return cssStylingFlag; 8385 } 8386 }; 8387 8388 //@} 8389 ///// The useCSS command ///// 8390 //@{ 8391 commands.usecss = { 8392 action: function (value) { 8393 // "If value is an ASCII case-insensitive match for the string "false", 8394 // set the CSS styling flag to true. Otherwise, set the CSS styling 8395 // flag to false." 8396 cssStylingFlag = String(value).toLowerCase() == "false"; 8397 } 8398 }; 8399 //@} 8400 8401 // Some final setup 8402 //@{ 8403 (function () { 8404 // Opera 11.50 doesn't implement Object.keys, so I have to make an explicit 8405 // temporary, which means I need an extra closure to not leak the temporaries 8406 // into the global namespace. >:( 8407 var commandNames = []; 8408 var command; 8409 for (command in commands) { 8410 if (commands.hasOwnProperty(command)) { 8411 commandNames.push(command); 8412 } 8413 } 8414 $_(commandNames).forEach(function (command) { 8415 // "If a command does not have a relevant CSS property specified, it 8416 // defaults to null." 8417 if (null == commands[command].relevantCssProperty) { 8418 commands[command].relevantCssProperty = null; 8419 } 8420 8421 // "If a command has inline command activated values defined but 8422 // nothing else defines when it is indeterminate, it is indeterminate 8423 // if among editable Text nodes effectively contained in the active 8424 // range, there is at least one whose effective command value is one of 8425 // the given values and at least one whose effective command value is 8426 // not one of the given values." 8427 if (null != commands[command].inlineCommandActivatedValues && null == commands[command].indeterm) { 8428 commands[command].indeterm = function (range) { 8429 var values = $_(getAllEffectivelyContainedNodes(range, function (node) { return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; })) 8430 .map(function (node) { return getEffectiveCommandValue(node, command); }); 8431 8432 var matchingValues = $_(values).filter(function (value) { 8433 return $_(commands[command].inlineCommandActivatedValues).indexOf(value) != -1; 8434 }); 8435 8436 return matchingValues.length >= 1 && values.length - matchingValues.length >= 1; 8437 }; 8438 } 8439 8440 // "If a command has inline command activated values defined, its state 8441 // is true if either no editable Text node is effectively contained in 8442 // the active range, and the active range's start node's effective 8443 // command value is one of the given values; or if there is at least 8444 // one editable Text node effectively contained in the active range, 8445 // and all of them have an effective command value equal to one of the 8446 // given values." 8447 if (null != commands[command].inlineCommandActivatedValues) { 8448 commands[command].state = function (range) { 8449 var nodes = getAllEffectivelyContainedNodes(range, function (node) { 8450 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 8451 }); 8452 8453 if (nodes.length == 0) { 8454 return $_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(range.startContainer, command)) != -1; 8455 } 8456 return $_(nodes).every(function (node) { 8457 return $_(commands[command].inlineCommandActivatedValues).indexOf(getEffectiveCommandValue(node, command)) != -1; 8458 }); 8459 }; 8460 } 8461 8462 // "If a command is a standard inline value command, it is 8463 // indeterminate if among editable Text nodes that are effectively 8464 // contained in the active range, there are two that have distinct 8465 // effective command values. Its value is the effective command value 8466 // of the first editable Text node that is effectively contained in the 8467 // active range, or if there is no such node, the effective command 8468 // value of the active range's start node." 8469 if (null != commands[command].standardInlineValueCommand) { 8470 commands[command].indeterm = function () { 8471 var values = $_(getAllEffectivelyContainedNodes(getActiveRange())).filter(function (node) { return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; }, true) 8472 .map(function (node) { return getEffectiveCommandValue(node, command); }); 8473 var i; 8474 for (i = 1; i < values.length; i++) { 8475 if (values[i] != values[i - 1]) { 8476 return true; 8477 } 8478 } 8479 return false; 8480 }; 8481 8482 commands[command].value = function (range) { 8483 var refNode = getAllEffectivelyContainedNodes(range, function (node) { 8484 return isEditable(node) && node.nodeType == $_.Node.TEXT_NODE; 8485 })[0]; 8486 8487 if (typeof refNode == "undefined") { 8488 refNode = range.startContainer; 8489 } 8490 8491 return getEffectiveCommandValue(refNode, command); 8492 }; 8493 } 8494 }); 8495 }()); 8496 //@} 8497 return { 8498 commands: commands, 8499 execCommand: myExecCommand, 8500 queryCommandIndeterm: myQueryCommandIndeterm, 8501 queryCommandState: myQueryCommandState, 8502 queryCommandValue: myQueryCommandValue, 8503 queryCommandEnabled: myQueryCommandEnabled, 8504 queryCommandSupported: myQueryCommandSupported, 8505 copyAttributes: copyAttributes, 8506 createEndBreak: createEndBreak, 8507 isEndBreak: isEndBreak, 8508 ensureContainerEditable: ensureContainerEditable, 8509 isEditingHost: isEditingHost, 8510 isEditable: isEditable, 8511 getStateOverride: getStateOverride, 8512 setStateOverride: setStateOverride, 8513 resetOverrides: resetOverrides, 8514 unsetStateOverride: unsetStateOverride 8515 }; 8516 }); // end define 8517 // vim: foldmarker=@{,@} foldmethod=marker 8518