Skip to content

Commit 17a7b3d

Browse files
committed
Added some more tests to cover new comment loading system
1 parent 44e6ece commit 17a7b3d

10 files changed

Lines changed: 231 additions & 60 deletions

build/CommentCoreLibrary.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,11 @@ var BilibiliFormat = (function () {
14301430
}
14311431

14321432
BilibiliFormat.XMLParser.prototype.parseOne = function (elem) {
1433-
var params = elem.getAttribute('p').split(',');
1433+
try {
1434+
var params = elem.getAttribute('p').split(',');
1435+
} catch (e) {
1436+
throw new Error("Unsupported object type or could not decompose.");
1437+
}
14341438
if (!elem.childNodes[0]) {
14351439
// Not a comment or nested comment, skip
14361440
return null;
@@ -1613,7 +1617,7 @@ var BilibiliFormat = (function () {
16131617
for (var i = 0; i < elements.length; i++) {
16141618
var comment = this.parseOne(elements[i]);
16151619
if (comment !== null) {
1616-
commentList.push();
1620+
commentList.push(comment);
16171621
}
16181622
}
16191623
return commentList;
@@ -1622,6 +1626,7 @@ var BilibiliFormat = (function () {
16221626
BilibiliFormat.TextParser = function (params) {
16231627
this._allowInsecureDomParsing = true;
16241628
this._attemptEscaping = true;
1629+
this._canSecureParse = false;
16251630
if (typeof params === 'object') {
16261631
this._allowInsecureDomParsing = params.allowInsecureDomParsing === false ? false : true;
16271632
this._attemptEscaping = params.attemptEscaping === false ? false : true;
@@ -1630,7 +1635,10 @@ var BilibiliFormat = (function () {
16301635
// We can't rely on innerHTML anyways. Maybe we're in a restricted context (i.e. node).
16311636
this._allowInsecureDomParsing = false;
16321637
}
1633-
if (this._allowInsecureDomParsing) {
1638+
if (typeof DOMParser !== 'undefined' && DOMParser !== null) {
1639+
this._canSecureNativeParse = true;
1640+
}
1641+
if (this._allowInsecureDomParsing || this._canSecureNativeParse) {
16341642
this._xmlParser = new BilibiliFormat.XMLParser(params);
16351643
}
16361644
};
@@ -1650,8 +1658,12 @@ var BilibiliFormat = (function () {
16501658
} else {
16511659
return this._xmlParser.parseOne(tags[0]);
16521660
}
1653-
} else {
1654-
throw new Error('Secure parsing not implemented yet.');
1661+
} else if (this._canSecureNativeParse) {
1662+
var domParser = new DOMParser();
1663+
return this._xmlParser.parseOne(
1664+
domParser.parseFromString(comment, 'application/xml'));
1665+
} else{
1666+
throw new Error('Secure native js parsing not implemented yet.');
16551667
}
16561668
};
16571669

@@ -1666,8 +1678,13 @@ var BilibiliFormat = (function () {
16661678
temp.innerHTML = source;
16671679
var tags = temp.getElementsByTagName('d');
16681680
return this._xmlParser.parseMany(tags);
1681+
} else if (this._canSecureNativeParse) {
1682+
var domParser = new DOMParser();
1683+
return this._xmlParser.parseMany(
1684+
domParser.parseFromString(comment, 'application/xml'));
1685+
16691686
} else {
1670-
throw new Error('Secure parsing not implemented yet.');
1687+
throw new Error('Secure native js parsing not implemented yet.');
16711688
}
16721689
};
16731690

build/CommentCoreLibrary.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/CommentCoreLibrary_spec.coffee

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ describe 'CommentManager', ->
5353

5454
it 'smoking test', ->
5555
jasmine.getFixtures().fixturesPath = "test/"
56-
comments = AcfunParser(readFixtures 'ac940133.json')
56+
json = JSON.parse readFixtures 'ac940133.json'
57+
comments = (new AcfunFormat.JSONParser()).parseMany json
5758
# TODO: Construct a json that cover all types of comments
5859
# and use it for smoking test
5960
manager.load comments
Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
11
'use strict'
22
describe 'AcfunFormat', ->
3-
jasmine.getFixtures().fixturesPath = "test/"
4-
it 'works', ->
5-
json = readFixtures 'ACFun.json'
6-
comments = AcfunParser(json)
7-
expect(comments.length).toBe 155
8-
expect(comments[0]).toEqual
9-
stime: 98200
10-
color: 16777215
11-
mode: 1
12-
size: 25
13-
hash: 'guest'
14-
date: 1315564729
15-
position: 'absolute'
16-
text: '我谢了你的爱'
3+
jasmine.getFixtures().fixturesPath = "test/synthetic/"
4+
textfix = (text) -> text.replace(/\ /g, "\u00a0")
5+
6+
it 'provides json parser', ->
7+
expect(typeof AcfunFormat.JSONParser).toBe "function"
8+
9+
describe '.JSONParser', ->
10+
raw = readFixtures 'AcfunFormat.json'
11+
parser = data = null
12+
13+
beforeEach ->
14+
parser = new AcfunFormat.JSONParser()
15+
data = JSON.parse(raw)
16+
17+
it 'can parse one', ->
18+
expect(parser.parseOne(data[0])).toEqual
19+
stime: 1000
20+
color: 16763904
21+
mode: 5
22+
size: 25
23+
hash: 'guest'
24+
date: 1315736602.0
25+
position: 'absolute'
26+
text: textfix 'This is just some test.'
27+
28+
it 'can parse list', ->
29+
comments = parser.parseMany(data)
30+
expect(comments.length).toBe 2
31+
expect(comments[0]).toEqual
32+
stime: 1000
33+
color: 16763904
34+
mode: 5
35+
size: 25
36+
hash: 'guest'
37+
date: 1315736602.0
38+
position: 'absolute'
39+
text: textfix 'This is just some test.'
Lines changed: 100 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,103 @@
11
'use strict'
22
describe 'BilibiliFormat', ->
33
jasmine.getFixtures().fixturesPath = "test/"
4-
it 'parses normal comments', ->
5-
# TODO: Update testing to pass in an XML object instead of
6-
# relying on the unsafe innerHTML.
7-
xml_text = readFixtures 'av207527.xml'
8-
comments = BilibiliParser(null, xml_text)
9-
expect(comments.length).toBe 12546
10-
expect(comments[0]).toEqual
11-
stime: 15105
12-
size: 25
13-
color: 16777215
14-
mode: 1
15-
date: 1388314569
16-
pool: 0
17-
position: 'absolute'
18-
dbid: 364586099
19-
hash: '1a87dd40'
20-
border: false
21-
text: '关了弹幕瞬间好多了'
22-
23-
it 'parses scripting comments', ->
24-
xml_text = readFixtures 'scripting/tsubasa.xml'
25-
comments = BilibiliParser(null, xml_text)
26-
expect(comments.length).toBe 654
27-
expect(comments[0].mode).toEqual 7
28-
expect(comments[653].mode).toEqual 8
29-
30-
it 'parses advanced comments', ->
31-
xml_text = readFixtures 'boss.xml'
32-
comments = BilibiliParser(null, xml_text)
33-
expect(comments.length).toBe 1000
34-
expect(comments[0].mode).toEqual 7
35-
expect(comments[0].motion).not.toBe null
4+
5+
it 'provides xml parser', ->
6+
expect(typeof BilibiliFormat.XMLParser).toBe 'function'
7+
8+
it 'provides text parser', ->
9+
expect(typeof BilibiliFormat.TextParser).toBe 'function'
10+
11+
describe '.XMLParser', ->
12+
parser = null
13+
14+
beforeEach ->
15+
parser = new BilibiliFormat.XMLParser()
16+
17+
it 'has sane defaults', ->
18+
expect(parser._attemptFix).toBe true
19+
expect(parser._logBadComments).toBe true
20+
21+
it 'can be configured', ->
22+
parser = new BilibiliFormat.XMLParser
23+
attemptFix: false
24+
logBadComments: false
25+
expect(parser._attemptFix).toBe false
26+
expect(parser._logBadComments).toBe false
27+
28+
it 'only accepts xml documents', ->
29+
expect( => parser.parseOne "foo").toThrow()
30+
31+
it 'can parse one', ->
32+
xmltext = readFixtures 'av207527.xml'
33+
dom = (new DOMParser()).parseFromString xmltext, "application/xml"
34+
expect(parser.parseOne dom.getElementsByTagName('d')[0]).toEqual
35+
stime: 15105
36+
size: 25
37+
color: 16777215
38+
mode: 1
39+
date: 1388314569
40+
pool: 0
41+
position: 'absolute'
42+
dbid: 364586099
43+
hash: '1a87dd40'
44+
border: false
45+
text: '关了弹幕瞬间好多了'
46+
47+
it 'can parse many', ->
48+
xmltext = readFixtures 'av207527.xml'
49+
dom = (new DOMParser()).parseFromString xmltext, "application/xml"
50+
comments = parser.parseMany dom
51+
expect(comments.length).toBe 12546
52+
expect(comments[0]).toEqual
53+
stime: 15105
54+
size: 25
55+
color: 16777215
56+
mode: 1
57+
date: 1388314569
58+
pool: 0
59+
position: 'absolute'
60+
dbid: 364586099
61+
hash: '1a87dd40'
62+
border: false
63+
text: '关了弹幕瞬间好多了'
64+
65+
describe '.TextParser', ->
66+
parser = null
67+
68+
beforeEach ->
69+
parser = new BilibiliFormat.TextParser()
70+
71+
it 'has sane defaults', ->
72+
expect(parser._allowInsecureDomParsing).toBe true
73+
expect(parser._attemptEscaping).toBe true
74+
75+
it 'can be configured', ->
76+
parser = new BilibiliFormat.TextParser
77+
allowInsecureDomParsing: false
78+
attemptEscaping: false
79+
expect(parser._allowInsecureDomParsing).toBe false
80+
expect(parser._attemptEscaping).toBe false
81+
82+
it 'propagates parameters', ->
83+
parser = new BilibiliFormat.TextParser
84+
attemptFix: false
85+
logBadComments: false
86+
allowInsecureDomParsing: true
87+
expect(parser._xmlParser instanceof BilibiliFormat.XMLParser).toBe true
88+
expect(parser._xmlParser._attemptFix).toBe false
89+
expect(parser._xmlParser._logBadComments).toBe false
90+
91+
# it 'parses scripting comments', ->
92+
# xml_text = readFixtures 'scripting/tsubasa.xml'
93+
# comments = BilibiliParser(null, xml_text)
94+
# expect(comments.length).toBe 654
95+
# expect(comments[0].mode).toEqual 7
96+
# expect(comments[653].mode).toEqual 8
97+
98+
# it 'parses advanced comments', ->
99+
# xml_text = readFixtures 'boss.xml'
100+
# comments = BilibiliParser(null, xml_text)
101+
# expect(comments.length).toBe 1000
102+
# expect(comments[0].mode).toEqual 7
103+
# expect(comments[0].motion).not.toBe null

src/parsers/BilibiliFormat.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ var BilibiliFormat = (function () {
4949
}
5050

5151
BilibiliFormat.XMLParser.prototype.parseOne = function (elem) {
52-
var params = elem.getAttribute('p').split(',');
52+
try {
53+
var params = elem.getAttribute('p').split(',');
54+
} catch (e) {
55+
throw new Error("Unsupported object type or could not decompose.");
56+
}
5357
if (!elem.childNodes[0]) {
5458
// Not a comment or nested comment, skip
5559
return null;
@@ -232,7 +236,7 @@ var BilibiliFormat = (function () {
232236
for (var i = 0; i < elements.length; i++) {
233237
var comment = this.parseOne(elements[i]);
234238
if (comment !== null) {
235-
commentList.push();
239+
commentList.push(comment);
236240
}
237241
}
238242
return commentList;
@@ -241,6 +245,7 @@ var BilibiliFormat = (function () {
241245
BilibiliFormat.TextParser = function (params) {
242246
this._allowInsecureDomParsing = true;
243247
this._attemptEscaping = true;
248+
this._canSecureParse = false;
244249
if (typeof params === 'object') {
245250
this._allowInsecureDomParsing = params.allowInsecureDomParsing === false ? false : true;
246251
this._attemptEscaping = params.attemptEscaping === false ? false : true;
@@ -249,7 +254,10 @@ var BilibiliFormat = (function () {
249254
// We can't rely on innerHTML anyways. Maybe we're in a restricted context (i.e. node).
250255
this._allowInsecureDomParsing = false;
251256
}
252-
if (this._allowInsecureDomParsing) {
257+
if (typeof DOMParser !== 'undefined' && DOMParser !== null) {
258+
this._canSecureNativeParse = true;
259+
}
260+
if (this._allowInsecureDomParsing || this._canSecureNativeParse) {
253261
this._xmlParser = new BilibiliFormat.XMLParser(params);
254262
}
255263
};
@@ -269,8 +277,12 @@ var BilibiliFormat = (function () {
269277
} else {
270278
return this._xmlParser.parseOne(tags[0]);
271279
}
272-
} else {
273-
throw new Error('Secure parsing not implemented yet.');
280+
} else if (this._canSecureNativeParse) {
281+
var domParser = new DOMParser();
282+
return this._xmlParser.parseOne(
283+
domParser.parseFromString(comment, 'application/xml'));
284+
} else{
285+
throw new Error('Secure native js parsing not implemented yet.');
274286
}
275287
};
276288

@@ -285,8 +297,13 @@ var BilibiliFormat = (function () {
285297
temp.innerHTML = source;
286298
var tags = temp.getElementsByTagName('d');
287299
return this._xmlParser.parseMany(tags);
300+
} else if (this._canSecureNativeParse) {
301+
var domParser = new DOMParser();
302+
return this._xmlParser.parseMany(
303+
domParser.parseFromString(comment, 'application/xml'));
304+
288305
} else {
289-
throw new Error('Secure parsing not implemented yet.');
306+
throw new Error('Secure native js parsing not implemented yet.');
290307
}
291308
};
292309

test/Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,7 @@
9191
* `scripting/*.biliscript`
9292

9393
测试代码弹幕的各种小脚本
94+
95+
* `synthetic/*`
96+
97+
人造弹幕文件,目标是测试到所有的属性的解析

test/synthetic/AcfunFormat.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"c":"1,16763904,5,25,guest,1315736602.0",
4+
"m":"This is just some test."
5+
},
6+
{
7+
"c":"1,16777215,1,25,guest,1315736602.0",
8+
"m":"Comment 2."
9+
}
10+
]

test/synthetic/BilibiliFormat.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<i>
3+
<d p="">这是一个测试</d>
4+
</i>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[
2+
{
3+
"mode": 1,
4+
"text": "This is a normal scrolling comment. Color is white.",
5+
"size": 25,
6+
"color": 16777215,
7+
"stime": 0
8+
},
9+
{
10+
"mode": 1,
11+
"text": "This is a normal scrolling comment. Size is large",
12+
"size": 60,
13+
"color": 16777215,
14+
"stime": 100
15+
},
16+
{
17+
"mode": 1,
18+
"text": "This is a normal scrolling comment. Color is red.",
19+
"size": 25,
20+
"color": 16711680,
21+
"stime": 200
22+
},
23+
{
24+
"mode": 2,
25+
"text": "This is a normal "
26+
}
27+
]

0 commit comments

Comments
 (0)