Changed: Split http parsing from CGroupHTML

--HG--
branch : develop
This commit is contained in:
Nimetu 2019-04-29 15:03:32 +03:00
parent 82f084982a
commit 7f41881be7
4 changed files with 94 additions and 29 deletions

View file

@ -151,7 +151,7 @@ namespace NLGUI
virtual void browse (const char *url); virtual void browse (const char *url);
// parse html string using libxml2 parser // parse html string using libxml2 parser
virtual bool parseHtml(std::string htmlString); bool parseHtml(const std::string &htmlString);
// Refresh // Refresh
void refresh(); void refresh();
@ -346,10 +346,6 @@ namespace NLGUI
// the current request is terminated // the current request is terminated
virtual void requestTerminated(); virtual void requestTerminated();
// libxml2 html parser functions
void htmlElement(xmlNode *node, int element_number);
void htmlWalkDOM(xmlNode *a_node);
// Get Home URL // Get Home URL
virtual std::string home(); virtual std::string home();
@ -815,6 +811,8 @@ namespace NLGUI
void buildHTTPPostParams (SFormFields &formfields); void buildHTTPPostParams (SFormFields &formfields);
private: private:
friend class CHtmlParser;
// decode all HTML entities // decode all HTML entities
static ucstring decodeHTMLEntities(const ucstring &str); static ucstring decodeHTMLEntities(const ucstring &str);

View file

@ -0,0 +1,52 @@
// Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
// 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 <http://www.gnu.org/licenses/>.
#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

View file

@ -48,6 +48,7 @@
#include "nel/gui/http_cache.h" #include "nel/gui/http_cache.h"
#include "nel/gui/http_hsts.h" #include "nel/gui/http_hsts.h"
#include "nel/gui/curl_certificates.h" #include "nel/gui/curl_certificates.h"
#include "nel/gui/html_parser.h"
#include <curl/curl.h> #include <curl/curl.h>
@ -6049,6 +6050,19 @@ namespace NLGUI
return true; 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) int CGroupHTML::luaClearRefresh(CLuaState &ls)
{ {
const char *funcName = "clearRefresh"; const char *funcName = "clearRefresh";
@ -6284,6 +6298,18 @@ namespace NLGUI
browse(url.c_str()); 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) inline bool isDigit(ucchar c, uint base = 16)
{ {

View file

@ -17,13 +17,14 @@
#include "stdpch.h" #include "stdpch.h"
#include "nel/gui/html_parser.h"
#include <string> #include <string>
#include <libxml/HTMLparser.h> #include <libxml/HTMLparser.h>
#include "nel/misc/types_nl.h" #include "nel/misc/types_nl.h"
#include "nel/gui/libwww.h" #include "nel/gui/libwww.h"
#include "nel/gui/group_html.h" #include "nel/gui/group_html.h"
#include "nel/gui/lua_ihm.h"
using namespace std; using namespace std;
using namespace NLMISC; using namespace NLMISC;
@ -35,7 +36,7 @@ using namespace NLMISC;
namespace NLGUI namespace NLGUI
{ {
// *************************************************************************** // ***************************************************************************
void CGroupHTML::htmlElement(xmlNode *node, int element_number) void CHtmlParser::htmlElement(xmlNode *node, int element_number)
{ {
SGML_dtd *HTML_DTD = HTML_dtd (); SGML_dtd *HTML_DTD = HTML_dtd ();
@ -65,30 +66,30 @@ namespace NLGUI
} }
} }
beginElement(element_number, present, value); _GroupHtml->beginElement(element_number, present, value);
} }
else else
{ {
beginUnparsedElement((const char *)(node->name), xmlStrlen(node->name)); _GroupHtml->beginUnparsedElement((const char *)(node->name), xmlStrlen(node->name));
} }
// recursive - text content / child nodes // recursive - text content / child nodes
htmlWalkDOM(node->children); parseNode(node->children);
// closing tag // closing tag
if (element_number < HTML_ELEMENTS) if (element_number < HTML_ELEMENTS)
{ {
endElement(element_number); _GroupHtml->endElement(element_number);
} }
else else
{ {
endUnparsedElement((const char *)(node->name), xmlStrlen(node->name)); _GroupHtml->endUnparsedElement((const char *)(node->name), xmlStrlen(node->name));
} }
} }
// *************************************************************************** // ***************************************************************************
// recursive function to walk html document // recursive function to walk html document
void CGroupHTML::htmlWalkDOM(xmlNode *a_node) void CHtmlParser::parseNode(xmlNode *a_node)
{ {
SGML_dtd *HTML_DTD = HTML_dtd (); SGML_dtd *HTML_DTD = HTML_dtd ();
@ -98,7 +99,7 @@ namespace NLGUI
{ {
if (node->type == XML_TEXT_NODE) if (node->type == XML_TEXT_NODE)
{ {
addText((const char *)(node->content), xmlStrlen(node->content)); _GroupHtml->addText((const char *)(node->content), xmlStrlen(node->content));
} }
else else
if (node->type == XML_ELEMENT_NODE) 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); htmlParserCtxtPtr parser = htmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL, XML_CHAR_ENCODING_UTF8);
if (!parser) if (!parser)
@ -320,7 +321,7 @@ namespace NLGUI
xmlNode *root = xmlDocGetRootElement(parser->myDoc); xmlNode *root = xmlDocGetRootElement(parser->myDoc);
if (root) if (root)
{ {
htmlWalkDOM(root); parseNode(root);
} }
else else
{ {
@ -339,18 +340,6 @@ namespace NLGUI
return success; 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;
} }
}