Personal tools

Attribute Type Coercion

From OpenLaszlo

Contents

Introduction

A way to convert attribute values to/from representations (as String).

Proposal

Overview

Introduce `Presentation Type`s that specify how to convert a corresponding language type to/from a string.

Changes

 //---
 // PresentationType
 //
 // A presentation type is a specification of how to map a Javascript type to a String
 // representation
 //---
 class PresentationType {
   function accept(value) { return value; }
   function present(value) { return "" + value; }
 }

2 new methods on node, acceptAttribute and presentAttribute

 //---
 // acceptAttribute
 // @param name:String the name of the attribute
 // @param type:PresentationType
 // @param value:String the string representation of the desired value
 //
 // Sets the named attribute to the result of converting the value using the
 // type's accept method
 //---
 function acceptAttribute(name, type, value) {
   this.setAttribute(name, type.accept(value));
 }
 
 //---
 // presentAttribute
 // @param name:String the name of the attribute
 // @param type:PresentationType
 // @returns value:String
 //
 // Returns the value of the named attribute converted to a string byt
 // type's present method
 //---
 function presentAttribute(name, type) {
   return type.present(this.getAttribute(name));
 }


Define a mapping table from the space of types for attributes to PresentationTypes.

Finally, for $path and $style constraints, the compiler will emit a constraint function that uses `acceptAttribute` to bind attributes, rather than setAttribute, according to the mapping table.

Examples

 //---
 // Example presentation type
 //---
 class BooleanPresentationType extends PresentationType {
   function accept(value) {
     switch (value.toLowerCase()) {
       case "0": case "false":
         return false;
       default:
         return true;
     }
   }
 
   function present(value) {
     if (value) {
       return "true";
     } else {
       return "false";
   }
 }

(And so on, for other types.)

History