Added bindings for HTMLParser.

This commit is contained in:
Relintai 2022-07-19 20:21:28 +02:00
parent d786fb5722
commit aa85a21418
2 changed files with 113 additions and 11 deletions

View File

@ -1,5 +1,6 @@
#include "html_parser.h" #include "html_parser.h"
#include "core/class_db.h"
#include "core/error_macros.h" #include "core/error_macros.h"
#include "core/log/logger.h" #include "core/log/logger.h"
@ -30,7 +31,11 @@ bool HTMLParserAttribute::match_attrib(const String &attrib) {
bool HTMLParserAttribute::match_data(const String &d) { bool HTMLParserAttribute::match_data(const String &d) {
return _data == d; return _data == d;
} }
bool HTMLParserAttribute::match_data(const Vector<String> &d) { bool HTMLParserAttribute::match_all_data(const Vector<String> &d) {
// todo
return false;
}
bool match_all_data_bind(const PoolStringArray &d) {
// todo // todo
return false; return false;
} }
@ -61,6 +66,28 @@ HTMLParserAttribute::HTMLParserAttribute() {
HTMLParserAttribute::~HTMLParserAttribute() { HTMLParserAttribute::~HTMLParserAttribute() {
} }
void HTMLParserAttribute::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_attribute"), &HTMLParserAttribute::get_attribute);
ClassDB::bind_method(D_METHOD("set_attribute", "val"), &HTMLParserAttribute::set_attribute);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "attribute"), "set_attribute", "get_attribute");
ClassDB::bind_method(D_METHOD("get_data"), &HTMLParserAttribute::get_data);
ClassDB::bind_method(D_METHOD("set_data", "val"), &HTMLParserAttribute::set_data);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "data"), "set_data", "get_data");
ClassDB::bind_method(D_METHOD("get_single"), &HTMLParserAttribute::get_single);
ClassDB::bind_method(D_METHOD("set_single", "val"), &HTMLParserAttribute::set_single);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "single"), "set_single", "get_single");
ClassDB::bind_method(D_METHOD("match_attrib", "attrib"), &HTMLParserAttribute::match_attrib);
ClassDB::bind_method(D_METHOD("match_data", "data"), &HTMLParserAttribute::match_data);
ClassDB::bind_method(D_METHOD("match_all_data", "data"), &HTMLParserAttribute::match_all_data_bind);
ClassDB::bind_method(D_METHOD("contains_data", "data"), &HTMLParserAttribute::contains_data);
ClassDB::bind_method(D_METHOD("convert_to_string"), &HTMLParserAttribute::convert_to_string);
ClassDB::bind_method(D_METHOD("print"), &HTMLParserAttribute::print);
};
void HTMLParserTag::add_child_tag(const Ref<HTMLParserTag> &tag) { void HTMLParserTag::add_child_tag(const Ref<HTMLParserTag> &tag) {
_tags.push_back(tag); _tags.push_back(tag);
} }
@ -149,15 +176,15 @@ Ref<HTMLParserTag> HTMLParserTag::get_first(const String &t) {
return Ref<HTMLParserTag>(); return Ref<HTMLParserTag>();
} }
Ref<HTMLParserTag> HTMLParserTag::get_first(const String &t, const String &attrib, const String &val) { Ref<HTMLParserTag> HTMLParserTag::get_firstc(const String &t, const String &attrib, const String &val) {
if (_tag == t) { if (_tag == t) {
if (has_attribute(attrib, val)) { if (has_attributec(attrib, val)) {
return Ref<HTMLParserTag>(this); return Ref<HTMLParserTag>(this);
} }
} }
for (int i = 0; i < _tags.size(); ++i) { for (int i = 0; i < _tags.size(); ++i) {
Ref<HTMLParserTag> ht = _tags.write[i]->get_first(t, attrib, val); Ref<HTMLParserTag> ht = _tags.write[i]->get_firstc(t, attrib, val);
if (ht.is_valid()) { if (ht.is_valid()) {
return ht; return ht;
@ -201,7 +228,7 @@ bool HTMLParserTag::has_attribute(const String &attrib) {
return false; return false;
} }
Ref<HTMLParserAttribute> HTMLParserTag::get_attribute(const String &attrib, const String &contains_val) { Ref<HTMLParserAttribute> HTMLParserTag::get_attributec(const String &attrib, const String &contains_val) {
for (int i = 0; i < _attributes.size(); ++i) { for (int i = 0; i < _attributes.size(); ++i) {
Ref<HTMLParserAttribute> a = _attributes[i]; Ref<HTMLParserAttribute> a = _attributes[i];
@ -213,7 +240,7 @@ Ref<HTMLParserAttribute> HTMLParserTag::get_attribute(const String &attrib, cons
return Ref<HTMLParserAttribute>(); return Ref<HTMLParserAttribute>();
} }
bool HTMLParserTag::has_attribute(const String &attrib, const String &contains_val) { bool HTMLParserTag::has_attributec(const String &attrib, const String &contains_val) {
for (int i = 0; i < _attributes.size(); ++i) { for (int i = 0; i < _attributes.size(); ++i) {
Ref<HTMLParserAttribute> a = _attributes[i]; Ref<HTMLParserAttribute> a = _attributes[i];
@ -502,6 +529,65 @@ HTMLParserTag::~HTMLParserTag() {
_attributes.clear(); _attributes.clear();
} }
void HTMLParserTag::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_type"), &HTMLParserTag::get_type);
ClassDB::bind_method(D_METHOD("set_type", "val"), &HTMLParserTag::set_type);
ADD_PROPERTY(PropertyInfo(Variant::INT, "type"), "set_type", "get_type");
ClassDB::bind_method(D_METHOD("get_tag"), &HTMLParserTag::get_tag);
ClassDB::bind_method(D_METHOD("set_tag", "val"), &HTMLParserTag::set_tag);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "tag"), "set_tag", "get_tag");
ClassDB::bind_method(D_METHOD("get_data"), &HTMLParserTag::get_data);
ClassDB::bind_method(D_METHOD("set_data", "val"), &HTMLParserTag::set_data);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "data"), "set_data", "get_data");
ClassDB::bind_method(D_METHOD("add_child_tag", "tag"), &HTMLParserTag::add_child_tag);
ClassDB::bind_method(D_METHOD("remote_child_tag", "index"), &HTMLParserTag::remote_child_tag);
ClassDB::bind_method(D_METHOD("get_child_tag", "index"), &HTMLParserTag::get_child_tag);
ClassDB::bind_method(D_METHOD("get_child_tag_count"), &HTMLParserTag::get_child_tag_count);
ClassDB::bind_method(D_METHOD("clear_child_tags"), &HTMLParserTag::clear_child_tags);
ClassDB::bind_method(D_METHOD("get_child_tags"), &HTMLParserTag::get_child_tags);
ClassDB::bind_method(D_METHOD("set_child_tags", "val"), &HTMLParserTag::set_child_tags);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "child_tags", PROPERTY_HINT_NONE, "17/17:HTMLParserTag", PROPERTY_USAGE_DEFAULT, "HTMLParserTag"), "set_child_tags", "get_child_tags");
ClassDB::bind_method(D_METHOD("add_child_attribute", "tag"), &HTMLParserTag::add_child_attribute);
ClassDB::bind_method(D_METHOD("remote_child_attribute", "index"), &HTMLParserTag::remote_child_attribute);
ClassDB::bind_method(D_METHOD("get_child_attribute", "index"), &HTMLParserTag::get_child_attribute);
ClassDB::bind_method(D_METHOD("get_child_attribute_count"), &HTMLParserTag::get_child_attribute_count);
ClassDB::bind_method(D_METHOD("clear_child_attributes"), &HTMLParserTag::clear_child_attributes);
ClassDB::bind_method(D_METHOD("get_attributes"), &HTMLParserTag::get_attributes);
ClassDB::bind_method(D_METHOD("set_attributes", "val"), &HTMLParserTag::set_attributes);
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "attributes", PROPERTY_HINT_NONE, "17/17:HTMLParserAttribute", PROPERTY_USAGE_DEFAULT, "HTMLParserAttribute"), "set_attributes", "get_attributes");
ClassDB::bind_method(D_METHOD("get_first", "t"), &HTMLParserTag::get_first);
ClassDB::bind_method(D_METHOD("get_firstc", "t", "attrib", "val"), &HTMLParserTag::get_firstc);
ClassDB::bind_method(D_METHOD("get_attribute_value", "attrib"), &HTMLParserTag::get_attribute_value);
ClassDB::bind_method(D_METHOD("get_attribute", "attrib"), &HTMLParserTag::get_attribute);
ClassDB::bind_method(D_METHOD("has_attribute", "attrib"), &HTMLParserTag::has_attribute);
ClassDB::bind_method(D_METHOD("get_attributec", "attrib", "contains_val"), &HTMLParserTag::get_attributec);
ClassDB::bind_method(D_METHOD("has_attributec", "attrib", "contains_val"), &HTMLParserTag::has_attributec);
ClassDB::bind_method(D_METHOD("process"), &HTMLParserTag::process);
ClassDB::bind_method(D_METHOD("parse_args", "args"), &HTMLParserTag::parse_args);
ClassDB::bind_method(D_METHOD("convert_to_string", "level"), &HTMLParserTag::convert_to_string, 0);
ClassDB::bind_method(D_METHOD("print"), &HTMLParserTag::print);
BIND_ENUM_CONSTANT(HTML_PARSER_TAG_TYPE_NONE);
BIND_ENUM_CONSTANT(HTML_PARSER_TAG_TYPE_OPENING_TAG);
BIND_ENUM_CONSTANT(HTML_PARSER_TAG_TYPE_CLOSING_TAG);
BIND_ENUM_CONSTANT(HTML_PARSER_TAG_TYPE_SELF_CLOSING_TAG);
BIND_ENUM_CONSTANT(HTML_PARSER_TAG_TYPE_COMMENT);
BIND_ENUM_CONSTANT(HTML_PARSER_TAG_TYPE_DOCTYPE);
BIND_ENUM_CONSTANT(HTML_PARSER_TAG_TYPE_CONTENT);
};
Ref<HTMLParserTag> HTMLParser::get_root() { Ref<HTMLParserTag> HTMLParser::get_root() {
return _root; return _root;
} }
@ -755,3 +841,10 @@ HTMLParser::HTMLParser() {
HTMLParser::~HTMLParser() { HTMLParser::~HTMLParser() {
_root.unref(); _root.unref();
} }
void HTMLParser::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_root"), &HTMLParser::get_root);
ClassDB::bind_method(D_METHOD("parse", "data"), &HTMLParser::parse);
ClassDB::bind_method(D_METHOD("convert_to_string"), &HTMLParser::convert_to_string);
ClassDB::bind_method(D_METHOD("print"), &HTMLParser::print);
};

View File

@ -2,6 +2,7 @@
#define HTML_PARSER_H #define HTML_PARSER_H
#include "core/ustring.h" #include "core/ustring.h"
#include "core/variant.h"
#include "core/vector.h" #include "core/vector.h"
#include "core/reference.h" #include "core/reference.h"
@ -12,7 +13,6 @@ class HTMLParserAttribute : public Reference {
public: public:
String get_attribute(); String get_attribute();
void set_attribute(const String &val); void set_attribute(const String &val);
String get_data(); String get_data();
void set_data(const String &val); void set_data(const String &val);
@ -21,7 +21,8 @@ public:
bool match_attrib(const String &attrib); bool match_attrib(const String &attrib);
bool match_data(const String &d); bool match_data(const String &d);
bool match_data(const Vector<String> &d); bool match_all_data(const Vector<String> &d);
bool match_all_data_bind(const PoolStringArray &d);
bool contains_data(const String &d); bool contains_data(const String &d);
String convert_to_string() const; String convert_to_string() const;
@ -31,6 +32,8 @@ public:
virtual ~HTMLParserAttribute(); virtual ~HTMLParserAttribute();
protected: protected:
static void _bind_methods();
String _attribute; String _attribute;
String _data; String _data;
bool _single; bool _single;
@ -78,15 +81,15 @@ public:
void set_attributes(const Vector<Variant> &val); void set_attributes(const Vector<Variant> &val);
Ref<HTMLParserTag> get_first(const String &t); Ref<HTMLParserTag> get_first(const String &t);
Ref<HTMLParserTag> get_first(const String &t, const String &attrib, const String &val); Ref<HTMLParserTag> get_firstc(const String &t, const String &attrib, const String &val);
String get_attribute_value(const String &attrib); String get_attribute_value(const String &attrib);
Ref<HTMLParserAttribute> get_attribute(const String &attrib); Ref<HTMLParserAttribute> get_attribute(const String &attrib);
bool has_attribute(const String &attrib); bool has_attribute(const String &attrib);
Ref<HTMLParserAttribute> get_attribute(const String &attrib, const String &contains_val); Ref<HTMLParserAttribute> get_attributec(const String &attrib, const String &contains_val);
bool has_attribute(const String &attrib, const String &contains_val); bool has_attributec(const String &attrib, const String &contains_val);
void process(); void process();
void parse_args(const String &args); void parse_args(const String &args);
@ -98,6 +101,8 @@ public:
virtual ~HTMLParserTag(); virtual ~HTMLParserTag();
protected: protected:
static void _bind_methods();
int _type; int _type;
String _tag; String _tag;
@ -123,7 +128,11 @@ public:
virtual ~HTMLParser(); virtual ~HTMLParser();
protected: protected:
static void _bind_methods();
Ref<HTMLParserTag> _root; Ref<HTMLParserTag> _root;
}; };
VARIANT_ENUM_CAST(HTMLParserTag::HTMLParserTagType);
#endif #endif