From ac01b090aefc6993e49403ac666a8e9d82862caa Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 21 Feb 2016 20:49:38 +0100 Subject: [PATCH] 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; } } -