home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 13 / MA_Cover_13.bin / source / c / tidy26jul99 / istack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-08  |  5.6 KB  |  255 lines

  1. /*
  2.   ilstack.c - inline stack for compatibility with Mosaic
  3.  
  4.   (c) 1998 (W3C) MIT, INRIA, Keio University
  5.   See tidy.c for the copyright notice.
  6. */
  7.  
  8. #include "platform.h"
  9. #include "html.h"
  10.  
  11. extern Bool   debug_flag;
  12. extern Node  *debug_element;
  13. extern Lexer *debug_lexer;
  14.  
  15. /* duplicate attributes */
  16. AttVal *DupAttrs(AttVal *attrs)
  17. {
  18.     AttVal *newattrs;
  19.     char *p;
  20.  
  21.     if (attrs == null)
  22.         return attrs;
  23.  
  24.     newattrs = (AttVal *)MemAlloc(sizeof(AttVal));
  25.     newattrs->next = DupAttrs(attrs->next);
  26.     newattrs->attribute = wstrdup(attrs->attribute);
  27.     p = wstrdup(attrs->value);
  28.     newattrs->value = p;
  29.     newattrs->delim = attrs->delim;
  30.     newattrs->dict = FindAttribute(newattrs);
  31.     return newattrs;
  32. }
  33.  
  34. /*
  35.   push a copy of an inline node onto stack
  36.   but don't push if implicit or OBJECT or APPLET
  37.   (implicit tags are ones generated from the istack)
  38.  
  39.   One issue arises with pushing inlines when
  40.   the tag is already pushed. For instance:
  41.  
  42.       <p><em>text
  43.       <p><em>more text
  44.  
  45.   Shouldn't be mapped to
  46.  
  47.       <p><em>text</em></p>
  48.       <p><em><em>more text</em></em>
  49. */
  50. void PushInline(Lexer *lexer, Node *node)
  51. {
  52.     IStack *istack;
  53.  
  54.     if (node->implicit)
  55.         return;
  56.  
  57.     if (node->tag == null)
  58.         return;
  59.  
  60.     if (!(node->tag->model & CM_INLINE))
  61.         return;
  62.  
  63.     if (node->tag->model & CM_OBJECT)
  64.         return;
  65.  
  66.     if (node->tag != tag_font && IsPushed(lexer, node))
  67.         return;
  68.  
  69.     /* make sure there is enough space for the stack */
  70.     if (lexer->istacksize + 1 > lexer->istacklength)
  71.     {
  72.         if (lexer->istacklength == 0)
  73.             lexer->istacklength = 6;   /* this is perhaps excessive */
  74.  
  75.         lexer->istacklength = lexer->istacklength * 2;
  76.         lexer->istack = (IStack *)MemRealloc(lexer->istack,
  77.                             sizeof(IStack)*(lexer->istacklength));
  78.     }
  79.  
  80.     istack = &(lexer->istack[lexer->istacksize]);
  81.     istack->tag = node->tag;
  82.  
  83.     istack->element = wstrdup(node->element);
  84.     istack->attributes = DupAttrs(node->attributes);
  85.     ++(lexer->istacksize);
  86. }
  87.  
  88. /* pop inline stack */
  89. void PopInline(Lexer *lexer, Node *node)
  90. {
  91.     AttVal *av;
  92.     IStack *istack;
  93.  
  94.     if (node)
  95.     {
  96.         if (node->tag == null)
  97.             return;
  98.  
  99.         if (!(node->tag->model & CM_INLINE))
  100.             return;
  101.  
  102.         if (node->tag->model & CM_OBJECT)
  103.             return;
  104.  
  105.         /* if node is </a> then pop until we find an <a> */
  106.         if (node->tag == tag_a)
  107.         {
  108.             while (lexer->istacksize > 0)
  109.             {
  110.                 --(lexer->istacksize);
  111.                 istack = &(lexer->istack[lexer->istacksize]);
  112.  
  113.                 while (istack->attributes)
  114.                 {
  115.                     av = istack->attributes;
  116.  
  117.                     if (av->attribute)
  118.                         MemFree(av->attribute);
  119.                     if (av->value)
  120.                         MemFree(av->value);
  121.  
  122.                     istack->attributes = av->next;
  123.                     MemFree(av);
  124.                 }
  125.  
  126.                 if (istack->tag == tag_a)
  127.                 {
  128.                     MemFree(istack->element);
  129.                     break;
  130.                 }
  131.  
  132.                 MemFree(istack->element);
  133.             }
  134.  
  135.             return;
  136.         }
  137.     }
  138.  
  139.     if (lexer->istacksize > 0)
  140.     {
  141.         --(lexer->istacksize);
  142.         istack = &(lexer->istack[lexer->istacksize]);
  143.  
  144.         while (istack->attributes)
  145.         {
  146.             av = istack->attributes;
  147.  
  148.             if (av->attribute)
  149.                 MemFree(av->attribute);
  150.             if (av->value)
  151.                 MemFree(av->value);
  152.  
  153.             istack->attributes = av->next;
  154.             MemFree(av);
  155.         }
  156.  
  157.         MemFree(istack->element);
  158.     }
  159. }
  160.  
  161. Bool IsPushed(Lexer *lexer, Node *node)
  162. {
  163.     int i;
  164.  
  165.     for (i = lexer->istacksize - 1; i >= 0; --i)
  166.     {
  167.         if (lexer->istack[i].tag == node->tag)
  168.             return yes;
  169.     }
  170.  
  171.     return no;
  172. }
  173.  
  174. /*
  175.   This has the effect of inserting "missing" inline
  176.   elements around the contents of blocklevel elements
  177.   such as P, TD, TH, DIV, PRE etc. This procedure is
  178.   called at the start of ParseBlock. when the inline
  179.   stack is not empty, as will be the case in:
  180.  
  181.     <i><h1>italic heading</h1></i>
  182.  
  183.   which is then treated as equivalent to
  184.  
  185.     <h1><i>italic heading</i></h1>
  186.  
  187.   This is implemented by setting the lexer into a mode
  188.   where it gets tokens from the inline stack rather than
  189.   from the input stream.
  190. */
  191. int InlineDup(Lexer *lexer, Node *node)
  192. {
  193.     int n;
  194.  
  195.     if ((n = lexer->istacksize - lexer->istackbase) > 0)
  196.     {
  197.         lexer->insert = &(lexer->istack[lexer->istackbase]);
  198.         lexer->inode = node;
  199.     }
  200.  
  201.     return n;
  202. }
  203.  
  204. Node *InsertedToken(Lexer *lexer)
  205. {
  206.     Node *node;
  207.     IStack *istack;
  208.     uint n;
  209.  
  210.     /* this will only be null if inode != null */
  211.     if (lexer->insert == null)
  212.     {
  213.         node = lexer->inode;
  214.         lexer->inode = null;
  215.         return node;
  216.     }
  217.  
  218.     /*
  219.     
  220.       is this is the "latest" node then update
  221.       the position, otherwise use current values
  222.     */
  223.  
  224.     if (lexer->inode == null)
  225.     {
  226.         lexer->lines = lexer->in->curline;
  227.         lexer->columns = lexer->in->curcol;
  228.     }
  229.  
  230.     node = NewNode();
  231.     node->type = StartTag;
  232.     node->implicit = yes;
  233.     node->start = lexer->txtstart;
  234.     node->end = lexer->txtstart;
  235.     istack = lexer->insert;
  236.     node->element = wstrdup(istack->element);
  237.     node->tag = istack->tag;
  238.     node->attributes = DupAttrs(istack->attributes);
  239.  
  240.     /* advance lexer to next item on the stack */
  241.     n = lexer->insert - &(lexer->istack[0]);
  242.  
  243.     /* and recover state if we have reached the end */
  244.     if (++n < lexer->istacksize)
  245.         lexer->insert = &(lexer->istack[n]);
  246.     else
  247.         lexer->insert = null;
  248.  
  249.     return node;
  250. }
  251.  
  252.  
  253.  
  254.  
  255.