I'm trying to use XML to convert the code that a Flex RTE creates to valid HTML. No problems on other HTML elements, but difficulty with unordered lists. I created a solution for moving consecutive LIs into a UL node. After trial and error, I came up with the following solution. My question is - surely there's a better way to do this?
for each (listXML:XML in xml..li) {
if (listXML.children().length() == 0) {
// list item is empty - make it an empty paragraph instead
listXML.parent().replace( listXML.childIndex(), <p /> );
} else if (listXML.parent().children()[listXML.childIndex() - 1].name() != 'ul') {
// first LI - wrap in UL
listXML.parent().replace(listXML.childIndex(), '<ul>' + listXML.toXMLString() + '</ul>');
} else if (listXML.parent().children()[listXML.childIndex() - 1].name() == 'ul') {
// move LI into previous UL node
var lastUL:int = listXML.parent().children()[listXML.childIndex() -1].childIndex();
var parentXML:XML = listXML.parent();
delete listXML.parent().children()[listXML.childIndex()];
parentXML.children()[lastUL].appendChild(listXML);
}
}