summaryrefslogtreecommitdiffstats
path: root/dom/xslt/xml
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /dom/xslt/xml
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'dom/xslt/xml')
-rw-r--r--dom/xslt/xml/moz.build19
-rw-r--r--dom/xslt/xml/txXMLParser.cpp62
-rw-r--r--dom/xslt/xml/txXMLParser.h26
-rw-r--r--dom/xslt/xml/txXMLUtils.cpp181
-rw-r--r--dom/xslt/xml/txXMLUtils.h82
5 files changed, 370 insertions, 0 deletions
diff --git a/dom/xslt/xml/moz.build b/dom/xslt/xml/moz.build
new file mode 100644
index 000000000..f20a27bba
--- /dev/null
+++ b/dom/xslt/xml/moz.build
@@ -0,0 +1,19 @@
+# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+UNIFIED_SOURCES += [
+ 'txXMLParser.cpp',
+ 'txXMLUtils.cpp',
+]
+
+LOCAL_INCLUDES += [
+ '../base',
+ '../xpath',
+ '../xslt',
+ '/dom/base',
+]
+
+FINAL_LIBRARY = 'xul'
diff --git a/dom/xslt/xml/txXMLParser.cpp b/dom/xslt/xml/txXMLParser.cpp
new file mode 100644
index 000000000..0cf281f2c
--- /dev/null
+++ b/dom/xslt/xml/txXMLParser.cpp
@@ -0,0 +1,62 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* 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 "txXMLParser.h"
+#include "txURIUtils.h"
+#include "txXPathTreeWalker.h"
+
+#include "nsIDocument.h"
+#include "nsIDOMDocument.h"
+#include "nsSyncLoadService.h"
+#include "nsNetUtil.h"
+#include "nsIURI.h"
+#include "nsIPrincipal.h"
+
+nsresult
+txParseDocumentFromURI(const nsAString& aHref,
+ const txXPathNode& aLoader,
+ nsAString& aErrMsg,
+ txXPathNode** aResult)
+{
+ NS_ENSURE_ARG_POINTER(aResult);
+ *aResult = nullptr;
+ nsCOMPtr<nsIURI> documentURI;
+ nsresult rv = NS_NewURI(getter_AddRefs(documentURI), aHref);
+ NS_ENSURE_SUCCESS(rv, rv);
+
+ nsIDocument* loaderDocument = txXPathNativeNode::getDocument(aLoader);
+
+ nsCOMPtr<nsILoadGroup> loadGroup = loaderDocument->GetDocumentLoadGroup();
+
+ // For the system principal loaderUri will be null here, which is good
+ // since that means that chrome documents can load any uri.
+
+ // Raw pointer, we want the resulting txXPathNode to hold a reference to
+ // the document.
+ nsIDOMDocument* theDocument = nullptr;
+ nsAutoSyncOperation sync(loaderDocument);
+ rv = nsSyncLoadService::LoadDocument(documentURI,
+ nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST,
+ loaderDocument->NodePrincipal(),
+ nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS,
+ loadGroup, true,
+ loaderDocument->GetReferrerPolicy(),
+ &theDocument);
+
+ if (NS_FAILED(rv)) {
+ aErrMsg.AppendLiteral("Document load of ");
+ aErrMsg.Append(aHref);
+ aErrMsg.AppendLiteral(" failed.");
+ return NS_FAILED(rv) ? rv : NS_ERROR_FAILURE;
+ }
+
+ *aResult = txXPathNativeNode::createXPathNode(theDocument);
+ if (!*aResult) {
+ NS_RELEASE(theDocument);
+ return NS_ERROR_FAILURE;
+ }
+
+ return NS_OK;
+}
diff --git a/dom/xslt/xml/txXMLParser.h b/dom/xslt/xml/txXMLParser.h
new file mode 100644
index 000000000..fea9defe3
--- /dev/null
+++ b/dom/xslt/xml/txXMLParser.h
@@ -0,0 +1,26 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* 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/. */
+
+#ifndef MITRE_XMLPARSER_H
+#define MITRE_XMLPARSER_H
+
+#include "txCore.h"
+
+class txXPathNode;
+
+/**
+ * API to load XML files into DOM datastructures.
+ * Parsing is either done by expat, or by expat via the syncloaderservice
+ */
+
+/**
+ * Parse a document from the aHref location, with referrer URI on behalf
+ * of the document aLoader.
+ */
+extern "C" nsresult
+txParseDocumentFromURI(const nsAString& aHref, const txXPathNode& aLoader,
+ nsAString& aErrMsg, txXPathNode** aResult);
+
+#endif
diff --git a/dom/xslt/xml/txXMLUtils.cpp b/dom/xslt/xml/txXMLUtils.cpp
new file mode 100644
index 000000000..58687568d
--- /dev/null
+++ b/dom/xslt/xml/txXMLUtils.cpp
@@ -0,0 +1,181 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* 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/. */
+
+/*
+ * XML utility classes
+ */
+
+#include "txXMLUtils.h"
+#include "nsString.h"
+#include "nsReadableUtils.h"
+#include "nsGkAtoms.h"
+#include "txStringUtils.h"
+#include "txNamespaceMap.h"
+#include "txXPathTreeWalker.h"
+#include "nsContentUtils.h"
+
+//------------------------------/
+//- Implementation of XMLUtils -/
+//------------------------------/
+
+// static
+nsresult
+XMLUtils::splitExpatName(const char16_t *aExpatName, nsIAtom **aPrefix,
+ nsIAtom **aLocalName, int32_t* aNameSpaceID)
+{
+ /**
+ * Expat can send the following:
+ * localName
+ * namespaceURI<separator>localName
+ * namespaceURI<separator>localName<separator>prefix
+ */
+
+ const char16_t *uriEnd = nullptr;
+ const char16_t *nameEnd = nullptr;
+ const char16_t *pos;
+ for (pos = aExpatName; *pos; ++pos) {
+ if (*pos == kExpatSeparatorChar) {
+ if (uriEnd) {
+ nameEnd = pos;
+ }
+ else {
+ uriEnd = pos;
+ }
+ }
+ }
+
+ const char16_t *nameStart;
+ if (uriEnd) {
+ *aNameSpaceID =
+ txNamespaceManager::getNamespaceID(nsDependentSubstring(aExpatName,
+ uriEnd));
+ if (*aNameSpaceID == kNameSpaceID_Unknown) {
+ return NS_ERROR_FAILURE;
+ }
+
+ nameStart = (uriEnd + 1);
+ if (nameEnd) {
+ const char16_t *prefixStart = nameEnd + 1;
+ *aPrefix = NS_Atomize(Substring(prefixStart, pos)).take();
+ if (!*aPrefix) {
+ return NS_ERROR_OUT_OF_MEMORY;
+ }
+ }
+ else {
+ nameEnd = pos;
+ *aPrefix = nullptr;
+ }
+ }
+ else {
+ *aNameSpaceID = kNameSpaceID_None;
+ nameStart = aExpatName;
+ nameEnd = pos;
+ *aPrefix = nullptr;
+ }
+
+ *aLocalName = NS_Atomize(Substring(nameStart, nameEnd)).take();
+
+ return *aLocalName ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
+}
+
+nsresult
+XMLUtils::splitQName(const nsAString& aName, nsIAtom** aPrefix,
+ nsIAtom** aLocalName)
+{
+ const nsAFlatString& qName = PromiseFlatString(aName);
+ const char16_t* colon;
+ bool valid = XMLUtils::isValidQName(qName, &colon);
+ if (!valid) {
+ return NS_ERROR_FAILURE;
+ }
+
+ if (colon) {
+ const char16_t *end;
+ qName.EndReading(end);
+
+ *aPrefix = NS_Atomize(Substring(qName.get(), colon)).take();
+ *aLocalName = NS_Atomize(Substring(colon + 1, end)).take();
+ }
+ else {
+ *aPrefix = nullptr;
+ *aLocalName = NS_Atomize(aName).take();
+ }
+
+ return NS_OK;
+}
+
+/**
+ * Returns true if the given string has only whitespace characters
+ */
+bool XMLUtils::isWhitespace(const nsAFlatString& aText)
+{
+ nsAFlatString::const_char_iterator start, end;
+ aText.BeginReading(start);
+ aText.EndReading(end);
+ for ( ; start != end; ++start) {
+ if (!isWhitespace(*start)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+/**
+ * Normalizes the value of a XML processing instruction
+**/
+void XMLUtils::normalizePIValue(nsAString& piValue)
+{
+ nsAutoString origValue(piValue);
+ uint32_t origLength = origValue.Length();
+ uint32_t conversionLoop = 0;
+ char16_t prevCh = 0;
+ piValue.Truncate();
+
+ while (conversionLoop < origLength) {
+ char16_t ch = origValue.CharAt(conversionLoop);
+ switch (ch) {
+ case '>':
+ {
+ if (prevCh == '?') {
+ piValue.Append(char16_t(' '));
+ }
+ break;
+ }
+ default:
+ {
+ break;
+ }
+ }
+ piValue.Append(ch);
+ prevCh = ch;
+ ++conversionLoop;
+ }
+}
+
+//static
+bool XMLUtils::isValidQName(const nsAFlatString& aQName,
+ const char16_t** aColon)
+{
+ return NS_SUCCEEDED(nsContentUtils::CheckQName(aQName, true, aColon));
+}
+
+//static
+bool XMLUtils::getXMLSpacePreserve(const txXPathNode& aNode)
+{
+ nsAutoString value;
+ txXPathTreeWalker walker(aNode);
+ do {
+ if (walker.getAttr(nsGkAtoms::space, kNameSpaceID_XML, value)) {
+ if (TX_StringEqualsAtom(value, nsGkAtoms::preserve)) {
+ return true;
+ }
+ if (TX_StringEqualsAtom(value, nsGkAtoms::_default)) {
+ return false;
+ }
+ }
+ } while (walker.moveToParent());
+
+ return false;
+}
diff --git a/dom/xslt/xml/txXMLUtils.h b/dom/xslt/xml/txXMLUtils.h
new file mode 100644
index 000000000..755d7410d
--- /dev/null
+++ b/dom/xslt/xml/txXMLUtils.h
@@ -0,0 +1,82 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* 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/. */
+
+/**
+ * An XML Utility class
+**/
+
+#ifndef MITRE_XMLUTILS_H
+#define MITRE_XMLUTILS_H
+
+#include "txCore.h"
+#include "nsDependentSubstring.h"
+#include "txXPathNode.h"
+
+#define kExpatSeparatorChar 0xFFFF
+
+extern "C" int MOZ_XMLIsLetter(const char* ptr);
+extern "C" int MOZ_XMLIsNCNameChar(const char* ptr);
+
+class nsIAtom;
+
+class XMLUtils {
+
+public:
+ static nsresult splitExpatName(const char16_t *aExpatName,
+ nsIAtom **aPrefix, nsIAtom **aLocalName,
+ int32_t* aNameSpaceID);
+ static nsresult splitQName(const nsAString& aName, nsIAtom** aPrefix,
+ nsIAtom** aLocalName);
+
+ /*
+ * Returns true if the given character is whitespace.
+ */
+ static bool isWhitespace(const char16_t& aChar)
+ {
+ return (aChar <= ' ' &&
+ (aChar == ' ' || aChar == '\r' ||
+ aChar == '\n'|| aChar == '\t'));
+ }
+
+ /**
+ * Returns true if the given string has only whitespace characters
+ */
+ static bool isWhitespace(const nsAFlatString& aText);
+
+ /**
+ * Normalizes the value of a XML processingInstruction
+ **/
+ static void normalizePIValue(nsAString& attValue);
+
+ /**
+ * Returns true if the given string is a valid XML QName
+ */
+ static bool isValidQName(const nsAFlatString& aQName,
+ const char16_t** aColon);
+
+ /**
+ * Returns true if the given character represents an Alpha letter
+ */
+ static bool isLetter(char16_t aChar)
+ {
+ return !!MOZ_XMLIsLetter(reinterpret_cast<const char*>(&aChar));
+ }
+
+ /**
+ * Returns true if the given character is an allowable NCName character
+ */
+ static bool isNCNameChar(char16_t aChar)
+ {
+ return !!MOZ_XMLIsNCNameChar(reinterpret_cast<const char*>(&aChar));
+ }
+
+ /*
+ * Walks up the document tree and returns true if the closest xml:space
+ * attribute is "preserve"
+ */
+ static bool getXMLSpacePreserve(const txXPathNode& aNode);
+};
+
+#endif