| 1022 | |
| 1023 | // Split a line after the given node. |
| 1024 | function breakAfter(lineEndNode) { |
| 1025 | // If there's nothing to the right, then we can skip ending the line |
| 1026 | // here, and move root-wards since splitting just before an end-tag |
| 1027 | // would require us to create a bunch of empty copies. |
| 1028 | while (!lineEndNode.nextSibling) { |
| 1029 | lineEndNode = lineEndNode.parentNode; |
| 1030 | if (!lineEndNode) { return; } |
| 1031 | } |
| 1032 | |
| 1033 | function breakLeftOf(limit, copy) { |
| 1034 | // Clone shallowly if this node needs to be on both sides of the break. |
| 1035 | var rightSide = copy ? limit.cloneNode(false) : limit; |
| 1036 | var parent = limit.parentNode; |
| 1037 | if (parent) { |
| 1038 | // We clone the parent chain. |
| 1039 | // This helps us resurrect important styling elements that cross lines. |
| 1040 | // E.g. in <i>Foo<br>Bar</i> |
| 1041 | // should be rewritten to <li><i>Foo</i></li><li><i>Bar</i></li>. |
| 1042 | var parentClone = breakLeftOf(parent, 1); |
| 1043 | // Move the clone and everything to the right of the original |
| 1044 | // onto the cloned parent. |
| 1045 | var next = limit.nextSibling; |
| 1046 | parentClone.appendChild(rightSide); |
| 1047 | for (var sibling = next; sibling; sibling = next) { |
| 1048 | next = sibling.nextSibling; |
| 1049 | parentClone.appendChild(sibling); |
| 1050 | } |
| 1051 | } |
| 1052 | return rightSide; |
| 1053 | } |
| 1054 | |
| 1055 | var copiedListItem = breakLeftOf(lineEndNode.nextSibling, 0); |
| 1056 | |
| 1057 | // Walk the parent chain until we reach an unattached LI. |
| 1058 | for (var parent; |
| 1059 | // Check nodeType since IE invents document fragments. |
| 1060 | (parent = copiedListItem.parentNode) && parent.nodeType === 1;) { |
| 1061 | copiedListItem = parent; |
| 1062 | } |
| 1063 | // Put it on the list of lines for later processing. |
| 1064 | listItems.push(copiedListItem); |
| 1065 | } |
| 1066 | |
| 1067 | // Split lines while there are lines left to split. |
| 1068 | for (var i = 0; // Number of lines that have been split so far. |