Commit 22eb61b8 authored by Lars Knoll's avatar Lars Knoll
Browse files

assignment to derived classes works now in the DOM.

So you can now do things like:

	Node n = ....;
	Element e;
	e = n;
	if(e.isNull())
		printf("error. n is not an Element\n");
	else
		// do some work with e

svn path=/trunk/kdelibs/; revision=34613
parent ae83ceb4
......@@ -39,12 +39,10 @@ DOMImplementation::DOMImplementation()
DOMImplementation::DOMImplementation(const DOMImplementation &)
{
// ####
}
DOMImplementation &DOMImplementation::operator = (const DOMImplementation &)
{
// ###
return *this;
}
......@@ -55,7 +53,7 @@ DOMImplementation::~DOMImplementation()
bool DOMImplementation::hasFeature( const DOMString &feature, const DOMString &version )
{
// no valid implementation at the moment... ;-)
if(feature == "HTML" && version == "0.1") return true;
if(feature == "HTML" && version == "1") return true;
return false;
}
......@@ -88,6 +86,17 @@ Document::Document(DocumentImpl *i) : Node(i)
{
}
Document &Document::operator = (const Node &other)
{
if(other.nodeType() != DOCUMENT_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
Document &Document::operator = (const Document &other)
{
Node::operator =(other);
......@@ -158,6 +167,11 @@ NodeList Document::getElementsByTagName( const DOMString &tagName )
return ((DocumentImpl *)impl)->getElementsByTagName( tagName );
}
bool Document::isHTMLDocument()
{
if(!impl) return false;
return ((DocumentImpl *)impl)->isHTMLDocument();
}
// ----------------------------------------------------------------------------
......@@ -169,6 +183,17 @@ DocumentFragment::DocumentFragment(const DocumentFragment &other) : Node(other)
{
}
DocumentFragment &DocumentFragment::operator = (const Node &other)
{
if(other.nodeType() != DOCUMENT_FRAGMENT_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
DocumentFragment &DocumentFragment::operator = (const DocumentFragment &other)
{
Node::operator =(other);
......@@ -193,6 +218,18 @@ DocumentType::DocumentType(const DocumentType &other) : Node(other)
{
}
DocumentType &DocumentType::operator = (const Node &other)
{
if(other.nodeType() != DOCUMENT_TYPE_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
DocumentType &DocumentType::operator = (const DocumentType &other)
{
Node::operator =(other);
......
......@@ -100,13 +100,14 @@ class Document : public Node
{
public:
Document();
/**
/**
* don't create an implementation if false
* use at own risk
*/
Document(bool);
Document(const Document &other);
Document & operator = (const Node &other);
Document & operator = (const Document &other);
~Document();
......@@ -270,9 +271,15 @@ public:
*/
NodeList getElementsByTagName ( const DOMString &tagname );
/**
* @internal
* not part of the DOM
*/
bool isHTMLDocument();
protected:
Document( DocumentImpl *i);
friend class Node;
};
......@@ -328,6 +335,7 @@ public:
DocumentFragment();
DocumentFragment(const DocumentFragment &other);
DocumentFragment & operator = (const Node &other);
DocumentFragment & operator = (const DocumentFragment &other);
~DocumentFragment();
......@@ -358,6 +366,7 @@ public:
DocumentType();
DocumentType(const DocumentType &other);
DocumentType & operator = (const Node &other);
DocumentType & operator = (const DocumentType &other);
~DocumentType();
......
......@@ -50,6 +50,8 @@ public:
virtual unsigned short nodeType() const;
virtual bool isDocumentNode() { return true; }
virtual bool isHTMLDocument() { return false; }
//DocumentType doctype() const;
//DOMImplementation implementation() const;
......
......@@ -42,6 +42,17 @@ Attr::Attr( AttrImpl *_impl )
impl->ref();
}
Attr &Attr::operator = (const Node &other)
{
if(other.nodeType() != ATTRIBUTE_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
Attr &Attr::operator = (const Attr &other)
{
Node::operator =(other);
......@@ -89,6 +100,17 @@ Element::Element(ElementImpl *impl) : Node(impl)
{
}
Element &Element::operator = (const Node &other)
{
if(other.nodeType() != ELEMENT_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
Element &Element::operator = (const Element &other)
{
Node::operator =(other);
......@@ -151,3 +173,9 @@ void Element::normalize()
{
if (impl) ((ElementImpl *)impl)->normalize();
}
bool Element::isHTMLElement()
{
if(!impl) return false;
return ((ElementImpl *)impl)->isHTMLElement();
}
......@@ -84,6 +84,7 @@ public:
Attr(const Attr &other);
Attr & operator = (const Node &other);
Attr & operator = (const Attr &other);
~Attr();
......@@ -143,7 +144,7 @@ public:
void setValue( const DOMString & );
protected:
Attr( AttrImpl *_impl );
};
......@@ -190,6 +191,7 @@ public:
Element(const Element &other);
Element & operator = (const Node &other);
Element & operator = (const Element &other);
~Element();
......@@ -349,6 +351,12 @@ public:
*/
void normalize ( );
/**
* @internal
* not part of the DOM
*/
bool isHTMLElement();
protected:
Element(ElementImpl *_impl);
......
......@@ -94,6 +94,8 @@ public:
virtual unsigned short nodeType() const;
virtual bool isElementNode() { return true; }
virtual bool isHTMLElement() { return false; }
DOMString tagName() const;
DOMString getAttribute ( const DOMString &name );
......
......@@ -78,7 +78,7 @@ Node NamedNodeMap::setNamedItem( const Node &arg )
throw DOMException(DOMException::HIERARCHY_REQUEST_ERR);
// ### check the attribute is not already used somewhere
return (NodeImpl *)((ElementImpl *)map)->setAttributeNode( (AttrImpl *)arg.impl );
}
......@@ -249,6 +249,12 @@ Node Node::cloneNode( bool deep )
return 0;
}
unsigned short Node::id() const
{
if(!impl) return 0;
return impl->id();
}
//-----------------------------------------------------------------------------
NodeList::NodeList()
......
......@@ -536,13 +536,32 @@ public:
*/
Node cloneNode ( bool deep );
/**
* @internal
* not part of the DOM.
* @returns the element id, in case this is an element, 0 otherwise
*/
unsigned short id() const;
/**
* tests if this Node is 0. Useful especially, if casting to a derived class:
*
* <pre>
* Node n = .....;
* // try to convert into an Element:
* Element e = n;
* if( e.isNull() )
* printf("node isn't an element node\n");
*/
bool isNull() { return (impl == 0); }
protected:
NodeImpl *impl;
Node( NodeImpl *_impl);
};
class Node;
class NodeListImpl;
/**
......
......@@ -37,6 +37,19 @@ CharacterData::CharacterData(const CharacterData &other) : Node(other)
{
}
CharacterData &CharacterData::operator = (const Node &other)
{
if( other.nodeType() != CDATA_SECTION_NODE &&
other.nodeType() != TEXT_NODE &&
other.nodeType() != COMMENT_NODE )
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
CharacterData &CharacterData::operator = (const CharacterData &other)
{
Node::operator =(other);
......@@ -102,6 +115,17 @@ Comment::Comment(const Comment &other) : CharacterData(other)
{
}
Comment &Comment::operator = (const Node &other)
{
if(other.nodeType() != COMMENT_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
Comment &Comment::operator = (const Comment &other)
{
CharacterData::operator =(other);
......@@ -122,9 +146,21 @@ Text::Text(const Text &other) : CharacterData(other)
{
}
Text &Text::operator = (const Node &other)
{
if(other.nodeType() != TEXT_NODE &&
other.nodeType() != CDATA_SECTION_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
Text &Text::operator = (const Text &other)
{
CharacterData::operator =(other);
Node::operator =(other);
return *this;
}
......
......@@ -46,6 +46,7 @@ public:
CharacterData();
CharacterData(const CharacterData &other);
CharacterData & operator = (const Node &other);
CharacterData & operator = (const CharacterData &other);
~CharacterData();
......@@ -213,6 +214,7 @@ public:
Comment();
Comment(const Comment &other);
Comment & operator = (const Node &other);
Comment & operator = (const Comment &other);
~Comment();
......@@ -252,6 +254,7 @@ public:
Text();
Text(const Text &other);
Text & operator = (const Node &other);
Text & operator = (const Text &other);
~Text();
......
......@@ -35,8 +35,20 @@ CDATASection::CDATASection(const CDATASection &) : Text()
{
}
CDATASection &CDATASection::operator = (const CDATASection &)
CDATASection &CDATASection::operator = (const Node &other)
{
if(other.nodeType() != CDATA_SECTION_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
CDATASection &CDATASection::operator = (const CDATASection &other)
{
Node::operator =(other);
return *this;
}
......@@ -53,8 +65,20 @@ Entity::Entity(const Entity &) : Node()
{
}
Entity &Entity::operator = (const Entity &)
Entity &Entity::operator = (const Node &other)
{
if(other.nodeType() != ENTITY_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
Entity &Entity::operator = (const Entity &other)
{
Node::operator =(other);
return *this;
}
......@@ -86,8 +110,20 @@ EntityReference::EntityReference(const EntityReference &) : Node()
{
}
EntityReference &EntityReference::operator = (const EntityReference &)
EntityReference &EntityReference::operator = (const Node &other)
{
if(other.nodeType() != ENTITY_REFERENCE_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
EntityReference &EntityReference::operator = (const EntityReference &other)
{
Node::operator =(other);
return *this;
}
......@@ -105,8 +141,20 @@ Notation::Notation(const Notation &) : Node()
{
}
Notation &Notation::operator = (const Notation &)
Notation &Notation::operator = (const Node &other)
{
if(other.nodeType() != NOTATION_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
Notation &Notation::operator = (const Notation &other)
{
Node::operator =(other);
return *this;
}
......@@ -134,8 +182,20 @@ ProcessingInstruction::ProcessingInstruction(const ProcessingInstruction &)
{
}
ProcessingInstruction &ProcessingInstruction::operator = (const ProcessingInstruction &)
ProcessingInstruction &ProcessingInstruction::operator = (const Node &other)
{
if(other.nodeType() != PROCESSING_INSTRUCTION_NODE)
{
impl = 0;
return *this;
}
Node::operator =(other);
return *this;
}
ProcessingInstruction &ProcessingInstruction::operator = (const ProcessingInstruction &other)
{
Node::operator =(other);
return *this;
}
......
......@@ -55,6 +55,7 @@ public:
CDATASection();
CDATASection(const CDATASection &other);
CDATASection & operator = (const Node &other);
CDATASection & operator = (const CDATASection &other);
~CDATASection();
......@@ -107,6 +108,7 @@ public:
Entity();
Entity(const Entity &other);
Entity & operator = (const Node &other);
Entity & operator = (const Entity &other);
~Entity();
......@@ -168,6 +170,7 @@ public:
EntityReference();
EntityReference(const EntityReference &other);
EntityReference & operator = (const Node &other);
EntityReference & operator = (const EntityReference &other);
~EntityReference();
......@@ -196,6 +199,7 @@ public:
Notation();
Notation(const Notation &other);
Notation & operator = (const Node &other);
Notation & operator = (const Notation &other);
~Notation();
......@@ -228,6 +232,7 @@ public:
ProcessingInstruction();
ProcessingInstruction(const ProcessingInstruction &other);
ProcessingInstruction & operator = (const Node &other);
ProcessingInstruction & operator = (const ProcessingInstruction &other);
~ProcessingInstruction();
......
......@@ -29,6 +29,7 @@
using namespace DOM;
#include "khtmlattrs.h"
#include "khtmltags.h"
HTMLBodyElement::HTMLBodyElement() : HTMLElement()
{
......@@ -42,6 +43,17 @@ HTMLBodyElement::HTMLBodyElement(HTMLBodyElementImpl *impl) : HTMLElement(impl)
{
}
HTMLBodyElement &HTMLBodyElement::operator = (const Node &other)
{
if(other.id() != ID_BODY)
{
impl = 0;
return *this;
}
Node::operator = (other);
return *this;
}
HTMLBodyElement &HTMLBodyElement::operator = (const HTMLBodyElement &other)
{
HTMLElement::operator = (other);
......@@ -132,6 +144,17 @@ HTMLFrameElement::HTMLFrameElement(HTMLFrameElementImpl *impl) : HTMLElement(imp
{
}
HTMLFrameElement &HTMLFrameElement::operator = (const Node &other)
{
if(other.id() != ID_FRAME)
{
impl = 0;
return *this;
}
Node::operator = (other);
return *this;
}
HTMLFrameElement &HTMLFrameElement::operator = (const HTMLFrameElement &other)
{
HTMLElement::operator = (other);
......@@ -250,6 +273,17 @@ HTMLFrameSetElement::HTMLFrameSetElement(HTMLFrameSetElementImpl *impl) : HTMLEl
{
}
HTMLFrameSetElement &HTMLFrameSetElement::operator = (const Node &other)
{
if(other.id() != ID_FRAMESET)
{
impl = 0;
return *this;
}
Node::operator = (other);
return *this;
}
HTMLFrameSetElement &HTMLFrameSetElement::operator = (const HTMLFrameSetElement &other)
{
HTMLElement::operator = (other);
......@@ -296,6 +330,17 @@ HTMLHeadElement::HTMLHeadElement(HTMLHeadElementImpl *impl) : HTMLElement(impl)
{
}
HTMLHeadElement &HTMLHeadElement::operator = (const Node &other)
{
if(other.id() != ID_HEAD)
{
impl = 0;
return *this;
}
Node::operator = (other);
return *this;
}
HTMLHeadElement &HTMLHeadElement::operator = (const HTMLHeadElement &other)
{
HTMLElement::operator = (other);
......@@ -331,6 +376,17 @@ HTMLHtmlElement::HTMLHtmlElement(HTMLHtmlElementImpl *impl) : HTMLElement(impl)
{
}
HTMLHtmlElement &HTMLHtmlElement::operator = (const Node &other)
{
if(other.id() != ID_HTML)
{
impl = 0;
return *this;
}
Node::operator = (other);
return *this;
}
HTMLHtmlElement &HTMLHtmlElement::operator = (const HTMLHtmlElement &other)
{
HTMLElement::operator = (other);
......
......@@ -46,6 +46,7 @@ protected:
public:
HTMLBodyElement & operator = (const HTMLBodyElement &other);
HTMLBodyElement & operator = (const Node &other);
<