views:

113

answers:

1

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);

    }
}
A: 

Thanks a lot! That was exactly what I was needing... Worked perfectly except for:

listXML.parent().replace(listXML.childIndex(), '(ul)' + listXML.toXMLString() + '(/ul)');

Instead I used:

listXML.parent().replace(listXML.childIndex(), (ul){listXML}(/ul));

Is there a better way to do it? I first try with string regexp but didn't worked out... But yes, proabably there's a better way to do it... Anyway! it works.. Thank again,

d

daniel