Skip to content

Commit

Permalink
refactor(sax): wrap FSM handler results as arrays, update ELEM_SINGLE
Browse files Browse the repository at this point in the history
- ELEM_SINGLE handler now emits both ELEM_START & ELEM_END events
  • Loading branch information
postspectacular committed Jun 19, 2018
1 parent a9ca135 commit 343e07f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/sax/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const isTagChar = (x: string) => {

const error = (s: ParseState, body: string) => {
s.state = State.ERROR;
return { type: Type.ERROR, body };
return [{ type: Type.ERROR, body }];
};

const illegalEscape = (s: ParseState, ch: string) =>
Expand All @@ -128,7 +128,7 @@ const unexpected = (s: ParseState, x: string) =>

const replaceEntities = (x: string) => x.replace(ENTITY_RE, (y) => ENTITIES[y]);

const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent[]> = {

[State.ERROR]: () => { },

Expand Down Expand Up @@ -177,7 +177,7 @@ const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
state.state = State.ELEM_BODY;
state.scope.push({ tag: state.tag, attribs: state.attribs, children: [] });
state.body = "";
return { type: Type.ELEM_START, tag: state.tag, attribs: state.attribs };
return [{ type: Type.ELEM_START, tag: state.tag, attribs: state.attribs }];
} else if (ch === "/") {
state.state = State.ELEM_SINGLE;
} else {
Expand All @@ -198,7 +198,7 @@ const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
scope[n - 2].children.push(res);
}
state.state = State.WAIT;
return { type: Type.ELEM_END, ...res };
return [{ type: Type.ELEM_END, ...res }];
} else {
error(state, state.tag);
}
Expand All @@ -214,7 +214,10 @@ const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
if (n > 0) {
state.scope[n - 1].children.push(res);
}
return { type: Type.ELEM_END, ...res };
return [
{ type: Type.ELEM_START, ...res },
{ type: Type.ELEM_END, ...res }
];
} else {
return unexpected(state, ch);
}
Expand All @@ -230,7 +233,7 @@ const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
b = replaceEntities(b);
}
state.scope[state.scope.length - 1].body = b;
res = { type: Type.ELEM_BODY, tag: state.tag, body: b };
res = [{ type: Type.ELEM_BODY, tag: state.tag, body: b }];
}
state.state = State.MAYBE_ELEM;
state.tag = "";
Expand Down Expand Up @@ -267,7 +270,7 @@ const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
state.state = State.ELEM_BODY;
state.scope.push({ tag: state.tag, attribs: state.attribs, children: [] });
state.body = "";
return { type: Type.ELEM_START, tag: state.tag, attribs: state.attribs };
return [{ type: Type.ELEM_START, tag: state.tag, attribs: state.attribs }];
} else if (ch === "/") {
state.state = State.ELEM_SINGLE;
} else if (!isWS(ch)) {
Expand Down Expand Up @@ -352,7 +355,7 @@ const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
return unexpected(state, ch);
}
state.state = State.WAIT;
return { type: Type.COMMENT, body: state.body.substr(0, n - 2) };
return [{ type: Type.COMMENT, body: state.body.substr(0, n - 2) }];
} else {
state.body += ch;
}
Expand All @@ -368,7 +371,7 @@ const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
}
} else if (ch === ">") {
state.state = State.WAIT;
return { type: Type.DOCTYPE, body: state.body.trim() };
return [{ type: Type.DOCTYPE, body: state.body.trim() }];
} else {
state.body += ch;
}
Expand All @@ -392,7 +395,7 @@ const PARSER: fsm.FSMStateMap<ParseState, string, ParseEvent> = {
if (ch === ">") {
state.state = State.WAIT;
state.isProc = false;
return { type: Type.PROC, tag: state.tag, attribs: state.attribs };
return [{ type: Type.PROC, tag: state.tag, attribs: state.attribs }];
} else {
return unexpected(state, ch);
}
Expand Down

0 comments on commit 343e07f

Please sign in to comment.