• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fxcrt/css/cfx_cssrulecollection.h"
8 
9 #include <algorithm>
10 #include <utility>
11 
12 #include "core/fxcrt/css/cfx_cssdeclaration.h"
13 #include "core/fxcrt/css/cfx_cssselector.h"
14 #include "core/fxcrt/css/cfx_cssstylerule.h"
15 #include "core/fxcrt/css/cfx_cssstylesheet.h"
16 #include "core/fxcrt/css/cfx_csssyntaxparser.h"
17 
18 CFX_CSSRuleCollection::CFX_CSSRuleCollection() = default;
19 
20 CFX_CSSRuleCollection::~CFX_CSSRuleCollection() = default;
21 
22 const std::vector<std::unique_ptr<CFX_CSSRuleCollection::Data>>*
GetTagRuleData(const WideString & tagname) const23 CFX_CSSRuleCollection::GetTagRuleData(const WideString& tagname) const {
24   auto it = m_TagRules.find(FX_HashCode_GetLoweredW(tagname.AsStringView()));
25   return it != m_TagRules.end() ? &it->second : nullptr;
26 }
27 
SetRulesFromSheet(const CFX_CSSStyleSheet * sheet)28 void CFX_CSSRuleCollection::SetRulesFromSheet(const CFX_CSSStyleSheet* sheet) {
29   m_TagRules.clear();
30   for (size_t i = 0; i < sheet->CountRules(); ++i)
31     AddRule(sheet->GetRule(i));
32 }
33 
AddRule(CFX_CSSStyleRule * pStyleRule)34 void CFX_CSSRuleCollection::AddRule(CFX_CSSStyleRule* pStyleRule) {
35   CFX_CSSDeclaration* pDeclaration = pStyleRule->GetDeclaration();
36   size_t nSelectors = pStyleRule->CountSelectorLists();
37   for (size_t i = 0; i < nSelectors; ++i) {
38     CFX_CSSSelector* pSelector = pStyleRule->GetSelectorList(i);
39     m_TagRules[pSelector->name_hash()].push_back(
40         std::make_unique<Data>(pSelector, pDeclaration));
41   }
42 }
43 
Data(CFX_CSSSelector * pSel,CFX_CSSDeclaration * pDecl)44 CFX_CSSRuleCollection::Data::Data(CFX_CSSSelector* pSel,
45                                   CFX_CSSDeclaration* pDecl)
46     : pSelector(pSel), pDeclaration(pDecl) {}
47