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

Skip to content

Commit 00a9360

Browse files
committed
Add tests and fix bugs
1 parent b8935e2 commit 00a9360

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/node.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
4242
return node;
4343
}
4444

45-
addChild(child: Node): void {
45+
addChild(child: BaseNode): void {
46+
if (child instanceof TextNode){
47+
const previousChild = this.children[this.children.length - 1];
48+
if (previousChild instanceof TextNode){
49+
// We flatten the text nodes.
50+
previousChild.text += child.text;
51+
return;
52+
}
53+
}
4654
this.children.push(child.clone());
4755
}
4856

@@ -99,7 +107,15 @@ export class RootNode extends BaseNode implements ChildrenHolder {
99107
this.children = children;
100108
}
101109

102-
addChild(child: Node): void {
110+
addChild(child: BaseNode): void {
111+
if (child instanceof TextNode){
112+
const previousChild = this.children[this.children.length - 1];
113+
if (previousChild instanceof TextNode){
114+
// We flatten the text nodes.
115+
previousChild.text += child.text;
116+
return;
117+
}
118+
}
103119
this.children.push(child.clone());
104120
}
105121

src/parser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export default class Parser {
3737
let buildingAttributeName = false;
3838
// Represents whether we're building the opening or closing tag.
3939
let buildingClosingTag = false;
40+
// Represents whether we're building a code tag. Ignore bbcode inside the code tag.
41+
let buildingCode = false;
4042
// Represents whether we're in a quote. In that case, ignore all spaces.
4143
let quoted = false;
4244
// Represents the name of the current tag we're building.
@@ -91,7 +93,7 @@ export default class Parser {
9193
} else if ((["]", " ", "="].includes(nextCharacter))){
9294
// We found a character that signifies the end of the tag name.
9395
// First, we determine if it is a valid tag name.
94-
if (this.supportedTagNames.includes(this.caseSensitive ? currentTagName : currentTagName.toLowerCase())) {
96+
if (this.supportedTagNames.includes(this.caseSensitive ? currentTagName : currentTagName.toLowerCase()) && (!buildingCode || currentTagName === "code")) {
9597
// The tag name is valid.
9698
if (nextCharacter === "]"){
9799
if (currentTagName === "*"){
@@ -127,6 +129,9 @@ export default class Parser {
127129
currentStack.push(currentTag);
128130
buildingTagName = false;
129131
buildingText = true;
132+
if (currentTagName.toLowerCase() === "code"){
133+
buildingCode = true;
134+
}
130135
currentTagName = "";
131136
}
132137
} else if (nextCharacter === "="){
@@ -143,7 +148,7 @@ export default class Parser {
143148
// We treat it as text.
144149
buildingText = true;
145150
buildingTagName = false;
146-
currentText += "[" + currentTagName;
151+
currentText += (buildingClosingTag ? "[/" : "[") + currentTagName + nextCharacter;
147152
}
148153
} else {
149154
// We're still building the tag's name.

tests/index.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,25 @@ describe("bbcode-ast tests", () => {
3333
expect(((parsed.children[1] as Node).children[0] as TextNode).text).to.equal("Hello world!");
3434
expect(parsed.toString()).to.equal("[b]Hello world![/b][i]Hello world![/i]");
3535
})
36+
37+
it("Should parse nested tags", () => {
38+
const parsed = defaultParser.parse("[b][i]Hello world![/i][/b]")
39+
expect(parsed.children.length).to.equal(1);
40+
expect(parsed.children[0].name).to.equal("b");
41+
expect((parsed.children[0] as Node).children.length).to.equal(1);
42+
expect((parsed.children[0] as Node).children[0].name).to.equal("i");
43+
expect(((parsed.children[0] as Node).children[0] as Node).children.length).to.equal(1);
44+
expect((((parsed.children[0] as Node).children[0] as Node).children[0] as TextNode).text).to.equal("Hello world!");
45+
expect(parsed.toString()).to.equal("[b][i]Hello world![/i][/b]");
46+
})
47+
48+
it("Should parse code tags and ignore content", () => {
49+
const parsed = defaultParser.parse("[code][b]Hello world![/b][/code]")
50+
expect(parsed.children.length).to.equal(1);
51+
expect(parsed.children[0].name).to.equal("code");
52+
expect((parsed.children[0] as Node).children.length).to.equal(1);
53+
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
54+
expect(((parsed.children[0] as Node).children[0] as TextNode).text).to.equal("[b]Hello world![/b]");
55+
expect(parsed.toString()).to.equal("[code][b]Hello world![/b][/code]");
56+
})
3657
})

0 commit comments

Comments
 (0)