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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
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 | #include "nsExpatDriver.h" |
michael@0 | 7 | #include "nsCOMPtr.h" |
michael@0 | 8 | #include "nsParserCIID.h" |
michael@0 | 9 | #include "CParserContext.h" |
michael@0 | 10 | #include "nsIExpatSink.h" |
michael@0 | 11 | #include "nsIExtendedExpatSink.h" |
michael@0 | 12 | #include "nsIContentSink.h" |
michael@0 | 13 | #include "nsParserMsgUtils.h" |
michael@0 | 14 | #include "nsIURL.h" |
michael@0 | 15 | #include "nsIUnicharInputStream.h" |
michael@0 | 16 | #include "nsISimpleUnicharStreamFactory.h" |
michael@0 | 17 | #include "nsNetUtil.h" |
michael@0 | 18 | #include "prprf.h" |
michael@0 | 19 | #include "prmem.h" |
michael@0 | 20 | #include "nsTextFormatter.h" |
michael@0 | 21 | #include "nsDirectoryServiceDefs.h" |
michael@0 | 22 | #include "nsCRT.h" |
michael@0 | 23 | #include "nsIConsoleService.h" |
michael@0 | 24 | #include "nsIScriptError.h" |
michael@0 | 25 | #include "nsIContentPolicy.h" |
michael@0 | 26 | #include "nsContentPolicyUtils.h" |
michael@0 | 27 | #include "nsError.h" |
michael@0 | 28 | #include "nsXPCOMCIDInternal.h" |
michael@0 | 29 | #include "nsUnicharInputStream.h" |
michael@0 | 30 | |
michael@0 | 31 | #define kExpatSeparatorChar 0xFFFF |
michael@0 | 32 | |
michael@0 | 33 | static const char16_t kUTF16[] = { 'U', 'T', 'F', '-', '1', '6', '\0' }; |
michael@0 | 34 | |
michael@0 | 35 | #ifdef PR_LOGGING |
michael@0 | 36 | static PRLogModuleInfo * |
michael@0 | 37 | GetExpatDriverLog() |
michael@0 | 38 | { |
michael@0 | 39 | static PRLogModuleInfo *sLog; |
michael@0 | 40 | if (!sLog) |
michael@0 | 41 | sLog = PR_NewLogModule("expatdriver"); |
michael@0 | 42 | return sLog; |
michael@0 | 43 | } |
michael@0 | 44 | #endif |
michael@0 | 45 | |
michael@0 | 46 | /***************************** EXPAT CALL BACKS ******************************/ |
michael@0 | 47 | // The callback handlers that get called from the expat parser. |
michael@0 | 48 | |
michael@0 | 49 | static void |
michael@0 | 50 | Driver_HandleXMLDeclaration(void *aUserData, |
michael@0 | 51 | const XML_Char *aVersion, |
michael@0 | 52 | const XML_Char *aEncoding, |
michael@0 | 53 | int aStandalone) |
michael@0 | 54 | { |
michael@0 | 55 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 56 | if (aUserData) { |
michael@0 | 57 | nsExpatDriver* driver = static_cast<nsExpatDriver*>(aUserData); |
michael@0 | 58 | driver->HandleXMLDeclaration(aVersion, aEncoding, aStandalone); |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | static void |
michael@0 | 63 | Driver_HandleStartElement(void *aUserData, |
michael@0 | 64 | const XML_Char *aName, |
michael@0 | 65 | const XML_Char **aAtts) |
michael@0 | 66 | { |
michael@0 | 67 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 68 | if (aUserData) { |
michael@0 | 69 | static_cast<nsExpatDriver*>(aUserData)->HandleStartElement(aName, |
michael@0 | 70 | aAtts); |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | static void |
michael@0 | 75 | Driver_HandleEndElement(void *aUserData, |
michael@0 | 76 | const XML_Char *aName) |
michael@0 | 77 | { |
michael@0 | 78 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 79 | if (aUserData) { |
michael@0 | 80 | static_cast<nsExpatDriver*>(aUserData)->HandleEndElement(aName); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | static void |
michael@0 | 85 | Driver_HandleCharacterData(void *aUserData, |
michael@0 | 86 | const XML_Char *aData, |
michael@0 | 87 | int aLength) |
michael@0 | 88 | { |
michael@0 | 89 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 90 | if (aUserData) { |
michael@0 | 91 | nsExpatDriver* driver = static_cast<nsExpatDriver*>(aUserData); |
michael@0 | 92 | driver->HandleCharacterData(aData, uint32_t(aLength)); |
michael@0 | 93 | } |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | static void |
michael@0 | 97 | Driver_HandleComment(void *aUserData, |
michael@0 | 98 | const XML_Char *aName) |
michael@0 | 99 | { |
michael@0 | 100 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 101 | if(aUserData) { |
michael@0 | 102 | static_cast<nsExpatDriver*>(aUserData)->HandleComment(aName); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | static void |
michael@0 | 107 | Driver_HandleProcessingInstruction(void *aUserData, |
michael@0 | 108 | const XML_Char *aTarget, |
michael@0 | 109 | const XML_Char *aData) |
michael@0 | 110 | { |
michael@0 | 111 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 112 | if (aUserData) { |
michael@0 | 113 | nsExpatDriver* driver = static_cast<nsExpatDriver*>(aUserData); |
michael@0 | 114 | driver->HandleProcessingInstruction(aTarget, aData); |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | static void |
michael@0 | 119 | Driver_HandleDefault(void *aUserData, |
michael@0 | 120 | const XML_Char *aData, |
michael@0 | 121 | int aLength) |
michael@0 | 122 | { |
michael@0 | 123 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 124 | if (aUserData) { |
michael@0 | 125 | nsExpatDriver* driver = static_cast<nsExpatDriver*>(aUserData); |
michael@0 | 126 | driver->HandleDefault(aData, uint32_t(aLength)); |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | static void |
michael@0 | 131 | Driver_HandleStartCdataSection(void *aUserData) |
michael@0 | 132 | { |
michael@0 | 133 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 134 | if (aUserData) { |
michael@0 | 135 | static_cast<nsExpatDriver*>(aUserData)->HandleStartCdataSection(); |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | static void |
michael@0 | 140 | Driver_HandleEndCdataSection(void *aUserData) |
michael@0 | 141 | { |
michael@0 | 142 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 143 | if (aUserData) { |
michael@0 | 144 | static_cast<nsExpatDriver*>(aUserData)->HandleEndCdataSection(); |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | static void |
michael@0 | 149 | Driver_HandleStartDoctypeDecl(void *aUserData, |
michael@0 | 150 | const XML_Char *aDoctypeName, |
michael@0 | 151 | const XML_Char *aSysid, |
michael@0 | 152 | const XML_Char *aPubid, |
michael@0 | 153 | int aHasInternalSubset) |
michael@0 | 154 | { |
michael@0 | 155 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 156 | if (aUserData) { |
michael@0 | 157 | static_cast<nsExpatDriver*>(aUserData)-> |
michael@0 | 158 | HandleStartDoctypeDecl(aDoctypeName, aSysid, aPubid, !!aHasInternalSubset); |
michael@0 | 159 | } |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | static void |
michael@0 | 163 | Driver_HandleEndDoctypeDecl(void *aUserData) |
michael@0 | 164 | { |
michael@0 | 165 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 166 | if (aUserData) { |
michael@0 | 167 | static_cast<nsExpatDriver*>(aUserData)->HandleEndDoctypeDecl(); |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | static int |
michael@0 | 172 | Driver_HandleExternalEntityRef(void *aExternalEntityRefHandler, |
michael@0 | 173 | const XML_Char *aOpenEntityNames, |
michael@0 | 174 | const XML_Char *aBase, |
michael@0 | 175 | const XML_Char *aSystemId, |
michael@0 | 176 | const XML_Char *aPublicId) |
michael@0 | 177 | { |
michael@0 | 178 | NS_ASSERTION(aExternalEntityRefHandler, "expat driver should exist"); |
michael@0 | 179 | if (!aExternalEntityRefHandler) { |
michael@0 | 180 | return 1; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | nsExpatDriver* driver = static_cast<nsExpatDriver*> |
michael@0 | 184 | (aExternalEntityRefHandler); |
michael@0 | 185 | |
michael@0 | 186 | return driver->HandleExternalEntityRef(aOpenEntityNames, aBase, aSystemId, |
michael@0 | 187 | aPublicId); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | static void |
michael@0 | 191 | Driver_HandleStartNamespaceDecl(void *aUserData, |
michael@0 | 192 | const XML_Char *aPrefix, |
michael@0 | 193 | const XML_Char *aUri) |
michael@0 | 194 | { |
michael@0 | 195 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 196 | if (aUserData) { |
michael@0 | 197 | static_cast<nsExpatDriver*>(aUserData)-> |
michael@0 | 198 | HandleStartNamespaceDecl(aPrefix, aUri); |
michael@0 | 199 | } |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | static void |
michael@0 | 203 | Driver_HandleEndNamespaceDecl(void *aUserData, |
michael@0 | 204 | const XML_Char *aPrefix) |
michael@0 | 205 | { |
michael@0 | 206 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 207 | if (aUserData) { |
michael@0 | 208 | static_cast<nsExpatDriver*>(aUserData)-> |
michael@0 | 209 | HandleEndNamespaceDecl(aPrefix); |
michael@0 | 210 | } |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | static void |
michael@0 | 214 | Driver_HandleNotationDecl(void *aUserData, |
michael@0 | 215 | const XML_Char *aNotationName, |
michael@0 | 216 | const XML_Char *aBase, |
michael@0 | 217 | const XML_Char *aSysid, |
michael@0 | 218 | const XML_Char *aPubid) |
michael@0 | 219 | { |
michael@0 | 220 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 221 | if (aUserData) { |
michael@0 | 222 | static_cast<nsExpatDriver*>(aUserData)-> |
michael@0 | 223 | HandleNotationDecl(aNotationName, aBase, aSysid, aPubid); |
michael@0 | 224 | } |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | static void |
michael@0 | 228 | Driver_HandleUnparsedEntityDecl(void *aUserData, |
michael@0 | 229 | const XML_Char *aEntityName, |
michael@0 | 230 | const XML_Char *aBase, |
michael@0 | 231 | const XML_Char *aSysid, |
michael@0 | 232 | const XML_Char *aPubid, |
michael@0 | 233 | const XML_Char *aNotationName) |
michael@0 | 234 | { |
michael@0 | 235 | NS_ASSERTION(aUserData, "expat driver should exist"); |
michael@0 | 236 | if (aUserData) { |
michael@0 | 237 | static_cast<nsExpatDriver*>(aUserData)-> |
michael@0 | 238 | HandleUnparsedEntityDecl(aEntityName, aBase, aSysid, aPubid, |
michael@0 | 239 | aNotationName); |
michael@0 | 240 | } |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | |
michael@0 | 244 | /***************************** END CALL BACKS ********************************/ |
michael@0 | 245 | |
michael@0 | 246 | /***************************** CATALOG UTILS *********************************/ |
michael@0 | 247 | |
michael@0 | 248 | // Initially added for bug 113400 to switch from the remote "XHTML 1.0 plus |
michael@0 | 249 | // MathML 2.0" DTD to the the lightweight customized version that Mozilla uses. |
michael@0 | 250 | // Since Mozilla is not validating, no need to fetch a *huge* file at each |
michael@0 | 251 | // click. |
michael@0 | 252 | // XXX The cleanest solution here would be to fix Bug 98413: Implement XML |
michael@0 | 253 | // Catalogs. |
michael@0 | 254 | struct nsCatalogData { |
michael@0 | 255 | const char* mPublicID; |
michael@0 | 256 | const char* mLocalDTD; |
michael@0 | 257 | const char* mAgentSheet; |
michael@0 | 258 | }; |
michael@0 | 259 | |
michael@0 | 260 | // The order of this table is guestimated to be in the optimum order |
michael@0 | 261 | static const nsCatalogData kCatalogTable[] = { |
michael@0 | 262 | { "-//W3C//DTD XHTML 1.0 Transitional//EN", "htmlmathml-f.ent", nullptr }, |
michael@0 | 263 | { "-//W3C//DTD XHTML 1.1//EN", "htmlmathml-f.ent", nullptr }, |
michael@0 | 264 | { "-//W3C//DTD XHTML 1.0 Strict//EN", "htmlmathml-f.ent", nullptr }, |
michael@0 | 265 | { "-//W3C//DTD XHTML 1.0 Frameset//EN", "htmlmathml-f.ent", nullptr }, |
michael@0 | 266 | { "-//W3C//DTD XHTML Basic 1.0//EN", "htmlmathml-f.ent", nullptr }, |
michael@0 | 267 | { "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN", "htmlmathml-f.ent", "resource://gre-resources/mathml.css" }, |
michael@0 | 268 | { "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN", "htmlmathml-f.ent", "resource://gre-resources/mathml.css" }, |
michael@0 | 269 | { "-//W3C//DTD MathML 2.0//EN", "htmlmathml-f.ent", "resource://gre-resources/mathml.css" }, |
michael@0 | 270 | { "-//WAPFORUM//DTD XHTML Mobile 1.0//EN", "htmlmathml-f.ent", nullptr }, |
michael@0 | 271 | { nullptr, nullptr, nullptr } |
michael@0 | 272 | }; |
michael@0 | 273 | |
michael@0 | 274 | static const nsCatalogData* |
michael@0 | 275 | LookupCatalogData(const char16_t* aPublicID) |
michael@0 | 276 | { |
michael@0 | 277 | nsDependentString publicID(aPublicID); |
michael@0 | 278 | |
michael@0 | 279 | // linear search for now since the number of entries is going to |
michael@0 | 280 | // be negligible, and the fix for bug 98413 would get rid of this |
michael@0 | 281 | // code anyway |
michael@0 | 282 | const nsCatalogData* data = kCatalogTable; |
michael@0 | 283 | while (data->mPublicID) { |
michael@0 | 284 | if (publicID.EqualsASCII(data->mPublicID)) { |
michael@0 | 285 | return data; |
michael@0 | 286 | } |
michael@0 | 287 | ++data; |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | return nullptr; |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | // This function provides a resource URI to a local DTD |
michael@0 | 294 | // in resource://gre/res/dtd/ which may or may not exist. |
michael@0 | 295 | // If aCatalogData is provided, it is used to remap the |
michael@0 | 296 | // DTD instead of taking the filename from the URI. |
michael@0 | 297 | static void |
michael@0 | 298 | GetLocalDTDURI(const nsCatalogData* aCatalogData, nsIURI* aDTD, |
michael@0 | 299 | nsIURI** aResult) |
michael@0 | 300 | { |
michael@0 | 301 | NS_ASSERTION(aDTD, "Null parameter."); |
michael@0 | 302 | |
michael@0 | 303 | nsAutoCString fileName; |
michael@0 | 304 | if (aCatalogData) { |
michael@0 | 305 | // remap the DTD to a known local DTD |
michael@0 | 306 | fileName.Assign(aCatalogData->mLocalDTD); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | if (fileName.IsEmpty()) { |
michael@0 | 310 | // Try to see if the user has installed the DTD file -- we extract the |
michael@0 | 311 | // filename.ext of the DTD here. Hence, for any DTD for which we have |
michael@0 | 312 | // no predefined mapping, users just have to copy the DTD file to our |
michael@0 | 313 | // special DTD directory and it will be picked. |
michael@0 | 314 | nsCOMPtr<nsIURL> dtdURL = do_QueryInterface(aDTD); |
michael@0 | 315 | if (!dtdURL) { |
michael@0 | 316 | return; |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | dtdURL->GetFileName(fileName); |
michael@0 | 320 | if (fileName.IsEmpty()) { |
michael@0 | 321 | return; |
michael@0 | 322 | } |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | nsAutoCString respath("resource://gre/res/dtd/"); |
michael@0 | 326 | respath += fileName; |
michael@0 | 327 | NS_NewURI(aResult, respath); |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | /***************************** END CATALOG UTILS *****************************/ |
michael@0 | 331 | |
michael@0 | 332 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsExpatDriver) |
michael@0 | 333 | NS_INTERFACE_MAP_ENTRY(nsITokenizer) |
michael@0 | 334 | NS_INTERFACE_MAP_ENTRY(nsIDTD) |
michael@0 | 335 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDTD) |
michael@0 | 336 | NS_INTERFACE_MAP_END |
michael@0 | 337 | |
michael@0 | 338 | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsExpatDriver) |
michael@0 | 339 | NS_IMPL_CYCLE_COLLECTING_RELEASE(nsExpatDriver) |
michael@0 | 340 | |
michael@0 | 341 | NS_IMPL_CYCLE_COLLECTION(nsExpatDriver, mSink, mExtendedSink) |
michael@0 | 342 | |
michael@0 | 343 | nsExpatDriver::nsExpatDriver() |
michael@0 | 344 | : mExpatParser(nullptr), |
michael@0 | 345 | mInCData(false), |
michael@0 | 346 | mInInternalSubset(false), |
michael@0 | 347 | mInExternalDTD(false), |
michael@0 | 348 | mMadeFinalCallToExpat(false), |
michael@0 | 349 | mIsFinalChunk(false), |
michael@0 | 350 | mInternalState(NS_OK), |
michael@0 | 351 | mExpatBuffered(0), |
michael@0 | 352 | mCatalogData(nullptr), |
michael@0 | 353 | mInnerWindowID(0) |
michael@0 | 354 | { |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | nsExpatDriver::~nsExpatDriver() |
michael@0 | 358 | { |
michael@0 | 359 | if (mExpatParser) { |
michael@0 | 360 | XML_ParserFree(mExpatParser); |
michael@0 | 361 | } |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | nsresult |
michael@0 | 365 | nsExpatDriver::HandleStartElement(const char16_t *aValue, |
michael@0 | 366 | const char16_t **aAtts) |
michael@0 | 367 | { |
michael@0 | 368 | NS_ASSERTION(mSink, "content sink not found!"); |
michael@0 | 369 | |
michael@0 | 370 | // Calculate the total number of elements in aAtts. |
michael@0 | 371 | // XML_GetSpecifiedAttributeCount will only give us the number of specified |
michael@0 | 372 | // attrs (twice that number, actually), so we have to check for default attrs |
michael@0 | 373 | // ourselves. |
michael@0 | 374 | uint32_t attrArrayLength; |
michael@0 | 375 | for (attrArrayLength = XML_GetSpecifiedAttributeCount(mExpatParser); |
michael@0 | 376 | aAtts[attrArrayLength]; |
michael@0 | 377 | attrArrayLength += 2) { |
michael@0 | 378 | // Just looping till we find out what the length is |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | if (mSink) { |
michael@0 | 382 | nsresult rv = mSink-> |
michael@0 | 383 | HandleStartElement(aValue, aAtts, attrArrayLength, |
michael@0 | 384 | XML_GetIdAttributeIndex(mExpatParser), |
michael@0 | 385 | XML_GetCurrentLineNumber(mExpatParser)); |
michael@0 | 386 | MaybeStopParser(rv); |
michael@0 | 387 | } |
michael@0 | 388 | |
michael@0 | 389 | return NS_OK; |
michael@0 | 390 | } |
michael@0 | 391 | |
michael@0 | 392 | nsresult |
michael@0 | 393 | nsExpatDriver::HandleEndElement(const char16_t *aValue) |
michael@0 | 394 | { |
michael@0 | 395 | NS_ASSERTION(mSink, "content sink not found!"); |
michael@0 | 396 | NS_ASSERTION(mInternalState != NS_ERROR_HTMLPARSER_BLOCK, |
michael@0 | 397 | "Shouldn't block from HandleStartElement."); |
michael@0 | 398 | |
michael@0 | 399 | if (mSink && mInternalState != NS_ERROR_HTMLPARSER_STOPPARSING) { |
michael@0 | 400 | nsresult rv = mSink->HandleEndElement(aValue); |
michael@0 | 401 | MaybeStopParser(rv); |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | return NS_OK; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | nsresult |
michael@0 | 408 | nsExpatDriver::HandleCharacterData(const char16_t *aValue, |
michael@0 | 409 | const uint32_t aLength) |
michael@0 | 410 | { |
michael@0 | 411 | NS_ASSERTION(mSink, "content sink not found!"); |
michael@0 | 412 | |
michael@0 | 413 | if (mInCData) { |
michael@0 | 414 | mCDataText.Append(aValue, aLength); |
michael@0 | 415 | } |
michael@0 | 416 | else if (mSink) { |
michael@0 | 417 | nsresult rv = mSink->HandleCharacterData(aValue, aLength); |
michael@0 | 418 | MaybeStopParser(rv); |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | return NS_OK; |
michael@0 | 422 | } |
michael@0 | 423 | |
michael@0 | 424 | nsresult |
michael@0 | 425 | nsExpatDriver::HandleComment(const char16_t *aValue) |
michael@0 | 426 | { |
michael@0 | 427 | NS_ASSERTION(mSink, "content sink not found!"); |
michael@0 | 428 | |
michael@0 | 429 | if (mInExternalDTD) { |
michael@0 | 430 | // Ignore comments from external DTDs |
michael@0 | 431 | return NS_OK; |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | if (mInInternalSubset) { |
michael@0 | 435 | mInternalSubset.AppendLiteral("<!--"); |
michael@0 | 436 | mInternalSubset.Append(aValue); |
michael@0 | 437 | mInternalSubset.AppendLiteral("-->"); |
michael@0 | 438 | } |
michael@0 | 439 | else if (mSink) { |
michael@0 | 440 | nsresult rv = mSink->HandleComment(aValue); |
michael@0 | 441 | MaybeStopParser(rv); |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | return NS_OK; |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | nsresult |
michael@0 | 448 | nsExpatDriver::HandleProcessingInstruction(const char16_t *aTarget, |
michael@0 | 449 | const char16_t *aData) |
michael@0 | 450 | { |
michael@0 | 451 | NS_ASSERTION(mSink, "content sink not found!"); |
michael@0 | 452 | |
michael@0 | 453 | if (mInExternalDTD) { |
michael@0 | 454 | // Ignore PIs in external DTDs for now. Eventually we want to |
michael@0 | 455 | // pass them to the sink in a way that doesn't put them in the DOM |
michael@0 | 456 | return NS_OK; |
michael@0 | 457 | } |
michael@0 | 458 | |
michael@0 | 459 | if (mInInternalSubset) { |
michael@0 | 460 | mInternalSubset.AppendLiteral("<?"); |
michael@0 | 461 | mInternalSubset.Append(aTarget); |
michael@0 | 462 | mInternalSubset.Append(' '); |
michael@0 | 463 | mInternalSubset.Append(aData); |
michael@0 | 464 | mInternalSubset.AppendLiteral("?>"); |
michael@0 | 465 | } |
michael@0 | 466 | else if (mSink) { |
michael@0 | 467 | nsresult rv = mSink->HandleProcessingInstruction(aTarget, aData); |
michael@0 | 468 | MaybeStopParser(rv); |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | return NS_OK; |
michael@0 | 472 | } |
michael@0 | 473 | |
michael@0 | 474 | nsresult |
michael@0 | 475 | nsExpatDriver::HandleXMLDeclaration(const char16_t *aVersion, |
michael@0 | 476 | const char16_t *aEncoding, |
michael@0 | 477 | int32_t aStandalone) |
michael@0 | 478 | { |
michael@0 | 479 | if (mSink) { |
michael@0 | 480 | nsresult rv = mSink->HandleXMLDeclaration(aVersion, aEncoding, aStandalone); |
michael@0 | 481 | MaybeStopParser(rv); |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | return NS_OK; |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | nsresult |
michael@0 | 488 | nsExpatDriver::HandleDefault(const char16_t *aValue, |
michael@0 | 489 | const uint32_t aLength) |
michael@0 | 490 | { |
michael@0 | 491 | NS_ASSERTION(mSink, "content sink not found!"); |
michael@0 | 492 | |
michael@0 | 493 | if (mInExternalDTD) { |
michael@0 | 494 | // Ignore newlines in external DTDs |
michael@0 | 495 | return NS_OK; |
michael@0 | 496 | } |
michael@0 | 497 | |
michael@0 | 498 | if (mInInternalSubset) { |
michael@0 | 499 | mInternalSubset.Append(aValue, aLength); |
michael@0 | 500 | } |
michael@0 | 501 | else if (mSink) { |
michael@0 | 502 | uint32_t i; |
michael@0 | 503 | nsresult rv = mInternalState; |
michael@0 | 504 | for (i = 0; i < aLength && NS_SUCCEEDED(rv); ++i) { |
michael@0 | 505 | if (aValue[i] == '\n' || aValue[i] == '\r') { |
michael@0 | 506 | rv = mSink->HandleCharacterData(&aValue[i], 1); |
michael@0 | 507 | } |
michael@0 | 508 | } |
michael@0 | 509 | MaybeStopParser(rv); |
michael@0 | 510 | } |
michael@0 | 511 | |
michael@0 | 512 | return NS_OK; |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | nsresult |
michael@0 | 516 | nsExpatDriver::HandleStartCdataSection() |
michael@0 | 517 | { |
michael@0 | 518 | mInCData = true; |
michael@0 | 519 | |
michael@0 | 520 | return NS_OK; |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | nsresult |
michael@0 | 524 | nsExpatDriver::HandleEndCdataSection() |
michael@0 | 525 | { |
michael@0 | 526 | NS_ASSERTION(mSink, "content sink not found!"); |
michael@0 | 527 | |
michael@0 | 528 | mInCData = false; |
michael@0 | 529 | if (mSink) { |
michael@0 | 530 | nsresult rv = mSink->HandleCDataSection(mCDataText.get(), |
michael@0 | 531 | mCDataText.Length()); |
michael@0 | 532 | MaybeStopParser(rv); |
michael@0 | 533 | } |
michael@0 | 534 | mCDataText.Truncate(); |
michael@0 | 535 | |
michael@0 | 536 | return NS_OK; |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | nsresult |
michael@0 | 540 | nsExpatDriver::HandleStartNamespaceDecl(const char16_t* aPrefix, |
michael@0 | 541 | const char16_t* aUri) |
michael@0 | 542 | { |
michael@0 | 543 | if (mExtendedSink) { |
michael@0 | 544 | nsresult rv = mExtendedSink->HandleStartNamespaceDecl(aPrefix, aUri); |
michael@0 | 545 | MaybeStopParser(rv); |
michael@0 | 546 | } |
michael@0 | 547 | return NS_OK; |
michael@0 | 548 | } |
michael@0 | 549 | |
michael@0 | 550 | nsresult |
michael@0 | 551 | nsExpatDriver::HandleEndNamespaceDecl(const char16_t* aPrefix) |
michael@0 | 552 | { |
michael@0 | 553 | if (mExtendedSink && mInternalState != NS_ERROR_HTMLPARSER_STOPPARSING) { |
michael@0 | 554 | nsresult rv = mExtendedSink->HandleEndNamespaceDecl(aPrefix); |
michael@0 | 555 | MaybeStopParser(rv); |
michael@0 | 556 | } |
michael@0 | 557 | return NS_OK; |
michael@0 | 558 | } |
michael@0 | 559 | |
michael@0 | 560 | nsresult |
michael@0 | 561 | nsExpatDriver::HandleNotationDecl(const char16_t* aNotationName, |
michael@0 | 562 | const char16_t* aBase, |
michael@0 | 563 | const char16_t* aSysid, |
michael@0 | 564 | const char16_t* aPubid) |
michael@0 | 565 | { |
michael@0 | 566 | if (mExtendedSink) { |
michael@0 | 567 | nsresult rv = mExtendedSink->HandleNotationDecl(aNotationName, aSysid, |
michael@0 | 568 | aPubid); |
michael@0 | 569 | MaybeStopParser(rv); |
michael@0 | 570 | } |
michael@0 | 571 | return NS_OK; |
michael@0 | 572 | } |
michael@0 | 573 | |
michael@0 | 574 | nsresult |
michael@0 | 575 | nsExpatDriver::HandleUnparsedEntityDecl(const char16_t* aEntityName, |
michael@0 | 576 | const char16_t* aBase, |
michael@0 | 577 | const char16_t* aSysid, |
michael@0 | 578 | const char16_t* aPubid, |
michael@0 | 579 | const char16_t* aNotationName) |
michael@0 | 580 | { |
michael@0 | 581 | if (mExtendedSink) { |
michael@0 | 582 | nsresult rv = mExtendedSink->HandleUnparsedEntityDecl(aEntityName, |
michael@0 | 583 | aSysid, |
michael@0 | 584 | aPubid, |
michael@0 | 585 | aNotationName); |
michael@0 | 586 | MaybeStopParser(rv); |
michael@0 | 587 | } |
michael@0 | 588 | return NS_OK; |
michael@0 | 589 | } |
michael@0 | 590 | |
michael@0 | 591 | nsresult |
michael@0 | 592 | nsExpatDriver::HandleStartDoctypeDecl(const char16_t* aDoctypeName, |
michael@0 | 593 | const char16_t* aSysid, |
michael@0 | 594 | const char16_t* aPubid, |
michael@0 | 595 | bool aHasInternalSubset) |
michael@0 | 596 | { |
michael@0 | 597 | mDoctypeName = aDoctypeName; |
michael@0 | 598 | mSystemID = aSysid; |
michael@0 | 599 | mPublicID = aPubid; |
michael@0 | 600 | |
michael@0 | 601 | if (mExtendedSink) { |
michael@0 | 602 | nsresult rv = mExtendedSink->HandleStartDTD(aDoctypeName, aSysid, aPubid); |
michael@0 | 603 | MaybeStopParser(rv); |
michael@0 | 604 | } |
michael@0 | 605 | |
michael@0 | 606 | if (aHasInternalSubset) { |
michael@0 | 607 | // Consuming a huge internal subset translates to numerous |
michael@0 | 608 | // allocations. In an effort to avoid too many allocations |
michael@0 | 609 | // setting mInternalSubset's capacity to be 1K ( just a guesstimate! ). |
michael@0 | 610 | mInInternalSubset = true; |
michael@0 | 611 | mInternalSubset.SetCapacity(1024); |
michael@0 | 612 | } else { |
michael@0 | 613 | // Distinguish missing internal subset from an empty one |
michael@0 | 614 | mInternalSubset.SetIsVoid(true); |
michael@0 | 615 | } |
michael@0 | 616 | |
michael@0 | 617 | return NS_OK; |
michael@0 | 618 | } |
michael@0 | 619 | |
michael@0 | 620 | nsresult |
michael@0 | 621 | nsExpatDriver::HandleEndDoctypeDecl() |
michael@0 | 622 | { |
michael@0 | 623 | NS_ASSERTION(mSink, "content sink not found!"); |
michael@0 | 624 | |
michael@0 | 625 | mInInternalSubset = false; |
michael@0 | 626 | |
michael@0 | 627 | if (mSink) { |
michael@0 | 628 | // let the sink know any additional knowledge that we have about the |
michael@0 | 629 | // document (currently, from bug 124570, we only expect to pass additional |
michael@0 | 630 | // agent sheets needed to layout the XML vocabulary of the document) |
michael@0 | 631 | nsCOMPtr<nsIURI> data; |
michael@0 | 632 | if (mCatalogData && mCatalogData->mAgentSheet) { |
michael@0 | 633 | NS_NewURI(getter_AddRefs(data), mCatalogData->mAgentSheet); |
michael@0 | 634 | } |
michael@0 | 635 | |
michael@0 | 636 | // Note: mInternalSubset already doesn't include the [] around it. |
michael@0 | 637 | nsresult rv = mSink->HandleDoctypeDecl(mInternalSubset, mDoctypeName, |
michael@0 | 638 | mSystemID, mPublicID, data); |
michael@0 | 639 | MaybeStopParser(rv); |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | mInternalSubset.SetCapacity(0); |
michael@0 | 643 | |
michael@0 | 644 | return NS_OK; |
michael@0 | 645 | } |
michael@0 | 646 | |
michael@0 | 647 | static NS_METHOD |
michael@0 | 648 | ExternalDTDStreamReaderFunc(nsIUnicharInputStream* aIn, |
michael@0 | 649 | void* aClosure, |
michael@0 | 650 | const char16_t* aFromSegment, |
michael@0 | 651 | uint32_t aToOffset, |
michael@0 | 652 | uint32_t aCount, |
michael@0 | 653 | uint32_t *aWriteCount) |
michael@0 | 654 | { |
michael@0 | 655 | // Pass the buffer to expat for parsing. |
michael@0 | 656 | if (XML_Parse((XML_Parser)aClosure, (const char *)aFromSegment, |
michael@0 | 657 | aCount * sizeof(char16_t), 0) == XML_STATUS_OK) { |
michael@0 | 658 | *aWriteCount = aCount; |
michael@0 | 659 | |
michael@0 | 660 | return NS_OK; |
michael@0 | 661 | } |
michael@0 | 662 | |
michael@0 | 663 | *aWriteCount = 0; |
michael@0 | 664 | |
michael@0 | 665 | return NS_ERROR_FAILURE; |
michael@0 | 666 | } |
michael@0 | 667 | |
michael@0 | 668 | int |
michael@0 | 669 | nsExpatDriver::HandleExternalEntityRef(const char16_t *openEntityNames, |
michael@0 | 670 | const char16_t *base, |
michael@0 | 671 | const char16_t *systemId, |
michael@0 | 672 | const char16_t *publicId) |
michael@0 | 673 | { |
michael@0 | 674 | if (mInInternalSubset && !mInExternalDTD && openEntityNames) { |
michael@0 | 675 | mInternalSubset.Append(char16_t('%')); |
michael@0 | 676 | mInternalSubset.Append(nsDependentString(openEntityNames)); |
michael@0 | 677 | mInternalSubset.Append(char16_t(';')); |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | // Load the external entity into a buffer. |
michael@0 | 681 | nsCOMPtr<nsIInputStream> in; |
michael@0 | 682 | nsAutoString absURL; |
michael@0 | 683 | nsresult rv = OpenInputStreamFromExternalDTD(publicId, systemId, base, |
michael@0 | 684 | getter_AddRefs(in), absURL); |
michael@0 | 685 | if (NS_FAILED(rv)) { |
michael@0 | 686 | #ifdef DEBUG |
michael@0 | 687 | nsCString message("Failed to open external DTD: publicId \""); |
michael@0 | 688 | AppendUTF16toUTF8(publicId, message); |
michael@0 | 689 | message += "\" systemId \""; |
michael@0 | 690 | AppendUTF16toUTF8(systemId, message); |
michael@0 | 691 | message += "\" base \""; |
michael@0 | 692 | AppendUTF16toUTF8(base, message); |
michael@0 | 693 | message += "\" URL \""; |
michael@0 | 694 | AppendUTF16toUTF8(absURL, message); |
michael@0 | 695 | message += "\""; |
michael@0 | 696 | NS_WARNING(message.get()); |
michael@0 | 697 | #endif |
michael@0 | 698 | return 1; |
michael@0 | 699 | } |
michael@0 | 700 | |
michael@0 | 701 | nsCOMPtr<nsIUnicharInputStream> uniIn; |
michael@0 | 702 | rv = nsSimpleUnicharStreamFactory::GetInstance()-> |
michael@0 | 703 | CreateInstanceFromUTF8Stream(in, getter_AddRefs(uniIn)); |
michael@0 | 704 | NS_ENSURE_SUCCESS(rv, 1); |
michael@0 | 705 | |
michael@0 | 706 | int result = 1; |
michael@0 | 707 | if (uniIn) { |
michael@0 | 708 | XML_Parser entParser = XML_ExternalEntityParserCreate(mExpatParser, 0, |
michael@0 | 709 | kUTF16); |
michael@0 | 710 | if (entParser) { |
michael@0 | 711 | XML_SetBase(entParser, absURL.get()); |
michael@0 | 712 | |
michael@0 | 713 | mInExternalDTD = true; |
michael@0 | 714 | |
michael@0 | 715 | uint32_t totalRead; |
michael@0 | 716 | do { |
michael@0 | 717 | rv = uniIn->ReadSegments(ExternalDTDStreamReaderFunc, entParser, |
michael@0 | 718 | uint32_t(-1), &totalRead); |
michael@0 | 719 | } while (NS_SUCCEEDED(rv) && totalRead > 0); |
michael@0 | 720 | |
michael@0 | 721 | result = XML_Parse(entParser, nullptr, 0, 1); |
michael@0 | 722 | |
michael@0 | 723 | mInExternalDTD = false; |
michael@0 | 724 | |
michael@0 | 725 | XML_ParserFree(entParser); |
michael@0 | 726 | } |
michael@0 | 727 | } |
michael@0 | 728 | |
michael@0 | 729 | return result; |
michael@0 | 730 | } |
michael@0 | 731 | |
michael@0 | 732 | nsresult |
michael@0 | 733 | nsExpatDriver::OpenInputStreamFromExternalDTD(const char16_t* aFPIStr, |
michael@0 | 734 | const char16_t* aURLStr, |
michael@0 | 735 | const char16_t* aBaseURL, |
michael@0 | 736 | nsIInputStream** aStream, |
michael@0 | 737 | nsAString& aAbsURL) |
michael@0 | 738 | { |
michael@0 | 739 | nsCOMPtr<nsIURI> baseURI; |
michael@0 | 740 | nsresult rv = NS_NewURI(getter_AddRefs(baseURI), |
michael@0 | 741 | NS_ConvertUTF16toUTF8(aBaseURL)); |
michael@0 | 742 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 743 | |
michael@0 | 744 | nsCOMPtr<nsIURI> uri; |
michael@0 | 745 | rv = NS_NewURI(getter_AddRefs(uri), NS_ConvertUTF16toUTF8(aURLStr), nullptr, |
michael@0 | 746 | baseURI); |
michael@0 | 747 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 748 | |
michael@0 | 749 | // check if it is alright to load this uri |
michael@0 | 750 | bool isChrome = false; |
michael@0 | 751 | uri->SchemeIs("chrome", &isChrome); |
michael@0 | 752 | if (!isChrome) { |
michael@0 | 753 | // since the url is not a chrome url, check to see if we can map the DTD |
michael@0 | 754 | // to a known local DTD, or if a DTD file of the same name exists in the |
michael@0 | 755 | // special DTD directory |
michael@0 | 756 | if (aFPIStr) { |
michael@0 | 757 | // see if the Formal Public Identifier (FPI) maps to a catalog entry |
michael@0 | 758 | mCatalogData = LookupCatalogData(aFPIStr); |
michael@0 | 759 | } |
michael@0 | 760 | |
michael@0 | 761 | nsCOMPtr<nsIURI> localURI; |
michael@0 | 762 | GetLocalDTDURI(mCatalogData, uri, getter_AddRefs(localURI)); |
michael@0 | 763 | if (!localURI) { |
michael@0 | 764 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 765 | } |
michael@0 | 766 | |
michael@0 | 767 | localURI.swap(uri); |
michael@0 | 768 | } |
michael@0 | 769 | |
michael@0 | 770 | nsCOMPtr<nsIDocument> doc; |
michael@0 | 771 | NS_ASSERTION(mSink == nsCOMPtr<nsIExpatSink>(do_QueryInterface(mOriginalSink)), |
michael@0 | 772 | "In nsExpatDriver::OpenInputStreamFromExternalDTD: " |
michael@0 | 773 | "mOriginalSink not the same object as mSink?"); |
michael@0 | 774 | if (mOriginalSink) |
michael@0 | 775 | doc = do_QueryInterface(mOriginalSink->GetTarget()); |
michael@0 | 776 | int16_t shouldLoad = nsIContentPolicy::ACCEPT; |
michael@0 | 777 | rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_DTD, |
michael@0 | 778 | uri, |
michael@0 | 779 | (doc ? doc->NodePrincipal() : nullptr), |
michael@0 | 780 | doc, |
michael@0 | 781 | EmptyCString(), //mime guess |
michael@0 | 782 | nullptr, //extra |
michael@0 | 783 | &shouldLoad); |
michael@0 | 784 | if (NS_FAILED(rv)) return rv; |
michael@0 | 785 | if (NS_CP_REJECTED(shouldLoad)) { |
michael@0 | 786 | // Disallowed by content policy |
michael@0 | 787 | return NS_ERROR_CONTENT_BLOCKED; |
michael@0 | 788 | } |
michael@0 | 789 | |
michael@0 | 790 | nsAutoCString absURL; |
michael@0 | 791 | uri->GetSpec(absURL); |
michael@0 | 792 | |
michael@0 | 793 | CopyUTF8toUTF16(absURL, aAbsURL); |
michael@0 | 794 | |
michael@0 | 795 | nsCOMPtr<nsIChannel> channel; |
michael@0 | 796 | rv = NS_NewChannel(getter_AddRefs(channel), uri); |
michael@0 | 797 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 798 | |
michael@0 | 799 | channel->SetContentType(NS_LITERAL_CSTRING("application/xml")); |
michael@0 | 800 | return channel->Open(aStream); |
michael@0 | 801 | } |
michael@0 | 802 | |
michael@0 | 803 | static nsresult |
michael@0 | 804 | CreateErrorText(const char16_t* aDescription, |
michael@0 | 805 | const char16_t* aSourceURL, |
michael@0 | 806 | const uint32_t aLineNumber, |
michael@0 | 807 | const uint32_t aColNumber, |
michael@0 | 808 | nsString& aErrorString) |
michael@0 | 809 | { |
michael@0 | 810 | aErrorString.Truncate(); |
michael@0 | 811 | |
michael@0 | 812 | nsAutoString msg; |
michael@0 | 813 | nsresult rv = |
michael@0 | 814 | nsParserMsgUtils::GetLocalizedStringByName(XMLPARSER_PROPERTIES, |
michael@0 | 815 | "XMLParsingError", msg); |
michael@0 | 816 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 817 | |
michael@0 | 818 | // XML Parsing Error: %1$S\nLocation: %2$S\nLine Number %3$u, Column %4$u: |
michael@0 | 819 | char16_t *message = nsTextFormatter::smprintf(msg.get(), aDescription, |
michael@0 | 820 | aSourceURL, aLineNumber, |
michael@0 | 821 | aColNumber); |
michael@0 | 822 | if (!message) { |
michael@0 | 823 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 824 | } |
michael@0 | 825 | |
michael@0 | 826 | aErrorString.Assign(message); |
michael@0 | 827 | nsTextFormatter::smprintf_free(message); |
michael@0 | 828 | |
michael@0 | 829 | return NS_OK; |
michael@0 | 830 | } |
michael@0 | 831 | |
michael@0 | 832 | static nsresult |
michael@0 | 833 | AppendErrorPointer(const int32_t aColNumber, |
michael@0 | 834 | const char16_t *aSourceLine, |
michael@0 | 835 | nsString& aSourceString) |
michael@0 | 836 | { |
michael@0 | 837 | aSourceString.Append(char16_t('\n')); |
michael@0 | 838 | |
michael@0 | 839 | // Last character will be '^'. |
michael@0 | 840 | int32_t last = aColNumber - 1; |
michael@0 | 841 | int32_t i; |
michael@0 | 842 | uint32_t minuses = 0; |
michael@0 | 843 | for (i = 0; i < last; ++i) { |
michael@0 | 844 | if (aSourceLine[i] == '\t') { |
michael@0 | 845 | // Since this uses |white-space: pre;| a tab stop equals 8 spaces. |
michael@0 | 846 | uint32_t add = 8 - (minuses % 8); |
michael@0 | 847 | aSourceString.AppendASCII("--------", add); |
michael@0 | 848 | minuses += add; |
michael@0 | 849 | } |
michael@0 | 850 | else { |
michael@0 | 851 | aSourceString.Append(char16_t('-')); |
michael@0 | 852 | ++minuses; |
michael@0 | 853 | } |
michael@0 | 854 | } |
michael@0 | 855 | aSourceString.Append(char16_t('^')); |
michael@0 | 856 | |
michael@0 | 857 | return NS_OK; |
michael@0 | 858 | } |
michael@0 | 859 | |
michael@0 | 860 | nsresult |
michael@0 | 861 | nsExpatDriver::HandleError() |
michael@0 | 862 | { |
michael@0 | 863 | int32_t code = XML_GetErrorCode(mExpatParser); |
michael@0 | 864 | NS_ASSERTION(code > XML_ERROR_NONE, "unexpected XML error code"); |
michael@0 | 865 | |
michael@0 | 866 | // Map Expat error code to an error string |
michael@0 | 867 | // XXX Deal with error returns. |
michael@0 | 868 | nsAutoString description; |
michael@0 | 869 | nsParserMsgUtils::GetLocalizedStringByID(XMLPARSER_PROPERTIES, code, |
michael@0 | 870 | description); |
michael@0 | 871 | |
michael@0 | 872 | if (code == XML_ERROR_TAG_MISMATCH) { |
michael@0 | 873 | /** |
michael@0 | 874 | * Expat can send the following: |
michael@0 | 875 | * localName |
michael@0 | 876 | * namespaceURI<separator>localName |
michael@0 | 877 | * namespaceURI<separator>localName<separator>prefix |
michael@0 | 878 | * |
michael@0 | 879 | * and we use 0xFFFF for the <separator>. |
michael@0 | 880 | * |
michael@0 | 881 | */ |
michael@0 | 882 | const char16_t *mismatch = MOZ_XML_GetMismatchedTag(mExpatParser); |
michael@0 | 883 | const char16_t *uriEnd = nullptr; |
michael@0 | 884 | const char16_t *nameEnd = nullptr; |
michael@0 | 885 | const char16_t *pos; |
michael@0 | 886 | for (pos = mismatch; *pos; ++pos) { |
michael@0 | 887 | if (*pos == kExpatSeparatorChar) { |
michael@0 | 888 | if (uriEnd) { |
michael@0 | 889 | nameEnd = pos; |
michael@0 | 890 | } |
michael@0 | 891 | else { |
michael@0 | 892 | uriEnd = pos; |
michael@0 | 893 | } |
michael@0 | 894 | } |
michael@0 | 895 | } |
michael@0 | 896 | |
michael@0 | 897 | nsAutoString tagName; |
michael@0 | 898 | if (uriEnd && nameEnd) { |
michael@0 | 899 | // We have a prefix. |
michael@0 | 900 | tagName.Append(nameEnd + 1, pos - nameEnd - 1); |
michael@0 | 901 | tagName.Append(char16_t(':')); |
michael@0 | 902 | } |
michael@0 | 903 | const char16_t *nameStart = uriEnd ? uriEnd + 1 : mismatch; |
michael@0 | 904 | tagName.Append(nameStart, (nameEnd ? nameEnd : pos) - nameStart); |
michael@0 | 905 | |
michael@0 | 906 | nsAutoString msg; |
michael@0 | 907 | nsParserMsgUtils::GetLocalizedStringByName(XMLPARSER_PROPERTIES, |
michael@0 | 908 | "Expected", msg); |
michael@0 | 909 | |
michael@0 | 910 | // . Expected: </%S>. |
michael@0 | 911 | char16_t *message = nsTextFormatter::smprintf(msg.get(), tagName.get()); |
michael@0 | 912 | if (!message) { |
michael@0 | 913 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 914 | } |
michael@0 | 915 | |
michael@0 | 916 | description.Append(message); |
michael@0 | 917 | |
michael@0 | 918 | nsTextFormatter::smprintf_free(message); |
michael@0 | 919 | } |
michael@0 | 920 | |
michael@0 | 921 | // Adjust the column number so that it is one based rather than zero based. |
michael@0 | 922 | uint32_t colNumber = XML_GetCurrentColumnNumber(mExpatParser) + 1; |
michael@0 | 923 | uint32_t lineNumber = XML_GetCurrentLineNumber(mExpatParser); |
michael@0 | 924 | |
michael@0 | 925 | nsAutoString errorText; |
michael@0 | 926 | CreateErrorText(description.get(), XML_GetBase(mExpatParser), lineNumber, |
michael@0 | 927 | colNumber, errorText); |
michael@0 | 928 | |
michael@0 | 929 | NS_ASSERTION(mSink, "no sink?"); |
michael@0 | 930 | |
michael@0 | 931 | nsAutoString sourceText(mLastLine); |
michael@0 | 932 | AppendErrorPointer(colNumber, mLastLine.get(), sourceText); |
michael@0 | 933 | |
michael@0 | 934 | // Try to create and initialize the script error. |
michael@0 | 935 | nsCOMPtr<nsIScriptError> serr(do_CreateInstance(NS_SCRIPTERROR_CONTRACTID)); |
michael@0 | 936 | nsresult rv = NS_ERROR_FAILURE; |
michael@0 | 937 | if (serr) { |
michael@0 | 938 | rv = serr->InitWithWindowID(description, |
michael@0 | 939 | mURISpec, |
michael@0 | 940 | mLastLine, |
michael@0 | 941 | lineNumber, colNumber, |
michael@0 | 942 | nsIScriptError::errorFlag, "malformed-xml", |
michael@0 | 943 | mInnerWindowID); |
michael@0 | 944 | } |
michael@0 | 945 | |
michael@0 | 946 | // If it didn't initialize, we can't do any logging. |
michael@0 | 947 | bool shouldReportError = NS_SUCCEEDED(rv); |
michael@0 | 948 | |
michael@0 | 949 | if (mSink && shouldReportError) { |
michael@0 | 950 | rv = mSink->ReportError(errorText.get(), |
michael@0 | 951 | sourceText.get(), |
michael@0 | 952 | serr, |
michael@0 | 953 | &shouldReportError); |
michael@0 | 954 | if (NS_FAILED(rv)) { |
michael@0 | 955 | shouldReportError = true; |
michael@0 | 956 | } |
michael@0 | 957 | } |
michael@0 | 958 | |
michael@0 | 959 | if (shouldReportError) { |
michael@0 | 960 | nsCOMPtr<nsIConsoleService> cs |
michael@0 | 961 | (do_GetService(NS_CONSOLESERVICE_CONTRACTID)); |
michael@0 | 962 | if (cs) { |
michael@0 | 963 | cs->LogMessage(serr); |
michael@0 | 964 | } |
michael@0 | 965 | } |
michael@0 | 966 | |
michael@0 | 967 | return NS_ERROR_HTMLPARSER_STOPPARSING; |
michael@0 | 968 | } |
michael@0 | 969 | |
michael@0 | 970 | void |
michael@0 | 971 | nsExpatDriver::ParseBuffer(const char16_t *aBuffer, |
michael@0 | 972 | uint32_t aLength, |
michael@0 | 973 | bool aIsFinal, |
michael@0 | 974 | uint32_t *aConsumed) |
michael@0 | 975 | { |
michael@0 | 976 | NS_ASSERTION((aBuffer && aLength != 0) || (!aBuffer && aLength == 0), "?"); |
michael@0 | 977 | NS_ASSERTION(mInternalState != NS_OK || aIsFinal || aBuffer, |
michael@0 | 978 | "Useless call, we won't call Expat"); |
michael@0 | 979 | NS_PRECONDITION(!BlockedOrInterrupted() || !aBuffer, |
michael@0 | 980 | "Non-null buffer when resuming"); |
michael@0 | 981 | NS_PRECONDITION(XML_GetCurrentByteIndex(mExpatParser) % sizeof(char16_t) == 0, |
michael@0 | 982 | "Consumed part of a char16_t?"); |
michael@0 | 983 | |
michael@0 | 984 | if (mExpatParser && (mInternalState == NS_OK || BlockedOrInterrupted())) { |
michael@0 | 985 | int32_t parserBytesBefore = XML_GetCurrentByteIndex(mExpatParser); |
michael@0 | 986 | NS_ASSERTION(parserBytesBefore >= 0, "Unexpected value"); |
michael@0 | 987 | |
michael@0 | 988 | XML_Status status; |
michael@0 | 989 | if (BlockedOrInterrupted()) { |
michael@0 | 990 | mInternalState = NS_OK; // Resume in case we're blocked. |
michael@0 | 991 | status = XML_ResumeParser(mExpatParser); |
michael@0 | 992 | } |
michael@0 | 993 | else { |
michael@0 | 994 | status = XML_Parse(mExpatParser, |
michael@0 | 995 | reinterpret_cast<const char*>(aBuffer), |
michael@0 | 996 | aLength * sizeof(char16_t), aIsFinal); |
michael@0 | 997 | } |
michael@0 | 998 | |
michael@0 | 999 | int32_t parserBytesConsumed = XML_GetCurrentByteIndex(mExpatParser); |
michael@0 | 1000 | |
michael@0 | 1001 | NS_ASSERTION(parserBytesConsumed >= 0, "Unexpected value"); |
michael@0 | 1002 | NS_ASSERTION(parserBytesConsumed >= parserBytesBefore, |
michael@0 | 1003 | "How'd this happen?"); |
michael@0 | 1004 | NS_ASSERTION(parserBytesConsumed % sizeof(char16_t) == 0, |
michael@0 | 1005 | "Consumed part of a char16_t?"); |
michael@0 | 1006 | |
michael@0 | 1007 | // Consumed something. |
michael@0 | 1008 | *aConsumed = (parserBytesConsumed - parserBytesBefore) / sizeof(char16_t); |
michael@0 | 1009 | NS_ASSERTION(*aConsumed <= aLength + mExpatBuffered, |
michael@0 | 1010 | "Too many bytes consumed?"); |
michael@0 | 1011 | |
michael@0 | 1012 | NS_ASSERTION(status != XML_STATUS_SUSPENDED || BlockedOrInterrupted(), |
michael@0 | 1013 | "Inconsistent expat suspension state."); |
michael@0 | 1014 | |
michael@0 | 1015 | if (status == XML_STATUS_ERROR) { |
michael@0 | 1016 | mInternalState = NS_ERROR_HTMLPARSER_STOPPARSING; |
michael@0 | 1017 | } |
michael@0 | 1018 | } |
michael@0 | 1019 | else { |
michael@0 | 1020 | *aConsumed = 0; |
michael@0 | 1021 | } |
michael@0 | 1022 | } |
michael@0 | 1023 | |
michael@0 | 1024 | NS_IMETHODIMP |
michael@0 | 1025 | nsExpatDriver::ConsumeToken(nsScanner& aScanner, bool& aFlushTokens) |
michael@0 | 1026 | { |
michael@0 | 1027 | // We keep the scanner pointing to the position where Expat will start |
michael@0 | 1028 | // parsing. |
michael@0 | 1029 | nsScannerIterator currentExpatPosition; |
michael@0 | 1030 | aScanner.CurrentPosition(currentExpatPosition); |
michael@0 | 1031 | |
michael@0 | 1032 | // This is the start of the first buffer that we need to pass to Expat. |
michael@0 | 1033 | nsScannerIterator start = currentExpatPosition; |
michael@0 | 1034 | start.advance(mExpatBuffered); |
michael@0 | 1035 | |
michael@0 | 1036 | // This is the end of the last buffer (at this point, more data could come in |
michael@0 | 1037 | // later). |
michael@0 | 1038 | nsScannerIterator end; |
michael@0 | 1039 | aScanner.EndReading(end); |
michael@0 | 1040 | |
michael@0 | 1041 | PR_LOG(GetExpatDriverLog(), PR_LOG_DEBUG, |
michael@0 | 1042 | ("Remaining in expat's buffer: %i, remaining in scanner: %i.", |
michael@0 | 1043 | mExpatBuffered, Distance(start, end))); |
michael@0 | 1044 | |
michael@0 | 1045 | // We want to call Expat if we have more buffers, or if we know there won't |
michael@0 | 1046 | // be more buffers (and so we want to flush the remaining data), or if we're |
michael@0 | 1047 | // currently blocked and there's data in Expat's buffer. |
michael@0 | 1048 | while (start != end || (mIsFinalChunk && !mMadeFinalCallToExpat) || |
michael@0 | 1049 | (BlockedOrInterrupted() && mExpatBuffered > 0)) { |
michael@0 | 1050 | bool noMoreBuffers = start == end && mIsFinalChunk; |
michael@0 | 1051 | bool blocked = BlockedOrInterrupted(); |
michael@0 | 1052 | |
michael@0 | 1053 | const char16_t *buffer; |
michael@0 | 1054 | uint32_t length; |
michael@0 | 1055 | if (blocked || noMoreBuffers) { |
michael@0 | 1056 | // If we're blocked we just resume Expat so we don't need a buffer, if |
michael@0 | 1057 | // there aren't any more buffers we pass a null buffer to Expat. |
michael@0 | 1058 | buffer = nullptr; |
michael@0 | 1059 | length = 0; |
michael@0 | 1060 | |
michael@0 | 1061 | #if defined(PR_LOGGING) || defined (DEBUG) |
michael@0 | 1062 | if (blocked) { |
michael@0 | 1063 | PR_LOG(GetExpatDriverLog(), PR_LOG_DEBUG, |
michael@0 | 1064 | ("Resuming Expat, will parse data remaining in Expat's " |
michael@0 | 1065 | "buffer.\nContent of Expat's buffer:\n-----\n%s\n-----\n", |
michael@0 | 1066 | NS_ConvertUTF16toUTF8(currentExpatPosition.get(), |
michael@0 | 1067 | mExpatBuffered).get())); |
michael@0 | 1068 | } |
michael@0 | 1069 | else { |
michael@0 | 1070 | NS_ASSERTION(mExpatBuffered == Distance(currentExpatPosition, end), |
michael@0 | 1071 | "Didn't pass all the data to Expat?"); |
michael@0 | 1072 | PR_LOG(GetExpatDriverLog(), PR_LOG_DEBUG, |
michael@0 | 1073 | ("Last call to Expat, will parse data remaining in Expat's " |
michael@0 | 1074 | "buffer.\nContent of Expat's buffer:\n-----\n%s\n-----\n", |
michael@0 | 1075 | NS_ConvertUTF16toUTF8(currentExpatPosition.get(), |
michael@0 | 1076 | mExpatBuffered).get())); |
michael@0 | 1077 | } |
michael@0 | 1078 | #endif |
michael@0 | 1079 | } |
michael@0 | 1080 | else { |
michael@0 | 1081 | buffer = start.get(); |
michael@0 | 1082 | length = uint32_t(start.size_forward()); |
michael@0 | 1083 | |
michael@0 | 1084 | PR_LOG(GetExpatDriverLog(), PR_LOG_DEBUG, |
michael@0 | 1085 | ("Calling Expat, will parse data remaining in Expat's buffer and " |
michael@0 | 1086 | "new data.\nContent of Expat's buffer:\n-----\n%s\n-----\nNew " |
michael@0 | 1087 | "data:\n-----\n%s\n-----\n", |
michael@0 | 1088 | NS_ConvertUTF16toUTF8(currentExpatPosition.get(), |
michael@0 | 1089 | mExpatBuffered).get(), |
michael@0 | 1090 | NS_ConvertUTF16toUTF8(start.get(), length).get())); |
michael@0 | 1091 | } |
michael@0 | 1092 | |
michael@0 | 1093 | uint32_t consumed; |
michael@0 | 1094 | ParseBuffer(buffer, length, noMoreBuffers, &consumed); |
michael@0 | 1095 | if (consumed > 0) { |
michael@0 | 1096 | nsScannerIterator oldExpatPosition = currentExpatPosition; |
michael@0 | 1097 | currentExpatPosition.advance(consumed); |
michael@0 | 1098 | |
michael@0 | 1099 | // We consumed some data, we want to store the last line of data that |
michael@0 | 1100 | // was consumed in case we run into an error (to show the line in which |
michael@0 | 1101 | // the error occurred). |
michael@0 | 1102 | |
michael@0 | 1103 | // The length of the last line that Expat has parsed. |
michael@0 | 1104 | XML_Size lastLineLength = XML_GetCurrentColumnNumber(mExpatParser); |
michael@0 | 1105 | |
michael@0 | 1106 | if (lastLineLength <= consumed) { |
michael@0 | 1107 | // The length of the last line was less than what expat consumed, so |
michael@0 | 1108 | // there was at least one line break in the consumed data. Store the |
michael@0 | 1109 | // last line until the point where we stopped parsing. |
michael@0 | 1110 | nsScannerIterator startLastLine = currentExpatPosition; |
michael@0 | 1111 | startLastLine.advance(-((ptrdiff_t)lastLineLength)); |
michael@0 | 1112 | CopyUnicodeTo(startLastLine, currentExpatPosition, mLastLine); |
michael@0 | 1113 | } |
michael@0 | 1114 | else { |
michael@0 | 1115 | // There was no line break in the consumed data, append the consumed |
michael@0 | 1116 | // data. |
michael@0 | 1117 | AppendUnicodeTo(oldExpatPosition, currentExpatPosition, mLastLine); |
michael@0 | 1118 | } |
michael@0 | 1119 | } |
michael@0 | 1120 | |
michael@0 | 1121 | mExpatBuffered += length - consumed; |
michael@0 | 1122 | |
michael@0 | 1123 | if (BlockedOrInterrupted()) { |
michael@0 | 1124 | PR_LOG(GetExpatDriverLog(), PR_LOG_DEBUG, |
michael@0 | 1125 | ("Blocked or interrupted parser (probably for loading linked " |
michael@0 | 1126 | "stylesheets or scripts).")); |
michael@0 | 1127 | |
michael@0 | 1128 | aScanner.SetPosition(currentExpatPosition, true); |
michael@0 | 1129 | aScanner.Mark(); |
michael@0 | 1130 | |
michael@0 | 1131 | return mInternalState; |
michael@0 | 1132 | } |
michael@0 | 1133 | |
michael@0 | 1134 | if (noMoreBuffers && mExpatBuffered == 0) { |
michael@0 | 1135 | mMadeFinalCallToExpat = true; |
michael@0 | 1136 | } |
michael@0 | 1137 | |
michael@0 | 1138 | if (NS_FAILED(mInternalState)) { |
michael@0 | 1139 | if (XML_GetErrorCode(mExpatParser) != XML_ERROR_NONE) { |
michael@0 | 1140 | NS_ASSERTION(mInternalState == NS_ERROR_HTMLPARSER_STOPPARSING, |
michael@0 | 1141 | "Unexpected error"); |
michael@0 | 1142 | |
michael@0 | 1143 | // Look for the next newline after the last one we consumed |
michael@0 | 1144 | nsScannerIterator lastLine = currentExpatPosition; |
michael@0 | 1145 | while (lastLine != end) { |
michael@0 | 1146 | length = uint32_t(lastLine.size_forward()); |
michael@0 | 1147 | uint32_t endOffset = 0; |
michael@0 | 1148 | const char16_t *buffer = lastLine.get(); |
michael@0 | 1149 | while (endOffset < length && buffer[endOffset] != '\n' && |
michael@0 | 1150 | buffer[endOffset] != '\r') { |
michael@0 | 1151 | ++endOffset; |
michael@0 | 1152 | } |
michael@0 | 1153 | mLastLine.Append(Substring(buffer, buffer + endOffset)); |
michael@0 | 1154 | if (endOffset < length) { |
michael@0 | 1155 | // We found a newline. |
michael@0 | 1156 | break; |
michael@0 | 1157 | } |
michael@0 | 1158 | |
michael@0 | 1159 | lastLine.advance(length); |
michael@0 | 1160 | } |
michael@0 | 1161 | |
michael@0 | 1162 | HandleError(); |
michael@0 | 1163 | } |
michael@0 | 1164 | |
michael@0 | 1165 | return mInternalState; |
michael@0 | 1166 | } |
michael@0 | 1167 | |
michael@0 | 1168 | // Either we have more buffers, or we were blocked (and we'll flush in the |
michael@0 | 1169 | // next iteration), or we should have emptied Expat's buffer. |
michael@0 | 1170 | NS_ASSERTION(!noMoreBuffers || blocked || |
michael@0 | 1171 | (mExpatBuffered == 0 && currentExpatPosition == end), |
michael@0 | 1172 | "Unreachable data left in Expat's buffer"); |
michael@0 | 1173 | |
michael@0 | 1174 | start.advance(length); |
michael@0 | 1175 | |
michael@0 | 1176 | // It's possible for start to have passed end if we received more data |
michael@0 | 1177 | // (e.g. if we spun the event loop in an inline script). Reload end now |
michael@0 | 1178 | // to compensate. |
michael@0 | 1179 | aScanner.EndReading(end); |
michael@0 | 1180 | } |
michael@0 | 1181 | |
michael@0 | 1182 | aScanner.SetPosition(currentExpatPosition, true); |
michael@0 | 1183 | aScanner.Mark(); |
michael@0 | 1184 | |
michael@0 | 1185 | PR_LOG(GetExpatDriverLog(), PR_LOG_DEBUG, |
michael@0 | 1186 | ("Remaining in expat's buffer: %i, remaining in scanner: %i.", |
michael@0 | 1187 | mExpatBuffered, Distance(currentExpatPosition, end))); |
michael@0 | 1188 | |
michael@0 | 1189 | return NS_SUCCEEDED(mInternalState) ? kEOF : NS_OK; |
michael@0 | 1190 | } |
michael@0 | 1191 | |
michael@0 | 1192 | NS_IMETHODIMP |
michael@0 | 1193 | nsExpatDriver::WillBuildModel(const CParserContext& aParserContext, |
michael@0 | 1194 | nsITokenizer* aTokenizer, |
michael@0 | 1195 | nsIContentSink* aSink) |
michael@0 | 1196 | { |
michael@0 | 1197 | mSink = do_QueryInterface(aSink); |
michael@0 | 1198 | if (!mSink) { |
michael@0 | 1199 | NS_ERROR("nsExpatDriver didn't get an nsIExpatSink"); |
michael@0 | 1200 | // Make sure future calls to us bail out as needed |
michael@0 | 1201 | mInternalState = NS_ERROR_UNEXPECTED; |
michael@0 | 1202 | return mInternalState; |
michael@0 | 1203 | } |
michael@0 | 1204 | |
michael@0 | 1205 | mOriginalSink = aSink; |
michael@0 | 1206 | |
michael@0 | 1207 | static const XML_Memory_Handling_Suite memsuite = |
michael@0 | 1208 | { |
michael@0 | 1209 | (void *(*)(size_t))PR_Malloc, |
michael@0 | 1210 | (void *(*)(void *, size_t))PR_Realloc, |
michael@0 | 1211 | PR_Free |
michael@0 | 1212 | }; |
michael@0 | 1213 | |
michael@0 | 1214 | static const char16_t kExpatSeparator[] = { kExpatSeparatorChar, '\0' }; |
michael@0 | 1215 | |
michael@0 | 1216 | mExpatParser = XML_ParserCreate_MM(kUTF16, &memsuite, kExpatSeparator); |
michael@0 | 1217 | NS_ENSURE_TRUE(mExpatParser, NS_ERROR_FAILURE); |
michael@0 | 1218 | |
michael@0 | 1219 | XML_SetReturnNSTriplet(mExpatParser, XML_TRUE); |
michael@0 | 1220 | |
michael@0 | 1221 | #ifdef XML_DTD |
michael@0 | 1222 | XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS); |
michael@0 | 1223 | #endif |
michael@0 | 1224 | |
michael@0 | 1225 | mURISpec = aParserContext.mScanner->GetFilename(); |
michael@0 | 1226 | |
michael@0 | 1227 | XML_SetBase(mExpatParser, mURISpec.get()); |
michael@0 | 1228 | |
michael@0 | 1229 | nsCOMPtr<nsIDocument> doc = do_QueryInterface(mOriginalSink->GetTarget()); |
michael@0 | 1230 | if (doc) { |
michael@0 | 1231 | nsCOMPtr<nsPIDOMWindow> win = doc->GetWindow(); |
michael@0 | 1232 | if (!win) { |
michael@0 | 1233 | bool aHasHadScriptHandlingObject; |
michael@0 | 1234 | nsIScriptGlobalObject *global = |
michael@0 | 1235 | doc->GetScriptHandlingObject(aHasHadScriptHandlingObject); |
michael@0 | 1236 | if (global) { |
michael@0 | 1237 | win = do_QueryInterface(global); |
michael@0 | 1238 | } |
michael@0 | 1239 | } |
michael@0 | 1240 | if (win && !win->IsInnerWindow()) { |
michael@0 | 1241 | win = win->GetCurrentInnerWindow(); |
michael@0 | 1242 | } |
michael@0 | 1243 | if (win) { |
michael@0 | 1244 | mInnerWindowID = win->WindowID(); |
michael@0 | 1245 | } |
michael@0 | 1246 | } |
michael@0 | 1247 | |
michael@0 | 1248 | // Set up the callbacks |
michael@0 | 1249 | XML_SetXmlDeclHandler(mExpatParser, Driver_HandleXMLDeclaration); |
michael@0 | 1250 | XML_SetElementHandler(mExpatParser, Driver_HandleStartElement, |
michael@0 | 1251 | Driver_HandleEndElement); |
michael@0 | 1252 | XML_SetCharacterDataHandler(mExpatParser, Driver_HandleCharacterData); |
michael@0 | 1253 | XML_SetProcessingInstructionHandler(mExpatParser, |
michael@0 | 1254 | Driver_HandleProcessingInstruction); |
michael@0 | 1255 | XML_SetDefaultHandlerExpand(mExpatParser, Driver_HandleDefault); |
michael@0 | 1256 | XML_SetExternalEntityRefHandler(mExpatParser, |
michael@0 | 1257 | (XML_ExternalEntityRefHandler) |
michael@0 | 1258 | Driver_HandleExternalEntityRef); |
michael@0 | 1259 | XML_SetExternalEntityRefHandlerArg(mExpatParser, this); |
michael@0 | 1260 | XML_SetCommentHandler(mExpatParser, Driver_HandleComment); |
michael@0 | 1261 | XML_SetCdataSectionHandler(mExpatParser, Driver_HandleStartCdataSection, |
michael@0 | 1262 | Driver_HandleEndCdataSection); |
michael@0 | 1263 | |
michael@0 | 1264 | XML_SetParamEntityParsing(mExpatParser, |
michael@0 | 1265 | XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE); |
michael@0 | 1266 | XML_SetDoctypeDeclHandler(mExpatParser, Driver_HandleStartDoctypeDecl, |
michael@0 | 1267 | Driver_HandleEndDoctypeDecl); |
michael@0 | 1268 | |
michael@0 | 1269 | // If the sink is an nsIExtendedExpatSink, |
michael@0 | 1270 | // register some addtional handlers. |
michael@0 | 1271 | mExtendedSink = do_QueryInterface(mSink); |
michael@0 | 1272 | if (mExtendedSink) { |
michael@0 | 1273 | XML_SetNamespaceDeclHandler(mExpatParser, |
michael@0 | 1274 | Driver_HandleStartNamespaceDecl, |
michael@0 | 1275 | Driver_HandleEndNamespaceDecl); |
michael@0 | 1276 | XML_SetUnparsedEntityDeclHandler(mExpatParser, |
michael@0 | 1277 | Driver_HandleUnparsedEntityDecl); |
michael@0 | 1278 | XML_SetNotationDeclHandler(mExpatParser, |
michael@0 | 1279 | Driver_HandleNotationDecl); |
michael@0 | 1280 | } |
michael@0 | 1281 | |
michael@0 | 1282 | // Set up the user data. |
michael@0 | 1283 | XML_SetUserData(mExpatParser, this); |
michael@0 | 1284 | |
michael@0 | 1285 | // XML must detect invalid character convertion |
michael@0 | 1286 | aParserContext.mScanner->OverrideReplacementCharacter(0xffff); |
michael@0 | 1287 | |
michael@0 | 1288 | return mInternalState; |
michael@0 | 1289 | } |
michael@0 | 1290 | |
michael@0 | 1291 | NS_IMETHODIMP |
michael@0 | 1292 | nsExpatDriver::BuildModel(nsITokenizer* aTokenizer, nsIContentSink* aSink) |
michael@0 | 1293 | { |
michael@0 | 1294 | return mInternalState; |
michael@0 | 1295 | } |
michael@0 | 1296 | |
michael@0 | 1297 | NS_IMETHODIMP |
michael@0 | 1298 | nsExpatDriver::DidBuildModel(nsresult anErrorCode) |
michael@0 | 1299 | { |
michael@0 | 1300 | mOriginalSink = nullptr; |
michael@0 | 1301 | mSink = nullptr; |
michael@0 | 1302 | mExtendedSink = nullptr; |
michael@0 | 1303 | return NS_OK; |
michael@0 | 1304 | } |
michael@0 | 1305 | |
michael@0 | 1306 | NS_IMETHODIMP |
michael@0 | 1307 | nsExpatDriver::WillTokenize(bool aIsFinalChunk) |
michael@0 | 1308 | { |
michael@0 | 1309 | mIsFinalChunk = aIsFinalChunk; |
michael@0 | 1310 | return NS_OK; |
michael@0 | 1311 | } |
michael@0 | 1312 | |
michael@0 | 1313 | NS_IMETHODIMP_(void) |
michael@0 | 1314 | nsExpatDriver::Terminate() |
michael@0 | 1315 | { |
michael@0 | 1316 | // XXX - not sure what happens to the unparsed data. |
michael@0 | 1317 | if (mExpatParser) { |
michael@0 | 1318 | XML_StopParser(mExpatParser, XML_FALSE); |
michael@0 | 1319 | } |
michael@0 | 1320 | mInternalState = NS_ERROR_HTMLPARSER_STOPPARSING; |
michael@0 | 1321 | } |
michael@0 | 1322 | |
michael@0 | 1323 | NS_IMETHODIMP_(int32_t) |
michael@0 | 1324 | nsExpatDriver::GetType() |
michael@0 | 1325 | { |
michael@0 | 1326 | return NS_IPARSER_FLAG_XML; |
michael@0 | 1327 | } |
michael@0 | 1328 | |
michael@0 | 1329 | NS_IMETHODIMP_(nsDTDMode) |
michael@0 | 1330 | nsExpatDriver::GetMode() const |
michael@0 | 1331 | { |
michael@0 | 1332 | return eDTDMode_full_standards; |
michael@0 | 1333 | } |
michael@0 | 1334 | |
michael@0 | 1335 | /*************************** Unused methods **********************************/ |
michael@0 | 1336 | |
michael@0 | 1337 | NS_IMETHODIMP_(bool) |
michael@0 | 1338 | nsExpatDriver::IsContainer(int32_t aTag) const |
michael@0 | 1339 | { |
michael@0 | 1340 | return true; |
michael@0 | 1341 | } |
michael@0 | 1342 | |
michael@0 | 1343 | NS_IMETHODIMP_(bool) |
michael@0 | 1344 | nsExpatDriver::CanContain(int32_t aParent,int32_t aChild) const |
michael@0 | 1345 | { |
michael@0 | 1346 | return true; |
michael@0 | 1347 | } |
michael@0 | 1348 | |
michael@0 | 1349 | void |
michael@0 | 1350 | nsExpatDriver::MaybeStopParser(nsresult aState) |
michael@0 | 1351 | { |
michael@0 | 1352 | if (NS_FAILED(aState)) { |
michael@0 | 1353 | // If we had a failure we want to override NS_ERROR_HTMLPARSER_INTERRUPTED |
michael@0 | 1354 | // and we want to override NS_ERROR_HTMLPARSER_BLOCK but not with |
michael@0 | 1355 | // NS_ERROR_HTMLPARSER_INTERRUPTED. |
michael@0 | 1356 | if (NS_SUCCEEDED(mInternalState) || |
michael@0 | 1357 | mInternalState == NS_ERROR_HTMLPARSER_INTERRUPTED || |
michael@0 | 1358 | (mInternalState == NS_ERROR_HTMLPARSER_BLOCK && |
michael@0 | 1359 | aState != NS_ERROR_HTMLPARSER_INTERRUPTED)) { |
michael@0 | 1360 | mInternalState = (aState == NS_ERROR_HTMLPARSER_INTERRUPTED || |
michael@0 | 1361 | aState == NS_ERROR_HTMLPARSER_BLOCK) ? |
michael@0 | 1362 | aState : |
michael@0 | 1363 | NS_ERROR_HTMLPARSER_STOPPARSING; |
michael@0 | 1364 | } |
michael@0 | 1365 | |
michael@0 | 1366 | // If we get an error then we need to stop Expat (by calling XML_StopParser |
michael@0 | 1367 | // with false as the last argument). If the parser should be blocked or |
michael@0 | 1368 | // interrupted we need to pause Expat (by calling XML_StopParser with |
michael@0 | 1369 | // true as the last argument). |
michael@0 | 1370 | XML_StopParser(mExpatParser, BlockedOrInterrupted()); |
michael@0 | 1371 | } |
michael@0 | 1372 | else if (NS_SUCCEEDED(mInternalState)) { |
michael@0 | 1373 | // Only clobber mInternalState with the success code if we didn't block or |
michael@0 | 1374 | // interrupt before. |
michael@0 | 1375 | mInternalState = aState; |
michael@0 | 1376 | } |
michael@0 | 1377 | } |