Commit 1184266b authored by jan.koester's avatar jan.koester
Browse files

added remove method

parent e3da4ef3
Loading
Loading
Loading
Loading
+87 −25
Original line number Diff line number Diff line
@@ -453,11 +453,11 @@ libhtmlpp::Element *libhtmlpp::HtmlString::_buildTree() {
                    }
                }
                if(!buf.empty()){
                    addelement(&firstEl,&lastEl);
                    lastEl->element=new TextElement();
                    lastEl->element->_firstElement=firstEl->element;
                    ((TextElement*)lastEl->element)->_Text=buf;
                    lastEl->terminator=false;
                    // addelement(&firstEl,&lastEl);
                    // lastEl->element=new TextElement();
                    // lastEl->element->_firstElement=firstEl->element;
                    // ((TextElement*)lastEl->element)->_Text=buf;
                    // lastEl->terminator=false;
                }
            }

@@ -476,7 +476,7 @@ libhtmlpp::Element *libhtmlpp::HtmlString::_buildTree() {
    while(firstEl){
        DocElements *next=firstEl->nextel;
        if(firstEl->terminator)
            delete firstEl->element;
             first->remove(firstEl->element);
        delete firstEl;
        firstEl=next;
    }
@@ -600,8 +600,6 @@ libhtmlpp::HtmlElement::HtmlElement(const libhtmlpp::HtmlElement* hel) : HtmlEle
}

libhtmlpp::HtmlElement::~HtmlElement(){
    _nextElement=nullptr;
    _prevElement=nullptr;

    Attributes *cura=_firstAttr;
    while(cura){
@@ -613,25 +611,24 @@ libhtmlpp::HtmlElement::~HtmlElement(){

    Element *cur=_firstElement;

    _firstAttr=nullptr;
    _lastAttr=nullptr;
    _childElement=nullptr;

    std::stack<Element*> childs;
    std::stack<Element*> delelm;

DELETEELEMENT:
    while(cur){
        Element *next=cur->_nextElement;
        if(cur!=this){
            cur->_firstElement=nullptr;
            cur->_nextElement=nullptr;
            cur->_prevElement=nullptr;
        if(cur->getType()==HtmlEl && ((HtmlElement*)cur)->_childElement){
            if(cur->getType()==HtmlEl){
                if(((HtmlElement*)cur)->_childElement){
                    childs.push(((HtmlElement*)cur)->_childElement);
                    ((HtmlElement*)cur)->_childElement=nullptr;
                }
        if(cur!=this)
                delelm.push(cur);
            }
        }
        cur=next;
    }
    if(!childs.empty()){
@@ -640,15 +637,24 @@ DELETEELEMENT:
        goto DELETEELEMENT;
    }

    _nextElement=nullptr;
    _prevElement=nullptr;
    _childElement=nullptr;
    _firstElement=nullptr;

    if(delelm.empty())
        return;

    while(!delelm.empty()){
        Element *itm=delelm.top();
    while(itm){
        itm=delelm.top();
        delete itm;
        delelm.pop();
    }

}

int libhtmlpp::Element::getType() const{
    return -1;
}

void libhtmlpp::HtmlElement::setTagname(const char* name){
@@ -743,6 +749,41 @@ libhtmlpp::HtmlElement & libhtmlpp::HtmlElement::operator=(const libhtmlpp::Html
    return *this;
}

void libhtmlpp::HtmlElement::remove(libhtmlpp::Element* el){
    Element *cur=_firstElement;

    std::stack<Element*> childs;

DELETEELEMENT:
    while(cur){
        Element *next=cur->_nextElement;
        if(cur->getType()==HtmlEl){
            if(((HtmlElement*)cur)->_childElement){
                childs.push(((HtmlElement*)cur)->_childElement);
                if(((HtmlElement*)cur)->_childElement==this)
                    ((HtmlElement*)cur)->_childElement=nullptr;
            }
        }
        if(cur==el){
            cur->_firstElement=this;
            if(cur->_prevElement)
                cur->_prevElement->_nextElement=cur->_nextElement;
            if(cur->_nextElement)
                cur->_nextElement->_prevElement=cur->_prevElement;
            cur->_prevElement=nullptr;
            cur->_nextElement=nullptr;
            delete cur;
        }
        cur=next;
    }
    if(!childs.empty()){
        cur=childs.top();
        childs.pop();
        goto DELETEELEMENT;
    }
}


namespace libhtmlpp {

    void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src){
@@ -936,7 +977,6 @@ libhtmlpp::Element::Element(const libhtmlpp::Element& el) : Element() {
}

libhtmlpp::Element::~Element(){

     Element *curel=_firstElement;
    _firstElement=nullptr;
    _prevElement=nullptr;
@@ -947,12 +987,34 @@ libhtmlpp::Element::~Element(){
        curel->_firstElement=nullptr;
        curel->_prevElement=nullptr;
        curel->_nextElement=nullptr;
        if(curel!=this)
        if(curel!=this && curel->getType()!=HtmlEl)
            delete curel;
        curel=next;
    }
};

void libhtmlpp::Element::remove(libhtmlpp::Element* el){
     Element *curel=_firstElement;
     while(curel){
         Element *next=curel->_nextElement;
         if(curel==el){
                curel->_firstElement=nullptr;

                if(curel->_prevElement)
                    curel->_prevElement->_nextElement=curel->_nextElement;

                if(curel->_nextElement)
                    curel->_nextElement->_prevElement=curel->_prevElement;
                curel->_prevElement=nullptr;
                curel->_nextElement=nullptr;
                delete curel;
                break;
         }
        curel=next;
    }
}


libhtmlpp::TextElement::TextElement() : Element(){
}

+3 −1
Original line number Diff line number Diff line
@@ -62,6 +62,8 @@ namespace libhtmlpp {
        Element*       nextElement() const;
        Element*       prevElement() const;

        virtual void   remove(Element* el);

        virtual int    getType() const=0;
    protected:
        Element*      _prevElement;
@@ -108,7 +110,7 @@ namespace libhtmlpp {
        HtmlElement *getElementbyTag(const char *tag) const;

        int    getType() const;

        void   remove(Element* el);
    protected:

        Element*    _childElement;