Skip to content

Commit 27b865d

Browse files
committed
Implement most of ob_ functions to be compatible with echo() and functions which depend on it
1 parent 25f24b0 commit 27b865d

16 files changed

+71
-36
lines changed

_experimental/outcontrol/flush.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@ function flush () {
55
// * returns 1: undefined
66

77
var PHP_OUTPUT_HANDLER_START = 1, PHP_OUTPUT_HANDLER_CONT = 2;
8-
this.php_js = this.phpjs || {};
8+
this.php_js = this.php_js || {};
99
var phpjs = this.php_js, obs = phpjs.obs;
1010

11-
// Not distinct from ob_flush() in JavaScript, since not sending to a browser
11+
// Not distinct from ob_flush() in JavaScript (though doesn't add to buffer), since not sending to a browser
1212
if (!obs || !obs.length) {
1313
return;
1414
}
1515
var flags = 0, ob = obs[obs.length-1], buffer = ob.buffer;
16-
if (obs.callback) {
16+
// Fix: verify the behavior works this way here (doesn't add as much to the buffer)
17+
if (ob.callback) {
1718
if (!ob.status) {
1819
flags |= PHP_OUTPUT_HANDLER_START;
1920
}
2021
flags |= PHP_OUTPUT_HANDLER_CONT;
2122
ob.status = 2;
22-
buffer = obs.callback(buffer, flags);
23+
buffer = ob.callback(buffer, flags);
2324
}
25+
var flushing = this.php_js.flushing;
26+
this.php_js.flushing = true;
2427
this.echo(buffer);
28+
this.php_js.flushing = flushing;
2529
ob.buffer = '';
2630
}

_experimental/outcontrol/output_add_rewrite_var.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function output_add_rewrite_var (name, value) {
88

99
var handlers = [], handler = '', startAgain = true;
1010

11-
this.php_js = this.phpjs || {};
11+
this.php_js = this.php_js || {};
1212
var phpjs = this.php_js, obs = phpjs.obs;
1313

1414
handlers = this.ob_list_handlers();

_experimental/outcontrol/output_reset_rewrite_vars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function output_reset_rewrite_vars () {
66

77
// Fix: also need to deal with those set by session_start() (will add to same obs?)
88

9-
this.php_js = this.phpjs || {};
9+
this.php_js = this.php_js || {};
1010
var phpjs = this.php_js, obs = phpjs.obs;
1111

1212
if (obs && obs.length && obs[obs.length-1].vars) {

_experimental/outcontrol/ob_clean.js renamed to functions/outcontrol/ob_clean.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ function ob_clean () {
55
// * returns 1: undefined
66

77
var PHP_OUTPUT_HANDLER_START = 1, PHP_OUTPUT_HANDLER_CONT = 2;
8-
this.php_js = this.phpjs || {};
8+
this.php_js = this.php_js || {};
99
var phpjs = this.php_js, obs = phpjs.obs;
1010

1111
if (!obs || !obs.length) {
1212
return;
1313
}
1414
var flags = 0, ob = obs[obs.length-1], buffer = ob.buffer;
15-
if (obs.callback) {
15+
if (ob.callback) {
1616
if (!ob.status) {
1717
flags |= PHP_OUTPUT_HANDLER_START;
1818
}
1919
flags |= PHP_OUTPUT_HANDLER_CONT;
2020
ob.status = 1;
21-
buffer = obs.callback(buffer, flags);
21+
buffer = ob.callback(buffer, flags);
2222
}
2323
ob.buffer = '';
2424
}

_experimental/outcontrol/ob_end_clean.js renamed to functions/outcontrol/ob_end_clean.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ function ob_end_clean () {
55
// * returns 1: true
66

77
var PHP_OUTPUT_HANDLER_START = 1, PHP_OUTPUT_HANDLER_END = 4;
8-
this.php_js = this.phpjs || {};
8+
this.php_js = this.php_js || {};
99
var phpjs = this.php_js, obs = phpjs.obs;
1010

1111
if (!obs || !obs.length) {
1212
return false;
1313
}
1414
var flags = 0, ob = obs[obs.length-1], buffer = ob.buffer;
15-
if (obs.callback) {
15+
if (ob.callback) {
1616
if (!ob.status) {
1717
flags |= PHP_OUTPUT_HANDLER_START;
1818
}
1919
flags |= PHP_OUTPUT_HANDLER_END;
2020
ob.status = 2;
21-
buffer = obs.callback(buffer, flags);
21+
buffer = ob.callback(buffer, flags);
2222
}
2323
obs.pop();
2424
return true;

_experimental/outcontrol/ob_end_flush.js renamed to functions/outcontrol/ob_end_flush.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@ function ob_end_flush () {
66

77
var PHP_OUTPUT_HANDLER_START = 1, PHP_OUTPUT_HANDLER_END = 4;
88

9-
this.php_js = this.phpjs || {};
9+
this.php_js = this.php_js || {};
1010
var obs = this.php_js.obs;
1111

1212
if (!obs || !obs.length) {
1313
return false;
1414
}
1515
var flags = 0, ob = obs[obs.length-1], buffer = ob.buffer;
16-
if (obs.callback) {
16+
if (ob.callback) {
1717
if (!ob.status) {
1818
flags |= PHP_OUTPUT_HANDLER_START;
1919
}
2020
flags |= PHP_OUTPUT_HANDLER_END;
2121
ob.status = 2;
22-
buffer = obs.callback(buffer, flags);
22+
buffer = ob.callback(buffer, flags);
2323
}
2424
obs.pop();
25-
this.echo(buffer);
25+
if (obs.length) {
26+
ob = obs[obs.length-1];
27+
ob.buffer += buffer;
28+
}
29+
else {
30+
this.echo(buffer);
31+
}
2632

2733
return true;
2834
}

_experimental/outcontrol/ob_flush.js renamed to functions/outcontrol/ob_flush.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@ function ob_flush () {
55
// * returns 1: undefined
66

77
var PHP_OUTPUT_HANDLER_START = 1, PHP_OUTPUT_HANDLER_CONT = 2;
8-
this.php_js = this.phpjs || {};
8+
this.php_js = this.php_js || {};
99
var phpjs = this.php_js, obs = phpjs.obs;
1010

1111
if (!obs || !obs.length) {
1212
return;
1313
}
1414
var flags = 0, ob = obs[obs.length-1], buffer = ob.buffer;
15-
if (obs.callback) {
15+
if (ob.callback) {
1616
if (!ob.status) {
1717
flags |= PHP_OUTPUT_HANDLER_START;
1818
}
1919
flags |= PHP_OUTPUT_HANDLER_CONT;
2020
ob.status = 1;
21-
buffer = obs.callback(buffer, flags);
21+
buffer = ob.callback(buffer, flags);
22+
}
23+
if (obs.length > 1) {
24+
obs[obs.length-2].buffer += buffer;
25+
}
26+
else {
27+
var flushing = this.php_js.flushing;
28+
this.php_js.flushing = true;
29+
this.echo(buffer);
30+
this.php_js.flushing = flushing;
2231
}
23-
this.echo(ob.buffer);
2432
ob.buffer = '';
2533
}

_experimental/outcontrol/ob_get_clean.js renamed to functions/outcontrol/ob_get_clean.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ function ob_get_clean () {
66

77
var PHP_OUTPUT_HANDLER_START = 1, PHP_OUTPUT_HANDLER_END = 4;
88

9-
this.php_js = this.phpjs || {};
9+
this.php_js = this.php_js || {};
1010
var phpjs = this.php_js, obs = phpjs.obs;
1111
if (!obs || !obs.length) {
1212
return false;
1313
}
1414
var flags = 0, ob = obs[obs.length-1], buffer = ob.buffer;
15-
if (obs.callback) {
15+
if (ob.callback) {
1616
if (!ob.status) {
1717
flags |= PHP_OUTPUT_HANDLER_START;
1818
}
1919
flags |= PHP_OUTPUT_HANDLER_END;
2020
ob.status = 2;
21-
buffer = obs.callback(buffer, flags);
21+
buffer = ob.callback(buffer, flags);
2222
}
2323
obs.pop();
2424
return buffer;

_experimental/outcontrol/ob_get_contents.js renamed to functions/outcontrol/ob_get_contents.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function ob_get_contents () {
44
// * example 1: ob_get_contents();
55
// * returns 1: 'some buffer contents'
66

7-
this.php_js = this.phpjs || {};
7+
this.php_js = this.php_js || {};
88
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
99
if (!obs || !obs.length) {
1010
return (ini && ini.output_buffering &&

_experimental/outcontrol/ob_get_flush.js renamed to functions/outcontrol/ob_get_flush.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ function ob_get_flush () {
55
// * returns 1: 'some buffer contents'
66

77
var PHP_OUTPUT_HANDLER_START = 1, PHP_OUTPUT_HANDLER_END = 4;
8-
this.php_js = this.phpjs || {};
8+
this.php_js = this.php_js || {};
99
var phpjs = this.php_js, obs = phpjs.obs;
1010
if (!obs || !obs.length) {
1111
return false;
1212
}
1313
var flags = 0, ob = obs[obs.length-1], buffer = ob.buffer;
14-
if (obs.callback) {
14+
if (ob.callback) {
1515
if (!ob.status) {
1616
flags |= PHP_OUTPUT_HANDLER_START;
1717
}
1818
flags |= PHP_OUTPUT_HANDLER_END;
1919
ob.status = 2;
20-
buffer = obs.callback(buffer, flags);
20+
buffer = ob.callback(buffer, flags);
2121
}
22-
this.echo(buffer);
2322
obs.pop();
23+
if (obs.length) {
24+
ob = obs[obs.length-1];
25+
ob.buffer += buffer;
26+
}
27+
else {
28+
this.echo(buffer);
29+
}
2430
return buffer;
2531
}

_experimental/outcontrol/ob_get_length.js renamed to functions/outcontrol/ob_get_length.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ function ob_get_length () {
44
// * example 1: ob_get_length();
55
// * returns 1: 155
66

7-
this.php_js = this.phpjs || {};
7+
this.php_js = this.php_js || {};
88
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
99

1010
if (!obs || !obs.length) {
1111
return (ini && ini['output_buffering'] &&
1212
(typeof ini['output_buffering'].local_value !== 'string' ||
1313
ini['output_buffering'].local_value.toLowerCase() !== 'off')) ? 0 : false; // If output was already buffered, it would be available in obs
1414
}
15+
// Fix: WIll probably need to change depending on Unicode semantics
1516
return obs[obs.length-1].buffer.length; // Retrieve length of most recently added buffer contents
1617
}

_experimental/outcontrol/ob_get_level.js renamed to functions/outcontrol/ob_get_level.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function ob_get_level () {
44
// * example 1: ob_get_level();
55
// * returns 1: 1
66

7-
this.php_js = this.phpjs || {};
7+
this.php_js = this.php_js || {};
88
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
99

1010
if (!obs || !obs.length) {

_experimental/outcontrol/ob_get_status.js renamed to functions/outcontrol/ob_get_status.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function ob_get_status (full_status) {
1313
return name[1];
1414
};
1515

16-
this.php_js = this.phpjs || {};
16+
this.php_js = this.php_js || {};
1717
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
1818

1919
if (!obs || !obs.length) {

_experimental/outcontrol/ob_list_handlers.js renamed to functions/outcontrol/ob_list_handlers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function ob_list_handlers () {
1313
return name[1];
1414
};
1515

16-
this.php_js = this.phpjs || {};
16+
this.php_js = this.php_js || {};
1717
var phpjs = this.php_js, ini = phpjs.ini;
1818

1919
if (!phpjs.obs || !phpjs.obs.length) {

_experimental/outcontrol/ob_start.js renamed to functions/outcontrol/ob_start.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
function ob_start (output_callback, chunk_size, erase) {
22
// http://kevin.vanzonneveld.net
33
// + original by: Brett Zamir (http://brett-zamir.me)
4+
// % note 1: chunk_size and erase arguments are not presently supported
45
// * example 1: ob_start('someCallback', 4096, true);
56
// * returns 1: true
67

78
var bufferObj = {}, internalType = false, extra=false;
89
erase = !(erase === false); // true is default
910
chunk_size = chunk_size === 1 ? 4096 : (chunk_size || 0);
1011

11-
this.php_js = this.phpjs || {};
12-
this.php_js.obs = phpjs.obs || []; // Array for nestable buffers
12+
this.php_js = this.php_js || {};
13+
this.php_js.obs = this.php_js.obs || []; // Array for nestable buffers
1314
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
1415

1516
if (!obs &&

functions/strings/echo.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function echo () {
1111
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
1212
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
1313
// + bugfixed by: EdorFaus
14+
// + improved by: Brett Zamir (http://brett-zamir.me)
1415
// % note 1: If browsers start to support DOM Level 3 Load and Save (parsing/serializing),
1516
// % note 1: we wouldn't need any such long code (even most of the code below). See
1617
// % note 1: link below for a cross-browser implementation in JavaScript. HTML5 might
@@ -130,11 +131,19 @@ function echo () {
130131
}
131132
};
132133

134+
this.php_js = this.php_js || {};
135+
var phpjs = this.php_js, ini = phpjs.ini, obs = phpjs.obs;
133136
for (i = 0; i < argc; i++ ) {
134137
arg = argv[i];
135-
if (this.php_js && this.php_js.ini && this.php_js.ini['phpjs.echo_embedded_vars']) {
136-
arg = arg.replace(/(.?)\{\$(.*?)\}/g, replacer);
138+
if (ini && ini['phpjs.echo_embedded_vars']) {
139+
arg = arg.replace(/(.?)\{?\$(\w*?\}|\w*)/g, replacer);
140+
}
141+
142+
if (!phpjs.flushing && obs && obs.length) { // If flushing we output, but otherwise presence of a buffer means caching output
143+
obs[obs.length-1].buffer += arg;
144+
continue;
137145
}
146+
138147
if (d.appendChild) {
139148
if (d.body) {
140149
if (win.navigator.appName === 'Microsoft Internet Explorer') { // We unfortunately cannot use feature detection, since this is an IE bug with cloneNode nodes being appended

0 commit comments

Comments
 (0)