From 7f41881be7b05bddc2a050c860fd845c3831de9e Mon Sep 17 00:00:00 2001 From: Nimetu Date: Mon, 29 Apr 2019 15:03:32 +0300 Subject: [PATCH] Changed: Split http parsing from CGroupHTML --HG-- branch : develop --- code/nel/include/nel/gui/group_html.h | 8 ++- code/nel/include/nel/gui/html_parser.h | 52 +++++++++++++++++++ code/nel/src/gui/group_html.cpp | 26 ++++++++++ ...{group_html_parser.cpp => html_parser.cpp} | 37 +++++-------- 4 files changed, 94 insertions(+), 29 deletions(-) create mode 100644 code/nel/include/nel/gui/html_parser.h rename code/nel/src/gui/{group_html_parser.cpp => html_parser.cpp} (89%) diff --git a/code/nel/include/nel/gui/group_html.h b/code/nel/include/nel/gui/group_html.h index 55daa3c52..b21841f8c 100644 --- a/code/nel/include/nel/gui/group_html.h +++ b/code/nel/include/nel/gui/group_html.h @@ -151,7 +151,7 @@ namespace NLGUI virtual void browse (const char *url); // parse html string using libxml2 parser - virtual bool parseHtml(std::string htmlString); + bool parseHtml(const std::string &htmlString); // Refresh void refresh(); @@ -346,10 +346,6 @@ namespace NLGUI // the current request is terminated virtual void requestTerminated(); - // libxml2 html parser functions - void htmlElement(xmlNode *node, int element_number); - void htmlWalkDOM(xmlNode *a_node); - // Get Home URL virtual std::string home(); @@ -815,6 +811,8 @@ namespace NLGUI void buildHTTPPostParams (SFormFields &formfields); private: + friend class CHtmlParser; + // decode all HTML entities static ucstring decodeHTMLEntities(const ucstring &str); diff --git a/code/nel/include/nel/gui/html_parser.h b/code/nel/include/nel/gui/html_parser.h new file mode 100644 index 000000000..132c4ac88 --- /dev/null +++ b/code/nel/include/nel/gui/html_parser.h @@ -0,0 +1,52 @@ +// Ryzom - MMORPG Framework +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +#ifndef CL_HTML_PARSER_H +#define CL_HTML_PARSER_H + +#include "nel/misc/types_nl.h" + +namespace NLGUI +{ + class CGroupHTML; + + /** + * \brief HTML parsing + * \date 2019-03-15 10:50 GMT + * \author Meelis Mägi (Nimetu) + */ + class CHtmlParser + { + public: + CHtmlParser(CGroupHTML *group) : _GroupHtml(group) + {} + + bool parseHtml(std::string htmlString); + + private: + // libxml2 html parser functions + void htmlElement(xmlNode *node, int element_number); + void parseNode(xmlNode *a_node); + + private: + + CGroupHTML *_GroupHtml; + }; + +} + +#endif + diff --git a/code/nel/src/gui/group_html.cpp b/code/nel/src/gui/group_html.cpp index 0bdebf865..36d0eab7f 100644 --- a/code/nel/src/gui/group_html.cpp +++ b/code/nel/src/gui/group_html.cpp @@ -48,6 +48,7 @@ #include "nel/gui/http_cache.h" #include "nel/gui/http_hsts.h" #include "nel/gui/curl_certificates.h" +#include "nel/gui/html_parser.h" #include @@ -6049,6 +6050,19 @@ namespace NLGUI return true; } + // *************************************************************************** + int CGroupHTML::luaParseHtml(CLuaState &ls) + { + const char *funcName = "parseHtml"; + CLuaIHM::checkArgCount(ls, funcName, 1); + CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); + std::string html = ls.toString(1); + + parseHtml(html); + + return 0; + } + int CGroupHTML::luaClearRefresh(CLuaState &ls) { const char *funcName = "clearRefresh"; @@ -6284,6 +6298,18 @@ namespace NLGUI browse(url.c_str()); } + // *************************************************************************** + bool CGroupHTML::parseHtml(const std::string &htmlString) + { + CHtmlParser html(this); + + bool result = html.parseHtml(htmlString); + if (result) + _DocumentHtml = htmlString; + + return result; + } + // *************************************************************************** inline bool isDigit(ucchar c, uint base = 16) { diff --git a/code/nel/src/gui/group_html_parser.cpp b/code/nel/src/gui/html_parser.cpp similarity index 89% rename from code/nel/src/gui/group_html_parser.cpp rename to code/nel/src/gui/html_parser.cpp index 0069fd77d..8470d0524 100644 --- a/code/nel/src/gui/group_html_parser.cpp +++ b/code/nel/src/gui/html_parser.cpp @@ -17,13 +17,14 @@ #include "stdpch.h" +#include "nel/gui/html_parser.h" + #include #include #include "nel/misc/types_nl.h" #include "nel/gui/libwww.h" #include "nel/gui/group_html.h" -#include "nel/gui/lua_ihm.h" using namespace std; using namespace NLMISC; @@ -35,7 +36,7 @@ using namespace NLMISC; namespace NLGUI { // *************************************************************************** - void CGroupHTML::htmlElement(xmlNode *node, int element_number) + void CHtmlParser::htmlElement(xmlNode *node, int element_number) { SGML_dtd *HTML_DTD = HTML_dtd (); @@ -65,30 +66,30 @@ namespace NLGUI } } - beginElement(element_number, present, value); + _GroupHtml->beginElement(element_number, present, value); } else { - beginUnparsedElement((const char *)(node->name), xmlStrlen(node->name)); + _GroupHtml->beginUnparsedElement((const char *)(node->name), xmlStrlen(node->name)); } // recursive - text content / child nodes - htmlWalkDOM(node->children); + parseNode(node->children); // closing tag if (element_number < HTML_ELEMENTS) { - endElement(element_number); + _GroupHtml->endElement(element_number); } else { - endUnparsedElement((const char *)(node->name), xmlStrlen(node->name)); + _GroupHtml->endUnparsedElement((const char *)(node->name), xmlStrlen(node->name)); } } // *************************************************************************** // recursive function to walk html document - void CGroupHTML::htmlWalkDOM(xmlNode *a_node) + void CHtmlParser::parseNode(xmlNode *a_node) { SGML_dtd *HTML_DTD = HTML_dtd (); @@ -98,7 +99,7 @@ namespace NLGUI { if (node->type == XML_TEXT_NODE) { - addText((const char *)(node->content), xmlStrlen(node->content)); + _GroupHtml->addText((const char *)(node->content), xmlStrlen(node->content)); } else if (node->type == XML_ELEMENT_NODE) @@ -297,7 +298,7 @@ namespace NLGUI } // *************************************************************************** - bool CGroupHTML::parseHtml(std::string htmlString) + bool CHtmlParser::parseHtml(std::string htmlString) { htmlParserCtxtPtr parser = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL, XML_CHAR_ENCODING_UTF8); if (!parser) @@ -320,7 +321,7 @@ namespace NLGUI xmlNode *root = xmlDocGetRootElement(parser->myDoc); if (root) { - htmlWalkDOM(root); + parseNode(root); } else { @@ -339,18 +340,6 @@ namespace NLGUI return success; } - // *************************************************************************** - int CGroupHTML::luaParseHtml(CLuaState &ls) - { - const char *funcName = "parseHtml"; - CLuaIHM::checkArgCount(ls, funcName, 1); - CLuaIHM::checkArgType(ls, funcName, 1, LUA_TSTRING); - std::string html = ls.toString(1); - - parseHtml(html); - - return 0; - } - } +