From bf023bc2c372d0de8918157b1d9b21b7f8bd947d Mon Sep 17 00:00:00 2001 From: Ryan Manns Date: Tue, 28 Aug 2018 20:36:26 -0400 Subject: [PATCH 1/6] Add namespaces as optional param to Document.get See docs here https://github.com/libxmljs/libxmljs/wiki/Namespaces --- types/libxmljs/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/libxmljs/index.d.ts b/types/libxmljs/index.d.ts index 7fc9e9e2bf..a07492c05d 100644 --- a/types/libxmljs/index.d.ts +++ b/types/libxmljs/index.d.ts @@ -41,7 +41,7 @@ export class Document { encoding(): string; encoding(enc: string): this; find(xpath: string): Element[]; - get(xpath: string): Element|null; + get(xpath: string, namespaces?: { [key: string]: string; }): Element|null; node(name: string, content?: string): Element; root(): Element|null; root(newRoot: Node): Node; From fc97008aabfb6aeab26fecc4f31c1d020a750f45 Mon Sep 17 00:00:00 2001 From: rycornell Date: Tue, 28 Aug 2018 20:56:34 -0400 Subject: [PATCH 2/6] Add a test for Document.get with namespaces --- types/libxmljs/libxmljs-tests.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/types/libxmljs/libxmljs-tests.ts b/types/libxmljs/libxmljs-tests.ts index 367d74c69d..72c480614e 100644 --- a/types/libxmljs/libxmljs-tests.ts +++ b/types/libxmljs/libxmljs-tests.ts @@ -47,3 +47,18 @@ const doc = new libxmljs.Document(); .node('sibling', 'with content!'); const {name, externalId, systemId} = doc.getDtd(); + +const xmlWithNs = '' + + '' + + '' + + 'grandchild content' + + '' + + 'with content!' + + ''; + +const xmlDocWithNs = libxmljs.parseXml(xmlWithNs); + +// xpath queries +const gchildWithNs = xmlDocWithNs.get('//a:grandchild', {"a": "http://test.com/test"})!; + +console.log(gchildWithNs.text()); // prints "grandchild content" \ No newline at end of file From 5d79b3151a44ec9a34527762e42ba9c21360ce35 Mon Sep 17 00:00:00 2001 From: rycornell Date: Tue, 28 Aug 2018 21:05:57 -0400 Subject: [PATCH 3/6] fix lint errors --- types/libxmljs/libxmljs-tests.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/libxmljs/libxmljs-tests.ts b/types/libxmljs/libxmljs-tests.ts index 72c480614e..d0d8fc635c 100644 --- a/types/libxmljs/libxmljs-tests.ts +++ b/types/libxmljs/libxmljs-tests.ts @@ -59,6 +59,6 @@ const xmlWithNs = '' + const xmlDocWithNs = libxmljs.parseXml(xmlWithNs); // xpath queries -const gchildWithNs = xmlDocWithNs.get('//a:grandchild', {"a": "http://test.com/test"})!; - -console.log(gchildWithNs.text()); // prints "grandchild content" \ No newline at end of file +const gchildWithNs = xmlDocWithNs.get('//a:grandchild', {a: "http://test.com/test"})!; + +console.log(gchildWithNs.text()); // prints "grandchild content" From b6c00e69c22a439b48eeb4cc5f6ec08cf761cb34 Mon Sep 17 00:00:00 2001 From: Ryan Manns Date: Tue, 28 Aug 2018 21:10:50 -0400 Subject: [PATCH 4/6] remove whitespace --- types/libxmljs/libxmljs-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/libxmljs/libxmljs-tests.ts b/types/libxmljs/libxmljs-tests.ts index d0d8fc635c..93157c13fc 100644 --- a/types/libxmljs/libxmljs-tests.ts +++ b/types/libxmljs/libxmljs-tests.ts @@ -60,5 +60,5 @@ const xmlDocWithNs = libxmljs.parseXml(xmlWithNs); // xpath queries const gchildWithNs = xmlDocWithNs.get('//a:grandchild', {a: "http://test.com/test"})!; - + console.log(gchildWithNs.text()); // prints "grandchild content" From 9a4e3396e8695026f8ec91a7511ac5b60d52a75f Mon Sep 17 00:00:00 2001 From: Ryan Manns Date: Wed, 29 Aug 2018 09:13:35 -0400 Subject: [PATCH 5/6] use single quotes consistently --- types/libxmljs/libxmljs-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/libxmljs/libxmljs-tests.ts b/types/libxmljs/libxmljs-tests.ts index 93157c13fc..fe720cc242 100644 --- a/types/libxmljs/libxmljs-tests.ts +++ b/types/libxmljs/libxmljs-tests.ts @@ -59,6 +59,6 @@ const xmlWithNs = '' + const xmlDocWithNs = libxmljs.parseXml(xmlWithNs); // xpath queries -const gchildWithNs = xmlDocWithNs.get('//a:grandchild', {a: "http://test.com/test"})!; +const gchildWithNs = xmlDocWithNs.get('//a:grandchild', {a: 'http://test.com/test'})!; console.log(gchildWithNs.text()); // prints "grandchild content" From 667d79bc358d3ed4d3426ba02ca08506f45c7c15 Mon Sep 17 00:00:00 2001 From: Ryan Manns Date: Wed, 29 Aug 2018 19:27:11 -0400 Subject: [PATCH 6/6] Create interface for string dictionary --- types/libxmljs/index.d.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/types/libxmljs/index.d.ts b/types/libxmljs/index.d.ts index a07492c05d..4305c662a8 100644 --- a/types/libxmljs/index.d.ts +++ b/types/libxmljs/index.d.ts @@ -12,16 +12,14 @@ export const libxml_version: string; export const libxml_parser_version: string; // tslint:disable-next-line:strict-export-declare-modifiers -interface ParseOptions { - [optionName: string]: string; -} +interface StringMap { [key: string]: string; } -export function parseXml(source: string, options?: ParseOptions): Document; -export function parseXmlString(source: string, options?: ParseOptions): Document; +export function parseXml(source: string, options?: StringMap): Document; +export function parseXmlString(source: string, options?: StringMap): Document; -export function parseHtml(source: string, options?: ParseOptions): Document; -export function parseHtmlString(source: string, options?: ParseOptions): Document; -export function parseHtmlFragment(source: string, options?: ParseOptions): Document; +export function parseHtml(source: string, options?: StringMap): Document; +export function parseHtmlString(source: string, options?: StringMap): Document; +export function parseHtmlFragment(source: string, options?: StringMap): Document; export function memoryUsage(): number; export function nodeCount(): number; @@ -41,7 +39,7 @@ export class Document { encoding(): string; encoding(enc: string): this; find(xpath: string): Element[]; - get(xpath: string, namespaces?: { [key: string]: string; }): Element|null; + get(xpath: string, namespaces?: StringMap): Element|null; node(name: string, content?: string): Element; root(): Element|null; root(newRoot: Node): Node; @@ -98,7 +96,7 @@ export class Element extends Node { text(): string; text(newText: string): this; attr(name: string): Attribute|null; - attr(attrObject: { [key: string]: string; }): this; + attr(attrObject: StringMap): this; attrs(): Attribute[]; doc(): Document; @@ -115,7 +113,7 @@ export class Element extends Node { addNextSibling(siblingNode: Node): Node; find(xpath: string, ns_uri?: string): Node[]; - find(xpath: string, namespaces: { [key: string]: string; }): Node[]; + find(xpath: string, namespaces: StringMap): Node[]; get(xpath: string, ns_uri?: string): Element|null; defineNamespace(prefixOrHref: string, hrefInCaseOfPrefix?: string): Namespace;