From 118077a609767e09518338e11cb8b572bfa9bab9 Mon Sep 17 00:00:00 2001 From: Samuel Ugochukwu Date: Thu, 5 Dec 2024 12:20:05 +0100 Subject: [PATCH] Update target-counter implementation --- source/cssparser.cpp | 4 ++-- source/document.cpp | 6 ++++-- source/document.h | 2 +- source/htmldocument.cpp | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/source/cssparser.cpp b/source/cssparser.cpp index 013a5a9..8c0a072 100644 --- a/source/cssparser.cpp +++ b/source/cssparser.cpp @@ -2312,7 +2312,7 @@ RefPtr CSSParser::consumeContentTargetCounter(CSSTokenStream& input, C if(separator == nullptr) return nullptr; values.push_back(std::move(separator)); - input.consumeIncludingWhitespace(); + input.consumeWhitespace(); } if(input.consumeCommaIncludingWhitespace()) { @@ -2320,7 +2320,7 @@ RefPtr CSSParser::consumeContentTargetCounter(CSSTokenStream& input, C if(listStyle == nullptr) return nullptr; values.push_back(std::move(listStyle)); - input.consumeIncludingWhitespace(); + input.consumeWhitespace(); } if(!input.empty()) diff --git a/source/document.cpp b/source/document.cpp index e8523b2..4f0f85e 100644 --- a/source/document.cpp +++ b/source/document.cpp @@ -705,9 +705,11 @@ void Document::addTargetCounters(const HeapString& id, const CounterMap& counter m_counterCache.emplace(id, counters); } -HeapString Document::getTargetCounterText(const HeapString& id, const GlobalString& name, const GlobalString& listStyle, const HeapString& separator) +HeapString Document::getTargetCounterText(const HeapString& fragment, const GlobalString& name, const GlobalString& listStyle, const HeapString& separator) { - if(auto counters = getTargetCounters(id)) + if(fragment.empty() || fragment.front() != '#') + return emptyGlo; + if(auto counters = getTargetCounters(fragment.substring(1))) return getCountersText(*counters, name, listStyle, separator); return emptyGlo; } diff --git a/source/document.h b/source/document.h index dd5291e..ca77e5c 100644 --- a/source/document.h +++ b/source/document.h @@ -312,7 +312,7 @@ class Document : public ContainerNode { const CounterMap* getTargetCounters(const HeapString& id) const; void addTargetCounters(const HeapString& id, const CounterMap& counters); - HeapString getTargetCounterText(const HeapString& id, const GlobalString& name, const GlobalString& listStyle, const HeapString& separator); + HeapString getTargetCounterText(const HeapString& fragment, const GlobalString& name, const GlobalString& listStyle, const HeapString& separator); HeapString getCountersText(const CounterMap& counters, const GlobalString& name, const GlobalString& listStyle, const HeapString& separator); void addAuthorJavaScript(const std::string_view& content); diff --git a/source/htmldocument.cpp b/source/htmldocument.cpp index 6878da7..920e24a 100644 --- a/source/htmldocument.cpp +++ b/source/htmldocument.cpp @@ -123,6 +123,7 @@ class ContentBoxBuilder { void addText(const HeapString& text); void addLeaderText(const HeapString& text); void addLeader(const RefPtr& value); + void addTargetCounter(const RefPtr& function); void addImage(RefPtr image); void addQuote(CSSValueID value); @@ -192,6 +193,36 @@ void ContentBoxBuilder::addLeader(const RefPtr& value) } } +void ContentBoxBuilder::addTargetCounter(const RefPtr& function) +{ + HeapString fragment; + GlobalString identifier; + HeapString seperator; + GlobalString listStyle; + + size_t index = 0; + + assert(function->id() == CSSValueID::TargetCounter || function->id() == CSSValueID::TargetCounters); + if(auto attr = to(function->at(index))) { + assert(attr->id() == CSSValueID::Attr); + fragment = m_element->getAttribute(to(*attr->value()).value()); + } else { + fragment = to(*function->at(index)).value(); + } + + ++index; + + identifier = to(*function->at(index++)).value(); + if(function->id() == CSSValueID::TargetCounters) + seperator = to(*function->at(index++)).value(); + if(index < function->size()) { + listStyle = to(*function->at(index++)).value(); + assert(index == function->size()); + } + + addText(m_element->document()->getTargetCounterText(fragment, identifier, listStyle, seperator)); +} + void ContentBoxBuilder::addImage(RefPtr image) { if(image == nullptr) @@ -257,6 +288,8 @@ void ContentBoxBuilder::build() addImage(image->fetch(m_element->document())); } else if(auto counter = to(value)) { addText(m_counters.counterText(counter->identifier(), counter->listStyle(), counter->separator())); + } else if(auto targetCounter = to(value)) { + addTargetCounter(targetCounter); } else if(auto ident = to(value)) { addQuote(ident->value()); } else {