dom/xslt/base/txURIUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/base/txURIUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "txURIUtils.h"
    1.10 +#include "nsNetUtil.h"
    1.11 +#include "nsIDocument.h"
    1.12 +#include "nsIPrincipal.h"
    1.13 +
    1.14 +/**
    1.15 + * URIUtils
    1.16 + * A set of utilities for handling URIs
    1.17 +**/
    1.18 +
    1.19 +/**
    1.20 + * Resolves the given href argument, using the given documentBase
    1.21 + * if necessary.
    1.22 + * The new resolved href will be appended to the given dest String
    1.23 +**/
    1.24 +void URIUtils::resolveHref(const nsAString& href, const nsAString& base,
    1.25 +                           nsAString& dest) {
    1.26 +    if (base.IsEmpty()) {
    1.27 +        dest.Append(href);
    1.28 +        return;
    1.29 +    }
    1.30 +    if (href.IsEmpty()) {
    1.31 +        dest.Append(base);
    1.32 +        return;
    1.33 +    }
    1.34 +    nsCOMPtr<nsIURI> pURL;
    1.35 +    nsAutoString resultHref;
    1.36 +    nsresult result = NS_NewURI(getter_AddRefs(pURL), base);
    1.37 +    if (NS_SUCCEEDED(result)) {
    1.38 +        NS_MakeAbsoluteURI(resultHref, href, pURL);
    1.39 +        dest.Append(resultHref);
    1.40 +    }
    1.41 +} //-- resolveHref
    1.42 +
    1.43 +// static
    1.44 +void
    1.45 +URIUtils::ResetWithSource(nsIDocument *aNewDoc, nsIDOMNode *aSourceNode)
    1.46 +{
    1.47 +    nsCOMPtr<nsINode> node = do_QueryInterface(aSourceNode);
    1.48 +    if (!node) {
    1.49 +        // XXXbz passing nullptr as the first arg to Reset is illegal
    1.50 +        aNewDoc->Reset(nullptr, nullptr);
    1.51 +        return;
    1.52 +    }
    1.53 +
    1.54 +    nsCOMPtr<nsIDocument> sourceDoc = node->OwnerDoc();
    1.55 +    nsIPrincipal* sourcePrincipal = sourceDoc->NodePrincipal();
    1.56 +
    1.57 +    // Copy the channel and loadgroup from the source document.
    1.58 +    nsCOMPtr<nsILoadGroup> loadGroup = sourceDoc->GetDocumentLoadGroup();
    1.59 +    nsCOMPtr<nsIChannel> channel = sourceDoc->GetChannel();
    1.60 +    if (!channel) {
    1.61 +        // Need to synthesize one
    1.62 +        if (NS_FAILED(NS_NewChannel(getter_AddRefs(channel),
    1.63 +                                    sourceDoc->GetDocumentURI(),
    1.64 +                                    nullptr,
    1.65 +                                    loadGroup))) {
    1.66 +            return;
    1.67 +        }
    1.68 +        channel->SetOwner(sourcePrincipal);
    1.69 +    }
    1.70 +    aNewDoc->Reset(channel, loadGroup);
    1.71 +    aNewDoc->SetPrincipal(sourcePrincipal);
    1.72 +    aNewDoc->SetBaseURI(sourceDoc->GetDocBaseURI());
    1.73 +
    1.74 +    // Copy charset
    1.75 +    aNewDoc->SetDocumentCharacterSetSource(
    1.76 +          sourceDoc->GetDocumentCharacterSetSource());
    1.77 +    aNewDoc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet());
    1.78 +}

mercurial