forked from xmldom/xmldom
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: report fatalError when doctype is inside elements (xmldom#550)
which is a regression compared to 0.8.x, most likely introduced as part of xmldom#498 - add check to `parseDoctypeCommentOrCData` - drop redundant and broken `Element.appendChild` implementation - `hasInsertableNodeType` now checks for `CharacterData` nodes instead of only text nodes - align ParseError and DOMException in how they are extending Error - wrap `DOMException`s in `ParseError` in sax parser - move custom errors to own module - and allow current and modern constructor arguments for DOMException
- Loading branch information
Showing
24 changed files
with
628 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
'use strict'; | ||
|
||
var conventions = require('./conventions'); | ||
|
||
function extendError(constructor, writableName) { | ||
constructor.prototype = Object.create(Error.prototype, { | ||
constructor: { value: constructor }, | ||
name: { value: constructor.name, enumerable: true, writable: writableName }, | ||
}); | ||
} | ||
|
||
var DOMExceptionName = conventions.freeze({ | ||
/** | ||
* the default value as defined by the spec | ||
*/ | ||
Error: 'Error', | ||
/** | ||
* @deprecated | ||
* Use RangeError instead. | ||
*/ | ||
IndexSizeError: 'IndexSizeError', | ||
/** | ||
* @deprecated | ||
* Just to match the related static code, not part of the spec. | ||
*/ | ||
DomstringSizeError: 'DomstringSizeError', | ||
HierarchyRequestError: 'HierarchyRequestError', | ||
WrongDocumentError: 'WrongDocumentError', | ||
InvalidCharacterError: 'InvalidCharacterError', | ||
/** | ||
* @deprecated | ||
* Just to match the related static code, not part of the spec. | ||
*/ | ||
NoDataAllowedError: 'NoDataAllowedError', | ||
NoModificationAllowedError: 'NoModificationAllowedError', | ||
NotFoundError: 'NotFoundError', | ||
NotSupportedError: 'NotSupportedError', | ||
InUseAttributeError: 'InUseAttributeError', | ||
InvalidStateError: 'InvalidStateError', | ||
SyntaxError: 'SyntaxError', | ||
InvalidModificationError: 'InvalidModificationError', | ||
NamespaceError: 'NamespaceError', | ||
/** | ||
* @deprecated | ||
* Use TypeError for invalid arguments, | ||
* "NotSupportedError" DOMException for unsupported operations, | ||
* and "NotAllowedError" DOMException for denied requests instead. | ||
*/ | ||
InvalidAccessError: 'InvalidAccessError', | ||
/** | ||
* @deprecated | ||
* Just to match the related static code, not part of the spec. | ||
*/ | ||
ValidationError: 'ValidationError', | ||
/** | ||
* @deprecated | ||
* Use TypeError instead. | ||
*/ | ||
TypeMismatchError: 'TypeMismatchError', | ||
SecurityError: 'SecurityError', | ||
NetworkError: 'NetworkError', | ||
AbortError: 'AbortError', | ||
/** | ||
* @deprecated | ||
* Just to match the related static code, not part of the spec. | ||
*/ | ||
URLMismatchError: 'URLMismatchError', | ||
QuotaExceededError: 'QuotaExceededError', | ||
TimeoutError: 'TimeoutError', | ||
InvalidNodeTypeError: 'InvalidNodeTypeError', | ||
DataCloneError: 'DataCloneError', | ||
EncodingError: 'EncodingError', | ||
NotReadableError: 'NotReadableError', | ||
UnknownError: 'UnknownError', | ||
ConstraintError: 'ConstraintError', | ||
DataError: 'DataError', | ||
TransactionInactiveError: 'TransactionInactiveError', | ||
ReadOnlyError: 'ReadOnlyError', | ||
VersionError: 'VersionError', | ||
OperationError: 'OperationError', | ||
NotAllowedError: 'NotAllowedError', | ||
OptOutError: 'OptOutError', | ||
}); | ||
var DOMExceptionNames = Object.keys(DOMExceptionName); | ||
|
||
function isValidDomExceptionCode(value) { | ||
return typeof value === 'number' && value >= 1 && value <= 25; | ||
} | ||
function endsWithError(value) { | ||
return typeof value === 'string' && value.substring(value.length - DOMExceptionName.Error.length) === DOMExceptionName.Error; | ||
} | ||
/** | ||
* DOM operations only raise exceptions in "exceptional" circumstances, i.e., when an operation | ||
* is impossible to perform (either for logical reasons, because data is lost, or because the | ||
* implementation has become unstable). In general, DOM methods return specific error values in | ||
* ordinary processing situations, such as out-of-bound errors when using NodeList. | ||
* | ||
* Implementations should raise other exceptions under other circumstances. For example, | ||
* implementations should raise an implementation-dependent exception if a null argument is | ||
* passed when null was not expected. | ||
* | ||
* This implementation supports the following usages: | ||
* 1. according to the living standard (both arguments are mandatory): | ||
* ``` | ||
* new DOMException("message (can be empty)", DOMExceptionNames.HierarchyRequestError) | ||
* ``` | ||
* 2. according to previous xmldom implementation (only the first argument is required): | ||
* ``` | ||
* new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "optional message") | ||
* ``` | ||
* both result in the proper name being set. | ||
* | ||
* @class DOMException | ||
* @param {number | string} messageOrCode | ||
* The reason why an operation is not acceptable. | ||
* If it is a number, it is used to determine the `name`, see | ||
* {@link https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-258A00AF ExceptionCode} | ||
* @param {string | keyof typeof DOMExceptionName | Error} [nameOrMessage] | ||
* The `name` to use for the error. | ||
* If `messageOrCode` is a number, this arguments is used as the `message` instead. | ||
* @augments Error | ||
* @see https://webidl.spec.whatwg.org/#idl-DOMException | ||
* @see https://webidl.spec.whatwg.org/#dfn-error-names-table | ||
* @see https://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-17189187 | ||
* @see http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html | ||
* @see http://www.w3.org/TR/REC-DOM-Level-1/ecma-script-language-binding.html | ||
*/ | ||
function DOMException(messageOrCode, nameOrMessage) { | ||
// support old way of passing arguments: first argument is a valid number | ||
if (isValidDomExceptionCode(messageOrCode)) { | ||
this.name = DOMExceptionNames[messageOrCode]; | ||
this.message = nameOrMessage || ''; | ||
} else { | ||
this.message = messageOrCode; | ||
this.name = endsWithError(nameOrMessage) ? nameOrMessage : DOMExceptionName.Error; | ||
} | ||
if (Error.captureStackTrace) Error.captureStackTrace(this, DOMException); | ||
} | ||
extendError(DOMException, true); | ||
Object.defineProperties(DOMException.prototype, { | ||
code: { | ||
enumerable: true, | ||
get: function () { | ||
var code = DOMExceptionNames.indexOf(this.name); | ||
if (isValidDomExceptionCode(code)) return code; | ||
return 0; | ||
}, | ||
}, | ||
}); | ||
|
||
DOMException.INDEX_SIZE_ERR = 1; | ||
DOMException.DOMSTRING_SIZE_ERR = 2; | ||
DOMException.HIERARCHY_REQUEST_ERR = 3; | ||
DOMException.WRONG_DOCUMENT_ERR = 4; | ||
DOMException.INVALID_CHARACTER_ERR = 5; | ||
DOMException.NO_DATA_ALLOWED_ERR = 6; | ||
DOMException.NO_MODIFICATION_ALLOWED_ERR = 7; | ||
DOMException.NOT_FOUND_ERR = 8; | ||
DOMException.NOT_SUPPORTED_ERR = 9; | ||
DOMException.INUSE_ATTRIBUTE_ERR = 10; | ||
DOMException.INVALID_STATE_ERR = 11; | ||
DOMException.SYNTAX_ERR = 12; | ||
DOMException.INVALID_MODIFICATION_ERR = 13; | ||
DOMException.NAMESPACE_ERR = 14; | ||
DOMException.INVALID_ACCESS_ERR = 15; | ||
DOMException.VALIDATION_ERR = 16; | ||
DOMException.TYPE_MISMATCH_ERR = 17; | ||
DOMException.SECURITY_ERR = 18; | ||
DOMException.NETWORK_ERR = 19; | ||
DOMException.ABORT_ERR = 20; | ||
DOMException.URL_MISMATCH_ERR = 21; | ||
DOMException.QUOTA_EXCEEDED_ERR = 22; | ||
DOMException.TIMEOUT_ERR = 23; | ||
DOMException.INVALID_NODE_TYPE_ERR = 24; | ||
DOMException.DATA_CLONE_ERR = 25; | ||
|
||
/** | ||
* Creates an error that will not be caught by XMLReader aka the SAX parser. | ||
* | ||
* @class | ||
* @param {string} message | ||
* @param {any} [locator] | ||
* @param {Error} [cause] | ||
* Optional, can provide details about the location in the source. | ||
*/ | ||
function ParseError(message, locator, cause) { | ||
this.message = message; | ||
this.locator = locator; | ||
this.cause = cause; | ||
if (Error.captureStackTrace) Error.captureStackTrace(this, ParseError); | ||
} | ||
extendError(ParseError); | ||
|
||
exports.DOMException = DOMException; | ||
exports.DOMExceptionName = DOMExceptionName; | ||
exports.ParseError = ParseError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.