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

better html4 html5 detection

parent 96a88bd9
Loading
Loading
Loading
Loading
+33 −23
Original line number Diff line number Diff line
@@ -1383,6 +1383,11 @@ void libhtmlpp::HtmlPage::saveFile(libhtmlpp::HtmlElement &html,const std::strin

}

bool libhtmlpp::HtmlPage::isHtml5(){
    return _Html5;
}


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

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

    const char typevalue4[] = {'H','T','M','L',' ','P','U','B','L','I','C'};
    size_t tpvl4 = 11;
    const char typevalue4[] = {'P','U','B','L','I','C'};
    size_t tpvl4 = 6;

    if ((i + tpvl) > page.size()) {
        HTMLException excp;
@@ -1418,30 +1423,35 @@ void libhtmlpp::HtmlPage::_CheckHeader(const HtmlString &page){
        throw excp;
    }

    int ii = 0,ie=i+tpvl,oldpos=i;
    int ii=0;

    while (i < ie) {
        if (page[i] != typevalue[ii]) {
            i=oldpos;
            if ((i + tpvl4) > page.size()) {
    while ( ii < tpvl) {
        if (tolower(page[i]) !=  tolower(doctype[ii])) {
            HTMLException excp;
            excp[HTMLException::Critical] << "Doctype header broken or wrong type";
            throw excp;
        }
            int ii4 = 0,ie4=i+tpvl4;
            while (i < ie4) {
                if(page[i] != typevalue4[ii4]){
                    HTMLException excp;
                    excp[HTMLException::Critical] << "wrong Doctype";
                    throw excp;
        ++i;
    }

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

    ii=0;

    bool html4=true;

    if(i +tpvl4  <page.size()){
        while(ii < tpvl4){
            if (tolower(page[i]) !=  tolower(typevalue4[ii])) {
                html4=false;
            }
        }
        ++i;
        ++ii;
    }

    _Html5=!html4;

}

void libhtmlpp::print(Element* el, HtmlString &output,bool formated) {
+2 −0
Original line number Diff line number Diff line
@@ -311,8 +311,10 @@ namespace libhtmlpp {
        void         loadString(libhtmlpp::HtmlElement &html,const std::string &src);
        void         loadString(libhtmlpp::HtmlElement &html,const HtmlString &node);
        void         loadString(libhtmlpp::HtmlElement &html,const HtmlString *node);
        bool         isHtml5();
    private:
        void         _CheckHeader(const HtmlString& page);
        bool         _Html5 = true;
    };

    class HtmlTable {
+5 −0
Original line number Diff line number Diff line
@@ -43,6 +43,11 @@ int main(int arc,char *argv[]){
        libhtmlpp::print(&index,html,true);
        std::cout << html.str() << std::endl;
        std::cout << Green << "Test Passed!" << NOCOLOR << std::endl;
        if(page.isHtml5()){
            std::cout << Green << "Is Html 5 Document" << NOCOLOR << std::endl;
        }else{
            std::cout << Green << "Is Html 4 Document" << NOCOLOR << std::endl;
        }
    }catch(libhtmlpp::HTMLException &exp){
        std::cout << exp.what() << std::endl;
        std::cout << Red << "Test not Passed!" << NOCOLOR << std::endl;