Skip to content

Commit

Permalink
Implemented 'iframe.src = "new-url"' causing an actual document load.
Browse files Browse the repository at this point in the history
Added matching tests, broke (i)frame-specific tests into a separate
file from the generic-window tests.
  • Loading branch information
gleneivey committed Jul 13, 2009
1 parent 110e978 commit a1746da
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 16 deletions.
20 changes: 20 additions & 0 deletions dist/env.js
Expand Up @@ -5890,9 +5890,29 @@ __extend__(HTMLFrameElement.prototype, {
},
set src(value){
this.setAttribute('src', value);

if (value.length > 0){
if (!this._content){
var frameWindow = {};
try {
_$envjs$makeObjectIntoWindow$_(frameWindow, $env);
frameWindow.location = value;
this._content = frameWindow;
} catch(e){
$error("failed to load frame content: from " + value, e);
}
}

this._content.location = value;
}
},
get contentDocument(){
if (!this._content)
return null;
return this._content.document;
},
get contentWindow(){
return this._content;
}
});

Expand Down
24 changes: 22 additions & 2 deletions dist/env.rhino.js
Expand Up @@ -36,9 +36,9 @@ var Envjs = function(){
$env.error = function(){};

//uncomment these if you want to get some internal log statementes
/**/$env.debug = function(msg){
/*$env.debug = function(msg){
$env.log(msg,"DEBUG");
};
};*/
$env.info = function(msg){
$env.log(msg,"INFO");
};
Expand Down Expand Up @@ -6327,9 +6327,29 @@ __extend__(HTMLFrameElement.prototype, {
},
set src(value){
this.setAttribute('src', value);

if (value.length > 0){
if (!this._content){
var frameWindow = {};
try {
_$envjs$makeObjectIntoWindow$_(frameWindow, $env);
frameWindow.location = value;
this._content = frameWindow;
} catch(e){
$error("failed to load frame content: from " + value, e);
}
}

this._content.location = value;
}
},
get contentDocument(){
if (!this._content)
return null;
return this._content.document;
},
get contentWindow(){
return this._content;
}
});

Expand Down
20 changes: 20 additions & 0 deletions src/html/frame.js
Expand Up @@ -55,9 +55,29 @@ __extend__(HTMLFrameElement.prototype, {
},
set src(value){
this.setAttribute('src', value);

if (value.length > 0){
if (!this._content){
var frameWindow = {};
try {
_$envjs$makeObjectIntoWindow$_(frameWindow, $env);
frameWindow.location = value;
this._content = frameWindow;
} catch(e){
$error("failed to load frame content: from " + value, e);
}
}

this._content.location = value;
}
},
get contentDocument(){
if (!this._content)
return null;
return this._content.document;
},
get contentWindow(){
return this._content;
}
});

Expand Down
13 changes: 13 additions & 0 deletions test/html/iframe2.html
@@ -0,0 +1,13 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en" dir="ltr" id="html">
<head>
<title>Content document for IFRAME loading in env.js unit test suite</title>
</head>
<body>
<p id="aParaInAnIframe">
Here is a short paragraph.
</p>
</body>
</html>
1 change: 1 addition & 0 deletions test/test.js
Expand Up @@ -26,6 +26,7 @@ window.onload = function(){
load(
"test/unit/dom.js",
"test/unit/window.js",
"test/unit/frame.js",
"test/unit/parser.js",
"test/unit/timer.js",
//NOTE: keep this test last because Prototype pollutes
Expand Down
50 changes: 50 additions & 0 deletions test/unit/frame.js
@@ -0,0 +1,50 @@
/*
* This file is a component of env.js,
* http://github.com/gleneivey/env-js/commits/master/README
* a Pure JavaScript Browser Environment
* Copyright 2009 John Resig, licensed under the MIT License
* http://www.opensource.org/licenses/mit-license.php
*/


module("frame");


// all tests to next comment rely on content of ../html/iframe.html
test("IFRAMEs load with accessible content", function() {
expect(3);

var iframe = document.getElementById('loadediframe');
try{ok (iframe.src == "html/iframe.html",
"Initial iframe src matches test page source");
}catch(e){print(e);}

var idoc = iframe.contentDocument;
var mtch = idoc.title.match(/IFRAME/);
try{ok (mtch && mtch.length > 0,
"Can get 'document' object from test iframe");
}catch(e){print(e);}

var para = idoc.getElementById('anElementWithText');
mtch = para.innerHTML.match(/content of a paragraph/);
try{ok (mtch && mtch.length > 0,
"Can get text from element in an iframe");
}catch(e){print(e);}
});

// all tests to next comment rely on content of ../html/iframe2.html
test("IFRAMEs reload on assignment to 'src'", function() {
expect(2);

var iframe = document.getElementById('loadediframe');
iframe.src = "html/iframe2.html";
try{ok (iframe.src == "html/iframe2.html",
"iframe.src matches value assigned");
}catch(e){print(e);}

var para = iframe.contentDocument.getElementById('aParaInAnIframe');
mtch = para.innerHTML.match(/short paragraph/);
try{ok (mtch && mtch.length > 0,
"IFRAME reloaded from correct source");
}catch(e){print(e);}
});
21 changes: 7 additions & 14 deletions test/unit/window.js
@@ -1,7 +1,7 @@
module("window");

test("Window Global Scope Equivalence", function() {
expect(5);
expect(7);

window.foo = "abc";
ok( window.foo == foo, "Property created on the window is available in global scope." );
Expand All @@ -17,17 +17,10 @@ test("Window Global Scope Equivalence", function() {
ok( $$$$$ === "12345", "Property is in global scope." );
ok( window.$$$$$ === "12345", "Property is in window scope." );

try{ ok(window.Math === Math,
"'window' object provides common global object facilities");
}catch(e){print(e);}
try{ ok(Math.sqrt(4) == 2,
"'window' provides Math.* when referenced implicitly/global");
}catch(e){print(e);}
});


test("Window scope in iframe isolated", function() {
expect(1);

// test cases here rely on JS in ../html/iframe.html
var idoc = document.getElementById('loadediframe').contentDocument;
var mtch = idoc.title.match(/IFRAME/);
try{ok (mtch && mtch.length > 0,
"Can get 'document' object from test iframe");
}catch(e){print(e);}
});

0 comments on commit a1746da

Please sign in to comment.