content/base/src/nsStyledElement.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set tw=80 expandtab softtabstop=2 ts=2 sw=2: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsStyledElement.h"
michael@0 8 #include "nsGkAtoms.h"
michael@0 9 #include "nsAttrValue.h"
michael@0 10 #include "nsAttrValueInlines.h"
michael@0 11 #include "mozilla/dom/ElementInlines.h"
michael@0 12 #include "mozilla/InternalMutationEvent.h"
michael@0 13 #include "nsDOMCSSDeclaration.h"
michael@0 14 #include "nsDOMCSSAttrDeclaration.h"
michael@0 15 #include "nsServiceManagerUtils.h"
michael@0 16 #include "nsIDocument.h"
michael@0 17 #include "mozilla/css/StyleRule.h"
michael@0 18 #include "nsCSSParser.h"
michael@0 19 #include "mozilla/css/Loader.h"
michael@0 20 #include "nsIDOMMutationEvent.h"
michael@0 21 #include "nsXULElement.h"
michael@0 22 #include "nsContentUtils.h"
michael@0 23 #include "nsStyleUtil.h"
michael@0 24
michael@0 25 using namespace mozilla;
michael@0 26 using namespace mozilla::dom;
michael@0 27
michael@0 28 //----------------------------------------------------------------------
michael@0 29 // nsIContent methods
michael@0 30
michael@0 31 nsIAtom*
michael@0 32 nsStyledElementNotElementCSSInlineStyle::GetClassAttributeName() const
michael@0 33 {
michael@0 34 return nsGkAtoms::_class;
michael@0 35 }
michael@0 36
michael@0 37 nsIAtom*
michael@0 38 nsStyledElementNotElementCSSInlineStyle::GetIDAttributeName() const
michael@0 39 {
michael@0 40 return nsGkAtoms::id;
michael@0 41 }
michael@0 42
michael@0 43 nsIAtom*
michael@0 44 nsStyledElementNotElementCSSInlineStyle::DoGetID() const
michael@0 45 {
michael@0 46 NS_ASSERTION(HasID(), "Unexpected call");
michael@0 47
michael@0 48 // The nullcheck here is needed because Element::UnsetAttr calls
michael@0 49 // out to various code between removing the attribute and we get a chance to
michael@0 50 // ClearHasID().
michael@0 51
michael@0 52 const nsAttrValue* attr = mAttrsAndChildren.GetAttr(nsGkAtoms::id);
michael@0 53
michael@0 54 return attr ? attr->GetAtomValue() : nullptr;
michael@0 55 }
michael@0 56
michael@0 57 const nsAttrValue*
michael@0 58 nsStyledElementNotElementCSSInlineStyle::DoGetClasses() const
michael@0 59 {
michael@0 60 NS_ASSERTION(HasFlag(NODE_MAY_HAVE_CLASS), "Unexpected call");
michael@0 61 return mAttrsAndChildren.GetAttr(nsGkAtoms::_class);
michael@0 62 }
michael@0 63
michael@0 64 bool
michael@0 65 nsStyledElementNotElementCSSInlineStyle::ParseAttribute(int32_t aNamespaceID,
michael@0 66 nsIAtom* aAttribute,
michael@0 67 const nsAString& aValue,
michael@0 68 nsAttrValue& aResult)
michael@0 69 {
michael@0 70 if (aNamespaceID == kNameSpaceID_None) {
michael@0 71 if (aAttribute == nsGkAtoms::style) {
michael@0 72 SetMayHaveStyle();
michael@0 73 ParseStyleAttribute(aValue, aResult, false);
michael@0 74 return true;
michael@0 75 }
michael@0 76 if (aAttribute == nsGkAtoms::_class) {
michael@0 77 SetFlags(NODE_MAY_HAVE_CLASS);
michael@0 78 aResult.ParseAtomArray(aValue);
michael@0 79 return true;
michael@0 80 }
michael@0 81 if (aAttribute == nsGkAtoms::id) {
michael@0 82 // Store id as an atom. id="" means that the element has no id,
michael@0 83 // not that it has an emptystring as the id.
michael@0 84 RemoveFromIdTable();
michael@0 85 if (aValue.IsEmpty()) {
michael@0 86 ClearHasID();
michael@0 87 return false;
michael@0 88 }
michael@0 89 aResult.ParseAtom(aValue);
michael@0 90 SetHasID();
michael@0 91 AddToIdTable(aResult.GetAtomValue());
michael@0 92 return true;
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 return nsStyledElementBase::ParseAttribute(aNamespaceID, aAttribute, aValue,
michael@0 97 aResult);
michael@0 98 }
michael@0 99
michael@0 100 nsresult
michael@0 101 nsStyledElementNotElementCSSInlineStyle::UnsetAttr(int32_t aNameSpaceID,
michael@0 102 nsIAtom* aAttribute,
michael@0 103 bool aNotify)
michael@0 104 {
michael@0 105 nsAutoScriptBlocker scriptBlocker;
michael@0 106 if (aAttribute == nsGkAtoms::id && aNameSpaceID == kNameSpaceID_None) {
michael@0 107 // Have to do this before clearing flag. See RemoveFromIdTable
michael@0 108 RemoveFromIdTable();
michael@0 109 }
michael@0 110
michael@0 111 return Element::UnsetAttr(aNameSpaceID, aAttribute, aNotify);
michael@0 112 }
michael@0 113
michael@0 114 nsresult
michael@0 115 nsStyledElementNotElementCSSInlineStyle::AfterSetAttr(int32_t aNamespaceID,
michael@0 116 nsIAtom* aAttribute,
michael@0 117 const nsAttrValue* aValue,
michael@0 118 bool aNotify)
michael@0 119 {
michael@0 120 if (aNamespaceID == kNameSpaceID_None && !aValue &&
michael@0 121 aAttribute == nsGkAtoms::id) {
michael@0 122 // The id has been removed when calling UnsetAttr but we kept it because
michael@0 123 // the id is used for some layout stuff between UnsetAttr and AfterSetAttr.
michael@0 124 // Now. the id is really removed so it would not be safe to keep this flag.
michael@0 125 ClearHasID();
michael@0 126 }
michael@0 127
michael@0 128 return Element::AfterSetAttr(aNamespaceID, aAttribute, aValue, aNotify);
michael@0 129 }
michael@0 130
michael@0 131 nsresult
michael@0 132 nsStyledElementNotElementCSSInlineStyle::SetInlineStyleRule(css::StyleRule* aStyleRule,
michael@0 133 const nsAString* aSerialized,
michael@0 134 bool aNotify)
michael@0 135 {
michael@0 136 SetMayHaveStyle();
michael@0 137 bool modification = false;
michael@0 138 nsAttrValue oldValue;
michael@0 139
michael@0 140 bool hasListeners = aNotify &&
michael@0 141 nsContentUtils::HasMutationListeners(this,
michael@0 142 NS_EVENT_BITS_MUTATION_ATTRMODIFIED,
michael@0 143 this);
michael@0 144
michael@0 145 // There's no point in comparing the stylerule pointers since we're always
michael@0 146 // getting a new stylerule here. And we can't compare the stringvalues of
michael@0 147 // the old and the new rules since both will point to the same declaration
michael@0 148 // and thus will be the same.
michael@0 149 if (hasListeners) {
michael@0 150 // save the old attribute so we can set up the mutation event properly
michael@0 151 // XXXbz if the old rule points to the same declaration as the new one,
michael@0 152 // this is getting the new attr value, not the old one....
michael@0 153 nsAutoString oldValueStr;
michael@0 154 modification = GetAttr(kNameSpaceID_None, nsGkAtoms::style,
michael@0 155 oldValueStr);
michael@0 156 if (modification) {
michael@0 157 oldValue.SetTo(oldValueStr);
michael@0 158 }
michael@0 159 }
michael@0 160 else if (aNotify && IsInDoc()) {
michael@0 161 modification = !!mAttrsAndChildren.GetAttr(nsGkAtoms::style);
michael@0 162 }
michael@0 163
michael@0 164 nsAttrValue attrValue(aStyleRule, aSerialized);
michael@0 165
michael@0 166 // XXXbz do we ever end up with ADDITION here? I doubt it.
michael@0 167 uint8_t modType = modification ?
michael@0 168 static_cast<uint8_t>(nsIDOMMutationEvent::MODIFICATION) :
michael@0 169 static_cast<uint8_t>(nsIDOMMutationEvent::ADDITION);
michael@0 170
michael@0 171 return SetAttrAndNotify(kNameSpaceID_None, nsGkAtoms::style, nullptr,
michael@0 172 oldValue, attrValue, modType, hasListeners,
michael@0 173 aNotify, kDontCallAfterSetAttr);
michael@0 174 }
michael@0 175
michael@0 176 css::StyleRule*
michael@0 177 nsStyledElementNotElementCSSInlineStyle::GetInlineStyleRule()
michael@0 178 {
michael@0 179 if (!MayHaveStyle()) {
michael@0 180 return nullptr;
michael@0 181 }
michael@0 182 const nsAttrValue* attrVal = mAttrsAndChildren.GetAttr(nsGkAtoms::style);
michael@0 183
michael@0 184 if (attrVal && attrVal->Type() == nsAttrValue::eCSSStyleRule) {
michael@0 185 return attrVal->GetCSSStyleRuleValue();
michael@0 186 }
michael@0 187
michael@0 188 return nullptr;
michael@0 189 }
michael@0 190
michael@0 191 // ---------------------------------------------------------------
michael@0 192 // Others and helpers
michael@0 193
michael@0 194 nsICSSDeclaration*
michael@0 195 nsStyledElementNotElementCSSInlineStyle::Style()
michael@0 196 {
michael@0 197 Element::nsDOMSlots *slots = DOMSlots();
michael@0 198
michael@0 199 if (!slots->mStyle) {
michael@0 200 // Just in case...
michael@0 201 ReparseStyleAttribute(true);
michael@0 202
michael@0 203 slots->mStyle = new nsDOMCSSAttributeDeclaration(this, false);
michael@0 204 SetMayHaveStyle();
michael@0 205 }
michael@0 206
michael@0 207 return slots->mStyle;
michael@0 208 }
michael@0 209
michael@0 210 nsresult
michael@0 211 nsStyledElementNotElementCSSInlineStyle::ReparseStyleAttribute(bool aForceInDataDoc)
michael@0 212 {
michael@0 213 if (!MayHaveStyle()) {
michael@0 214 return NS_OK;
michael@0 215 }
michael@0 216 const nsAttrValue* oldVal = mAttrsAndChildren.GetAttr(nsGkAtoms::style);
michael@0 217
michael@0 218 if (oldVal && oldVal->Type() != nsAttrValue::eCSSStyleRule) {
michael@0 219 nsAttrValue attrValue;
michael@0 220 nsAutoString stringValue;
michael@0 221 oldVal->ToString(stringValue);
michael@0 222 ParseStyleAttribute(stringValue, attrValue, aForceInDataDoc);
michael@0 223 // Don't bother going through SetInlineStyleRule, we don't want to fire off
michael@0 224 // mutation events or document notifications anyway
michael@0 225 nsresult rv = mAttrsAndChildren.SetAndTakeAttr(nsGkAtoms::style, attrValue);
michael@0 226 NS_ENSURE_SUCCESS(rv, rv);
michael@0 227 }
michael@0 228
michael@0 229 return NS_OK;
michael@0 230 }
michael@0 231
michael@0 232 void
michael@0 233 nsStyledElementNotElementCSSInlineStyle::ParseStyleAttribute(const nsAString& aValue,
michael@0 234 nsAttrValue& aResult,
michael@0 235 bool aForceInDataDoc)
michael@0 236 {
michael@0 237 nsIDocument* doc = OwnerDoc();
michael@0 238
michael@0 239 if (!nsStyleUtil::CSPAllowsInlineStyle(nullptr, NodePrincipal(),
michael@0 240 doc->GetDocumentURI(), 0, aValue,
michael@0 241 nullptr))
michael@0 242 return;
michael@0 243
michael@0 244 if (aForceInDataDoc ||
michael@0 245 !doc->IsLoadedAsData() ||
michael@0 246 doc->IsStaticDocument()) {
michael@0 247 bool isCSS = true; // assume CSS until proven otherwise
michael@0 248
michael@0 249 if (!IsInNativeAnonymousSubtree()) { // native anonymous content
michael@0 250 // always assumes CSS
michael@0 251 nsAutoString styleType;
michael@0 252 doc->GetHeaderData(nsGkAtoms::headerContentStyleType, styleType);
michael@0 253 if (!styleType.IsEmpty()) {
michael@0 254 static const char textCssStr[] = "text/css";
michael@0 255 isCSS = (styleType.EqualsIgnoreCase(textCssStr, sizeof(textCssStr) - 1));
michael@0 256 }
michael@0 257 }
michael@0 258
michael@0 259 if (isCSS && aResult.ParseStyleAttribute(aValue, this)) {
michael@0 260 return;
michael@0 261 }
michael@0 262 }
michael@0 263
michael@0 264 aResult.SetTo(aValue);
michael@0 265 }

mercurial