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 "xfa/fde/css/cfde_cssrulecollection.h"
8
9#include <algorithm>
10#include <utility>
11
12#include "third_party/base/ptr_util.h"
13#include "xfa/fde/css/cfde_cssdeclaration.h"
14#include "xfa/fde/css/cfde_cssselector.h"
15#include "xfa/fde/css/cfde_cssstylerule.h"
16#include "xfa/fde/css/cfde_cssstylesheet.h"
17#include "xfa/fde/css/cfde_csssyntaxparser.h"
18
19CFDE_CSSRuleCollection::CFDE_CSSRuleCollection() : m_iSelectors(0) {}
20
21CFDE_CSSRuleCollection::~CFDE_CSSRuleCollection() {
22  Clear();
23}
24
25void CFDE_CSSRuleCollection::Clear() {
26  m_TagRules.clear();
27  m_iSelectors = 0;
28}
29
30const std::vector<std::unique_ptr<CFDE_CSSRuleCollection::Data>>*
31CFDE_CSSRuleCollection::GetTagRuleData(const CFX_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
36void CFDE_CSSRuleCollection::AddRulesFrom(const CFDE_CSSStyleSheet* sheet,
37                                          CFGAS_FontMgr* pFontMgr) {
38  int32_t iRules = sheet->CountRules();
39  for (int32_t j = 0; j < iRules; j++)
40    AddRulesFrom(sheet, sheet->GetRule(j), pFontMgr);
41}
42
43void CFDE_CSSRuleCollection::AddRulesFrom(const CFDE_CSSStyleSheet* pStyleSheet,
44                                          CFDE_CSSStyleRule* pStyleRule,
45                                          CFGAS_FontMgr* pFontMgr) {
46  CFDE_CSSDeclaration* pDeclaration = pStyleRule->GetDeclaration();
47  int32_t iSelectors = pStyleRule->CountSelectorLists();
48  for (int32_t i = 0; i < iSelectors; ++i) {
49    CFDE_CSSSelector* pSelector = pStyleRule->GetSelectorList(i);
50    m_TagRules[pSelector->GetNameHash()].push_back(
51        pdfium::MakeUnique<Data>(pSelector, pDeclaration));
52    m_iSelectors++;
53  }
54}
55
56CFDE_CSSRuleCollection::Data::Data(CFDE_CSSSelector* pSel,
57                                   CFDE_CSSDeclaration* pDecl)
58    : pSelector(pSel), pDeclaration(pDecl) {}
59