Skip to content

Commit

Permalink
Added some more tests to cover new comment loading system
Browse files Browse the repository at this point in the history
  • Loading branch information
jabbany committed Oct 14, 2016
1 parent 44e6ece commit 17a7b3d
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 60 deletions.
29 changes: 23 additions & 6 deletions build/CommentCoreLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,11 @@ var BilibiliFormat = (function () {
}

BilibiliFormat.XMLParser.prototype.parseOne = function (elem) {
var params = elem.getAttribute('p').split(',');
try {
var params = elem.getAttribute('p').split(',');
} catch (e) {
throw new Error("Unsupported object type or could not decompose.");
}
if (!elem.childNodes[0]) {
// Not a comment or nested comment, skip
return null;
Expand Down Expand Up @@ -1613,7 +1617,7 @@ var BilibiliFormat = (function () {
for (var i = 0; i < elements.length; i++) {
var comment = this.parseOne(elements[i]);
if (comment !== null) {
commentList.push();
commentList.push(comment);
}
}
return commentList;
Expand All @@ -1622,6 +1626,7 @@ var BilibiliFormat = (function () {
BilibiliFormat.TextParser = function (params) {
this._allowInsecureDomParsing = true;
this._attemptEscaping = true;
this._canSecureParse = false;
if (typeof params === 'object') {
this._allowInsecureDomParsing = params.allowInsecureDomParsing === false ? false : true;
this._attemptEscaping = params.attemptEscaping === false ? false : true;
Expand All @@ -1630,7 +1635,10 @@ var BilibiliFormat = (function () {
// We can't rely on innerHTML anyways. Maybe we're in a restricted context (i.e. node).
this._allowInsecureDomParsing = false;
}
if (this._allowInsecureDomParsing) {
if (typeof DOMParser !== 'undefined' && DOMParser !== null) {
this._canSecureNativeParse = true;
}
if (this._allowInsecureDomParsing || this._canSecureNativeParse) {
this._xmlParser = new BilibiliFormat.XMLParser(params);
}
};
Expand All @@ -1650,8 +1658,12 @@ var BilibiliFormat = (function () {
} else {
return this._xmlParser.parseOne(tags[0]);
}
} else {
throw new Error('Secure parsing not implemented yet.');
} else if (this._canSecureNativeParse) {
var domParser = new DOMParser();
return this._xmlParser.parseOne(
domParser.parseFromString(comment, 'application/xml'));
} else{
throw new Error('Secure native js parsing not implemented yet.');
}
};

Expand All @@ -1666,8 +1678,13 @@ var BilibiliFormat = (function () {
temp.innerHTML = source;
var tags = temp.getElementsByTagName('d');
return this._xmlParser.parseMany(tags);
} else if (this._canSecureNativeParse) {
var domParser = new DOMParser();
return this._xmlParser.parseMany(
domParser.parseFromString(comment, 'application/xml'));

} else {
throw new Error('Secure parsing not implemented yet.');
throw new Error('Secure native js parsing not implemented yet.');
}
};

Expand Down
2 changes: 1 addition & 1 deletion build/CommentCoreLibrary.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion spec/CommentCoreLibrary_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ describe 'CommentManager', ->

it 'smoking test', ->
jasmine.getFixtures().fixturesPath = "test/"
comments = AcfunParser(readFixtures 'ac940133.json')
json = JSON.parse readFixtures 'ac940133.json'
comments = (new AcfunFormat.JSONParser()).parseMany json
# TODO: Construct a json that cover all types of comments
# and use it for smoking test
manager.load comments
Expand Down
51 changes: 37 additions & 14 deletions spec/parsers/AcfunFormat_spec.coffee
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
'use strict'
describe 'AcfunFormat', ->
jasmine.getFixtures().fixturesPath = "test/"
it 'works', ->
json = readFixtures 'ACFun.json'
comments = AcfunParser(json)
expect(comments.length).toBe 155
expect(comments[0]).toEqual
stime: 98200
color: 16777215
mode: 1
size: 25
hash: 'guest'
date: 1315564729
position: 'absolute'
text: '我谢了你的爱'
jasmine.getFixtures().fixturesPath = "test/synthetic/"
textfix = (text) -> text.replace(/\ /g, "\u00a0")

it 'provides json parser', ->
expect(typeof AcfunFormat.JSONParser).toBe "function"

describe '.JSONParser', ->
raw = readFixtures 'AcfunFormat.json'
parser = data = null

beforeEach ->
parser = new AcfunFormat.JSONParser()
data = JSON.parse(raw)

it 'can parse one', ->
expect(parser.parseOne(data[0])).toEqual
stime: 1000
color: 16763904
mode: 5
size: 25
hash: 'guest'
date: 1315736602.0
position: 'absolute'
text: textfix 'This is just some test.'

it 'can parse list', ->
comments = parser.parseMany(data)
expect(comments.length).toBe 2
expect(comments[0]).toEqual
stime: 1000
color: 16763904
mode: 5
size: 25
hash: 'guest'
date: 1315736602.0
position: 'absolute'
text: textfix 'This is just some test.'
132 changes: 100 additions & 32 deletions spec/parsers/BilibiliFormat_spec.coffee
Original file line number Diff line number Diff line change
@@ -1,35 +1,103 @@
'use strict'
describe 'BilibiliFormat', ->
jasmine.getFixtures().fixturesPath = "test/"
it 'parses normal comments', ->
# TODO: Update testing to pass in an XML object instead of
# relying on the unsafe innerHTML.
xml_text = readFixtures 'av207527.xml'
comments = BilibiliParser(null, xml_text)
expect(comments.length).toBe 12546
expect(comments[0]).toEqual
stime: 15105
size: 25
color: 16777215
mode: 1
date: 1388314569
pool: 0
position: 'absolute'
dbid: 364586099
hash: '1a87dd40'
border: false
text: '关了弹幕瞬间好多了'

it 'parses scripting comments', ->
xml_text = readFixtures 'scripting/tsubasa.xml'
comments = BilibiliParser(null, xml_text)
expect(comments.length).toBe 654
expect(comments[0].mode).toEqual 7
expect(comments[653].mode).toEqual 8

it 'parses advanced comments', ->
xml_text = readFixtures 'boss.xml'
comments = BilibiliParser(null, xml_text)
expect(comments.length).toBe 1000
expect(comments[0].mode).toEqual 7
expect(comments[0].motion).not.toBe null

it 'provides xml parser', ->
expect(typeof BilibiliFormat.XMLParser).toBe 'function'

it 'provides text parser', ->
expect(typeof BilibiliFormat.TextParser).toBe 'function'

describe '.XMLParser', ->
parser = null

beforeEach ->
parser = new BilibiliFormat.XMLParser()

it 'has sane defaults', ->
expect(parser._attemptFix).toBe true
expect(parser._logBadComments).toBe true

it 'can be configured', ->
parser = new BilibiliFormat.XMLParser
attemptFix: false
logBadComments: false
expect(parser._attemptFix).toBe false
expect(parser._logBadComments).toBe false

it 'only accepts xml documents', ->
expect( => parser.parseOne "foo").toThrow()

it 'can parse one', ->
xmltext = readFixtures 'av207527.xml'
dom = (new DOMParser()).parseFromString xmltext, "application/xml"
expect(parser.parseOne dom.getElementsByTagName('d')[0]).toEqual
stime: 15105
size: 25
color: 16777215
mode: 1
date: 1388314569
pool: 0
position: 'absolute'
dbid: 364586099
hash: '1a87dd40'
border: false
text: '关了弹幕瞬间好多了'

it 'can parse many', ->
xmltext = readFixtures 'av207527.xml'
dom = (new DOMParser()).parseFromString xmltext, "application/xml"
comments = parser.parseMany dom
expect(comments.length).toBe 12546
expect(comments[0]).toEqual
stime: 15105
size: 25
color: 16777215
mode: 1
date: 1388314569
pool: 0
position: 'absolute'
dbid: 364586099
hash: '1a87dd40'
border: false
text: '关了弹幕瞬间好多了'

describe '.TextParser', ->
parser = null

beforeEach ->
parser = new BilibiliFormat.TextParser()

it 'has sane defaults', ->
expect(parser._allowInsecureDomParsing).toBe true
expect(parser._attemptEscaping).toBe true

it 'can be configured', ->
parser = new BilibiliFormat.TextParser
allowInsecureDomParsing: false
attemptEscaping: false
expect(parser._allowInsecureDomParsing).toBe false
expect(parser._attemptEscaping).toBe false

it 'propagates parameters', ->
parser = new BilibiliFormat.TextParser
attemptFix: false
logBadComments: false
allowInsecureDomParsing: true
expect(parser._xmlParser instanceof BilibiliFormat.XMLParser).toBe true
expect(parser._xmlParser._attemptFix).toBe false
expect(parser._xmlParser._logBadComments).toBe false

# it 'parses scripting comments', ->
# xml_text = readFixtures 'scripting/tsubasa.xml'
# comments = BilibiliParser(null, xml_text)
# expect(comments.length).toBe 654
# expect(comments[0].mode).toEqual 7
# expect(comments[653].mode).toEqual 8

# it 'parses advanced comments', ->
# xml_text = readFixtures 'boss.xml'
# comments = BilibiliParser(null, xml_text)
# expect(comments.length).toBe 1000
# expect(comments[0].mode).toEqual 7
# expect(comments[0].motion).not.toBe null
29 changes: 23 additions & 6 deletions src/parsers/BilibiliFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ var BilibiliFormat = (function () {
}

BilibiliFormat.XMLParser.prototype.parseOne = function (elem) {
var params = elem.getAttribute('p').split(',');
try {
var params = elem.getAttribute('p').split(',');
} catch (e) {
throw new Error("Unsupported object type or could not decompose.");
}
if (!elem.childNodes[0]) {
// Not a comment or nested comment, skip
return null;
Expand Down Expand Up @@ -232,7 +236,7 @@ var BilibiliFormat = (function () {
for (var i = 0; i < elements.length; i++) {
var comment = this.parseOne(elements[i]);
if (comment !== null) {
commentList.push();
commentList.push(comment);
}
}
return commentList;
Expand All @@ -241,6 +245,7 @@ var BilibiliFormat = (function () {
BilibiliFormat.TextParser = function (params) {
this._allowInsecureDomParsing = true;
this._attemptEscaping = true;
this._canSecureParse = false;
if (typeof params === 'object') {
this._allowInsecureDomParsing = params.allowInsecureDomParsing === false ? false : true;
this._attemptEscaping = params.attemptEscaping === false ? false : true;
Expand All @@ -249,7 +254,10 @@ var BilibiliFormat = (function () {
// We can't rely on innerHTML anyways. Maybe we're in a restricted context (i.e. node).
this._allowInsecureDomParsing = false;
}
if (this._allowInsecureDomParsing) {
if (typeof DOMParser !== 'undefined' && DOMParser !== null) {
this._canSecureNativeParse = true;
}
if (this._allowInsecureDomParsing || this._canSecureNativeParse) {
this._xmlParser = new BilibiliFormat.XMLParser(params);
}
};
Expand All @@ -269,8 +277,12 @@ var BilibiliFormat = (function () {
} else {
return this._xmlParser.parseOne(tags[0]);
}
} else {
throw new Error('Secure parsing not implemented yet.');
} else if (this._canSecureNativeParse) {
var domParser = new DOMParser();
return this._xmlParser.parseOne(
domParser.parseFromString(comment, 'application/xml'));
} else{
throw new Error('Secure native js parsing not implemented yet.');
}
};

Expand All @@ -285,8 +297,13 @@ var BilibiliFormat = (function () {
temp.innerHTML = source;
var tags = temp.getElementsByTagName('d');
return this._xmlParser.parseMany(tags);
} else if (this._canSecureNativeParse) {
var domParser = new DOMParser();
return this._xmlParser.parseMany(
domParser.parseFromString(comment, 'application/xml'));

} else {
throw new Error('Secure parsing not implemented yet.');
throw new Error('Secure native js parsing not implemented yet.');
}
};

Expand Down
4 changes: 4 additions & 0 deletions test/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@
* `scripting/*.biliscript`

测试代码弹幕的各种小脚本

* `synthetic/*`

人造弹幕文件,目标是测试到所有的属性的解析
10 changes: 10 additions & 0 deletions test/synthetic/AcfunFormat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"c":"1,16763904,5,25,guest,1315736602.0",
"m":"This is just some test."
},
{
"c":"1,16777215,1,25,guest,1315736602.0",
"m":"Comment 2."
}
]
4 changes: 4 additions & 0 deletions test/synthetic/BilibiliFormat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<i>
<d p="">这是一个测试</d>
</i>
27 changes: 27 additions & 0 deletions test/synthetic/CommonDanmakuFormat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"mode": 1,
"text": "This is a normal scrolling comment. Color is white.",
"size": 25,
"color": 16777215,
"stime": 0
},
{
"mode": 1,
"text": "This is a normal scrolling comment. Size is large",
"size": 60,
"color": 16777215,
"stime": 100
},
{
"mode": 1,
"text": "This is a normal scrolling comment. Color is red.",
"size": 25,
"color": 16711680,
"stime": 200
},
{
"mode": 2,
"text": "This is a normal "
}
]

0 comments on commit 17a7b3d

Please sign in to comment.