Annotation of Amaya/amaya/XHTMLbuilder.c, revision 1.82

1.1       cvs         1: /*
                      2:  *
1.78      vatton      3:  *  (c) COPYRIGHT INRIA and W3C, 1996-2003
1.1       cvs         4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
                      7: 
                      8: /*
1.30      cvs         9:  * XHTMLbuilder.c
1.23      cvs        10:  * Builds the corresponding abstract tree for a Thot document of type HTML.
1.1       cvs        11:  *
1.20      cvs        12:  * Authors: L. Carcone
                     13:  *          V. Quint 
1.1       cvs        14:  */
                     15: 
                     16: #define THOT_EXPORT extern
                     17: #include "amaya.h"
                     18: #include "css.h"
1.2       cvs        19: #include "parser.h"
                     20: #include "HTML.h"
1.30      cvs        21: #include "fetchHTMLname.h"
1.2       cvs        22: 
1.13      cvs        23: #include "css_f.h"
1.76      vatton     24: #include "EDITstyle_f.h"
1.13      cvs        25: #include "fetchXMLname_f.h"
1.30      cvs        26: #include "fetchHTMLname_f.h"
1.22      cvs        27: #include "html2thot_f.h"
1.1       cvs        28: #include "HTMLactions_f.h"
                     29: #include "HTMLedit_f.h"
1.22      cvs        30: #include "HTMLform_f.h"
1.1       cvs        31: #include "HTMLimage_f.h"
                     32: #include "HTMLtable_f.h"
                     33: #include "HTMLimage_f.h"
                     34: #include "UIcss_f.h"
1.13      cvs        35: #include "styleparser_f.h"
1.2       cvs        36: #include "XHTMLbuilder_f.h"
1.13      cvs        37: #include "Xml2thot_f.h"
1.1       cvs        38: 
                     39: /* maximum length of a Thot structure schema name */
                     40: #define MAX_SS_NAME_LENGTH 32
                     41: 
1.30      cvs        42: #define MaxMsgLength 200
                     43: 
1.47      cvs        44: /* Elements that cannot contain text as immediate children.
                     45:    When some text is present in the HTML file it must be 
                     46:    surrounded by a Pseudo_paragraph element */
                     47: static int          NoTextChild[] =
                     48: {
                     49:    HTML_EL_Document, HTML_EL_HTML, HTML_EL_HEAD, HTML_EL_BODY,
                     50:    HTML_EL_Definition_List, HTML_EL_Block_Quote, HTML_EL_Directory,
                     51:    HTML_EL_Form, HTML_EL_Menu, HTML_EL_FIELDSET,
                     52:    HTML_EL_Numbered_List, HTML_EL_Option_Menu,
                     53:    HTML_EL_Unnumbered_List, HTML_EL_Definition, HTML_EL_List_Item,
                     54:    HTML_EL_MAP, HTML_EL_map, HTML_EL_Applet,
                     55:    HTML_EL_Object, HTML_EL_IFRAME, HTML_EL_NOFRAMES,
                     56:    HTML_EL_Division, HTML_EL_Center, HTML_EL_NOSCRIPT,
                     57:    HTML_EL_Data_cell, HTML_EL_Heading_cell,
                     58:    0};
                     59: 
1.28      cvs        60: /* Define a pointer to let parser functions access the HTML entity table */
                     61: extern XmlEntity *pXhtmlEntityTable;
1.6       cvs        62: 
1.30      cvs        63: /* maximum size of error messages */
                     64: #define MaxMsgLength 200
                     65: 
1.6       cvs        66: /*----------------------------------------------------------------------
1.15      cvs        67:   ParseCharset:
1.6       cvs        68:   Parses the element HTTP-EQUIV and looks for the charset value.
                     69:   ----------------------------------------------------------------------*/
1.30      cvs        70: void             ParseCharset (Element el, Document doc) 
                     71: 
1.6       cvs        72: {
1.15      cvs        73:    AttributeType attrType;
                     74:    Attribute     attr;
                     75:    SSchema       docSSchema;
                     76:    CHARSET       charset;
1.39      cvs        77:    char         *text, *text2, *ptrText, *str;
                     78:    char          charsetname[MAX_LENGTH];
1.15      cvs        79:    int           length;
1.6       cvs        80:    int           pos, index = 0;
                     81: 
1.15      cvs        82:    charset = TtaGetDocumentCharset (doc);
                     83:    if (charset != UNDEFINED_CHARSET)
                     84:      /* the charset was already defined by the http header */
                     85:      return;
1.6       cvs        86: 
                     87:    docSSchema = TtaGetDocumentSSchema (doc);
                     88:    attrType.AttrSSchema = docSSchema;
                     89:    attrType.AttrTypeNum = HTML_ATTR_http_equiv;
                     90:    attr = TtaGetAttribute (el, attrType);
                     91:    if (attr != NULL)
                     92:      {
                     93:        /* There is a HTTP-EQUIV attribute */
                     94:        length = TtaGetTextAttributeLength (attr);
                     95:        if (length > 0)
                     96:         {
1.37      cvs        97:           text = TtaGetMemory (length + 1);
1.6       cvs        98:           TtaGiveTextAttributeValue (attr, text, &length);
1.37      cvs        99:           if (!strcasecmp (text, "content-type"))
1.6       cvs       100:             {
                    101:               attrType.AttrTypeNum = HTML_ATTR_meta_content;
                    102:               attr = TtaGetAttribute (el, attrType);
                    103:               if (attr != NULL)
                    104:                 {
                    105:                   length = TtaGetTextAttributeLength (attr);
                    106:                   if (length > 0)
                    107:                     {
1.37      cvs       108:                       text2 = TtaGetMemory (length + 1);
1.6       cvs       109:                       TtaGiveTextAttributeValue (attr, text2, &length);
                    110:                       ptrText = text2;
                    111:                       while (*ptrText)
                    112:                         {
1.53      vatton    113:                           *ptrText = tolower (*ptrText);
1.6       cvs       114:                           ptrText++;
                    115:                         }
                    116:                       
1.37      cvs       117:                       str = strstr (text2, "charset=");
1.6       cvs       118:                       if (str)
                    119:                         {
                    120:                           pos = str - text2 + 8;
1.37      cvs       121:                           while (text2[pos] != SPACE &&
                    122:                                  text2[pos] != TAB && text2[pos] != EOS)
1.6       cvs       123:                             charsetname[index++] = text2[pos++];
1.37      cvs       124:                           charsetname[index] = EOS;
1.15      cvs       125:                           charset = TtaGetCharset (charsetname);
                    126:                           if (charset != UNDEFINED_CHARSET)
1.77      cvs       127:                             TtaSetDocumentCharset (doc, charset, FALSE);
1.6       cvs       128:                         }
                    129:                       TtaFreeMemory (text2);
                    130:                     }       
                    131:                 } 
                    132:             }
                    133:           TtaFreeMemory (text);
                    134:         }
                    135:      }
                    136: }
                    137: 
1.23      cvs       138: /*----------------------------------------------------------------------
1.47      cvs       139:    XhtmlCannotContainText 
                    140:    Return TRUE if element el is a block element.
                    141:   ----------------------------------------------------------------------*/
                    142: ThotBool      XhtmlCannotContainText (ElementType elType)
                    143: 
                    144: {
                    145:    int        i;
                    146:    ThotBool   ret;
                    147: 
                    148:    if (strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
                    149:       /* not an HTML element */
                    150:       ret = TRUE;
                    151:    else
                    152:      {
                    153:        ret = FALSE;
                    154:        i = 0;
                    155:        while (NoTextChild[i] > 0 && NoTextChild[i] != elType.ElTypeNum)
                    156:            i++;
                    157:        if (NoTextChild[i] == elType.ElTypeNum)
                    158:            ret = TRUE;
                    159:      }
                    160:    return ret;
1.23      cvs       161: }
                    162: 
1.6       cvs       163: /*----------------------------------------------------------------------
                    164:   XhtmlElementComplete
1.20      cvs       165:   Complete Xhtml elements.
1.6       cvs       166:   Check its attributes and its contents.
                    167:   ----------------------------------------------------------------------*/
1.72      cvs       168: void       XhtmlElementComplete (ParserData *context, Element el, int *error)
1.30      cvs       169: 
                    170: {
1.72      cvs       171:    Document       doc;   
1.30      cvs       172:    ElementType    elType, newElType, childType;
                    173:    Element        constElem, child, desc, leaf, prev, next, last,
1.62      quint     174:                  elFrames, lastFrame, lastChild, parent, picture, content;
1.30      cvs       175:    Attribute      attr;
                    176:    AttributeType  attrType;
                    177:    Language       lang;
1.41      cvs       178:    char           *text;
1.39      cvs       179:    char           lastChar[2];
1.79      quint     180:    char           *name1, *data;
1.30      cvs       181:    int            length;
                    182:    SSchema        docSSchema;
1.62      quint     183:    ThotBool       isImage;
                    184:    PresentationValue    pval;
                    185:    PresentationContext  ctxt;
1.6       cvs       186: 
                    187:    *error = 0;
1.72      cvs       188:    doc = context->doc;
1.6       cvs       189:    docSSchema = TtaGetDocumentSSchema (doc);
                    190: 
                    191:    elType = TtaGetElementType (el);
                    192:    /* is this a block-level element in a character-level element? */
1.56      cvs       193:    if (!IsXMLElementInline (elType, doc) &&
1.24      cvs       194:        elType.ElTypeNum != HTML_EL_Comment_ &&
                    195:        elType.ElTypeNum != HTML_EL_XMLPI)
1.6       cvs       196:        BlockInCharLevelElem (el);
                    197: 
                    198:    newElType.ElSSchema = elType.ElSSchema;
                    199:    switch (elType.ElTypeNum)
                    200:      {
1.62      quint     201:      case HTML_EL_Object:      /* it's an object */
1.79      quint     202:        data = NULL;
1.62      quint     203:        isImage = FALSE;
                    204:        /* is there a type attribute on the object element? */
                    205:        attrType.AttrSSchema = elType.ElSSchema;
                    206:        attrType.AttrTypeNum = HTML_ATTR_Object_type;
1.6       cvs       207:        attr = TtaGetAttribute (el, attrType);
1.62      quint     208:        if (attr)
                    209:         /* there is a type attribute. Get its value to see if the object
                    210:            represents an image */
1.6       cvs       211:         {
                    212:           length = TtaGetTextAttributeLength (attr);
                    213:           if (length > 0)
                    214:             {
1.37      cvs       215:               name1 = TtaGetMemory (length + 1);
1.6       cvs       216:               TtaGiveTextAttributeValue (attr, name1, &length);
1.68      kahan     217:               if (!strcmp (name1, AM_MATHML_MIME_TYPE) ||
1.62      quint     218:                   !strcmp (name1, "application/postscript") ||
                    219:                   !strcmp (name1, "image/x-bitmap") ||
                    220:                   !strcmp (name1, "image/x-xpixmap") ||
                    221:                   !strcmp (name1, "image/gif") ||
                    222:                   !strcmp (name1, "image/jpeg") ||
                    223:                   !strcmp (name1, "image/png") ||
1.67      kahan     224:                   !strcmp (name1, "image/svg") ||
1.68      kahan     225:                   !strcmp (name1, AM_SVG_MIME_TYPE))
1.62      quint     226:                 isImage = TRUE;
                    227:               TtaFreeMemory (name1);
                    228:             }
                    229:         }
1.79      quint     230: 
                    231:        attrType.AttrTypeNum = HTML_ATTR_data;
                    232:        attr = TtaGetAttribute (el, attrType);
                    233:        if (attr)
                    234:         /* the object has a data attribute */
                    235:         {
                    236:           length = TtaGetTextAttributeLength (attr);
                    237:           if (length > 0)
                    238:             {
                    239:               data = TtaGetMemory (length + 1);
                    240:               TtaGiveTextAttributeValue (attr, data, &length);
                    241:               if (!isImage)
                    242:                 if (!strcmp (&data[length-3], "mml") ||
                    243:                     !strcmp (&data[length-3], "gif") ||
                    244:                     !strcmp (&data[length-3], "jpg") ||
                    245:                     !strcmp (&data[length-4], "jpeg") ||
                    246:                     !strcmp (&data[length-3], "png") ||
                    247:                     !strcmp (&data[length-3], "svg") ||
1.80      cvs       248:                     !strcmp (&data[length-4], "svgz") ||
                    249:                     !strcmp (&data[length-3], "htm"))
1.79      quint     250:                   isImage = TRUE;
                    251:             }
                    252:         }
1.62      quint     253:        picture = NULL;     /* no PICTURE element yet */
                    254:        child = TtaGetFirstChild (el);
                    255:        if (isImage)
                    256:         /* the object represents an image. We need a PICTURE element as
                    257:            child of the object to hold the image */
                    258:         {
                    259:           if (child)
                    260:             {
                    261:               elType = TtaGetElementType (child);
                    262:               if (elType.ElTypeNum == HTML_EL_PICTURE_UNIT)
                    263:                 /* there is already a PICTURE element */
                    264:                 picture = child;
                    265:             }
1.79      quint     266:           /* if the object element has no PICTURE element as its first child
1.62      quint     267:              create one */
                    268:           if (!picture)
                    269:             {
                    270:               elType.ElTypeNum = HTML_EL_PICTURE_UNIT;
                    271:               picture = TtaNewTree (doc, elType, "");
                    272:               if (child)
                    273:                 TtaInsertSibling (picture, child, TRUE, doc);
                    274:               else
                    275:                 TtaInsertFirstChild (&picture, el, doc);
                    276:               child = picture;
                    277:             }
                    278:           /* copy attribute data of the object into the SRC attribute of
                    279:              the PICTURE element */
1.79      quint     280:           if (data)
                    281:             /* the object has a data attribute */
                    282:             {
                    283:               attrType.AttrTypeNum = HTML_ATTR_SRC;
                    284:               attr = TtaGetAttribute (picture, attrType);
                    285:               if (attr == NULL)
                    286:                 {
                    287:                   attr = TtaNewAttribute (attrType);
                    288:                   TtaAttachAttribute (picture, attr, doc);
                    289:                 }
                    290:               TtaSetAttributeText (attr, data, picture, doc);
                    291:             }
                    292:           attrType.AttrTypeNum = HTML_ATTR_Height_;
1.62      quint     293:           attr = TtaGetAttribute (el, attrType);
                    294:           if (attr)
1.79      quint     295:             /* the Object has a height attribute. Applies it to the
                    296:                picture element */
                    297:             {
                    298:               length = TtaGetTextAttributeLength (attr);
                    299:               if (length > 0)
                    300:                 {
                    301:                   text = TtaGetMemory (length + 1);
                    302:                   TtaGiveTextAttributeValue (attr, text, &length);
                    303:                   /* create the corresponding attribute IntHeightPercent or */
                    304:                   /* IntHeightPxl */
                    305:                   CreateAttrHeightPercentPxl (text, el, doc, -1);
                    306:                   TtaFreeMemory (text);
                    307:                 }
                    308:             }
                    309:           attrType.AttrTypeNum = HTML_ATTR_Width__;
                    310:           attr = TtaGetAttribute (el, attrType);
                    311:           if (attr)
                    312:             /* the Object has a width attribute. Applies it to the
                    313:                picture element */
1.62      quint     314:             {
                    315:               length = TtaGetTextAttributeLength (attr);
                    316:               if (length > 0)
1.6       cvs       317:                 {
1.79      quint     318:                   text = TtaGetMemory (length + 1);
                    319:                   TtaGiveTextAttributeValue (attr, text, &length);
                    320:                   /* create the corresponding attribute IntWidthPercent or */
                    321:                   /* IntWidthPxl */
                    322:                   CreateAttrWidthPercentPxl (text, el, doc, -1);
                    323:                   TtaFreeMemory (text);
1.6       cvs       324:                 }
                    325:             }
                    326:         }
                    327:        /* is the Object_Content element already created ? */
1.62      quint     328:        if (child)
                    329:         /* the object element has at least 1 child element */
                    330:         {
                    331:           content = NULL;
                    332:           desc = child;
1.6       cvs       333:           elType = TtaGetElementType (desc);
1.62      quint     334:           if (elType.ElTypeNum != HTML_EL_Object_Content)
                    335:             {
                    336:               TtaNextSibling(&desc);
                    337:               if (desc)
                    338:                 elType = TtaGetElementType (desc);
                    339:             }
                    340:           /* is it the Object_Content element ? */
                    341:           if (elType.ElTypeNum == HTML_EL_Object_Content)
                    342:             content = desc;
                    343:           else
1.6       cvs       344:             {
1.62      quint     345:               /* create an Object_Content element */
                    346:               elType.ElTypeNum = HTML_EL_Object_Content;
                    347:               content = TtaNewElement (doc, elType);
                    348:               if (picture)
                    349:                 TtaInsertSibling (content, picture, FALSE, doc);
                    350:               else
                    351:                 TtaInsertSibling (content, child, TRUE, doc);
                    352:               /* move previous existing children into Object_Content */
1.6       cvs       353:               child = TtaGetLastChild(el);
1.62      quint     354:               while (child != content)
                    355:                 {
                    356:                   TtaRemoveTree (child, doc);
                    357:                   TtaInsertFirstChild (&child, content, doc);
                    358:                   child = TtaGetLastChild(el);
                    359:                 }
                    360:             }
                    361:           if (picture && content)
                    362:             /* there is a picture element. The Object_Content must not be
                    363:                displayed in the main view */
                    364:             {
                    365:               ctxt = TtaGetSpecificStyleContext (doc);
                    366:               /* the presentation rule to be set is not a CSS rule */
                    367:               ctxt->cssSpecificity = 0;
                    368:               ctxt->destroy = FALSE;
                    369:               pval.typed_data.unit = STYLE_UNIT_PX;
                    370:               pval.typed_data.value = 0;
                    371:               pval.typed_data.real = FALSE;
                    372:               TtaSetStylePresentation (PRVisibility, content, NULL, ctxt, pval);
1.6       cvs       373:             }
                    374:         }
1.79      quint     375:        if (data)
                    376:         TtaFreeMemory (data);
1.6       cvs       377:        break;
1.62      quint     378: 
1.6       cvs       379:      case HTML_EL_Unnumbered_List:
                    380:      case HTML_EL_Numbered_List:
                    381:      case HTML_EL_Menu:
                    382:      case HTML_EL_Directory:
                    383:        /* It's a List element. It should only have List_Item children.
                    384:          If it has List element chidren, move these List elements
                    385:          within their previous List_Item sibling.  This is to fix
                    386:          a bug in document generated by Mozilla. */
                    387:        prev = NULL;
                    388:        next = NULL;
                    389:        child = TtaGetFirstChild (el);
                    390:        while (child != NULL)
                    391:         {
                    392:           next = child;
                    393:           TtaNextSibling (&next);
                    394:           elType = TtaGetElementType (child);
                    395:           if (elType.ElTypeNum == HTML_EL_Unnumbered_List ||
                    396:               elType.ElTypeNum == HTML_EL_Numbered_List ||
                    397:               elType.ElTypeNum == HTML_EL_Menu ||
                    398:               elType.ElTypeNum == HTML_EL_Directory)
                    399:             /* this list element is a child of another list element */
                    400:             if (prev)
                    401:               {
                    402:                 elType = TtaGetElementType (prev);
                    403:                 if (elType.ElTypeNum == HTML_EL_List_Item)
                    404:                   {
                    405:                     /* get the last child of the previous List_Item */
                    406:                     desc = TtaGetFirstChild (prev);
                    407:                     last = NULL;
                    408:                     while (desc)
                    409:                       {
                    410:                         last = desc;
                    411:                         TtaNextSibling (&desc);
                    412:                       }
                    413:                     /* move the list element after the last child of the
                    414:                        previous List_Item */
                    415:                     TtaRemoveTree (child, doc);
                    416:                     if (last)
                    417:                       TtaInsertSibling (child, last, FALSE, doc);
                    418:                     else
                    419:                       TtaInsertFirstChild (&child, prev, doc);
                    420:                     child = prev;
                    421:                   }
                    422:               }
                    423:           prev = child;
                    424:           child = next;
                    425:         }
                    426:        break;
                    427:        
                    428:      case HTML_EL_FRAMESET:
                    429:        /* The FRAMESET element is now complete.  Gather all its FRAMESET
                    430:          and FRAME children and wrap them up in a Frames element */
                    431:        elFrames = NULL; lastFrame = NULL;
                    432:        lastChild = NULL;
                    433:        child = TtaGetFirstChild (el);
                    434:        while (child != NULL)
                    435:         {
                    436:           next = child;
                    437:           TtaNextSibling (&next);
                    438:           elType = TtaGetElementType (child);
                    439:           if (elType.ElTypeNum == HTML_EL_FRAMESET ||
                    440:               elType.ElTypeNum == HTML_EL_FRAME ||
                    441:               elType.ElTypeNum == HTML_EL_Comment_)
                    442:             {
                    443:               /* create the Frames element if it does not exist */
                    444:               if (elFrames == NULL)
                    445:                 {
                    446:                   newElType.ElSSchema = docSSchema;
                    447:                   newElType.ElTypeNum = HTML_EL_Frames;
                    448:                   elFrames = TtaNewElement (doc, newElType);
1.63      cvs       449:                   if (DocumentMeta[doc]->xmlformat)
                    450:                     XmlSetElemLineNumber (elFrames);
                    451:                   else
                    452:                     SetHtmlElemLineNumber (elFrames);
1.6       cvs       453:                   TtaInsertSibling (elFrames, child, TRUE, doc);
                    454:                 }
                    455:               /* move the element as the last child of the Frames element */
                    456:               TtaRemoveTree (child, doc);
                    457:               if (lastFrame == NULL)
                    458:                 TtaInsertFirstChild (&child, elFrames, doc);
                    459:               else
                    460:                 TtaInsertSibling (child, lastFrame, FALSE, doc);
                    461:               lastFrame = child;
                    462:             }
                    463:           child = next;
                    464:         }
                    465:        break;
                    466:        
                    467:      case HTML_EL_Input:       /* it's an INPUT without any TYPE attribute */
                    468:        /* Create a child of type Text_Input */
                    469:        elType.ElTypeNum = HTML_EL_Text_Input;
                    470:        child = TtaNewTree (doc, elType, "");
1.63      cvs       471:        if (DocumentMeta[doc]->xmlformat)
                    472:         XmlSetElemLineNumber (child);
                    473:        else
                    474:         SetHtmlElemLineNumber (child);
1.6       cvs       475:        TtaInsertFirstChild (&child, el, doc);
                    476:        /* now, process it like a Text_Input element */
                    477: 
                    478:      case HTML_EL_Text_Input:
                    479:      case HTML_EL_Password_Input:
                    480:      case HTML_EL_File_Input:
                    481:        /* get element Inserted_Text */
                    482:        child = TtaGetFirstChild (el);
                    483:        if (child != NULL)
                    484:         {
                    485:           attrType.AttrSSchema = docSSchema;
                    486:           attrType.AttrTypeNum = HTML_ATTR_Value_;
                    487:           attr = TtaGetAttribute (el, attrType);
                    488:           if (attr != NULL)
                    489:             {
                    490:               /* copy the value of attribute "value" into the first text
                    491:                  leaf of element */
                    492:               length = TtaGetTextAttributeLength (attr);
                    493:               if (length > 0)
                    494:                 {
                    495:                   /* get the text leaf */
                    496:                   leaf = TtaGetFirstChild (child);
                    497:                   if (leaf != NULL)
                    498:                     {
                    499:                       childType = TtaGetElementType (leaf);
                    500:                       if (childType.ElTypeNum == HTML_EL_TEXT_UNIT)
                    501:                         {
                    502:                           /* copy attribute value into the text leaf */
1.37      cvs       503:                           text = TtaGetMemory (length + 1);
1.6       cvs       504:                           TtaGiveTextAttributeValue (attr, text, &length);
                    505:                           TtaSetTextContent (leaf, text, 
                    506:                                              TtaGetDefaultLanguage (), doc);
                    507:                           TtaFreeMemory (text);
                    508:                         }
                    509:                     }
                    510:                 }
                    511:             }
                    512:         }
                    513:        break;
                    514:        
                    515:      case HTML_EL_META:
1.15      cvs       516:        ParseCharset (el, doc);
1.6       cvs       517:        break;
                    518: 
                    519:      case HTML_EL_STYLE_:      /* it's a STYLE element */
1.60      vatton    520:      case HTML_EL_SCRIPT_:     /* it's a SCRIPT element */
1.6       cvs       521:      case HTML_EL_Preformatted:        /* it's a PRE */
                    522:        /* if the last line of the Preformatted is empty, remove it */
                    523:        leaf = XmlLastLeafInElement (el);
                    524:        if (leaf != NULL)
                    525:         {
                    526:           elType = TtaGetElementType (leaf);
                    527:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT)
                    528:             /* the last leaf is a TEXT element */
                    529:             {
                    530:               length = TtaGetTextLength (leaf);
                    531:               if (length > 0)
                    532:                 {
                    533:                   TtaGiveSubString (leaf, lastChar, length, 1);
                    534:                   if (lastChar[0] == EOL)
                    535:                     /* last character is new line, delete it */
                    536:                     {
                    537:                       if (length == 1)
                    538:                         /* empty TEXT element */
                    539:                         TtaDeleteTree (leaf, doc);
                    540:                       else
                    541:                         /* remove the last character */
                    542:                         TtaDeleteTextContent (leaf, length, 1, doc);
                    543:                     }
                    544:                 }
                    545:             }
                    546:         }
1.75      kahan     547:        if (DocumentMeta[doc] && DocumentMeta[doc]->xmlformat)
1.63      cvs       548:         {
                    549:           if (IsXmlParsingCSS ())
                    550:             {
                    551:               text = GetStyleContents (el);
                    552:               if (text)
                    553:                 {
1.71      vatton    554:                   ReadCSSRules (doc, NULL, text, NULL,
1.82    ! quint     555:                                 TtaGetElementLineNumber (el), FALSE, el);
1.63      cvs       556:                   TtaFreeMemory (text);
                    557:                 }
                    558:               SetXmlParsingCSS (FALSE);
                    559:             }
                    560:         }
                    561:        else
1.6       cvs       562:         {
1.63      cvs       563:           if (IsHtmlParsingCSS ())
1.6       cvs       564:             {
1.63      cvs       565:               text = GetStyleContents (el);
                    566:               if (text)
                    567:                 {
1.71      vatton    568:                   ReadCSSRules (doc, NULL, text, NULL,
1.82    ! quint     569:                                 TtaGetElementLineNumber (el), FALSE, el);
1.63      cvs       570:                   TtaFreeMemory (text);
                    571:                 }
                    572:               SetHtmlParsingCSS (FALSE);
1.6       cvs       573:             }
                    574:         }
                    575:        /* and continue as if it were a Preformatted or a Script */
                    576:        break;
                    577:        
                    578:      case HTML_EL_Text_Area:   /* it's a Text_Area */
1.69      cvs       579:        if (DocumentMeta[doc]->xmlformat)
                    580:         SetParsingTextArea (FALSE);
                    581:        else
                    582:         SetHtmlParsingTextArea (FALSE);
1.6       cvs       583:        child = TtaGetFirstChild (el);
                    584:        if (child == NULL)
                    585:         /* it's an empty Text_Area */
                    586:         /* insert a Inserted_Text element in the element */
                    587:         {
                    588:           newElType.ElTypeNum = HTML_EL_Inserted_Text;
                    589:           child = TtaNewTree (doc, newElType, "");
                    590:           TtaInsertFirstChild (&child, el, doc);
                    591:         }
                    592:        else
                    593:         {
                    594:           /* save the text into Default_Value attribute */
                    595:           attrType.AttrSSchema = docSSchema;
                    596:           attrType.AttrTypeNum = HTML_ATTR_Default_Value;
                    597:           if (TtaGetAttribute (el, attrType) == NULL)
                    598:             /* attribute Default_Value is missing */
                    599:             {
                    600:               attr = TtaNewAttribute (attrType);
                    601:               TtaAttachAttribute (el, attr, doc);
                    602:               desc = TtaGetFirstChild (child);
                    603:               length = TtaGetTextLength (desc) + 1;
1.37      cvs       604:               text = TtaGetMemory (length);
1.6       cvs       605:               TtaGiveTextContent (desc, text, &length, &lang);
                    606:               TtaSetAttributeText (attr, text, el, doc);
                    607:               TtaFreeMemory (text);
                    608:             }
                    609:         }
                    610:        /* insert a Frame element */
                    611:        newElType.ElTypeNum = HTML_EL_Frame;
                    612:        constElem = TtaNewTree (doc, newElType, "");
                    613:        TtaInsertSibling (constElem, child, FALSE, doc);
                    614:        break;
                    615:        
                    616:      case HTML_EL_Radio_Input:
                    617:      case HTML_EL_Checkbox_Input:
                    618:        /* put an attribute Checked if it is missing */
                    619:        attrType.AttrSSchema = docSSchema;
                    620:        attrType.AttrTypeNum = HTML_ATTR_Checked;
                    621:        if (TtaGetAttribute (el, attrType) == NULL)
                    622:         /* attribute Checked is missing */
                    623:         {
                    624:           attr = TtaNewAttribute (attrType);
                    625:           TtaAttachAttribute (el, attr, doc);
                    626:           TtaSetAttributeValue (attr, HTML_ATTR_Checked_VAL_No_, el, doc);
                    627:         }
                    628:        break;
                    629:        
                    630:      case HTML_EL_Option_Menu:
                    631:        /* Check that at least one option has a SELECTED attribute */
                    632:        OnlyOneOptionSelected (el, doc, TRUE);
                    633:        break;
                    634: 
                    635:      case HTML_EL_PICTURE_UNIT:
                    636:        break;
                    637:        
                    638:      case HTML_EL_LINK:
                    639:        CheckCSSLink (el, doc, docSSchema);
                    640:        break;
                    641:        
                    642:      case HTML_EL_Data_cell:
                    643:      case HTML_EL_Heading_cell:
                    644:        /* insert a pseudo paragraph into empty cells */
                    645:        child = TtaGetFirstChild (el);
                    646:        if (child == NULL)
                    647:         {
                    648:           elType.ElTypeNum = HTML_EL_Pseudo_paragraph;
                    649:           child = TtaNewTree (doc, elType, "");
                    650:           if (child != NULL)
                    651:               TtaInsertFirstChild (&child, el, doc);
                    652:         }
                    653:        
1.80      cvs       654:        /* detect whether we are parsing a whole table or just a cell */
1.63      cvs       655:        if (DocumentMeta[doc]->xmlformat)
                    656:         {
                    657:           if (IsWithinXmlTable ())
                    658:             NewCell (el, doc, FALSE);
                    659:         }
                    660:        else
                    661:         {
                    662:           if (IsWithinHtmlTable ())
                    663:             NewCell (el, doc, FALSE);
                    664:         }
1.6       cvs       665:        break;
                    666:        
                    667:      case HTML_EL_Table:
                    668:        CheckTable (el, doc);
                    669:        SubWithinTable ();
                    670:        break;
                    671:        
                    672:      case HTML_EL_TITLE:
                    673:        /* show the TITLE in the main window */
                    674:        UpdateTitle (el, doc);
                    675:        break;
1.41      cvs       676: 
                    677:      case HTML_EL_rbc:
                    678:        /* an rbc element has been read. Its parent should be a complex_ruby.
                    679:          Change the type of the parent, as simple_ruby are created by
                    680:          default */
                    681:        parent = TtaGetParent (el);
                    682:        if (parent)
                    683:         {
                    684:           newElType = TtaGetElementType (parent);
                    685:           if (newElType.ElSSchema == elType.ElSSchema &&
                    686:               newElType.ElTypeNum == HTML_EL_simple_ruby)
                    687:              ChangeElementType (parent, HTML_EL_complex_ruby);
                    688:         }
                    689:        break;
                    690: 
                    691:      case HTML_EL_rtc1:
                    692:        /* an rtc element has been parsed. If it has already a rtc1 sibling,
                    693:          change its type to rtc2 */
                    694:        prev = el;
                    695:        do
                    696:         {
                    697:           TtaPreviousSibling(&prev);
                    698:           if (prev)
                    699:             {
                    700:               newElType = TtaGetElementType (prev);
                    701:               if (newElType.ElSSchema == elType.ElSSchema &&
                    702:                   newElType.ElTypeNum == HTML_EL_rtc1)
                    703:                 {
                    704:                   ChangeElementType (el, HTML_EL_rtc2);
                    705:                   prev = NULL;
                    706:                 }
                    707:             }
                    708:         }
                    709:        while (prev);
                    710:        break;
                    711: 
1.6       cvs       712:      default:
                    713:        break;
                    714:      }
                    715: }
1.1       cvs       716: 
                    717: /*----------------------------------------------------------------------
1.30      cvs       718:    PutInContent    
                    719:    Put the string ChrString in the leaf of current element.
                    720:   ----------------------------------------------------------------------*/
1.39      cvs       721: Element         PutInContent (char *ChrString, ParserData *context)
1.30      cvs       722: 
                    723: {
                    724:    Element      el, child;
                    725:    ElementType  elType;
                    726:    int          length;
                    727: 
                    728:    el = NULL;
                    729:    if (context->lastElement != NULL)
                    730:      {
                    731:        /* search first leaf of current element */
                    732:        el = context->lastElement;
                    733:        do
                    734:          {
                    735:             child = TtaGetFirstChild (el);
                    736:             if (child != NULL)
                    737:                el = child;
                    738:          }
                    739:        while (child != NULL);
                    740:        elType = TtaGetElementType (el);
                    741:        length = 0;
                    742:        if (elType.ElTypeNum == 1)
                    743:           length = TtaGetTextLength (el);
                    744:        if (length == 0)
                    745:           TtaSetTextContent (el, ChrString, context->language, context->doc);
                    746:        else
                    747:           TtaAppendTextContent (el, ChrString, context->doc);
                    748:      }
                    749:    return el;
                    750: }
                    751: 
                    752: /*----------------------------------------------------------------------
1.51      cvs       753:    UnknownXhtmlNameSpace
1.65      cvs       754:    The element doesn't belong to a supported namespace
1.51      cvs       755:   ----------------------------------------------------------------------*/
1.65      cvs       756: void               UnknownXhtmlNameSpace (ParserData *context,
                    757:                                          Element *unknownEl,
                    758:                                          char* content)
1.51      cvs       759: {
                    760:    ElementType     elType;
1.65      cvs       761:    Element         elText;
1.51      cvs       762: 
                    763:    /* Create a new Invalid_element */
                    764:    elType.ElSSchema = GetXMLSSchema (XHTML_TYPE, context->doc);
                    765:    elType.ElTypeNum = HTML_EL_Unknown_namespace;
1.65      cvs       766:    *unknownEl = TtaNewElement (context->doc, elType);
                    767:    if (*unknownEl != NULL)
1.51      cvs       768:      {
1.65      cvs       769:        XmlSetElemLineNumber (*unknownEl);
                    770:        InsertXmlElement (unknownEl);
1.51      cvs       771:        context->lastElementClosed = TRUE;
                    772:        elType.ElTypeNum = HTML_EL_TEXT_UNIT;
                    773:        elText = TtaNewElement (context->doc, elType);
                    774:        XmlSetElemLineNumber (elText);
1.65      cvs       775:        TtaInsertFirstChild (&elText, *unknownEl, context->doc);
1.51      cvs       776:        TtaSetTextContent (elText, content, context->language, context->doc);
                    777:        TtaSetAccessRight (elText, ReadOnly, context->doc);
                    778:      }
                    779: }
                    780: 
                    781: /*----------------------------------------------------------------------
1.30      cvs       782:    CreateHTMLAttribute
                    783:    create an attribute of type attrType for the element el.
                    784:   ----------------------------------------------------------------------*/
                    785: void           CreateHTMLAttribute (Element       el,
                    786:                                    AttributeType attrType,
1.46      cvs       787:                                    char*         text,
1.30      cvs       788:                                    ThotBool      isInvalid,
                    789:                                    Document      doc,
                    790:                                    Attribute    *lastAttribute,
                    791:                                    Element      *lastAttrElement)
                    792:      
                    793: {
                    794:    int         attrKind;
                    795:    int         length;
1.39      cvs       796:    char       *buffer;
1.30      cvs       797:    Attribute   attr, oldAttr;
                    798: 
                    799:    if (attrType.AttrTypeNum != 0)
                    800:      {
                    801:        oldAttr = TtaGetAttribute (el, attrType);
                    802:        if (oldAttr != NULL)
                    803:         /* this attribute already exists */
                    804:         attr = oldAttr;
                    805:        else
                    806:         /* create a new attribute and attach it to the element */
                    807:         {
                    808:           attr = TtaNewAttribute (attrType);
                    809:           TtaAttachAttribute (el, attr, doc);
                    810:         }
                    811:        *lastAttribute = attr;
                    812:        *lastAttrElement = el;
                    813: 
                    814:        TtaGiveAttributeType (attr, &attrType, &attrKind);
                    815:        if (attrKind == 0)      /* enumerate */
                    816:         TtaSetAttributeValue (attr, 1, el, doc);
                    817: 
                    818:        /* attribute BORDER without any value (ThotBool attribute) is */
                    819:        /* considered as BORDER=1 */
                    820:        if (attrType.AttrTypeNum == HTML_ATTR_Border)
                    821:         TtaSetAttributeValue (attr, 1, el, doc);
                    822: 
                    823:        if (isInvalid)
                    824:         /* Copy the name of the invalid attribute as the content */
                    825:         /* of the Invalid_attribute attribute. */
                    826:         {
1.37      cvs       827:           length = strlen (text) + 2;
1.30      cvs       828:           length += TtaGetTextAttributeLength (attr);
1.37      cvs       829:           buffer = TtaGetMemory (length + 1);
1.30      cvs       830:           TtaGiveTextAttributeValue (attr, buffer, &length);
1.37      cvs       831:           strcat (buffer, " ");
                    832:           strcat (buffer, text);
1.30      cvs       833:           TtaSetAttributeText (attr, buffer, el, doc);
                    834:           TtaFreeMemory (buffer);
                    835:         }
                    836:      }
                    837: }
                    838: 
                    839: /*----------------------------------------------------------------------
                    840:    HTMLTypeAttrValue
                    841:    Value val has been read for the HTML attribute TYPE.
                    842:    Create a child for the current Thot element INPUT accordingly.
                    843:   ----------------------------------------------------------------------*/
1.46      cvs       844: void               HTMLTypeAttrValue (char       *val,
1.30      cvs       845:                                      Attribute   lastAttribute,
                    846:                                      Element     lastAttrElement,
                    847:                                      ParserData *context)
                    848: 
                    849: {
                    850:   ElementType      elType;
                    851:   Element          newChild;
                    852:   AttributeType    attrType;
                    853:   Attribute        attr;
1.39      cvs       854:   char             msgBuffer[MaxMsgLength];
1.30      cvs       855:   int              value;
                    856:   int              numberOfLinesRead;
                    857: 
                    858:   value = MapAttrValue (DummyAttribute, val);
                    859:   if (value < 0)
                    860:     {
1.37      cvs       861:       if (strlen (val) > MaxMsgLength - 40)
                    862:          val[MaxMsgLength - 40] = EOS;
                    863:       sprintf (msgBuffer, "Unknown attribute value \"type = %s\"", val);
1.42      cvs       864:       HTMLParseError (context->doc, msgBuffer);
1.30      cvs       865:       attrType.AttrSSchema = TtaGetDocumentSSchema (context->doc);
                    866:       attrType.AttrTypeNum = pHTMLAttributeMapping[0].ThotAttribute;
1.37      cvs       867:       sprintf (msgBuffer, "type=%s", val);
1.30      cvs       868:       CreateHTMLAttribute (context->lastElement, attrType, msgBuffer, TRUE,
                    869:                           context->doc, &lastAttribute, &lastAttrElement);
                    870:     }
                    871:   else
                    872:     {
                    873:       elType = TtaGetElementType (context->lastElement);
                    874:       if (elType.ElTypeNum != HTML_EL_Input)
                    875:        {
1.37      cvs       876:          if (strlen (val) > MaxMsgLength - 40)
                    877:            val[MaxMsgLength - 40] = EOS;
                    878:          sprintf (msgBuffer, "Duplicate attribute \"type = %s\"", val);
1.30      cvs       879:        }
                    880:       else
                    881:        {
                    882:          elType.ElSSchema = TtaGetDocumentSSchema (context->doc);
                    883:          elType.ElTypeNum = value;
                    884:          newChild = TtaNewTree (context->doc, elType, "");
                    885:          numberOfLinesRead = 0;
                    886:          TtaSetElementLineNumber (newChild, numberOfLinesRead);
                    887:          TtaInsertFirstChild (&newChild, context->lastElement, context->doc);
                    888:          if (value == HTML_EL_PICTURE_UNIT)
                    889:            {
                    890:              /* add the attribute IsInput to input pictures */
                    891:              attrType.AttrSSchema = elType.ElSSchema;
                    892:              attrType.AttrTypeNum = HTML_ATTR_IsInput;
                    893:              attr = TtaNewAttribute (attrType);
                    894:              TtaAttachAttribute (newChild, attr, context->doc);
                    895:            }
                    896:        }
                    897:     }
                    898: }
                    899: 
                    900: /*----------------------------------------------------------------------
                    901:    XhtmlTypeAttrValue 
                    902:    Value val has been read for the HTML attribute TYPE.
                    903:    Create a child for the current Thot element INPUT accordingly.
                    904:   ----------------------------------------------------------------------*/
1.46      cvs       905: void              XhtmlTypeAttrValue (char       *val,
1.30      cvs       906:                                      Attribute   currentAttribute,
                    907:                                      Element     lastAttrElement,
                    908:                                      ParserData *context)
                    909: 
                    910: {
                    911:   ElementType     elType;
                    912:   Element         newChild;
                    913:   AttributeType   attrType;
                    914:   Attribute       attr;
1.39      cvs       915:   char            msgBuffer[MaxMsgLength];
1.30      cvs       916:   int             value;
                    917:   ThotBool        level;
                    918: 
                    919:   attrType.AttrTypeNum = DummyAttribute;
                    920:   MapHTMLAttributeValue (val, attrType, &value);
                    921:   if (value < 0)
                    922:     {
1.37      cvs       923:       sprintf (msgBuffer, "Unknown attribute value \"type=%s\"", val);
1.30      cvs       924:       XmlParseError (errorParsing, msgBuffer, 0);
1.36      cvs       925:       MapHTMLAttribute ("unknown_attr", &attrType, NULL,
1.30      cvs       926:                        &level, context->doc);
1.37      cvs       927:       sprintf (msgBuffer, "type=%s", val);
1.30      cvs       928:       CreateHTMLAttribute (context->lastElement, attrType, msgBuffer, TRUE,
                    929:                           context->doc, &currentAttribute, &lastAttrElement);
                    930:     }
                    931:   else
                    932:     {
                    933:       elType = TtaGetElementType (context->lastElement);
                    934:       if (elType.ElTypeNum != HTML_EL_Input)
                    935:        {
1.37      cvs       936:          sprintf (msgBuffer, "Duplicate attribute \"type = %s\"", val);
1.30      cvs       937:          XmlParseError (errorParsing, msgBuffer, 0);
                    938:        }
                    939:       else
                    940:        {
                    941:          elType.ElTypeNum = value;
                    942:          newChild = TtaNewTree (context->doc, elType, "");
                    943:          XmlSetElemLineNumber (newChild);
                    944:          TtaInsertFirstChild (&newChild, context->lastElement, context->doc);
                    945:          if (value == HTML_EL_PICTURE_UNIT)
                    946:            {
                    947:              /* add the attribute IsInput to input pictures */
                    948:              attrType.AttrSSchema = elType.ElSSchema;
                    949:              attrType.AttrTypeNum = HTML_ATTR_IsInput;
                    950:              attr = TtaNewAttribute (attrType);
                    951:              TtaAttachAttribute (newChild, attr, context->doc);
                    952:            }
                    953:        }
                    954:     }
                    955: }
                    956: 
                    957: /*----------------------------------------------------------------------
                    958:    CreateAttrWidthPercentPxl
1.79      quint     959:    an HTML attribute "width" has been created for a Table, an image,
                    960:    an Object of a HR.
1.30      cvs       961:    Create the corresponding attribute IntWidthPercent or IntWidthPxl.
                    962:    oldWidth is -1 or the old image width.
                    963:   ----------------------------------------------------------------------*/
1.58      vatton    964: void CreateAttrWidthPercentPxl (char *buffer, Element el,
                    965:                                Document doc, int oldWidth)
1.30      cvs       966: {
                    967:   AttributeType   attrTypePxl, attrTypePercent;
                    968:   Attribute       attrOld, attrNew;
                    969:   int             length, val;
1.39      cvs       970:   char            msgBuffer[MaxMsgLength];
1.79      quint     971:   ElementType    elType, childType;
                    972:   Element         origEl, child;
1.30      cvs       973:   int             w, h;
                    974:   ThotBool        isImage;
                    975: 
1.79      quint     976:   origEl = el;
1.30      cvs       977:   elType = TtaGetElementType (el);
                    978:   isImage = (elType.ElTypeNum == HTML_EL_PICTURE_UNIT ||
                    979:             elType.ElTypeNum == HTML_EL_Data_cell ||
1.79      quint     980:             elType.ElTypeNum == HTML_EL_Heading_cell ||
                    981:             elType.ElTypeNum == HTML_EL_Object);
                    982:   if (elType.ElTypeNum == HTML_EL_Object)
                    983:     /* the width attribute is attached to an Object element */
                    984:     {
                    985:       child = TtaGetFirstChild (el);
                    986:       if (child)
                    987:        {
                    988:          childType = TtaGetElementType (child);
                    989:          if (childType.ElTypeNum == HTML_EL_PICTURE_UNIT)
                    990:            /* the Object element is of type image. apply the width
                    991:               attribute to the actual image element */
                    992:            el = child;
                    993:        }
                    994:     }
1.30      cvs       995:   /* remove trailing spaces */
1.37      cvs       996:   length = strlen (buffer) - 1;
1.30      cvs       997:   while (length > 0 && buffer[length] <= SPACE)
                    998:     length--;
                    999:   attrTypePxl.AttrSSchema = TtaGetDocumentSSchema (doc);
                   1000:   attrTypePercent.AttrSSchema = TtaGetDocumentSSchema (doc);
                   1001:   attrTypePxl.AttrTypeNum = HTML_ATTR_IntWidthPxl;
                   1002:   attrTypePercent.AttrTypeNum = HTML_ATTR_IntWidthPercent;
                   1003:   /* is the last character a '%' ? */
                   1004:   if (buffer[length] == '%')
                   1005:     {
                   1006:       /* remove IntWidthPxl */
                   1007:       attrOld = TtaGetAttribute (el, attrTypePxl);
                   1008:       /* update IntWidthPercent */
                   1009:       attrNew = TtaGetAttribute (el, attrTypePercent);
                   1010:       if (attrNew == NULL)
                   1011:        {
                   1012:          attrNew = TtaNewAttribute (attrTypePercent);
                   1013:          TtaAttachAttribute (el, attrNew, doc);
                   1014:        }
                   1015:       else if (isImage && oldWidth == -1)
                   1016:        {
                   1017:          if (attrOld == NULL)
                   1018:            oldWidth = TtaGetAttributeValue (attrNew);
                   1019:          else
                   1020:            oldWidth = TtaGetAttributeValue (attrOld);
                   1021:        }
                   1022:     }
                   1023:   else
                   1024:     {
                   1025:       /* remove IntWidthPercent */
                   1026:       attrOld = TtaGetAttribute (el, attrTypePercent);
                   1027:       /* update IntWidthPxl */
                   1028:       attrNew = TtaGetAttribute (el, attrTypePxl);
                   1029:       if (attrNew == NULL)
                   1030:        {
                   1031:          attrNew = TtaNewAttribute (attrTypePxl);
                   1032:          TtaAttachAttribute (el, attrNew, doc);
                   1033:        }
                   1034:       else if (isImage && oldWidth == -1)
                   1035:        {
                   1036:          TtaGiveBoxSize (el, doc, 1, UnPixel, &w, &h);
                   1037:          if (attrOld == NULL)
                   1038:            oldWidth = w * TtaGetAttributeValue (attrNew) / 100;
                   1039:          else
                   1040:            oldWidth = w * TtaGetAttributeValue (attrOld) / 100;          
                   1041:        }
                   1042:     }
                   1043: 
                   1044:   if (attrOld != NULL)
                   1045:     TtaRemoveAttribute (el, attrOld, doc);
1.43      cvs      1046:   if (sscanf (buffer, "%d", &val))
1.30      cvs      1047:     TtaSetAttributeValue (attrNew, val, el, doc);
                   1048:   else
                   1049:     /* its not a number. Delete attribute and send an error message */
                   1050:     {
                   1051:     TtaRemoveAttribute (el, attrNew, doc);
1.37      cvs      1052:     if (strlen (buffer) > MaxMsgLength - 30)
1.30      cvs      1053:         buffer[MaxMsgLength - 30] = EOS;
1.37      cvs      1054:     sprintf (msgBuffer, "Invalid attribute value \"%s\"", buffer);
1.42      cvs      1055:     HTMLParseError (doc, msgBuffer);
1.30      cvs      1056:     }
                   1057:   if (isImage)
1.79      quint    1058:     UpdateImageMap (origEl, doc, oldWidth, -1);
1.30      cvs      1059: }
                   1060: 
                   1061: /*----------------------------------------------------------------------
1.58      vatton   1062:    CreateAttrHeightPercentPxl
1.79      quint    1063:    an HTML attribute "width" has been created for a Table, an image,
                   1064:    an Object or a HR.
1.58      vatton   1065:    Create the corresponding attribute IntHeightPercent or IntHeightPxl.
                   1066:    oldHeight is -1 or the old image width.
                   1067:   ----------------------------------------------------------------------*/
                   1068: void CreateAttrHeightPercentPxl (char *buffer, Element el,
                   1069:                                Document doc, int oldHeight)
                   1070: {
                   1071:   AttributeType   attrTypePxl, attrTypePercent;
                   1072:   Attribute       attrOld, attrNew;
                   1073:   int             length, val;
                   1074:   char            msgBuffer[MaxMsgLength];
1.79      quint    1075:   ElementType    elType, childType;
                   1076:   Element         origEl, child;
1.58      vatton   1077:   int             w, h;
                   1078:   ThotBool        isImage;
                   1079: 
1.79      quint    1080:   origEl = el;
1.58      vatton   1081:   elType = TtaGetElementType (el);
                   1082:   isImage = (elType.ElTypeNum == HTML_EL_PICTURE_UNIT ||
                   1083:             elType.ElTypeNum == HTML_EL_Data_cell ||
1.79      quint    1084:             elType.ElTypeNum == HTML_EL_Heading_cell ||
                   1085:             elType.ElTypeNum == HTML_EL_Object);
                   1086:   if (elType.ElTypeNum == HTML_EL_Object)
                   1087:     /* the height attribute is attached to an Object element */
                   1088:     {
                   1089:       child = TtaGetFirstChild (el);
1.81      cvs      1090:       if (!child)
                   1091:        return;
                   1092:       else
1.79      quint    1093:        {
                   1094:          childType = TtaGetElementType (child);
                   1095:          if (childType.ElTypeNum == HTML_EL_PICTURE_UNIT)
                   1096:            /* the Object element is of type image. apply the width
                   1097:               attribute to the actual image element */
                   1098:            el = child;
1.81      cvs      1099:          else
                   1100:            return;
1.79      quint    1101:        }
                   1102:     }
1.58      vatton   1103:   /* remove trailing spaces */
                   1104:   length = strlen (buffer) - 1;
                   1105:   while (length > 0 && buffer[length] <= SPACE)
                   1106:     length--;
                   1107:   attrTypePxl.AttrSSchema = TtaGetDocumentSSchema (doc);
                   1108:   attrTypePercent.AttrSSchema = TtaGetDocumentSSchema (doc);
                   1109:   attrTypePxl.AttrTypeNum = HTML_ATTR_IntHeightPxl;
                   1110:   attrTypePercent.AttrTypeNum = HTML_ATTR_IntHeightPercent;
                   1111:   /* is the last character a '%' ? */
                   1112:   if (buffer[length] == '%')
                   1113:     {
                   1114:       /* remove IntHeightPxl */
                   1115:       attrOld = TtaGetAttribute (el, attrTypePxl);
                   1116:       /* update IntHeightPercent */
                   1117:       attrNew = TtaGetAttribute (el, attrTypePercent);
                   1118:       if (attrNew == NULL)
                   1119:        {
                   1120:          attrNew = TtaNewAttribute (attrTypePercent);
                   1121:          TtaAttachAttribute (el, attrNew, doc);
                   1122:        }
                   1123:       else if (isImage && oldHeight == -1)
                   1124:        {
                   1125:          if (attrOld == NULL)
                   1126:            oldHeight = TtaGetAttributeValue (attrNew);
                   1127:          else
                   1128:            oldHeight = TtaGetAttributeValue (attrOld);
                   1129:        }
                   1130:     }
                   1131:   else
                   1132:     {
                   1133:       /* remove IntHeightPercent */
                   1134:       attrOld = TtaGetAttribute (el, attrTypePercent);
                   1135:       /* update IntHeightPxl */
                   1136:       attrNew = TtaGetAttribute (el, attrTypePxl);
                   1137:       if (attrNew == NULL)
                   1138:        {
                   1139:          attrNew = TtaNewAttribute (attrTypePxl);
                   1140:          TtaAttachAttribute (el, attrNew, doc);
                   1141:        }
                   1142:       else if (isImage && oldHeight == -1)
                   1143:        {
                   1144:          TtaGiveBoxSize (el, doc, 1, UnPixel, &w, &h);
                   1145:          if (attrOld == NULL)
                   1146:            oldHeight = w * TtaGetAttributeValue (attrNew) / 100;
                   1147:          else
                   1148:            oldHeight = w * TtaGetAttributeValue (attrOld) / 100;         
                   1149:        }
                   1150:     }
                   1151: 
                   1152:   if (attrOld != NULL)
                   1153:     TtaRemoveAttribute (el, attrOld, doc);
                   1154:   if (sscanf (buffer, "%d", &val))
                   1155:     TtaSetAttributeValue (attrNew, val, el, doc);
                   1156:   else
                   1157:     /* its not a number. Delete attribute and send an error message */
                   1158:     {
                   1159:     TtaRemoveAttribute (el, attrNew, doc);
                   1160:     if (strlen (buffer) > MaxMsgLength - 30)
                   1161:         buffer[MaxMsgLength - 30] = EOS;
                   1162:     sprintf (msgBuffer, "Invalid attribute value \"%s\"", buffer);
                   1163:     HTMLParseError (doc, msgBuffer);
                   1164:     }
                   1165:   if (isImage)
1.79      quint    1166:     UpdateImageMap (origEl, doc, oldHeight, -1);
1.58      vatton   1167: }
                   1168: 
                   1169: /*----------------------------------------------------------------------
1.30      cvs      1170:    CreateAttrIntSize
                   1171:    an HTML attribute "size" has been created for a Font element.
                   1172:    Create the corresponding internal attribute.
                   1173:   ----------------------------------------------------------------------*/
1.46      cvs      1174: void              CreateAttrIntSize (char *buffer, Element el, Document doc)
1.30      cvs      1175: 
                   1176: {
                   1177:    AttributeType  attrType;
                   1178:    int            val, ind, factor, delta;
                   1179:    Attribute      attr;
1.37      cvs      1180:    char         msgBuffer[MaxMsgLength];
1.30      cvs      1181: 
                   1182:    /* is the first character a '+' or a '-' ? */
                   1183:    ind = 0;
                   1184:    factor = 1;
                   1185:    delta = 0;
                   1186:    if (buffer[0] == '+')
                   1187:      {
                   1188:        attrType.AttrTypeNum = HTML_ATTR_IntSizeIncr;
                   1189:        ind++;
                   1190:        factor = 2;
                   1191:      }
                   1192:    else if (buffer[0] == '-')
                   1193:      {
                   1194:        attrType.AttrTypeNum = HTML_ATTR_IntSizeDecr;
                   1195:        ind++;
                   1196:        factor = 2;
                   1197:      }
                   1198:    else
                   1199:      {
                   1200:        attrType.AttrTypeNum = HTML_ATTR_IntSizeRel;
                   1201:        delta = 1;
                   1202:      }
                   1203:    attrType.AttrSSchema = TtaGetDocumentSSchema (doc);
                   1204:    attr = TtaGetAttribute (el, attrType);
1.43      cvs      1205:    if (sscanf (&buffer[ind], "%d", &val))
1.30      cvs      1206:      {
                   1207:        val = val * factor + delta;
                   1208:        if (attr == NULL)
                   1209:         {
                   1210:           /* this attribute doesn't exist, create it */
                   1211:           attr = TtaNewAttribute (attrType);
                   1212:           TtaAttachAttribute (el, attr, doc);
                   1213:         }
                   1214:        TtaSetAttributeValue (attr, val, el, doc);
                   1215:      }
                   1216:    else
                   1217:      /* its not a number. Delete attribute and send an error message */
                   1218:      {
                   1219:        if (attr)
                   1220:          TtaRemoveAttribute (el, attr, doc);
1.37      cvs      1221:        if (strlen (buffer) > MaxMsgLength - 30)
1.30      cvs      1222:          buffer[MaxMsgLength - 30] = EOS;
1.37      cvs      1223:        sprintf (msgBuffer, "Invalid attribute value \"%s\"", buffer);
1.42      cvs      1224:        HTMLParseError (doc, msgBuffer);
1.30      cvs      1225:      }
                   1226: }
                   1227: /*----------------------------------------------------------------------
                   1228:    EndOfHTMLAttributeValue
                   1229:    Filling of an HTML attribute value
                   1230:   ----------------------------------------------------------------------*/
1.48      vatton   1231: void EndOfHTMLAttributeValue (char *attrValue,
                   1232:                              AttributeMapping *lastMappedAttr,
                   1233:                              Attribute currentAttribute,
                   1234:                              Element lastAttrElement,
                   1235:                              ThotBool UnknownAttr,
                   1236:                              ParserData *context,
                   1237:                              ThotBool isXML)
1.30      cvs      1238: {
1.48      vatton   1239:   AttributeType   attrType, attrType1;
                   1240:   Attribute       attr;
1.79      quint    1241:   ElementType    elType;
1.48      vatton   1242:   Element         child, root;
                   1243:   Language        lang;
                   1244:   char            translation;
                   1245:   char            shape;
                   1246:   char           *buffer;
                   1247:   char           *attrName;
                   1248:   char            msgBuffer[MaxMsgLength];
                   1249:   int             val;
                   1250:   int             length;
                   1251:   int             attrKind;
                   1252:   ThotBool        done = FALSE;
1.30      cvs      1253: 
1.48      vatton   1254:   /* treatments of some particular HTML attributes */
                   1255:   if (!strcmp (lastMappedAttr->XMLattribute, "style"))
                   1256:     {
                   1257:       TtaSetAttributeText (currentAttribute, attrValue,
                   1258:                           lastAttrElement, context->doc);
                   1259:       ParseHTMLSpecificStyle (context->lastElement, attrValue,
1.61      quint    1260:                              context->doc, 100, FALSE);
1.48      vatton   1261:       done = TRUE;
                   1262:     }
                   1263:   else
                   1264:     {
                   1265:       if (!strcmp (lastMappedAttr->XMLattribute, "link"))
                   1266:        HTMLSetAlinkColor (context->doc, attrValue);
                   1267:       else if (!strcmp (lastMappedAttr->XMLattribute, "alink"))
                   1268:        HTMLSetAactiveColor (context->doc, attrValue);
                   1269:       else if (!strcmp (lastMappedAttr->XMLattribute, "vlink"))
                   1270:        HTMLSetAvisitedColor (context->doc, attrValue);
                   1271:     }
1.30      cvs      1272: 
1.48      vatton   1273:   if (!done)
                   1274:     {
                   1275:       val = 0;
                   1276:       translation = lastMappedAttr->AttrOrContent;
                   1277:       switch (translation)
                   1278:        {
                   1279:        case 'C':       /* Content */
                   1280:          child = PutInContent (attrValue, context);
                   1281:          if (child != NULL)
                   1282:            TtaAppendTextContent (child, "\" ", context->doc);
                   1283:          break;
                   1284:          
                   1285:        case 'A':
                   1286:          if (currentAttribute != NULL)
                   1287:            {
                   1288:              TtaGiveAttributeType (currentAttribute, &attrType, &attrKind);
                   1289:              switch (attrKind)
                   1290:                {
                   1291:                case 0: /* enumerate */
                   1292:                  if (isXML)
                   1293:                    MapHTMLAttributeValue (attrValue, attrType, &val);
                   1294:                  else
                   1295:                    val = MapAttrValue (lastMappedAttr->ThotAttribute,
1.40      cvs      1296:                                        attrValue);
1.48      vatton   1297:                  if (val < 0)
                   1298:                    {
                   1299:                      TtaGiveAttributeType (currentAttribute,
                   1300:                                            &attrType, &attrKind);
                   1301:                      attrName = TtaGetAttributeName (attrType);
                   1302:                      sprintf (msgBuffer,
1.51      cvs      1303:                               "Invalid attribute value \"%s = %s\"",
1.48      vatton   1304:                               attrName, attrValue);
                   1305:                      if (isXML)
                   1306:                        XmlParseError (errorParsing, msgBuffer, 0);
                   1307:                      else
1.51      cvs      1308:                        {
                   1309:                          HTMLParseError (context->doc, msgBuffer);
1.48      vatton   1310:                      /* remove the attribute and replace it by an */
1.51      cvs      1311:                      /* Invalid_attribute (not for XHTML) */
                   1312:                          TtaRemoveAttribute (lastAttrElement,
                   1313:                                              currentAttribute, context->doc);
1.48      vatton   1314:                          attrType.AttrSSchema = 
                   1315:                            TtaGetDocumentSSchema (context->doc);
                   1316:                          attrType.AttrTypeNum =
                   1317:                            pHTMLAttributeMapping[0].ThotAttribute;
1.51      cvs      1318:                          sprintf (msgBuffer, "%s=%s", attrName, attrValue);
                   1319:                          CreateHTMLAttribute (lastAttrElement, attrType,
                   1320:                                               msgBuffer, TRUE, context->doc,
                   1321:                                               &currentAttribute,
                   1322:                                               &lastAttrElement);
1.48      vatton   1323:                        }
                   1324:                    }
                   1325:                  else
                   1326:                    TtaSetAttributeValue (currentAttribute, val,
                   1327:                                          lastAttrElement, context->doc);
                   1328:                  break;
                   1329:                case 1: /* integer */
                   1330:                  if (attrType.AttrTypeNum == HTML_ATTR_Border &&
                   1331:                      !strcasecmp (attrValue, "border"))
                   1332:                    {
                   1333:                      /* border="border" for a table */
                   1334:                      val = 1;
                   1335:                      TtaSetAttributeValue (currentAttribute, val,
1.30      cvs      1336:                                            lastAttrElement, context->doc);
1.48      vatton   1337:                    }
                   1338:                  else if (sscanf (attrValue, "%d", &val))
                   1339:                    TtaSetAttributeValue (currentAttribute, val,
                   1340:                                          lastAttrElement, context->doc);
                   1341:                  else
                   1342:                    {
                   1343:                      TtaRemoveAttribute (lastAttrElement, currentAttribute,
                   1344:                                          context->doc);
                   1345:                      sprintf (msgBuffer,
                   1346:                               "Unknown attribute value \"%s\"",
                   1347:                               attrValue);
                   1348:                      if (isXML)
                   1349:                        XmlParseError (errorParsing, msgBuffer, 0);
                   1350:                      else
                   1351:                        HTMLParseError (context->doc, msgBuffer);
                   1352:                    }
                   1353:                  break;
                   1354:                case 2: /* text */
                   1355:                  if (!UnknownAttr)
                   1356:                    {
                   1357:                      TtaSetAttributeText (currentAttribute, attrValue,
                   1358:                                           lastAttrElement, context->doc);
1.55      cvs      1359:                      if (attrType.AttrTypeNum == HTML_ATTR_Language)
1.48      vatton   1360:                        {
                   1361:                          /* it's a LANG attribute value */
                   1362:                          lang = TtaGetLanguageIdFromName (attrValue);
1.70      vatton   1363:                          if (lang < 0)
1.48      vatton   1364:                            {
                   1365:                              sprintf (msgBuffer,
                   1366:                                       "warning - unsupported language: %s",
                   1367:                                       attrValue);
                   1368:                              if (isXML)
                   1369:                                XmlParseError (errorParsing, msgBuffer, 0);
                   1370:                              else
                   1371:                                HTMLParseError (context->doc, msgBuffer);
                   1372:                            }
                   1373:                          else
                   1374:                            {
                   1375:                              /* change current language */
                   1376:                              context->language = lang;
                   1377:                              if (isXML)
                   1378:                                SetLanguagInXmlStack (lang);
                   1379:                              else
                   1380:                                SetLanguagInHTMLStack (lang);
                   1381:                            }
                   1382:                          root = TtaGetRootElement (context->doc);
                   1383:                          if (lastAttrElement == root)
                   1384:                            /* it's a LANG attribute on the root element */
                   1385:                            /* set the RealLang attribute */
                   1386:                            {
                   1387:                              attrType1.AttrSSchema = TtaGetDocumentSSchema (context->doc);
                   1388:                              attrType1.AttrTypeNum = HTML_ATTR_RealLang;
                   1389:                              /* this attribute could be already present,
                   1390:                                 (lang and xml:lang attributes) */
                   1391:                              if (!TtaGetAttribute (lastAttrElement,
                   1392:                                                    attrType1))
                   1393:                                /* it's not present. Add it */
                   1394:                                {
                   1395:                                  attr = TtaNewAttribute (attrType1);
                   1396:                                  TtaAttachAttribute (lastAttrElement,
                   1397:                                                      attr, context->doc);
                   1398:                                  TtaSetAttributeValue (attr,
                   1399:                                                        HTML_ATTR_RealLang_VAL_Yes_,
                   1400:                                                        lastAttrElement,
                   1401:                                                        context->doc);
                   1402:                                }
                   1403:                            }
                   1404:                        }
                   1405:                      else if (attrType.AttrTypeNum == HTML_ATTR_accesskey)
                   1406:                        TtaAddAccessKey (context->doc, (unsigned int)attrValue[0],
                   1407:                                         lastAttrElement);
                   1408:                    }
                   1409:                  else
                   1410:                    {
1.51      cvs      1411:                      /* this is the content of an invalid attribute */
                   1412:                      /* append it to the current Invalid_attribute */
                   1413:                      if (!isXML)
                   1414:                        {
                   1415:                          length = strlen (attrValue) + 2;
                   1416:                          length += TtaGetTextAttributeLength (currentAttribute);
                   1417:                          buffer = TtaGetMemory (length + 1);
                   1418:                          TtaGiveTextAttributeValue (currentAttribute,
                   1419:                                                     buffer, &length);
                   1420:                          strcat (buffer, "=");
                   1421:                          strcat (buffer, attrValue);
                   1422:                          TtaSetAttributeText (currentAttribute, buffer,
                   1423:                                               lastAttrElement, context->doc);
                   1424:                          TtaFreeMemory (buffer);
                   1425:                        }
1.48      vatton   1426:                    }
                   1427:                  break;
                   1428:                case 3: /* reference */
                   1429:                  break;
                   1430:                }
                   1431:            }
                   1432:          break;
                   1433:          
                   1434:        case SPACE:
                   1435:          if (isXML)
                   1436:            XhtmlTypeAttrValue (attrValue, currentAttribute,
1.30      cvs      1437:                                lastAttrElement, context);
1.48      vatton   1438:          else
                   1439:            HTMLTypeAttrValue (attrValue, currentAttribute,
                   1440:                               lastAttrElement, context);
                   1441:          break;
                   1442:          
1.30      cvs      1443:         default:
                   1444:           break;
1.48      vatton   1445:        }
1.30      cvs      1446: 
                   1447:       if (lastMappedAttr->ThotAttribute == HTML_ATTR_Width__)
1.79      quint    1448:        /* HTML attribute "width" */
                   1449:        {
                   1450:          /* if it's an Object element, wait until all attributes are handled,
                   1451:             especially the data attribute that may generate the image to
                   1452:             which the width has to be applied */
                   1453:          elType = TtaGetElementType (lastAttrElement);
                   1454:          if (elType.ElTypeNum != HTML_EL_Object)
                   1455:            /* create the corresponding attribute IntWidthPercent or */
                   1456:            /* IntWidthPxl */
                   1457:            CreateAttrWidthPercentPxl (attrValue, lastAttrElement,
                   1458:                                       context->doc, -1);
                   1459:        }
1.58      vatton   1460:       else if (lastMappedAttr->ThotAttribute == HTML_ATTR_Height_)
1.79      quint    1461:        /* HTML attribute "height" */
                   1462:        {
                   1463:          /* if it's an Object element, wait until all attributes are handled,
                   1464:             especially the data attribute that may generate the image to
                   1465:             which the height has to be applied */
                   1466:          elType = TtaGetElementType (lastAttrElement);
                   1467:          if (elType.ElTypeNum != HTML_EL_Object)
                   1468:            /* create the corresponding attribute IntHeightPercent or */
                   1469:            /* IntHeightPxl */
                   1470:            CreateAttrHeightPercentPxl (attrValue, lastAttrElement,
                   1471:                                        context->doc, -1);
                   1472:        }
1.48      vatton   1473:       else if (!strcmp (lastMappedAttr->XMLattribute, "size"))
                   1474:        {
                   1475:          TtaGiveAttributeType (currentAttribute, &attrType, &attrKind);
                   1476:          if (attrType.AttrTypeNum == HTML_ATTR_Font_size)
                   1477:            CreateAttrIntSize (attrValue, lastAttrElement, context->doc);
                   1478:        }
                   1479:       else if (!strcmp (lastMappedAttr->XMLattribute, "shape"))
                   1480:        {
                   1481:          child = TtaGetFirstChild (lastAttrElement);
                   1482:          if (child != NULL)
                   1483:            {
                   1484:              switch (val)
                   1485:                {
                   1486:                case HTML_ATTR_shape_VAL_rectangle:
                   1487:                  shape = 'R';
                   1488:                  break;
                   1489:                case HTML_ATTR_shape_VAL_circle:
                   1490:                  shape = 'a';
                   1491:                  break;
                   1492:                case HTML_ATTR_shape_VAL_polygon:
                   1493:                  shape = 'p';
                   1494:                  break;
                   1495:                default:
                   1496:                  shape = SPACE;
                   1497:                  break;
                   1498:                }
                   1499:              TtaSetGraphicsShape (child, shape, context->doc);
                   1500:            }
                   1501:        }
                   1502:       else if (!strcmp (lastMappedAttr->XMLattribute, "value"))
                   1503:        {
                   1504:          elType = TtaGetElementType (lastAttrElement);
                   1505:          if (elType.ElTypeNum == HTML_EL_Text_Input ||
                   1506:              elType.ElTypeNum == HTML_EL_Password_Input ||
                   1507:              elType.ElTypeNum == HTML_EL_File_Input ||
                   1508:              elType.ElTypeNum == HTML_EL_Input)
                   1509:            /* create a Default_Value attribute with the same content */
                   1510:            {
                   1511:              attrType1.AttrSSchema = attrType.AttrSSchema;
                   1512:              attrType1.AttrTypeNum = HTML_ATTR_Default_Value;
                   1513:              attr = TtaNewAttribute (attrType1);
                   1514:              TtaAttachAttribute (lastAttrElement, attr, context->doc);
                   1515:              TtaSetAttributeText (attr, attrValue,
                   1516:                                   lastAttrElement, context->doc);
                   1517:            }
                   1518:        }
1.30      cvs      1519:       else
1.48      vatton   1520:        {
                   1521:          /* Some HTML attributes are equivalent to a CSS property:      */
                   1522:          /*      background     ->                   background         */
                   1523:          /*      bgcolor        ->                   background         */
                   1524:          /*      text           ->                   color              */
                   1525:          /*      color          ->                   color              */
                   1526:          if (!strcmp (lastMappedAttr->XMLattribute, "background"))
                   1527:            {
                   1528:              if (strlen (attrValue) > MaxMsgLength - 30)
                   1529:                attrValue[MaxMsgLength - 30] = EOS;
1.76      vatton   1530:              HTMLSetBackgroundImage (context->doc, context->lastElement,
                   1531:                                      STYLE_REPEAT, attrValue, FALSE);
1.48      vatton   1532:            }
                   1533:          else if (!strcmp (lastMappedAttr->XMLattribute, "bgcolor"))
1.76      vatton   1534:            HTMLSetBackgroundColor (context->doc, context->lastElement,
                   1535:                                    attrValue);
1.48      vatton   1536:          else if (!strcmp (lastMappedAttr->XMLattribute, "text") ||
                   1537:                   !strcmp (lastMappedAttr->XMLattribute, "color"))
                   1538:            HTMLSetForegroundColor (context->doc,
                   1539:                                    context->lastElement, attrValue);
                   1540:        }
                   1541:     }
1.30      cvs      1542: }
                   1543: 
                   1544: /*----------------------------------------------------------------------
1.16      cvs      1545:    MapHTMLAttributeValue
1.2       cvs      1546:    Search in the Attribute Value Mapping Table the entry for the attribute
1.66      vatton   1547:    ThotAtt and its value attVal. Returns the corresponding Thot value.
1.1       cvs      1548:   ----------------------------------------------------------------------*/
1.66      vatton   1549: void MapHTMLAttributeValue (char *attVal, AttributeType attrType, int *value)
1.1       cvs      1550: {
1.66      vatton   1551:   MapXMLAttributeValue (XHTML_TYPE, attVal, attrType, value);
1.1       cvs      1552: }

Webmaster