From ac01b090aefc6993e49403ac666a8e9d82862caa Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 21 Feb 2016 20:49:38 +0100 Subject: [PATCH 1/4] Added the isSelfClosing property to the Tag interface Split of the original Tag interface into Tag and QualifiedTag to represent the two modes of using sax-js (using namespace prefixes or ignoring them). The QualifiedTag interface now describes correctly the attributes property. --- sax/sax-tests.ts | 5 +++-- sax/sax.d.ts | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/sax/sax-tests.ts b/sax/sax-tests.ts index 3eeecc4fc..58fef9e38 100644 --- a/sax/sax-tests.ts +++ b/sax/sax-tests.ts @@ -17,7 +17,9 @@ parser.onerror = function(e: Error) { parser.ontext = function(text: string) { }; -parser.onopentag = function(tag: sax.Tag) { +parser.onopentag = function(tag: sax.QualifiedTag) { + for (let attr in tag.attributes) + console.log(typeof(attr)); }; parser.onattribute = function(attr: { name: string; value: string; }) { @@ -40,4 +42,3 @@ import fs = require("fs"); fs.createReadStream("file.xml") .pipe(saxStream) .pipe(fs.createWriteStream("file-copy.xml")); - diff --git a/sax/sax.d.ts b/sax/sax.d.ts index fb3d7e785..91c773db3 100644 --- a/sax/sax.d.ts +++ b/sax/sax.d.ts @@ -16,17 +16,39 @@ declare module "sax" { position?: boolean; } - export interface Tag { - name: string; - attributes: { [key: string]: string }; - - // Available if opt.xmlns - ns?: { [key: string]: string }; - prefix?: string; - local?: string; - uri?: string; + export interface QualifiedName { + name: string; + prefix: string; + local: string; + uri: string; } + export interface Attribute extends QualifiedName { + value: string; + } + + interface BaseTag { + name: string; + isSelfClosing: boolean; + } + + export interface QualifiedTag extends QualifiedName, BaseTag { + ns: { [key: string]: string }; + attributes: { [key: string]: Attribute }; + } + + export interface Tag extends BaseTag { + attributes: { [key: string]: string }; + } + + // export interface Tag extends BaseTag { + // // If opt.xmlns available + // ns?: { [key: string]: string }; + // // If opt.xmlns available, the attributes map value is an object type + // attributes: { [key: string]: string | Attribute }; + // isSelfClosing: boolean; + // } + export function parser(strict: boolean, opt: SAXOptions): SAXParser; export class SAXParser { constructor(strict: boolean, opt: SAXOptions); @@ -54,7 +76,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 +97,3 @@ declare module "sax" { private _parser: SAXParser; } } - From 4f6567dfa742b58fb7fc97b2edabe95bb0970261 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 21 Feb 2016 20:56:14 +0100 Subject: [PATCH 2/4] Added some comments to the changes in sax.d.ts --- sax/sax.d.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/sax/sax.d.ts b/sax/sax.d.ts index 91c773db3..8d0d8784b 100644 --- a/sax/sax.d.ts +++ b/sax/sax.d.ts @@ -32,6 +32,7 @@ declare module "sax" { isSelfClosing: boolean; } + // Interface used when the xmlns option is set export interface QualifiedTag extends QualifiedName, BaseTag { ns: { [key: string]: string }; attributes: { [key: string]: Attribute }; @@ -41,14 +42,6 @@ declare module "sax" { attributes: { [key: string]: string }; } - // export interface Tag extends BaseTag { - // // If opt.xmlns available - // ns?: { [key: string]: string }; - // // If opt.xmlns available, the attributes map value is an object type - // attributes: { [key: string]: string | Attribute }; - // isSelfClosing: boolean; - // } - export function parser(strict: boolean, opt: SAXOptions): SAXParser; export class SAXParser { constructor(strict: boolean, opt: SAXOptions); From 2671c5772403c07c5b9452f39a1658cfc5353c7e Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 22 Feb 2016 21:06:57 +0100 Subject: [PATCH 3/4] Fixed indentations --- sax/sax.d.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sax/sax.d.ts b/sax/sax.d.ts index 8d0d8784b..9249452c4 100644 --- a/sax/sax.d.ts +++ b/sax/sax.d.ts @@ -17,19 +17,19 @@ declare module "sax" { } export interface QualifiedName { - name: string; - prefix: string; - local: string; - uri: string; + name: string; + prefix: string; + local: string; + uri: string; } export interface Attribute extends QualifiedName { - value: string; + value: string; } interface BaseTag { - name: string; - isSelfClosing: boolean; + name: string; + isSelfClosing: boolean; } // Interface used when the xmlns option is set @@ -39,7 +39,7 @@ declare module "sax" { } export interface Tag extends BaseTag { - attributes: { [key: string]: string }; + attributes: { [key: string]: string }; } export function parser(strict: boolean, opt: SAXOptions): SAXParser; From df7f9a52652656faf977bc34f52bea4891275fe0 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 25 Feb 2016 21:24:28 +0100 Subject: [PATCH 4/4] 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 {