Skip to content
This repository was archived by the owner on Jul 15, 2019. It is now read-only.

Commit 0f7304b

Browse files
committed
Merge pull request #4 from neraliu/comment-the-code
add the comment to the logic of the switch case.
2 parents ac87c6a + 1a696d7 commit 0f7304b

2 files changed

Lines changed: 33 additions & 24 deletions

File tree

src/derived-states.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ DerivedState.TransitionsSparse = {
2828
53: {46: 1}
2929
};
3030

31+
/* this is the meaning of the key being used in below matrix
32+
33+
0 - do nothing in the postWalk callback, so the input will be filtered out (default switch case).
34+
1 - append the input to the output buffer, the output buffer will be returned in the purify function call.
35+
2 - the core logic to handle the tagName, attribute value.
36+
3 - clean up the attribute value with space encounters.
37+
4 - set the attribute value to the attribute name when it transits to "after attribute value (quoted) state" or "before attribute name state".
38+
39+
the following 2 handling have the assumption of canonicalization to address some parse error of the html web pages.
40+
5 - append the '<' with next char for markup declaration open state
41+
6 - set the self closing tag if solidus is encountered.
42+
43+
*/
3144
DerivedState.Transitions = [
3245
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
3346
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

tests/unit/html-purify.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ Authors: Aditya Mahendrakar <maditya@yahoo-inc.com>
88
Albert Yu <albertyu@yahoo-inc.com>
99
Adonis Fung <adon@yahoo-inc.com>
1010
*/
11-
12-
1311
(function () {
1412

1513
require("mocha");
16-
1714
var assert = require("assert"),
1815
testVectors = require("../test-vectors.js"),
1916
html5secVectors = testVectors.html5secVectors,
2017
generalVectors = testVectors.generalVectors,
2118
Purifier = require("../../src/html-purify");
19+
2220
describe('HTML Purify', function() {
2321

2422
it('should allow whitelisted tags and attributes', function(){
@@ -49,63 +47,61 @@ Authors: Aditya Mahendrakar <maditya@yahoo-inc.com>
4947
it('should allow self-closing tags', function(){
5048
var html = "<br /> hello world </br>"
5149
var output = (new Purifier()).purify(html);
52-
console.log(output);
50+
console.log(output);
5351
assert.equal(output, '<br /> hello world </br>');
5452
});
53+
5554
it('should handle href attributes', function(){
5655
var html = "<a href=\"http://www.yahoo.com\">yahoo</a>";
5756
var output = (new Purifier()).purify(html);
58-
console.log(output);
57+
console.log(output);
5958
assert.equal(output, '<a href="http://www.yahoo.com">yahoo</a>');
6059
});
60+
6161
it('should handle js in href attributes', function(){
6262
var html = "<a href=\"javascript:alert(1)\">yahoo</a>";
6363
var output = (new Purifier()).purify(html);
64-
console.log(output);
64+
console.log(output);
6565
assert.equal(output, '<a href="x-javascript:alert(1)">yahoo</a>');
6666
});
6767

68-
it('should strip attributes in the end tag', function(){
68+
it('should strip attributes in the end tag', function(){
6969
var html = "<h1 dir = \"asd\">hello</h1 id=\"bar\">"
7070
var output = (new Purifier()).purify(html);
71-
console.log(output);
71+
console.log(output);
7272
assert.equal(output, '<h1 dir=\"asd\">hello</h1>');
7373
});
7474

7575
it('should handle characters between attributes correctly', function(){
7676
var html = "<h1 label dir = \"asd\" evil1 defer \"evil2\" evil3 evil4=\"asdasd\" icon 'evil5>hello</h1>"
7777
var output = (new Purifier()).purify(html);
78-
console.log(output);
78+
console.log(output);
7979
assert.equal(output, '<h1 label dir=\"asd\" defer icon>hello</h1>');
8080
});
8181

8282
it('should allow style attribute if the css is valid', function(){
8383
var html = "<div style=\"color:#0000FF\">"
8484
var output = (new Purifier()).purify(html);
85-
console.log(output);
85+
console.log(output);
8686
assert.equal(output, '<div style=\"color:#0000FF\">');
8787

8888
// invalid css
89-
html = "<div style=\"color;foobar\">"
90-
output = (new Purifier()).purify(html);
91-
console.log(output);
92-
assert.equal(output, '<div>');
89+
html = "<div style=\"color;foobar\">";
90+
output = (new Purifier()).purify(html);
91+
console.log(output);
92+
assert.equal(output, '<div>');
9393
});
9494

9595
it('should handle additional vectors', function(){
9696
var output, i, vector;
9797
for (var i = 0; i < generalVectors.length; i++) {
98-
vector = generalVectors[i].input;
99-
output = (new Purifier()).purify(vector);
100-
console.log("*****" + generalVectors[i].id + "*****");
101-
console.log("input ==> " + vector);
102-
console.log("output ==> " + output);
103-
assert.equal(output, generalVectors[i].output);
98+
vector = generalVectors[i].input;
99+
output = (new Purifier()).purify(vector);
100+
console.log("*****" + generalVectors[i].id + "*****");
101+
console.log("input ==> " + vector);
102+
console.log("output ==> " + output);
103+
assert.equal(output, generalVectors[i].output);
104104
}
105-
106105
});
107-
108-
109106
});
110-
111107
}());

0 commit comments

Comments
 (0)