diff --git a/sax/sax-tests.ts b/sax/sax-tests.ts index 3eeecc4fc3..3d347abb9b 100644 --- a/sax/sax-tests.ts +++ b/sax/sax-tests.ts @@ -1,43 +1,89 @@ /// /// import sax = require("sax"); - -var opts: sax.SAXOptions = { - lowercase: true, - normalize: true, - xmlns: true, - position: true -}; - -var parser = sax.parser(/*strict=*/true, opts); - -parser.onerror = function(e: Error) { -}; - -parser.ontext = function(text: string) { -}; - -parser.onopentag = function(tag: sax.Tag) { -}; - -parser.onattribute = function(attr: { name: string; value: string; }) { -}; - -parser.onend = function() { -}; - -parser.write("Hello, world!").close(); - - -var saxStream = sax.createStream(/*strict=*/true, opts); - -saxStream.on("error", function(e: Error) { - this._parser.error = null; - this._parser.resume(); -}); - import fs = require("fs"); -fs.createReadStream("file.xml") - .pipe(saxStream) - .pipe(fs.createWriteStream("file-copy.xml")); +(function xmlnsTests() { + let opts: sax.SAXOptions = { + lowercase: true, + normalize: true, + xmlns: true, + position: true + }; + + let parser = sax.parser(/*strict=*/true, opts); + + parser.onerror = function(e: Error) { + }; + + parser.ontext = function(text: string) { + }; + + parser.onopentag = function(tag: sax.QualifiedTag) { + let prefix: string = tag.prefix; + let local: string = tag.local; + let uri: string = tag.uri; + let name: string = tag.name; + let isSelfClosing: boolean = tag.isSelfClosing; + + let attr: sax.QualifiedAttribute = tag.attributes["name"]; + if (attr) { + let attrPrefix: string = attr.prefix; + let attrLocal: string = attr.local; + let attrUri: string = attr.uri; + let attrName: string = attr.name; + let attrValue: string = attr.value; + } + }; + + parser.onattribute = function(attr: { name: string; value: string; }) { + }; + + parser.onend = function() { + }; + + parser.write("Hello, world!").close(); + + + let saxStream = sax.createStream(/*strict=*/true, opts); + + saxStream.on("error", function(e: Error) { + this._parser.error = null; + this._parser.resume(); + }); + + fs.createReadStream("file.xml") + .pipe(saxStream) + .pipe(fs.createWriteStream("file-copy.xml")); +})(); + +(function noXmlnsTests() { + let opts: sax.SAXOptions = { + lowercase: true, + normalize: true, + xmlns: false, + position: true + }; + + let parser = sax.parser(/*strict=*/true, opts); + + parser.onerror = function(e: Error) { + }; + + parser.ontext = function(text: string) { + }; + + parser.onopentag = function(tag: sax.Tag) { + let name: string = tag.name; + let isSelfClosing: boolean = tag.isSelfClosing; + let attrValue: string = tag.attributes["name"]; + }; + + parser.onattribute = function(attr: { name: string; value: string; }) { + }; + + parser.onend = function() { + }; + + parser.write("Hello, world!").close(); +})(); diff --git a/sax/sax.d.ts b/sax/sax.d.ts index fb3d7e7859..e4eaa8da06 100644 --- a/sax/sax.d.ts +++ b/sax/sax.d.ts @@ -16,15 +16,30 @@ declare module "sax" { position?: boolean; } - export interface Tag { + export interface QualifiedName { name: string; - attributes: { [key: string]: string }; + prefix: string; + local: string; + uri: string; + } - // Available if opt.xmlns - ns?: { [key: string]: string }; - prefix?: string; - local?: string; - uri?: string; + export interface QualifiedAttribute extends QualifiedName { + value: string; + } + + interface BaseTag { + name: string; + isSelfClosing: boolean; + } + + // Interface used when the xmlns option is set + export interface QualifiedTag extends QualifiedName, BaseTag { + ns: { [key: string]: string }; + attributes: { [key: string]: QualifiedAttribute }; + } + + export interface Tag extends BaseTag { + attributes: { [key: string]: string }; } export function parser(strict: boolean, opt: SAXOptions): SAXParser; @@ -54,7 +69,7 @@ declare module "sax" { ontext(t: string): void; ondoctype(doctype: string): void; onprocessinginstruction(node: { name: string; body: string }): void; - onopentag(tag: Tag): void; + onopentag(tag: Tag | QualifiedTag): void; onclosetag(tagName: string): void; onattribute(attr: { name: string; value: string }): void; oncomment(comment: string): void; @@ -75,4 +90,3 @@ declare module "sax" { private _parser: SAXParser; } } -