2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "HTMLElement.h"
26 #include "CSSPropertyNames.h"
27 #include "CSSValueKeywords.h"
28 #include "DocumentFragment.h"
30 #include "EventListener.h"
31 #include "EventNames.h"
32 #include "ExceptionCode.h"
34 #include "HTMLBRElement.h"
35 #include "HTMLDocument.h"
36 #include "HTMLElementFactory.h"
37 #include "HTMLFormElement.h"
38 #include "HTMLNames.h"
39 #include "HTMLTokenizer.h" // parseHTMLDocumentFragment
40 #include "RenderWordBreak.h"
43 #include "TextIterator.h"
44 #include "XMLTokenizer.h"
46 #include <wtf/StdLibExtras.h>
50 using namespace HTMLNames;
55 HTMLElement::HTMLElement(const QualifiedName& tagName, Document *doc)
56 : StyledElement(tagName, doc)
60 HTMLElement::~HTMLElement()
64 String HTMLElement::nodeName() const
66 // FIXME: Would be nice to have an atomicstring lookup based off uppercase chars that does not have to copy
67 // the string on a hit in the hash.
68 if (document()->isHTMLDocument())
69 return tagQName().localName().string().upper();
70 return Element::nodeName();
73 HTMLTagStatus HTMLElement::endTagRequirement() const
75 if (hasLocalName(wbrTag))
76 return TagStatusForbidden;
77 if (hasLocalName(dtTag) || hasLocalName(ddTag))
78 return TagStatusOptional;
80 // Same values as <span>. This way custom tag name elements will behave like inline spans.
81 return TagStatusRequired;
84 int HTMLElement::tagPriority() const
86 if (hasLocalName(wbrTag))
88 if (hasLocalName(addressTag) || hasLocalName(ddTag) || hasLocalName(dtTag) || hasLocalName(noscriptTag))
90 if (hasLocalName(centerTag) || hasLocalName(nobrTag))
92 if (hasLocalName(noembedTag) || hasLocalName(noframesTag))
95 // Same values as <span>. This way custom tag name elements will behave like inline spans.
99 PassRefPtr<Node> HTMLElement::cloneNode(bool deep)
101 RefPtr<HTMLElement> clone = HTMLElementFactory::createHTMLElement(tagQName(), document(), 0, false);
106 clone->attributes()->setAttributes(*namedAttrMap);
108 clone->copyNonAttributeProperties(this);
111 cloneChildNodes(clone.get());
113 return clone.release();
116 bool HTMLElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
118 if (attrName == alignAttr ||
119 attrName == contenteditableAttr) {
123 if (attrName == dirAttr) {
124 result = hasLocalName(bdoTag) ? eBDO : eUniversal;
128 return StyledElement::mapToEntry(attrName, result);
131 void HTMLElement::parseMappedAttribute(MappedAttribute *attr)
133 if (attr->name() == idAttr || attr->name() == classAttr || attr->name() == styleAttr)
134 return StyledElement::parseMappedAttribute(attr);
137 if (attr->name() == alignAttr) {
138 if (equalIgnoringCase(attr->value(), "middle"))
139 addCSSProperty(attr, CSSPropertyTextAlign, "center");
141 addCSSProperty(attr, CSSPropertyTextAlign, attr->value());
142 } else if (attr->name() == contenteditableAttr) {
143 setContentEditable(attr);
144 } else if (attr->name() == tabindexAttr) {
145 indexstring = getAttribute(tabindexAttr);
146 if (indexstring.length()) {
148 int tabindex = indexstring.toIntStrict(&parsedOK);
150 // Clamp tabindex to the range of 'short' to match Firefox's behavior.
151 setTabIndexExplicitly(max(static_cast<int>(std::numeric_limits<short>::min()), min(tabindex, static_cast<int>(std::numeric_limits<short>::max()))));
153 } else if (attr->name() == langAttr) {
155 } else if (attr->name() == dirAttr) {
156 addCSSProperty(attr, CSSPropertyDirection, attr->value());
157 addCSSProperty(attr, CSSPropertyUnicodeBidi, hasLocalName(bdoTag) ? CSSValueBidiOverride : CSSValueEmbed);
160 else if (attr->name() == onclickAttr) {
161 setInlineEventListenerForTypeAndAttribute(eventNames().clickEvent, attr);
162 } else if (attr->name() == oncontextmenuAttr) {
163 setInlineEventListenerForTypeAndAttribute(eventNames().contextmenuEvent, attr);
164 } else if (attr->name() == ondblclickAttr) {
165 setInlineEventListenerForTypeAndAttribute(eventNames().dblclickEvent, attr);
166 } else if (attr->name() == onmousedownAttr) {
167 setInlineEventListenerForTypeAndAttribute(eventNames().mousedownEvent, attr);
168 } else if (attr->name() == onmousemoveAttr) {
169 setInlineEventListenerForTypeAndAttribute(eventNames().mousemoveEvent, attr);
170 } else if (attr->name() == onmouseoutAttr) {
171 setInlineEventListenerForTypeAndAttribute(eventNames().mouseoutEvent, attr);
172 } else if (attr->name() == onmouseoverAttr) {
173 setInlineEventListenerForTypeAndAttribute(eventNames().mouseoverEvent, attr);
174 } else if (attr->name() == onmouseupAttr) {
175 setInlineEventListenerForTypeAndAttribute(eventNames().mouseupEvent, attr);
176 } else if (attr->name() == onmousewheelAttr) {
177 setInlineEventListenerForTypeAndAttribute(eventNames().mousewheelEvent, attr);
178 } else if (attr->name() == onfocusAttr) {
179 setInlineEventListenerForTypeAndAttribute(eventNames().focusEvent, attr);
180 } else if (attr->name() == onblurAttr) {
181 setInlineEventListenerForTypeAndAttribute(eventNames().blurEvent, attr);
182 } else if (attr->name() == onkeydownAttr) {
183 setInlineEventListenerForTypeAndAttribute(eventNames().keydownEvent, attr);
184 } else if (attr->name() == onkeypressAttr) {
185 setInlineEventListenerForTypeAndAttribute(eventNames().keypressEvent, attr);
186 } else if (attr->name() == onkeyupAttr) {
187 setInlineEventListenerForTypeAndAttribute(eventNames().keyupEvent, attr);
188 } else if (attr->name() == onscrollAttr) {
189 setInlineEventListenerForTypeAndAttribute(eventNames().scrollEvent, attr);
190 } else if (attr->name() == onbeforecutAttr) {
191 setInlineEventListenerForTypeAndAttribute(eventNames().beforecutEvent, attr);
192 } else if (attr->name() == oncutAttr) {
193 setInlineEventListenerForTypeAndAttribute(eventNames().cutEvent, attr);
194 } else if (attr->name() == onbeforecopyAttr) {
195 setInlineEventListenerForTypeAndAttribute(eventNames().beforecopyEvent, attr);
196 } else if (attr->name() == oncopyAttr) {
197 setInlineEventListenerForTypeAndAttribute(eventNames().copyEvent, attr);
198 } else if (attr->name() == onbeforepasteAttr) {
199 setInlineEventListenerForTypeAndAttribute(eventNames().beforepasteEvent, attr);
200 } else if (attr->name() == onpasteAttr) {
201 setInlineEventListenerForTypeAndAttribute(eventNames().pasteEvent, attr);
202 } else if (attr->name() == ondragenterAttr) {
203 setInlineEventListenerForTypeAndAttribute(eventNames().dragenterEvent, attr);
204 } else if (attr->name() == ondragoverAttr) {
205 setInlineEventListenerForTypeAndAttribute(eventNames().dragoverEvent, attr);
206 } else if (attr->name() == ondragleaveAttr) {
207 setInlineEventListenerForTypeAndAttribute(eventNames().dragleaveEvent, attr);
208 } else if (attr->name() == ondropAttr) {
209 setInlineEventListenerForTypeAndAttribute(eventNames().dropEvent, attr);
210 } else if (attr->name() == ondragstartAttr) {
211 setInlineEventListenerForTypeAndAttribute(eventNames().dragstartEvent, attr);
212 } else if (attr->name() == ondragAttr) {
213 setInlineEventListenerForTypeAndAttribute(eventNames().dragEvent, attr);
214 } else if (attr->name() == ondragendAttr) {
215 setInlineEventListenerForTypeAndAttribute(eventNames().dragendEvent, attr);
216 } else if (attr->name() == onselectstartAttr) {
217 setInlineEventListenerForTypeAndAttribute(eventNames().selectstartEvent, attr);
218 } else if (attr->name() == onsubmitAttr) {
219 setInlineEventListenerForTypeAndAttribute(eventNames().submitEvent, attr);
220 } else if (attr->name() == onerrorAttr) {
221 setInlineEventListenerForTypeAndAttribute(eventNames().errorEvent, attr);
222 } else if (attr->name() == onwebkitanimationstartAttr) {
223 setInlineEventListenerForTypeAndAttribute(eventNames().webkitAnimationStartEvent, attr);
224 } else if (attr->name() == onwebkitanimationiterationAttr) {
225 setInlineEventListenerForTypeAndAttribute(eventNames().webkitAnimationIterationEvent, attr);
226 } else if (attr->name() == onwebkitanimationendAttr) {
227 setInlineEventListenerForTypeAndAttribute(eventNames().webkitAnimationEndEvent, attr);
228 } else if (attr->name() == onwebkittransitionendAttr) {
229 setInlineEventListenerForTypeAndAttribute(eventNames().webkitTransitionEndEvent, attr);
233 String HTMLElement::innerHTML() const
235 return createMarkup(this, ChildrenOnly);
238 String HTMLElement::outerHTML() const
240 return createMarkup(this);
243 PassRefPtr<DocumentFragment> HTMLElement::createContextualFragment(const String &html)
245 // the following is in accordance with the definition as used by IE
246 if (endTagRequirement() == TagStatusForbidden)
249 if (hasLocalName(colTag) || hasLocalName(colgroupTag) || hasLocalName(framesetTag) ||
250 hasLocalName(headTag) || hasLocalName(styleTag) || hasLocalName(titleTag))
253 RefPtr<DocumentFragment> fragment = new DocumentFragment(document());
255 if (document()->isHTMLDocument())
256 parseHTMLDocumentFragment(html, fragment.get());
258 if (!parseXMLDocumentFragment(html, fragment.get(), this))
259 // FIXME: We should propagate a syntax error exception out here.
263 // Exceptions are ignored because none ought to happen here.
264 int ignoredExceptionCode;
266 // we need to pop <html> and <body> elements and remove <head> to
267 // accommodate folks passing complete HTML documents to make the
268 // child of an element.
270 RefPtr<Node> nextNode;
271 for (RefPtr<Node> node = fragment->firstChild(); node; node = nextNode) {
272 nextNode = node->nextSibling();
273 if (node->hasTagName(htmlTag) || node->hasTagName(bodyTag)) {
274 Node *firstChild = node->firstChild();
276 nextNode = firstChild;
277 RefPtr<Node> nextChild;
278 for (RefPtr<Node> child = firstChild; child; child = nextChild) {
279 nextChild = child->nextSibling();
280 node->removeChild(child.get(), ignoredExceptionCode);
281 ASSERT(!ignoredExceptionCode);
282 fragment->insertBefore(child, node.get(), ignoredExceptionCode);
283 ASSERT(!ignoredExceptionCode);
285 fragment->removeChild(node.get(), ignoredExceptionCode);
286 ASSERT(!ignoredExceptionCode);
287 } else if (node->hasTagName(headTag)) {
288 fragment->removeChild(node.get(), ignoredExceptionCode);
289 ASSERT(!ignoredExceptionCode);
293 return fragment.release();
296 static inline bool hasOneChild(ContainerNode* node)
298 Node* firstChild = node->firstChild();
299 return firstChild && !firstChild->nextSibling();
302 static inline bool hasOneTextChild(ContainerNode* node)
304 return hasOneChild(node) && node->firstChild()->isTextNode();
307 static void replaceChildrenWithFragment(HTMLElement* element, PassRefPtr<DocumentFragment> fragment, ExceptionCode& ec)
309 if (!fragment->firstChild()) {
310 element->removeChildren();
314 if (hasOneTextChild(element) && hasOneTextChild(fragment.get())) {
315 static_cast<Text*>(element->firstChild())->setData(static_cast<Text*>(fragment->firstChild())->string(), ec);
319 if (hasOneChild(element)) {
320 element->replaceChild(fragment, element->firstChild(), ec);
324 element->removeChildren();
325 element->appendChild(fragment, ec);
328 static void replaceChildrenWithText(HTMLElement* element, const String& text, ExceptionCode& ec)
330 if (hasOneTextChild(element)) {
331 static_cast<Text*>(element->firstChild())->setData(text, ec);
335 RefPtr<Text> textNode = new Text(element->document(), text);
337 if (hasOneChild(element)) {
338 element->replaceChild(textNode.release(), element->firstChild(), ec);
342 element->removeChildren();
343 element->appendChild(textNode.release(), ec);
346 void HTMLElement::setInnerHTML(const String& html, ExceptionCode& ec)
348 RefPtr<DocumentFragment> fragment = createContextualFragment(html);
350 ec = NO_MODIFICATION_ALLOWED_ERR;
354 replaceChildrenWithFragment(this, fragment.release(), ec);
357 void HTMLElement::setOuterHTML(const String& html, ExceptionCode& ec)
360 if (!p || !p->isHTMLElement()) {
361 ec = NO_MODIFICATION_ALLOWED_ERR;
365 HTMLElement* parent = static_cast<HTMLElement*>(p);
366 RefPtr<DocumentFragment> fragment = parent->createContextualFragment(html);
368 ec = NO_MODIFICATION_ALLOWED_ERR;
372 // FIXME: Why doesn't this have code to merge neighboring text nodes the way setOuterText does?
374 parent->replaceChild(fragment.release(), this, ec);
377 void HTMLElement::setInnerText(const String& text, ExceptionCode& ec)
379 // follow the IE specs about when this is allowed
380 if (endTagRequirement() == TagStatusForbidden) {
381 ec = NO_MODIFICATION_ALLOWED_ERR;
384 if (hasLocalName(colTag) || hasLocalName(colgroupTag) || hasLocalName(framesetTag) ||
385 hasLocalName(headTag) || hasLocalName(htmlTag) || hasLocalName(tableTag) ||
386 hasLocalName(tbodyTag) || hasLocalName(tfootTag) || hasLocalName(theadTag) ||
387 hasLocalName(trTag)) {
388 ec = NO_MODIFICATION_ALLOWED_ERR;
392 // FIXME: This doesn't take whitespace collapsing into account at all.
394 if (!text.contains('\n') && !text.contains('\r')) {
395 if (text.isEmpty()) {
399 replaceChildrenWithText(this, text, ec);
403 // FIXME: Do we need to be able to detect preserveNewline style even when there's no renderer?
404 // FIXME: Can the renderer be out of date here? Do we need to call updateRendering?
405 // For example, for the contents of textarea elements that are display:none?
406 RenderObject* r = renderer();
407 if (r && r->style()->preserveNewline()) {
408 if (!text.contains('\r')) {
409 replaceChildrenWithText(this, text, ec);
412 String textWithConsistentLineBreaks = text;
413 textWithConsistentLineBreaks.replace("\r\n", "\n");
414 textWithConsistentLineBreaks.replace('\r', '\n');
415 replaceChildrenWithText(this, textWithConsistentLineBreaks, ec);
419 // Add text nodes and <br> elements.
421 RefPtr<DocumentFragment> fragment = new DocumentFragment(document());
424 int length = text.length();
425 for (int i = 0; i < length; ++i) {
427 if (c == '\n' || c == '\r') {
429 fragment->appendChild(new Text(document(), text.substring(lineStart, i - lineStart)), ec);
433 if (!(c == '\n' && i != 0 && prev == '\r')) {
434 fragment->appendChild(new HTMLBRElement(brTag, document()), ec);
442 if (length > lineStart)
443 fragment->appendChild(new Text(document(), text.substring(lineStart, length - lineStart)), ec);
444 replaceChildrenWithFragment(this, fragment.release(), ec);
447 void HTMLElement::setOuterText(const String &text, ExceptionCode& ec)
449 // follow the IE specs about when this is allowed
450 if (endTagRequirement() == TagStatusForbidden) {
451 ec = NO_MODIFICATION_ALLOWED_ERR;
454 if (hasLocalName(colTag) || hasLocalName(colgroupTag) || hasLocalName(framesetTag) ||
455 hasLocalName(headTag) || hasLocalName(htmlTag) || hasLocalName(tableTag) ||
456 hasLocalName(tbodyTag) || hasLocalName(tfootTag) || hasLocalName(theadTag) ||
457 hasLocalName(trTag)) {
458 ec = NO_MODIFICATION_ALLOWED_ERR;
462 Node* parent = parentNode();
464 ec = NO_MODIFICATION_ALLOWED_ERR;
468 // FIXME: This creates a new text node even when the text is empty.
469 // FIXME: This creates a single text node even when the text has CR and LF
470 // characters in it. Instead it should create <br> elements.
471 RefPtr<Text> t = new Text(document(), text);
473 parent->replaceChild(t, this, ec);
477 // is previous node a text node? if so, merge into it
478 Node* prev = t->previousSibling();
479 if (prev && prev->isTextNode()) {
480 Text* textPrev = static_cast<Text*>(prev);
481 textPrev->appendData(t->data(), ec);
490 // is next node a text node? if so, merge it in
491 Node* next = t->nextSibling();
492 if (next && next->isTextNode()) {
493 Text* textNext = static_cast<Text*>(next);
494 t->appendData(textNext->data(), ec);
497 textNext->remove(ec);
503 Node* HTMLElement::insertAdjacent(const String& where, Node* newChild, ExceptionCode& ec)
505 // In Internet Explorer if the element has no parent and where is "beforeBegin" or "afterEnd",
506 // a document fragment is created and the elements appended in the correct order. This document
507 // fragment isn't returned anywhere.
509 // This is impossible for us to implement as the DOM tree does not allow for such structures,
510 // Opera also appears to disallow such usage.
512 if (equalIgnoringCase(where, "beforeBegin")) {
513 if (Node* p = parent())
514 return p->insertBefore(newChild, this, ec) ? newChild : 0;
518 if (equalIgnoringCase(where, "afterBegin"))
519 return insertBefore(newChild, firstChild(), ec) ? newChild : 0;
521 if (equalIgnoringCase(where, "beforeEnd"))
522 return appendChild(newChild, ec) ? newChild : 0;
524 if (equalIgnoringCase(where, "afterEnd")) {
525 if (Node* p = parent())
526 return p->insertBefore(newChild, nextSibling(), ec) ? newChild : 0;
530 // IE throws COM Exception E_INVALIDARG; this is the best DOM exception alternative
531 ec = NOT_SUPPORTED_ERR;
535 Element* HTMLElement::insertAdjacentElement(const String& where, Element* newChild, ExceptionCode& ec)
538 // IE throws COM Exception E_INVALIDARG; this is the best DOM exception alternative
539 ec = TYPE_MISMATCH_ERR;
543 Node* returnValue = insertAdjacent(where, newChild, ec);
544 ASSERT(!returnValue || returnValue->isElementNode());
545 return static_cast<Element*>(returnValue);
548 void HTMLElement::insertAdjacentHTML(const String& where, const String& html, ExceptionCode& ec)
550 RefPtr<DocumentFragment> fragment = document()->createDocumentFragment();
551 if (document()->isHTMLDocument())
552 parseHTMLDocumentFragment(html, fragment.get());
554 if (!parseXMLDocumentFragment(html, fragment.get(), this))
555 // FIXME: We should propagate a syntax error exception out here.
559 insertAdjacent(where, fragment.get(), ec);
562 void HTMLElement::insertAdjacentText(const String& where, const String& text, ExceptionCode& ec)
564 RefPtr<Text> textNode = document()->createTextNode(text);
565 insertAdjacent(where, textNode.get(), ec);
568 void HTMLElement::addHTMLAlignment(MappedAttribute* attr)
570 addHTMLAlignmentToStyledElement(this, attr);
573 void HTMLElement::addHTMLAlignmentToStyledElement(StyledElement* element, MappedAttribute* attr)
575 // vertical alignment with respect to the current baseline of the text
576 // right or left means floating images
577 int floatValue = CSSValueInvalid;
578 int verticalAlignValue = CSSValueInvalid;
580 const AtomicString& alignment = attr->value();
581 if (equalIgnoringCase(alignment, "absmiddle"))
582 verticalAlignValue = CSSValueMiddle;
583 else if (equalIgnoringCase(alignment, "absbottom"))
584 verticalAlignValue = CSSValueBottom;
585 else if (equalIgnoringCase(alignment, "left")) {
586 floatValue = CSSValueLeft;
587 verticalAlignValue = CSSValueTop;
588 } else if (equalIgnoringCase(alignment, "right")) {
589 floatValue = CSSValueRight;
590 verticalAlignValue = CSSValueTop;
591 } else if (equalIgnoringCase(alignment, "top"))
592 verticalAlignValue = CSSValueTop;
593 else if (equalIgnoringCase(alignment, "middle"))
594 verticalAlignValue = CSSValueWebkitBaselineMiddle;
595 else if (equalIgnoringCase(alignment, "center"))
596 verticalAlignValue = CSSValueMiddle;
597 else if (equalIgnoringCase(alignment, "bottom"))
598 verticalAlignValue = CSSValueBaseline;
599 else if (equalIgnoringCase(alignment, "texttop"))
600 verticalAlignValue = CSSValueTextTop;
602 if (floatValue != CSSValueInvalid)
603 element->addCSSProperty(attr, CSSPropertyFloat, floatValue);
605 if (verticalAlignValue != CSSValueInvalid)
606 element->addCSSProperty(attr, CSSPropertyVerticalAlign, verticalAlignValue);
609 bool HTMLElement::isFocusable() const
611 return Element::isFocusable() || (isContentEditable() && parent() && !parent()->isContentEditable());
614 bool HTMLElement::isContentEditable() const
616 if (document()->frame() && document()->frame()->isContentEditable())
619 // FIXME: this is a terrible thing to do here:
620 // https://bugs.webkit.org/show_bug.cgi?id=21834
621 document()->updateRendering();
625 return parentNode()->isContentEditable();
630 return renderer()->style()->userModify() == READ_WRITE || renderer()->style()->userModify() == READ_WRITE_PLAINTEXT_ONLY;
633 bool HTMLElement::isContentRichlyEditable() const
635 if (document()->frame() && document()->frame()->isContentEditable())
638 document()->updateRendering();
642 return parentNode()->isContentEditable();
647 return renderer()->style()->userModify() == READ_WRITE;
650 String HTMLElement::contentEditable() const
652 document()->updateRendering();
657 switch (renderer()->style()->userModify()) {
662 case READ_WRITE_PLAINTEXT_ONLY:
663 return "plaintext-only";
669 void HTMLElement::setContentEditable(MappedAttribute* attr)
671 const AtomicString& enabled = attr->value();
672 if (enabled.isEmpty() || equalIgnoringCase(enabled, "true")) {
673 addCSSProperty(attr, CSSPropertyWebkitUserModify, CSSValueReadWrite);
674 addCSSProperty(attr, CSSPropertyWordWrap, CSSValueBreakWord);
675 addCSSProperty(attr, CSSPropertyWebkitNbspMode, CSSValueSpace);
676 addCSSProperty(attr, CSSPropertyWebkitLineBreak, CSSValueAfterWhiteSpace);
677 } else if (equalIgnoringCase(enabled, "false")) {
678 addCSSProperty(attr, CSSPropertyWebkitUserModify, CSSValueReadOnly);
679 attr->decl()->removeProperty(CSSPropertyWordWrap, false);
680 attr->decl()->removeProperty(CSSPropertyWebkitNbspMode, false);
681 attr->decl()->removeProperty(CSSPropertyWebkitLineBreak, false);
682 } else if (equalIgnoringCase(enabled, "inherit")) {
683 addCSSProperty(attr, CSSPropertyWebkitUserModify, CSSValueInherit);
684 attr->decl()->removeProperty(CSSPropertyWordWrap, false);
685 attr->decl()->removeProperty(CSSPropertyWebkitNbspMode, false);
686 attr->decl()->removeProperty(CSSPropertyWebkitLineBreak, false);
687 } else if (equalIgnoringCase(enabled, "plaintext-only")) {
688 addCSSProperty(attr, CSSPropertyWebkitUserModify, CSSValueReadWritePlaintextOnly);
689 addCSSProperty(attr, CSSPropertyWordWrap, CSSValueBreakWord);
690 addCSSProperty(attr, CSSPropertyWebkitNbspMode, CSSValueSpace);
691 addCSSProperty(attr, CSSPropertyWebkitLineBreak, CSSValueAfterWhiteSpace);
695 void HTMLElement::setContentEditable(const String &enabled)
697 if (enabled == "inherit") {
699 removeAttribute(contenteditableAttr, ec);
702 setAttribute(contenteditableAttr, enabled.isEmpty() ? "true" : enabled);
705 void HTMLElement::click()
707 dispatchSimulatedClick(0, false, false);
710 // accessKeyAction is used by the accessibility support code
711 // to send events to elements that our JavaScript caller does
712 // does not. The elements JS is interested in have subclasses
713 // that override this method to direct the click appropriately.
714 // Here in the base class, then, we only send the click if
715 // the caller wants it to go to any HTMLElement, and we say
716 // to send the mouse events in addition to the click.
717 void HTMLElement::accessKeyAction(bool sendToAnyElement)
719 if (sendToAnyElement)
720 dispatchSimulatedClick(0, true);
723 String HTMLElement::id() const
725 return getAttribute(idAttr);
728 void HTMLElement::setId(const String& value)
730 setAttribute(idAttr, value);
733 String HTMLElement::title() const
735 return getAttribute(titleAttr);
738 void HTMLElement::setTitle(const String& value)
740 setAttribute(titleAttr, value);
743 String HTMLElement::lang() const
745 return getAttribute(langAttr);
748 void HTMLElement::setLang(const String& value)
750 setAttribute(langAttr, value);
753 String HTMLElement::dir() const
755 return getAttribute(dirAttr);
758 void HTMLElement::setDir(const String &value)
760 setAttribute(dirAttr, value);
763 String HTMLElement::className() const
765 return getAttribute(classAttr);
768 void HTMLElement::setClassName(const String &value)
770 setAttribute(classAttr, value);
773 short HTMLElement::tabIndex() const
776 return Element::tabIndex();
780 void HTMLElement::setTabIndex(int value)
782 setAttribute(tabindexAttr, String::number(value));
785 PassRefPtr<HTMLCollection> HTMLElement::children()
787 return HTMLCollection::create(this, HTMLCollection::NodeChildren);
791 bool HTMLElement::childAllowed(Node *newChild)
793 if (!Element::childAllowed(newChild))
796 // For XML documents, we are non-validating and do not check against a DTD, even for HTML elements.
797 if (!document()->isHTMLDocument())
800 // Future-proof for XML content inside HTML documents (we may allow this some day).
801 if (newChild->isElementNode() && !newChild->isHTMLElement())
804 // Elements with forbidden tag status can never have children
805 if (endTagRequirement() == TagStatusForbidden)
808 // Comment nodes are always allowed.
809 if (newChild->isCommentNode())
812 // Now call checkDTD.
813 return checkDTD(newChild);
817 // This unfortunate function is only needed when checking against the DTD. Other languages (like SVG) won't need this.
818 bool HTMLElement::isRecognizedTagName(const QualifiedName& tagName)
820 DEFINE_STATIC_LOCAL(HashSet<AtomicStringImpl*>, tagList, ());
821 if (tagList.isEmpty()) {
823 WebCore::QualifiedName** tags = HTMLNames::getHTMLTags(&tagCount);
824 for (size_t i = 0; i < tagCount; i++)
825 tagList.add(tags[i]->localName().impl());
827 return tagList.contains(tagName.localName().impl());
830 // The terms inline and block are used here loosely. Don't make the mistake of assuming all inlines or all blocks
831 // need to be in these two lists.
832 HashSet<AtomicStringImpl*>* inlineTagList()
834 DEFINE_STATIC_LOCAL(HashSet<AtomicStringImpl*>, tagList, ());
835 if (tagList.isEmpty()) {
836 tagList.add(ttTag.localName().impl());
837 tagList.add(iTag.localName().impl());
838 tagList.add(bTag.localName().impl());
839 tagList.add(uTag.localName().impl());
840 tagList.add(sTag.localName().impl());
841 tagList.add(strikeTag.localName().impl());
842 tagList.add(bigTag.localName().impl());
843 tagList.add(smallTag.localName().impl());
844 tagList.add(emTag.localName().impl());
845 tagList.add(strongTag.localName().impl());
846 tagList.add(dfnTag.localName().impl());
847 tagList.add(codeTag.localName().impl());
848 tagList.add(sampTag.localName().impl());
849 tagList.add(kbdTag.localName().impl());
850 tagList.add(varTag.localName().impl());
851 tagList.add(citeTag.localName().impl());
852 tagList.add(abbrTag.localName().impl());
853 tagList.add(acronymTag.localName().impl());
854 tagList.add(aTag.localName().impl());
855 tagList.add(canvasTag.localName().impl());
856 tagList.add(imgTag.localName().impl());
857 tagList.add(appletTag.localName().impl());
858 tagList.add(objectTag.localName().impl());
859 tagList.add(embedTag.localName().impl());
860 tagList.add(fontTag.localName().impl());
861 tagList.add(basefontTag.localName().impl());
862 tagList.add(brTag.localName().impl());
863 tagList.add(scriptTag.localName().impl());
864 tagList.add(styleTag.localName().impl());
865 tagList.add(linkTag.localName().impl());
866 tagList.add(mapTag.localName().impl());
867 tagList.add(qTag.localName().impl());
868 tagList.add(subTag.localName().impl());
869 tagList.add(supTag.localName().impl());
870 tagList.add(spanTag.localName().impl());
871 tagList.add(bdoTag.localName().impl());
872 tagList.add(iframeTag.localName().impl());
873 tagList.add(inputTag.localName().impl());
874 tagList.add(keygenTag.localName().impl());
875 tagList.add(selectTag.localName().impl());
876 tagList.add(textareaTag.localName().impl());
877 tagList.add(labelTag.localName().impl());
878 tagList.add(buttonTag.localName().impl());
879 tagList.add(insTag.localName().impl());
880 tagList.add(delTag.localName().impl());
881 tagList.add(nobrTag.localName().impl());
882 tagList.add(wbrTag.localName().impl());
884 tagList.add(audioTag.localName().impl());
885 tagList.add(videoTag.localName().impl());
891 HashSet<AtomicStringImpl*>* blockTagList()
893 DEFINE_STATIC_LOCAL(HashSet<AtomicStringImpl*>, tagList, ());
894 if (tagList.isEmpty()) {
895 tagList.add(addressTag.localName().impl());
896 tagList.add(blockquoteTag.localName().impl());
897 tagList.add(centerTag.localName().impl());
898 tagList.add(ddTag.localName().impl());
899 tagList.add(dirTag.localName().impl());
900 tagList.add(divTag.localName().impl());
901 tagList.add(dlTag.localName().impl());
902 tagList.add(dtTag.localName().impl());
903 tagList.add(fieldsetTag.localName().impl());
904 tagList.add(formTag.localName().impl());
905 tagList.add(h1Tag.localName().impl());
906 tagList.add(h2Tag.localName().impl());
907 tagList.add(h3Tag.localName().impl());
908 tagList.add(h4Tag.localName().impl());
909 tagList.add(h5Tag.localName().impl());
910 tagList.add(h6Tag.localName().impl());
911 tagList.add(hrTag.localName().impl());
912 tagList.add(isindexTag.localName().impl());
913 tagList.add(layerTag.localName().impl());
914 tagList.add(liTag.localName().impl());
915 tagList.add(listingTag.localName().impl());
916 tagList.add(marqueeTag.localName().impl());
917 tagList.add(menuTag.localName().impl());
918 tagList.add(noembedTag.localName().impl());
919 tagList.add(noframesTag.localName().impl());
920 tagList.add(nolayerTag.localName().impl());
921 tagList.add(noscriptTag.localName().impl());
922 tagList.add(olTag.localName().impl());
923 tagList.add(pTag.localName().impl());
924 tagList.add(plaintextTag.localName().impl());
925 tagList.add(preTag.localName().impl());
926 tagList.add(tableTag.localName().impl());
927 tagList.add(ulTag.localName().impl());
928 tagList.add(xmpTag.localName().impl());
933 bool HTMLElement::inEitherTagList(const Node* newChild)
935 if (newChild->isTextNode())
938 if (newChild->isHTMLElement()) {
939 const HTMLElement* child = static_cast<const HTMLElement*>(newChild);
940 if (inlineTagList()->contains(child->tagQName().localName().impl()))
942 if (blockTagList()->contains(child->tagQName().localName().impl()))
944 return !isRecognizedTagName(child->tagQName()); // Accept custom html tags
950 bool HTMLElement::inInlineTagList(const Node* newChild)
952 if (newChild->isTextNode())
955 if (newChild->isHTMLElement()) {
956 const HTMLElement* child = static_cast<const HTMLElement*>(newChild);
957 if (inlineTagList()->contains(child->tagQName().localName().impl()))
959 return !isRecognizedTagName(child->tagQName()); // Accept custom html tags
965 bool HTMLElement::inBlockTagList(const Node* newChild)
967 if (newChild->isTextNode())
970 if (newChild->isHTMLElement()) {
971 const HTMLElement* child = static_cast<const HTMLElement*>(newChild);
972 return (blockTagList()->contains(child->tagQName().localName().impl()));
978 bool HTMLElement::checkDTD(const Node* newChild)
980 if (hasLocalName(addressTag) && newChild->hasTagName(pTag))
982 return inEitherTagList(newChild);
985 bool HTMLElement::rendererIsNeeded(RenderStyle *style)
987 if (hasLocalName(noscriptTag)) {
988 Settings* settings = document()->settings();
989 if (settings && settings->isJavaScriptEnabled())
992 return (document()->documentElement() == this) || (style->display() != NONE);
995 RenderObject* HTMLElement::createRenderer(RenderArena* arena, RenderStyle* style)
997 if (hasLocalName(wbrTag))
998 return new (arena) RenderWordBreak(this);
999 return RenderObject::createObject(this, style);
1002 HTMLFormElement* HTMLElement::findFormAncestor() const
1004 for (Node* ancestor = parentNode(); ancestor; ancestor = ancestor->parentNode())
1005 if (ancestor->hasTagName(formTag))
1006 return static_cast<HTMLFormElement*>(ancestor);
1010 HTMLFormElement* HTMLElement::virtualForm() const
1012 return findFormAncestor();
1015 } // namespace WebCore
1018 void dumpInnerHTML(WebCore::HTMLElement* element)
1020 printf("%s\n", element->innerHTML().ascii().data());