From 2b4c86e2bdd4eac88b5eb8a71c0b302843e032fb Mon Sep 17 00:00:00 2001 From: thatcher Date: Wed, 17 Mar 2010 01:41:25 -0400 Subject: [PATCH] thanks to nickg, added the Image Html interface --- build.properties | 2 +- src/html/img.js | 24 ++++++++++++++++++-- test/specs/html/spec.js | 50 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/build.properties b/build.properties index fe0039f7..6ca25cc4 100644 --- a/build.properties +++ b/build.properties @@ -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} diff --git a/src/html/img.js b/src/html/img.js index 4aab518b..769ba2c9 100644 --- a/src/html/img.js +++ b/src/html/img.js @@ -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); @@ -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); @@ -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; + diff --git a/test/specs/html/spec.js b/test/specs/html/spec.js index 80b3b584..8bf169e4 100644 --- a/test/specs/html/spec.js +++ b/test/specs/html/spec.js @@ -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'); @@ -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 @@ -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(); +});*/