DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "HTMLBodyElement.h"
#include "mozilla/dom/BindContext.h"
#include "mozilla/dom/BindContext.h"
#include "mozilla/dom/HTMLBodyElementBinding.h"
#include "mozilla/MappedDeclarations.h"
#include "mozilla/HTMLEditor.h"
#include "mozilla/TextEditor.h"
#include "nsAttrValueInlines.h"
#include "nsAttrValueInlines.h"
#include "nsGkAtoms.h"
#include "nsStyleConsts.h"
#include "nsPresContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Document.h"
#include "DocumentInlines.h"
#include "nsDocShell.h"
#include "nsHTMLStyleSheet.h"
#include "nsMappedAttributes.h"
#include "nsIDocShell.h"
#include "nsIDocShell.h"
#include "nsGlobalWindow.h"

NS_IMPL_NS_NEW_HTML_ELEMENT(Body)

namespace mozilla {
namespace mozilla {
namespace dom {

//----------------------------------------------------------------------

HTMLBodyElement::~HTMLBodyElement() = default;

JSObject* HTMLBodyElement::WrapNode(JSContext* aCx,
JSObject* HTMLBodyElement::WrapNode(JSContext* aCx,
                                    JS::Handle<JSObject*> aGivenProto) {
  return HTMLBodyElement_Binding::Wrap(aCx, this, aGivenProto);
}

NS_IMPL_ELEMENT_CLONE(HTMLBodyElement)
NS_IMPL_ELEMENT_CLONE(HTMLBodyElement)

bool HTMLBodyElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
                                     const nsAString& aValue,
                                     nsIPrincipal* aMaybeScriptedPrincipal,
                                     nsAttrValue& aResult) {
                                     nsAttrValue& aResult) {
  if (aNamespaceID == kNameSpaceID_None) {
    if (aAttribute == nsGkAtoms::bgcolor || aAttribute == nsGkAtoms::text ||
        aAttribute == nsGkAtoms::link || aAttribute == nsGkAtoms::alink ||
        aAttribute == nsGkAtoms::vlink) {
        aAttribute == nsGkAtoms::vlink) {
      return aResult.ParseColor(aValue);
    }
    if (aAttribute == nsGkAtoms::marginwidth ||
        aAttribute == nsGkAtoms::marginheight ||
        aAttribute == nsGkAtoms::topmargin ||
        aAttribute == nsGkAtoms::topmargin ||
        aAttribute == nsGkAtoms::bottommargin ||
        aAttribute == nsGkAtoms::leftmargin ||
        aAttribute == nsGkAtoms::rightmargin) {
      return aResult.ParseNonNegativeIntValue(aValue);
    }
    }
  }

  return nsGenericHTMLElement::ParseBackgroundAttribute(
             aNamespaceID, aAttribute, aValue, aResult) ||
         nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
         nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
                                              aMaybeScriptedPrincipal, aResult);
}

void HTMLBodyElement::MapAttributesIntoRule(
    const nsMappedAttributes* aAttributes, MappedDeclarations& aDecls) {
    const nsMappedAttributes* aAttributes, MappedDeclarations& aDecls) {
  // This is the one place where we try to set the same property
  // multiple times in presentation attributes. Servo does not support
  // querying if a property is set (because that is O(n) behavior
  // in ServoSpecifiedValues). Instead, we use the below values to keep
  // track of whether we have already set a property, and if so, what value
  // track of whether we have already set a property, and if so, what value
  // we set it to (which is used when handling margin
  // attributes from the containing frame element)

  int32_t bodyMarginWidth = -1;
  int32_t bodyMarginHeight = -1;
  int32_t bodyMarginHeight = -1;
  int32_t bodyTopMargin = -1;
  int32_t bodyBottomMargin = -1;
  int32_t bodyLeftMargin = -1;
  int32_t bodyRightMargin = -1;

  const nsAttrValue* value;
  // if marginwidth/marginheight are set, reflect them as 'margin'
  // if marginwidth/marginheight are set, reflect them as 'margin'
  value = aAttributes->GetAttr(nsGkAtoms::marginwidth);
  if (value && value->Type() == nsAttrValue::eInteger) {
    bodyMarginWidth = value->GetIntegerValue();
    if (bodyMarginWidth < 0) {
      bodyMarginWidth = 0;
      bodyMarginWidth = 0;
    }
    aDecls.SetPixelValueIfUnset(eCSSProperty_margin_left,
                                (float)bodyMarginWidth);
    aDecls.SetPixelValueIfUnset(eCSSProperty_margin_right,
                                (float)bodyMarginWidth);
                                (float)bodyMarginWidth);
  }

  value = aAttributes->GetAttr(nsGkAtoms::marginheight);
  if (value && value->Type() == nsAttrValue::eInteger) {
    bodyMarginHeight = value->GetIntegerValue();
    bodyMarginHeight = value->GetIntegerValue();
    if (bodyMarginHeight < 0) {
      bodyMarginHeight = 0;
    }
    aDecls.SetPixelValueIfUnset(eCSSProperty_margin_top,
                                (float)bodyMarginHeight);
                                (float)bodyMarginHeight);
    aDecls.SetPixelValueIfUnset(eCSSProperty_margin_bottom,
                                (float)bodyMarginHeight);
  }

  // topmargin (IE-attribute)
  if (bodyMarginHeight == -1) {
    value = aAttributes->GetAttr(nsGkAtoms::topmargin);
    if (value && value->Type() == nsAttrValue::eInteger) {
      bodyTopMargin = value->GetIntegerValue();
      if (bodyTopMargin < 0) {
      if (bodyTopMargin < 0) {
        bodyTopMargin = 0;
      }
      aDecls.SetPixelValueIfUnset(eCSSProperty_margin_top,
                                  (float)bodyTopMargin);
    }
    }
  }
  // bottommargin (IE-attribute)

  if (bodyMarginHeight == -1) {
    value = aAttributes->GetAttr(nsGkAtoms::bottommargin);
    value = aAttributes->GetAttr(nsGkAtoms::bottommargin);
    if (value && value->Type() == nsAttrValue::eInteger) {
      bodyBottomMargin = value->GetIntegerValue();
      if (bodyBottomMargin < 0) {
        bodyBottomMargin = 0;
      }
      }
      aDecls.SetPixelValueIfUnset(eCSSProperty_margin_bottom,
                                  (float)bodyBottomMargin);
    }
  }


  // leftmargin (IE-attribute)
  if (bodyMarginWidth == -1) {
    value = aAttributes->GetAttr(nsGkAtoms::leftmargin);
    if (value && value->Type() == nsAttrValue::eInteger) {
      bodyLeftMargin = value->GetIntegerValue();
      bodyLeftMargin = value->GetIntegerValue();
      if (bodyLeftMargin < 0) {
        bodyLeftMargin = 0;
      }
      aDecls.SetPixelValueIfUnset(eCSSProperty_margin_left,
                                  (float)bodyLeftMargin);
                                  (float)bodyLeftMargin);
    }
  }
  // rightmargin (IE-attribute)
  if (bodyMarginWidth == -1) {
    value = aAttributes->GetAttr(nsGkAtoms::rightmargin);
    if (value && value->Type() == nsAttrValue::eInteger) {
      bodyRightMargin = value->GetIntegerValue();
      if (bodyRightMargin < 0) {
      if (bodyRightMargin < 0) {
        bodyRightMargin = 0;
      }
      aDecls.SetPixelValueIfUnset(eCSSProperty_margin_right,
                                  (float)bodyRightMargin);
    }
    }
  }

  // if marginwidth or marginheight is set in the <frame> and not set in the
  // <body> reflect them as margin in the <body>
  if (bodyMarginWidth == -1 || bodyMarginHeight == -1) {
  if (bodyMarginWidth == -1 || bodyMarginHeight == -1) {
    if (nsDocShell* ds = nsDocShell::Cast(aDecls.Document()->GetDocShell())) {
      CSSIntSize margins = ds->GetFrameMargins();
      int32_t frameMarginWidth = margins.width;
      int32_t frameMarginHeight = margins.height;


      if (bodyMarginWidth == -1 && frameMarginWidth >= 0) {
        if (bodyLeftMargin == -1) {
          aDecls.SetPixelValueIfUnset(eCSSProperty_margin_left,
                                      (float)frameMarginWidth);
        }
        }
        if (bodyRightMargin == -1) {
          aDecls.SetPixelValueIfUnset(eCSSProperty_margin_right,
                                      (float)frameMarginWidth);
        }
      }
      }

      if (bodyMarginHeight == -1 && frameMarginHeight >= 0) {
        if (bodyTopMargin == -1) {
          aDecls.SetPixelValueIfUnset(eCSSProperty_margin_top,
                                      (float)frameMarginHeight);
        }
        if (bodyBottomMargin == -1) {
        if (bodyBottomMargin == -1) {
          aDecls.SetPixelValueIfUnset(eCSSProperty_margin_bottom,
                                      (float)frameMarginHeight);
        }
      }
    }
  }
  }

  // When display if first asked for, go ahead and get our colors set up.
  if (nsHTMLStyleSheet* styleSheet =
          aDecls.Document()->GetAttributeStyleSheet()) {
    nscolor color;
    nscolor color;
    value = aAttributes->GetAttr(nsGkAtoms::link);
    if (value && value->GetColorValue(color)) {
      styleSheet->SetLinkColor(color);
    }
    }

    value = aAttributes->GetAttr(nsGkAtoms::alink);
    if (value && value->GetColorValue(color)) {
      styleSheet->SetActiveLinkColor(color);
    }
    }

    value = aAttributes->GetAttr(nsGkAtoms::vlink);
    if (value && value->GetColorValue(color)) {
      styleSheet->SetVisitedLinkColor(color);
    }
    }
  }

  if (!aDecls.PropertyIsSet(eCSSProperty_color)) {
    // color: color
    nscolor color;
    nscolor color;
    value = aAttributes->GetAttr(nsGkAtoms::text);
    if (value && value->GetColorValue(color)) {
      aDecls.SetColorValue(eCSSProperty_color, color);
    }
  }
  }

  nsGenericHTMLElement::MapBackgroundAttributesInto(aAttributes, aDecls);
  nsGenericHTMLElement::MapCommonAttributesInto(aAttributes, aDecls);
}


nsMapRuleToAttributesFunc HTMLBodyElement::GetAttributeMappingFunction() const {
  return &MapAttributesIntoRule;
}

NS_IMETHODIMP_(bool)
NS_IMETHODIMP_(bool)
HTMLBodyElement::IsAttributeMapped(const nsAtom* aAttribute) const {
HTMLBodyElement::IsAttributeMapped(const nsAtom* aAttribute) const {
  static const MappedAttributeEntry attributes[] = {
      {nsGkAtoms::link},
      {nsGkAtoms::vlink},
      {nsGkAtoms::alink},
      {nsGkAtoms::text},
      {nsGkAtoms::text},
      {nsGkAtoms::marginwidth},
      {nsGkAtoms::marginheight},
      {nsGkAtoms::topmargin},
      {nsGkAtoms::rightmargin},
      {nsGkAtoms::bottommargin},
      {nsGkAtoms::bottommargin},
      {nsGkAtoms::leftmargin},
      {nullptr},
  };

  static const MappedAttributeEntry* const map[] = {
  static const MappedAttributeEntry* const map[] = {
      attributes,
      sCommonAttributeMap,
      sBackgroundAttributeMap,
  };


  return FindAttributeDependence(aAttribute, map);
}

already_AddRefed<TextEditor> HTMLBodyElement::GetAssociatedEditor() {
  RefPtr<TextEditor> textEditor = GetTextEditorInternal();
  RefPtr<TextEditor> textEditor = GetTextEditorInternal();
  if (textEditor) {
    return textEditor.forget();
  }

  // Make sure this is the actual body of the document
  if (this != OwnerDoc()->GetBodyElement()) {
    return nullptr;
    return nullptr;
  }

  // For designmode, try to get document's editor
  nsPresContext* presContext = GetPresContext(eForComposedDoc);
  if (!presContext) {
  if (!presContext) {
    return nullptr;
  }

  nsCOMPtr<nsIDocShell> docShell = presContext->GetDocShell();
  if (!docShell) {
    return nullptr;
  }

  RefPtr<HTMLEditor> htmlEditor = docShell->GetHTMLEditor();
  return htmlEditor.forget();
}
}

bool HTMLBodyElement::IsEventAttributeNameInternal(nsAtom* aName) {
  return nsContentUtils::IsEventAttributeName(
      aName, EventNameType_HTML | EventNameType_HTMLBodyOrFramesetOnly);
}
}

nsresult HTMLBodyElement::BindToTree(BindContext& aContext, nsINode& aParent) {
  nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
  NS_ENSURE_SUCCESS(rv, rv);
  return mAttrs.ForceMapped(this, &aContext.OwnerDoc());
  return mAttrs.ForceMapped(this, &aContext.OwnerDoc());
}

nsresult HTMLBodyElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
                                       const nsAttrValue* aValue,
                                       const nsAttrValue* aOldValue,
                                       const nsAttrValue* aOldValue,
                                       nsIPrincipal* aSubjectPrincipal,
                                       bool aNotify) {
  nsresult rv = nsGenericHTMLElement::AfterSetAttr(
      aNameSpaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
  NS_ENSURE_SUCCESS(rv, rv);
  NS_ENSURE_SUCCESS(rv, rv);
  // if the last mapped attribute was removed, don't clear the
  // nsMappedAttributes, our style can still depend on the containing frame
  // element
  if (!aValue && IsAttributeMapped(aName)) {
    nsresult rv = mAttrs.ForceMapped(this, OwnerDoc());
    nsresult rv = mAttrs.ForceMapped(this, OwnerDoc());
    NS_ENSURE_SUCCESS(rv, rv);
  }

  return NS_OK;
  return NS_OK;
}

#define EVENT(name_, id_, type_, \
              struct_) /* nothing; handled by the superclass */
// nsGenericHTMLElement::GetOnError returns
// already_AddRefed<EventHandlerNonNull> while other getters return
// already_AddRefed<EventHandlerNonNull> while other getters return
// EventHandlerNonNull*, so allow passing in the type to use here.
#define WINDOW_EVENT_HELPER(name_, type_)                              \
  type_* HTMLBodyElement::GetOn##name_() {                             \
    if (nsPIDOMWindowInner* win = OwnerDoc()->GetInnerWindow()) {      \
      nsGlobalWindowInner* globalWin = nsGlobalWindowInner::Cast(win); \
      nsGlobalWindowInner* globalWin = nsGlobalWindowInner::Cast(win); \
      return globalWin->GetOn##name_();                                \
    }                                                                  \
    return nullptr;                                                    \
  }                                                                    \
  void HTMLBodyElement::SetOn##name_(type_* handler) {                 \
    nsPIDOMWindowInner* win = OwnerDoc()->GetInnerWindow();            \
    if (!win) {                                                        \
      return;                                                          \
    }                                                                  \
    }                                                                  \
                                                                       \
    nsGlobalWindowInner* globalWin = nsGlobalWindowInner::Cast(win);   \
    return globalWin->SetOn##name_(handler);                           \
  }
#define WINDOW_EVENT(name_, id_, type_, struct_) \
#define WINDOW_EVENT(name_, id_, type_, struct_) \
  WINDOW_EVENT_HELPER(name_, EventHandlerNonNull)
#define BEFOREUNLOAD_EVENT(name_, id_, type_, struct_) \
  WINDOW_EVENT_HELPER(name_, OnBeforeUnloadEventHandlerNonNull)
#include "mozilla/EventNameList.h"  // IWYU pragma: keep
#undef BEFOREUNLOAD_EVENT
#undef BEFOREUNLOAD_EVENT
#undef WINDOW_EVENT
#undef WINDOW_EVENT_HELPER
#undef EVENT

}  // namespace dom
}  // namespace dom
}  // namespace mozilla