2
Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
3
The copyrights embodied in the content of this file are licensed by
4
Yahoo! Inc. under the BSD (revised) open source license
6
@author Dan Vlad Dascalescu <dandv@yahoo-inc.com>
11
<html xmlns="http://www.w3.org/1999/xhtml">
13
<script src="../../js/codemirror.js" type="text/javascript"></script>
14
<title>CodeMirror: PHP+HTML+JavaScript+CSS mixed-mode demonstration</title>
15
<link rel="stylesheet" type="text/css" href="../../css/docs.css"/>
17
<body style="padding: 20px;">
19
<p>This is a complex demonstration of the <b>PHP+HTML+JavaScript+CSS mixed-mode
20
syntax highlight</b> capabilities of <a href="../../index.html">CodeMirror</a>.
21
<?php ... ?> tags use the PHP parser, <script> tags use the JavaScript
22
parser, and <style> tags use the CSS parser. The rest of the content is
23
parsed using the XML parser in HTML mode.</p>
25
<p>Features of the PHP parser:
27
<li>special "deprecated" style for PHP4 keywords like 'var'
28
<li>support for PHP 5.3 keywords: 'namespace', 'use'
29
<li>911 predefined constants, 1301 predefined functions, 105 predeclared classes
30
from a typical PHP installation in a LAMP environment
31
<li>new feature: syntax error flagging, thus enabling strict parsing of:
33
<li>function definitions with explicitly or implicitly typed arguments and default values
34
<li>modifiers (public, static etc.) applied to method and member definitions
35
<li>foreach(array_expression as $key [=> $value]) loops
37
<li>differentiation between single-quoted strings and double-quoted interpolating strings
41
<div style="border: 1px solid black; padding: 3px; background-color: #F8F8F8">
42
<textarea id="code" cols="120" rows="30">
43
The "root" parser is XML in HTML mode.
44
Next, we can switch into PHP mode, for example. This is
45
<?php echo 'text output by';
48
On the line above, we just had an XML syntax error due to the </b> tag not being opened.
50
<?xml version='1.0' encoding='UTF-8' standalone='yes'?> HTML text will follow
53
<title>Similarly, the 'script' tag will switch to the JavaScript parser:</title>
54
<script type="text/javascript">
55
// Press enter inside the object and your new line will be suitably
58
enter: "newline-and-indent",
59
tab: "reindent-selection",
60
ctrl_enter: "reparse-buffer",
63
ctrl_backspace: "undo-for-safari-which-stupidly-enough-blocks-ctrl-z"
66
// Press tab on the next line and the wrong indentation will be fixed.
67
var regex = /foo|bar/i;
70
// Local variables get a different colour than global ones.
76
/* Some example CSS */
78
@import url("something.css");
83
font-family: tahoma, arial, sans-serif;
89
text-decoration: none !important;
96
h1:before, h2:before {
101
font-family: courier, monospace;
110
The PHP code below contains some deliberate errors. Play with the editor by fixing them
111
and observing how the highlight changes.
118
namespace A::B::C::D x;
119
self::range($row['lft'], $row['rgt'])); // error: extra ')'
120
$a = (b() + 4) 5 foo; // error: missing operators
122
$parent = self::range($max + 1, $max + 1);
123
$row[attributes][$attribute_name] = $attribute_value;
124
$row[attributes()][$attribute_name] = $attribute_value;
125
$row[attributes(5)][$attribute_name] = $attribute_value;
126
$row[$attributes()][$attribute_name] = $attribute_value;
127
abstract class 5 extends foo implements echo {
128
private function domainObjectBuilder() {
129
return $this->use_domain_object_builder
130
? $this->domain()->objectBuilder()
134
const $myconst = 'some string';
136
// this is a single-line C++-style comment
137
# this is a single-line shell-style comment
138
private var $a = __FILE__;
139
protected static $b = timezone_transitions_get('some parameter here');
140
global $g = isset("string");
141
static $s = hash_update_file; // warning: predefined function non-call
142
function mike ($var) $foo;
143
mike(A::func(param));
144
func($b $c); // error: function parameters must be comma-separated
145
foo bar; // error: no operator
146
$baz $quux; // error: no operator
147
public abstract function loadPageXML(util_FilePath $filename, $merge=0+$foo, $x, $y=3) {
148
$newrow[$key] = $val;
150
$state = $row['c'] == 1;
151
$attribute_values[$attribute_name] = null;
152
$row['attributes'][$attribute_name] = $attribute_value;
153
$result[$row['element']][$row['attribute']] = $row['value'];
154
$sql = "multiline string
155
line2 is special - it'll interpolate variables like $state and method calls
156
{$this->cache->add($key, 5)} and maybe \"more\"
159
$sql = 'multiline string
160
single quoting means no \'interpolation\' like "$start" or method call
161
{$this->cache->add($key, 5)} will happen
164
$bitpattern = 1 << 2;
166
$incorrect = <<< 5 EOSTRING // FIXME: CodeMirror update bug: add a letter before 5 and notice that syntax is not updated until EOF, even with continuousScanning: 500
167
error: the identifier must conform to the identifier rules
170
SELECT attribute, element, value
171
FROM attribute_values
174
$this->lr_cache->add($key, self::range($row['lft'], $row['rgt']));
175
$composite_string = <<<EOSTRING
179
$page_lft = ($domain->name() == 'page') ? $start + 1 : $page_start + 1;
180
echo "This is class foo";
181
echo "a = ".$this ->a[2+3*$array["foo"]]."";
182
echo "b = {$this->b}"; // FIXME: highlight interpolation in strings
184
final function makecoffee error($types = array("cappuccino"), $coffeeMaker = NULL) {
185
$out_of_way_amount = $max - $child->left() + 1;
186
$absolute_pos = $child->left() - $move->width();
188
$varfunc(1, 'x') + foo() - 5;
189
$funcarray[$i]('param1', $param2);
190
$lr[$domain_name] = $this->get_left_and_right($domain,
193
$domain_list = is_null($domain) ?
195
array($domain->name());
196
foreach (r3_Domain::names() as $domain_name) {
197
$placeholders = 'distance LIKE '
198
. implode(array_fill(1, $num_distances, '?'),
199
' OR distance LIKE ');
202
return $this->target*$this->trans+myfunc(__METHOD__);
204
echo 'This is a test'; /* This comment will cause a problem */
208
case "r3core_AddTemplateToTargetEvent":
209
$this->notifyAddTemplateToTarget( $genevent );
211
case "r3core_GenerateTargetEvent" $this:
212
for($i=0; $i<=this->method(); $i++) {
213
echo 'Syntax "highlighting"';
216
foreach($array xor $loader->parse_fn($filename) as $key => value) {
219
} catch( Exception $e ) {
220
/** restore the backup
222
$this->loadAll($tmp, $event, true);
223
// `php -l` doesn't complain at all at this (it assumes string constants):
224
this + makes * no - sense;
230
throw new r3_util_Exception( get_class( $genevent ) . " does not map" );
239
php("works", $here, 2);
250
<h1>This is an <?php # echo 'simple';?> example.</h1>
251
<p>The header above will say 'This is an example'.</p>
252
<h1>This is an <?php // echo 'simple';?> example.</h1>
263
span.test {font-family: arial, 'lucida console', sans-serif}
278
<script type="text/javascript">
279
var editor = CodeMirror.fromTextArea('code', {
281
parserfile: ["parsexml.js", "parsecss.js", "tokenizejavascript.js", "parsejavascript.js",
282
"../contrib/php/js/tokenizephp.js", "../contrib/php/js/parsephp.js",
283
"../contrib/php/js/parsephphtmlmixed.js"],
284
stylesheet: ["../../css/xmlcolors.css", "../../css/jscolors.css", "../../css/csscolors.css", "css/phpcolors.css"],
286
continuousScanning: 500