bool XMLHandler::startElement( const QString& namespaceURI, const QString& /*localName*/, const QString& qName, const QXmlAttributes& atts )
{
    if (m_currentNode->nodeType() == Node::TEXT_NODE)
	exitText();

    ElementImpl *newElement;
    if (namespaceURI.isNull())
	newElement = m_doc->createElement(qName);
    else
	newElement = m_doc->createElementNS(namespaceURI,qName);

    // ### handle exceptions
    int i;
    for (i = 0; i < atts.length(); i++)
	newElement->setAttribute(atts.localName(i),atts.value(i));
    if (m_currentNode->addChild(newElement)) {
	if (m_view)
	    newElement->attach(m_view);
	m_currentNode = newElement;
	return TRUE;
    }
    else {
	delete newElement;
	return FALSE;
    }
}
예제 #2
0
bool XMLHandler::startElement(const QString &namespaceURI, const QString & /*localName*/, const QString &qName, const QXmlAttributes &atts)
{
    if(currentNode()->nodeType() == Node::TEXT_NODE)
        exitText();

    DOMString nsURI;
    if(!namespaceURI.isNull())
        nsURI = DOMString(namespaceURI);
    else
        // No namespace declared, default to the no namespace
        nsURI = DOMString("");
    ElementImpl *newElement = m_doc->createElementNS(nsURI, qName);
    if(!newElement)
        return false;
    int i;
    for(i = 0; i < atts.length(); i++)
    {
        int exceptioncode = 0;
        QString uriString = atts.uri(i);
        QString qnString = atts.qName(i);
        fixUpNSURI(uriString, qnString);
        DOMString uri(uriString);
        DOMString qn(qnString);
        DOMString val(atts.value(i));
        newElement->setAttributeNS(uri, qn, val, exceptioncode);
        if(exceptioncode) // exception setting attributes
            return false;
    }

    if(newElement->id() == ID_SCRIPT || newElement->id() == makeId(xhtmlNamespace, ID_SCRIPT))
        static_cast< HTMLScriptElementImpl * >(newElement)->setCreatedByParser(true);

    // this is tricky. in general the node doesn't have to attach to the one it's in. as far
    // as standards go this is wrong, but there's literally thousands of documents where
    // we see <p><ul>...</ul></p>. the following code is there for those cases.
    // when we can't attach to the currently holding us node we try to attach to its parent
    bool attached = false;
    for(NodeImpl *current = currentNode(); current; current = current->parent())
    {
        attached = current->addChild(newElement);
        if(attached)
            break;
    }
    if(attached)
    {
        if(m_view && !newElement->attached() && !m_doc->hasPendingSheets())
            newElement->attach();
        pushNode(newElement);
        return true;
    }
    else
    {
        delete newElement;
        return false;
    }

    // ### DOM spec states: "if there is no markup inside an element's content, the text is contained in a
    // single object implementing the Text interface that is the only child of the element."... do we
    // need to ensure that empty elements always have an empty text child?
}