@thi.ng/sax
Advanced tools
Comparing version 0.3.21 to 0.4.0
@@ -6,2 +6,14 @@ # Change Log | ||
<a name="0.4.0"></a> | ||
# [0.4.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/sax@0.3.21...@thi.ng/sax@0.4.0) (2018-09-24) | ||
### Features | ||
* **sax:** update parse() to return iterator if input given (optional) ([665564c](https://github.com/thi-ng/umbrella/commit/665564c)) | ||
<a name="0.3.21"></a> | ||
@@ -8,0 +20,0 @@ ## [0.3.21](https://github.com/thi-ng/umbrella/compare/@thi.ng/sax@0.3.21-alpha.1...@thi.ng/sax@0.3.21) (2018-09-22) |
@@ -73,6 +73,8 @@ import { IObjectOf } from "@thi.ng/api"; | ||
* Returns XML parser transducer, optionally configured with given | ||
* options. | ||
* options. If `src` is also given, returns an iterator instead. | ||
* | ||
* @param opts | ||
*/ | ||
export declare const parse: (opts?: Partial<ParseOpts>) => Transducer<string, ParseEvent>; | ||
export declare function parse(opts?: Partial<ParseOpts>): Transducer<string, ParseEvent>; | ||
export declare function parse(src: string): IterableIterator<ParseEvent>; | ||
export declare function parse(opts: Partial<ParseOpts>, src: string): IterableIterator<ParseEvent>; |
126
index.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const transducers_fsm_1 = require("@thi.ng/transducers-fsm"); | ||
const iterator_1 = require("@thi.ng/transducers/iterator"); | ||
var Type; | ||
@@ -81,18 +82,19 @@ (function (Type) { | ||
}; | ||
/** | ||
* Returns XML parser transducer, optionally configured with given | ||
* options. | ||
* | ||
* @param opts | ||
*/ | ||
exports.parse = (opts) => transducers_fsm_1.fsm({ | ||
states: PARSER, | ||
init: () => ({ | ||
state: State.WAIT, | ||
scope: [], | ||
pos: 0, | ||
opts: Object.assign({ children: true, entities: false, trim: false }, opts), | ||
}), | ||
terminate: State.ERROR | ||
}); | ||
function parse(...args) { | ||
const iter = iterator_1.$iter(parse, args, iterator_1.iterator); | ||
if (iter) { | ||
return iter; | ||
} | ||
return transducers_fsm_1.fsm({ | ||
states: PARSER, | ||
init: () => ({ | ||
state: 0 /* WAIT */, | ||
scope: [], | ||
pos: 0, | ||
opts: Object.assign({ children: true, entities: false, trim: false }, args[0]), | ||
}), | ||
terminate: 1 /* ERROR */ | ||
}); | ||
} | ||
exports.parse = parse; | ||
const isWS = (x) => { | ||
@@ -115,3 +117,3 @@ const c = x.charCodeAt(0); | ||
const error = (s, body) => { | ||
s.state = State.ERROR; | ||
s.state = 1 /* ERROR */; | ||
return [{ type: Type.ERROR, body }]; | ||
@@ -123,8 +125,8 @@ }; | ||
const PARSER = { | ||
[State.ERROR]: () => { }, | ||
[State.WAIT]: (state, ch) => { | ||
[1 /* ERROR */]: () => { }, | ||
[0 /* WAIT */]: (state, ch) => { | ||
state.pos++; | ||
if (!isWS(ch)) { | ||
if (ch === "<") { | ||
state.state = State.MAYBE_ELEM; | ||
state.state = 2 /* MAYBE_ELEM */; | ||
} | ||
@@ -136,3 +138,3 @@ else { | ||
}, | ||
[State.MAYBE_ELEM]: (state, ch) => { | ||
[2 /* MAYBE_ELEM */]: (state, ch) => { | ||
state.pos++; | ||
@@ -143,7 +145,7 @@ if (ch === "/") { | ||
} | ||
state.state = State.ELEM_END; | ||
state.state = 4 /* ELEM_END */; | ||
state.tag = ""; | ||
} | ||
else if (isTagChar(ch)) { | ||
state.state = State.ELEM_START; | ||
state.state = 3 /* ELEM_START */; | ||
state.tag = ch; | ||
@@ -153,6 +155,6 @@ state.attribs = {}; | ||
else if (ch === "!") { | ||
state.state = State.MAYBE_INSTRUCTION; | ||
state.state = 11 /* MAYBE_INSTRUCTION */; | ||
} | ||
else if (ch === "?") { | ||
state.state = State.PROC_DECL; | ||
state.state = 15 /* PROC_DECL */; | ||
state.phase = 0; | ||
@@ -166,3 +168,3 @@ state.tag = ""; | ||
}, | ||
[State.ELEM_START]: (state, ch) => { | ||
[3 /* ELEM_START */]: (state, ch) => { | ||
state.pos++; | ||
@@ -173,6 +175,6 @@ if (isTagChar(ch)) { | ||
else if (isWS(ch)) { | ||
state.state = State.MAYBE_ATTRIB; | ||
state.state = 7 /* MAYBE_ATTRIB */; | ||
} | ||
else if (ch === ">") { | ||
state.state = State.ELEM_BODY; | ||
state.state = 6 /* ELEM_BODY */; | ||
state.scope.push({ tag: state.tag, attribs: state.attribs, children: [] }); | ||
@@ -183,3 +185,3 @@ state.body = ""; | ||
else if (ch === "/") { | ||
state.state = State.ELEM_SINGLE; | ||
state.state = 5 /* ELEM_SINGLE */; | ||
} | ||
@@ -190,3 +192,3 @@ else { | ||
}, | ||
[State.ELEM_END]: (state, ch) => { | ||
[4 /* ELEM_END */]: (state, ch) => { | ||
state.pos++; | ||
@@ -204,3 +206,3 @@ if (isTagChar(ch)) { | ||
} | ||
state.state = State.WAIT; | ||
state.state = 0 /* WAIT */; | ||
return [Object.assign({ type: Type.ELEM_END }, res)]; | ||
@@ -213,6 +215,6 @@ } | ||
}, | ||
[State.ELEM_SINGLE]: (state, ch) => { | ||
[5 /* ELEM_SINGLE */]: (state, ch) => { | ||
state.pos++; | ||
if (ch === ">") { | ||
state.state = State.WAIT; | ||
state.state = 0 /* WAIT */; | ||
const n = state.scope.length; | ||
@@ -232,3 +234,3 @@ const res = { tag: state.tag, attribs: state.attribs }; | ||
}, | ||
[State.ELEM_BODY]: (state, ch) => { | ||
[6 /* ELEM_BODY */]: (state, ch) => { | ||
state.pos++; | ||
@@ -239,3 +241,3 @@ let b = state.body; | ||
const t = state.tag; | ||
state.state = State.MAYBE_ELEM; | ||
state.state = 2 /* MAYBE_ELEM */; | ||
state.tag = ""; | ||
@@ -271,6 +273,6 @@ if (b.length > 0) { | ||
}, | ||
[State.MAYBE_ATTRIB]: (state, ch) => { | ||
[7 /* MAYBE_ATTRIB */]: (state, ch) => { | ||
state.pos++; | ||
if (isTagChar(ch)) { | ||
state.state = State.ATTRIB_NAME; | ||
state.state = 8 /* ATTRIB_NAME */; | ||
state.name = ch; | ||
@@ -282,3 +284,3 @@ state.val = ""; | ||
if (ch === "?") { | ||
state.state = State.PROC_END; | ||
state.state = 16 /* PROC_END */; | ||
} | ||
@@ -291,3 +293,3 @@ else if (!isWS(ch)) { | ||
if (ch === ">") { | ||
state.state = State.ELEM_BODY; | ||
state.state = 6 /* ELEM_BODY */; | ||
state.scope.push({ tag: state.tag, attribs: state.attribs, children: [] }); | ||
@@ -298,3 +300,3 @@ state.body = ""; | ||
else if (ch === "/") { | ||
state.state = State.ELEM_SINGLE; | ||
state.state = 5 /* ELEM_SINGLE */; | ||
} | ||
@@ -307,3 +309,3 @@ else if (!isWS(ch)) { | ||
}, | ||
[State.ATTRIB_NAME]: (state, ch) => { | ||
[8 /* ATTRIB_NAME */]: (state, ch) => { | ||
state.pos++; | ||
@@ -314,3 +316,3 @@ if (isTagChar(ch)) { | ||
else if (ch === "=") { | ||
state.state = State.ATTRIB_VAL_START; | ||
state.state = 9 /* ATTRIB_VAL_START */; | ||
} | ||
@@ -321,6 +323,6 @@ else { | ||
}, | ||
[State.ATTRIB_VAL_START]: (state, ch) => { | ||
[9 /* ATTRIB_VAL_START */]: (state, ch) => { | ||
state.pos++; | ||
if (ch === "\"" || ch === "'") { | ||
state.state = State.ATTRIB_VALUE; | ||
state.state = 10 /* ATTRIB_VALUE */; | ||
state.quote = ch; | ||
@@ -332,3 +334,3 @@ } | ||
}, | ||
[State.ATTRIB_VALUE]: (state, ch) => { | ||
[10 /* ATTRIB_VALUE */]: (state, ch) => { | ||
state.pos++; | ||
@@ -354,11 +356,11 @@ let v = state.val; | ||
state.attribs[state.name] = v; | ||
state.state = State.MAYBE_ATTRIB; | ||
state.state = 7 /* MAYBE_ATTRIB */; | ||
}, | ||
[State.MAYBE_INSTRUCTION]: (state, ch) => { | ||
[11 /* MAYBE_INSTRUCTION */]: (state, ch) => { | ||
state.pos++; | ||
if (ch === "-") { | ||
state.state = State.COMMENT; | ||
state.state = 12 /* COMMENT */; | ||
} | ||
else if (ch === "D") { | ||
state.state = State.DOCTYPE; | ||
state.state = 14 /* DOCTYPE */; | ||
state.phase = 1; | ||
@@ -368,3 +370,3 @@ state.body = ""; | ||
else if (ch === "[") { | ||
state.state = State.CDATA; | ||
state.state = 17 /* CDATA */; | ||
state.phase = 1; | ||
@@ -377,6 +379,6 @@ state.body = ""; | ||
}, | ||
[State.COMMENT]: (state, ch) => { | ||
[12 /* COMMENT */]: (state, ch) => { | ||
state.pos++; | ||
if (ch === "-") { | ||
state.state = State.COMMENT_BODY; | ||
state.state = 13 /* COMMENT_BODY */; | ||
state.body = ""; | ||
@@ -388,3 +390,3 @@ } | ||
}, | ||
[State.COMMENT_BODY]: (state, ch) => { | ||
[13 /* COMMENT_BODY */]: (state, ch) => { | ||
state.pos++; | ||
@@ -396,3 +398,3 @@ if (ch === ">") { | ||
} | ||
state.state = State.WAIT; | ||
state.state = 0 /* WAIT */; | ||
let b = state.body.substr(0, n - 2); | ||
@@ -411,3 +413,3 @@ if (state.opts.trim) { | ||
}, | ||
[State.DOCTYPE]: (state, ch) => { | ||
[14 /* DOCTYPE */]: (state, ch) => { | ||
state.pos++; | ||
@@ -423,3 +425,3 @@ if (state.phase < 8) { | ||
else if (ch === ">") { | ||
state.state = State.WAIT; | ||
state.state = 0 /* WAIT */; | ||
return [{ type: Type.DOCTYPE, body: state.body.trim() }]; | ||
@@ -431,3 +433,3 @@ } | ||
}, | ||
[State.CDATA]: (state, ch) => { | ||
[17 /* CDATA */]: (state, ch) => { | ||
state.pos++; | ||
@@ -448,3 +450,3 @@ if (state.phase < 7) { | ||
} | ||
state.state = State.WAIT; | ||
state.state = 0 /* WAIT */; | ||
let b = state.body.substr(0, n - 2); | ||
@@ -463,3 +465,3 @@ if (state.opts.trim) { | ||
}, | ||
[State.PROC_DECL]: (state, ch) => { | ||
[15 /* PROC_DECL */]: (state, ch) => { | ||
state.pos++; | ||
@@ -470,3 +472,3 @@ if (isTagChar(ch)) { | ||
else if (isWS(ch)) { | ||
state.state = State.MAYBE_ATTRIB; | ||
state.state = 7 /* MAYBE_ATTRIB */; | ||
state.isProc = true; | ||
@@ -479,6 +481,6 @@ state.attribs = {}; | ||
}, | ||
[State.PROC_END]: (state, ch) => { | ||
[16 /* PROC_END */]: (state, ch) => { | ||
state.pos++; | ||
if (ch === ">") { | ||
state.state = State.WAIT; | ||
state.state = 0 /* WAIT */; | ||
state.isProc = false; | ||
@@ -485,0 +487,0 @@ return [{ type: Type.PROC, tag: state.tag, attribs: state.attribs }]; |
{ | ||
"name": "@thi.ng/sax", | ||
"version": "0.3.21", | ||
"version": "0.4.0", | ||
"description": "Transducer-based, SAX-like, non-validating, speedy & tiny XML parser", | ||
@@ -31,5 +31,5 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@thi.ng/api": "^4.2.0", | ||
"@thi.ng/transducers": "^2.1.2", | ||
"@thi.ng/transducers-fsm": "^0.2.20" | ||
"@thi.ng/api": "^4.2.1", | ||
"@thi.ng/transducers": "^2.1.3", | ||
"@thi.ng/transducers-fsm": "^0.2.21" | ||
}, | ||
@@ -48,3 +48,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "02548fd2518eb97eb11a9834c4e0a8e6ab5499a7" | ||
"gitHead": "cf05efbf251312aff6a7b96861c79d245f4418cd" | ||
} |
@@ -5,2 +5,3 @@ # @thi.ng/sax | ||
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/sax.svg) | ||
[![Twitter Follow](https://img.shields.io/twitter/follow/thing_umbrella.svg?style=flat-square&label=twitter)](https://twitter.com/thing_umbrella) | ||
@@ -59,6 +60,10 @@ This project is part of the | ||
// sax.parse() returns a transducer | ||
doc = [...tx.iterator(sax.parse(), src)] | ||
// (see description of `type` values further below) | ||
// ...or returns iterator if input is given | ||
doc = [...sax.parse(src)] | ||
// (see description of `type` values and parse options further below) | ||
// [ { type: 0, | ||
@@ -155,2 +160,6 @@ // tag: 'xml', | ||
This example shows how SVG can be parsed into | ||
[@thi.ng/hiccup](https://github.com/thi-ng/umbrella/tree/master/packages/hiccup) | ||
format. | ||
```ts | ||
@@ -157,0 +166,0 @@ import { defmulti, DEFAULT } from "@thi.ng/defmulti"; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45277
538
289
Updated@thi.ng/api@^4.2.1
Updated@thi.ng/transducers@^2.1.3