Skip to content

Commit

Permalink
context 添加steps
Browse files Browse the repository at this point in the history
  • Loading branch information
imingyu committed Feb 19, 2021
1 parent 9acc833 commit e72e5c7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "forgiving-xml-parser",
"version": "1.2.0",
"version": "1.2.1",
"description": "An forgiving XML/HTML parser and serializer for JavaScript.",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
1 change: 1 addition & 0 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const boundStepsToContext = (
let eventNode: FxNode = context.currentNode;
for (let index = 0, len = steps.length; index < len; index++) {
const currentStepItem = steps[index] as FxNodeTryStep;
context && context.steps.push(currentStepItem);
currentStepItem.target = eventNode;
const { step, cursor, data } = currentStepItem;
setContextMaxCursor(context, cursor);
Expand Down
8 changes: 8 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
findNodeParser,
isFunc,
filterOptions,
stepToJSON,
} from "./util";
import { TextParser } from "./node/text";
import { DEFAULT_PARSE_OPTIONS } from "./var";
Expand All @@ -30,6 +31,7 @@ export const parse = (xml: string, options?: FxParseOptions): FxParseResult => {
lineNumber: 1,
column: 1,
nodes: [],
steps: [],
options,
};
try {
Expand All @@ -54,6 +56,7 @@ export const parse = (xml: string, options?: FxParseOptions): FxParseResult => {
maxLine: context.maxLineNumber,
maxCol: context.maxColumn,
nodes: context.nodes,
steps: context.steps,
} as FxParseResult;
} catch (error) {
return {
Expand All @@ -80,6 +83,11 @@ export const parseResultToJSON = (
pick("maxLine", res, parseResult, options);
pick("maxCol", res, parseResult, options);
pick("xml", res, parseResult, options);
if (parseResult.steps && options.steps) {
res.steps = parseResult.steps.map((step) => {
return stepToJSON(step, options);
});
}
if (!parseResult.error) {
res.nodes = parseResult.nodes.map((node) => {
if (hasFilter) {
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface FxParseContext extends FxCursorPosition {
xmlLength: number;
xml: string;
nodes: FxNode[];
steps: FxTryStep[];
maxLineNumber: number;
maxColumn: number;
currentNode?: FxNode;
Expand Down Expand Up @@ -270,13 +271,15 @@ export interface FxParseResultJSON {
maxLine?: number;
maxCol?: number;
xml?: string;
steps?: FxTryStep[];
nodes?: FxNodeJSON[];
error?: FxWrong;
}
export interface FxParseResult extends FxParseResultJSON {
maxLine: number;
maxCol: number;
xml: string;
steps?: FxNodeTryStep[];
nodes?: FxNode[];
}
export interface FxBoundStepsLoopCallback {
Expand Down
69 changes: 36 additions & 33 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
FxStartTagCompare,
FxNodeLocationInfo,
FxLocation,
FxNodeTryStep,
} from "./types";
import { REX_SPACE } from "./var";

Expand Down Expand Up @@ -110,6 +111,40 @@ export const pick = (
res[prop] = parseResult[prop];
}
};
export const stepToJSON = (step: FxNodeTryStep, options: FxToJSONOptions): FxTryStep => {
const hasFilter = isFunc(options.dataFilter);
const res = {
...step,
};
delete res.target;
if (Array.isArray(res.data)) {
let [nodeType, closeType, customType] = res.data;
if (typeof nodeType === "object") {
const np = nodeType as FxNodeAdapter;
customType = customType || np.nodeCustomType;
nodeType = np.nodeType;
}
const data = [nodeType, closeType, customType];
if (!data[2]) {
data.splice(2, 1);
}
res.data = data as FxTryStepData;
} else if (typeof res.data === "object") {
const np = res.data as FxNodeAdapter;
res.data = [
np.nodeType,
step.step === FxEventType.nodeEnd ? FxNodeCloseType.fullClosed : undefined,
np.nodeCustomType,
];
if (!res.data[2]) {
res.data.pop();
}
if (!res.data[1]) {
res.data.splice(1, 1);
}
}
return hasFilter ? (options.dataFilter(step, res) as FxTryStep) : res;
};
export const nodeToJSON = (node: FxNode, options: FxToJSONOptions): FxNodeJSON => {
const res = {} as FxNodeJSON;
const hasFilter = isFunc(options.dataFilter);
Expand All @@ -118,39 +153,7 @@ export const nodeToJSON = (node: FxNode, options: FxToJSONOptions): FxNodeJSON =
if (prop === "steps") {
if (options.steps) {
res[prop] = node[prop].map((step) => {
const res = {
...step,
};
delete res.target;
if (Array.isArray(res.data)) {
let [nodeType, closeType, customType] = res.data;
if (typeof nodeType === "object") {
const np = nodeType as FxNodeAdapter;
customType = customType || np.nodeCustomType;
nodeType = np.nodeType;
}
const data = [nodeType, closeType, customType];
if (!data[2]) {
data.splice(2, 1);
}
res.data = data as FxTryStepData;
} else if (typeof res.data === "object") {
const np = res.data as FxNodeAdapter;
res.data = [
np.nodeType,
step.step === FxEventType.nodeEnd
? FxNodeCloseType.fullClosed
: undefined,
np.nodeCustomType,
];
if (!res.data[2]) {
res.data.pop();
}
if (!res.data[1]) {
res.data.splice(1, 1);
}
}
return hasFilter ? (options.dataFilter(step, res) as FxTryStep) : res;
return stepToJSON(step, options);
});
}
} else if (prop === "closeType") {
Expand Down

0 comments on commit e72e5c7

Please sign in to comment.