Skip to content

Commit 8e0e896

Browse files
committedOct 17, 2020
fix. import / export
1 parent 80230ab commit 8e0e896

7 files changed

+91
-38
lines changed
 

‎context.ts

+11-12
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class Element extends QName {
4444
private _parent?: Element;
4545

4646
uri?: string;
47-
standAlone = false;
47+
emptyElement = false;
4848

4949
constructor(name: string, parent?: Element) {
5050
super(name);
@@ -78,7 +78,7 @@ export class Element extends QName {
7878
}
7979
}
8080

81-
// read only Attribute
81+
/** information of parsed attribute, readonly. */
8282
export class AttributeInfo extends QName {
8383
private _attribute: Attribute;
8484

@@ -100,7 +100,7 @@ export class AttributeInfo extends QName {
100100
}
101101
}
102102

103-
// read only Element
103+
/** information of parsed element, readonly. */
104104
export class ElementInfo extends QName {
105105
private _element: Element;
106106

@@ -127,12 +127,8 @@ export class ElementInfo extends QName {
127127
});
128128
}
129129

130-
get prefixMappings(): { ns: string, uri: string }[] {
131-
return this._element.prefixMappings;
132-
}
133-
134-
get standAlone(): boolean {
135-
return this._element.standAlone;
130+
get emptyElement(): boolean {
131+
return this._element.emptyElement;
136132
}
137133
}
138134

@@ -141,20 +137,20 @@ export interface XMLPosition {
141137
column: number;
142138
}
143139

144-
export interface Locatable {
140+
export interface XMLLocator {
145141
position: XMLPosition;
146142
}
147143

148144
export class XMLParseContext {
149-
private _locator?: Locatable;
145+
private _locator?: XMLLocator;
150146
private _memento = '';
151147
private _elementStack: Element[] = [];
152148
private _namespaces: { [ns: string]: string | undefined } = {};
153149

154150
quote: '' | '"' | '\'' = '';
155151
state = 'BEFORE_DOCUMENT';
156152

157-
constructor(locator?: Locatable) {
153+
constructor(locator?: XMLLocator) {
158154
this._locator = locator;
159155
}
160156

@@ -211,6 +207,7 @@ export interface XMLParseHandler {
211207
(cx: XMLParseContext, c: string): XMLParseEvent[];
212208
}
213209

210+
/** XML parsing error. the parser convert this error to "error" event. */
214211
export class XMLParseError extends Error {
215212
private _position: XMLPosition;
216213

@@ -219,10 +216,12 @@ export class XMLParseError extends Error {
219216
this._position = cx.position;
220217
}
221218

219+
/** line number on XML source */
222220
get line(): number {
223221
return this._position.line;
224222
}
225223

224+
/** column number on XML source */
226225
get column(): number {
227226
return this._position.column;
228227
}

‎context_test.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.
22

3-
import { assertEquals } from 'https://deno.land/std@0.74.0/testing/asserts.ts';
4-
import { Attribute, AttributeInfo, Element, ElementInfo, XMLParseContext } from './context.ts';
3+
import {
4+
assertEquals,
5+
} from 'https://deno.land/std@0.74.0/testing/asserts.ts';
6+
7+
import {
8+
Attribute,
9+
AttributeInfo,
10+
Element,
11+
ElementInfo,
12+
XMLParseContext,
13+
} from './context.ts';
514

615
Deno.test('Attribute xmlns', () => {
716
const attribute = new Attribute('xmlns');
@@ -53,15 +62,15 @@ Deno.test('ElementInfo', () => {
5362
const element = new Element('a:b', parent);
5463
element.newAttribute('c');
5564
element.uri = 'https://xmlp.test/xmlns/a';
56-
element.standAlone = true;
65+
element.emptyElement = true;
5766
const elementInfo = new ElementInfo(element);
5867
assertEquals(elementInfo.qName, 'a:b');
5968
assertEquals(elementInfo.prefix, 'a');
6069
assertEquals(elementInfo.localPart, 'b');
6170
assertEquals(elementInfo.uri, 'https://xmlp.test/xmlns/a');
6271
assertEquals(elementInfo.parent!.qName, 'parent');
6372
assertEquals(elementInfo.attributes[0].qName, 'c');
64-
assertEquals(elementInfo.standAlone, true);
73+
assertEquals(elementInfo.emptyElement, true);
6574
});
6675

6776
Deno.test('XMLParseContext memento & appendMemento & clearMemento', () => {

‎handler.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.
22

3-
import { XMLParseContext, XMLParseEvent, XMLParseError, ElementInfo } from './context.ts';
3+
import {
4+
XMLParseContext,
5+
XMLParseEvent,
6+
XMLParseError,
7+
ElementInfo,
8+
} from './context.ts';
49

510
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]/
611
const NAME_BODY = /[:_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\u00B7\u0300-\u036F\u203F-\u2040.\d-]/
@@ -291,7 +296,7 @@ export function handleEmptyElementTag(cx: XMLParseContext, c: string): XMLParseE
291296
throw new XMLParseError('Forward-slash in start-tag not followed by &gt', cx);
292297
}
293298
const element = cx.peekElement()!;
294-
element.standAlone = true;
299+
element.emptyElement = true;
295300
events = emitStartElement(cx).concat(emitEndElement(cx, element.qName));
296301
cx.state = 'GENERAL_STUFF';
297302
return events;

‎handler_test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.
22

3-
import { assertEquals, assertThrows } from 'https://deno.land/std@0.74.0/testing/asserts.ts';
4-
import { XMLParseContext } from './context.ts';
3+
import {
4+
assertEquals,
5+
assertThrows,
6+
} from 'https://deno.land/std@0.74.0/testing/asserts.ts';
7+
8+
import {
9+
XMLParseContext,
10+
} from './context.ts';
11+
512
import * as handler from './handler.ts';
613

714
Deno.test('handleBeforeDocument', () => {

‎mod.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.
22

3-
export * from './parser.ts';
3+
export {
4+
SAXParser,
5+
PullParser,
6+
} from './parser.ts';
7+
export type {
8+
PullResult,
9+
} from './parser.ts';
410

5-
// to implement a new handler
6-
export * from './context.ts';
11+
export {
12+
AttributeInfo,
13+
ElementInfo,
14+
XMLParseError,
15+
} from './context.ts';

‎parser.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.
22

3-
import { Locatable, XMLParseHandler, XMLParseContext, XMLPosition, ElementInfo, XMLParseEvent, XMLParseError } from './context.ts';
3+
import {
4+
XMLParseHandler,
5+
XMLParseContext,
6+
XMLParseEvent,
7+
XMLParseError,
8+
XMLLocator,
9+
XMLPosition,
10+
ElementInfo,
11+
} from './context.ts';
12+
413
import * as handler from './handler.ts';
514

6-
export abstract class ParserBase implements Locatable {
15+
export abstract class ParserBase implements XMLLocator {
716
private _cx = new XMLParseContext(this);
817
private _handlers: { [state: string]: XMLParseHandler } = {};
918
private _chunk = '';
@@ -106,19 +115,13 @@ export abstract class ParserBase implements Locatable {
106115
return this._position;
107116
}
108117
}
109-
/**
110-
* Custom SAX event listener type, register by {@code SAXParser#on}.
111-
*/
112-
export interface SAXListener {
113-
// deno-lint-ignore no-explicit-any
114-
(...arg: any[]): void;
115-
}
116118

117119
/**
118120
* SAX-style XML parser.
119121
*/
120122
export class SAXParser extends ParserBase implements UnderlyingSink<Uint8Array> {
121-
private _listeners: { [name: string]: SAXListener[] } = {};
123+
// deno-lint-ignore no-explicit-any
124+
private _listeners: { [name: string]: ((...arg: any[]) => void)[] } = {};
122125
private _controller?: WritableStreamDefaultController;
123126

124127
protected fireListeners(event: XMLParseEvent) {
@@ -152,6 +155,11 @@ export class SAXParser extends ParserBase implements UnderlyingSink<Uint8Array>
152155
}
153156
}
154157

158+
/**
159+
* implements UnderlyingSink<Uint8Array>
160+
* @param chunk XML data chunk
161+
* @param controller error reporter, Deno writable stream uses internal.
162+
*/
155163
write(chunk: Uint8Array, controller?: WritableStreamDefaultController) {
156164
try {
157165
this._controller = controller;
@@ -210,9 +218,9 @@ export class SAXParser extends ParserBase implements UnderlyingSink<Uint8Array>
210218
on(event: 'end_element', listener: (element: ElementInfo) => void): this;
211219
on(event: 'end_prefix_mapping', listener: (ns: string, uri: string) => void): this;
212220
on(event: 'end_document', listener: () => void): this;
221+
on(event: 'error', listener: (error: XMLParseError) => void): this;
213222
// deno-lint-ignore no-explicit-any
214-
on(event: 'error', listener: (error: any) => void): this;
215-
on(event: string, listener: SAXListener): this {
223+
on(event: string, listener: (...arg: any[]) => void): this {
216224
const list = this._listeners[event] || [];
217225
list.push(listener);
218226
this._listeners[event] = list;

‎parser_test.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
// Copyright 2020 Masataka Kurihara. All rights reserved. MIT license.
22

3-
import { assert, assertEquals, assertThrows } from 'https://deno.land/std@0.74.0/testing/asserts.ts';
4-
import { ElementInfo, XMLParseContext, XMLParseEvent, XMLParseError } from "./context.ts";
5-
import { ParserBase, SAXParser, PullParser, PullResult } from './parser.ts';
3+
import {
4+
assert,
5+
assertEquals,
6+
assertThrows,
7+
} from 'https://deno.land/std@0.74.0/testing/asserts.ts';
8+
9+
import {
10+
ElementInfo,
11+
XMLParseContext,
12+
XMLParseEvent,
13+
XMLParseError,
14+
} from "./context.ts";
15+
16+
import {
17+
ParserBase,
18+
SAXParser,
19+
PullParser,
20+
PullResult,
21+
} from './parser.ts';
622

723
Deno.test('ParserBase chunk & hasNext & readNext & position', () => {
824
// protected -> public visiblity

0 commit comments

Comments
 (0)
Please sign in to comment.