1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-01-03 14:57:44 -05:00

[js] switch to libxml++ . Begining.

This commit is contained in:
Witold Filipczyk 2021-05-09 19:56:00 +02:00
parent 471c9282ee
commit d71ef9e57c
4 changed files with 49 additions and 40 deletions

View File

@ -88,6 +88,7 @@ conf_data.set('CONFIG_LIBEVENT', get_option('libevent'))
conf_data.set('CONFIG_X', get_option('x')) conf_data.set('CONFIG_X', get_option('x'))
conf_data.set('CONFIG_LIBDOM', get_option('libdom')) conf_data.set('CONFIG_LIBDOM', get_option('libdom'))
conf_data.set('CONFIG_HTMLCXX', get_option('htmlcxx')) conf_data.set('CONFIG_HTMLCXX', get_option('htmlcxx'))
conf_data.set('CONFIG_XML', get_option('xml'))
#CONFIG_BOOKMARKS=true #CONFIG_BOOKMARKS=true
#CONFIG_XBEL_BOOKMARKS=true #CONFIG_XBEL_BOOKMARKS=true
@ -301,6 +302,14 @@ if conf_data.get('CONFIG_HTMLCXX')
deps += htmlcxxdeps deps += htmlcxxdeps
endif endif
if conf_data.get('CONFIG_XML')
xml2deps = dependency('libxml2')
deps += xml2deps
xmlplusplusdeps = dependency('libxml++-5.0')
deps += xmlplusplusdeps
endif
if conf_data.get('CONFIG_SCRIPTING_PYTHON') if conf_data.get('CONFIG_SCRIPTING_PYTHON')
python3deps = dependency('python3-embed') python3deps = dependency('python3-embed')
deps += python3deps deps += python3deps

View File

@ -62,3 +62,4 @@ option('libevent', type: 'boolean', value: false, description: 'compile with lib
option('x', type: 'boolean', value: false, description: 'use the X Window System') option('x', type: 'boolean', value: false, description: 'use the X Window System')
option('libdom', type: 'boolean', value: false, description: 'libdom') option('libdom', type: 'boolean', value: false, description: 'libdom')
option('htmlcxx', type: 'boolean', value: false, description: 'htmlcxx') option('htmlcxx', type: 'boolean', value: false, description: 'htmlcxx')
option('xml', type: 'boolean', value: false, description: 'libxml++')

View File

@ -49,8 +49,9 @@
#include "viewer/text/link.h" #include "viewer/text/link.h"
#include "viewer/text/vs.h" #include "viewer/text/vs.h"
#include <htmlcxx/html/ParserDom.h> #include <libxml/tree.h>
using namespace htmlcxx; #include <libxml/HTMLparser.h>
#include <libxml++/libxml++.h>
#include <iostream> #include <iostream>
@ -698,12 +699,14 @@ document_parse(struct document *document)
add_bytes_to_string(&str, f->data, f->length); add_bytes_to_string(&str, f->data, f->length);
HTML::ParserDom parser; // Parse HTML and create a DOM tree
tree<HTML::Node> *dom = new tree<HTML::Node>; xmlDoc* doc = htmlReadDoc((xmlChar*)str.source, NULL, NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);
*dom = parser.parseTree(str.source); // Encapsulate raw libxml document in a libxml++ wrapper
xmlNode* r = xmlDocGetRootElement(doc);
xmlpp::Element* root = new xmlpp::Element(r);
done_string(&str); done_string(&str);
return (void *)dom; return (void *)root;
} }
@ -731,9 +734,7 @@ document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true; return true;
} }
tree<HTML::Node> *dom = document->dom; xmlpp::Element* root = (xmlpp::Element *)document->dom;
tree<HTML::Node>::iterator it = dom->begin();
tree<HTML::Node>::iterator end = dom->end();
struct string idstr; struct string idstr;
@ -741,21 +742,22 @@ document_getElementById(JSContext *ctx, unsigned int argc, JS::Value *vp)
jshandle_value_to_char_string(&idstr, ctx, &args[0]); jshandle_value_to_char_string(&idstr, ctx, &args[0]);
std::string id = idstr.source; std::string id = idstr.source;
JSObject *elem = nullptr; std::string xpath = "//*[@id=\"";
xpath += id;
for (; it != end; ++it) { xpath += "\"]";
if (it->isTag()) {
it->parseAttributes();
if (it->attribute("id").first && it->attribute("id").second == id) {
tree<HTML::Node> *node = new tree<HTML::Node>;
*node = *it;
elem = getElement(ctx, node);
break;
}
}
}
done_string(&idstr); done_string(&idstr);
auto elements = root->find(xpath);
if (elements.size() == 0) {
args.rval().setNull();
return true;
}
auto node = elements[0];
JSObject *elem = getElement(ctx, node);
if (elem) { if (elem) {
args.rval().setObject(*elem); args.rval().setObject(*elem);
} else { } else {

View File

@ -46,12 +46,12 @@
#include "viewer/text/link.h" #include "viewer/text/link.h"
#include "viewer/text/vs.h" #include "viewer/text/vs.h"
#include <libxml++/libxml++.h>
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
using namespace htmlcxx;
static bool element_get_property_childElementCount(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool element_get_property_childElementCount(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool element_get_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool element_set_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -84,16 +84,16 @@ JSClass element_class = {
}; };
JSPropertySpec element_props[] = { JSPropertySpec element_props[] = {
JS_PSG("childElementCount", element_get_property_childElementCount, JSPROP_ENUMERATE), // JS_PSG("childElementCount", element_get_property_childElementCount, JSPROP_ENUMERATE),
JS_PSGS("className", element_get_property_className, element_set_property_className, JSPROP_ENUMERATE), // JS_PSGS("className", element_get_property_className, element_set_property_className, JSPROP_ENUMERATE),
JS_PSGS("dir", element_get_property_dir, element_set_property_dir, JSPROP_ENUMERATE), // JS_PSGS("dir", element_get_property_dir, element_set_property_dir, JSPROP_ENUMERATE),
JS_PSGS("id", element_get_property_id, element_set_property_id, JSPROP_ENUMERATE), JS_PSGS("id", element_get_property_id, element_set_property_id, JSPROP_ENUMERATE),
JS_PSGS("innerHTML", element_get_property_innerHtml, element_set_property_innerHtml, JSPROP_ENUMERATE), // JS_PSGS("innerHTML", element_get_property_innerHtml, element_set_property_innerHtml, JSPROP_ENUMERATE),
JS_PSGS("lang", element_get_property_lang, element_set_property_lang, JSPROP_ENUMERATE), // JS_PSGS("lang", element_get_property_lang, element_set_property_lang, JSPROP_ENUMERATE),
JS_PSGS("outerHTML", element_get_property_outerHtml, element_set_property_outerHtml, JSPROP_ENUMERATE), // JS_PSGS("outerHTML", element_get_property_outerHtml, element_set_property_outerHtml, JSPROP_ENUMERATE),
JS_PSG("tagName", element_get_property_tagName, JSPROP_ENUMERATE), // JS_PSG("tagName", element_get_property_tagName, JSPROP_ENUMERATE),
JS_PSGS("textContent", element_get_property_textContent, element_set_property_textContent, JSPROP_ENUMERATE), // JS_PSGS("textContent", element_get_property_textContent, element_set_property_textContent, JSPROP_ENUMERATE),
JS_PSGS("title", element_get_property_title, element_set_property_title, JSPROP_ENUMERATE), // JS_PSGS("title", element_get_property_title, element_set_property_title, JSPROP_ENUMERATE),
JS_PS_END JS_PS_END
}; };
@ -268,17 +268,14 @@ element_get_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp)
return false; return false;
} }
tree<HTML::Node> *el = JS_GetPrivate(hobj); xmlpp::Element *el = JS_GetPrivate(hobj);
if (!el) { if (!el) {
args.rval().setNull(); args.rval().setNull();
return true; return true;
} }
tree<HTML::Node>::iterator it = el->begin(); std::string v = el->get_attribute_value("id");
it->parseAttributes();
std::string v = it->attribute("id").second;
args.rval().setString(JS_NewStringCopyZ(ctx, v.c_str())); args.rval().setString(JS_NewStringCopyZ(ctx, v.c_str()));
return true; return true;
@ -877,8 +874,8 @@ static bool element_hasAttribute(JSContext *ctx, unsigned int argc, JS::Value *r
static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval); static bool element_hasAttributes(JSContext *ctx, unsigned int argc, JS::Value *rval);
const spidermonkeyFunctionSpec element_funcs[] = { const spidermonkeyFunctionSpec element_funcs[] = {
{ "hasAttribute", element_hasAttribute, 1 }, // { "hasAttribute", element_hasAttribute, 1 },
{ "hasAttributes", element_hasAttributes, 0 }, // { "hasAttributes", element_hasAttributes, 0 },
{ NULL } { NULL }
}; };