From c94f8a9104288444c36ad0251147b144d92d7f50 Mon Sep 17 00:00:00 2001 From: troy Date: Mon, 4 May 1998 20:34:37 +0000 Subject: [PATCH] Implemented DOM document's createTextNode() function, and changed insertBefore() handler to allow refChild to be null git-svn-id: svn://10.0.0.236/trunk@1103 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/src/nsDocument.cpp | 14 ------- mozilla/content/base/src/nsDocument.h | 4 +- .../html/document/src/nsHTMLDocument.cpp | 14 +++++++ .../html/document/src/nsHTMLDocument.h | 7 ++-- mozilla/dom/src/nsJSDocument.cpp | 25 +++++++++++- mozilla/dom/src/nsJSText.cpp | 2 +- mozilla/layout/base/src/nsDocument.cpp | 14 ------- mozilla/layout/base/src/nsDocument.h | 4 +- .../layout/html/base/src/nsHTMLContainer.cpp | 38 +++++++++++++------ .../html/document/src/nsHTMLDocument.cpp | 14 +++++++ .../layout/html/document/src/nsHTMLDocument.h | 7 ++-- 11 files changed, 91 insertions(+), 52 deletions(-) diff --git a/mozilla/content/base/src/nsDocument.cpp b/mozilla/content/base/src/nsDocument.cpp index 04f9c7079a5..a7f24bfcafb 100644 --- a/mozilla/content/base/src/nsDocument.cpp +++ b/mozilla/content/base/src/nsDocument.cpp @@ -583,20 +583,6 @@ nsresult nsDocument::CreateDocumentContext(nsIDOMDocumentContext **aDocContext) return NS_ERROR_NOT_IMPLEMENTED; } -nsresult nsDocument::CreateElement(nsString &aTagName, - nsIDOMAttributeList *aAttributes, - nsIDOMElement **aElement) -{ - //XXX TBI (currently there's a cheesy implementation in nsHTMLDocument) - return NS_ERROR_NOT_IMPLEMENTED; -} - -nsresult nsDocument::CreateTextNode(nsString &aData, nsIDOMText** aTextNode) -{ - //XXX TBI - return NS_ERROR_NOT_IMPLEMENTED; -} - nsresult nsDocument::CreateComment(nsString &aData, nsIDOMComment **aComment) { //XXX TBI diff --git a/mozilla/content/base/src/nsDocument.h b/mozilla/content/base/src/nsDocument.h index d0aa0956653..692cc5b584b 100644 --- a/mozilla/content/base/src/nsDocument.h +++ b/mozilla/content/base/src/nsDocument.h @@ -161,8 +161,8 @@ public: virtual nsresult CreateDocumentContext(nsIDOMDocumentContext **aDocContext); virtual nsresult CreateElement(nsString &aTagName, nsIDOMAttributeList *aAttributes, - nsIDOMElement **aElement); - virtual nsresult CreateTextNode(nsString &aData, nsIDOMText** aTextNode); + nsIDOMElement **aElement) = 0; + virtual nsresult CreateTextNode(nsString &aData, nsIDOMText** aTextNode) = 0; virtual nsresult CreateComment(nsString &aData, nsIDOMComment **aComment); virtual nsresult CreatePI(nsString &aName, nsString &aData, nsIDOMPI **aPI); virtual nsresult CreateAttribute(nsString &aName, diff --git a/mozilla/content/html/document/src/nsHTMLDocument.cpp b/mozilla/content/html/document/src/nsHTMLDocument.cpp index f37763f5ae8..c11ef775081 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/content/html/document/src/nsHTMLDocument.cpp @@ -28,9 +28,11 @@ #include "nsIImageMap.h" #include "nsIHTMLContent.h" #include "nsIDOMElement.h" +#include "nsIDOMText.h" static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID); static NS_DEFINE_IID(kIDOMElementIID, NS_IDOMELEMENT_IID); +static NS_DEFINE_IID(kIDOMTextIID, NS_IDOMTEXT_IID); static NS_DEFINE_IID(kIHTMLDocumentIID, NS_IHTMLDOCUMENT_IID); NS_LAYOUT nsresult @@ -208,6 +210,18 @@ nsresult nsHTMLDocument::CreateElement(nsString &aTagName, return rv; } +nsresult nsHTMLDocument::CreateTextNode(nsString &aData, nsIDOMText** aTextNode) +{ + nsIHTMLContent* text = nsnull; + nsresult rv = NS_NewHTMLText(&text, aData, aData.Length()); + + if (NS_OK == rv) { + rv = text->QueryInterface(kIDOMTextIID, (void**)aTextNode); + } + + return rv; +} + //---------------------------------------------------------------------- // Aggregation class to give nsHTMLDocument the nsIHTMLDocument interface diff --git a/mozilla/content/html/document/src/nsHTMLDocument.h b/mozilla/content/html/document/src/nsHTMLDocument.h index e6ac9d4a9f9..ca62208840c 100644 --- a/mozilla/content/html/document/src/nsHTMLDocument.h +++ b/mozilla/content/html/document/src/nsHTMLDocument.h @@ -42,9 +42,10 @@ public: return offsetof(nsHTMLDocument,mIHTMLDocument); } - virtual nsresult CreateElement(nsString &aTagName, - nsIDOMAttributeList *aAttributes, - nsIDOMElement **aElement); + virtual nsresult CreateElement(nsString &aTagName, + nsIDOMAttributeList *aAttributes, + nsIDOMElement **aElement); + virtual nsresult CreateTextNode(nsString &aData, nsIDOMText** aTextNode); protected: virtual void AddStyleSheetToSet(nsIStyleSheet* aSheet, nsIStyleSet* aSet); diff --git a/mozilla/dom/src/nsJSDocument.cpp b/mozilla/dom/src/nsJSDocument.cpp index c24bba32600..bbdd86cf6a3 100644 --- a/mozilla/dom/src/nsJSDocument.cpp +++ b/mozilla/dom/src/nsJSDocument.cpp @@ -23,6 +23,7 @@ #include "nsIDOMDocument.h" #include "nsIDOMNode.h" #include "nsIDOMElement.h" +#include "nsIDOMText.h" #include "nsString.h" static NS_DEFINE_IID(kIScriptObjectIID, NS_ISCRIPTOBJECT_IID); @@ -195,7 +196,29 @@ CreateElement(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval PR_STATIC_CALLBACK(JSBool) CreateTextNode(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { - //XXX TBI + nsIDOMDocument *document = (nsIDOMDocument*)JS_GetPrivate(cx, obj); + + if (nsnull != document) { + NS_ASSERTION(1 == argc, "unexpected argument count"); + + JSString* jsstring1 = JSVAL_TO_STRING(argv[0]); + nsString data(JS_GetStringChars(jsstring1)); + nsIDOMText* textNode; + + if (NS_OK == document->CreateTextNode(data, &textNode)) { + // get the js object + nsIScriptObjectOwner *owner = nsnull; + if (NS_OK == textNode->QueryInterface(kIScriptObjectOwnerIID, (void**)&owner)) { + JSObject *object = nsnull; + if (NS_OK == owner->GetScriptObject(cx, (void**)&object)) { + // set the return value + *rval = OBJECT_TO_JSVAL(object); + } + NS_RELEASE(owner); + } + NS_RELEASE(textNode); + } + } return JS_TRUE; } diff --git a/mozilla/dom/src/nsJSText.cpp b/mozilla/dom/src/nsJSText.cpp index e3ad7f4c3d0..1023557efca 100644 --- a/mozilla/dom/src/nsJSText.cpp +++ b/mozilla/dom/src/nsJSText.cpp @@ -42,7 +42,7 @@ PR_STATIC_CALLBACK(JSBool) GetTextProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp) { nsIDOMText *text = (nsIDOMText*)JS_GetPrivate(cx, obj); - NS_ASSERTION(nsnull != text, "null pointer"); + // NS_ASSERTION(nsnull != text, "null pointer"); if (JSVAL_IS_INT(id)) { switch(JSVAL_TO_INT(id)) { diff --git a/mozilla/layout/base/src/nsDocument.cpp b/mozilla/layout/base/src/nsDocument.cpp index 04f9c7079a5..a7f24bfcafb 100644 --- a/mozilla/layout/base/src/nsDocument.cpp +++ b/mozilla/layout/base/src/nsDocument.cpp @@ -583,20 +583,6 @@ nsresult nsDocument::CreateDocumentContext(nsIDOMDocumentContext **aDocContext) return NS_ERROR_NOT_IMPLEMENTED; } -nsresult nsDocument::CreateElement(nsString &aTagName, - nsIDOMAttributeList *aAttributes, - nsIDOMElement **aElement) -{ - //XXX TBI (currently there's a cheesy implementation in nsHTMLDocument) - return NS_ERROR_NOT_IMPLEMENTED; -} - -nsresult nsDocument::CreateTextNode(nsString &aData, nsIDOMText** aTextNode) -{ - //XXX TBI - return NS_ERROR_NOT_IMPLEMENTED; -} - nsresult nsDocument::CreateComment(nsString &aData, nsIDOMComment **aComment) { //XXX TBI diff --git a/mozilla/layout/base/src/nsDocument.h b/mozilla/layout/base/src/nsDocument.h index d0aa0956653..692cc5b584b 100644 --- a/mozilla/layout/base/src/nsDocument.h +++ b/mozilla/layout/base/src/nsDocument.h @@ -161,8 +161,8 @@ public: virtual nsresult CreateDocumentContext(nsIDOMDocumentContext **aDocContext); virtual nsresult CreateElement(nsString &aTagName, nsIDOMAttributeList *aAttributes, - nsIDOMElement **aElement); - virtual nsresult CreateTextNode(nsString &aData, nsIDOMText** aTextNode); + nsIDOMElement **aElement) = 0; + virtual nsresult CreateTextNode(nsString &aData, nsIDOMText** aTextNode) = 0; virtual nsresult CreateComment(nsString &aData, nsIDOMComment **aComment); virtual nsresult CreatePI(nsString &aName, nsString &aData, nsIDOMPI **aPI); virtual nsresult CreateAttribute(nsString &aName, diff --git a/mozilla/layout/html/base/src/nsHTMLContainer.cpp b/mozilla/layout/html/base/src/nsHTMLContainer.cpp index 8f506ba65a5..bc640a86b66 100644 --- a/mozilla/layout/html/base/src/nsHTMLContainer.cpp +++ b/mozilla/layout/html/base/src/nsHTMLContainer.cpp @@ -783,23 +783,37 @@ nsresult nsHTMLContainer::GetFirstChild(nsIDOMNode **aNode) nsresult nsHTMLContainer::InsertBefore(nsIDOMNode *newChild, nsIDOMNode *refChild) { - nsIContent* content = nsnull; - nsresult res = refChild->QueryInterface(kIContentIID, (void**)&content); - NS_ASSERTION(NS_OK == res, "Must be an nsIContent"); + NS_PRECONDITION(nsnull != newChild, "Null new child"); + + // Get the nsIContent interface for the new content + nsIContent* newContent = nsnull; + nsresult res = newChild->QueryInterface(kIContentIID, (void**)&newContent); + NS_ASSERTION(NS_OK == res, "New child must be an nsIContent"); + if (NS_OK == res) { - PRInt32 pos = IndexOf(content); - if (pos >= 0) { - nsIContent* newContent = nsnull; - res = newChild->QueryInterface(kIContentIID, (void**)&newContent); - NS_ASSERTION(NS_OK == res, "Must be an nsIContent"); + if (nsnull == refChild) { + // Append the new child to the end + if (PR_FALSE == AppendChild(newContent)) { + res == NS_ERROR_FAILURE; + } + } else { + nsIContent* content = nsnull; + + // Get the index of where to insert the new child + res = refChild->QueryInterface(kIContentIID, (void**)&content); + NS_ASSERTION(NS_OK == res, "Ref child must be an nsIContent"); if (NS_OK == res) { - if (PR_FALSE == InsertChildAt(newContent, pos)) { - res = NS_ERROR_FAILURE; + PRInt32 pos = IndexOf(content); + if (pos >= 0) { + if (PR_FALSE == InsertChildAt(newContent, pos)) { + res = NS_ERROR_FAILURE; + } } - NS_RELEASE(newContent); + NS_RELEASE(content); } } - NS_RELEASE(content); + + NS_RELEASE(newContent); } return res; diff --git a/mozilla/layout/html/document/src/nsHTMLDocument.cpp b/mozilla/layout/html/document/src/nsHTMLDocument.cpp index f37763f5ae8..c11ef775081 100644 --- a/mozilla/layout/html/document/src/nsHTMLDocument.cpp +++ b/mozilla/layout/html/document/src/nsHTMLDocument.cpp @@ -28,9 +28,11 @@ #include "nsIImageMap.h" #include "nsIHTMLContent.h" #include "nsIDOMElement.h" +#include "nsIDOMText.h" static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID); static NS_DEFINE_IID(kIDOMElementIID, NS_IDOMELEMENT_IID); +static NS_DEFINE_IID(kIDOMTextIID, NS_IDOMTEXT_IID); static NS_DEFINE_IID(kIHTMLDocumentIID, NS_IHTMLDOCUMENT_IID); NS_LAYOUT nsresult @@ -208,6 +210,18 @@ nsresult nsHTMLDocument::CreateElement(nsString &aTagName, return rv; } +nsresult nsHTMLDocument::CreateTextNode(nsString &aData, nsIDOMText** aTextNode) +{ + nsIHTMLContent* text = nsnull; + nsresult rv = NS_NewHTMLText(&text, aData, aData.Length()); + + if (NS_OK == rv) { + rv = text->QueryInterface(kIDOMTextIID, (void**)aTextNode); + } + + return rv; +} + //---------------------------------------------------------------------- // Aggregation class to give nsHTMLDocument the nsIHTMLDocument interface diff --git a/mozilla/layout/html/document/src/nsHTMLDocument.h b/mozilla/layout/html/document/src/nsHTMLDocument.h index e6ac9d4a9f9..ca62208840c 100644 --- a/mozilla/layout/html/document/src/nsHTMLDocument.h +++ b/mozilla/layout/html/document/src/nsHTMLDocument.h @@ -42,9 +42,10 @@ public: return offsetof(nsHTMLDocument,mIHTMLDocument); } - virtual nsresult CreateElement(nsString &aTagName, - nsIDOMAttributeList *aAttributes, - nsIDOMElement **aElement); + virtual nsresult CreateElement(nsString &aTagName, + nsIDOMAttributeList *aAttributes, + nsIDOMElement **aElement); + virtual nsresult CreateTextNode(nsString &aData, nsIDOMText** aTextNode); protected: virtual void AddStyleSheetToSet(nsIStyleSheet* aSheet, nsIStyleSet* aSet);