aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichelCalonder <michel.calonder@web.de>2023-12-25 11:18:00 +0100
committerMichelCalonder <michel.calonder@web.de>2023-12-25 11:18:00 +0100
commitb9368a4bc14e98b486f277cfaef37ee0785dfda0 (patch)
treebe16b18749b41cd288721b410f407da36ed2b554
parentff6aa5fcc5280ae22a7ea9bff645fb0b41872211 (diff)
support for counter_s_
-rw-r--r--include/litehtml/element.h1
-rw-r--r--include/litehtml/html.h2
-rw-r--r--src/el_before_after.cpp12
-rw-r--r--src/element.cpp33
-rw-r--r--src/html.cpp6
5 files changed, 48 insertions, 6 deletions
diff --git a/include/litehtml/element.h b/include/litehtml/element.h
index e9cfbafd..27177993 100644
--- a/include/litehtml/element.h
+++ b/include/litehtml/element.h
@@ -148,6 +148,7 @@ namespace litehtml
}
string get_counter_value(const string& counter_name);
+ string get_counters_value(const string_vector& parameters);
void increment_counter(const string& counter_name, const int increment = 1);
void reset_counter(const string& counter_name, const int value = 0);
diff --git a/include/litehtml/html.h b/include/litehtml/html.h
index cbd0d26b..818d5ce5 100644
--- a/include/litehtml/html.h
+++ b/include/litehtml/html.h
@@ -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 = ';');
diff --git a/src/el_before_after.cpp b/src/el_before_after.cpp
index 7f1ddf27..c7c2d680 100644
--- a/src/el_before_after.cpp
+++ b/src/el_before_after.cpp
@@ -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
@@ -157,9 +157,17 @@ void litehtml::el_before_after_base::add_function( const string& fnc, const stri
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);
if(!p_url.empty())
diff --git a/src/element.cpp b/src/element.cpp
index 310b5e59..07efba7b 100644
--- a/src/element.cpp
+++ b/src/element.cpp
@@ -298,6 +298,39 @@ litehtml::string litehtml::element::get_counter_value(const string& counter_name
return "0";
}
+string litehtml::element::get_counters_value(const string_vector& parameters)
+{
+ string result = "";
+ if (parameters.size() >= 2) {
+ const string& counter_name = 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);
+ 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, instancieate one with value '0'
+ shared_from_this()->m_counter_values[counter_name] = 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& counter_name, std::map<string, int>::iterator& map_iterator) {
element::ptr current = shared_from_this();
diff --git a/src/html.cpp b/src/html.cpp
index 4c25b9f3..ee8ddad8 100644
--- a/src/html.cpp
+++ b/src/html.cpp
@@ -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);
@@ -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());