www.webdeveloper.com
Results 1 to 4 of 4

Thread: [RESOLVED] character replacement problem

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    51

    resolved [RESOLVED] character replacement problem

    Here's the function:
    Code:
    js.attr = function () {
        var a = arguments;
        var e = a[0];
        var v = a[2];
        var r = null;
        a = a[1].toLowerCase();
        if (js.def.obj(e, "dom") && js.def.txt(a) && a !== "") {
            var s = '<' + e.nodeName.toLowerCase();
            var l = s;
            var c = e.attributes;
            var o = true;
            var n = "", t = "";
            var d = true;
            for (var i = 0;i < c.length;i++) {
                n = c[i].nodeName.toLowerCase();
                if (e.getAttribute(n)) {
                    t = c[i].nodeValue;
                    if (js.def.bol(t)) t = n;
                    l += ' ' + n + '="' + t + '"';
                    if (n === a) {
                        if (js.def.txt(v)) {
                            o = false;
                            if (v !== "") {
                                s += ' ' + n + '="' + v + '"';
                                r = v;
                            }
                        } else if (t !== "") r = t;
                        d = false;
                    } if (t === "") o = false;
                    if (o) s += ' ' + n + '="' + t + '"';
                    o = true;
                }
            } t = '>' + e.innerHTML + '</' + e.nodeName.toLowerCase() + '>';
            if (d && js.def.txt(v) && v !== "") s += ' ' + a + '="' + v + '"';
            s += t; l += t;
            o = e.parentNode;
            v = o.innerHTML;
            d = false;
            t = false;
            n = null;
            for (i = 0;i < v.length;i++) {
                if (d) {
                    if (t) {
                        if (!n) {
                            if (v[i] === '"') {
                                n = '"';
                            } else {
                                n = ' ';
                                v = v.substr(0, i) + '"' + v.substr(i);
                            } i++;
                        } if (v[i] === n) {
                            if (n === ' ') {
                                v = v.substr(0, i) + '"' + v.substr(i);
                                i++;
                            }
                            n = null;
                            t = false;
                        }
                    } else {
                        v[i] = v[i].toLowerCase();
                        if (v[i] === '>') d = false;
                        else if (v[i] === '=') t = true;
                    }
                } if (v[i] === '<') d = true;
            } v.replace(/ [a-z]+\=("")? /, " ");alert(v);
            o.innerHTML = v.replace(l, s);
        } return r;
    };
    My problem is that the tag name/s is not converted to lower case by force as it/they should be. every time I try the function the alert box produces html with upper case tags (this is IE by the way), the reason I'm not using getAttribute and related functions is because of IE's problems. only reason that I'm converting case at all is because not all browsers produce the same case so I want to force the case to a single standard.

    Edit: indecently this is part of a framework I'm building so feel free to use it if you think of a fix.
    Last edited by awsdert; 10-05-2010 at 02:07 PM.

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Code:
    v[i] = v[i].toLowerCase();
    In JS it is not possible to change characters in a string; you must build a new string overwriting the original.

    Code:
    v.replace(/ [a-z]+\=("")? /, " ");
    That has no effect, use

    Code:
    v = v.replace(/ [a-z]+\=("")? /, " ");

  3. #3
    Join Date
    Aug 2010
    Posts
    51
    the v.replace thing i would have spotted on my own but thanks anyway
    as for the non character changing, why is that?

  4. #4
    Join Date
    Aug 2010
    Posts
    51
    I finally fixed it and as far as I can see it is fully cross browser (as long as DOM lvl 1 is supported)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles