Skip to content

Commit 2da839a

Browse files
author
Daniel Herzog
committed
Fixed todos; Cleaned up; Improved tokenizer test
1 parent c84753e commit 2da839a

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

src/syntaxhighlight/http-header/tokenizer.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ window.cls = window.cls || {};
44

55
cls.HTTPHeaderTokenizer = function()
66
{
7-
var CR = "\r";
87
var LF = "\n";
98
var PUNCTUATOR = ":";
10-
var WHITESPACE_CHARS =
11-
{
12-
'\u0009': 1, // Tab <TAB>
13-
'\u0020': 1, // Space <SP>
9+
var WHITESPACE_CHARS = {
10+
"\u0009": 1, // Tab <TAB>
11+
"\u0020": 1, // Space <SP>
1412
};
1513

1614
this._buffer = "";
@@ -24,46 +22,39 @@ cls.HTTPHeaderTokenizer = function()
2422
this._buffer = input_buffer;
2523
this._emitToken = ontoken;
2624
while (this._state_handler !== this._state_handlers.EOF)
27-
{
2825
this._state_handler.apply(this);
29-
}
3026

3127
this._state_handlers.EOF.apply(this);
3228
};
3329

34-
this._state_handlers =
30+
this._state_handlers =
3531
{
3632
FIRST_LINE_PART: function()
3733
{
3834
if (this._is_EOF())
39-
{
4035
return false;
41-
}
36+
4237
var c = this._buffer.charAt(this._current_pos++);
4338
this._token_type = cls.HTTPHeaderTokenizer.types.FIRST_LINE_PART;
4439
if (c in WHITESPACE_CHARS)
4540
{
4641
this._emitToken(this._token_type ,this._token_buffer);
4742
this._token_buffer = "";
48-
// For now, LF and whitespace add to the next token. Visually that makes no difference.
4943
}
5044
else
5145
if (c === LF)
5246
{
5347
this._emitToken(this._token_type ,this._token_buffer);
5448
this._token_buffer = "";
55-
this._emitToken(cls.HTTPHeaderTokenizer.types.LINE_SEPARATOR, c); // todo: don't emit your own token.
5649
this._state_handler = this._state_handlers.NAME;
57-
return false;
5850
}
5951
this._token_buffer += c;
6052
},
6153
NAME: function()
6254
{
6355
if (this._is_EOF())
64-
{
6556
return false;
66-
}
57+
6758
var c = this._buffer.charAt(this._current_pos++);
6859
this._token_type = cls.HTTPHeaderTokenizer.types.NAME;
6960
if (c === PUNCTUATOR)
@@ -79,9 +70,8 @@ cls.HTTPHeaderTokenizer = function()
7970
VALUE: function()
8071
{
8172
if (this._is_EOF())
82-
{
8373
return false;
84-
}
74+
8575
var c = this._buffer.charAt(this._current_pos++);
8676
this._token_type = cls.HTTPHeaderTokenizer.types.VALUE;
8777
// LF only means switching to header when the following char is not whitespace.
@@ -90,7 +80,6 @@ cls.HTTPHeaderTokenizer = function()
9080
this._emitToken(this._token_type ,this._token_buffer);
9181
this._token_buffer = "";
9282
this._state_handler = this._state_handlers.NAME;
93-
// For now, LF and whitespace add to the next token. Visually that makes no difference.
9483
}
9584
this._token_buffer += c;
9685
},
@@ -108,13 +97,12 @@ cls.HTTPHeaderTokenizer = function()
10897
return true;
10998
}
11099
return false;
111-
}
100+
};
112101
}
113102

114103
cls.HTTPHeaderTokenizer.types = {
115104
FIRST_LINE_PART : 1,
116105
NAME : 2,
117106
VALUE : 3,
118-
PUNCTUATOR : 4,
119-
LINE_SEPARATOR : 5
107+
PUNCTUATOR : 4
120108
};

test-scripts/network-view/header-tokenizer.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</style>
3232

3333
<script src="../../src/scripts/dom.js"></script>
34-
<script src="../../src/syntaxhighlight/http/tokenizer.js"></script>
34+
<script src="../../src/syntaxhighlight/http-header/tokenizer.js"></script>
3535

3636
<script>
3737

@@ -92,11 +92,13 @@
9292

9393
window.onload = function()
9494
{
95-
document.body.firstElementChild.render(get_tokens(raw_headers).map(token_template));
96-
document.body.firstElementChild.render([["br"], ["br"]]);
97-
document.body.firstElementChild.render(get_tokens(raw_headers2).map(token_template));
98-
document.body.firstElementChild.render([["br"], ["br"]]);
99-
document.body.firstElementChild.render(get_tokens(raw_headers3).map(token_template));
95+
var headers = [raw_headers, raw_headers2, raw_headers3];
96+
headers.forEach(function(header_string){
97+
var pre = document.body.firstElementChild.render(["pre"])
98+
pre.render(get_tokens(header_string).map(token_template));
99+
if (pre.textContent !== header_string)
100+
throw(new Error("Tokenized headers produced different result."));
101+
});
100102
}
101103

102104
</script>

0 commit comments

Comments
 (0)