Home | History | Annotate | Download | only in css
      1 // Copyright 2016 PDFium Authors. All rights reserved.
      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 #include "third_party/base/ptr_util.h"
     18 
     19 CFX_CSSRuleCollection::CFX_CSSRuleCollection() : m_iSelectors(0) {}
     20 
     21 CFX_CSSRuleCollection::~CFX_CSSRuleCollection() {
     22   Clear();
     23 }
     24 
     25 void CFX_CSSRuleCollection::Clear() {
     26   m_TagRules.clear();
     27   m_iSelectors = 0;
     28 }
     29 
     30 const std::vector<std::unique_ptr<CFX_CSSRuleCollection::Data>>*
     31 CFX_CSSRuleCollection::GetTagRuleData(const WideString& tagname) const {
     32   auto it = m_TagRules.find(FX_HashCode_GetW(tagname.c_str(), true));
     33   return it != m_TagRules.end() ? &it->second : nullptr;
     34 }
     35 
     36 void CFX_CSSRuleCollection::AddRulesFrom(const CFX_CSSStyleSheet* sheet) {
     37   int32_t iRules = sheet->CountRules();
     38   for (int32_t j = 0; j < iRules; j++)
     39     AddRulesFrom(sheet, sheet->GetRule(j));
     40 }
     41 
     42 void CFX_CSSRuleCollection::AddRulesFrom(const CFX_CSSStyleSheet* pStyleSheet,
     43                                          CFX_CSSStyleRule* pStyleRule) {
     44   CFX_CSSDeclaration* pDeclaration = pStyleRule->GetDeclaration();
     45   int32_t iSelectors = pStyleRule->CountSelectorLists();
     46   for (int32_t i = 0; i < iSelectors; ++i) {
     47     CFX_CSSSelector* pSelector = pStyleRule->GetSelectorList(i);
     48     m_TagRules[pSelector->GetNameHash()].push_back(
     49         pdfium::MakeUnique<Data>(pSelector, pDeclaration));
     50     m_iSelectors++;
     51   }
     52 }
     53 
     54 CFX_CSSRuleCollection::Data::Data(CFX_CSSSelector* pSel,
     55                                   CFX_CSSDeclaration* pDecl)
     56     : pSelector(pSel), pDeclaration(pDecl) {}
     57