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