Bug 1453869 part 5. Remove nsIDOMParser::ParseFromBuffer. r=mrbkap

MozReview-Commit-ID: 4KuM0HRI2BC
This commit is contained in:
Boris Zbarsky 2018-04-20 23:01:25 -04:00
parent af62ed5dc8
commit 54fbc1b396
7 changed files with 27 additions and 77 deletions

View File

@ -8,6 +8,7 @@
#include "nsIDOMDocument.h"
#include "nsNetUtil.h"
#include "nsDOMString.h"
#include "nsIStreamListener.h"
#include "nsStringStream.h"
#include "nsIScriptError.h"
@ -103,57 +104,28 @@ DOMParser::ParseFromString(const nsAString& aStr, SupportedType aType,
}
already_AddRefed<nsIDocument>
DOMParser::ParseFromBuffer(const Sequence<uint8_t>& aBuf, uint32_t aBufLen,
SupportedType aType, ErrorResult& rv)
DOMParser::ParseFromBuffer(const Uint8Array& aBuf, SupportedType aType,
ErrorResult& aRv)
{
if (aBufLen > aBuf.Length()) {
rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY);
return nullptr;
}
nsCOMPtr<nsIDOMDocument> domDocument;
rv = DOMParser::ParseFromBuffer(aBuf.Elements(), aBufLen,
StringFromSupportedType(aType),
getter_AddRefs(domDocument));
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
return document.forget();
aBuf.ComputeLengthAndData();
return ParseFromBuffer(MakeSpan(aBuf.Data(), aBuf.Length()), aType, aRv);
}
already_AddRefed<nsIDocument>
DOMParser::ParseFromBuffer(const Uint8Array& aBuf, uint32_t aBufLen,
SupportedType aType, ErrorResult& rv)
DOMParser::ParseFromBuffer(Span<const uint8_t> aBuf, SupportedType aType,
ErrorResult& aRv)
{
aBuf.ComputeLengthAndData();
if (aBufLen > aBuf.Length()) {
rv.Throw(NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY);
return nullptr;
}
nsCOMPtr<nsIDOMDocument> domDocument;
rv = DOMParser::ParseFromBuffer(aBuf.Data(), aBufLen,
StringFromSupportedType(aType),
getter_AddRefs(domDocument));
nsCOMPtr<nsIDocument> document(do_QueryInterface(domDocument));
return document.forget();
}
NS_IMETHODIMP
DOMParser::ParseFromBuffer(const uint8_t *buf,
uint32_t bufLen,
const char *contentType,
nsIDOMDocument **aResult)
{
NS_ENSURE_ARG_POINTER(buf);
NS_ENSURE_ARG_POINTER(aResult);
// The new stream holds a reference to the buffer
nsCOMPtr<nsIInputStream> stream;
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
reinterpret_cast<const char *>(buf),
bufLen, NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv))
return rv;
reinterpret_cast<const char *>(aBuf.Elements()),
aBuf.Length(), NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
return ParseFromStream(stream, nullptr, bufLen, contentType, aResult);
return ParseFromStream(stream, VoidString(), aBuf.Length(), aType, aRv);
}
@ -166,7 +138,7 @@ DOMParser::ParseFromStream(nsIInputStream* aStream,
{
nsCOMPtr<nsIDOMDocument> domDocument;
rv = DOMParser::ParseFromStream(aStream,
NS_ConvertUTF16toUTF8(aCharset).get(),
DOMStringIsNull(aCharset) ? nullptr : NS_ConvertUTF16toUTF8(aCharset).get(),
aContentLength,
StringFromSupportedType(aType),
getter_AddRefs(domDocument));

View File

@ -13,6 +13,7 @@
#include "nsWeakReference.h"
#include "nsWrapperCache.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/Span.h"
#include "mozilla/dom/DOMParserBinding.h"
#include "mozilla/dom/TypedArray.h"
@ -47,15 +48,15 @@ public:
already_AddRefed<nsIDocument>
ParseFromString(const nsAString& aStr, SupportedType aType, ErrorResult& aRv);
// Sequence converts to Span, so we can use this overload for both
// the Sequence case and our internal uses.
already_AddRefed<nsIDocument>
ParseFromBuffer(const mozilla::dom::Sequence<uint8_t>& aBuf,
uint32_t aBufLen, mozilla::dom::SupportedType aType,
mozilla::ErrorResult& rv);
ParseFromBuffer(Span<const uint8_t> aBuf, SupportedType aType,
ErrorResult& aRv);
already_AddRefed<nsIDocument>
ParseFromBuffer(const mozilla::dom::Uint8Array& aBuf, uint32_t aBufLen,
mozilla::dom::SupportedType aType,
mozilla::ErrorResult& rv);
ParseFromBuffer(const Uint8Array& aBuf, SupportedType aType,
ErrorResult& aRv);
already_AddRefed<nsIDocument>
ParseFromStream(nsIInputStream* aStream, const nsAString& aCharset,

View File

@ -22,19 +22,6 @@ interface nsIGlobalObject;
[shim(DOMParser), uuid(70b9600e-8622-4c93-9ad8-22c28058dc44)]
interface nsIDOMParser : nsISupports
{
/**
* The buffer is parsed into a DOM document.
* The charset is determined from the xml entity decl.
*
* @param buf The octet array data to be parsed
* @param bufLen Length (in bytes) of the data
* @param contentType The content type of the data (see parseFromStream)
* @returns The DOM document created as a result of parsing the
* string
*/
nsIDOMDocument parseFromBuffer([const,array,size_is(bufLen)] in octet buf,
in uint32_t bufLen, in string contentType);
/**
* The byte stream passed in is parsed into a DOM document.
*

View File

@ -93,13 +93,8 @@ function runTest(parser, serializer) {
];
for (let input of inputs) {
let a = input.array;
is(serializer.serializeToString(parser.parseFromBuffer(a, a.length, t.type)), t.expected,
is(serializer.serializeToString(parser.parseFromBuffer(a, t.type)), t.expected,
input.name + " test for " + t.type);
throws(function() {
parser.parseFromBuffer(a, a.length + 1, t.type);
}, "NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY",
input.name + " should throw if bufLen parameter is greater than actual length"
);
}
let istream = Cc["@mozilla.org/io/string-input-stream;1"].

View File

@ -25,14 +25,10 @@ interface DOMParser {
Document parseFromString(DOMString str, SupportedType type);
// Mozilla-specific stuff
// Throws if the passed-in length is greater than the actual sequence length
[NewObject, Throws, ChromeOnly]
Document parseFromBuffer(sequence<octet> buf, unsigned long bufLen,
SupportedType type);
// Throws if the passed-in length is greater than the actual typed array length
Document parseFromBuffer(sequence<octet> buf, SupportedType type);
[NewObject, Throws, ChromeOnly]
Document parseFromBuffer(Uint8Array buf, unsigned long bufLen,
SupportedType type);
Document parseFromBuffer(Uint8Array buf, SupportedType type);
[NewObject, Throws, ChromeOnly]
Document parseFromStream(InputStream stream, DOMString? charset,
long contentLength, SupportedType type);

View File

@ -1560,7 +1560,7 @@ Engine.prototype = {
var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
createInstance(Ci.nsIDOMParser);
var doc = parser.parseFromBuffer(aBytes, aBytes.length, "text/xml");
var doc = parser.parseFromBuffer(aBytes, "text/xml");
aEngine._data = doc.documentElement;
try {

View File

@ -139,8 +139,7 @@ var PropertyListUtils = Object.freeze({
createInstance(Ci.nsIDOMParser);
let bytesView = new Uint8Array(aBuffer);
try {
let doc = domParser.parseFromBuffer(bytesView, bytesView.length,
"application/xml");
let doc = domParser.parseFromBuffer(bytesView, "application/xml");
return new XMLPropertyListReader(doc).root;
} catch (ex) {
throw new Error("aBuffer cannot be parsed as a DOM document: " + ex);