[KSS-checkins] r50322 - in kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes: . kukit

reebalazs at codespeak.net reebalazs at codespeak.net
Fri Jan 4 17:57:36 CET 2008


Author: reebalazs
Date: Fri Jan  4 17:57:35 2008
New Revision: 50322

Modified:
   kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/   (props changed)
   kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/kukit/dom.js
   kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/kukit/providerreg.js
Log:
Refactor the recursive attribute lookup.
Add getKssValue provider.

Modified: kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/kukit/dom.js
==============================================================================
--- kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/kukit/dom.js	(original)
+++ kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/kukit/dom.js	Fri Jan  4 17:57:35 2008
@@ -227,27 +227,57 @@
 
 var _kssAttrNamespace = 'kssattr';
 
+// the namespace prefix for kss values, 
+// i.e.:
+//              class="... kss-attr-key-value..."
+//              id=="kss-id-key-value"
+// (XHTML:)     kss-attr:key-value 
+//
+var _kssNamespacePrefix = 'kss';
+
+var _getKssValueFromEncodings = function(encodings, prefix) {
+    // Value us a list of values.
+    // If a value equals prefix-value, it will find it
+    // and return the value after the prefix and the dash.
+    // (First value found will be returned.)
+    //
+    // For example:
+    //
+    //     _getKssValueFromEncodings(['kss-attr-key1-value1', 'kss-attr-key2-value2',
+    //          'kss-id-key1-value1'], "kss-attr-key1')
+    //
+    // results 'value1'.
+    //
+    // Legacy example:
+    //
+    //    _getKssValueFromEncodings(['kssattr-key1-value1', 'kssatt-rkey2-value2'], 
+    //          "kss-attr-key1')
+    //
+    //  results 'value1'.
+    //
+    prefix = prefix + '-';
+    var prefixLength = prefix.length;
+    for (var i=0; i<encodings.length; i++) {
+        var encoding = encodings[i];
+        // Does the value start with the prefix?
+        if (encoding.substr(0, prefixLength) == prefix) {
+            // Found it.
+            return encoding.substr(prefixLength);
+        }
+    }
+    return null;
+};
+
+// BBB hint: used by getKssAttribute only, for providers
+// kssAttr and  currentFormVarForKssAttr.
 var _getKssClassAttribute = function(node, attrname) {
     // Gets a given kss attribute from the class
     var klass = dom.getAttribute(node, 'class');
-    var result = null;
     if (klass) {
         var splitclass = klass.split(/ +/);
-        for (var i=0; i<splitclass.length; i++) {
-            var elem = splitclass[i];
-            var splitelem = elem.split('-', 3);
-            if (splitelem.length == 3 &&
-                splitelem[0] == _kssAttrNamespace
-                    && splitelem[1] == attrname) {
-                // Found it. (The last one will be valid,
-                // in case of duplication)
-                var index = splitelem[0].length + splitelem[1].length + 2;
-                result = elem.substr(index);
-            }
-
-        }
+        return _getKssValueFromEncodings(splitclass, 'kssattr-' + attrname);
     }
-    return result;
+    return null;
 };
 
 dom.getKssAttribute = function(node, attrname) {
@@ -271,15 +301,66 @@
     dom.setAttribute(node, fullName);
 };
 
+/* 
+ * Handling of kss values
+ */
+
+dom.getKssValue = function(node, keyType, key) {
+    // Gets a given kss value
+    // first try from the namespace (XHTML), then from the class and id
+    var namespacedName = _kssNamespacePrefix + '-' + keyType;
+    // We access node.getAttribute directly, because we don't need the
+    // other checks in dom.getAttribute
+    var attrName = namespacedName + ':' + key;
+    var result = node.getAttribute(attrName);
+    // XXX if this was '' it is the same as notfound,
+    // so it shadows the class attribute!
+    // This means setting an attribute to '' is the same as deleting it - 
+    // at least at the moment
+    if (! result) {
+        // Now try to get it from the class and id encodings.
+        // Having it in the id gives the advantage that we can use
+        // kss-id-key-value both as a unique html id, and widget markup.
+        var klass = dom.getAttribute(node, 'class');
+        var encodings;
+        if (klass) {
+            encodings = klass.split(/ +/);
+        } else {
+            encodings = [];
+        }
+        var id = dom.getAttribute(node, 'id');
+        if (id) {
+            // We have an id, consider it too
+            // id will be inserted 1st, ie. it overrides possible doubles in classes
+            encodings.unshift(id);
+        }
+        // Get the result-
+        var prefix = namespacedName + '-' + key;
+        return _getKssValueFromEncodings(encodings, prefix);
+    } else {
+        return null;
+    }
+};
+
+dom.setKssValue = function(node, keyType, key, value) {
+    // Sets a given kss attribute on the namespace
+    var namespacedName = _kssNamespacePrefix + '-' + keyType;
+    // We access node.setAttribute directly, because we don't need the
+    // other checks in dom.setAttribute
+    var attrName = namespacedName + ':' + key;
+    node.setAttribute(attrName, value);
+};
+
+
 /* Recursive query of node attributes
    getter is a function that gets the value from the node.
 */
 
-dom.getRecursiveAttribute =
-    function(node, attrname, recurseParents, getter) {
-    var value = getter(node, attrname);
+dom.locateMarkup =
+    function(node, recurseParents, getter, p1, p2, p3, p4, p5) {
+    var value = getter(node, p1, p2, p3, p4, p5);
+    var element = node;
     if (recurseParents) {
-        var element = node;
         // need to recurse even if value="" !
         // We cannot figure out if there exists
         // and attribute in a crossbrowser way, or it is set to "".
@@ -288,16 +369,24 @@
             if (! element || ! element.getAttribute) {
                 break;
             }
-            value = getter(element, attrname);
+            value = getter(element, p1, p2, p3, p4, p5);
         }
     } 
     if (typeof(value) == 'undefined') {
         // notfound arguments will get null
         value = null;
     }
-    return value;
+    // We return both the value and the node where
+    // it was found.
+    return {value:value, node:element};
 };
 
+dom.getRecursiveAttribute =
+    function(node, attrname, recurseParents, getter) {
+    return dom.locateMarkup(node,
+            recurseParents, getter, attrname).value;
+}
+
 /*
 *  class EmbeddedContentLoadedScheduler
 *

Modified: kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/kukit/providerreg.js
==============================================================================
--- kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/kukit/providerreg.js	(original)
+++ kukit/kukit.js/branch/ree-1.4-markup-and-syntax-changes/kukit/providerreg.js	Fri Jan  4 17:57:35 2008
@@ -284,3 +284,39 @@
 };
 kukit.pprovidersGlobalRegistry.register('pass', kukit.pr.PassPP);
 
+kukit.pr.KssValuePP = function() {};
+kukit.pr.KssValuePP.prototype = {
+    check: function(args) {
+;;;     if (args.length != 1 && args.length != 2) {
+;;;         kukit.E = 'kssValue provider needs 2 or 3 argument (keytype, key,';
+;;;         kukit.E += ' [recurseParents]).';
+;;;         throw new Error(kukit.E);
+;;;     }
+    },
+    eval: function(args, node) {
+        var keytype =  args[0];
+        var key =  args[1];
+        // recurseParents is by default true
+        var recurseParents = true;
+;;;     if (keytype.match(/[ -]/)) {
+;;;         kukit.E = 'keytype parameter in kssValue provider cannot contain';
+;;;         kukit.E += ' dashes or spaces.';
+;;;         throw new Error(kukit.E);
+;;;     }
+;;;     if (key.match(/[ -]/)) {
+;;;         kukit.E = 'key parameter in kssValue provider cannot contain';
+;;;         kukit.E += ' dashes or spaces.';
+;;;         throw new Error(kukit.E);
+;;;     }
+        if (args.length == 3) {
+            recurseParents = args[2];
+;;;         kukit.E = '3rd attribute of kssAttr must be a boolean.';
+            kukit.ut.evalBool(recurseParents, kukit.E);
+        }
+        return kukit.dom.getRecursiveAttribute(node, argname, recurseParents,
+            kukit.dom.getKssValue);
+    }
+};
+kukit.pprovidersGlobalRegistry.register('kssValue', kukit.pr.KssValuePP);
+
+


More information about the Kukit-checkins mailing list