Created
December 7, 2009 15:32
-
-
Save brettz9/250870 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | |
<head> | |
<title>Test replacing PHP.JS blocks</title> | |
<script type="text/javascript"> | |
<!--//--><![CDATA[//><!-- | |
// This solution had been posted to http://phpjs.org earlier, but not archived there or at archive.org; only found record at http://tubeurl.com/spider/phpjs.org | |
// Fix: | |
// 1) Get echo() to support output buffering and implement below | |
// 2) If no procInst in HTML, then use comments--e.g., IE-style? | |
// 3) Move away from XPath in HTML to DOM traversal? | |
// To work with echo(), we need a different implementation of echo() which returns the string (and doesn't append to the DOM, since we want to append it) | |
function echo (str) { | |
return str; | |
} | |
// Alternatively to echo(), or in addition, we could just define our own underscore (_) function which does this, thus not interfering with PHP | |
function _ (str) { | |
return str; | |
} | |
function populatePHPJSBlocks () { | |
var getXPathNodes = function (xpathText) { | |
if (document.evaluate) { | |
var procInsts = []; | |
var pis = document.evaluate(xpathText, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); | |
var pi = pis.iterateNext(); | |
while(pi) { | |
procInsts.push(pi); | |
pi = pis.iterateNext(); | |
} | |
return procInsts; | |
} | |
else { | |
try { | |
return document.documentElement.selectNodes(xpathText); | |
} | |
catch(e) { | |
throw "XPath is not supported by this browser."; | |
} | |
} | |
}; | |
var procInsts = []; | |
var procInsts = getXPathNodes('//processing-instruction("phpjs")'); | |
// Didn't work in loop below for some reason, so doing feature testing here (more efficient anyways) | |
if (window.XMLList) { | |
// We use eval() here to avoid problems with non-E4X browsers | |
eval("default xml namespace = new Namespace('http://www.w3.org/1999/xhtml');"); | |
} | |
for (var i=0; i < procInsts.length; i++) { | |
var evald = eval(procInsts[i].data); | |
var node; | |
if (typeof evald === 'xml') { | |
node = new DOMParser().parseFromString(evald.toXMLString(), 'text/xml').documentElement; | |
} | |
else if (evald instanceof Node) { | |
node = evald; | |
} | |
else { | |
node = document.createTextNode(evald); | |
} | |
procInsts[i].parentNode.replaceChild(node, procInsts[i]); | |
} | |
var procInsts = []; | |
var procInsts = getXPathNodes('//processing-instruction("_")'); | |
for (var i=0; i < procInsts.length; i++) { | |
var evald = eval(procInsts[i].data); | |
if (typeof evald === 'xml') { | |
node = new DOMParser().parseFromString(evald.toXMLString(), 'text/xml').documentElement; | |
} | |
else if (evald instanceof Node) { | |
node = evald; | |
} | |
else { | |
node = document.createTextNode(evald); | |
} | |
procInsts[i].parentNode.replaceChild(node, procInsts[i]); | |
} | |
} | |
window.addEventListener('load', function () { | |
populatePHPJSBlocks(); | |
}, false); | |
//--><!]]></script> | |
</head> | |
<body> | |
<p><?_ 'He'+'llo' ?></p> | |
<p>Static text in between "Hello" and "world!"</p> | |
<p><?phpjs _('world!'); ?></p> | |
<p><?phpjs echo('Finished!'); ?></p> | |
<!-- Comment out the following for Opera or Safari as they will only work in an E4X-compliant browser like Mozilla/Firefox --> | |
<p><?phpjs <em>Really finished!</em> ?></p> | |
<p><?_ <strong>Really, really finished at {new Date()}!</strong> ?></p> | |
<p><?_ var tt = document.createElementNS('http://www.w3.org/1999/xhtml', 'tt'); tt.innerHTML = 'abcdef'; tt; ?></p> | |
<p><?phpjs var tt = document.createElementNS('http://www.w3.org/1999/xhtml', 'tt'); tt.innerHTML = 'ghijkl'; tt; ?></p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could just use HTML now for this (and eval results if using the above JS approach)