Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(848)

Unified Diff: Source/core/dom/StyleElement.cpp

Issue 28553005: Avoid parsing css text if there are identical inline style blocks. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Added StyleSheetContentsCache class Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/dom/StyleElement.cpp
diff --git a/Source/core/dom/StyleElement.cpp b/Source/core/dom/StyleElement.cpp
index 6a6037c85f0ca3a36eec38648178e60bb09e0cfa..97605624bb69348ae9f5835469ea371489b4d05e 100644
--- a/Source/core/dom/StyleElement.cpp
+++ b/Source/core/dom/StyleElement.cpp
@@ -35,6 +35,8 @@
namespace WebCore {
+static const unsigned cacheableTextContentSizeLimit = (unsigned)-1; // UNLIMITED
+
static bool isCSS(Element* element, const AtomicString& type)
{
return type.isEmpty() || (element->isHTMLElement() ? equalIgnoringCase(type, "text/css") : (type == "text/css"));
@@ -115,7 +117,12 @@ void StyleElement::process(Element* element)
void StyleElement::clearSheet()
{
ASSERT(m_sheet);
+ // If the sheet was cached one(=!m_sheetText.isEmpty()), the cached sheet has an owner document and
+ // document is not destroyed (not in ~Document), we need to unregister.
+ if (!m_sheetText.isEmpty() && m_sheet->ownerDocument() && m_sheet->ownerDocument()->isActive())
+ m_sheet->ownerDocument()->styleEngine()->unregisterStyleSheetContents(m_sheetText);
m_sheet.release()->clearOwnerNode();
+ m_sheetText = AtomicString();
}
void StyleElement::createSheet(Element* e, const String& text)
@@ -129,6 +136,9 @@ void StyleElement::createSheet(Element* e, const String& text)
clearSheet();
}
+ ASSERT(!isHTMLStyleElement(e) || m_sheetText.isEmpty());
+ AtomicString sheetText = isHTMLStyleElement(e) ? AtomicString(text.impl()) : AtomicString();
+
// If type is empty or CSS, this is a CSS style sheet.
const AtomicString& type = this->type();
bool passesContentSecurityPolicyChecks = document.contentSecurityPolicy()->allowStyleNonce(e->fastGetAttribute(HTMLNames::nonceAttr)) || document.contentSecurityPolicy()->allowInlineStyle(e->document().url(), m_startPosition.m_line);
@@ -142,17 +152,30 @@ void StyleElement::createSheet(Element* e, const String& text)
m_loading = true;
TextPosition startPosition = m_startPosition == TextPosition::belowRangePosition() ? TextPosition::minimumPosition() : m_startPosition;
- m_sheet = CSSStyleSheet::createInline(e, KURL(), startPosition, document.inputEncoding());
- m_sheet->setMediaQueries(mediaQueries.release());
- m_sheet->setTitle(e->title());
- m_sheet->contents()->parseStringAtPosition(text, startPosition, m_createdByParser);
-
+ if (StyleSheetContents* contents = document.styleEngine()->findStyleSheetContents(sheetText)) {
+ // FIXME: should we use registerClient to avoid allocating a new StyleSheetContents?
+ // In this case, StyleSheetContents::singleOwnerNode() doesn't work. So need to modify
+ // codes depending on singleOwnerNode().
+ m_sheet = CSSStyleSheet::createInline(contents->copy(), e, startPosition);
+ m_sheet->setMediaQueries(mediaQueries.release());
+ m_sheet->setTitle(e->title());
+ } else {
+ m_sheet = CSSStyleSheet::createInline(e, KURL(), startPosition, document.inputEncoding());
+ m_sheet->setMediaQueries(mediaQueries.release());
+ m_sheet->setTitle(e->title());
+ m_sheet->contents()->parseStringAtPosition(text, startPosition, m_createdByParser);
+ }
m_loading = false;
}
}
- if (m_sheet)
+ if (m_sheet) {
m_sheet->contents()->checkLoaded();
+ if (!sheetText.isEmpty() && m_sheet->contents()->isCacheable()) {
+ m_sheetText = sheetText;
+ document.styleEngine()->registerStyleSheetContents(m_sheetText, m_sheet->contents());
+ }
+ }
}
bool StyleElement::isLoading() const

Powered by Google App Engine
This is Rietveld 408576698