Skip to content

Commit 2751fdc

Browse files
committed
Finally fix test cases
1 parent 00a9360 commit 2751fdc

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import Parser from "./parser";
33
export * from "./node";
44
export { default as Parser } from "./parser";
55

6-
const defaultTags = ["b", "u", "i", "s", "center", "right", "color", "size", "yt", "list", "url", "img", "spoiler", "code"]
6+
const defaultTags = ["b", "u", "i", "s", "center", "right", "color", "size", "yt", "list", "*", "url", "img", "spoiler", "code"]
77
const defaultParser = new Parser(defaultTags, false);
88
export default defaultParser;

src/node.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,13 @@ export class RootNode extends BaseNode implements ChildrenHolder {
129129
}
130130

131131
export class ListItemNode extends RootNode {
132-
name = "ListItemNode";
132+
name = "*";
133133

134134
toString(): string {
135135
return "[*]" + super.toString();
136136
}
137+
138+
clone(): ListItemNode {
139+
return new ListItemNode(this.children.map(child => child.clone()));
140+
}
137141
}

src/parser.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,16 @@ export default class Parser {
7979
// We have to be building a tag or tag data.
8080
if (buildingTagName) {
8181
// We're building the tag's name.
82-
if (nextCharacter === "*" && !currentTagName) {
82+
if (nextCharacter === "*" && !currentTagName && !buildingClosingTag) {
8383
// This is a list node closing tag.
8484
const lastStackElement = currentStack[currentStack.length - 1];
85-
if (lastStackElement instanceof ListItemNode) {
85+
if (lastStackElement.name === "*") {
8686
// We finished the last list item.
8787
currentStack.pop();
8888
const previousStackElement = currentStack[currentStack.length - 1];
8989
previousStackElement.addChild(lastStackElement);
9090
}
91+
currentTagName += nextCharacter;
9192
} else if (nextCharacter === "/" && !currentTagName) {
9293
buildingClosingTag = true;
9394
} else if ((["]", " ", "="].includes(nextCharacter))){
@@ -96,7 +97,7 @@ export default class Parser {
9697
if (this.supportedTagNames.includes(this.caseSensitive ? currentTagName : currentTagName.toLowerCase()) && (!buildingCode || currentTagName === "code")) {
9798
// The tag name is valid.
9899
if (nextCharacter === "]"){
99-
if (currentTagName === "*"){
100+
if (currentTagName === "*" && !buildingClosingTag) {
100101
// This is a list node opening tag.
101102
const listNode = new ListItemNode();
102103
currentStack.push(listNode);
@@ -108,7 +109,7 @@ export default class Parser {
108109
let lastElement = currentStack.pop()!;
109110
if (currentTagName === "list"){
110111
// List tag. If the last element is a list item, we need to add it to the previous element.
111-
if (lastElement instanceof ListItemNode){
112+
if (lastElement.name === "*"){
112113
const previousElement = currentStack.pop()!;
113114
previousElement.addChild(lastElement);
114115
lastElement = previousElement;

tests/index.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "chai";
2-
import defaultParser, {Node, TextNode} from "../src/index";
2+
import defaultParser, {ListItemNode, Node, TextNode} from "../src/index";
33

44
describe("bbcode-ast tests", () => {
55
it("Should parse tag-free text", () => {
@@ -54,4 +54,20 @@ describe("bbcode-ast tests", () => {
5454
expect(((parsed.children[0] as Node).children[0] as TextNode).text).to.equal("[b]Hello world![/b]");
5555
expect(parsed.toString()).to.equal("[code][b]Hello world![/b][/code]");
5656
})
57+
58+
it("Should parse list tags", () => {
59+
const parsed = defaultParser.parse("[list][*]Hello world![*]Hello world![/list]")
60+
expect(parsed.children.length).to.equal(1);
61+
expect(parsed.children[0].name).to.equal("list");
62+
expect((parsed.children[0] as Node).children.length).to.equal(2);
63+
expect((parsed.children[0] as Node).children[0].name).to.equal("*");
64+
expect(((parsed.children[0] as Node).children[0] as ListItemNode).children.length).to.equal(1);
65+
expect(((parsed.children[0] as Node).children[0] as ListItemNode).children[0].name).to.equal("TextNode");
66+
expect((((parsed.children[0] as Node).children[0] as ListItemNode).children[0] as TextNode).text).to.equal("Hello world!");
67+
expect((parsed.children[0] as Node).children[1].name).to.equal("*");
68+
expect(((parsed.children[0] as Node).children[1] as ListItemNode).children.length).to.equal(1);
69+
expect(((parsed.children[0] as Node).children[1] as ListItemNode).children[0].name).to.equal("TextNode");
70+
expect((((parsed.children[0] as Node).children[1] as ListItemNode).children[0] as TextNode).text).to.equal("Hello world!");
71+
expect(parsed.toString()).to.equal("[list][*]Hello world![*]Hello world![/list]");
72+
})
5773
})

0 commit comments

Comments
 (0)