Skip to content

Commit

Permalink
while parsing scripts in <head> can add new scripts to <head> and the…
Browse files Browse the repository at this point in the history
…y will execute. Tested in new directory.... use "ant fulldoc-spec" since its so krazy
  • Loading branch information
client9 committed May 2, 2010
1 parent 5483f32 commit e6c93e6
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 23 deletions.
23 changes: 23 additions & 0 deletions specs/fulldoc/boot.js
@@ -0,0 +1,23 @@

/**
* @author thatcher
*/
var myprint = print;
load('dist/env.rhino.js');


Envjs({
scriptTypes: { "text/javascript": true },
});
window.location = 'specs/fulldoc/index.html';

var div = window.document.getElementById('qunit-testresult');
print("DIV = " + div);
var spans = div.getElementsByTagName('SPAN')

var summary = {};
for (var i = 0; i < spans.length; ++i) {
var clazz = spans[i].getAttribute('class');
summary[clazz] = parseInt(spans[i].textContent);
print(clazz + ' = ' + summary[clazz]);
}
41 changes: 41 additions & 0 deletions specs/fulldoc/index.html
@@ -0,0 +1,41 @@
<?xml encoding='utf-8'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="eng" dir="ltr">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<title>Envjs Parser Spec</title>

<link rel="stylesheet"
media="screen"
href="../qunit.css"/>

<script src="../qunit.js"
type="text/javascript" ></script>

<script src="spec.js"
type="text/javascript" ></script>


</head>
<body id="body">
<h1 id="qunit-header">
<img src="../fixtures/images/icon-green.png"/>
<span>Envjs Parser Spec</span>
</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<div id='qunit-testrunner-toolbar'></div>
<div id='qunit-test-summary'>
<ol id='qunit-tests'></ol>
</div>
<div id="qunit-main" style="display:none;">
<!--
Test HTML here:
not required to be in here but it makes it convenient
by convention to avoid having to look at the html
artifacts the test may require or have generated as
a side effect.
-->
</div>
</body>
</html>
46 changes: 46 additions & 0 deletions specs/fulldoc/spec.js
@@ -0,0 +1,46 @@
QUnit.module('integration');

/**
* This is very different from the other tests in that the "work"
* in done, //outside// of a test(). The test() just check the
* final results. This is needed since the tests must run in
* <head> tag of the main document.
*/

var isenvjs;
try {
isenvjs = runningUnderEnvjs();
} catch (e) {
isenvjs= false;
}
/**
* If a script in running in <head>, then document.body === null
*
* Due to frame scoping rules, we have indirectly make the test.
* What we are really doing is this:
* <html><head><script>
* ok(document.body === null);
* </script><head><body></body></html>
*/
document.bodyinhead = document.body;

/**
* in <head>
* Create a new <script> element attached to <head>
* It should be run immediately(?)
*
*/
var s = document.createElement('script');
s.type = "text/javascript";
s.text = "document.fired = true;";
document.documentElement.getElementsByTagName("head")[0].appendChild(s)

test('document.body is null in head', function() {
expect(1);
ok(document.bodyinhead === null, 'doc.body === null');
});

test('added new script to head from head', function() {
expect(1);
ok(document.fired === true, 'appended script element ran');
});
54 changes: 31 additions & 23 deletions src/html/document.js
Expand Up @@ -234,27 +234,23 @@ __extend__(HTMLDocument.prototype, {
title.textContent = titleStr;
},

get body(){
//console.log('get body');
if (!this.documentElement) {
this.appendChild(this.createElement('html'));
}
var body,
element = this.documentElement,
length = element.childNodes.length,
i;
//check for the presence of the head element in this html doc
for(i=0;i<length;i++){
if(element.childNodes[i].nodeType === Node.ELEMENT_NODE){
if(element.childNodes[i].tagName.toLowerCase() === 'body'){
return element.childNodes[i];
}
get body() {
var element = this.documentElement,
length = element.childNodes.length,
i;
for (i=0; i<length; i++) {
if (element.childNodes[i].nodeType === Node.ELEMENT_NODE &&
element.childNodes[i].tagName === 'BODY') {
return element.childNodes[i];
}
}
//no head? ugh bad news html.. I guess we'll force the issue?
return element.appendChild(this.createElement('body'));
return null;
},
set body(){console.log('set body');/**in firefox this is a benevolent do nothing*/},
set body() {
/* in firefox this is a benevolent do nothing*/
console.log('set body');
},

get cookie(){
return Envjs.getCookies(this.location+'');
},
Expand Down Expand Up @@ -391,9 +387,22 @@ Aspect.around({
// node.namespaceURI, node.nodeName, node);
switch(doc.parsing){
case true:
//handled by parser if included
//console.log('html document in parse mode');
break;

/**
* Very special case. While in parsing mode, in head, a
* script can add another script tag to the head, and it will
* be evaluated. This is tested in 'ant fulldoc-spec' tests.
*
* Not quite sure if the require that the new script tag must
* be in the head is correct or not. NamespaceURI == null
* might also need to corrected too.
*/
if (node.namespaceURI === null &&
node.tagName === 'SCRIPT' &&
this.tagName == 'HEAD') {
okay = Envjs.loadLocalScript(node, null);
}
break;
case false:
switch(node.namespaceURI){
case null:
Expand Down Expand Up @@ -459,7 +468,7 @@ Aspect.around({
console.log('error loading html element %s %e', node, e.toString());
}
break;

case 'link':
if (node.href && node.href.length > 0) {
__loadLink__(node, node.href);
Expand Down Expand Up @@ -627,4 +636,3 @@ var __removeNamedMap__ = function(target, node) {
delete target[nodename];
}
};

0 comments on commit e6c93e6

Please sign in to comment.