Skip to content

Commit

Permalink
Update target-counter implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Dec 5, 2024
1 parent 0150367 commit 118077a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
4 changes: 2 additions & 2 deletions source/cssparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2312,15 +2312,15 @@ RefPtr<CSSValue> CSSParser::consumeContentTargetCounter(CSSTokenStream& input, C
if(separator == nullptr)
return nullptr;
values.push_back(std::move(separator));
input.consumeIncludingWhitespace();
input.consumeWhitespace();
}

if(input.consumeCommaIncludingWhitespace()) {
auto listStyle = consumeCustomIdent(input);
if(listStyle == nullptr)
return nullptr;
values.push_back(std::move(listStyle));
input.consumeIncludingWhitespace();
input.consumeWhitespace();
}

if(!input.empty())
Expand Down
6 changes: 4 additions & 2 deletions source/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion source/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions source/htmldocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class ContentBoxBuilder {
void addText(const HeapString& text);
void addLeaderText(const HeapString& text);
void addLeader(const RefPtr<CSSValue>& value);
void addTargetCounter(const RefPtr<CSSFunctionValue>& function);
void addImage(RefPtr<Image> image);
void addQuote(CSSValueID value);

Expand Down Expand Up @@ -192,6 +193,36 @@ void ContentBoxBuilder::addLeader(const RefPtr<CSSValue>& value)
}
}

void ContentBoxBuilder::addTargetCounter(const RefPtr<CSSFunctionValue>& 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<CSSUnaryFunctionValue>(function->at(index))) {
assert(attr->id() == CSSValueID::Attr);
fragment = m_element->getAttribute(to<CSSCustomIdentValue>(*attr->value()).value());
} else {
fragment = to<CSSLocalUrlValue>(*function->at(index)).value();
}

++index;

identifier = to<CSSCustomIdentValue>(*function->at(index++)).value();
if(function->id() == CSSValueID::TargetCounters)
seperator = to<CSSStringValue>(*function->at(index++)).value();
if(index < function->size()) {
listStyle = to<CSSCustomIdentValue>(*function->at(index++)).value();
assert(index == function->size());
}

addText(m_element->document()->getTargetCounterText(fragment, identifier, listStyle, seperator));
}

void ContentBoxBuilder::addImage(RefPtr<Image> image)
{
if(image == nullptr)
Expand Down Expand Up @@ -257,6 +288,8 @@ void ContentBoxBuilder::build()
addImage(image->fetch(m_element->document()));
} else if(auto counter = to<CSSCounterValue>(value)) {
addText(m_counters.counterText(counter->identifier(), counter->listStyle(), counter->separator()));
} else if(auto targetCounter = to<CSSFunctionValue>(value)) {
addTargetCounter(targetCounter);
} else if(auto ident = to<CSSIdentValue>(value)) {
addQuote(ident->value());
} else {
Expand Down

0 comments on commit 118077a

Please sign in to comment.