Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b8935e2

Browse files
committed
Add tests and fix bugs
1 parent 0ca6307 commit b8935e2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/parser.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default class Parser {
6666
// We've already built some text. Add it as a text node.
6767
const textNode = new TextNode(currentText);
6868
currentStack[currentStack.length - 1].addChild(textNode);
69+
currentText = "";
6970
}
7071
} else {
7172
// We're building text, and we found a character other than "[".
@@ -85,6 +86,8 @@ export default class Parser {
8586
const previousStackElement = currentStack[currentStack.length - 1];
8687
previousStackElement.addChild(lastStackElement);
8788
}
89+
} else if (nextCharacter === "/" && !currentTagName) {
90+
buildingClosingTag = true;
8891
} else if ((["]", " ", "="].includes(nextCharacter))){
8992
// We found a character that signifies the end of the tag name.
9093
// First, we determine if it is a valid tag name.
@@ -113,6 +116,10 @@ export default class Parser {
113116
throw new Error(`Expected closing tag for '${currentTagName}', found '${lastElement.name}'.`);
114117
} else {
115118
currentStack[currentStack.length - 1].addChild(lastElement);
119+
buildingText = true;
120+
buildingClosingTag = false;
121+
buildingTagName = false;
122+
currentTagName = "";
116123
}
117124
} else {
118125
// Simple tag, there are no attributes or values. We push a tag to the stack and continue.

tests/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,18 @@ describe("bbcode-ast tests", () => {
1919
expect(((parsed.children[0] as Node).children[0] as TextNode).text).to.equal("Hello world!");
2020
expect(parsed.toString()).to.equal("[b]Hello world![/b]");
2121
})
22+
23+
it("Should parse multiple simple tags", () => {
24+
const parsed = defaultParser.parse("[b]Hello world![/b][i]Hello world![/i]")
25+
expect(parsed.children.length).to.equal(2);
26+
expect(parsed.children[0].name).to.equal("b");
27+
expect((parsed.children[0] as Node).children.length).to.equal(1);
28+
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
29+
expect(((parsed.children[0] as Node).children[0] as TextNode).text).to.equal("Hello world!");
30+
expect(parsed.children[1].name).to.equal("i");
31+
expect((parsed.children[1] as Node).children.length).to.equal(1);
32+
expect((parsed.children[1] as Node).children[0].name).to.equal("TextNode");
33+
expect(((parsed.children[1] as Node).children[0] as TextNode).text).to.equal("Hello world!");
34+
expect(parsed.toString()).to.equal("[b]Hello world![/b][i]Hello world![/i]");
35+
})
2236
})

0 commit comments

Comments
 (0)