@journeyapps/domparser
Advanced tools
Comparing version 0.2.0 to 0.3.0
import { XMLDocument } from './XMLDocument'; | ||
import { DOMImplementation } from './DOMImplementation'; | ||
export interface DOMParserOptions { | ||
implementation: DOMImplementation; | ||
} | ||
export declare class DOMParser implements globalThis.DOMParser { | ||
private options; | ||
export declare class DOMParser { | ||
private options?; | ||
constructor(options?: Partial<DOMParserOptions>); | ||
parseFromString(source: string, _type?: SupportedType): XMLDocument; | ||
parseFromString(source: string, _type?: 'text/html' | 'text/xml' | 'application/xml'): XMLDocument; | ||
} |
export { DOMParser } from './DOMParser'; | ||
export { XMLSerializer } from './XMLSerializer'; | ||
export { XMLElement } from './XMLElement'; | ||
export { XMLError } from './XMLError'; | ||
export { XMLDocument } from './XMLDocument'; | ||
export { XMLPosition } from './XMLPosition'; | ||
export { XMLAttribute } from './XMLAttribute'; | ||
export { XMLNode } from './XMLNode'; | ||
export declare const ELEMENT_NODE = 1; | ||
export declare const ATTRIBUTE_NODE = 2; | ||
export declare const TEXT_NODE = 3; | ||
export * from './types'; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -7,7 +10,3 @@ var DOMParser_1 = require("./DOMParser"); | ||
exports.XMLSerializer = XMLSerializer_1.XMLSerializer; | ||
var XMLError_1 = require("./XMLError"); | ||
exports.XMLError = XMLError_1.XMLError; | ||
exports.ELEMENT_NODE = 1; | ||
exports.ATTRIBUTE_NODE = 2; | ||
exports.TEXT_NODE = 3; | ||
__export(require("./types")); | ||
//# sourceMappingURL=index.js.map |
import { XMLNode } from './XMLNode'; | ||
export interface IterableNodeList<T extends XMLNode = XMLNode> extends NodeListOf<T>, Iterable<T> { | ||
export interface IterableNodeList<T extends XMLNode = XMLNode> { | ||
readonly length: number; | ||
item(index: number): T; | ||
[index: number]: T; | ||
forEach?: any; | ||
} |
import { XMLElement } from './XMLElement'; | ||
export interface XMLAttribute extends Attr { | ||
import { XMLNode } from './XMLNode'; | ||
export interface XMLAttribute extends XMLNode { | ||
readonly ownerElement: XMLElement | null; | ||
readonly localName: string; | ||
readonly name: string; | ||
readonly namespaceURI: string | null; | ||
readonly prefix: string | null; | ||
readonly specified: boolean; | ||
value: string; | ||
} |
import { XMLError } from './XMLError'; | ||
import { XMLElement } from './XMLElement'; | ||
import { XMLLocator } from './XMLLocator'; | ||
import { XMLNode } from './XMLNode'; | ||
import { XMLNode, XMLCharacterNode } from './XMLNode'; | ||
import { IterableNodeList } from './IterableNodeList'; | ||
import { DOMImplementation } from './DOMImplementation'; | ||
/** | ||
@@ -10,14 +12,30 @@ * A Document extended with: | ||
*/ | ||
export declare type XMLDocument = XMLNode & XMLDocumentInternal; | ||
interface XMLDocumentInternal extends Document { | ||
documentElement: XMLElement & HTMLElement; | ||
errors: XMLError[]; | ||
xmlVersion: string; | ||
locator: XMLLocator; | ||
readonly parentNode: XMLNode & ParentNode | null; | ||
getElementsByTagName(qualifiedName: string): HTMLCollectionOf<XMLElement>; | ||
export interface XMLDocument extends XMLNode { | ||
documentElement: XMLElement; | ||
implementation: DOMImplementation; | ||
errors?: XMLError[]; | ||
xmlVersion?: string; | ||
locator?: XMLLocator; | ||
readonly parentNode: XMLNode | null; | ||
getElementsByTagName(qualifiedName: string): IterableNodeList<XMLElement>; | ||
/** | ||
* Returns a reference to the first object with the specified value of the ID or NAME attribute. | ||
* @param elementId String that specifies the ID value. Case-insensitive. | ||
*/ | ||
getElementById(elementId: string): XMLElement | null; | ||
/** | ||
* Gets a collection of objects based on the value of the NAME or ID attribute. | ||
* @param elementName Gets a collection of objects based on the value of the NAME or ID attribute. | ||
*/ | ||
getElementsByName(elementName: string): IterableNodeList<XMLElement>; | ||
/** | ||
* Retrieves a collection of objects based on the specified element name. | ||
* @param name Specifies the name of an element. | ||
*/ | ||
getElementsByTagName(qualifiedName: string): IterableNodeList<XMLElement>; | ||
getElementsByTagNameNS(namespaceURI: string, localName: string): IterableNodeList<XMLElement>; | ||
/** | ||
* Returns a CDATASection node whose data is data. | ||
*/ | ||
createCDATASection(data: string): XMLNode & CDATASection; | ||
createCDATASection(data: string): XMLCharacterNode; | ||
/** | ||
@@ -27,3 +45,3 @@ * Creates a comment object with the specified data. | ||
*/ | ||
createComment(data: string): XMLNode & Comment; | ||
createComment(data: string): XMLCharacterNode; | ||
/** | ||
@@ -33,3 +51,3 @@ * Creates a text string from the specified value. | ||
*/ | ||
createTextNode(data: string): XMLNode & Text; | ||
createTextNode(data: string): XMLCharacterNode; | ||
/** | ||
@@ -42,3 +60,3 @@ * Returns a ProcessingInstruction node whose target is target and data is data. | ||
*/ | ||
createProcessingInstruction(target: string, data: string): XMLNode & ProcessingInstruction; | ||
createProcessingInstruction(target: string, data: string): XMLNode; | ||
/** | ||
@@ -49,16 +67,4 @@ * Creates an instance of the element for the specified tag. | ||
createElement(tagName: string): XMLElement; | ||
createElementNS(namespaceURI: string | null, qualifiedName: string, options?: ElementCreationOptions): XMLElement; | ||
createElementNS(namespace: string | null, qualifiedName: string, options?: ElementCreationOptions): XMLElement; | ||
/** | ||
* Creates an instance of the element for the specified tag. | ||
* @param tagName The name of an element. | ||
*/ | ||
createElement<K extends keyof HTMLElementTagNameMap>(tagName: K, options?: ElementCreationOptions): HTMLElementTagNameMap[K]; | ||
/** @deprecated */ | ||
createElement<K extends keyof HTMLElementDeprecatedTagNameMap>(tagName: K, options?: ElementCreationOptions): HTMLElementDeprecatedTagNameMap[K]; | ||
createElement(tagName: string, options?: ElementCreationOptions): HTMLElement; | ||
createElementNS(namespaceURI: 'http://www.w3.org/1999/xhtml', qualifiedName: string): HTMLElement; | ||
createElementNS<K extends keyof SVGElementTagNameMap>(namespaceURI: 'http://www.w3.org/2000/svg', qualifiedName: K): SVGElementTagNameMap[K]; | ||
createElementNS(namespaceURI: 'http://www.w3.org/2000/svg', qualifiedName: string): SVGElement; | ||
createElementNS(namespaceURI: string | null, qualifiedName: string, options?: any): XMLElement; | ||
createElementNS(namespace: string | null, qualifiedName: string, options?: any): XMLElement; | ||
} | ||
export {}; |
@@ -1,5 +0,14 @@ | ||
declare let ourDOMParser: typeof DOMParser; | ||
declare let ourXMLSerializer: typeof XMLSerializer; | ||
import { XMLNode } from './XMLNode'; | ||
import { DOMParser as DOMParserInterface } from './DOMParser'; | ||
import { XMLSerializer as XMLSerializerInterface } from './XMLSerializer'; | ||
import { DOMImplementation } from './DOMImplementation'; | ||
declare let ourDOMParser: typeof DOMParserInterface; | ||
declare let ourXMLSerializer: typeof XMLSerializerInterface; | ||
declare let implementation: DOMImplementation; | ||
export { implementation, ourDOMParser as DOMParser, ourXMLSerializer as XMLSerializer }; | ||
export declare function setImplementation(impl: DOMImplementation): void; | ||
export declare function setParsers(options: { | ||
DOMParser: typeof DOMParserInterface; | ||
XMLSerializer: typeof XMLSerializerInterface; | ||
}): void; | ||
/** | ||
@@ -10,2 +19,2 @@ * Custom implementation of https://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isEqualNode | ||
*/ | ||
export declare function isEqualNode(a: Node, b: Node): boolean; | ||
export declare function isEqualNode(a: XMLNode, b: XMLNode): boolean; |
@@ -9,3 +9,3 @@ "use strict"; | ||
exports.implementation = implementation; | ||
if (typeof window != 'undefined') { | ||
if (typeof document != 'undefined') { | ||
exports.implementation = implementation = document.implementation; | ||
@@ -15,9 +15,11 @@ exports.DOMParser = ourDOMParser = DOMParser; | ||
} | ||
else { | ||
// @ts-ignore | ||
const xmldom = require('xmldom'); | ||
exports.implementation = implementation = new xmldom.DOMImplementation(); | ||
exports.DOMParser = ourDOMParser = xmldom.DOMParser; | ||
exports.XMLSerializer = ourXMLSerializer = xmldom.XMLSerializer; | ||
function setImplementation(impl) { | ||
exports.implementation = implementation = impl; | ||
} | ||
exports.setImplementation = setImplementation; | ||
function setParsers(options) { | ||
exports.DOMParser = ourDOMParser = options.DOMParser; | ||
exports.XMLSerializer = ourXMLSerializer = options.XMLSerializer; | ||
} | ||
exports.setParsers = setParsers; | ||
function same(attr1, attr2) { | ||
@@ -24,0 +26,0 @@ if (attr1 == null) { |
@@ -6,7 +6,49 @@ import { XMLAttributePosition } from './XMLAttributePosition'; | ||
import { IterableNodeList } from './IterableNodeList'; | ||
export declare type XMLElement = XMLElementInternal & Element; | ||
interface XMLElementInternal extends XMLNode { | ||
export interface XMLElement extends XMLNode { | ||
getAttributeNode(name: string): XMLAttribute | null; | ||
getAttributeNodeNS(namespaceURI: string, localName: string): XMLAttribute | null; | ||
/** | ||
* Sets the value of element's first attribute whose qualified name is qualifiedName to value. | ||
*/ | ||
setAttribute(qualifiedName: string, value: string): void; | ||
/** | ||
* Sets the value of element's attribute whose namespace is namespace and local name is localName to value. | ||
*/ | ||
setAttributeNS(namespace: string | null, qualifiedName: string, value: string): void; | ||
setAttributeNode(attr: XMLAttribute): XMLAttribute | null; | ||
setAttributeNodeNS(attr: XMLAttribute): XMLAttribute | null; | ||
/** | ||
* Returns element's first attribute whose qualified name is qualifiedName, and null if there is no such attribute otherwise. | ||
*/ | ||
getAttribute(qualifiedName: string): string | null; | ||
/** | ||
* Returns element's attribute whose namespace is namespace and local name is localName, and null if there is no such attribute otherwise. | ||
*/ | ||
getAttributeNS(namespace: string | null, localName: string): string | null; | ||
/** | ||
* Returns true if element has an attribute whose qualified name is qualifiedName, and false otherwise. | ||
*/ | ||
hasAttribute(qualifiedName: string): boolean; | ||
/** | ||
* Returns true if element has an attribute whose namespace is namespace and local name is localName. | ||
*/ | ||
hasAttributeNS(namespace: string | null, localName: string): boolean; | ||
/** | ||
* Returns true if element has attributes, and false otherwise. | ||
*/ | ||
hasAttributes(): boolean; | ||
/** | ||
* Removes element's first attribute whose qualified name is qualifiedName. | ||
*/ | ||
removeAttribute(qualifiedName: string): void; | ||
/** | ||
* Removes element's attribute whose namespace is namespace and local name is localName. | ||
*/ | ||
removeAttributeNS(namespace: string | null, localName: string): void; | ||
removeAttributeNode(attr: XMLAttribute): XMLAttribute; | ||
ownerDocument: XMLDocument; | ||
readonly tagName: string; | ||
readonly localName: string; | ||
readonly prefix: string; | ||
readonly attributes: NamedNodeMap; | ||
/** | ||
@@ -26,2 +68,13 @@ * Returns the children. | ||
} | ||
interface NamedNodeMap<T = XMLAttribute> { | ||
readonly length: number; | ||
getNamedItem(qualifiedName: string): T | null; | ||
getNamedItemNS(namespace: string | null, localName: string): T | null; | ||
item(index: number): T | null; | ||
removeNamedItem(qualifiedName: string): T; | ||
removeNamedItemNS(namespace: string | null, localName: string): T; | ||
setNamedItem(attr: T): T | null; | ||
setNamedItemNS(attr: T): T | null; | ||
[index: number]: T; | ||
} | ||
export {}; |
import { XMLElement } from './XMLElement'; | ||
import { XMLDocument } from './XMLDocument'; | ||
import { IterableNodeList } from './IterableNodeList'; | ||
export interface XMLNode extends Node { | ||
export declare const ELEMENT_NODE = 1; | ||
export declare const ATTRIBUTE_NODE = 2; | ||
export declare const TEXT_NODE = 3; | ||
export declare const CDATA_SECTION_NODE = 4; | ||
export declare const ENTITY_REFERENCE_NODE = 5; | ||
export declare const ENTITY_NODE = 6; | ||
export declare const PROCESSING_INSTRUCTION_NODE = 7; | ||
export declare const COMMENT_NODE = 8; | ||
export declare const DOCUMENT_NODE = 9; | ||
export declare const DOCUMENT_TYPE_NODE = 10; | ||
export declare const DOCUMENT_FRAGMENT_NODE = 11; | ||
export declare const NOTATION_NODE = 12; | ||
export interface XMLNodeLike { | ||
readonly nodeType: number; | ||
readonly nodeName: string; | ||
nodeValue: string | null; | ||
} | ||
export interface XMLNode extends XMLNodeLike { | ||
/** | ||
* Returns the children. | ||
*/ | ||
readonly childNodes: IterableNodeList<XMLNode & ChildNode>; | ||
readonly childNodes: IterableNodeList<XMLNode>; | ||
/** | ||
* Returns the first child. | ||
*/ | ||
readonly firstChild: XMLNode & ChildNode | null; | ||
readonly firstChild: XMLNode | null; | ||
/** | ||
* Returns the last child. | ||
*/ | ||
readonly lastChild: XMLNode & ChildNode | null; | ||
readonly lastChild: XMLNode | null; | ||
/** | ||
* Returns the next sibling. | ||
*/ | ||
readonly nextSibling: XMLNode & ChildNode | null; | ||
readonly nextSibling: XMLNode | null; | ||
/** | ||
@@ -29,7 +46,7 @@ * Returns the node document. | ||
*/ | ||
readonly parentElement: XMLElement & HTMLElement | null; | ||
readonly parentElement: XMLElement | null; | ||
/** | ||
* Returns the parent. | ||
*/ | ||
readonly parentNode: XMLNode & ParentNode | null; | ||
readonly parentNode: XMLNode | null; | ||
/** | ||
@@ -39,2 +56,4 @@ * Returns the previous sibling. | ||
readonly previousSibling: XMLNode | null; | ||
readonly namespaceURI: string; | ||
textContent: string; | ||
/** | ||
@@ -44,2 +63,10 @@ * Returns a copy of node. If deep is true, the copy also includes the node's descendants. | ||
cloneNode(deep?: boolean): XMLNode; | ||
isEqualNode(other: XMLNode | any): boolean; | ||
hasChildNodes(): boolean; | ||
appendChild<T extends XMLNode>(newChild: T): T; | ||
insertBefore<T extends XMLNode>(newChild: T, refChild: XMLNode | null): T; | ||
} | ||
export interface XMLCharacterNode extends XMLNode { | ||
data: string; | ||
readonly length: number; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ELEMENT_NODE = 1; | ||
exports.ATTRIBUTE_NODE = 2; | ||
exports.TEXT_NODE = 3; | ||
exports.CDATA_SECTION_NODE = 4; | ||
exports.ENTITY_REFERENCE_NODE = 5; | ||
exports.ENTITY_NODE = 6; | ||
exports.PROCESSING_INSTRUCTION_NODE = 7; | ||
exports.COMMENT_NODE = 8; | ||
exports.DOCUMENT_NODE = 9; | ||
exports.DOCUMENT_TYPE_NODE = 10; | ||
exports.DOCUMENT_FRAGMENT_NODE = 11; | ||
exports.NOTATION_NODE = 12; | ||
//# sourceMappingURL=XMLNode.js.map |
@@ -0,3 +1,4 @@ | ||
import { XMLNodeLike } from './XMLNode'; | ||
export declare class XMLSerializer { | ||
serializeToString(node: Node): string; | ||
serializeToString(anyNode: XMLNodeLike): any; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const native = require("./xmldom"); | ||
const _1 = require("."); | ||
function nativeSerializeToString(node) { | ||
@@ -8,9 +9,10 @@ return new native.XMLSerializer().serializeToString(node); | ||
class XMLSerializer { | ||
serializeToString(node) { | ||
serializeToString(anyNode) { | ||
const node = anyNode; | ||
// Whitespace characters between processing instructions are lost, and browsers serialize them differently. | ||
// We write these individually, and include newline characters in between them. | ||
// Note that our DOMParser is the only one that saves the processing instructions in the DOM, browsers don't do this. | ||
if (node.nodeType == node.DOCUMENT_NODE && | ||
if (node.nodeType == _1.DOCUMENT_NODE && | ||
node.firstChild && | ||
node.firstChild.nodeType == node.PROCESSING_INSTRUCTION_NODE) { | ||
node.firstChild.nodeType == _1.PROCESSING_INSTRUCTION_NODE) { | ||
var children = node.childNodes; | ||
@@ -20,3 +22,3 @@ var result = ''; | ||
const child = children[i]; | ||
if (child.nodeType == child.TEXT_NODE) { | ||
if (child.nodeType == _1.TEXT_NODE) { | ||
// Workaround for xmldom inserting extra newlines | ||
@@ -23,0 +25,0 @@ continue; |
{ | ||
"name": "@journeyapps/domparser", | ||
"description": "Basic XML DOM parsing", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"main": "lib/node.js", | ||
"browser": "lib/index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "ts-node node_modules/jasmine/bin/jasmine test/*_spec.ts", | ||
"test": "ts-node --project ./tsconfig.test.json node_modules/jasmine/bin/jasmine test/*_spec.ts", | ||
"test:browsers": "karma start --single-run", | ||
@@ -37,4 +38,6 @@ "test:sax": "tap sax-test/*.js", | ||
"files": [ | ||
"lib" | ||
"lib", | ||
"types.d.ts", | ||
"types.js" | ||
] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
147188
61
2765