📄 html_elementimpl.cpp
字号:
// -*- 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(DocumentPtr *doc) : ElementImpl( doc ){ m_htmlCompat = doc && doc->document() ? doc->document()->htmlMode() != DocumentImpl::XHtml : false;}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(); setChanged(); // in case of a CSS selector on id getDocument()->incDOMTreeVersion(); break; case ATTR_CLASS: case ATTR_NAME: setChanged(); // in case of a CSS selector on class/name getDocument()->incDOMTreeVersion(); break; case ATTR_STYLE: setHasStyle(); 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;// 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();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -