1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "txXMLUtils.h"
12 #include "nsReadableUtils.h"
13 #include "nsGkAtoms.h"
14 #include "txStringUtils.h"
15 #include "txNamespaceMap.h"
16 #include "txXPathTreeWalker.h"
17 #include "nsContentUtils.h"
19 //------------------------------/
20 //- Implementation of XMLUtils -/
21 //------------------------------/
24 nsresult
XMLUtils::splitExpatName(const char16_t
* aExpatName
, nsAtom
** aPrefix
,
25 nsAtom
** aLocalName
, int32_t* aNameSpaceID
) {
27 * Expat can send the following:
29 * namespaceURI<separator>localName
30 * namespaceURI<separator>localName<separator>prefix
33 const char16_t
* uriEnd
= nullptr;
34 const char16_t
* nameEnd
= nullptr;
36 for (pos
= aExpatName
; *pos
; ++pos
) {
37 if (*pos
== kExpatSeparatorChar
) {
46 const char16_t
* nameStart
;
48 *aNameSpaceID
= txNamespaceManager::getNamespaceID(
49 nsDependentSubstring(aExpatName
, uriEnd
));
50 if (*aNameSpaceID
== kNameSpaceID_Unknown
) {
51 return NS_ERROR_FAILURE
;
54 nameStart
= (uriEnd
+ 1);
56 const char16_t
* prefixStart
= nameEnd
+ 1;
57 *aPrefix
= NS_Atomize(Substring(prefixStart
, pos
)).take();
59 return NS_ERROR_OUT_OF_MEMORY
;
66 *aNameSpaceID
= kNameSpaceID_None
;
67 nameStart
= aExpatName
;
72 *aLocalName
= NS_Atomize(Substring(nameStart
, nameEnd
)).take();
74 return *aLocalName
? NS_OK
: NS_ERROR_OUT_OF_MEMORY
;
77 nsresult
XMLUtils::splitQName(const nsAString
& aName
, nsAtom
** aPrefix
,
78 nsAtom
** aLocalName
) {
79 const char16_t
* colon
;
80 bool valid
= XMLUtils::isValidQName(aName
, &colon
);
82 return NS_ERROR_FAILURE
;
87 aName
.EndReading(end
);
89 *aPrefix
= NS_Atomize(Substring(aName
.BeginReading(), colon
)).take();
90 *aLocalName
= NS_Atomize(Substring(colon
+ 1, end
)).take();
93 *aLocalName
= NS_Atomize(aName
).take();
100 * Returns true if the given string has only whitespace characters
102 bool XMLUtils::isWhitespace(const nsAString
& aText
) {
103 nsString::const_char_iterator start
, end
;
104 aText
.BeginReading(start
);
105 aText
.EndReading(end
);
106 for (; start
!= end
; ++start
) {
107 if (!isWhitespace(*start
)) {
115 * Normalizes the value of a XML processing instruction
117 void XMLUtils::normalizePIValue(nsAString
& piValue
) {
118 nsAutoString
origValue(piValue
);
119 uint32_t origLength
= origValue
.Length();
120 uint32_t conversionLoop
= 0;
124 while (conversionLoop
< origLength
) {
125 char16_t ch
= origValue
.CharAt(conversionLoop
);
129 piValue
.Append(char16_t(' '));
144 bool XMLUtils::isValidQName(const nsAString
& aQName
, const char16_t
** aColon
) {
145 return NS_SUCCEEDED(nsContentUtils::CheckQName(aQName
, true, aColon
));
149 bool XMLUtils::getXMLSpacePreserve(const txXPathNode
& aNode
) {
151 txXPathTreeWalker
walker(aNode
);
153 if (walker
.getAttr(nsGkAtoms::space
, kNameSpaceID_XML
, value
)) {
154 if (TX_StringEqualsAtom(value
, nsGkAtoms::preserve
)) {
157 if (TX_StringEqualsAtom(value
, nsGkAtoms::_default
)) {
161 } while (walker
.moveToParent());