Re: Column problem with a empty script tag

Marc Gueury <[email protected]>
Newsgroups gmane.comp.web.html-tidy.devel
Message-ID <[email protected]>
Hello,

To follow my mail of yesterday, I finally understood that the new code 
fixes another problem.
(a wrong message is written when there is a empty <script></script> tag.)

The base reason of the problem is that UngetChar is used more
than once. The library for UngetChar is not made for it. You can
call just once, else you loose your column number.

So, I made a fix, testes it, and it works. It is basically a history of 
all the last columns
where a char is taken.
When calling ReadChar, and that there is no buffer, the history is 
populated.
When calling Ungetchar, it goes back in the history.
When calling PopChar (=ReadChar when there is a buffer) it goes forward 
in the history.

It solves the problem in lexer.c. There was there a bad trick because 
there 2 UngetChar
where also called one after the other.

Thanks by advance,

Marc

Ps: Here is the 3 diffs  between the 26-oct-2004 version + my proposed fix.
     I do not know how to submit this :-) Please correct me if it is wrong.
 - streamio.h
 - streamio.c
 - lexer.c

--- ..\..\tidy_26_10_2004\src\streamio.h    2004-03-08 
08:59:32.000000000 +0100
+++ streamio.h    2004-11-05 13:02:20.502436800 +0100
@@ -39,6 +39,7 @@
 ************************/
 
 #define CHARBUF_SIZE 5
+#define LASTCOL_SIZE 64
 
 /* non-raw input is cleaned up*/
 struct _StreamIn
@@ -49,7 +50,8 @@
     uint   bufpos;
     uint   bufsize;
     int    tabs;
-    int    lastcol;
+    int    lastcols[LASTCOL_SIZE];
+    int    curlastcol;
     int    curcol;
     int    curline;
     int    encoding;
    
-----------------------------------------------------------------------------     

    
--- ..\..\tidy_26_10_2004\src\streamio.c    2004-08-07 
09:58:24.000000000 +0200
+++ streamio.c    2004-11-05 13:15:02.888694400 +0100
@@ -114,6 +114,7 @@
     in->doc = doc;
     in->bufsize = CHARBUF_SIZE;
     in->charbuf = MemAlloc(sizeof(tchar) * in->bufsize);
+    in->curlastcol = 0;
 #ifdef TIDY_STORE_ORIGINAL_TEXT
     in->otextbuf = NULL;
     in->otextlen = 0;
@@ -272,7 +273,13 @@
     if ( in->pushed )
         return PopChar( in );
 
-    in->lastcol = in->curcol;
+    // Rotating array storing the last 64 (LASTCOL_SIZE) lastcol
+    in->curlastcol++;
+    if( in->curlastcol>=LASTCOL_SIZE )
+    {
+        in->curlastcol-=LASTCOL_SIZE;
+    }
+    in->lastcols[in->curlastcol] = in->curcol;
 
     if ( in->tabs > 0 )
     {
@@ -478,6 +485,14 @@
             return c;
         }
         in->curcol++;
+
+        // Rotating array of lastcol
+        in->curlastcol++;
+        if( in->curlastcol>=LASTCOL_SIZE )
+        {
+          in->curlastcol-=LASTCOL_SIZE;
+        }
+
     }
     return c;
 }
@@ -500,7 +515,13 @@
     if (c == '\n')
         --(in->curline);
 
-    in->curcol = in->lastcol;
+    // Rotating array storing the last 64 (LASTCOL_SIZE) lastcol
+    in->curcol = in->lastcols[in->curlastcol];
+    in->curlastcol--;
+    if( in->curlastcol<0 )
+    {
+        in->curlastcol+=LASTCOL_SIZE;
+    }
 }
 
--- ..\..\tidy_26_10_2004\src\lexer.c    2004-08-12 09:58:06.000000000 +0200
+++ lexer.c    2004-11-05 14:24:43.099542400 +0100
@@ -2230,7 +2230,6 @@
                 {
                     UngetChar(c, doc->docIn);     /* push back letter */
                     UngetChar('<', doc->docIn);
-                    --(doc->docIn->curcol);
                     lexer->lexsize -= 2;      /* discard "<" + letter */
                     lexer->txtend = lexer->lexsize;
                     lexer->state = LEX_STARTTAG;         /* ready to 
read tag name */





Marc Gueury wrote:

> Hello all,
>
> I was analyzing pages when I noticed that sometimes the column where 
> the error was reported
> was wrong for some pages.
>
> Then I finished to make this testcase:
>
> <html><head>
>  <title>Google</title>
> </head>
> <body>
> <script>
> </script><a id=1a href="/imghp?hl=en&tab=wi">Images</a>
> </body></html>
>
> The output is output:
> line 6 column 43 - Warning: unescaped & or unknown entity "&tab"
>
> This should be:
> line 6 column 37 - Warning: unescaped & or unknown entity "&tab"
>
> First, I got the newest and the oldest version of Tidy.
> It work in the old version and not in the last one
>
> After some debugging, the reason of this is a change in lexer.c line 1838
> The <script> tag being empty seems to cause a problem.
>
> New Version (wrong)
>
>            if (matches && nested-- <= 0)
>            {
>                for (i = lexer->lexsize - 1; i >= start; --i)
>                    UngetChar((uint)lexer->lexbuf[i], doc->docIn);
>                UngetChar('/', doc->docIn);
>                UngetChar('<', doc->docIn);
>                lexer->lexsize -= (lexer->lexsize - start) + 2;
>                break;
>            }
>           Old version (It works if I copy paste this code in the new 
> one too)          
>            if (matches && nested-- <= 0)
>            {
>                /* skip trailing white space in end tag */
>                while (IsWhite(c))
>                    c = ReadChar(doc->docIn);
>
>                /* <script>...</script<p>... */
>                if (c == '<')
>                    UngetChar(c, doc->docIn);
>
>                lexer->lexsize -= (lexer->lexsize - start) + 2;
>                break;
>            }
>
> What can I do with this ?
>
> Thanks by advance.
>
> Marc         
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by:
> Sybase ASE Linux Express Edition - download now for FREE
> LinuxWorld Reader's Choice Award Winner for best database on Linux.
> http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
> _______________________________________________
> Tidy-develop mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/tidy-develop
>
>
tidy_bug_patch_2.txt (text/plain, 2.4 KB)
--- ..\..\tidy_26_10_2004\src\streamio.h	2004-03-08 08:59:32.000000000 +0100
+++ streamio.h	2004-11-05 13:02:20.502436800 +0100
@@ -39,6 +39,7 @@
 ************************/
 
 #define CHARBUF_SIZE 5
+#define LASTCOL_SIZE 64
 
 /* non-raw input is cleaned up*/
 struct _StreamIn
@@ -49,7 +50,8 @@
     uint   bufpos;
     uint   bufsize;
     int    tabs;
-    int    lastcol;
+    int    lastcols[LASTCOL_SIZE];
+    int    curlastcol;
     int    curcol;
     int    curline;
     int    encoding;
     
-----------------------------------------------------------------------------     
     
--- ..\..\tidy_26_10_2004\src\streamio.c	2004-08-07 09:58:24.000000000 +0200
+++ streamio.c	2004-11-05 13:15:02.888694400 +0100
@@ -114,6 +114,7 @@
     in->doc = doc;
     in->bufsize = CHARBUF_SIZE;
     in->charbuf = MemAlloc(sizeof(tchar) * in->bufsize);
+    in->curlastcol = 0;
 #ifdef TIDY_STORE_ORIGINAL_TEXT
     in->otextbuf = NULL;
     in->otextlen = 0;
@@ -272,7 +273,13 @@
     if ( in->pushed )
         return PopChar( in );
 
-    in->lastcol = in->curcol;
+    // Rotating array storing the last 64 (LASTCOL_SIZE) lastcol
+    in->curlastcol++;
+    if( in->curlastcol>=LASTCOL_SIZE ) 
+    {
+        in->curlastcol-=LASTCOL_SIZE;
+    }
+    in->lastcols[in->curlastcol] = in->curcol;
 
     if ( in->tabs > 0 )
     {
@@ -478,6 +485,14 @@
             return c;
         }
         in->curcol++;
+
+        // Rotating array of lastcol
+        in->curlastcol++;
+        if( in->curlastcol>=LASTCOL_SIZE ) 
+        {
+          in->curlastcol-=LASTCOL_SIZE;
+        }
+
     }
     return c;
 }
@@ -500,7 +515,13 @@
     if (c == '\n')
         --(in->curline);
 
-    in->curcol = in->lastcol;
+    // Rotating array storing the last 64 (LASTCOL_SIZE) lastcol
+    in->curcol = in->lastcols[in->curlastcol];
+    in->curlastcol--;
+    if( in->curlastcol<0 ) 
+    {
+        in->curlastcol+=LASTCOL_SIZE;
+    }
 }
 
--- ..\..\tidy_26_10_2004\src\lexer.c	2004-08-12 09:58:06.000000000 +0200
+++ lexer.c	2004-11-05 14:24:43.099542400 +0100
@@ -2230,7 +2230,6 @@
                 {
                     UngetChar(c, doc->docIn);     /* push back letter */
                     UngetChar('<', doc->docIn);
-                    --(doc->docIn->curcol);
                     lexer->lexsize -= 2;      /* discard "<" + letter */
                     lexer->txtend = lexer->lexsize;
                     lexer->state = LEX_STARTTAG;         /* ready to read tag name */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.