diff --git a/types/xmldoc/index.d.ts b/types/xmldoc/index.d.ts index 8590c68e16..7d521ac237 100644 --- a/types/xmldoc/index.d.ts +++ b/types/xmldoc/index.d.ts @@ -1,11 +1,13 @@ -// Type definitions for xmldoc 0.5.1 +// Type definitions for xmldoc 1.1.0 // Project: https://www.npmjs.com/package/xmldoc -// Definitions by: Xavier Stouder +// Definitions by: Xavier Stouder , Andrew Sheehan // Definitions: https://github.com/DefinitelyTyped/ export class XmlDocument { constructor(xmlString: string); + doctype: string; + eachChild(func: (child: XmlElement, index?: number, array?: XmlElement[]) => void): void; childNamed(name: string): XmlElement; childrenNamed(name: string): XmlElement[]; @@ -13,8 +15,19 @@ export class XmlDocument { descendantWithPath(path: string): XmlElement; valueWithPath(path: string): string; toString(opts?: XmlOptions): string; + toStringWithIndent(indent: string, opts?: XmlOptions): string; } + export class XmlElement { + constructor(tag: XmlElement); + + name: string; + attr: XmlAttributes; + val: string; + children: XmlElement[]; + firstChild: XmlElement; + lastChild: XmlElement; + eachChild(func: (child: XmlElement, index?: number, array?: XmlElement[]) => void): void; childNamed(name: string): XmlElement; childrenNamed(name: string): XmlElement[]; @@ -22,10 +35,42 @@ export class XmlElement { descendantWithPath(path: string): XmlElement; valueWithPath(path: string): string; toString(opts?: XmlOptions): string; + toStringWithIndent(indent: string, opts?: XmlOptions): string; +} + +export class XmlTextNode { + constructor(text: string); + + toString(opts?: XmlOptions): string; + toStringWithIndent(indent: string, opts?: XmlOptions): string; +} + +export class XmlCDataNode { + constructor(cdata: string); + + toString(opts?: XmlOptions): string; + toStringWithIndent(indent: string, opts?: XmlOptions): string; +} + +export class XmlCommentNode { + constructor(comment: string); + + toString(opts?: XmlOptions): string; + toStringWithIndent(indent: string, opts?: XmlOptions): string; } export interface XmlOptions { compressed?: boolean; - trimmed?: boolean; + html?: boolean; preserveWhitespace?: boolean; + trimmed?: boolean; +} + +export interface XmlTag { + name: string; + attributes: XmlAttributes; +} + +export interface XmlAttributes { + [key: string]: string; } diff --git a/types/xmldoc/xmldoc-tests.ts b/types/xmldoc/xmldoc-tests.ts index 09a3f35d9e..17942e2658 100644 --- a/types/xmldoc/xmldoc-tests.ts +++ b/types/xmldoc/xmldoc-tests.ts @@ -1,4 +1,44 @@ import * as xmldoc from "xmldoc"; -const doc = new xmldoc.XmlDocument("magic"); -console.log(doc.toString()); \ No newline at end of file +// +// https://github.com/nfarina/xmldoc#usage +// +const document = new xmldoc.XmlDocument("xml"); + +// +// https://github.com/nfarina/xmldoc#descendantwithpathpath +// +const bookNode = new xmldoc.XmlDocument(` + + + George R. R. Martin + ... + + ... + `); + +const nameNode = bookNode.descendantWithPath("author.name"); // return node + +// +// https://github.com/nfarina/xmldoc#valuewithpathpath +// +const authorName = bookNode.valueWithPath("author.name"); // return "George R. R. Martin" +const authorIsProper = bookNode.valueWithPath("author.name@isProper"); // return "true" + +// +// https://github.com/nfarina/xmldoc#tostringoptions +// +document.toString({ compressed: true }) // strips indents and linebreaks +document.toString({ trimmed: true }) // trims long strings for easier debugging +document.toString({ preserveWhitespace: true }) // prevents whitespace being removed from around element values + +const xml = "looooooong value"; +console.log("My document: \n" + new xmldoc.XmlDocument(xml).toString({ trimmed: true })) +/* Prints: + +My Document: + + loooooooo… + + +*/