1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
|
// -*- c-basic-offset: 4; -*-
/**
* This file is part of the DOM implementation for KDE.
*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2003 Dirk Mueller (mueller@kde.org)
* Copyright (C) 2002 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
// -------------------------------------------------------------------------
//#define DEBUG
//#define DEBUG_LAYOUT
//#define PAR_DEBUG
//#define EVENT_DEBUG
//#define UNSUPPORTED_ATTR
#include "html/dtd.h"
#include "html/html_elementimpl.h"
#include "html/html_documentimpl.h"
#include "html/htmltokenizer.h"
#include "misc/htmlhashes.h"
#include "khtmlview.h"
#include "khtml_part.h"
#include "rendering/render_object.h"
#include "rendering/render_replaced.h"
#include "css/css_valueimpl.h"
#include "css/css_stylesheetimpl.h"
#include "css/cssproperties.h"
#include "css/cssvalues.h"
#include "xml/dom_textimpl.h"
#include "xml/dom2_eventsimpl.h"
#include <kdebug.h>
#include <kglobal.h>
#include "html_elementimpl.h"
using namespace DOM;
using namespace khtml;
HTMLElementImpl::HTMLElementImpl(DocumentImpl *doc)
: ElementImpl( doc )
{
m_htmlCompat = doc && doc->htmlMode() != DocumentImpl::XHtml;
}
HTMLElementImpl::~HTMLElementImpl()
{
}
bool HTMLElementImpl::isInline() const
{
if (renderer())
return ElementImpl::isInline();
switch(id()) {
case ID_A:
case ID_FONT:
case ID_TT:
case ID_U:
case ID_B:
case ID_I:
case ID_S:
case ID_STRIKE:
case ID_BIG:
case ID_SMALL:
// %phrase
case ID_EM:
case ID_STRONG:
case ID_DFN:
case ID_CODE:
case ID_SAMP:
case ID_KBD:
case ID_VAR:
case ID_CITE:
case ID_ABBR:
case ID_ACRONYM:
// %special
case ID_SUB:
case ID_SUP:
case ID_SPAN:
case ID_NOBR:
case ID_WBR:
return true;
default:
return ElementImpl::isInline();
}
}
DOMString HTMLElementImpl::namespaceURI() const
{
return (!m_htmlCompat) ?
DOMString(XHTML_NAMESPACE) : DOMString();
}
DOMString HTMLElementImpl::localName() const
{
// We only have a localName if we were created by createElementNS(), in which
// case we are an XHTML element. This also means we have a lowercase name.
if (!m_htmlCompat) // XHTML == not HTMLCompat
{
NodeImpl::Id _id = id();
DOMString tn;
if ( _id >= ID_LAST_TAG )
tn = getDocument()->getName(ElementId, _id);
else // HTML tag
tn = getTagName( _id );
return tn; // lowercase already
}
// createElement() always returns elements with a null localName.
else
return DOMString();
}
DOMString HTMLElementImpl::tagName() const
{
DOMString tn;
NodeImpl::Id _id = id();
if ( _id >= ID_LAST_TAG )
tn = getDocument()->getName(ElementId, _id);
else // HTML tag
tn = getTagName( _id );
if ( m_htmlCompat )
tn = tn.upper();
if (m_prefix)
return DOMString(m_prefix) + ":" + tn;
return tn;
}
void HTMLElementImpl::parseAttribute(AttributeImpl *attr)
{
DOMString indexstring;
switch( attr->id() )
{
case ATTR_ALIGN:
if (attr->val()) {
if ( strcasecmp(attr->value(), "middle" ) == 0 )
addCSSProperty( CSS_PROP_TEXT_ALIGN, CSS_VAL_CENTER );
else
addCSSProperty(CSS_PROP_TEXT_ALIGN, attr->value().lower());
}
else
removeCSSProperty(CSS_PROP_TEXT_ALIGN);
break;
// the core attributes...
case ATTR_ID:
// unique id
setHasID();
getDocument()->incDOMTreeVersion();
break;
case ATTR_CLASS:
if (attr->val()) {
DOMString v = attr->value();
const QChar* s = v.unicode();
int l = v.length();
while( l && !s->isSpace() )
l--,s++;
setHasClassList(l);
setHasClass(true);
} else {
setHasClassList(false);
setHasClass(false);
}
break;
case ATTR_NAME:
getDocument()->incDOMTreeVersion();
break;
case ATTR_STYLE:
if (m_styleDecls)
m_styleDecls->removeCSSHints();
else
createDecl();
m_styleDecls->setProperty(attr->value());
setChanged();
break;
case ATTR_TABINDEX:
indexstring=getAttribute(ATTR_TABINDEX);
if (indexstring.length())
setTabIndex(indexstring.toInt());
break;
// i18n attributes
case ATTR_LANG:
break;
case ATTR_DIR:
addCSSProperty(CSS_PROP_DIRECTION, attr->value().lower());
addCSSProperty(CSS_PROP_UNICODE_BIDI, CSS_VAL_EMBED);
break;
// standard events
case ATTR_ONCLICK:
setHTMLEventListener(EventImpl::KHTML_ECMA_CLICK_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onclick", this));
break;
case ATTR_ONDBLCLICK:
setHTMLEventListener(EventImpl::KHTML_ECMA_DBLCLICK_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "ondblclick", this));
break;
case ATTR_ONMOUSEDOWN:
setHTMLEventListener(EventImpl::MOUSEDOWN_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onmousedown", this));
break;
case ATTR_ONMOUSEMOVE:
setHTMLEventListener(EventImpl::MOUSEMOVE_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onmousemove", this));
break;
case ATTR_ONMOUSEOUT:
setHTMLEventListener(EventImpl::MOUSEOUT_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onmouseout", this));
break;
case ATTR_ONMOUSEOVER:
setHTMLEventListener(EventImpl::MOUSEOVER_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onmouseover", this));
break;
case ATTR_ONMOUSEUP:
setHTMLEventListener(EventImpl::MOUSEUP_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onmouseup", this));
break;
case ATTR_ONKEYDOWN:
setHTMLEventListener(EventImpl::KEYDOWN_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onkeydown", this));
break;
case ATTR_ONKEYPRESS:
setHTMLEventListener(EventImpl::KEYPRESS_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onkeypress", this));
break;
case ATTR_ONKEYUP:
setHTMLEventListener(EventImpl::KEYUP_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onkeyup", this));
break;
case ATTR_ONFOCUS:
setHTMLEventListener(EventImpl::FOCUS_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onfocus", this));
break;
case ATTR_ONBLUR:
setHTMLEventListener(EventImpl::BLUR_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onblur", this));
break;
case ATTR_ONSCROLL:
setHTMLEventListener(EventImpl::SCROLL_EVENT,
getDocument()->createHTMLEventListener(attr->value().string(), "onscroll", this));
break;
// other misc attributes
default:
#ifdef UNSUPPORTED_ATTR
kdDebug(6030) << "UATTR: <" << this->nodeName().string() << "> ["
<< attr->name().string() << "]=[" << attr->value().string() << "]" << endl;
#endif
break;
}
}
void HTMLElementImpl::recalcStyle( StyleChange ch )
{
ElementImpl::recalcStyle( ch );
if (m_render /*&& changed*/)
m_render->updateFromElement();
}
void HTMLElementImpl::addCSSProperty(int id, const DOMString &value)
{
if(!m_styleDecls) createDecl();
m_styleDecls->setProperty(id, value, false, true);
setChanged();
}
void HTMLElementImpl::addCSSProperty(int id, int value)
{
if(!m_styleDecls) createDecl();
m_styleDecls->setProperty(id, value, false, true);
setChanged();
}
void HTMLElementImpl::addCSSLength(int id, const DOMString &value, bool numOnly, bool multiLength)
{
if(!m_styleDecls) createDecl();
// strip attribute garbage to avoid CSS parsing errors
// ### create specialized hook that avoids parsing every
// value twice!
if ( value.implementation() ) {
// match \s*[+-]?\d*(\.\d*)?[%\*]?
unsigned i = 0, j = 0;
QChar* s = value.implementation()->s;
unsigned l = value.implementation()->l;
while (i < l && s[i].isSpace())
++i;
if (i < l && (s[i] == '+' || s[i] == '-'))
++i;
while (i < l && s[i].isDigit())
++i,++j;
// no digits!
if (j == 0) return;
int v = kClamp( QConstString(s, i).string().toInt(), -8192, 8191 ) ;
const char* suffix = "px";
if (!numOnly || multiLength) {
// look if we find a % or *
while (i < l) {
if (multiLength && s[i] == '*') {
suffix = "";
break;
}
if (s[i] == '%') {
suffix = "%";
break;
}
++i;
}
}
if (numOnly) suffix = "";
QString ns = QString::number(v) + suffix;
m_styleDecls->setLengthProperty( id, DOMString( ns ), false, true, multiLength );
setChanged();
return;
}
m_styleDecls->setLengthProperty(id, value, false, true, multiLength);
setChanged();
}
static inline bool isHexDigit( const QChar &c ) {
return ( c >= '0' && c <= '9' ) ||
( c >= 'a' && c <= 'f' ) ||
( c >= 'A' && c <= 'F' );
}
static inline int toHex( const QChar &c ) {
return ( (c >= '0' && c <= '9')
? (c.unicode() - '0')
: ( ( c >= 'a' && c <= 'f' )
? (c.unicode() - 'a' + 10)
: ( ( c >= 'A' && c <= 'F' )
? (c.unicode() - 'A' + 10)
: -1 ) ) );
}
/* color parsing that tries to match as close as possible IE 6. */
void HTMLElementImpl::addHTMLColor( int id, const DOMString &c )
{
if(!m_styleDecls) createDecl();
// this is the only case no color gets applied in IE.
if ( !c.length() ) {
removeCSSProperty(id);
return;
}
if ( m_styleDecls->setProperty(id, c, false, true) )
return;
QString color = c.string();
// not something that fits the specs.
// we're emulating IEs color parser here. It maps transparent to black, otherwise it tries to build a rgb value
// out of everyhting you put in. The algorithm is experimentally determined, but seems to work for all test cases I have.
// the length of the color value is rounded up to the next
// multiple of 3. each part of the rgb triple then gets one third
// of the length.
//
// Each triplet is parsed byte by byte, mapping
// each number to a hex value (0-9a-fA-F to their values
// everything else to 0).
//
// The highest non zero digit in all triplets is remembered, and
// used as a normalization point to normalize to values between 0
// and 255.
if ( color.lower() != "transparent" ) {
if ( color[0] == '#' )
color.remove( 0, 1 );
int basicLength = (color.length() + 2) / 3;
if ( basicLength > 1 ) {
// IE ignores colors with three digits or less
// qDebug("trying to fix up color '%s'. basicLength=%d, length=%d",
// color.latin1(), basicLength, color.length() );
int colors[3] = { 0, 0, 0 };
int component = 0;
int pos = 0;
int maxDigit = basicLength-1;
while ( component < 3 ) {
// search forward for digits in the string
int numDigits = 0;
while ( pos < (int)color.length() && numDigits < basicLength ) {
int hex = toHex( color[pos] );
colors[component] = (colors[component] << 4);
if ( hex > 0 ) {
colors[component] += hex;
maxDigit = kMin( maxDigit, numDigits );
}
numDigits++;
pos++;
}
while ( numDigits++ < basicLength )
colors[component] <<= 4;
component++;
}
maxDigit = basicLength - maxDigit;
// qDebug("color is %x %x %x, maxDigit=%d", colors[0], colors[1], colors[2], maxDigit );
// normalize to 00-ff. The highest filled digit counts, minimum is 2 digits
maxDigit -= 2;
colors[0] >>= 4*maxDigit;
colors[1] >>= 4*maxDigit;
colors[2] >>= 4*maxDigit;
// qDebug("normalized color is %x %x %x", colors[0], colors[1], colors[2] );
// assert( colors[0] < 0x100 && colors[1] < 0x100 && colors[2] < 0x100 );
color.sprintf("#%02x%02x%02x", colors[0], colors[1], colors[2] );
// qDebug( "trying to add fixed color string '%s'", color.latin1() );
if ( m_styleDecls->setProperty(id, DOMString(color), false, true) )
return;
}
}
m_styleDecls->setProperty(id, CSS_VAL_BLACK, false, true);
}
void HTMLElementImpl::removeCSSProperty(int id)
{
if(!m_styleDecls)
return;
m_styleDecls->setParent(getDocument()->elementSheet());
m_styleDecls->removeProperty(id, true /*nonCSSHint */);
setChanged();
}
DOMString HTMLElementImpl::innerHTML() const
{
QString result; //Use QString to accumulate since DOMString is poor for appends
for (NodeImpl *child = firstChild(); child != NULL; child = child->nextSibling()) {
DOMString kid = child->toString();
result += QConstString(kid.unicode(), kid.length()).string();
}
return result;
}
DOMString HTMLElementImpl::innerText() const
{
QString text = "";
if(!firstChild())
return text;
const NodeImpl *n = this;
// find the next text/image after the anchor, to get a position
while(n) {
if(n->firstChild())
n = n->firstChild();
else if(n->nextSibling())
n = n->nextSibling();
else {
NodeImpl *next = 0;
while(!next) {
n = n->parentNode();
if(!n || n == (NodeImpl *)this ) goto end;
next = n->nextSibling();
}
n = next;
}
if(n->isTextNode() ) {
DOMStringImpl* data = static_cast<const TextImpl *>(n)->string();
text += QConstString(data->s, data->l).string();
}
}
end:
return text;
}
DocumentFragment HTMLElementImpl::createContextualFragment( const DOMString &html )
{
// the following is in accordance with the definition as used by IE
if( endTag[id()] == FORBIDDEN )
return DocumentFragment();
// IE disallows innerHTML on inline elements.
// I don't see why we should have this restriction, as our
// dhtml engine can cope with it. Lars
//if ( isInline() ) return false;
switch( id() ) {
case ID_COL:
case ID_COLGROUP:
case ID_FRAMESET:
case ID_HEAD:
case ID_TABLE:
case ID_TBODY:
case ID_TFOOT:
case ID_THEAD:
case ID_TITLE:
return DocumentFragment();
default:
break;
}
if ( !getDocument()->isHTMLDocument() )
return DocumentFragment();
DocumentFragmentImpl* fragment = new DocumentFragmentImpl( docPtr() );
DocumentFragment f( fragment );
{
HTMLTokenizer tok( docPtr(), fragment );
tok.begin();
tok.write( html.string(), true );
tok.end();
}
// Exceptions are ignored because none ought to happen here.
int ignoredExceptionCode;
// we need to pop <html> and <body> elements and remove <head> to
// accomadate folks passing complete HTML documents to make the
// child of an element.
for ( NodeImpl* node = fragment->firstChild(); node; ) {
if (node->id() == ID_HTML || node->id() == ID_BODY) {
NodeImpl* firstChild = node->firstChild();
NodeImpl* child = firstChild;
while ( child ) {
NodeImpl *nextChild = child->nextSibling();
fragment->insertBefore(child, node, ignoredExceptionCode);
child = nextChild;
}
if ( !firstChild ) {
NodeImpl *nextNode = node->nextSibling();
fragment->removeChild(node, ignoredExceptionCode);
node = nextNode;
} else {
fragment->removeChild(node, ignoredExceptionCode);
node = firstChild;
}
} else if (node->id() == ID_HEAD) {
NodeImpl *nextNode = node->nextSibling();
fragment->removeChild(node, ignoredExceptionCode);
node = nextNode;
} else {
node = node->nextSibling();
}
}
return f;
}
void HTMLElementImpl::setInnerHTML( const DOMString &html, int &exceptioncode )
{
// Works line innerText in Gecko
// ### test if needed for ID_SCRIPT as well.
if ( id() == ID_STYLE ) {
setInnerText(html, exceptioncode);
return;
}
DocumentFragment fragment = createContextualFragment( html );
if ( fragment.isNull() ) {
exceptioncode = DOMException::NO_MODIFICATION_ALLOWED_ERR;
return;
}
// Make sure adding the new child is ok, before removing all children (#96187)
checkAddChild( fragment.handle(), exceptioncode );
if ( exceptioncode )
return;
removeChildren();
appendChild( fragment.handle(), exceptioncode );
}
void HTMLElementImpl::setInnerText( const DOMString &text, int& exceptioncode )
{
// following the IE specs.
if( endTag[id()] == FORBIDDEN ) {
exceptioncode = DOMException::NO_MODIFICATION_ALLOWED_ERR;
return;
}
// IE disallows innerHTML on inline elements. I don't see why we should have this restriction, as our
// dhtml engine can cope with it. Lars
//if ( isInline() ) return false;
switch( id() ) {
case ID_COL:
case ID_COLGROUP:
case ID_FRAMESET:
case ID_HEAD:
case ID_HTML:
case ID_TABLE:
case ID_TBODY:
case ID_TFOOT:
case ID_THEAD:
case ID_TR:
exceptioncode = DOMException::NO_MODIFICATION_ALLOWED_ERR;
return;
default:
break;
}
removeChildren();
TextImpl *t = new TextImpl( docPtr(), text.implementation() );
appendChild( t, exceptioncode );
}
void HTMLElementImpl::addHTMLAlignment( DOMString alignment )
{
//qDebug("alignment is %s", alignment.string().latin1() );
// vertical alignment with respect to the current baseline of the text
// right or left means floating images
int propfloat = -1;
int propvalign = -1;
if ( strcasecmp( alignment, "absmiddle" ) == 0 ) {
propvalign = CSS_VAL_MIDDLE;
} else if ( strcasecmp( alignment, "absbottom" ) == 0 ) {
propvalign = CSS_VAL_BOTTOM;
} else if ( strcasecmp( alignment, "left" ) == 0 ) {
propfloat = CSS_VAL_LEFT;
propvalign = CSS_VAL_TOP;
} else if ( strcasecmp( alignment, "right" ) == 0 ) {
propfloat = CSS_VAL_RIGHT;
propvalign = CSS_VAL_TOP;
} else if ( strcasecmp( alignment, "top" ) == 0 ) {
propvalign = CSS_VAL_TOP;
} else if ( strcasecmp( alignment, "middle" ) == 0 ) {
propvalign = CSS_VAL__KHTML_BASELINE_MIDDLE;
} else if ( strcasecmp( alignment, "center" ) == 0 ) {
propvalign = CSS_VAL_MIDDLE;
} else if ( strcasecmp( alignment, "bottom" ) == 0 ) {
propvalign = CSS_VAL_BASELINE;
} else if ( strcasecmp ( alignment, "texttop") == 0 ) {
propvalign = CSS_VAL_TEXT_TOP;
}
if ( propfloat != -1 )
addCSSProperty( CSS_PROP_FLOAT, propfloat );
if ( propvalign != -1 )
addCSSProperty( CSS_PROP_VERTICAL_ALIGN, propvalign );
}
DOMString HTMLElementImpl::toString() const
{
if (!hasChildNodes()) {
DOMString result = openTagStartToString();
result += ">";
if (endTag[id()] == REQUIRED) {
result += "</";
result += tagName();
result += ">";
}
return result;
}
return ElementImpl::toString();
}
// -------------------------------------------------------------------------
HTMLGenericElementImpl::HTMLGenericElementImpl(DocumentImpl *doc, ushort i)
: HTMLElementImpl(doc)
{
_id = i;
}
HTMLGenericElementImpl::~HTMLGenericElementImpl()
{
}
|