Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

basic implementation for css counters #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 14 include/litehtml/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace litehtml

virtual void select_all(const css_selector& selector, elements_list& res);
element::ptr _add_before_after(int type, const style& style);

private:
std::map<string_id, int> m_counter_values;

public:
explicit element(const std::shared_ptr<document>& doc);
virtual ~element() = default;
Expand Down Expand Up @@ -142,6 +146,16 @@ namespace litehtml
{
return _add_before_after(1, style);
}

string get_counter_value(const string& counter_name);
string get_counters_value(const string_vector& parameters);
void increment_counter(const string_id& counter_name_id, const int increment = 1);
void reset_counter(const string_id& counter_name_id, const int value = 0);

private:
std::vector<element::ptr> get_siblings_before() const;
bool find_counter(const string_id& counter_name_id, std::map<string_id, int>::iterator& map_iterator);
void parse_counter_tokens(const string_vector& tokens, const int default_value, std::function<void(const string_id&, const int)> handler) const;
};

//////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 4 additions & 2 deletions 6 include/litehtml/html.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace litehtml
{
void trim(string &s);
void trim(string &s, const string& chars_to_trim = " \n\r\t");
void lcase(string &s);
int value_index(const string& val, const string& strings, int defValue = -1, char delim = ';');
string index_value(int index, const string& strings, char delim = ';');
Expand All @@ -36,7 +36,9 @@ namespace litehtml

int t_strcasecmp(const char *s1, const char *s2);
int t_strncasecmp(const char *s1, const char *s2, size_t n);


bool is_number(const string& string, const bool allow_dot = 1);

inline int t_isdigit(int c)
{
return (c >= '0' && c <= '9');
Expand Down
4 changes: 4 additions & 0 deletions 4 include/litehtml/html_tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ namespace litehtml
string get_list_marker_text(int index);
element::ptr get_element_before(const style& style, bool create);
element::ptr get_element_after(const style& style, bool create);

private:
void handle_counter_properties();

};

/************************************************************************/
Expand Down
5 changes: 4 additions & 1 deletion 5 include/litehtml/string_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ STRING_ID(
_flex_shrink_,
_flex_basis_,

_caption_side_,
_counter_reset_,
_counter_increment_,

_caption_side_,
);
#undef STRING_ID
extern const string_id empty_id; // _id("")
Expand Down
13 changes: 11 additions & 2 deletions 13 src/el_before_after.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void litehtml::el_before_after_base::add_text( const string& txt )

void litehtml::el_before_after_base::add_function( const string& fnc, const string& params )
{
int idx = value_index(fnc, "attr;counter;url");
int idx = value_index(fnc, "attr;counter;counters;url");
switch(idx)
{
// attr
Expand All @@ -155,9 +155,18 @@ void litehtml::el_before_after_base::add_function( const string& fnc, const stri
break;
// counter
case 1:
add_text(get_counter_value(params));
break;
// url
// counters
case 2:
{
string_vector tokens;
split_string(params, tokens, ",");
add_text(get_counters_value(tokens));
}
break;
// url
case 3:
{
string p_url = params;
trim(p_url);
Expand Down
116 changes: 116 additions & 0 deletions 116 src/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,122 @@ bool element::is_block_formatting_context() const
return false;
}

litehtml::string litehtml::element::get_counter_value(const string& counter_name)
{
std::map<string_id, int>::iterator i;
if (find_counter(_id(counter_name), i))
{
return std::to_string(i->second);
}
return "0";
}

string litehtml::element::get_counters_value(const string_vector& parameters)
{
string result = "";
if (parameters.size() >= 2) {
const string_id counter_name_id = _id(parameters[0]);
string delims = parameters[1];
litehtml::trim(delims, "\"'");

string_vector values;

element::ptr current = shared_from_this();
while (current != nullptr)
{
auto map_iterator = current->m_counter_values.find(counter_name_id);
if (map_iterator != current->m_counter_values.end()) {
values.push_back(std::to_string(map_iterator->second));
}
current = current->parent();
}
if (values.empty()) {
// if no counter is found, instanciate one with value '0'
shared_from_this()->m_counter_values[counter_name_id] = 0;
result = "0";
}
else {
std::reverse(values.begin(), values.end());
litehtml::join_string(result, values, delims);
}
}
return result;
}


bool litehtml::element::find_counter(const string_id& counter_name_id, std::map<string_id, int>::iterator& map_iterator) {
element::ptr current = shared_from_this();

while (current != nullptr)
{
map_iterator = current->m_counter_values.find(counter_name_id);
if (map_iterator != current->m_counter_values.end()) {
return true;
}

// on each level, search previous siblings too
std::vector<element::ptr> siblings = current->get_siblings_before();
std::reverse(siblings.begin(), siblings.end());
for (const element::ptr& sibling : siblings) {
map_iterator = sibling->m_counter_values.find(counter_name_id);
if (map_iterator != sibling->m_counter_values.end()) {
return true;
}
}
current = current->parent();
}

return false;
}

std::vector<element::ptr> litehtml::element::get_siblings_before() const
{
std::vector<element::ptr> siblings;
if (parent() != nullptr) {
for (const element::ptr& sybling : parent()->children()) {
if (sybling == shared_from_this()) {
break;
}
siblings.push_back(sybling);
}
}
return siblings;
}


void litehtml::element::parse_counter_tokens(const string_vector& tokens, const int default_value, std::function<void(const string_id&, const int)> handler) const {
int pos = 0;
while (pos < tokens.size()) {
string name = tokens[pos];
int value = default_value;
if (pos < tokens.size() - 1 && litehtml::is_number(tokens[pos + 1], 0)) {
value = atoi(tokens[pos + 1].c_str());
pos += 2;
}
else {
pos += 1;
}
handler(_id(name), value);
}
}

void litehtml::element::increment_counter(const string_id& counter_name_id, const int increment)
{
std::map<string_id, int>::iterator i;
if (find_counter(counter_name_id, i)) {
i->second = i->second + increment;
}
else {
// if counter is not found, initialize one on this element
m_counter_values[counter_name_id] = increment;
}
}

void litehtml::element::reset_counter(const string_id& counter_name_id, const int value)
{
m_counter_values[counter_name_id] = value;
}

const background* element::get_background(bool own_only) LITEHTML_RETURN_FUNC(nullptr)
void element::add_style( const style& style) LITEHTML_EMPTY_FUNC
void element::select_all(const css_selector& selector, elements_list& res) LITEHTML_EMPTY_FUNC
Expand Down
17 changes: 14 additions & 3 deletions 17 src/html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include "types.h"
#include "utf8_strings.h"

void litehtml::trim(string &s)
void litehtml::trim(string &s, const string& chars_to_trim)
{
string::size_type pos = s.find_first_not_of(" \n\r\t");
string::size_type pos = s.find_first_not_of(chars_to_trim);
if(pos != string::npos)
{
s.erase(s.begin(), s.begin() + pos);
Expand All @@ -14,7 +14,7 @@ void litehtml::trim(string &s)
s = "";
return;
}
pos = s.find_last_not_of(" \n\r\t");
pos = s.find_last_not_of(chars_to_trim);
if(pos != string::npos)
{
s.erase(s.begin() + pos + 1, s.end());
Expand Down Expand Up @@ -277,3 +277,14 @@ litehtml::string litehtml::get_escaped_string(const string& in_str)
}
return ret;
}

bool litehtml::is_number(const string& string, const bool allow_dot) {
for (auto ch : string)
{
if (!(t_isdigit(ch) || (allow_dot && ch == '.')))
{
return false;
}
}
return true;
}
24 changes: 24 additions & 0 deletions 24 src/html_tag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1548,9 +1548,33 @@ litehtml::element::ptr litehtml::html_tag::get_element_after(const style& style,
return nullptr;
}


void litehtml::html_tag::handle_counter_properties()
{
const auto& reset_property = m_style.get_property(string_id::_counter_reset_);
if (reset_property.m_type == prop_type_string_vector) {
auto reset_function = [&](const string_id&name_id, const int value) {
reset_counter(name_id, value);
};
parse_counter_tokens(reset_property.m_string_vector, 0, reset_function);
return;
}

const auto& inc_property = m_style.get_property(string_id::_counter_increment_);
if (inc_property.m_type == prop_type_string_vector) {
auto inc_function = [&](const string_id&name_id, const int value) {
increment_counter(name_id, value);
};
parse_counter_tokens(inc_property.m_string_vector, 1, inc_function);
return;
}
}


void litehtml::html_tag::add_style(const style& style)
{
m_style.combine(style);
handle_counter_properties();
}

void litehtml::html_tag::refresh_styles()
Expand Down
23 changes: 10 additions & 13 deletions 23 src/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,15 @@ void style::add_property(string_id name, const string& val, const string& baseur
add_parsed_property(_flex_basis_, property_value(length, important));
break;

case _counter_increment_:
case _counter_reset_:
{
string_vector tokens;
split_string(val, tokens, " ");
add_parsed_property(name, property_value(tokens, important));
break;
}

default:
add_parsed_property(name, property_value(val, important));
}
Expand Down Expand Up @@ -992,18 +1001,6 @@ void style::parse_font(const string& val, bool important)

void style::parse_flex(const string& val, bool important)
{
auto is_number = [](const string& val)
{
for (auto ch : val)
{
if ((ch < '0' || ch > '9') && ch != '.')
{
return false;
}
}
return true;
};

css_length _auto = css_length::predef_value(flex_basis_auto);

if (val == "initial")
Expand Down Expand Up @@ -1046,7 +1043,7 @@ void style::parse_flex(const string& val, bool important)
float grow = t_strtof(tokens[0]);
add_parsed_property(_flex_grow_, property_value(grow, important));

if (is_number(tokens[1]))
if (litehtml::is_number(tokens[1]))
{
float shrink = t_strtof(tokens[1]);
add_parsed_property(_flex_shrink_, property_value(shrink, important));
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.