Forum

July 12th, 2025
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
More on periods/colons in IDs
22/03/2010
17:23
Avatar
rodk
Member
Members
Forum Posts: 12
Member Since:
19/03/2010
sp_UserOfflineSmall Offline

Using jqGrid 3.6.4

In addition to my previously reported problems with having periods in the ID, simply using a function to escape the periods and colons is not a complete solution.  Many places in the code of jqGrid appear to use the unescaped ID and I would imagine changing this would be a major undertaking.  However, there are a multitude of characters that are technically invalid to be used in HTML element ID attributes, which are limited by the SGML NAME standard (must start with a letter and contain only letters, numbers, hyphens, underscores, colons and dots.  However, from a data standpoint, the ID that the server (i.e. a database schema) uses could contain just about any character.  Obviously these two paradigms are going to create a HUGE problem.  Unless one subscribes to the notion that EVERY db table contain an auto-increment/sequence as the PK (I won't get into debating the merits or lack thereof here).

As I see it, the only two possibilities for fixing this error are:

A) the developer must find someway to encode the ID so that it does not contain any characters which may cause problems.

B) jqGrid must not base HTML element IDs on the ID from the data.

I shall look into option A and report back my progress.  Base64 encoding has SOME possibilities.  Unfortunately, Base64 encoded output contains 3 illegal characters: +, / and = (= used for padding).  Those could be replaced with valid characters, except the only characters allowed for an ID attribute that aren't already part of Base64 encoding are hyphens, underscores, periods and colons.  Since periods and colons are out, we only have two possible values to replace 3 characters.

Option B is beyond my perview.  However, not being familiar with the code (and lacking a strong enough background in JS) I wonder if it might be possible to use generated IDs rather than using the row_id as the basis of creating the element iDs?  Having just started with jqGrid, I'm not sure where this might introduce a problem.  For subgrids, the function defined in subGridRowExpanded() is passed the subgrid_id (the div created by jqGrid to contain the subgrid) as well as the row_id.  Obviously this means the subgrid_id does not need to be based on the row_id.

If someone could point me to where in the code the div ID is determined, I would be happy to test this out by changing the derived ID to a generated ID.

I suppose I could change all of my thousands of existing DB tables to use sequences for PK but besides grating on my sense of what are and aren't DB best practices and the amount of work involved, this doesn't solve the problem for anyone else that may run afoul of this issue.

22/03/2010
18:16
Avatar
rodk
Member
Members
Forum Posts: 12
Member Since:
19/03/2010
sp_UserOfflineSmall Offline

OK, I think I've come up with a solution to Option A.

As mentioned, Base64 includes 3 characters that are NOT valid in element IDs: '+', '\' and '=' and we have only two available characters to use to replace them: '-' and '_'.

However, the '=' only appears on the END of the encoding, as padding characters to even the bit count.  So, the solution I've devised replaces the '+' and '\' with '_' and '-' respectively and replaces the trailing '='s with the count of the number of '='. Since the number of equal signs appearing is either 0, 1 or 2, we know we are never adding more than one character.

Obviously, to decode this, we simply need to replace the last character with the number of =s it designates, replace the '_' and '-' with '+' and '\' respecively and base64 decode the resulting string to get back our original ID.

The following two functions in PHP will do just that:

function idencode($s) {
    $r = base64_encode($s);
    // replace + and /
    $r = str_replace('+', '_', $r);
    $r = str_replace('/', '-', $r);
    // replace trailing "=" with the count of number of "="
    $eqpos = strpos($r, '=');
    if($eqpos===false) {
        $count = 0;
    }
    else {
        $count = strlen($r) - $eqpos;
        // remove =
        $r = str_replace('=', '', $r);
    }
    // add count
    $r .= $count;
    return $r;
}

function iddecode($s) {
    print "$s\n\n";
    // replace "=" at end
    $count = substr($s, -1);
    $r = substr($s, 0, -1);
    if($count>0) {
        for($i=0;$i<$count;$i++) {
            $r .= '=';
        }
    }
    // replace - and _
    $r = str_replace('_', '+', $r);
    $r = str_replace('-', '/', $r);
    return base64_decode($r);
}

So far, my tests using this with jqGrid have been successful.

Tony, you might want to consider adding this to the documentation.

25/03/2010
09:00
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

Thank you for the investigations and recommendations.

I think that there will be a long disscusion on should we have id or not, should we have on every DB table

uniquie id or not. All these are up to developer and for the concrete project.

jqGrid actually have a lot of features and and you actually can not care about the ids.

If the id is not set or can not be found we use a counter.

I think that you can use the following code from jqGrid

    jqID : function(sid){
        sid = sid + "";
        return sid.replace(/([\.\:\[\]])/g,"\\$1");
    },

This function can be easy extended with characters what you want to be replaced - i.e after you load jqGrid js files you can do

$.extend($.jgrid,{

jqID : function (id) {

// your code here

}

Best Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

25/03/2010
21:25
Avatar
rodk
Member
Members
Forum Posts: 12
Member Since:
19/03/2010
sp_UserOfflineSmall Offline

Tony,

Thanks.  Didn't know about jqID.  I'm going to play around with it a bit to see if I can determine the best way to handle this.  It's probable that each solution will have places where one works better than the others.

Forum Timezone: Europe/Sofia

Most Users Ever Online: 994

Currently Online:
55 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information