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 (882de07e4cbe)

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
/* -*- 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
 * 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/. */
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/HTMLSharedElement.h"
#include "mozilla/dom/BindContext.h"
#include "mozilla/dom/HTMLBaseElementBinding.h"
#include "mozilla/dom/HTMLDirectoryElementBinding.h"
#include "mozilla/dom/HTMLDirectoryElementBinding.h"
#include "mozilla/dom/HTMLHeadElementBinding.h"
#include "mozilla/dom/HTMLHtmlElementBinding.h"
#include "mozilla/dom/HTMLParamElementBinding.h"
#include "mozilla/dom/HTMLQuoteElementBinding.h"


#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/MappedDeclarations.h"
#include "nsAttrValueInlines.h"
#include "nsStyleConsts.h"
#include "nsMappedAttributes.h"
#include "nsMappedAttributes.h"
#include "nsContentUtils.h"
#include "nsIContentSecurityPolicy.h"
#include "nsIURI.h"

NS_IMPL_NS_NEW_HTML_ELEMENT(Shared)
NS_IMPL_NS_NEW_HTML_ELEMENT(Shared)

namespace mozilla {
namespace dom {

extern nsAttrValue::EnumTable kListTypeTable[];

HTMLSharedElement::~HTMLSharedElement() = default;


NS_IMPL_ELEMENT_CLONE(HTMLSharedElement)

void HTMLSharedElement::GetHref(nsAString& aValue) {
  MOZ_ASSERT(mNodeInfo->Equals(nsGkAtoms::base),
             "This should only get called for <base> elements");
             "This should only get called for <base> elements");
  nsAutoString href;
  GetAttr(kNameSpaceID_None, nsGkAtoms::href, href);

  nsCOMPtr<nsIURI> uri;
  Document* doc = OwnerDoc();
  Document* doc = OwnerDoc();
  nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri), href, doc,
                                            doc->GetFallbackBaseURI());

  if (!uri) {
    aValue = href;
    aValue = href;
    return;
  }

  nsAutoCString spec;
  nsAutoCString spec;
  uri->GetSpec(spec);
  CopyUTF8toUTF16(spec, aValue);
}

void HTMLSharedElement::DoneAddingChildren(bool aHaveNotified) {
void HTMLSharedElement::DoneAddingChildren(bool aHaveNotified) {
  if (mNodeInfo->Equals(nsGkAtoms::head)) {
    nsCOMPtr<Document> doc = GetUncomposedDoc();
    if (doc) {
      doc->OnL10nResourceContainerParsed();
    }
    }

    RefPtr<AsyncEventDispatcher> asyncDispatcher = new AsyncEventDispatcher(
        this, NS_LITERAL_STRING("DOMHeadElementParsed"), CanBubble::eYes,
        ChromeOnlyDispatch::eYes);
    // Always run async in order to avoid running script when the content
    // Always run async in order to avoid running script when the content
    // sink isn't expecting it.
    asyncDispatcher->PostDOMEvent();
  }
}

bool HTMLSharedElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
                                       const nsAString& aValue,
                                       const nsAString& aValue,
                                       nsIPrincipal* aMaybeScriptedPrincipal,
                                       nsAttrValue& aResult) {
  if (aNamespaceID == kNameSpaceID_None && mNodeInfo->Equals(nsGkAtoms::dir)) {
    if (aAttribute == nsGkAtoms::type) {
    if (aAttribute == nsGkAtoms::type) {
      return aResult.ParseEnumValue(aValue, mozilla::dom::kListTypeTable,
                                    false);
    }
    if (aAttribute == nsGkAtoms::start) {
      return aResult.ParseIntWithBounds(aValue, 1);
      return aResult.ParseIntWithBounds(aValue, 1);
    }
  }

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

static void DirectoryMapAttributesIntoRule(
    const nsMappedAttributes* aAttributes, MappedDeclarations& aDecls) {
  if (!aDecls.PropertyIsSet(eCSSProperty_list_style_type)) {
  if (!aDecls.PropertyIsSet(eCSSProperty_list_style_type)) {
    // type: enum
    const nsAttrValue* value = aAttributes->GetAttr(nsGkAtoms::type);
    if (value) {
      if (value->Type() == nsAttrValue::eEnum) {
        aDecls.SetKeywordValue(eCSSProperty_list_style_type,
        aDecls.SetKeywordValue(eCSSProperty_list_style_type,
                               value->GetEnumValue());
      } else {
        aDecls.SetKeywordValue(eCSSProperty_list_style_type,
                               NS_STYLE_LIST_STYLE_DISC);
      }
      }
    }
  }

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

NS_IMETHODIMP_(bool)
HTMLSharedElement::IsAttributeMapped(const nsAtom* aAttribute) const {
  if (mNodeInfo->Equals(nsGkAtoms::dir)) {
    static const MappedAttributeEntry attributes[] = {
    static const MappedAttributeEntry attributes[] = {
        {nsGkAtoms::type},
        // { nsGkAtoms::compact }, // XXX
        {nullptr}};

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

    return FindAttributeDependence(aAttribute, map);
    return FindAttributeDependence(aAttribute, map);
  }

  return nsGenericHTMLElement::IsAttributeMapped(aAttribute);
}


static void SetBaseURIUsingFirstBaseWithHref(Document* aDocument,
                                             nsIContent* aMustMatch) {
  MOZ_ASSERT(aDocument, "Need a document!");

  for (nsIContent* child = aDocument->GetFirstChild(); child;
  for (nsIContent* child = aDocument->GetFirstChild(); child;
       child = child->GetNextNode()) {
    if (child->IsHTMLElement(nsGkAtoms::base) &&
        child->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
      if (aMustMatch && child != aMustMatch) {
        return;
      }


      // Resolve the <base> element's href relative to our document's
      // fallback base URI.
      nsAutoString href;
      child->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::href, href);


      nsCOMPtr<nsIURI> newBaseURI;
      nsContentUtils::NewURIWithDocumentCharset(
          getter_AddRefs(newBaseURI), href, aDocument,
          aDocument->GetFallbackBaseURI());

      // Check if CSP allows this base-uri
      nsresult rv = NS_OK;
      nsresult rv = NS_OK;
      nsCOMPtr<nsIContentSecurityPolicy> csp = aDocument->GetCsp();
      if (csp && newBaseURI) {
        // base-uri is only enforced if explicitly defined in the
        // policy - do *not* consult default-src, see:
        // http://www.w3.org/TR/CSP2/#directive-default-src
        // http://www.w3.org/TR/CSP2/#directive-default-src
        bool cspPermitsBaseURI = true;
        rv = csp->Permits(child->AsElement(), nullptr /* nsICSPEventListener */,
                          newBaseURI,
                          nsIContentSecurityPolicy::BASE_URI_DIRECTIVE, true,
                          nsIContentSecurityPolicy::BASE_URI_DIRECTIVE, true,
                          &cspPermitsBaseURI);
        if (NS_FAILED(rv) || !cspPermitsBaseURI) {
          newBaseURI = nullptr;
        }
      }
      }
      aDocument->SetBaseURI(newBaseURI);
      aDocument->SetChromeXHRDocBaseURI(nullptr);
      return;
    }
  }
  }

  aDocument->SetBaseURI(nullptr);
}

static void SetBaseTargetUsingFirstBaseWithTarget(Document* aDocument,
static void SetBaseTargetUsingFirstBaseWithTarget(Document* aDocument,
                                                  nsIContent* aMustMatch) {
  MOZ_ASSERT(aDocument, "Need a document!");

  for (nsIContent* child = aDocument->GetFirstChild(); child;
       child = child->GetNextNode()) {
       child = child->GetNextNode()) {
    if (child->IsHTMLElement(nsGkAtoms::base) &&
        child->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::target)) {
      if (aMustMatch && child != aMustMatch) {
        return;
      }
      }

      nsString target;
      child->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::target, target);
      child->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::target, target);
      aDocument->SetBaseTarget(target);
      return;
    }
  }


  aDocument->SetBaseTarget(EmptyString());
}

nsresult HTMLSharedElement::AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
                                         const nsAttrValue* aValue,
                                         const nsAttrValue* aValue,
                                         const nsAttrValue* aOldValue,
                                         nsIPrincipal* aSubjectPrincipal,
                                         bool aNotify) {
  if (aNamespaceID == kNameSpaceID_None) {
    if (aName == nsGkAtoms::href) {
    if (aName == nsGkAtoms::href) {
      // If the href attribute of a <base> tag is changing, we may need to
      // update the document's base URI, which will cause all the links on the
      // page to be re-resolved given the new base.
      // If the href is being unset (aValue is null), we will need to find a new
      // <base>.
      // <base>.
      if (mNodeInfo->Equals(nsGkAtoms::base) && IsInUncomposedDoc()) {
        SetBaseURIUsingFirstBaseWithHref(GetUncomposedDoc(),
                                         aValue ? this : nullptr);
      }
    } else if (aName == nsGkAtoms::target) {
      // The target attribute is in pretty much the same situation as the href
      // The target attribute is in pretty much the same situation as the href
      // attribute, above.
      if (mNodeInfo->Equals(nsGkAtoms::base) && IsInUncomposedDoc()) {
        SetBaseTargetUsingFirstBaseWithTarget(GetUncomposedDoc(),
                                              aValue ? this : nullptr);
      }
      }
    }
  }

  return nsGenericHTMLElement::AfterSetAttr(
      aNamespaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
}

nsresult HTMLSharedElement::BindToTree(BindContext& aContext,
nsresult HTMLSharedElement::BindToTree(BindContext& aContext,
                                       nsINode& aParent) {
  nsresult rv = nsGenericHTMLElement::BindToTree(aContext, aParent);
  NS_ENSURE_SUCCESS(rv, rv);

  // The document stores a pointer to its base URI and base target, which we may
  // need to update here.
  if (mNodeInfo->Equals(nsGkAtoms::base) && IsInUncomposedDoc()) {
  if (mNodeInfo->Equals(nsGkAtoms::base) && IsInUncomposedDoc()) {
    if (HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
      SetBaseURIUsingFirstBaseWithHref(&aContext.OwnerDoc(), this);
    }
    if (HasAttr(kNameSpaceID_None, nsGkAtoms::target)) {
      SetBaseTargetUsingFirstBaseWithTarget(&aContext.OwnerDoc(), this);
      SetBaseTargetUsingFirstBaseWithTarget(&aContext.OwnerDoc(), this);
    }
  }

  return NS_OK;
}
}

void HTMLSharedElement::UnbindFromTree(bool aNullParent) {
  Document* doc = GetUncomposedDoc();

  nsGenericHTMLElement::UnbindFromTree(aNullParent);
  nsGenericHTMLElement::UnbindFromTree(aNullParent);

  // If we're removing a <base> from a document, we may need to update the
  // document's base URI and base target
  if (doc && mNodeInfo->Equals(nsGkAtoms::base)) {
    if (HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
    if (HasAttr(kNameSpaceID_None, nsGkAtoms::href)) {
      SetBaseURIUsingFirstBaseWithHref(doc, nullptr);
    }
    if (HasAttr(kNameSpaceID_None, nsGkAtoms::target)) {
      SetBaseTargetUsingFirstBaseWithTarget(doc, nullptr);
      SetBaseTargetUsingFirstBaseWithTarget(doc, nullptr);
    }
  }
}

nsMapRuleToAttributesFunc HTMLSharedElement::GetAttributeMappingFunction()
    const {
    const {
  if (mNodeInfo->Equals(nsGkAtoms::dir)) {
    return &DirectoryMapAttributesIntoRule;
  }

  return nsGenericHTMLElement::GetAttributeMappingFunction();
  return nsGenericHTMLElement::GetAttributeMappingFunction();
}

JSObject* HTMLSharedElement::WrapNode(JSContext* aCx,
                                      JS::Handle<JSObject*> aGivenProto) {
  if (mNodeInfo->Equals(nsGkAtoms::param)) {
  if (mNodeInfo->Equals(nsGkAtoms::param)) {
    return HTMLParamElement_Binding::Wrap(aCx, this, aGivenProto);
  }
  if (mNodeInfo->Equals(nsGkAtoms::base)) {
    return HTMLBaseElement_Binding::Wrap(aCx, this, aGivenProto);
  }
  }
  if (mNodeInfo->Equals(nsGkAtoms::dir)) {
    return HTMLDirectoryElement_Binding::Wrap(aCx, this, aGivenProto);
  }
  if (mNodeInfo->Equals(nsGkAtoms::q) ||
      mNodeInfo->Equals(nsGkAtoms::blockquote)) {
      mNodeInfo->Equals(nsGkAtoms::blockquote)) {
    return HTMLQuoteElement_Binding::Wrap(aCx, this, aGivenProto);
  }
  if (mNodeInfo->Equals(nsGkAtoms::head)) {
    return HTMLHeadElement_Binding::Wrap(aCx, this, aGivenProto);
  }
  }
  MOZ_ASSERT(mNodeInfo->Equals(nsGkAtoms::html));
  return HTMLHtmlElement_Binding::Wrap(aCx, this, aGivenProto);
}

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