Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * XML utility classes |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #include "txXMLUtils.h" |
michael@0 | 11 | #include "nsString.h" |
michael@0 | 12 | #include "nsReadableUtils.h" |
michael@0 | 13 | #include "nsGkAtoms.h" |
michael@0 | 14 | #include "txStringUtils.h" |
michael@0 | 15 | #include "txNamespaceMap.h" |
michael@0 | 16 | #include "txXPathTreeWalker.h" |
michael@0 | 17 | #include "nsContentUtils.h" |
michael@0 | 18 | |
michael@0 | 19 | nsresult |
michael@0 | 20 | txExpandedName::init(const nsAString& aQName, txNamespaceMap* aResolver, |
michael@0 | 21 | bool aUseDefault) |
michael@0 | 22 | { |
michael@0 | 23 | const nsAFlatString& qName = PromiseFlatString(aQName); |
michael@0 | 24 | const char16_t* colon; |
michael@0 | 25 | bool valid = XMLUtils::isValidQName(qName, &colon); |
michael@0 | 26 | if (!valid) { |
michael@0 | 27 | return NS_ERROR_FAILURE; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | if (colon) { |
michael@0 | 31 | nsCOMPtr<nsIAtom> prefix = do_GetAtom(Substring(qName.get(), colon)); |
michael@0 | 32 | int32_t namespaceID = aResolver->lookupNamespace(prefix); |
michael@0 | 33 | if (namespaceID == kNameSpaceID_Unknown) |
michael@0 | 34 | return NS_ERROR_FAILURE; |
michael@0 | 35 | mNamespaceID = namespaceID; |
michael@0 | 36 | |
michael@0 | 37 | const char16_t *end; |
michael@0 | 38 | qName.EndReading(end); |
michael@0 | 39 | mLocalName = do_GetAtom(Substring(colon + 1, end)); |
michael@0 | 40 | } |
michael@0 | 41 | else { |
michael@0 | 42 | mNamespaceID = aUseDefault ? aResolver->lookupNamespace(nullptr) : |
michael@0 | 43 | kNameSpaceID_None; |
michael@0 | 44 | mLocalName = do_GetAtom(aQName); |
michael@0 | 45 | } |
michael@0 | 46 | return NS_OK; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | //------------------------------/ |
michael@0 | 50 | //- Implementation of XMLUtils -/ |
michael@0 | 51 | //------------------------------/ |
michael@0 | 52 | |
michael@0 | 53 | // static |
michael@0 | 54 | nsresult |
michael@0 | 55 | XMLUtils::splitExpatName(const char16_t *aExpatName, nsIAtom **aPrefix, |
michael@0 | 56 | nsIAtom **aLocalName, int32_t* aNameSpaceID) |
michael@0 | 57 | { |
michael@0 | 58 | /** |
michael@0 | 59 | * Expat can send the following: |
michael@0 | 60 | * localName |
michael@0 | 61 | * namespaceURI<separator>localName |
michael@0 | 62 | * namespaceURI<separator>localName<separator>prefix |
michael@0 | 63 | */ |
michael@0 | 64 | |
michael@0 | 65 | const char16_t *uriEnd = nullptr; |
michael@0 | 66 | const char16_t *nameEnd = nullptr; |
michael@0 | 67 | const char16_t *pos; |
michael@0 | 68 | for (pos = aExpatName; *pos; ++pos) { |
michael@0 | 69 | if (*pos == kExpatSeparatorChar) { |
michael@0 | 70 | if (uriEnd) { |
michael@0 | 71 | nameEnd = pos; |
michael@0 | 72 | } |
michael@0 | 73 | else { |
michael@0 | 74 | uriEnd = pos; |
michael@0 | 75 | } |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | const char16_t *nameStart; |
michael@0 | 80 | if (uriEnd) { |
michael@0 | 81 | *aNameSpaceID = |
michael@0 | 82 | txNamespaceManager::getNamespaceID(nsDependentSubstring(aExpatName, |
michael@0 | 83 | uriEnd)); |
michael@0 | 84 | if (*aNameSpaceID == kNameSpaceID_Unknown) { |
michael@0 | 85 | return NS_ERROR_FAILURE; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | nameStart = (uriEnd + 1); |
michael@0 | 89 | if (nameEnd) { |
michael@0 | 90 | const char16_t *prefixStart = nameEnd + 1; |
michael@0 | 91 | *aPrefix = NS_NewAtom(Substring(prefixStart, pos)).take(); |
michael@0 | 92 | if (!*aPrefix) { |
michael@0 | 93 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | else { |
michael@0 | 97 | nameEnd = pos; |
michael@0 | 98 | *aPrefix = nullptr; |
michael@0 | 99 | } |
michael@0 | 100 | } |
michael@0 | 101 | else { |
michael@0 | 102 | *aNameSpaceID = kNameSpaceID_None; |
michael@0 | 103 | nameStart = aExpatName; |
michael@0 | 104 | nameEnd = pos; |
michael@0 | 105 | *aPrefix = nullptr; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | *aLocalName = NS_NewAtom(Substring(nameStart, nameEnd)).take(); |
michael@0 | 109 | |
michael@0 | 110 | return *aLocalName ? NS_OK : NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | nsresult |
michael@0 | 114 | XMLUtils::splitQName(const nsAString& aName, nsIAtom** aPrefix, |
michael@0 | 115 | nsIAtom** aLocalName) |
michael@0 | 116 | { |
michael@0 | 117 | const nsAFlatString& qName = PromiseFlatString(aName); |
michael@0 | 118 | const char16_t* colon; |
michael@0 | 119 | bool valid = XMLUtils::isValidQName(qName, &colon); |
michael@0 | 120 | if (!valid) { |
michael@0 | 121 | return NS_ERROR_FAILURE; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | if (colon) { |
michael@0 | 125 | const char16_t *end; |
michael@0 | 126 | qName.EndReading(end); |
michael@0 | 127 | |
michael@0 | 128 | *aPrefix = NS_NewAtom(Substring(qName.get(), colon)).take(); |
michael@0 | 129 | *aLocalName = NS_NewAtom(Substring(colon + 1, end)).take(); |
michael@0 | 130 | } |
michael@0 | 131 | else { |
michael@0 | 132 | *aPrefix = nullptr; |
michael@0 | 133 | *aLocalName = NS_NewAtom(aName).take(); |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | return NS_OK; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Returns true if the given string has only whitespace characters |
michael@0 | 141 | */ |
michael@0 | 142 | bool XMLUtils::isWhitespace(const nsAFlatString& aText) |
michael@0 | 143 | { |
michael@0 | 144 | nsAFlatString::const_char_iterator start, end; |
michael@0 | 145 | aText.BeginReading(start); |
michael@0 | 146 | aText.EndReading(end); |
michael@0 | 147 | for ( ; start != end; ++start) { |
michael@0 | 148 | if (!isWhitespace(*start)) { |
michael@0 | 149 | return false; |
michael@0 | 150 | } |
michael@0 | 151 | } |
michael@0 | 152 | return true; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | /** |
michael@0 | 156 | * Normalizes the value of a XML processing instruction |
michael@0 | 157 | **/ |
michael@0 | 158 | void XMLUtils::normalizePIValue(nsAString& piValue) |
michael@0 | 159 | { |
michael@0 | 160 | nsAutoString origValue(piValue); |
michael@0 | 161 | uint32_t origLength = origValue.Length(); |
michael@0 | 162 | uint32_t conversionLoop = 0; |
michael@0 | 163 | char16_t prevCh = 0; |
michael@0 | 164 | piValue.Truncate(); |
michael@0 | 165 | |
michael@0 | 166 | while (conversionLoop < origLength) { |
michael@0 | 167 | char16_t ch = origValue.CharAt(conversionLoop); |
michael@0 | 168 | switch (ch) { |
michael@0 | 169 | case '>': |
michael@0 | 170 | { |
michael@0 | 171 | if (prevCh == '?') { |
michael@0 | 172 | piValue.Append(char16_t(' ')); |
michael@0 | 173 | } |
michael@0 | 174 | break; |
michael@0 | 175 | } |
michael@0 | 176 | default: |
michael@0 | 177 | { |
michael@0 | 178 | break; |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | piValue.Append(ch); |
michael@0 | 182 | prevCh = ch; |
michael@0 | 183 | ++conversionLoop; |
michael@0 | 184 | } |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | //static |
michael@0 | 188 | bool XMLUtils::isValidQName(const nsAFlatString& aQName, |
michael@0 | 189 | const char16_t** aColon) |
michael@0 | 190 | { |
michael@0 | 191 | return NS_SUCCEEDED(nsContentUtils::CheckQName(aQName, true, aColon)); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | //static |
michael@0 | 195 | bool XMLUtils::getXMLSpacePreserve(const txXPathNode& aNode) |
michael@0 | 196 | { |
michael@0 | 197 | nsAutoString value; |
michael@0 | 198 | txXPathTreeWalker walker(aNode); |
michael@0 | 199 | do { |
michael@0 | 200 | if (walker.getAttr(nsGkAtoms::space, kNameSpaceID_XML, value)) { |
michael@0 | 201 | if (TX_StringEqualsAtom(value, nsGkAtoms::preserve)) { |
michael@0 | 202 | return true; |
michael@0 | 203 | } |
michael@0 | 204 | if (TX_StringEqualsAtom(value, nsGkAtoms::_default)) { |
michael@0 | 205 | return false; |
michael@0 | 206 | } |
michael@0 | 207 | } |
michael@0 | 208 | } while (walker.moveToParent()); |
michael@0 | 209 | |
michael@0 | 210 | return false; |
michael@0 | 211 | } |