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

Skip to content

Commit abf9456

Browse files
committed
Some more tests
1 parent 2751fdc commit abf9456

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
3434
super();
3535
this.name = params.name;
3636
this.attributes = params.attributes || {};
37+
this.value = params.value;
3738
}
3839

3940
clone(): Node {
@@ -69,7 +70,7 @@ export class Node extends BaseNode implements ChildrenHolder, AttributeHolder {
6970
nodeString += `=${this.value}`;
7071
}
7172
Object.entries(this.attributes).forEach(([key, value]) => {
72-
nodeString += ` ${key}="${value}"`;
73+
nodeString += ` ${key}=${value}`;
7374
});
7475
nodeString += ']';
7576
this.children.forEach(child => {

src/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export default class Parser {
234234
}
235235
if (Object.keys(currentTagAttributes).length !== 0) {
236236
Object.entries(currentTagAttributes).forEach(([key, value]) => {
237-
tagText += ` ${key}="${value}"`;
237+
tagText += ` ${key}=${value}`;
238238
});
239239
}
240240
if (buildingAttributeName){

tests/index.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,50 @@ describe("bbcode-ast tests", () => {
7070
expect((((parsed.children[0] as Node).children[1] as ListItemNode).children[0] as TextNode).text).to.equal("Hello world!");
7171
expect(parsed.toString()).to.equal("[list][*]Hello world![*]Hello world![/list]");
7272
})
73+
74+
it("Should parse simple attributes", () => {
75+
const parsed = defaultParser.parse("[color=red]Hello world![/color]")
76+
expect(parsed.children.length).to.equal(1);
77+
expect(parsed.children[0].name).to.equal("color");
78+
expect((parsed.children[0] as Node).value).to.equal("red");
79+
expect((parsed.children[0] as Node).children.length).to.equal(1);
80+
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
81+
expect(((parsed.children[0] as Node).children[0] as TextNode).text).to.equal("Hello world!");
82+
expect(parsed.toString()).to.equal("[color=red]Hello world![/color]");
83+
})
84+
85+
it("Should parse simple attributes with space and quotes", () => {
86+
const parsed = defaultParser.parse('[spoiler="a spoiler"]Hello world![/spoiler]')
87+
expect(parsed.children.length).to.equal(1);
88+
expect(parsed.children[0].name).to.equal("spoiler");
89+
expect((parsed.children[0] as Node).value).to.equal('"a spoiler"');
90+
expect((parsed.children[0] as Node).children.length).to.equal(1);
91+
expect((parsed.children[0] as Node).children[0].name).to.equal("TextNode");
92+
expect(((parsed.children[0] as Node).children[0] as TextNode).text).to.equal("Hello world!");
93+
expect(parsed.toString()).to.equal('[spoiler="a spoiler"]Hello world![/spoiler]');
94+
})
95+
96+
it("Should parse complex attributes", () => {
97+
const parsed = defaultParser.parse("[url=https://www.google.com][img align=right]https://i.imgur.com/oz0a7.jpg[/img][/url]")
98+
expect(parsed.children.length).to.equal(1);
99+
expect(parsed.children[0].name).to.equal("url");
100+
expect((parsed.children[0] as Node).value).to.equal("https://www.google.com");
101+
expect((parsed.children[0] as Node).children.length).to.equal(1);
102+
expect((parsed.children[0] as Node).children[0].name).to.equal("img");
103+
expect(((parsed.children[0] as Node).children[0] as Node).attributes).to.deep.equal({align: "right"});
104+
expect((((parsed.children[0] as Node).children[0] as Node).children[0] as TextNode).text).to.equal("https://i.imgur.com/oz0a7.jpg");
105+
expect(parsed.toString()).to.equal("[url=https://www.google.com][img align=right]https://i.imgur.com/oz0a7.jpg[/img][/url]");
106+
})
107+
108+
it("Should parse complex attributes and quotes", () => {
109+
const parsed = defaultParser.parse("[url=https://www.google.com][img align='right']https://i.imgur.com/oz0a7.jpg[/img][/url]")
110+
expect(parsed.children.length).to.equal(1);
111+
expect(parsed.children[0].name).to.equal("url");
112+
expect((parsed.children[0] as Node).value).to.equal("https://www.google.com");
113+
expect((parsed.children[0] as Node).children.length).to.equal(1);
114+
expect((parsed.children[0] as Node).children[0].name).to.equal("img");
115+
expect(((parsed.children[0] as Node).children[0] as Node).attributes).to.deep.equal({align: "'right'"});
116+
expect((((parsed.children[0] as Node).children[0] as Node).children[0] as TextNode).text).to.equal("https://i.imgur.com/oz0a7.jpg");
117+
expect(parsed.toString()).to.equal("[url=https://www.google.com][img align='right']https://i.imgur.com/oz0a7.jpg[/img][/url]");
118+
})
73119
})

0 commit comments

Comments
 (0)