Skip to content

Commit e72e5c7

Browse files
committed
context 添加steps
1 parent 9acc833 commit e72e5c7

File tree

5 files changed

+49
-34
lines changed

5 files changed

+49
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "forgiving-xml-parser",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "An forgiving XML/HTML parser and serializer for JavaScript.",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",

src/option.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const boundStepsToContext = (
3838
let eventNode: FxNode = context.currentNode;
3939
for (let index = 0, len = steps.length; index < len; index++) {
4040
const currentStepItem = steps[index] as FxNodeTryStep;
41+
context && context.steps.push(currentStepItem);
4142
currentStepItem.target = eventNode;
4243
const { step, cursor, data } = currentStepItem;
4344
setContextMaxCursor(context, cursor);

src/parse.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
findNodeParser,
1616
isFunc,
1717
filterOptions,
18+
stepToJSON,
1819
} from "./util";
1920
import { TextParser } from "./node/text";
2021
import { DEFAULT_PARSE_OPTIONS } from "./var";
@@ -30,6 +31,7 @@ export const parse = (xml: string, options?: FxParseOptions): FxParseResult => {
3031
lineNumber: 1,
3132
column: 1,
3233
nodes: [],
34+
steps: [],
3335
options,
3436
};
3537
try {
@@ -54,6 +56,7 @@ export const parse = (xml: string, options?: FxParseOptions): FxParseResult => {
5456
maxLine: context.maxLineNumber,
5557
maxCol: context.maxColumn,
5658
nodes: context.nodes,
59+
steps: context.steps,
5760
} as FxParseResult;
5861
} catch (error) {
5962
return {
@@ -80,6 +83,11 @@ export const parseResultToJSON = (
8083
pick("maxLine", res, parseResult, options);
8184
pick("maxCol", res, parseResult, options);
8285
pick("xml", res, parseResult, options);
86+
if (parseResult.steps && options.steps) {
87+
res.steps = parseResult.steps.map((step) => {
88+
return stepToJSON(step, options);
89+
});
90+
}
8391
if (!parseResult.error) {
8492
res.nodes = parseResult.nodes.map((node) => {
8593
if (hasFilter) {

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export interface FxParseContext extends FxCursorPosition {
7676
xmlLength: number;
7777
xml: string;
7878
nodes: FxNode[];
79+
steps: FxTryStep[];
7980
maxLineNumber: number;
8081
maxColumn: number;
8182
currentNode?: FxNode;
@@ -270,13 +271,15 @@ export interface FxParseResultJSON {
270271
maxLine?: number;
271272
maxCol?: number;
272273
xml?: string;
274+
steps?: FxTryStep[];
273275
nodes?: FxNodeJSON[];
274276
error?: FxWrong;
275277
}
276278
export interface FxParseResult extends FxParseResultJSON {
277279
maxLine: number;
278280
maxCol: number;
279281
xml: string;
282+
steps?: FxNodeTryStep[];
280283
nodes?: FxNode[];
281284
}
282285
export interface FxBoundStepsLoopCallback {

src/util.ts

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
FxStartTagCompare,
2020
FxNodeLocationInfo,
2121
FxLocation,
22+
FxNodeTryStep,
2223
} from "./types";
2324
import { REX_SPACE } from "./var";
2425

@@ -110,6 +111,40 @@ export const pick = (
110111
res[prop] = parseResult[prop];
111112
}
112113
};
114+
export const stepToJSON = (step: FxNodeTryStep, options: FxToJSONOptions): FxTryStep => {
115+
const hasFilter = isFunc(options.dataFilter);
116+
const res = {
117+
...step,
118+
};
119+
delete res.target;
120+
if (Array.isArray(res.data)) {
121+
let [nodeType, closeType, customType] = res.data;
122+
if (typeof nodeType === "object") {
123+
const np = nodeType as FxNodeAdapter;
124+
customType = customType || np.nodeCustomType;
125+
nodeType = np.nodeType;
126+
}
127+
const data = [nodeType, closeType, customType];
128+
if (!data[2]) {
129+
data.splice(2, 1);
130+
}
131+
res.data = data as FxTryStepData;
132+
} else if (typeof res.data === "object") {
133+
const np = res.data as FxNodeAdapter;
134+
res.data = [
135+
np.nodeType,
136+
step.step === FxEventType.nodeEnd ? FxNodeCloseType.fullClosed : undefined,
137+
np.nodeCustomType,
138+
];
139+
if (!res.data[2]) {
140+
res.data.pop();
141+
}
142+
if (!res.data[1]) {
143+
res.data.splice(1, 1);
144+
}
145+
}
146+
return hasFilter ? (options.dataFilter(step, res) as FxTryStep) : res;
147+
};
113148
export const nodeToJSON = (node: FxNode, options: FxToJSONOptions): FxNodeJSON => {
114149
const res = {} as FxNodeJSON;
115150
const hasFilter = isFunc(options.dataFilter);
@@ -118,39 +153,7 @@ export const nodeToJSON = (node: FxNode, options: FxToJSONOptions): FxNodeJSON =
118153
if (prop === "steps") {
119154
if (options.steps) {
120155
res[prop] = node[prop].map((step) => {
121-
const res = {
122-
...step,
123-
};
124-
delete res.target;
125-
if (Array.isArray(res.data)) {
126-
let [nodeType, closeType, customType] = res.data;
127-
if (typeof nodeType === "object") {
128-
const np = nodeType as FxNodeAdapter;
129-
customType = customType || np.nodeCustomType;
130-
nodeType = np.nodeType;
131-
}
132-
const data = [nodeType, closeType, customType];
133-
if (!data[2]) {
134-
data.splice(2, 1);
135-
}
136-
res.data = data as FxTryStepData;
137-
} else if (typeof res.data === "object") {
138-
const np = res.data as FxNodeAdapter;
139-
res.data = [
140-
np.nodeType,
141-
step.step === FxEventType.nodeEnd
142-
? FxNodeCloseType.fullClosed
143-
: undefined,
144-
np.nodeCustomType,
145-
];
146-
if (!res.data[2]) {
147-
res.data.pop();
148-
}
149-
if (!res.data[1]) {
150-
res.data.splice(1, 1);
151-
}
152-
}
153-
return hasFilter ? (options.dataFilter(step, res) as FxTryStep) : res;
156+
return stepToJSON(step, options);
154157
});
155158
}
156159
} else if (prop === "closeType") {

0 commit comments

Comments
 (0)