Skip to content

Commit

Permalink
thanks to nickg, added the Image Html interface
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcher committed Mar 17, 2010
1 parent 80ad06d commit 2b4c86e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.properties
Expand Up @@ -2,7 +2,7 @@
PROJECT: env-js
BUILD_MAJOR: 1
BUILD_MINOR: 2
BUILD_ID: 0.9
BUILD_ID: 0.10
BUILD_VERSION: ${BUILD_MAJOR}.${BUILD_MINOR}.${BUILD_ID}
BUILD: ${PROJECT}.${BUILD_VERSION}
VERSION: ${BUILD} ${DSTAMP}
Expand Down
24 changes: 22 additions & 2 deletions src/html/img.js
Expand Up @@ -14,7 +14,7 @@ __extend__(HTMLImageElement.prototype, {
this.setAttribute('alt', value);
},
get height(){
return this.getAttribute('height');
return parseInt(this.getAttribute('height')) || 0;
},
set height(value){
this.setAttribute('height', value);
Expand Down Expand Up @@ -48,7 +48,7 @@ __extend__(HTMLImageElement.prototype, {
this.dispatchEvent( event, false );
},
get width(){
return this.getAttribute('width');
return parseInt(this.getAttribute('width')) || 0;
},
set width(value){
this.setAttribute('width', value);
Expand All @@ -57,3 +57,23 @@ __extend__(HTMLImageElement.prototype, {
__eval__(this.getAttribute('onload')||'', this)
}
});


/*
* html5 4.8.1
* http://dev.w3.org/html5/spec/Overview.html#the-img-element
*/
Image = function(width, height) {
// Not sure if "[global].document" satifies this requirement:
// "The element's document must be the active document of the
// browsing context of the Window object on which the interface
// object of the invoked constructor is found."

HTMLElement.apply(this, [document]);
// Note: firefox will throw an error if the width/height
// is not an integer. Safari just converts to 0 on error.
this.width = parseInt(width) || 0;
this.height = parseInt(height) || 0;
};
Image.prototype = new HTMLImageElement;

50 changes: 49 additions & 1 deletion test/specs/html/spec.js
Expand Up @@ -2,7 +2,7 @@ module('html');

test('HTML Interfaces Available', function(){

expect(40);
expect(41);
ok(HTMLDocument, 'HTMLDocument defined');
ok(HTMLElement, 'HTMLElement defined');
ok(HTMLCollection, 'HTMLCollection defined');
Expand Down Expand Up @@ -43,6 +43,14 @@ test('HTML Interfaces Available', function(){
ok(HTMLTextAreaElement, 'HTMLTextAreaElement defined');
ok(HTMLTitleElement, 'HTMLTitleElement defined');
ok(HTMLUnknownElement, 'HTMLUnknownElement defined');

// Image has a constructor, that implements the HTMLImageElement interface
// http://dev.w3.org/html5/spec/Overview.html#the-img-element
ok(Image, 'Image defined');

// Option has a constructor and implements the HTMLOptionElement interface
// http://dev.w3.org/html5/spec/Overview.html#the-option-element
//ok(Option, 'Option defined');
});

// mock the global document object if not available
Expand Down Expand Up @@ -323,4 +331,44 @@ test('HTMLDocument.createElement(script)', function(){
// TODO: forms, input radio
//http://envjs.lighthouseapp.com/projects/21590/tickets/91-radio-button-value-attribute-output-as-defaultvalue-in-html

/* Image and Option below are unique in the DOM in that they
* have defined constructors, and have implied
* owner documents.
*/
test("Image", function() {
var x = new Image()
// determined experimentally
equals(x.width, 0, 'default width is 0');
equals(x.height, 0, 'default height is 0');

x = new Image(1);
equals(x.width, 1, 'width');
equals(x.height, 0, 'default height is 0');

x = new Image(1,9);
equals(x.width, 1, 'width');
equals(x.height, 9, 'height');

// numbers as strings ok
x = new Image("1","9");
equals(x.width, 1, 'width');
equals(x.height, 9, 'height');

// make sure attributes are being set.

equals(x.getAttribute('width'), 1, 'width from attributes');
equals(x.getAttribute('height'), 9, 'height from attributes');

// make sure we are getting back true numbers and not strings
equals(typeof(x.width), 'number', 'width is a number');
equals(typeof(x.height), 'number', 'height is a number');

// and setting bogus values
x.setAttribute('width', 'foo');
equals(x.width, 0, 'bad width default to 0');
});


/*test("Option", function() {
var x = new Option();
});*/

0 comments on commit 2b4c86e

Please sign in to comment.