1/**
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "StyleSheetList.h"
23
24#include "CSSStyleSheet.h"
25#include "Document.h"
26#include "HTMLNames.h"
27#include "HTMLStyleElement.h"
28#include "ShadowRoot.h"
29#include "StyleScope.h"
30#include <wtf/text/WTFString.h>
31
32namespace WebCore {
33
34using namespace HTMLNames;
35
36StyleSheetList::StyleSheetList(Document& document)
37 : m_document(makeWeakPtr(document))
38{
39}
40
41StyleSheetList::StyleSheetList(ShadowRoot& shadowRoot)
42 : m_shadowRoot(&shadowRoot)
43{
44}
45
46StyleSheetList::~StyleSheetList() = default;
47
48inline const Vector<RefPtr<StyleSheet>>& StyleSheetList::styleSheets() const
49{
50 if (m_document)
51 return m_document->styleScope().styleSheetsForStyleSheetList();
52 if (m_shadowRoot)
53 return m_shadowRoot->styleScope().styleSheetsForStyleSheetList();
54 return m_detachedStyleSheets;
55}
56
57Node* StyleSheetList::ownerNode() const
58{
59 if (m_document)
60 return m_document.get();
61 return m_shadowRoot;
62}
63
64void StyleSheetList::detach()
65{
66 if (m_document) {
67 ASSERT(!m_shadowRoot);
68 m_detachedStyleSheets = m_document->styleScope().styleSheetsForStyleSheetList();
69 m_document = nullptr;
70 } else if (m_shadowRoot) {
71 ASSERT(!m_document);
72 m_detachedStyleSheets = m_shadowRoot->styleScope().styleSheetsForStyleSheetList();
73 m_shadowRoot = nullptr;
74 } else
75 ASSERT_NOT_REACHED();
76}
77
78unsigned StyleSheetList::length() const
79{
80 return styleSheets().size();
81}
82
83StyleSheet* StyleSheetList::item(unsigned index)
84{
85 const Vector<RefPtr<StyleSheet>>& sheets = styleSheets();
86 return index < sheets.size() ? sheets[index].get() : 0;
87}
88
89CSSStyleSheet* StyleSheetList::namedItem(const AtomicString& name) const
90{
91 // Support the named getter on document for backwards compatibility.
92 if (!m_document)
93 return nullptr;
94
95 // IE also supports retrieving a stylesheet by name, using the name/id of the <style> tag
96 // (this is consistent with all the other collections)
97 // ### Bad implementation because returns a single element (are IDs always unique?)
98 // and doesn't look for name attribute.
99 // But unicity of stylesheet ids is good practice anyway ;)
100 Element* element = m_document->getElementById(name);
101 if (is<HTMLStyleElement>(element))
102 return downcast<HTMLStyleElement>(element)->sheet();
103 return nullptr;
104}
105
106Vector<AtomicString> StyleSheetList::supportedPropertyNames()
107{
108 // FIXME: Should be implemented.
109 return Vector<AtomicString>();
110}
111
112} // namespace WebCore
113