Commit da86206e authored by jan.koester's avatar jan.koester
Browse files

fixed

parent 3fc84d1a
Loading
Loading
Loading
Loading
+52 −129
Original line number Diff line number Diff line
@@ -25,10 +25,10 @@ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>

#include <algorithm>
#include <fstream>
@@ -299,7 +299,7 @@ void libhtmlpp::HtmlString::_buildtreenode(const libhtmlpp::DocElements *firstel
    auto checkterminator = [](const DocElements *termel, const DocElements *end){
        int i=0;

        for (DocElements* curcel=termel->nextel; curcel; curcel=curcel->nextel) {
        for (const DocElements* curcel=termel->nextel; curcel; curcel=curcel->nextel) {

            if (curcel->element && curcel->element->getType()==HtmlEl && !curcel->terminator &&
                *((HtmlElement*)curcel->element) == ((HtmlElement*)termel->element)) {
@@ -317,12 +317,12 @@ void libhtmlpp::HtmlString::_buildtreenode(const libhtmlpp::DocElements *firstel
            if(curcel==end)
                break;
        }
        return (DocElements*) nullptr;
        return (const DocElements*) nullptr;
    };

NEXTDOCEL:

    DocElements *parent=checkterminator(start,end);
    const DocElements *parent=checkterminator(start,end);


    if(prev && !start->terminator){
@@ -385,136 +385,52 @@ libhtmlpp::Element *libhtmlpp::HtmlString::_buildTree() {
    };

    HTMLException excp;
    size_t open=std::string::npos;
    size_t terminate=std::string::npos;
    size_t close=std::string::npos;
    size_t starttag=std::string::npos;
    size_t comment=std::string::npos;
    size_t prevclose=std::string::npos;

    bool added=false;
    size_t close=_Data.size();

    for(size_t ii=0; ii<_Data.size(); ++ii){
        switch(_Data[ii]){
            case HTMLTAG_OPEN:
                if(open==std::string::npos){
                    open = ii;
                }
                break;
            case HTMLTAG_TERMINATE:
                if(open!=std::string::npos && close ==std::string::npos
                    && starttag ==std::string::npos ){
                    terminate=ii;
        if(_Data[ii]==HTMLTAG_OPEN){
                if(int(ii-close)>1){
                    std::vector<char> buf;
                    std::copy(_Data.begin()+close,_Data.begin()+ii,std::back_inserter(buf));
                    addelement(&firstEl,&lastEl,new TextElement());
                    std::copy(buf.begin(),buf.end(),std::inserter<std::vector<char>>(((TextElement*)(lastEl->element))->_Text,
                                    ((TextElement*)lastEl->element)->_Text.begin()));
                }
                break;
            case HTMLTAG_CLOSE:
                if(open!=std::string::npos){
                    close = ii;
        }

        if(strncmp(_Data.data()+ii,"<!--",4)==0){
                ii+=4;
                size_t start=ii;
                while(ii<_Data.size()){
                    if(strncmp(_Data.data()+ii,"-->",3)==0 ) {
                        addelement(&firstEl,&lastEl,new CommentElement());
                        std::copy(_Data.begin()+start,_Data.begin()+ii,
                                std::back_inserter(((CommentElement*)lastEl->element)->_Comment));
                        close=ii+3;
                        break;
            case HTMLTAG_COMMENT:
                if(open!=std::string::npos && close ==std::string::npos){
                    if( ii+3 < _Data.size() && _Data[ii+1]=='-' && _Data[ii+2]=='-'){
                        comment=(ii+=2);
                    }
                    ++ii;
                }
                break;
            case ' ':
                break;
            default:
                if(open!=std::string::npos && close ==std::string::npos &&
                    starttag==std::string::npos && comment==std::string::npos)
                    starttag=ii;
                break;
        }

        if(open!=std::string::npos &&  close !=std::string::npos && starttag!=std::string::npos){
        }else if(_Data[ii]==HTMLTAG_OPEN){
                addelement(&firstEl,&lastEl,new HtmlElement());

            if(terminate!=std::string::npos){
                size_t start=ii;
                while(ii<_Data.size()){
                    if(_Data[ii]==HTMLTAG_TERMINATE){
                        lastEl->terminator=true;
                    }
                    if(_Data[ii]==HTMLTAG_CLOSE) {
                        std::vector<char> tel;

            ++close;

            std::copy(_Data.begin()+open,_Data.begin()+close,std::inserter<std::vector<char>>(tel,tel.begin()));
                        std::copy(_Data.begin()+start,_Data.begin()+ii,
                                std::inserter<std::vector<char>>(tel,tel.begin()));
                        _serialelize(tel,(HtmlElement*)lastEl->element);
            prevclose=close;
            added=true;
        }else if (open!=std::string::npos &&  comment !=std::string::npos){
            int ctlvl=0;
            size_t endcomment=std::string::npos;
            while(ii < _Data.size()){
                switch(_Data[ii]){
                    case '-':
                        if(endcomment==std::string::npos)
                            endcomment=ii;
                    ++ctlvl;
                    break;
                    case '>':
                        if(endcomment!=std::string::npos && ctlvl==2){
                            if(endcomment!=std::string::npos && ctlvl==2){
                                addelement(&firstEl,&lastEl,new CommentElement());
                                std::vector<char> cel;
                                std::copy(_Data.begin()+(comment+1),_Data.begin()+endcomment,std::inserter<std::vector<char>>(cel,cel.begin()));
                                ((CommentElement*)lastEl->element)->_Comment=cel;
                            }
                            prevclose=++ii;
                            added=true;
                            goto ENDCOMMANDTAGDOUND;
                        }
                        break;
                    default:
                        ctlvl=0;
                        endcomment=std::string::npos;
                        close=ii+1;
                        break;
                    }
                    ++ii;
                }
        }
ENDCOMMANDTAGDOUND:
        if(prevclose!=std::string::npos && open!=std::string::npos
            && int(open-prevclose) > 0){
            std::vector<char> buf;
            bool start=false;
            for(size_t it = prevclose; it<open; ++it){
                switch(_Data[it]){
                    case '\r':
                        continue;
                    case '\n':
                        continue;
                    case ' ':
                        if(!start)
                            continue;
                        else
                            buf.push_back(_Data[it]);
                        continue;
                    default:
                        start=true;
                        buf.push_back(_Data[it]);
                        continue;
                }
            }
            if(!buf.empty()){
                addelement(&firstEl,&lastEl,new TextElement());
                std::copy(buf.begin(),buf.end(),std::inserter<std::vector<char>>(((TextElement*)(lastEl->element))->_Text,
                          ((TextElement*)lastEl->element)->_Text.begin()));
                lastEl->terminator=false;
            }
            prevclose=open;
        }

        if(added){
            open=std::string::npos;
            terminate=std::string::npos;
            close=std::string::npos;
            starttag=std::string::npos;
            comment=std::string::npos;
            added=false;
    }
    }

    Element *first = firstEl->element;

    _buildtreenode(firstEl,lastEl);
@@ -784,7 +700,7 @@ void libhtmlpp::HtmlElement::appendChild(const Element& el){
bool libhtmlpp::HtmlElement::operator==(const HtmlElement *hel){
    if(_TagName.size() != hel->_TagName.size())
        return false;
    if(memcmp(_TagName.data(),hel->_TagName.data(),_TagName.size())==0)
    if(strncmp(_TagName.data(),hel->_TagName.data(),_TagName.size())==0)
        return true;
    return false;
}
@@ -792,7 +708,7 @@ bool libhtmlpp::HtmlElement::operator==(const HtmlElement *hel){
bool libhtmlpp::HtmlElement::operator==(const HtmlElement &hel){
    if(_TagName.size() != hel._TagName.size())
        return false;
    if(memcmp(_TagName.data(),hel._TagName.data(),_TagName.size())==0)
    if(strncmp(_TagName.data(),hel._TagName.data(),_TagName.size())==0)
        return true;
    return false;
}
@@ -853,7 +769,7 @@ DELETEELEMENT:
namespace libhtmlpp {

    void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src){
        const libhtmlpp::Element* prev=nullptr;
        const libhtmlpp::Element* prev=dest->prevElement();
        if(!src || !dest)
            return;
        struct cpyel {
@@ -882,6 +798,13 @@ namespace libhtmlpp {

        Element *firstdest=dest;

        if(firstdest->prevElement()){
            do{
                firstdest=firstdest->_prevElement;
            }while(firstdest->_prevElement);
        }


NEWEL:
        if(src->getType()==HtmlEl && dest->getType()==HtmlEl){
            ((libhtmlpp::HtmlElement*)dest)->_TagName=(((libhtmlpp::HtmlElement*)src)->_TagName);