From d73421ed56f0f0a6d3469cb10cd0a0776f589f4e Mon Sep 17 00:00:00 2001 From: masataka Date: Thu, 15 Oct 2020 09:41:26 +0900 Subject: [PATCH] Add type to pull event object's known properties --- README.md | 4 ++-- context.ts | 2 ++ context_test.ts | 2 ++ handler.ts | 2 ++ handler_test.ts | 2 ++ mod.ts | 2 ++ parser.ts | 22 +++++++++++++++++++++- parser_test.ts | 8 +++++--- 8 files changed, 38 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 2ae3f96..b2de203 100644 --- a/README.md +++ b/README.md @@ -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(); @@ -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(); diff --git a/context.ts b/context.ts index 66b1eba..9fb07a4 100644 --- a/context.ts +++ b/context.ts @@ -1,3 +1,5 @@ +// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. + export class QName { private _qName: string; protected _prefix: string; diff --git a/context_test.ts b/context_test.ts index 38c928b..a5a5ebe 100644 --- a/context_test.ts +++ b/context_test.ts @@ -1,3 +1,5 @@ +// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. + import { assertEquals } from 'https://deno.land/std@0.74.0/testing/asserts.ts'; import { Attribute, AttributeInfo, Element, ElementInfo, XMLParseContext } from './context.ts'; diff --git a/handler.ts b/handler.ts index d7895d0..be1034f 100644 --- a/handler.ts +++ b/handler.ts @@ -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]/ diff --git a/handler_test.ts b/handler_test.ts index ee53343..61d7627 100644 --- a/handler_test.ts +++ b/handler_test.ts @@ -1,3 +1,5 @@ +// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. + import { assertEquals, assertThrows } from 'https://deno.land/std@0.74.0/testing/asserts.ts'; import { XMLParseContext } from './context.ts'; import * as handler from './handler.ts'; diff --git a/mod.ts b/mod.ts index 2b948a8..3302b2a 100644 --- a/mod.ts +++ b/mod.ts @@ -1,3 +1,5 @@ +// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. + export * from './parser.ts'; // to implement a new handler diff --git a/parser.ts b/parser.ts index f4c353c..1fcb5ff 100644 --- a/parser.ts +++ b/parser.ts @@ -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'; @@ -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; @@ -214,8 +218,24 @@ export class SAXParser extends ParserBase implements UnderlyingSink } } +/** + * 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; } diff --git a/parser_test.ts b/parser_test.ts index 8559c7c..ff746cf 100644 --- a/parser_test.ts +++ b/parser_test.ts @@ -1,3 +1,5 @@ +// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. + import { assert, assertEquals, assertThrows } from 'https://deno.land/std@0.74.0/testing/asserts.ts'; import { ElementInfo, XMLParseContext, XMLParseEvent, XMLParseError } from "./context.ts"; import { ParserBase, SAXParser, PullParser, PullResult } from './parser.ts'; @@ -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) {