Commit 024e9ad2 authored by jan.koester's avatar jan.koester
Browse files

comments now seperated

parent 2aa7c9cd
Loading
Loading
Loading
Loading
+95 −30
Original line number Diff line number Diff line
@@ -56,8 +56,8 @@ namespace libhtmlpp {
        bool                    terminator;
        class DocElements*      nextel;
        class DocElements*      prevel;
        int                     spos;
        int                     epos;
        ssize_t                 spos;
        ssize_t                 epos;

        DocElements() {
            nextel = nullptr;
@@ -264,7 +264,7 @@ bool libhtmlpp::HtmlString::validate(std::string *err){
    return false;
}

void libhtmlpp::HtmlString::_buildtreenode(libhtmlpp::DocElements *start){
void libhtmlpp::HtmlString::_buildtreenode(libhtmlpp::DocElements *srcel){

    struct cpyel {
        cpyel(){
@@ -286,6 +286,7 @@ void libhtmlpp::HtmlString::_buildtreenode(libhtmlpp::DocElements *start){

    std::stack<cpyel> cpylist;

    DocElements *start=srcel;
    DocElements *next=start->nextel;
    DocElements *prev=start->prevel;
    DocElements *end=nullptr;
@@ -374,41 +375,61 @@ libhtmlpp::Element* libhtmlpp::HtmlString::_buildTree(ssize_t& pos) {
    size_t i = 0;

    while( i < _HTableSize ) {
        if(_HTable[i][0] == -1 || _HTable[i][2] == -1)
            continue;
        if(_HTable[i][0] == -1 || _HTable[i][2] == -1){
            break;
        }

        addelement(&firstEl,&lastEl);

        lastEl->spos = _HTable[i][0];
        lastEl->epos = _HTable[i][2]+1;
        lastEl->epos = _HTable[i][2];

        if (_HTable[i][1] != -1){
            lastEl->terminator = true;
        }


        bool comment = false;

        if((size_t)lastEl->spos+3<_Data.size()){
            if(_Data.at(lastEl->spos)=='<' && _Data.at(lastEl->spos+1)=='!' &&
                _Data.at(lastEl->spos+2)=='-' && _Data.at(lastEl->spos+3)=='-')
                comment=true;
        }

        if(!comment){
            std::vector<char> el;
            std::copy(_Data.begin()+lastEl->spos,_Data.begin()+lastEl->epos,std::inserter<std::vector<char>>(el,el.begin()));

        std::cout.write(el.data(),el.size())<<std::endl;

            lastEl->element=new HtmlElement();

            _serialelize(el,(HtmlElement*)lastEl->element);
        }else{
            lastEl->element=new CommentElement();
            std::copy(_Data.begin()+lastEl->spos,_Data.begin()+lastEl->epos,
                        std::inserter<std::vector<char>>(((CommentElement*) lastEl->element)->_Comment,((CommentElement*) lastEl->element)->_Comment.begin()));
            ++i;
            continue;
        }

        ++i;
        std::cout << i << ": " << _HTableSize << std::endl;

        size_t epos = i < _HTableSize ? _HTable[i][0] :  _Data.size();
        ssize_t epos = i < _HTableSize ? _HTable[i][0] :  _Data.size();
        --epos;

        if(epos - _HTable[i-1][2] > 0){
        ssize_t spos = _HTable[i-1][2]+1;

        std::cout << spos << ":" << epos << ": "<< int(epos - spos)  << std::endl;

        if(int(epos - spos) > 0){
            addelement(&firstEl,&lastEl);
            lastEl->element=new TextElement();
            lastEl->spos = _HTable[i-1][2]+1;
            lastEl->spos = spos;
            lastEl->epos = epos;
            std::copy(_Data.begin()+lastEl->spos,_Data.begin()+lastEl->epos,
                        std::inserter<std::vector<char>>(((TextElement*) lastEl->element)->_Text,((TextElement*) lastEl->element)->_Text.begin()));
        }

    }

    _buildtreenode(firstEl);
@@ -511,11 +532,7 @@ void libhtmlpp::HtmlString::_parseTree(){
    for (size_t i = 0; i < _Data.size(); ++i) {
        switch (_Data[i]) {
            case HTMLTAG_CLOSE:
                 if (memcmp(_Data.data()+i,"<--",3) !=0) {
                        if (memcmp(_Data.data()+(i - 2),"-->",3)!=0) {
                ++closetag;
                    }
                 }
            break;
        default:
            break;
@@ -539,13 +556,9 @@ void libhtmlpp::HtmlString::_parseTree(){
        switch(_Data[ii]){
            case HTMLTAG_OPEN:
                if(!open){
                    if ( memcmp(_Data.data()+ii,"<!--",4) != 0 ) {
                    open = true;
                    pterm = true;
                    _HTable[ip][0] = ii;
                    }else{
                        ii+=4;
                    }
                }
                break;
            case HTMLTAG_TERMINATE:
@@ -553,11 +566,9 @@ void libhtmlpp::HtmlString::_parseTree(){
                    _HTable[ip][1]=ii;
                break;
            case HTMLTAG_CLOSE:
                if (memcmp(_Data.data()+ii,"<--",3) !=0 && open) {
                    _HTable[ip][2] = ii;
                    ++ip;
                    open = false;
                }
                break;
            case ' ':
                break;
@@ -902,7 +913,40 @@ void libhtmlpp::TextElement::setText(const char* txt){
const char * libhtmlpp::TextElement::getText(){
    _CStr=_Text;
    _CStr.push_back('\0');
    return _Text.data();
    return _CStr.data();
}

libhtmlpp::CommentElement::CommentElement() : Element(){
    _Type=CommentEl;
}

libhtmlpp::CommentElement::CommentElement(const CommentElement &comel) : Element(){
    _Type=CommentEl;
    _copy(this,&comel);
}

libhtmlpp::CommentElement::~CommentElement(){
}

libhtmlpp::CommentElement & libhtmlpp::CommentElement::operator=(const libhtmlpp::Element& hel){
    _copy(this,&hel);
    return *this;
}

libhtmlpp::CommentElement & libhtmlpp::CommentElement::operator=(const libhtmlpp::Element* hel){
    _copy(this,hel);
    return *this;
}

void libhtmlpp::CommentElement::setComment(const char* txt){
    std::copy(txt,txt+strlen(txt),
              std::insert_iterator<std::vector<char>>(_Comment,_Comment.begin()));
}

const char * libhtmlpp::CommentElement::getComment(){
    _CStr=_Comment;
    _CStr.push_back('\0');
    return _CStr.data();
}


@@ -1076,7 +1120,28 @@ PRINTNEXTEL:
                }
            }
        }break;
        case CommentEl: {
            output.append(((CommentElement*)el)->_Comment.data(),((CommentElement*)el)->_Comment.size());

            if (el->_nextElement) {
                el=el->_nextElement;
                goto PRINTNEXTEL;
            }

            while(!cpylist->empty()){
                el=cpylist->top();

                output.append("</");
                output.append(((HtmlElement*) el)->_TagName.data(),((HtmlElement*) el)->_TagName.size());
                output.append(">");

                cpylist->pop();
                if(el->_nextElement){
                    el=el->_nextElement;
                    goto PRINTNEXTEL;
                }
            }
        }break;
        default:
            HTMLException excp;
            excp[HTMLException::Error] << "Unkown Elementtype";
+23 −2
Original line number Diff line number Diff line
@@ -40,7 +40,8 @@ namespace libhtmlpp {

    enum ElementType{
        TextEl=0,
        HtmlEl=1
        HtmlEl=1,
        CommentEl=2
    };

    class Element {
@@ -154,6 +155,26 @@ namespace libhtmlpp {
        friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
    };

    class CommentElement : public Element{
    public:
        CommentElement();
        CommentElement(const CommentElement &comel);
        ~CommentElement();

        CommentElement& operator=(const Element &hel);
        CommentElement& operator=(const Element *hel);

        const char *getComment();
        void        setComment(const char *txt);

    protected:
        std::vector<char> _Comment;
        std::vector<char> _CStr;
        friend class HtmlString;
        friend void  print(Element* el, HtmlString &output);
        friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);
    };

    void print(Element* el, HtmlString &output);

    class HtmlString {
@@ -199,7 +220,7 @@ namespace libhtmlpp {
        void               _parseTree();
        void               _serialelize(std::vector<char> in, HtmlElement* out);
        Element*           _buildTree(ssize_t& pos);
        void               _buildtreenode(DocElements *start);
        void               _buildtreenode(DocElements *srcel);
        std::vector<char>  _Data;
        std::vector<char>  _CStr;
        ssize_t**          _HTable;
+5 −5
Original line number Diff line number Diff line
@@ -38,11 +38,11 @@ class HtmlCopy{
public:
    HtmlCopy(libhtmlpp::Element *index){
        index2=index;
        libhtmlpp::HtmlElement  el("span");
        el.setAttribute("id","txt3");
        libhtmlpp::HtmlElement *body=index2.getElementbyTag("body");
        if(body)
            body->appendChild(&el);
        // libhtmlpp::HtmlElement  el("span");
        // el.setAttribute("id","txt3");
        // libhtmlpp::HtmlElement *body=index2.getElementbyTag("body");
        // if(body)
        //     body->appendChild(&el);
    }

    void printModify(){