From df7f9a52652656faf977bc34f52bea4891275fe0 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 25 Feb 2016 21:24:28 +0100 Subject: [PATCH] Interface Attribute renamed to QualifiedAttribute for consistency Added type tests --- sax/sax-tests.ts | 125 ++++++++++++++++++++++++++++++++--------------- sax/sax.d.ts | 4 +- 2 files changed, 87 insertions(+), 42 deletions(-) diff --git a/sax/sax-tests.ts b/sax/sax-tests.ts index 58fef9e38..3d347abb9 100644 --- a/sax/sax-tests.ts +++ b/sax/sax-tests.ts @@ -1,44 +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.QualifiedTag) { - for (let attr in tag.attributes) - console.log(typeof(attr)); -}; - -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 9249452c4..e4eaa8da0 100644 --- a/sax/sax.d.ts +++ b/sax/sax.d.ts @@ -23,7 +23,7 @@ declare module "sax" { uri: string; } - export interface Attribute extends QualifiedName { + export interface QualifiedAttribute extends QualifiedName { value: string; } @@ -35,7 +35,7 @@ declare module "sax" { // Interface used when the xmlns option is set export interface QualifiedTag extends QualifiedName, BaseTag { ns: { [key: string]: string }; - attributes: { [key: string]: Attribute }; + attributes: { [key: string]: QualifiedAttribute }; } export interface Tag extends BaseTag {