Merge pull request #8200 from mtrivolli/mtrivolli-sax

Updated definitions for sax-js
This commit is contained in:
Masahiro Wakame
2016-02-26 20:55:56 +09:00
2 changed files with 107 additions and 47 deletions

View File

@@ -1,43 +1,89 @@
/// <reference path="../node/node.d.ts" />
/// <reference path="./sax.d.ts" />
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("<xml>Hello, <who name=\"world\">world</who>!</xml>").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("<xml>Hello, <who name=\"world\">world</who>!</xml>").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("<xml>Hello, <who name=\"world\">world</who>!</xml>").close();
})();

32
sax/sax.d.ts vendored
View File

@@ -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;
}
}