summaryrefslogtreecommitdiff
path: root/xfa/fde/css
diff options
context:
space:
mode:
Diffstat (limited to 'xfa/fde/css')
-rw-r--r--xfa/fde/css/cfde_cssrulecollection.cpp62
-rw-r--r--xfa/fde/css/cfde_cssrulecollection.h22
-rw-r--r--xfa/fde/css/cfde_cssstyleselector.cpp35
-rw-r--r--xfa/fde/css/cfde_cssstyleselector.h7
4 files changed, 47 insertions, 79 deletions
diff --git a/xfa/fde/css/cfde_cssrulecollection.cpp b/xfa/fde/css/cfde_cssrulecollection.cpp
index c140c8e682..a72e1bb0bd 100644
--- a/xfa/fde/css/cfde_cssrulecollection.cpp
+++ b/xfa/fde/css/cfde_cssrulecollection.cpp
@@ -7,9 +7,9 @@
#include "xfa/fde/css/cfde_cssrulecollection.h"
#include <algorithm>
-#include <map>
-#include <memory>
+#include <utility>
+#include "third_party/base/ptr_util.h"
#include "xfa/fde/css/cfde_cssdeclaration.h"
#include "xfa/fde/css/cfde_cssselector.h"
#include "xfa/fde/css/cfde_cssstylerule.h"
@@ -17,15 +17,21 @@
#include "xfa/fde/css/cfde_csssyntaxparser.h"
#include "xfa/fde/css/cfde_csstagcache.h"
+CFDE_CSSRuleCollection::CFDE_CSSRuleCollection() : m_iSelectors(0) {}
+
+CFDE_CSSRuleCollection::~CFDE_CSSRuleCollection() {
+ Clear();
+}
+
void CFDE_CSSRuleCollection::Clear() {
m_TagRules.clear();
m_iSelectors = 0;
}
-CFDE_CSSRuleCollection::CFDE_CSSRuleCollection() : m_iSelectors(0) {}
-
-CFDE_CSSRuleCollection::~CFDE_CSSRuleCollection() {
- Clear();
+const std::vector<std::unique_ptr<CFDE_CSSRuleCollection::Data>>*
+CFDE_CSSRuleCollection::GetTagRuleData(uint32_t dwTagHash) const {
+ auto it = m_TagRules.find(dwTagHash);
+ return it != m_TagRules.end() ? &it->second : nullptr;
}
void CFDE_CSSRuleCollection::AddRulesFrom(const CFDE_CSSStyleSheet* sheet,
@@ -42,46 +48,12 @@ void CFDE_CSSRuleCollection::AddRulesFrom(const CFDE_CSSStyleSheet* pStyleSheet,
int32_t iSelectors = pStyleRule->CountSelectorLists();
for (int32_t i = 0; i < iSelectors; ++i) {
CFDE_CSSSelector* pSelector = pStyleRule->GetSelectorList(i);
- AddRuleTo(&m_TagRules, pSelector->GetNameHash(), pSelector, pDeclaration);
+ m_TagRules[pSelector->GetNameHash()].push_back(
+ pdfium::MakeUnique<Data>(pSelector, pDeclaration));
+ m_iSelectors++;
}
}
-void CFDE_CSSRuleCollection::AddRuleTo(std::map<uint32_t, Data*>* pMap,
- uint32_t dwKey,
- CFDE_CSSSelector* pSel,
- CFDE_CSSDeclaration* pDecl) {
- Data* pData = NewRuleData(pSel, pDecl);
- Data* pList = (*pMap)[dwKey];
- if (!pList) {
- (*pMap)[dwKey] = pData;
- } else if (AddRuleTo(&pList, pData)) {
- (*pMap)[dwKey] = pList;
- }
-}
-
-bool CFDE_CSSRuleCollection::AddRuleTo(Data** pList, Data* pData) {
- if (*pList) {
- pData->pNext = (*pList)->pNext;
- (*pList)->pNext = pData;
- return false;
- }
- *pList = pData;
- return true;
-}
-
-CFDE_CSSRuleCollection::Data* CFDE_CSSRuleCollection::NewRuleData(
- CFDE_CSSSelector* pSel,
- CFDE_CSSDeclaration* pDecl) {
- return new Data(pSel, pDecl, ++m_iSelectors);
-}
-
CFDE_CSSRuleCollection::Data::Data(CFDE_CSSSelector* pSel,
- CFDE_CSSDeclaration* pDecl,
- uint32_t dwPos)
- : pSelector(pSel), pDeclaration(pDecl), dwPriority(dwPos), pNext(nullptr) {
- static const uint32_t s_Specific[5] = {0x00010000, 0x00010000, 0x00100000,
- 0x00100000, 0x01000000};
- for (; pSel; pSel = pSel->GetNextSelector()) {
- dwPriority += s_Specific[static_cast<int>(pSel->GetType())];
- }
-}
+ CFDE_CSSDeclaration* pDecl)
+ : pSelector(pSel), pDeclaration(pDecl) {}
diff --git a/xfa/fde/css/cfde_cssrulecollection.h b/xfa/fde/css/cfde_cssrulecollection.h
index 47e7696c34..d9bbeb3736 100644
--- a/xfa/fde/css/cfde_cssrulecollection.h
+++ b/xfa/fde/css/cfde_cssrulecollection.h
@@ -8,6 +8,8 @@
#define XFA_FDE_CSS_CFDE_CSSRULECOLLECTION_H_
#include <map>
+#include <memory>
+#include <vector>
#include "core/fxcrt/fx_basic.h"
@@ -21,12 +23,10 @@ class CFDE_CSSRuleCollection {
public:
class Data {
public:
- Data(CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl, uint32_t dwPos);
+ Data(CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl);
CFDE_CSSSelector* const pSelector;
CFDE_CSSDeclaration* const pDeclaration;
- uint32_t dwPriority;
- Data* pNext;
};
CFDE_CSSRuleCollection();
@@ -36,23 +36,15 @@ class CFDE_CSSRuleCollection {
void Clear();
int32_t CountSelectors() const { return m_iSelectors; }
- Data* GetTagRuleData(uint32_t dwTagHash) {
- auto it = m_TagRules.find(dwTagHash);
- return it != m_TagRules.end() ? it->second : nullptr;
- }
+ const std::vector<std::unique_ptr<Data>>* GetTagRuleData(
+ uint32_t dwTagHash) const;
private:
void AddRulesFrom(const CFDE_CSSStyleSheet* pStyleSheet,
CFDE_CSSStyleRule* pRule,
CFGAS_FontMgr* pFontMgr);
- void AddRuleTo(std::map<uint32_t, Data*>* pMap,
- uint32_t dwKey,
- CFDE_CSSSelector* pSel,
- CFDE_CSSDeclaration* pDecl);
- bool AddRuleTo(Data** pList, Data* pData);
- Data* NewRuleData(CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl);
-
- std::map<uint32_t, Data*> m_TagRules;
+
+ std::map<uint32_t, std::vector<std::unique_ptr<Data>>> m_TagRules;
int32_t m_iSelectors;
};
diff --git a/xfa/fde/css/cfde_cssstyleselector.cpp b/xfa/fde/css/cfde_cssstyleselector.cpp
index b7275fca93..63d8e0986b 100644
--- a/xfa/fde/css/cfde_cssstyleselector.cpp
+++ b/xfa/fde/css/cfde_cssstyleselector.cpp
@@ -85,27 +85,30 @@ int32_t CFDE_CSSStyleSelector::MatchDeclarations(
if (m_UARules.CountSelectors() == 0)
return 0;
- if (pCache->HashTag())
- MatchRules(pCache, m_UARules.GetTagRuleData(pCache->HashTag()));
-
- std::sort(m_MatchedRules.begin(), m_MatchedRules.end(),
- [](const CFDE_CSSRuleCollection::Data* p1,
- const CFDE_CSSRuleCollection::Data* p2) {
- return p1->dwPriority < p2->dwPriority;
- });
- for (const auto& rule : m_MatchedRules)
+ // The ::Data is owned by the m_RuleCollection.
+ std::vector<CFDE_CSSRuleCollection::Data*> matchedRules;
+
+ if (pCache->HashTag()) {
+ MatchRules(&matchedRules, pCache,
+ m_UARules.GetTagRuleData(pCache->HashTag()));
+ }
+
+ for (const auto& rule : matchedRules)
matchedDecls.Add(rule->pDeclaration);
- m_MatchedRules.clear();
return matchedDecls.GetSize();
}
-void CFDE_CSSStyleSelector::MatchRules(CFDE_CSSTagCache* pCache,
- CFDE_CSSRuleCollection::Data* pList) {
- while (pList) {
- if (MatchSelector(pCache, pList->pSelector))
- m_MatchedRules.push_back(pList);
- pList = pList->pNext;
+void CFDE_CSSStyleSelector::MatchRules(
+ std::vector<CFDE_CSSRuleCollection::Data*>* matchedRules,
+ CFDE_CSSTagCache* pCache,
+ const std::vector<std::unique_ptr<CFDE_CSSRuleCollection::Data>>* pList) {
+ if (!pList)
+ return;
+
+ for (const auto& d : *pList) {
+ if (MatchSelector(pCache, d->pSelector))
+ matchedRules->push_back(d.get());
}
}
diff --git a/xfa/fde/css/cfde_cssstyleselector.h b/xfa/fde/css/cfde_cssstyleselector.h
index 6eb32aef62..2b03d7e961 100644
--- a/xfa/fde/css/cfde_cssstyleselector.h
+++ b/xfa/fde/css/cfde_cssstyleselector.h
@@ -48,8 +48,10 @@ class CFDE_CSSStyleSelector {
CFDE_CSSComputedStyle* pDestStyle);
private:
- void MatchRules(CFDE_CSSTagCache* pCache,
- CFDE_CSSRuleCollection::Data* pList);
+ void MatchRules(
+ std::vector<CFDE_CSSRuleCollection::Data*>* matchedRules,
+ CFDE_CSSTagCache* pCache,
+ const std::vector<std::unique_ptr<CFDE_CSSRuleCollection::Data>>* pList);
bool MatchSelector(CFDE_CSSTagCache* pCache, CFDE_CSSSelector* pSel);
void AppendInlineStyle(CFDE_CSSDeclaration* pDecl,
const FX_WCHAR* psz,
@@ -80,7 +82,6 @@ class CFDE_CSSStyleSelector {
std::unique_ptr<CFDE_CSSStyleSheet> m_UAStyles;
CFDE_CSSRuleCollection m_UARules;
std::unique_ptr<CFDE_CSSAccelerator> m_pAccelerator;
- std::vector<CFDE_CSSRuleCollection::Data*> m_MatchedRules;
};
#endif // XFA_FDE_CSS_CFDE_CSSSTYLESELECTOR_H_