Файл: library/wysihtml5/src/dom/resolve_list.js
Строк: 103
<?php
/**
* Unwraps an unordered/ordered list
*
* @param {Element} element The list element which should be unwrapped
*
* @example
* <!-- Assume the following dom: -->
* <ul id="list">
* <li>eminem</li>
* <li>dr. dre</li>
* <li>50 Cent</li>
* </ul>
*
* <script>
* wysihtml5.dom.resolveList(document.getElementById("list"));
* </script>
*
* <!-- Will result in: -->
* eminem<br>
* dr. dre<br>
* 50 Cent<br>
*/
(function(dom) {
function _isBlockElement(node) {
return dom.getStyle("display").from(node) === "block";
}
function _isLineBreak(node) {
return node.nodeName === "BR";
}
function _appendLineBreak(element) {
var lineBreak = element.ownerDocument.createElement("br");
element.appendChild(lineBreak);
}
function resolveList(list, useLineBreaks) {
if (!list.nodeName.match(/^(MENU|UL|OL)$/)) {
return;
}
var doc = list.ownerDocument,
fragment = doc.createDocumentFragment(),
previousSibling = list.previousElementSibling || list.previousSibling,
firstChild,
lastChild,
isLastChild,
shouldAppendLineBreak,
paragraph,
listItem;
if (useLineBreaks) {
// Insert line break if list is after a non-block element
if (previousSibling && !_isBlockElement(previousSibling)) {
_appendLineBreak(fragment);
}
while (listItem = (list.firstElementChild || list.firstChild)) {
lastChild = listItem.lastChild;
while (firstChild = listItem.firstChild) {
isLastChild = firstChild === lastChild;
// This needs to be done before appending it to the fragment, as it otherwise will lose style information
shouldAppendLineBreak = isLastChild && !_isBlockElement(firstChild) && !_isLineBreak(firstChild);
fragment.appendChild(firstChild);
if (shouldAppendLineBreak) {
_appendLineBreak(fragment);
}
}
listItem.parentNode.removeChild(listItem);
}
} else {
while (listItem = (list.firstElementChild || list.firstChild)) {
if (listItem.querySelector && listItem.querySelector("div, p, ul, ol, menu, blockquote, h1, h2, h3, h4, h5, h6")) {
while (firstChild = listItem.firstChild) {
fragment.appendChild(firstChild);
}
} else {
paragraph = doc.createElement("p");
while (firstChild = listItem.firstChild) {
paragraph.appendChild(firstChild);
}
fragment.appendChild(paragraph);
}
listItem.parentNode.removeChild(listItem);
}
}
list.parentNode.replaceChild(fragment, list);
}
dom.resolveList = resolveList;
})(wysihtml5.dom);
?>