From kde-commits Thu Mar 15 19:32:35 2007 From: Maks Orlovich Date: Thu, 15 Mar 2007 19:32:35 +0000 To: kde-commits Subject: branches/KDE/3.5/kdelibs/khtml Message-Id: <1173987155.228063.635.nullmailer () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=117398715824097 SVN commit 642894 by orlovich: Implement support for textContent, which websites use heavily on their mozilla paths as it does not do innerText.. M +16 -0 dom/dom_node.cpp M +35 -1 dom/dom_node.h M +7 -0 ecma/kjs_dom.cpp M +1 -1 ecma/kjs_dom.h M +16 -0 xml/dom_docimpl.cpp M +6 -0 xml/dom_docimpl.h M +15 -0 xml/dom_elementimpl.cpp M +4 -0 xml/dom_elementimpl.h M +31 -0 xml/dom_nodeimpl.cpp M +9 -0 xml/dom_nodeimpl.h M +10 -0 xml/dom_textimpl.cpp M +3 -0 xml/dom_textimpl.h --- branches/KDE/3.5/kdelibs/khtml/dom/dom_node.cpp #642893:642894 @@ -432,6 +432,22 @@ return impl->getRect(); } +DOMString Node::textContent( ) const +{ + if(impl) return impl->textContent(); + return DOMString(); +} + +void Node::setTextContent(const DOMString &content) const +{ + if (!impl) throw DOMException(DOMException::NOT_FOUND_ERR); + + int exceptioncode = 0; + impl->setTextContent( content, exceptioncode ); + if (exceptioncode) + throw DOMException(exceptioncode); +} + //----------------------------------------------------------------------------- NodeList::NodeList() --- branches/KDE/3.5/kdelibs/khtml/dom/dom_node.h #642893:642894 @@ -21,10 +21,14 @@ * This file includes excerpts from the Document Object Model (DOM) * Level 1 Specification (Recommendation) * http://www.w3.org/TR/REC-DOM-Level-1/ - * Copyright © World Wide Web Consortium , (Massachusetts Institute of + * Copyright World Wide Web Consortium , (Massachusetts Institute of * Technology , Institut National de Recherche en Informatique et en * Automatique , Keio University ). All Rights Reserved. * + * This file includes excerpts from the Document Object Model (DOM) + * Level 3 Core Specification (Recommendation) + * http://www.w3.org/TR/DOM-Level-3-Core/ + * Copyright ©2004 W3C® (MIT, ERCIM, Keio), All Rights Reserved. */ #ifndef _DOM_Node_h_ #define _DOM_Node_h_ @@ -751,6 +755,34 @@ bool hasAttributes ( ); /** + * Introduced in DOM Level 3 + * + * This attribute returns the text content of this node and its descendants. + * On getting, no serialization is performed, the returned string does not contain any markup. + * + * @since 3.5.7 + */ + DOMString textContent( ) const; + + /** + * Introduced in DOM Level 3 + * + * @see textContent + * + * On setting, any possible children this node may have are removed and, if the new + * string is not empty or null, replaced by a single Text node containing the string this attribute is set to. + * No parsing is performed, the input string is taken as pure textual content. + * + * @exception DOMException + * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. + * Note: KHTML will also raise this if setContent is called on things + * that do not have child nodes. + * + * @since 3.5.7 + */ + void setTextContent(const DOMString &content) const; + + /** * Introduced in DOM Level 2 * This method is from the EventTarget interface * @@ -836,6 +868,8 @@ */ bool dispatchEvent(const Event &evt); + + /** * @internal * not part of the DOM. --- branches/KDE/3.5/kdelibs/khtml/ecma/kjs_dom.cpp #642893:642894 @@ -130,6 +130,8 @@ prefix DOMNode::Prefix DontDelete localName DOMNode::LocalName DontDelete|ReadOnly ownerDocument DOMNode::OwnerDocument DontDelete|ReadOnly +# DOM3 + textContent DOMNode::TextContent DontDelete # Event handlers # IE also has: onactivate, onbefore*, oncontextmenu, oncontrolselect, oncut, # ondeactivate, ondrag*, ondrop, onfocusin, onfocusout, onhelp, onmousewheel, @@ -250,6 +252,8 @@ return getString(node.namespaceURI()); // Moz returns null if not set (dom/namespaces.html) case Prefix: return getString(node.prefix()); // Moz returns null if not set (dom/namespaces.html) + case TextContent: + return getString(node.textContent()); //DOM3 says return null, but I really should test mozilla.. case LocalName: return getString(node.localName()); // Moz returns null if not set (dom/namespaces.html) case OwnerDocument: @@ -392,6 +396,9 @@ case Prefix: node.setPrefix(value.toString(exec).string()); break; + case TextContent: + node.setTextContent(value.toString(exec).string()); + break; case OnAbort: setListener(exec,DOM::EventImpl::ABORT_EVENT,value); break; --- branches/KDE/3.5/kdelibs/khtml/ecma/kjs_dom.h #642893:642894 @@ -67,7 +67,7 @@ OnResize, OnSelect, OnSubmit, OnUnload, OffsetLeft, OffsetTop, OffsetWidth, OffsetHeight, OffsetParent, ClientWidth, ClientHeight, ScrollLeft, ScrollTop, - ScrollWidth, ScrollHeight, SourceIndex }; + ScrollWidth, ScrollHeight, SourceIndex, TextContent }; protected: DOM::Node node; --- branches/KDE/3.5/kdelibs/khtml/xml/dom_docimpl.cpp #642893:642894 @@ -763,6 +763,14 @@ return Node::DOCUMENT_NODE; } +DOMStringImpl* DocumentImpl::textContent() const +{ + return 0; +} + +void DocumentImpl::setTextContent( const DOMString&, int& ) +{} + ElementImpl *DocumentImpl::createHTMLElement( const DOMString &name ) { uint id = khtml::getTagID( name.string().lower().latin1(), name.string().length() ); @@ -2855,6 +2863,14 @@ return 0; } +DOMStringImpl* DocumentTypeImpl::textContent() const +{ + return 0; +} + +void DocumentTypeImpl::setTextContent( const DOMString&, int& ) +{} + NamedNodeMapImpl * DocumentTypeImpl::entities() const { if ( !m_entities ) { --- branches/KDE/3.5/kdelibs/khtml/xml/dom_docimpl.h #642893:642894 @@ -219,6 +219,9 @@ virtual DOMString nodeName() const; virtual unsigned short nodeType() const; + virtual DOMStringImpl* textContent() const; + virtual void setTextContent( const DOMString &text, int& exceptioncode ); + // Other methods (not part of DOM) virtual bool isDocumentNode() const { return true; } virtual bool isHTMLDocument() const { return false; } @@ -733,6 +736,9 @@ virtual bool childTypeAllowed( unsigned short type ); virtual NodeImpl *cloneNode ( bool deep ); + virtual DOMStringImpl* textContent() const; + virtual void setTextContent( const DOMString &text, int& exceptioncode ); + // Other methods (not part of DOM) void setName(const DOMString& n) { m_qualifiedName = n; } void setPublicId(const DOMString& publicId) { m_publicId = publicId; } --- branches/KDE/3.5/kdelibs/khtml/xml/dom_elementimpl.cpp #642893:642894 @@ -249,6 +249,21 @@ m_element = element; } +// Strictly speaking, these two methods should not be needed, but +// we can't fully deal with the mess that are DOM attributes right.. +DOMStringImpl* AttrImpl::textContent() const +{ + if (m_value) + return new DOMStringImpl(m_value->s, m_value->l); + else + return 0; +} + +void AttrImpl::setTextContent( const DOMString &text, int& exceptioncode ) +{ + setValue(text, exceptioncode); +} + // ------------------------------------------------------------------------- void AttributeImpl::setValue(DOMStringImpl *value, ElementImpl *element) --- branches/KDE/3.5/kdelibs/khtml/xml/dom_elementimpl.h #642893:642894 @@ -83,6 +83,10 @@ virtual void setNodeValue( const DOMString &, int &exceptioncode ); virtual NodeImpl *cloneNode ( bool deep ); + virtual DOMStringImpl* textContent() const; + virtual void setTextContent( const DOMString &text, int& exceptioncode ); + + // Other methods (not part of DOM) virtual bool isAttributeNode() const { return true; } virtual bool childAllowed( NodeImpl *newChild ); --- branches/KDE/3.5/kdelibs/khtml/xml/dom_nodeimpl.cpp #642893:642894 @@ -977,6 +977,21 @@ // return renderer() ? renderer()->maxOffset() : 1; } +DOMStringImpl* NodeImpl::textContent() const +{ + QString out; + for (NodeImpl *child = firstChild(); child != 0; child = child->nextSibling()) { + short type = child->nodeType(); + if (type != Node::COMMENT_NODE && type != Node::PROCESSING_INSTRUCTION_NODE) { + DOMStringImpl* kidText = child->textContent(); + if (kidText) + out += QConstString(kidText->s, kidText->l).string(); + delete kidText; + } + } + return new DOMStringImpl(out.unicode(), out.length()); +} + //------------------------------------------------------------------------- NodeBaseImpl::~NodeBaseImpl() @@ -1617,6 +1632,22 @@ } } +void NodeBaseImpl::setTextContent( const DOMString &text, int& exceptioncode ) +{ + // NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. + if (isReadOnly()) { + exceptioncode = DOMException::NO_MODIFICATION_ALLOWED_ERR; + return; + } + + removeChildren(); + + if ( !text.isEmpty() && !text.isNull() ) { + TextImpl *t = new TextImpl( docPtr(), text.implementation() ); + appendChild( t, exceptioncode ); + } +} + // --------------------------------------------------------------------------- NodeImpl *NodeListImpl::item( unsigned long index ) const { --- branches/KDE/3.5/kdelibs/khtml/xml/dom_nodeimpl.h #642893:642894 @@ -172,6 +172,13 @@ // qualified element name. virtual Id id() const { return 0; } + // These are the DOM 3 Core answer to innerText/setInnerText, and are used + // quite a bit since Mozilla doesn't do innerText. They do, however, behave slightly + // differently. The default implementation is for ELEMENT_NODE, ATTRIBUTE_NODE, + // ENTITY_NODE, ENTITY_REFERENCE_NODE, DOCUMENT_FRAGMENT_NODE. + virtual DOMStringImpl* textContent() const; + virtual void setTextContent( const DOMString &text, int& exceptioncode ) = 0; + enum IdType { AttributeId, ElementId, @@ -495,6 +502,8 @@ virtual NodeImpl *appendChild ( NodeImpl *newChild, int &exceptioncode ); virtual bool hasChildNodes ( ) const; + virtual void setTextContent( const DOMString &text, int& exceptioncode ); + // Other methods (not part of DOM) void removeChildren(); void cloneChildNodes(NodeImpl *clone); --- branches/KDE/3.5/kdelibs/khtml/xml/dom_textimpl.cpp #642893:642894 @@ -265,6 +265,16 @@ return r->maxOffset(); } +DOMStringImpl* CharacterDataImpl::textContent() const +{ + return new DOMStringImpl(str->s, str->l); +} + +void CharacterDataImpl::setTextContent( const DOMString &str, int& exceptioncode ) +{ + setData(str, exceptioncode); +} + // --------------------------------------------------------------------------- DOMString CommentImpl::nodeName() const --- branches/KDE/3.5/kdelibs/khtml/xml/dom_textimpl.h #642893:642894 @@ -60,6 +60,9 @@ virtual DOMString nodeValue() const; virtual void setNodeValue( const DOMString &_nodeValue, int &exceptioncode ); + virtual DOMStringImpl* textContent() const; + virtual void setTextContent( const DOMString &text, int& exceptioncode ); + // Other methods (not part of DOM) DOMStringImpl *string() const { return str; }