Commit 2ba75d0f authored by jan.koester's avatar jan.koester
Browse files

push

parent b59ceba9
Loading
Loading
Loading
Loading
+236 −229
Original line number Diff line number Diff line
/*******************************************************************************
Copyright (c) 2021, Jan Koester jan.koester@gmx.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 * Copyright (c) 2021, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <iostream>
@@ -229,11 +229,12 @@ const std::vector<char>& libhtmlpp::HtmlString::data() {
libhtmlpp::Element &libhtmlpp::HtmlString::parse() {
    HTMLException excp;
    _buildTree();
    return *rootEl;
    return *_rootEl;
}

void libhtmlpp::HtmlString::_buildtreenode(std::unique_ptr<libhtmlpp::DocElements> &firstel,libhtmlpp::DocElements *lastel){

void libhtmlpp::HtmlString::_buildtreenode(DocElements *firstel,
                                           libhtmlpp::DocElements *lastel)
{
    if (!firstel || !lastel) {
        HTMLException excp;
        excp[HTMLException::Error] << "No start Element!";
@@ -241,15 +242,8 @@ void libhtmlpp::HtmlString::_buildtreenode(std::unique_ptr<libhtmlpp::DocElement
    }

    struct cpyel {
        cpyel(){
            start=nullptr;
            end=nullptr;
        };

        cpyel(const cpyel &src){
            start=src.start;
            end=src.end;
        };
        cpyel() : start(nullptr), end(nullptr) {}
        cpyel(const cpyel &src) : start(src.start), end(src.end) {}

        libhtmlpp::DocElements *start;
        libhtmlpp::DocElements *end;
@@ -258,48 +252,64 @@ void libhtmlpp::HtmlString::_buildtreenode(std::unique_ptr<libhtmlpp::DocElement
    std::stack<cpyel> cpylist;

    const DocElements   *prev=nullptr;
    DocElements         *start=firstel.get();
    DocElements         *start=firstel;
    DocElements         *next=firstel->nextel.get();
    const DocElements   *end=lastel;

    auto checkterminator = [](DocElements *termel, const DocElements *end){
        int i=0;
    auto skip_empty = [&](DocElements* cur, const DocElements* stop) -> DocElements* {
        while (cur && cur != stop && (!cur->element)) {
            cur = cur->nextel.get();
        }
        return cur;
    };

    auto checkterminator = [&](DocElements* termel, const DocElements* end) -> DocElements* {
        if (!termel || !termel->element || termel->terminator ||
            termel->element->getType() != HtmlEl)
            return nullptr;

        if(termel->element->getType()!=HtmlEl)
            return (DocElements*) nullptr;
        const std::string &tagname =
        dynamic_cast<HtmlElement*>(termel->element.get())->getTagname();

        for (DocElements* curcel=termel->nextel.get(); curcel; curcel=curcel->nextel.get()) {
        int nesting_level = 0;

            if (curcel->element && curcel->element->getType()==HtmlEl && !curcel->terminator &&
                  *reinterpret_cast<HtmlElement*>(curcel->element.get()) ==
                  reinterpret_cast<HtmlElement*>(termel->element.get()) ) {
                ++i;
            }
        for (DocElements* cur = skip_empty(termel->nextel.get(), end);
             cur && cur != end;
        cur = skip_empty(cur->nextel.get(), end))
             {
                 if (!cur->element) continue;

            if (curcel->element && curcel->element->getType()==HtmlEl && curcel->terminator &&
                 *reinterpret_cast<HtmlElement*>(curcel->element.get()) ==
                 reinterpret_cast<HtmlElement*>(termel->element.get()) ) {
                if(i==0){
                    return curcel;
                 if (cur->element->getType() == HtmlEl) {
                     const std::string &curtag =
                     dynamic_cast<HtmlElement*>(cur->element.get())->getTagname();

                     if (curtag == tagname) {
                         if (cur->terminator) {
                             if (nesting_level == 0)
                                 return cur;
                             --nesting_level;
                         } else {
                    --i;
                             ++nesting_level;
                         }
                     }
                 }
            if(curcel==end)
                break;
             }

        for(size_t ii=0; ContainerTypes[ii]; ++ii){
            if(reinterpret_cast<HtmlElement*>(termel->element.get())->getTagname()==ContainerTypes[ii]){
             // require explicit terminator for container tags
             for (size_t i = 0; ContainerTypes[i]; ++i) {
                 if (tagname == ContainerTypes[i]) {
                     HTMLException e;
                e[HTMLException::Error] << reinterpret_cast<HtmlElement*>(termel->element.get())->getTagname() << " must be terminated ! " << reinterpret_cast<HtmlElement*>(termel->element.get())->getAtributte("id") << " test";
                     e[HTMLException::Error] << tagname << " must be terminated ! "
                     << dynamic_cast<HtmlElement*>(termel->element.get())
                     ->getAtributte("id");
                     throw e;
                 }
             }

        return (DocElements*) nullptr;
             return nullptr;
    };

    size_t counter =0;

NEXTDOCEL:
    std::cout << "start" << start->element.get() << std::endl;

@@ -351,6 +361,7 @@ NEXTDOCEL:
        cpylist.pop();
        goto NEXTDOCEL;
    }

}

void libhtmlpp::HtmlString::_buildTree() {
@@ -410,7 +421,7 @@ BUILDTREE_STARTLOOP:
                    if( std::equal(_Data.begin()+ii,_Data.begin()+(ii+8),"</script")) {

                        std::copy(_Data.begin()+close,_Data.begin()+ii,
                                  std::back_inserter(((SvgElement*)lastEl->element.get())->_Svg));
                                  std::back_inserter(((ScriptElement*)lastEl->element.get())->_Script));
                        ii+=8;
                        break;
                    }
@@ -501,18 +512,13 @@ BUILDTREE_STARTLOOP:
        }
    }

    for(DocElements *el=firstEl.get(); el; el=el->nextel.get()){
        if(el->element->getType()==HtmlEl)
            std::cerr << "Html: " << reinterpret_cast<HtmlElement*>(el->element.get())->getTagname() << "Term: "<< el->terminator <<std::endl;
        else if(el->element->getType()==ScriptEL)
            std::cerr << "Script: " << reinterpret_cast<ScriptElement*>(el->element.get())->getScript() <<std::endl;
        else if(el->element->getType()==TextEl)
            std::cerr << "Text: " << reinterpret_cast<TextElement*>(el->element.get())->getText() <<std::endl;
    for(DocElements *cur=firstEl.get(); cur; cur=cur->nextel.get()){
        std::cout << cur->element->getType() << std::endl;
    }

    _buildtreenode(firstEl,lastEl);
    _buildtreenode(firstEl.get(),lastEl);

    rootEl=std::move(firstEl->element);
    _rootEl=std::move(firstEl->element);
}

void libhtmlpp::HtmlString::_serialelize(std::vector<char> in, libhtmlpp::HtmlElement *out) {
@@ -743,7 +749,7 @@ bool libhtmlpp::HtmlElement::operator==(const HtmlElement *hel){
        return false;
    if( _TagName.size() != hel->_TagName.size())
        return false;
    if(_TagName==hel->_TagName)
    if(std::equal(_TagName.begin(),_TagName.end(),hel->_TagName.begin()))
        return true;
    return false;
}
@@ -751,7 +757,7 @@ bool libhtmlpp::HtmlElement::operator==(const HtmlElement *hel){
bool libhtmlpp::HtmlElement::operator==(const HtmlElement &hel){
    if(_TagName.size() != hel._TagName.size())
        return false;
    if(_TagName==hel._TagName)
    if(std::equal(_TagName.begin(),_TagName.end(),hel._TagName.begin()))
        return true;
    return false;
}
@@ -987,9 +993,9 @@ void libhtmlpp::Element::insertBefore(libhtmlpp::Element* el){
    // }
    // Element *nexel=_prevElement->_nextElement,*prev=nullptr;
    /*
    while(nexel){
        prev=nexel;
        nexel=nexel->nextElement();
     *    while(nexel){
     *        prev=nexel;
     *        nexel=nexel->nextElement();
}

nexel=this;
@@ -1403,9 +1409,10 @@ void libhtmlpp::print(Element* el, HtmlString &output,bool formated) {

    switch(el->getType()){
        case HtmlEl:{
            HtmlElement* htmlel = dynamic_cast<HtmlElement*>(el);
            output.append("<");
            output.append(((HtmlElement*) el)->getTagname());
            for (HtmlElement::Attributes* curattr = ((HtmlElement*) el)->_firstAttr.get(); curattr; curattr = curattr->_nextAttr.get()) {
            output.append(htmlel->getTagname());
            for (HtmlElement::Attributes* curattr = htmlel->_firstAttr.get(); curattr; curattr = curattr->_nextAttr.get()) {
                output.append(" ");
                std::copy(
                    curattr->_Key.begin(),
@@ -1430,21 +1437,21 @@ void libhtmlpp::print(Element* el, HtmlString &output,bool formated) {
                virgin=false;
            }

            if (((HtmlElement*) el)->_childElement) {
            if (htmlel->_childElement) {
                if(formated)
                    output.append("\r\n");
                cpylist.push(el);
                el=((HtmlElement*) el)->_childElement.get();
                el=htmlel->_childElement.get();
                ++lvl;
                goto PRINTNEXTEL;
            }

            //Container must be always terminated fuck html5
            if(isContainer(((HtmlElement*) el)->getTagname())){
            if(isContainer(htmlel->getTagname())){
                output.append("</");
                std::copy(
                    ((HtmlElement*) el)->_TagName.begin(),
                    ((HtmlElement*) el)->_TagName.end(),
                    htmlel->_TagName.begin(),
                          htmlel->_TagName.end(),
                          std::back_inserter(output)
                );
                output.append(">");
@@ -1453,8 +1460,8 @@ void libhtmlpp::print(Element* el, HtmlString &output,bool formated) {
            if(formated)
                output.append("\r\n");

            if (el->_nextElement) {
                el=el->_nextElement.get();
            if (htmlel->_nextElement) {
                el=htmlel->_nextElement.get();
                goto PRINTNEXTEL;
            }
        }break;
+6 −7
Original line number Diff line number Diff line
@@ -71,7 +71,6 @@ namespace libhtmlpp {
        Element();

        std::unique_ptr<Element> _nextElement;

        Element*                             _prevElement;

        friend class HtmlElement;
@@ -118,7 +117,7 @@ namespace libhtmlpp {
        void   remove(Element* el);
    protected:

        std::unique_ptr<Element> _childElement;
        std::unique_ptr<Element>  _childElement=nullptr;

        struct Attributes {
            Attributes();
@@ -204,7 +203,7 @@ namespace libhtmlpp {

    protected:

        Element*    _childElement=nullptr;
        std::unique_ptr<Element> _childElement=nullptr;

        std::vector<char> _Script;
        friend class HtmlString;
@@ -233,7 +232,7 @@ namespace libhtmlpp {

    protected:

        Element*    _childElement=nullptr;
        std::unique_ptr<Element> _childElement=nullptr;

        std::vector<char> _Svg;
        friend class HtmlString;
@@ -288,10 +287,10 @@ namespace libhtmlpp {
        libhtmlpp::Element &parse();

    private:
        std::unique_ptr<Element> rootEl;
        std::unique_ptr<Element> _rootEl;
        void                 _serialelize(std::vector<char> in, HtmlElement* out);
        void                 _buildTree();
        void                 _buildtreenode(std::unique_ptr<DocElements> &firstel,DocElements *lastel);
        void                 _buildtreenode(DocElements *firstel,DocElements *lastel);
        std::vector<char>    _Data;
        std::string                 _Str;
        friend void HtmlEncode(const std::string &input,HtmlString *output);