libxmljs: add typings for latest version

Important changes of the v0.18 version:

- The previous libxmljs-tests.ts file used a require, thus made any
  variable from the imported 'libxmljs' typed 'any'. Hence, the test file
  actually tested no type declarations at all.
  This has been fixed via an ordinary 'import' statement.
- Enabled strict null checks in tsconfig.json
- Removed some exported classes (such as XMLDocument, HTMLDocument)
which are neither used nor exported anymore in more recent libxmljs versions.
- Added null return values to functions which sometimes return null,
  e.g. prevSibling(): Node|null.
This commit is contained in:
ComFreek
2018-06-02 18:17:19 +02:00
parent e4a45e562b
commit bc93f0695f
9 changed files with 187 additions and 293 deletions

View File

@@ -1,114 +1,173 @@
// Type definitions for Libxmljs v0.14.2
// Project: https://github.com/polotek/libxmljs
// Type definitions for Libxmljs 0.18
// Project: https://github.com/libxmljs/libxmljs
// Definitions by: François de Campredon <https://github.com/fdecampredon>
// ComFreek <https://github.com/ComFreek>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node"/>
import { EventEmitter } from 'events';
export const version: string;
export const libxml_version: string;
export const libxml_parser_version: string;
// tslint:disable-next-line:strict-export-declare-modifiers
interface ParseOptions {
[optionName: string]: string;
}
import events = require('events');
export function parseXml(source: string, options?: ParseOptions): Document;
export function parseXmlString(source: string, options?: ParseOptions): Document;
export declare function parseXml(source: string): XMLDocument;
export declare function parseHtml(source: string): HTMLDocument;
export declare function parseXmlString(source: string, options?: { [key: string]: string }): XMLDocument;
export declare function parseHtmlString(source: string): HTMLDocument;
export function parseHtml(source: string, options?: ParseOptions): Document;
export function parseHtmlString(source: string, options?: ParseOptions): Document;
export function parseHtmlFragment(source: string, options?: ParseOptions): Document;
export function memoryUsage(): number;
export function nodeCount(): number;
export declare class XMLDocument {
constructor(version: number, encoding: string);
child(idx: number): Element | undefined;
export class Document {
/**
* Create a new XML Document
* @param version XML document version, defaults to 1.0
* @param encoding Encoding, defaults to utf8
*/
constructor(version?: number, encoding?: string);
errors: SyntaxError[];
child(idx: number): Element|null;
childNodes(): Element[];
errors(): SyntaxError[];
encoding(): string;
encoding(enc: string): void;
encoding(enc: string): this;
find(xpath: string): Element[];
get(xpath: string): Element | undefined;
node(name: string, content: string): Element;
root(): Element;
toString(): string;
validate(xsdDoc: XMLDocument): boolean;
validationErrors: XmlError[];
version(): Number;
get(xpath: string): Element|null;
node(name: string, content?: string): Element;
root(): Element|null;
root(newRoot: Node): Node;
toString(formatted?: boolean): string;
type(): 'document';
version(): string;
setDtd(name: string, ext: string, sys: string): void;
getDtd(): {
name: string;
externalId: string;
systemId: string;
};
}
export declare class HTMLDocument extends XMLDocument {
export class Node {
doc(): Document;
parent(): Element|Document;
/**
* The namespace or null in case of comment nodes
*/
namespace(): Namespace|null;
/**
* An array of namespaces that the object belongs to.
*
* @param local If it is true, only the namespace declarations local to this
* node are returned, rather than all of the namespaces in scope
* at this node (including the ones from the parent elements).
*/
namespaces(local?: boolean): Namespace[];
prevSibling(): Node|null;
nextSibling(): Node|null;
type(): 'comment'|'element'|'text'|'attribute';
remove(): this;
clone(): this;
/**
* Serializes the node to a string. The string will contain all contents of the node formatted as XML and can be used to print the node.
*/
toString(format?: boolean|{
declaration: boolean;
selfCloseEmpty: boolean;
whitespace: boolean;
type: 'xml'|'html'|'xhtml'
}): string;
}
export declare class Element {
constructor(doc: XMLDocument, name: string, content?: string);
export class Element extends Node {
constructor(doc: Document, name: string, content?: string);
node(name: string, content?: string): Element;
name(): string;
name(newName: string): void;
name(newName: string): this;
text(): string;
attr(name: string): Attribute;
attr(attr: Attribute): void;
attr(attrObject: { [key: string]: string; }): void;
text(newText: string): this;
attr(name: string): Attribute|null;
attr(attrObject: { [key: string]: string; }): this;
attrs(): Attribute[];
parent(): Element;
doc(): XMLDocument;
child(idx: number): Element | undefined;
childNodes(): Element[];
addChild(child: Element): Element;
nextSibling(): Element;
nextElement(): Element;
addNextSibling(siblingNode: Element): Element;
prevSibling(): Element;
prevElement(): Element;
addPrevSibling(siblingNode: Element): Element;
find(xpath: string): Element[];
find(xpath: string, ns_uri: string): Element[];
find(xpath: string, namespaces: { [key: string]: string; }): Element[];
get(xpath: string): Element | undefined;
get(xpath: string, ns_uri: string): Element | undefined;
get(xpath: string, ns_uri: { [key: string]: string; }): Element | undefined;
defineNamespace(href: string): Namespace;
defineNamespace(prefix: string, href: string): Namespace;
namespace(): Namespace;
namespace(ns: Namespace): void;
namespace(href: string): void;
namespace(prefix: string, href: string): void;
remove(): void;
doc(): Document;
child(idx: number): Node | null;
childNodes(): Node[];
/**
* @return The original element, not the child.
*/
addChild(child: Element): this;
prevElement(): Element|null;
nextElement(): Element|null;
addNextSibling(siblingNode: Node): Node;
find(xpath: string, ns_uri?: string): Node[];
find(xpath: string, namespaces: { [key: string]: string; }): Node[];
get(xpath: string, ns_uri?: string): Element|null;
defineNamespace(prefixOrHref: string, hrefInCaseOfPrefix?: string): Namespace;
namespace(): Namespace|null;
namespace(newNamespace: Namespace): this;
namespace(prefixOrHref: string, hrefInCaseOfPrefix?: string): Namespace;
replace(replacement: string): string;
replace(replacement: Element): Element;
path(): string;
type(): string;
}
export declare class Attribute {
constructor(node: Element, name: string, value: string);
constructor(node: Element, name: string, value: string, ns: Namespace);
export class Attribute {
name(): string;
namespace(): Namespace;
namespace(ns: Namespace): Namespace;
nextSibling(): Attribute;
node(): Element;
prevSibling(): Attribute;
remove(): void;
value(): string;
value(newValue: string): Attribute;
namespace(): Namespace;
remove(): void;
}
export declare class Namespace {
constructor(node: Element, prefix: string, href: string);
export class Namespace {
href(): string;
prefix(): string;
}
export declare class SaxParser extends events.EventEmitter {
export class SaxParser extends EventEmitter {
constructor();
parseString(source: string): boolean;
}
export declare class SaxPushParser extends events.EventEmitter {
export class SaxPushParser extends EventEmitter {
constructor();
push(source: string): boolean;
}
export interface XmlError {
domain: number;
code: number;
message: string;
level: number;
file?: string;
export interface SyntaxError {
domain: number|null;
code: number|null;
message: string|null;
level: number|null;
file: string|null;
line: number|null;
/**
* 1-based column number, 0 if not applicable/available.
*/
column: number;
line: number;
str1: number|null;
str2: number|null;
str3: number|null;
int1: number|null;
}

View File

@@ -1,7 +1,6 @@
import * as libxmljs from 'libxmljs';
var libxmljs = require("libxmljs");
var xml = '<?xml version="1.0" encoding="UTF-8"?>' +
const xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
'<child foo="bar">' +
'<grandchild baz="fizbuzz">grandchild content</grandchild>' +
@@ -9,40 +8,42 @@ var xml = '<?xml version="1.0" encoding="UTF-8"?>' +
'<sibling>with content!</sibling>' +
'</root>';
var xmlDoc = libxmljs.parseXml(xml);
const xmlDoc = libxmljs.parseXml(xml);
// xpath queries
var gchild = xmlDoc.get('//grandchild');
const gchild = xmlDoc.get('//grandchild')!;
console.log(gchild.text()); // prints "grandchild content"
var children = xmlDoc.root().childNodes();
var child = children[0];
const children = xmlDoc.root()!.childNodes();
const child = children[0] as libxmljs.Element;
console.log(child.attr('foo').value()); // prints "bar"
console.log(child.attr('foo')!.value()); // prints "bar"
var parser = new libxmljs.SaxParser();
const parser = new libxmljs.SaxParser();
parser.on('startDocument', null);
parser.on('startElement', null);
parser.on('startDocument', () => 0);
parser.on('startElement', () => 0);
var parser2 = new libxmljs.SaxPushParser();
const parser2 = new libxmljs.SaxPushParser();
// connect any callbacks here
parser2
.on('startDocument', null)
.on('startElement', null)
.on('startDocument', () => 0)
.on('startElement', () => 0);
var xmlChunk: any;
const xmlChunk = '';
while(xmlChunk) {
while (xmlChunk) {
parser2.push(xmlChunk);
}
var doc = new libxmljs.Document();
doc.node('root')
const doc = new libxmljs.Document();
((doc.node('root')
.node('child').attr({foo: 'bar'})
.node('grandchild', 'grandchild content').attr({baz: 'fizbuzz'})
.parent()
.parent()
) as libxmljs.Element).parent()
.node('sibling', 'with content!');
const {name, externalId, systemId} = doc.getDtd();

View File

@@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [

View File

@@ -1,79 +1 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
}
{ "extends": "dtslint/dt.json" }

View File

@@ -1,59 +1,37 @@
// Type definitions for node-libxslt
// Type definitions for node-libxslt 0.7
// Project: https://github.com/albanm/node-libxslt
// Definitions by: Alejandro Sánchez <https://github.com/alejo90>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="libxmljs" />
// TypeScript Version: 2.2
import * as xmljs from 'libxmljs';
import {
ApplyCallback,
ApplyResult,
ApplyStringCallback,
OutputFormat,
ParseCallback
} from './internal-types';
export const libxmljs: typeof xmljs;
type OutputFormat = 'document' | 'string';
export interface ApplyOptions {
outputFormat?: OutputFormat;
noWrapParams?: boolean;
}
type ApplyResult = string | xmljs.XMLDocument;
type ApplyCallback = (err: Error, result: ApplyResult) => void;
type ApplyStringCallback = (err: Error, result: string) => void;
type ApplyDocumentCallback = (err: Error, result: xmljs.XMLDocument) => void;
export interface Stylesheet {
apply(source: string, params?: Object): string;
apply(source: string, params: Object, options: ApplyOptions): ApplyResult;
apply(source: string, params: Object, options: ApplyOptions, callback: ApplyCallback): void;
apply(source: string, callback: ApplyStringCallback): void;
apply(source: xmljs.XMLDocument, params?: Object): xmljs.XMLDocument;
apply(source: xmljs.XMLDocument, params: Object, options: ApplyOptions): ApplyResult;
apply(source: xmljs.XMLDocument, params: Object, options: ApplyOptions, callback: ApplyCallback): void;
apply(source: xmljs.XMLDocument, callback: ApplyDocumentCallback): void;
applyToFile(sourcePath: string, params: Object, options: ApplyOptions, callback: ApplyStringCallback): void;
apply(source: string, params?: object): string;
apply(source: string|xmljs.Document, params: object, options: ApplyOptions): ApplyResult;
apply(source: string|xmljs.Document, params: object, options: ApplyOptions, callback: ApplyCallback): void;
apply(source: string|xmljs.Document, callback: ApplyStringCallback): void;
apply(source: xmljs.Document, params?: object): xmljs.Document;
applyToFile(sourcePath: string, params: object, options: ApplyOptions, callback: ApplyStringCallback): void;
applyToFile(sourcePath: string, callback: ApplyStringCallback): void;
}
type ParseCallback = (err: Error, stylesheet: Stylesheet) => void;
export function parse(source: string): Stylesheet;
export function parse(source: string, callback: ParseCallback): void;
export function parse(source: xmljs.XMLDocument): Stylesheet;
export function parse(source: xmljs.XMLDocument, callback: ParseCallback): void;
export function parse(source: string|xmljs.Document): Stylesheet;
export function parse(source: string|xmljs.Document, callback: ParseCallback): void;
export function parseFile(sourcePath: string, callback: ParseCallback): void;

11
types/libxslt/internal-types.d.ts vendored Normal file
View File

@@ -0,0 +1,11 @@
import * as xmljs from 'libxmljs';
import { Stylesheet } from './index';
export type OutputFormat = 'document' | 'string';
export type ApplyResult = string | xmljs.Document;
export type ApplyCallback = (err: Error, result: ApplyResult) => void;
export type ApplyStringCallback = (err: Error, result: string) => void;
export type ApplyDocumentCallback = (err: Error, result: xmljs.Document) => void;
export type ParseCallback = (err: Error, stylesheet: Stylesheet) => void;

View File

@@ -1,7 +1,7 @@
import * as libxslt from 'libxslt';
import * as libxmljs from 'libxmljs';
const document: libxmljs.XMLDocument = libxslt.libxmljs.parseXmlString('<xml></xml>');
const document: libxmljs.Document = libxslt.libxmljs.parseXmlString('<xml></xml>');
let stylesheet: libxslt.Stylesheet;
@@ -36,7 +36,7 @@ applyOptions = {
let transformedString: string;
let transformedDocument: libxmljs.XMLDocument;
let transformedDocument: libxmljs.Document;
transformedString = stylesheet.apply('<xml></xml>');

View File

@@ -18,6 +18,7 @@
},
"files": [
"index.d.ts",
"internal-types.d.ts",
"libxslt-tests.ts"
]
}

View File

@@ -1,79 +1 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
}
{ "extends": "dtslint/dt.json" }