Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/fde_cssstyleselector.h" | |
| 8 | |
| 9 #include <algorithm> | |
| 10 #include <memory> | |
| 11 | |
| 12 #include "xfa/fde/css/fde_csscache.h" | |
| 13 #include "xfa/fde/css/fde_cssdeclaration.h" | |
| 14 #include "xfa/fde/css/fde_cssstylesheet.h" | |
| 15 #include "xfa/fde/css/fde_csssyntax.h" | |
| 16 #include "xfa/fxfa/app/xfa_textlayout.h" | |
| 17 #include "xfa/fde/css/fde_cssstyleselector.h" | |
|
dsinclair
2016/07/20 16:09:02
nit: Includes should be in sorted order.
npm_g
2016/07/20 17:04:03
Done.
| |
| 18 | |
| 19 #define FDE_CSSUNIVERSALHASH ('*') | |
| 20 | |
|
dsinclair
2016/07/20 16:09:02
The file should be named cfde_cssrulecollection to
npm_g
2016/07/20 17:04:03
Done.
| |
| 21 void CFDE_CSSRuleCollection::Clear() { | |
| 22 m_IDRules.RemoveAll(); | |
| 23 m_TagRules.RemoveAll(); | |
| 24 m_ClassRules.RemoveAll(); | |
| 25 m_pUniversalRules = nullptr; | |
| 26 m_pStaticStore = nullptr; | |
| 27 m_iSelectors = 0; | |
| 28 } | |
| 29 | |
| 30 CFDE_CSSRuleCollection::CFDE_CSSRuleCollection() | |
| 31 : m_pStaticStore(nullptr), | |
| 32 m_pUniversalRules(nullptr), | |
| 33 m_pPersudoRules(nullptr), | |
| 34 m_iSelectors(0) {} | |
| 35 | |
| 36 CFDE_CSSRuleCollection::~CFDE_CSSRuleCollection() { | |
| 37 Clear(); | |
| 38 } | |
| 39 | |
| 40 void CFDE_CSSRuleCollection::AddRulesFrom(const CFDE_CSSStyleSheetArray& sheets, | |
| 41 uint32_t dwMediaList, | |
| 42 IFGAS_FontMgr* pFontMgr) { | |
| 43 int32_t iSheets = sheets.GetSize(); | |
| 44 for (int32_t i = 0; i < iSheets; ++i) { | |
| 45 IFDE_CSSStyleSheet* pSheet = sheets.GetAt(i); | |
| 46 if (uint32_t dwMatchMedia = pSheet->GetMediaList() & dwMediaList) { | |
| 47 int32_t iRules = pSheet->CountRules(); | |
| 48 for (int32_t j = 0; j < iRules; j++) { | |
| 49 AddRulesFrom(pSheet, pSheet->GetRule(j), dwMatchMedia, pFontMgr); | |
| 50 } | |
| 51 } | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 void CFDE_CSSRuleCollection::AddRulesFrom(IFDE_CSSStyleSheet* pStyleSheet, | |
| 56 IFDE_CSSRule* pRule, | |
| 57 uint32_t dwMediaList, | |
| 58 IFGAS_FontMgr* pFontMgr) { | |
| 59 switch (pRule->GetType()) { | |
| 60 case FDE_CSSRULETYPE_Style: { | |
| 61 IFDE_CSSStyleRule* pStyleRule = static_cast<IFDE_CSSStyleRule*>(pRule); | |
| 62 CFDE_CSSDeclaration* pDeclaration = pStyleRule->GetDeclaration(); | |
| 63 int32_t iSelectors = pStyleRule->CountSelectorLists(); | |
| 64 for (int32_t i = 0; i < iSelectors; ++i) { | |
| 65 CFDE_CSSSelector* pSelector = pStyleRule->GetSelectorList(i); | |
| 66 if (pSelector->GetType() == FDE_CSSSELECTORTYPE_Persudo) { | |
| 67 FDE_CSSRuleData* pData = NewRuleData(pSelector, pDeclaration); | |
| 68 AddRuleTo(m_pPersudoRules, pData); | |
| 69 continue; | |
| 70 } | |
| 71 if (pSelector->GetNameHash() != FDE_CSSUNIVERSALHASH) { | |
| 72 AddRuleTo(m_TagRules, pSelector->GetNameHash(), pSelector, | |
| 73 pDeclaration); | |
| 74 continue; | |
| 75 } | |
| 76 CFDE_CSSSelector* pNext = pSelector->GetNextSelector(); | |
| 77 if (!pNext) { | |
| 78 FDE_CSSRuleData* pData = NewRuleData(pSelector, pDeclaration); | |
| 79 AddRuleTo(m_pUniversalRules, pData); | |
| 80 continue; | |
| 81 } | |
| 82 switch (pNext->GetType()) { | |
| 83 case FDE_CSSSELECTORTYPE_ID: | |
| 84 AddRuleTo(m_IDRules, pNext->GetNameHash(), pSelector, pDeclaration); | |
| 85 break; | |
| 86 case FDE_CSSSELECTORTYPE_Class: | |
| 87 AddRuleTo(m_ClassRules, pNext->GetNameHash(), pSelector, | |
| 88 pDeclaration); | |
| 89 break; | |
| 90 case FDE_CSSSELECTORTYPE_Descendant: | |
| 91 case FDE_CSSSELECTORTYPE_Element: | |
| 92 AddRuleTo(m_pUniversalRules, NewRuleData(pSelector, pDeclaration)); | |
| 93 break; | |
| 94 default: | |
| 95 ASSERT(FALSE); | |
| 96 break; | |
| 97 } | |
| 98 } | |
| 99 } break; | |
| 100 case FDE_CSSRULETYPE_Media: { | |
| 101 IFDE_CSSMediaRule* pMediaRule = static_cast<IFDE_CSSMediaRule*>(pRule); | |
| 102 if (pMediaRule->GetMediaList() & dwMediaList) { | |
| 103 int32_t iRules = pMediaRule->CountRules(); | |
| 104 for (int32_t i = 0; i < iRules; ++i) { | |
| 105 AddRulesFrom(pStyleSheet, pMediaRule->GetRule(i), dwMediaList, | |
| 106 pFontMgr); | |
| 107 } | |
| 108 } | |
| 109 } break; | |
| 110 default: | |
| 111 break; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void CFDE_CSSRuleCollection::AddRuleTo(CFX_MapPtrToPtr& map, | |
| 116 uint32_t dwKey, | |
| 117 CFDE_CSSSelector* pSel, | |
| 118 CFDE_CSSDeclaration* pDecl) { | |
| 119 void* pKey = (void*)(uintptr_t)dwKey; | |
| 120 FDE_CSSRuleData* pData = NewRuleData(pSel, pDecl); | |
| 121 FDE_CSSRuleData* pList = nullptr; | |
| 122 if (!map.Lookup(pKey, (void*&)pList)) { | |
| 123 map.SetAt(pKey, pData); | |
| 124 } else if (AddRuleTo(pList, pData)) { | |
| 125 map.SetAt(pKey, pList); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 FX_BOOL CFDE_CSSRuleCollection::AddRuleTo(FDE_CSSRuleData*& pList, | |
| 130 FDE_CSSRuleData* pData) { | |
| 131 if (pList) { | |
| 132 pData->pNext = pList->pNext; | |
| 133 pList->pNext = pData; | |
| 134 return FALSE; | |
| 135 } | |
| 136 | |
| 137 pList = pData; | |
| 138 return TRUE; | |
| 139 } | |
| 140 | |
| 141 FDE_CSSRuleData* CFDE_CSSRuleCollection::NewRuleData( | |
| 142 CFDE_CSSSelector* pSel, | |
| 143 CFDE_CSSDeclaration* pDecl) { | |
| 144 return FXTARGET_NewWith(m_pStaticStore) | |
| 145 FDE_CSSRuleData(pSel, pDecl, ++m_iSelectors); | |
| 146 } | |
| OLD | NEW |