diff --git a/README.md b/README.md index f263e56d3..52df37825 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ List of Definitions * [ko.editables](http://romanych.github.com/ko.editables/) (by [Oisin Grehan](https://github.com/oising)) * [KoLite](https://github.com/CodeSeven/kolite) (by [Boris Yankov](https://github.com/borisyankov)) * [Leaflet](https://github.com/Leaflet/Leaflet) (by [Vladimir](https://github.com/rgripper)) +* [Libxmljs](https://github.com/polotek/libxmljs) (by [François de Campredon](https://github.com/fdecampredon)) * [linq.js](http://linqjs.codeplex.com/) (by [Marcin Najder](https://github.com/marcinnajder)) * [Marked](https://github.com/chjj/marked) (by [William Orr](https://github.com/worr)) * [Modernizr](http://modernizr.com/) (by [Boris Yankov](https://github.com/borisyankov)) diff --git a/libxmljs/libxmljs-tests.ts b/libxmljs/libxmljs-tests.ts new file mode 100644 index 000000000..bf73a6d98 --- /dev/null +++ b/libxmljs/libxmljs-tests.ts @@ -0,0 +1,49 @@ +/// +/// + +var libxmljs = require("libxmljs"); +var xml = '' + + '' + + '' + + 'grandchild content' + + '' + + 'with content!' + + ''; + +var xmlDoc = libxmljs.parseXmlString(xml); + +// xpath queries +var gchild = xmlDoc.get('//grandchild'); + +console.log(gchild.text()); // prints "grandchild content" + +var children = xmlDoc.root().childNodes(); +var child = children[0]; + +console.log(child.attr('foo').value()); // prints "bar" + +var parser = new libxmljs.SaxParser(); + +parser.on('startDocument', null); +parser.on('startElement', null); + +var parser2 = new libxmljs.SaxPushParser(); + +// connect any callbacks here +parser2 + .on('startDocument', null) + .on('startElement', null) + +var xmlChunk: any; + +while(xmlChunk) { + parser2.push(xmlChunk); +} + +var doc = new libxmljs.Document(); + doc.node('root') + .node('child').attr({foo: 'bar'}) + .node('grandchild', 'grandchild content').attr({baz: 'fizbuzz'}) + .parent() + .parent() + .node('sibling', 'with content!'); diff --git a/libxmljs/libxmljs.d.ts b/libxmljs/libxmljs.d.ts new file mode 100644 index 000000000..b26aeabf1 --- /dev/null +++ b/libxmljs/libxmljs.d.ts @@ -0,0 +1,111 @@ +// Type definitions for Libxmljs +// Project: https://github.com/polotek/libxmljs +// Definitions by: François de Campredon +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "libxmljs" { + export function parseXmlString(source:string):XMLDocument; + export function parseHtmlString(source:string):HTMLDocument; + + + export class XMLDocument { + constructor(version:number, encoding:string); + child(idx:number):Element; + childNodes():Element[]; + errors():SyntaxError[]; + encoding():string; + encoding(enc:string):void; + find(xpath:string):Element[]; + get(xpath:string):Element; + node(name:string, content:string):Element; + root():Element; + toString():string; + version():Number; + } + + export class HTMLDocument extends XMLDocument { + + } + + + export class Element { + constructor(doc:XMLDocument, name:string, content?:string); + name():string; + name(newName:string):void; + text():string; + attr(name:string):string; + attr(attr:Attribute); + attr(attrObject:{[key:string]:string;}):void; + attrs():Attribute[]; + parent():Element; + doc():XMLDocument; + child(idx:number):Element; + childNodes():Element[]; + addChild(child:Element); + nextSibling():Element; + nextElement():Element; + addNextSibling(siblingNode:Element):Element; + prevSibling():Element; + prevElement():Element; + addPrevSibling(siblingNode:Element); + find(xpath:string):Element[]; + find(xpath:string, ns_uri:string):Element[]; + get(xpath:string, ns_uri:string):Element; + find(xpath:string, namespaces:{[key:string]:string;}):Element[]; + get(xpath, ns_uri:{[key:string]:string;}):Element; + defineNamespace(href:string):Namespace; + defineNamespace(prefix:string, href:string):Namespace; + namespace():Namespace; + namespace(ns:Namespace):void; + namespace(href:string):void; + namespace(prefix:string, href:string):void; + remove():void; + path():string; + type():string; + } + + + export class Attribute { + constructor(node:Element, name:string, value:string); + constructor(node:Element, name:string, value:string, ns:Namespace); + name():string; + namespace():Namespace; + namespace(ns:Namespace):Namespace; + nextSibling():Attribute; + node():Element; + prevSibling():Attribute; + remove():void; + value():string; + } + + export class Namespace { + constructor(node:Element, prefix:string, href:string); + href():string; + prefix():string; + } + + export class SaxParser { + parseString(source:string):bool; + addListener(event: string, listener: Function); + on(event: string, listener: Function): any; + once(event: string, listener: Function): void; + removeListener(event: string, listener: Function): void; + removeAllListener(event: string): void; + setMaxListeners(n: number): void; + listeners(event: string): { Function; }[]; + emit(event: string, arg1?: any, arg2?: any): void; + } + + + export class SaxPushParser { + push(source:string):bool; + addListener(event: string, listener: Function); + on(event: string, listener: Function): any; + once(event: string, listener: Function): void; + removeListener(event: string, listener: Function): void; + removeAllListener(event: string): void; + setMaxListeners(n: number): void; + listeners(event: string): { Function; }[]; + emit(event: string, arg1?: any, arg2?: any): void; + } +} \ No newline at end of file