rejetto forum

%filename-encoded% suggestion

habazlam · 17 · 14049

0 Members and 1 Guest are viewing this topic.

Offline habazlam

  • Occasional poster
  • *
    • Posts: 6
    • View Profile
Hi rejetto,

Assume that I'm doing something like this in .tpl:

....onClick="SomeFunction('%filename%')"...., where SomeFunction() prosesses STRING %filename% in some manner.

Now imagine that %filename% is equal to O'Reilly - Writing HFS Modules with Perl and C.chm :)

Obviously, I get a syntax error if I the "'" (apostroph) symbol is not encoded (certainly, there are other symbols that must be encoded for this case).

Of course, there are a lot of tricks to make a workaround for this kind of errors, but as a result, the JavaScript code in .tpl becomes unnecessarily complex.

This is the reason of my suggestion of the %filename-encoded% symbol.

BTW, thanks a lot for the HFS project: I was so impressed that began to develop a single-page HTML/DOM version of .tpl in my spare time :)


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
ok, please give me a full list of char.s to be encoded, and for each one the desired encoding.
and do it soon because the stable version is about to be published.


Offline Flynsarmy

  • Occasional poster
  • *
    • Posts: 92
  • DENIED!
    • View Profile
    • Flynsarmy
These two are essential:
' = %27
" = %22

I suppose these might also be worth encoding?
! = %21
( = %28
) = %29



Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
i'm unsure this encoding is suitable for javascript context.
i mean, if he doesn't use the filename for making an URL, but for other purposes, this encoding may be inappropriate.
hasn't javascript a way to encode characters inside strings?
PHP uses \xHH where HH is hexadecimal code. AFAIK, also C uses this method.


Offline Flynsarmy

  • Occasional poster
  • *
    • Posts: 92
  • DENIED!
    • View Profile
    • Flynsarmy
A function could be written to encode a string by searching through it for any matches
and switching them to the new value, but javascript is slow and it would be far less
redundant and better for the page load if it was done automatically by HFS. We've had
this problem before on ToG. It would only cost 2 extra bytes per symbol changed if
it was done with HFS, but it would take an entire line and a slow javascript execution
to do it that way.


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
i think you misunderstood.
the thing i'm talking about requires no extra processing in javascript.
in php "A" and "\x41" is exactly the same thing for the programmer.
it's a different source for the same thing, i'll have a string made with one byte/char.


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
indeed, i just tried
document.write('Test \x41');

this thing is exactly the same as
document.write('Test A');


i would introduce a symbol %filename-js% with ' and " encoded. is it enough?


Offline habazlam

  • Occasional poster
  • *
    • Posts: 6
    • View Profile
Hi rejetto,

Quote
i would introduce a symbol %filename-js% with ' and " encoded. is it enough?

Sure. Formally speaking, only string delimiters must be encoded inside %filename-js%:

Code: [Select]
<script type="text/javascript">
document.write('Test \x27<br>');
document.write('Test \x22<br>');
</script>

But I have to point you to the fact that theoretically the symbols in .tpl may be defined by .tpl developer as JavaScript fragments too, and in this case he/she may want to use JavaScript "escape sequences". So, you should isolate the first two ones from such replacement in other HFS functions (if you're going to use this replacement function in other pieces of HFS code, of course ;) ):

Code: [Select]
>>>> \'       Single quotation mark
>>>> \"       Double quotation mark
     \\       Backslash
     \b       Backspace
     \f       Form feed
     \n       New line
     \r       Carriage return
     \t       Horizontal tab
     \ddd     Octal sequence (3 digits: ddd)
     \xdd     Hexadecimal sequence (2 digits: dd)
     \udddd   Unicode sequence (4 hex digits: dddd)





Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
Quote
you should isolate the first two ones

isolate? what you mean? to have two different encodings? if so, what's the gain?


Offline habazlam

  • Occasional poster
  • *
    • Posts: 6
    • View Profile
Quote
isolate? what you mean? to have two different encodings? if so, what's the gain?
In other words, just make such replacements only in %filename-js% and other native symbols, generated by HFS you may want to introduce, not in user-defined ones.

Example:

Code: [Select]
[progress-upload-file]
document.getElementById('PBFile').innerHTML  = '%filename%'+'Some string with some substrings \"quoted\" in \'different\' manners';
....and so on

Please note that theoretically both apostroph and double quote signs may appear in [progress-upload-file] in several syntax contexts (also the apostroph sign may be contained in %filename%) in this case. If you'll begin to sort these things out on the server side, a huge source of bugs may be created - just leave user-defined symbols for the .tpl developer's responsibility.


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
i don't see how this can be a problem.
ALL will remain the same, BUT i will introduce the new symbol %filename-js% to be used in place of %filename% within javascript.

do you see any problem?


Offline habazlam

  • Occasional poster
  • *
    • Posts: 6
    • View Profile

Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile

Offline habazlam

  • Occasional poster
  • *
    • Posts: 6
    • View Profile
I meant that in the HFS/tpl context "symbol" is a kind of macro definition, right? From the other hand, there is the list of "symbols", which are generated by HFS, and a .tpl developer can't change them. So, these two parts of HFS symbols may be called as "server-defined" and "user-defined" - just for convenience :)


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
truly, i still don't understand what a user-defined symbol is.
can you please make an example (or two)?


Offline habazlam

  • Occasional poster
  • *
    • Posts: 6
    • View Profile
Hi rejetto,

It was just my attmpt of of classification your "symbols" :)

For example, %filename% may be called "server defined symbol", but apparently %unauthorized% is totally "user-defined".


Offline rejetto

  • Administrator
  • Tireless poster
  • *****
    • Posts: 13510
    • View Profile
%unauthorized% doesn't exist :)

maybe %style% is an example of user-defined symbol?