Skip to content

Commit 69e3704

Browse files
committed
Fixed serious issues with tester & Work on html_translations_tables
1 parent 7173d25 commit 69e3704

File tree

4 files changed

+104
-50
lines changed

4 files changed

+104
-50
lines changed

_tools/tester.js

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,77 @@
11
function tester_comparer(result, should) {
2-
var valid = true;
3-
4-
if( result === null || result === false || result === undefined ) {
5-
if(result != should){
2+
var _getType = function(obj) {
3+
// Objects are php associative arrays.
4+
var t = typeof obj;
5+
t = t.toLowerCase();
6+
if (t == "object") {
7+
t = "array";
8+
}
9+
return t;
10+
}
11+
12+
// Determine types
13+
resType = _getType(result);
14+
shdType = _getType(should);
15+
16+
if (resType != shdType) {
17+
// Bail out if types don't match
18+
return false;
19+
}
20+
21+
// Iterate & compare arrays
22+
if (resType == "array") {
23+
24+
// Bail out if count doesn't match
25+
resCount = tester_count(result, 'COUNT_RECURSIVE');
26+
shdCount = tester_count(should, 'COUNT_RECURSIVE');
27+
if (resCount != shdCount) {
628
return false;
729
}
8-
} else if( result.constructor === Array || result.constructor === Object ) {
9-
for (key in result){
10-
// Recurse
11-
subres = tester_comparer(result[key], should[key]);
12-
if(subres[0] < 1){
13-
return subres;
30+
31+
for (key in should) {
32+
if (typeof(result[key]) == 'undefined') {
33+
// Bail out if element doesn't even exist in test-result
34+
return false;
35+
}
36+
if (false == tester_comparer(result[key], should[key])) {
37+
return false;
1438
}
1539
}
16-
} else {
17-
if(should != result){
18-
return false;
40+
// Arrays matched
41+
return true;
42+
}
43+
44+
// 'Simple' types.
45+
if(result != should){
46+
return false;
47+
}
48+
49+
// Simple types matched;
50+
return true;
51+
}
52+
53+
function tester_count( mixed_var, mode ) {
54+
// http://kevin.vanzonneveld.net
55+
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
56+
// + input by: _argos
57+
// * example 1: count([[0,0],[0,-4]], 'COUNT_RECURSIVE');
58+
// * returns 1: 6
59+
// * example 2: count({'one' : [1,2,3,4,5]}, 'COUNT_RECURSIVE');
60+
// * returns 2: 6
61+
62+
var key, cnt = 0;
63+
64+
if( mode == 'COUNT_RECURSIVE' ) mode = 1;
65+
if( mode != 1 ) mode = 0;
66+
67+
for (key in mixed_var){
68+
cnt++;
69+
if( mode==1 && mixed_var[key] && (mixed_var[key].constructor === Array || mixed_var[key].constructor === Object) ){
70+
cnt += count(mixed_var[key], 1);
1971
}
2072
}
2173

22-
return valid;
74+
return cnt;
2375
}
2476

2577
function tester_trim( str, charlist ) {

functions/strings/get_html_translation_table.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,38 @@ function get_html_translation_table(table, quote_style) {
44
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
55
// % note: It has been decided that we're not going to add global
66
// % note: dependencies to php.js. Meaning the constants are not
7-
// % note: real constants.
8-
// * example 1: get_html_translation_table('Kevin van Zonneveld');
9-
// * returns 1: 1249991249
7+
// % note: real constants, but strings instead. integers are also supported if someone
8+
// % note: chooses to create the constants themselves.
9+
// % note: Table from http://www.the-art-of-web.com/html/character-codes/
10+
// * example 1: get_html_translation_table('HTML_SPECIALCHARS');
11+
// * returns 1: {'"': '&quot;', '&': '&amp;', '<': '&lt;', '>': '&gt;'}
1012

11-
var constMapping = {}, entities = {}, decimal = 0, symbol = '';
13+
var entities = {}, histogram = {}, decimal = 0, symbol = '';
14+
var constMappingTable = {}, constMappingQuoteStyle = {};
1215
var useTable = table, useQuoteStyle = quote_style;
13-
16+
1417
// Translate arguments
15-
constMapping['table'][0] = 'HTML_SPECIALCHARS';
16-
constMapping['table'][1] = 'HTML_ENTITIES';
17-
constMapping['quote_style'][0] = 'ENT_NOQUOTES';
18-
constMapping['quote_style'][2] = 'ENT_COMPAT';
19-
constMapping['quote_style'][3] = 'ENT_QUOTES';
18+
constMappingTable[0] = 'HTML_SPECIALCHARS';
19+
constMappingTable[1] = 'HTML_ENTITIES';
20+
constMappingQuoteStyle[0] = 'ENT_NOQUOTES';
21+
constMappingQuoteStyle[2] = 'ENT_COMPAT';
22+
constMappingQuoteStyle[3] = 'ENT_QUOTES';
2023

21-
// Map
22-
if (constMapping['table'][useTable]) {
23-
useTable = constMapping['table'][useTable];
24+
// Map or Default
25+
if (!(useTable = constMappingTable[useTable])) {
26+
useTable = 'HTML_SPECIALCHARS'
2427
}
25-
if (constMapping['quote_style'][useQuoteStyle]) {
26-
useQuoteStyle = constMapping['quote_style'][useQuoteStyle];
28+
if (!(useQuoteStyle = constMappingQuoteStyle[useQuoteStyle])) {
29+
useQuoteStyle = 'ENT_COMPAT';
2730
}
2831

29-
// Default
30-
if (!useTable) useTable = 'HTML_SPECIALCHARS';
31-
if (!useQuoteStyle) useQuoteStyle = 'ENT_COMPAT';
32-
3332
if (useTable == 'HTML_SPECIALCHARS') {
3433
// ascii decimals for better compatibility
3534
entities['60'] = '&lt;';
3635
entities['62'] = '&gt;';
3736
entities['38'] = '&amp;';
38-
39-
if (quote_style != 'ENT_QUOTES') {
40-
entities['34'] = '&quot;';
41-
}
42-
43-
if (quote_style == 'ENT_QUOTES') {
44-
entities['39'] = '&#39;';
45-
}
4637
} else if (useTable == 'HTML_ENTITIES') {
4738
// ascii decimals for better compatibility
48-
entities['34'] = '&quot;';
4939
entities['38'] = '&amp;';
5040
entities['60'] = '&lt;';
5141
entities['62'] = '&gt;';
@@ -148,7 +138,15 @@ function get_html_translation_table(table, quote_style) {
148138
} else {
149139
return false;
150140
}
151-
141+
142+
if (useQuoteStyle != 'ENT_QUOTES') {
143+
entities['34'] = '&quot;';
144+
}
145+
146+
if (useQuoteStyle == 'ENT_QUOTES') {
147+
entities['39'] = '&#39;';
148+
}
149+
152150
// ascii decimals to real symbols
153151
for (decimal in entities) {
154152
symbol = String.fromCharCode(decimal)

functions/strings/htmlentities.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
function htmlentities( string ){
1+
function htmlentities (string, quote_style) {
22
// http://kevin.vanzonneveld.net
33
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
44
// + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
55
// + improved by: nobbler
66
// + tweaked by: Jack
77
// + bugfixed by: Onno Marsman
8-
// % note: table from http://www.the-art-of-web.com/html/character-codes/
8+
// - depends on: get_html_translation_table
99
// * example 1: htmlentities('Kevin & van Zonneveld');
1010
// * returns 1: 'Kevin &amp; van Zonneveld'
1111

@@ -112,17 +112,20 @@ function htmlentities( string ){
112112
histogram['253'] = 'yacute';
113113
histogram['254'] = 'thorn';
114114
histogram['255'] = 'yuml';
115-
115+
116+
histogram = get_html_translation_table('htmlentities', quote_style);
117+
116118
string += '';
117119
stringl = string.length
118120
for (i = 0; i < stringl; ++i) {
119121
code = string.charCodeAt(i);
120-
if (code in histogram) {
121-
tmp_arr[i] = '&'+histogram[code]+';';
122+
foundSymbol = string.charAt(i)
123+
if (matchSymbol in histogram) {
124+
tmp_arr[i] = histogram[matchSymbol];
122125
} else {
123-
tmp_arr[i] = string.charAt(i);
126+
tmp_arr[i] = foundSymbol;
124127
}
125128
}
126-
129+
127130
return tmp_arr.join('');
128131
}

functions/strings/htmlspecialchars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ function htmlspecialchars(string, quote_style) {
44
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
55
// + bugfixed by: Nathan
66
// + bugfixed by: Arno
7+
// - depends on: get_html_translation_table
78
// * example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
89
// * returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
910

0 commit comments

Comments
 (0)