Skip to content

Commit

Permalink
Add type to pull event object's known properties
Browse files Browse the repository at this point in the history
  • Loading branch information
masataka committed Oct 15, 2020
1 parent 4960621 commit d73421e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ When using in SAX style, create an instance of the parser and register the liste
The XML to be parsed is specified by Deno.Reader, UINT8 array, or a character string.

```typescript
import { SAXParser } from 'https://denopkg.com/masataka/xmlp/mod.ts';
import { SAXParser } from 'https://deno.land/x/xmlp/mod.ts';

// create a SAX parser instance
const parser = new SAXParser();
Expand Down Expand Up @@ -59,7 +59,7 @@ I think it's more interesting to write the Pull style than the SAX. This Pull pa
Currently the Pull parser supports Uint8 arrays and strings, not Deno.Reader.

```typeScript
import { PullParser } from 'https://denopkg.com/masataka/xmlp/mod.ts';
import { PullParser } from 'https://deno.land/x/xmlp/mod.ts';

// create a pull parser instance
const parser = new PullParser();
Expand Down
2 changes: 2 additions & 0 deletions context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.

export class QName {
private _qName: string;
protected _prefix: string;
Expand Down
2 changes: 2 additions & 0 deletions context_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.

import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts';
import { Attribute, AttributeInfo, Element, ElementInfo, XMLParseContext } from './context.ts';

Expand Down
2 changes: 2 additions & 0 deletions handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.

import { XMLParseContext, XMLParseEvent, XMLParseError, ElementInfo } from './context.ts';

const NAME_HEAD = /[:_A-Za-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/
Expand Down
2 changes: 2 additions & 0 deletions handler_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.

import { assertEquals, assertThrows } from 'https://deno.land/[email protected]/testing/asserts.ts';
import { XMLParseContext } from './context.ts';
import * as handler from './handler.ts';
Expand Down
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.

export * from './parser.ts';

// to implement a new handler
Expand Down
22 changes: 21 additions & 1 deletion parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.

import { Locatable, XMLParseHandler, XMLParseContext, XMLPosition, ElementInfo, XMLParseEvent, XMLParseError } from './context.ts';
import * as handler from './handler.ts';

Expand Down Expand Up @@ -104,7 +106,9 @@ export abstract class ParserBase implements Locatable {
return this._position;
}
}

/**
* Custom SAX event listener type, register by {@code SAXParser#on}.
*/
// deno-lint-ignore no-explicit-any
export type SAXListener = (...arg: any[]) => void;

Expand Down Expand Up @@ -214,8 +218,24 @@ export class SAXParser extends ParserBase implements UnderlyingSink<Uint8Array>
}
}

/**
* PullParser returns a iterator of this.
*/
export type PullResult = {
/** event name */
name: string;

// known properties
procInst?: string;
sgmlDecl?: string;
text?: string;
element?: ElementInfo;
cdata?: boolean;
doctype?: string;
ns?: string;
uri?: string;
comment?: string;

// deno-lint-ignore no-explicit-any
[key: string]: any;
}
Expand Down
8 changes: 5 additions & 3 deletions parser_test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.

import { assert, assertEquals, assertThrows } from 'https://deno.land/[email protected]/testing/asserts.ts';
import { ElementInfo, XMLParseContext, XMLParseEvent, XMLParseError } from "./context.ts";
import { ParserBase, SAXParser, PullParser, PullResult } from './parser.ts';
Expand Down Expand Up @@ -110,9 +112,9 @@ Deno.test('PullParser', async () => {
assertEquals(events.next().value, { name: 'start_document' });
assertEquals(events.next().value, { name: 'start_prefix_mapping', ns: 'atom', uri: 'http://www.w3.org/2005/Atom' });
assertEquals(events.next().value, { name: 'start_prefix_mapping', ns: 'm', uri: 'https://xmlp.test/m' });
assertEquals((events.next().value as PullResult).element.qName, 'rss');
assertEquals((events.next().value as PullResult).element.qName, 'channel');
assertEquals((events.next().value as PullResult).element.qName, 'title');
assertEquals((events.next().value as PullResult).element!.qName, 'rss');
assertEquals((events.next().value as PullResult).element!.qName, 'channel');
assertEquals((events.next().value as PullResult).element!.qName, 'title');
assertEquals((events.next().value as PullResult).text, 'XML Parser for Deno');
assertEquals((events.next().value as PullResult).name, 'end_element');
while(true) {
Expand Down

0 comments on commit d73421e

Please sign in to comment.