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

changed api

copy operator
parent 0006435e
Loading
Loading
Loading
Loading
+58 −22
Changes for src/html.cpp: 58 added lines, 22 removed lines.
Original line number Diff line number Diff line
@@ -125,7 +125,12 @@ libhtmlpp::HtmlString &libhtmlpp::HtmlString::operator=(const char *src){
    return *this;
}

const char libhtmlpp::HtmlString::operator[](size_t pos) {
const char libhtmlpp::HtmlString::operator[](size_t pos) const{
    if(_Data.length()<pos){
        HTMLException exp;
        exp[HTMLException::Error] << "HtmlString[] out of range !";
        throw exp;
    }
    return _Data[pos];
}

@@ -169,7 +174,7 @@ const size_t libhtmlpp::HtmlString::size() {
    return _Data.size();
}

const size_t libhtmlpp::HtmlString::length() {
const size_t libhtmlpp::HtmlString::length() const{
    return _Data.length();
}

@@ -442,6 +447,13 @@ libhtmlpp::HtmlElement::HtmlElement(const char *tagname){
        _TagName = tagname;
}

libhtmlpp::HtmlElement::HtmlElement(){
    _firstAttr=nullptr;
    _lastAttr=nullptr;
    _childElement=nullptr;
    _Type=HtmlEl;
}

libhtmlpp::HtmlElement::~HtmlElement(){
    if(_Type==HtmlEl){
        delete   _firstAttr;
@@ -476,6 +488,29 @@ void libhtmlpp::HtmlElement::appendChild(libhtmlpp::Element* el){
    }
}

libhtmlpp::Element& libhtmlpp::HtmlElement::operator=(const Element &hel){
    Element *dest=nullptr,*prev=nullptr;
    for(Element *curel=(Element*)&hel; curel; curel=curel->nextElement()){
        if(curel->_Type==HtmlEl){
            dest=new HtmlElement(((HtmlElement*)curel)->_TagName.c_str());
            for(Attributes *cattr=((HtmlElement*)curel)->_firstAttr; cattr; cattr=cattr->_nextAttr){
                ((HtmlElement*)dest)->setAttribute(cattr->_Key.c_str(),cattr->_Value.c_str());
            }
            if(((HtmlElement*)curel)->_childElement){
                ((HtmlElement*)dest)->_childElement=((HtmlElement*)curel)->_childElement;
            }
        }else if(curel->_Type==TextEl){
            dest=new TextElement();
            ((TextElement*)dest)->_Text=((TextElement*)curel)->_Text;
        }
        dest->_Type=curel->_Type;
        dest->_prevElement=prev;
        prev=dest;
        dest=dest->_nextElement;
    }
    return *dest;
}


void libhtmlpp::Element::insertBefore(libhtmlpp::Element* el){
    _prevElement->_nextElement=el;
@@ -506,13 +541,15 @@ int libhtmlpp::Element::getType(){


libhtmlpp::HtmlPage::HtmlPage(){
    _RootNode=nullptr;
}

libhtmlpp::HtmlPage::~HtmlPage(){
    delete _RootNode;
}

libhtmlpp::HtmlString *libhtmlpp::HtmlPage::loadFile(const char* path){
    _Page.clear();
libhtmlpp::HtmlElement *libhtmlpp::HtmlPage::loadFile(const char* path){
    HtmlString page;
    char tmp[HTML_BLOCKSIZE];
    std::ifstream fs;
    try{
@@ -524,36 +561,35 @@ libhtmlpp::HtmlString *libhtmlpp::HtmlPage::loadFile(const char* path){

    while (fs.good()) {
        fs.read(tmp,HTML_BLOCKSIZE);
        _Page.append(tmp,fs.gcount());
        page.append(tmp,fs.gcount());
    }
    fs.close();
    _CheckHeader();
    return &_Page;
    _RootNode=loadString(page);
    return _RootNode;
}

libhtmlpp::HtmlString *libhtmlpp::HtmlPage::loadString(const std::string src){
    _Page.clear();
    _Page << src;
    _CheckHeader();
    return &_Page;
libhtmlpp::HtmlElement *libhtmlpp::HtmlPage::loadString(const std::string src){
    HtmlString page;
    page << src;
    _RootNode=loadString(page);
    return _RootNode;
}

libhtmlpp::HtmlString *libhtmlpp::HtmlPage::loadString(HtmlString node){
    _Page.clear();
    _Page=node;
    _CheckHeader();
    return &_Page;
libhtmlpp::HtmlElement *libhtmlpp::HtmlPage::loadString(HtmlString node){
    _RootNode=node.parse();
    _CheckHeader(node);
    return _RootNode;
}

void libhtmlpp::HtmlPage::saveFile(const char* path){
}

void libhtmlpp::HtmlPage::_CheckHeader(){
void libhtmlpp::HtmlPage::_CheckHeader(const HtmlString &page){
    const char type[] = { '!','D','O','C','T','Y','P','E' };
    int i = 0;

    while (i < 8) {
        if (_Page[i+1] != type[i]) {
        if (page[i+1] != type[i]) {
            HTMLException excp;
            excp[HTMLException::Critical] << "No Doctype found arborting";
            throw excp;
@@ -563,12 +599,12 @@ void libhtmlpp::HtmlPage::_CheckHeader(){

    do{
        ++i;
    } while (_Page[i] == ' ');
    } while (page[i] == ' ');

    const char typevalue[] = { 'h','t','m','l' };
    size_t tpvl = 4;

    if ((i + tpvl) > _Page.length()) {
    if ((i + tpvl) > page.length()) {
        HTMLException excp;
        excp[HTMLException::Critical] << "Doctype header broken or wrong type";
        throw excp;
@@ -577,7 +613,7 @@ void libhtmlpp::HtmlPage::_CheckHeader(){
    int ii = 0,ie=i+tpvl;

    while (i < ie) {
        if (_Page[i] != typevalue[ii]) {
        if (page[i] != typevalue[ii]) {
            HTMLException excp;
            excp[HTMLException::Critical] << "wrong Doctype";
            throw excp;
+16 −7
Changes for src/html.h: 16 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -52,6 +52,8 @@ namespace libhtmlpp {
            delete _nextElement;
        };

        Element(const Element &el) = delete;

        void insertAfter(Element* el);
        void insertBefore(Element* el);

@@ -71,9 +73,13 @@ namespace libhtmlpp {

    class HtmlElement : public Element {
    public:
        HtmlElement();
        HtmlElement(const char* tag);
        HtmlElement(const HtmlElement &hel) = delete;
        ~HtmlElement();

        Element&     operator=(const Element &hel);

        void         setAttribute(const char* name, const char* value);
        void         setIntAttribute(const char* name, int value);

@@ -106,6 +112,7 @@ namespace libhtmlpp {
        //if text Attributes must be zero
        Attributes*    _firstAttr;
        Attributes*    _lastAttr;

        friend class HtmlString;
        friend class HtmlTable;
        friend void print(Element* el, HtmlElement* parent,std::string &output);
@@ -114,10 +121,12 @@ namespace libhtmlpp {
    class TextElement : public Element{
    public:
        TextElement();
        TextElement(const TextElement &texel) = delete;
        ~TextElement();
    protected:
        std::string   _Text;
        friend class HtmlString;
        friend class HtmlElement;
        friend void print(Element* el, HtmlElement* parent,std::string &output);
    };

@@ -141,7 +150,7 @@ namespace libhtmlpp {
        HtmlString& operator+=(const char* src);
        HtmlString& operator+=(HtmlString& hstring);
        HtmlString& operator=(const char* src);
        const char operator[](size_t pos);
        const char  operator[](size_t pos) const;

        HtmlString& operator<<(const char* src);
        HtmlString& operator<<(std::string src);
@@ -151,7 +160,7 @@ namespace libhtmlpp {
        HtmlString& operator<<(unsigned long src);

        const size_t       size() ;
        const size_t       length();
        const size_t       length() const;
        void               clear();
        HtmlElement*       parse();
    private:
@@ -170,13 +179,13 @@ namespace libhtmlpp {
    public:
        HtmlPage();
        ~HtmlPage();
        HtmlString *loadFile(const char* path);
        HtmlElement *loadFile(const char* path);
        void         saveFile(const char* path);
        HtmlString *loadString(const std::string src);
        HtmlString *loadString(HtmlString node);
        HtmlElement *loadString(const std::string src);
        HtmlElement *loadString(HtmlString node);
    private:
        void       _CheckHeader();
        HtmlString _Page;
        void         _CheckHeader(const HtmlString& page);
        HtmlElement *_RootNode;
    };

    class HtmlTable {
+9 −4
Changes for test/CMakeLists.txt: 9 added lines, 4 removed lines.
Original line number Diff line number Diff line
add_executable(csstest csstest.cpp )
target_link_libraries(csstest htmlpp)

add_test(csstest csstest)
# add_executable(csstest csstest.cpp )
# target_link_libraries(csstest htmlpp)
#
# add_test(csstest csstest)

add_executable(htmlpagetest htmlpagetest.cpp)
target_link_libraries(htmlpagetest htmlpp)
@@ -9,4 +9,9 @@ target_link_libraries(htmlpagetest htmlpp)
add_test(htmlpagetest_right htmlpagetest ${CMAKE_SOURCE_DIR}/test/htmlfiles/right.html)
add_test(htmlpagetest_wrong htmlpagetest ${CMAKE_SOURCE_DIR}/test/htmlfiles/wrong.html)

add_executable(htmlcopytest htmlcopytest.cpp)
target_link_libraries(htmlcopytest htmlpp)

add_test(htmlcopytest_right htmlcopytest ${CMAKE_SOURCE_DIR}/test/htmlfiles/right.html)

set_tests_properties(htmlpagetest_wrong PROPERTIES WILL_FAIL TRUE)
+1 −2
Changes for test/htmlpagetest.cpp: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
@@ -37,10 +37,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
int main(int arc,char *argv[]){
    libhtmlpp::HtmlPage page;
    try{
        libhtmlpp::Element *index=page.loadFile(argv[1])->parse();
        libhtmlpp::Element *index=page.loadFile(argv[1]);
        std::string html;
        libhtmlpp::print(index,nullptr,html);
        delete index;
        std::cout << html << std::endl;
        std::cout << Green << "Test Passed!" << NOCOLOR << std::endl;
    }catch(libhtmlpp::HTMLException &exp){