SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2004
    Location
    Belgrade, Serbia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up X library improvement: CSS property name to DOM name, using RegExp

    I have used Mike Foster's X lib pretty much, and I'm glad that I have something to contribute.

    In the last version, I've seen that Mike is asking (in x_dom.js):

    // convert css property name to object property name for IE (can this be done with RE?)

    It can. I needed this switch from CSS to DOM name a while ago, and asked my friend (http://www.vesic.org), regular RegExp guru, to do it for me.
    Here it is:

    function xGetDOMPropName(sP) {
    var sProperty = sP.replace(new RegExp ('-', 'gi'),' ');
    sProperty = sProperty.replace(/\b(\w)/g, function(whole, par1) { var i=0;i++; if(i == 1) return par1; return par1.toUpperCase(); });
    sProperty = sProperty.replace(new RegExp (' ', 'gi'),'');
    return sProperty = sProperty.charAt(0).toLowerCase() + sProperty.substring(1);
    }

    Mike, if you like it, include it in the library.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi aleck, Welcome to SitePoint Forums!

    and Thanks very much for answering my question about how to do that with an RE.

    Here's the current function in question:

    Code:
    function xGetComputedStyle(oEle, sProp)
    {
      var p = 0;
      if(document.defaultView && document.defaultView.getComputedStyle){
        p = document.defaultView.getComputedStyle(oEle,'').getPropertyValue(sProp)
      }
      else if(oEle.currentStyle) {
        // convert css property name to object property name for IE
        var a = sProp.split('-');
        sProp = a[0];
        for (var i=1; i<a.length; ++i) {
          c = a[i].charAt(0);
          sProp += a[i].replace(c, c.toUpperCase());
        }   
        p = oEle.currentStyle[sProp];
      }
      return parseInt(p) || 0;
    }
    I had hoped that a RE solution would be shorter (less code) and perhaps more efficient at runtime - but now I'm not sure. I think the current function is just as efficient, if not more. What do you think? Is there a way I could make the current function more efficient?

    btw, I'm going to save your code and learn from it - I'm not too good with REs

  3. #3
    SitePoint Member
    Join Date
    Mar 2004
    Location
    Belgrade, Serbia
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster
    I had hoped that a RE solution would be shorter (less code) and perhaps more efficient at runtime - but now I'm not sure. I think the current function is just as efficient, if not more. What do you think? Is there a way I could make the current function more efficient?
    To be honest, I have no idea. I needed the same thing before you come up with it, and wrote virtually the same function. I was under the impression that it would be faster with REs so I asked a friend to write it for me.
    He said that it was quite a challenge.
    Never really measured which one is faster.

    Quote Originally Posted by MikeFoster
    btw, I'm going to save your code and learn from it - I'm not too good with REs
    Not mine, a friend's. REs are the weakest part of my knowledge, something I just can`t grasp.

    Maybe when some lazy weeks arrive...

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks aleck,

    Have a great day!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •