📄 html_formimpl.cpp
字号:
}// -------------------------------------------------------------------------HTMLLabelElementImpl::HTMLLabelElementImpl(DocumentPtr *doc) : HTMLElementImpl(doc){}HTMLLabelElementImpl::~HTMLLabelElementImpl(){}const DOMString HTMLLabelElementImpl::nodeName() const{ return "LABEL";}ushort HTMLLabelElementImpl::id() const{ return ID_LABEL;}void HTMLLabelElementImpl::parseAttribute(AttrImpl *attr){ switch(attr->attrId) { case ATTR_ONFOCUS: setHTMLEventListener(EventImpl::FOCUS_EVENT, ownerDocument()->createHTMLEventListener(attr->value().string())); break; case ATTR_ONBLUR: setHTMLEventListener(EventImpl::BLUR_EVENT, ownerDocument()->createHTMLEventListener(attr->value().string())); break; default: HTMLElementImpl::parseAttribute(attr); }}ElementImpl *HTMLLabelElementImpl::formElement(){ DOMString formElementId = getAttribute(ATTR_FOR); if (formElementId.isNull() || formElementId.isEmpty()) return 0; return ownerDocument()->getElementById(formElementId);}// -------------------------------------------------------------------------HTMLLegendElementImpl::HTMLLegendElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f) : HTMLGenericFormElementImpl(doc, f){}HTMLLegendElementImpl::HTMLLegendElementImpl(DocumentPtr *doc) : HTMLGenericFormElementImpl(doc){}HTMLLegendElementImpl::~HTMLLegendElementImpl(){}const DOMString HTMLLegendElementImpl::nodeName() const{ return "LEGEND";}ushort HTMLLegendElementImpl::id() const{ return ID_LEGEND;}// -------------------------------------------------------------------------HTMLSelectElementImpl::HTMLSelectElementImpl(DocumentPtr *doc) : HTMLGenericFormElementImpl(doc){ m_multiple = false; view = 0; // 0 means invalid (i.e. not set) m_size = 0;}HTMLSelectElementImpl::HTMLSelectElementImpl(DocumentPtr *doc, HTMLFormElementImpl *f) : HTMLGenericFormElementImpl(doc, f){ m_multiple = false; view = 0; // 0 means invalid (i.e. not set) m_size = 0;}ushort HTMLSelectElementImpl::id() const{ return ID_SELECT;}DOMString HTMLSelectElementImpl::type() const{ return (m_multiple ? "select-multiple" : "select-one");}long HTMLSelectElementImpl::selectedIndex() const{ uint i; bool hasOption = false; for (i = 0; i < m_listItems.size(); i++) { if (m_listItems[i]->id() == ID_OPTION) hasOption = true; if (m_listItems[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(m_listItems[i])->selected()) return listToOptionIndex(int(i)); // selectedIndex is the *first* selected item; there may be others } return hasOption ? 0 : -1;}void HTMLSelectElementImpl::setSelectedIndex( long index ){ // deselect all other options and select only the new one int listIndex; for (listIndex = 0; listIndex < int(m_listItems.size()); listIndex++) { if (m_listItems[listIndex]->id() == ID_OPTION) static_cast<HTMLOptionElementImpl*>(m_listItems[listIndex])->setSelected(false); } listIndex = optionToListIndex(index); if (listIndex >= 0) static_cast<HTMLOptionElementImpl*>(m_listItems[listIndex])->setSelected(true); setChanged(true);}long HTMLSelectElementImpl::length() const{ int len = 0; uint i; for (i = 0; i < m_listItems.size(); i++) { if (m_listItems[i]->id() == ID_OPTION) len++; } return len;}void HTMLSelectElementImpl::add( const HTMLElement &element, const HTMLElement &before ){ if(element.isNull() || element.id() != ID_OPTION) return; int exceptioncode = 0; insertBefore(element.handle(), before.handle(), exceptioncode ); if (!exceptioncode) recalcListItems();}void HTMLSelectElementImpl::remove( long index ){ int exceptioncode = 0; int listIndex = optionToListIndex(index); if(listIndex < 0 || index >= int(m_listItems.size())) return; // ### what should we do ? remove the last item? removeChild(m_listItems[listIndex], exceptioncode); if( !exceptioncode ) recalcListItems();}DOMString HTMLSelectElementImpl::value( ){ uint i; for (i = 0; i < m_listItems.size(); i++) { if (m_listItems[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(m_listItems[i])->selected()) return static_cast<HTMLOptionElementImpl*>(m_listItems[i])->value(); } return 0;}void HTMLSelectElementImpl::setValue(DOMStringImpl* /*value*/){ // ### find the option with value() matching the given parameter // and make it the current selection.}QString HTMLSelectElementImpl::state( ){ QString state; QArray<HTMLGenericFormElementImpl*> items = listItems(); int l = items.count(); state.fill('.', l); for(int i = 0; i < l; i++) if(items[i]->id() == ID_OPTION && static_cast<HTMLOptionElementImpl*>(items[i])->selected()) state[i] = 'X'; return state;}void HTMLSelectElementImpl::restoreState(const QString &_state){ recalcListItems(); QString state = _state; if(!state.isEmpty() && !state.contains('X') && !m_multiple) { ASSERT("should not happen in restoreState!"); state[0] = 'X'; } QArray<HTMLGenericFormElementImpl*> items = listItems(); int l = items.count(); for(int i = 0; i < l; i++) { if(items[i]->id() == ID_OPTION) { HTMLOptionElementImpl* oe = static_cast<HTMLOptionElementImpl*>(items[i]); oe->setSelected(state[i] == 'X'); } } setChanged(true);}NodeImpl *HTMLSelectElementImpl::insertBefore ( NodeImpl *newChild, NodeImpl *refChild, int &exceptioncode ){ NodeImpl *result = HTMLGenericFormElementImpl::insertBefore(newChild,refChild, exceptioncode ); if (!exceptioncode) recalcListItems(); return result;}NodeImpl *HTMLSelectElementImpl::replaceChild ( NodeImpl *newChild, NodeImpl *oldChild, int &exceptioncode ){ NodeImpl *result = HTMLGenericFormElementImpl::replaceChild(newChild,oldChild, exceptioncode); if( !exceptioncode ) recalcListItems(); return result;}NodeImpl *HTMLSelectElementImpl::removeChild ( NodeImpl *oldChild, int &exceptioncode ){ NodeImpl *result = HTMLGenericFormElementImpl::removeChild(oldChild, exceptioncode); if( !exceptioncode ) recalcListItems(); return result;}NodeImpl *HTMLSelectElementImpl::appendChild ( NodeImpl *newChild, int &exceptioncode ){ NodeImpl *result = HTMLGenericFormElementImpl::appendChild(newChild, exceptioncode); if( !exceptioncode ) recalcListItems(); setChanged(true); return result;}void HTMLSelectElementImpl::parseAttribute(AttrImpl *attr){ switch(attr->attrId) { case ATTR_SIZE: m_size = QMAX( attr->val()->toInt(), 1 ); break; case ATTR_MULTIPLE: m_multiple = (attr->val() != 0); break; case ATTR_ACCESSKEY: // ### ignore for the moment break; case ATTR_ONFOCUS: setHTMLEventListener(EventImpl::FOCUS_EVENT, ownerDocument()->createHTMLEventListener(attr->value().string())); break; case ATTR_ONBLUR: setHTMLEventListener(EventImpl::BLUR_EVENT, ownerDocument()->createHTMLEventListener(attr->value().string())); break; case ATTR_ONCHANGE: setHTMLEventListener(EventImpl::CHANGE_EVENT, ownerDocument()->createHTMLEventListener(attr->value().string())); break; default: HTMLGenericFormElementImpl::parseAttribute(attr); }}void HTMLSelectElementImpl::attach(){ addCSSProperty(CSS_PROP_COLOR, "text"); setStyle(ownerDocument()->styleSelector()->styleForElement(this)); view = ownerDocument()->view(); khtml::RenderObject *r = _parent->renderer(); if(r && m_style->display() != NONE) { RenderSelect *f = new RenderSelect(view, this); if (f) { m_render = f; m_render->setStyle(m_style); r->addChild(m_render, nextRenderer()); } } HTMLElementImpl::attach();}bool HTMLSelectElementImpl::encoding(const QTextCodec* codec, khtml::encodingList& encoded_values, bool){ bool successful = false; QCString enc_name = fixUpfromUnicode(codec, _name.string().stripWhiteSpace()); uint i; for (i = 0; i < m_listItems.size(); i++) { if (m_listItems[i]->id() == ID_OPTION) { HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(m_listItems[i]); if (option->selected()) { encoded_values += enc_name; if (option->value().isNull()) encoded_values += fixUpfromUnicode(codec, option->text().string().stripWhiteSpace()); else encoded_values += fixUpfromUnicode(codec, option->value().string()); successful = true; } } } // ### this case should not happen. make sure that we select the first option // in any case. otherwise we have no consistency with the DOM interface. FIXME! // we return the first one if it was a combobox select if (!successful && !m_multiple && m_size <= 1 && m_listItems.size() && (m_listItems[0]->id() == ID_OPTION) ) { HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(m_listItems[0]); encoded_values += enc_name; if (option->value().isNull()) encoded_values += fixUpfromUnicode(codec, option->text().string().stripWhiteSpace()); else encoded_values += fixUpfromUnicode(codec, option->value().string()); successful = true; } return successful;}int HTMLSelectElementImpl::optionToListIndex(int optionIndex) const{ if (optionIndex < 0 || optionIndex >= int(m_listItems.size())) return -1; int listIndex = 0; int optionIndex2 = 0; for (; optionIndex2 < int(m_listItems.size()) && optionIndex2 <= optionIndex; listIndex++) { // not a typo! if (m_listItems[listIndex]->id() == ID_OPTION) optionIndex2++; } listIndex--; return listIndex;}int HTMLSelectElementImpl::listToOptionIndex(int listIndex) const{ if (listIndex < 0 || listIndex >= int(m_listItems.size()) || m_listItems[listIndex]->id() != ID_OPTION) return -1; int optionIndex = 0; // actual index of option not counting OPTGROUP entries that may be in list int i; for (i = 0; i < listIndex; i++) if (m_listItems[i]->id() == ID_OPTION) optionIndex++; return optionIndex;}void HTMLSelectElementImpl::recalcListItems(){ NodeImpl* current = firstChild(); bool inOptGroup = false; m_listItems.resize(0); bool foundSelected = false; while(current) { if (!inOptGroup && current->id() == ID_OPTGROUP && current->firstChild()) { // ### what if optgroup contains just comments? don't want one of no options in it... m_listItems.resize(m_listItems.size()+1); m_listItems[m_listItems.size()-1] = static_cast<HTMLGenericFormElementImpl*>(current); current = current->firstChild(); inOptGroup = true; } if (current->id() == ID_OPTION) { m_listItems.resize(m_listItems.size()+1); m_listItems[m_listItems.size()-1] = static_cast<HTMLGenericFormElementImpl*>(current); if (foundSelected && !m_multiple && static_cast<HTMLOptionElementImpl*>(current)->selected()) static_cast<HTMLOptionElementImpl*>(current)->setSelected(false); foundSelected = static_cast<HTMLOptionElementImpl*>(current)->selected(); } NodeImpl *parent = current->parentNode(); current = current->nextSibling(); if (!current) { if (inOptGroup) { current = parent->nextSibling(); inOptGroup = false; } } } if ( m_render ) static_cast<RenderSelect*>(m_render)->setOptionsChanged(true); setChanged(true);}void HTMLSelectElementImpl::reset(){ uint i; for (i = 0; i < m_listItems.size(); i++) { if (m_listItems[i]->id() == ID_OPTION) { HTMLOptionElementImpl *option = static_cast<HTMLOptionElementImpl*>(m_listItems[i]); bool selected = (!option->getAttribute(ATTR_SELECTED).isNull()); option->setSelected(selected); } } if ( m_render ) static_cast<RenderSelect*>(m_render)->setSelectionChanged(true); setChanged( true );}void HTMLSelectElementImpl::notifyOptionSelected(HTMLOptionElementImpl *selectedOption, bool selected){ if (selected && !m_multiple) { // deselect all other options uint i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -