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