From 5dbbb06562fefd5aaafe3175438522a9efe18f1d Mon Sep 17 00:00:00 2001 From: "vidur%netscape.com" Date: Tue, 11 May 1999 20:20:40 +0000 Subject: [PATCH] Added IsValidFragment to nsIDOMNSRange interface and CreateRange to nsIDOMNSDocument. --- dom/public/coreDom/nsIDOMNSDocument.h | 5 +++ dom/public/idl/coreDom/Document.idl | 2 +- dom/public/idl/range/Range.idl | 1 + dom/public/range/nsIDOMNSRange.h | 4 +++ dom/src/coreDOM/nsJSDocument.cpp | 44 +++++++++++++++++++++++++ dom/src/range/nsJSRange.cpp | 46 ++++++++++++++++++++++++++- 6 files changed, 100 insertions(+), 2 deletions(-) diff --git a/dom/public/coreDom/nsIDOMNSDocument.h b/dom/public/coreDom/nsIDOMNSDocument.h index d09a6a94f21..e3c693d8e35 100644 --- a/dom/public/coreDom/nsIDOMNSDocument.h +++ b/dom/public/coreDom/nsIDOMNSDocument.h @@ -26,6 +26,7 @@ class nsIDOMElement; class nsIDOMStyleSheetCollection; +class nsIDOMRange; #define NS_IDOMNSDOCUMENT_IID \ { 0xa6cf90cd, 0x15b3, 0x11d2, \ @@ -38,18 +39,22 @@ public: NS_IMETHOD GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets)=0; NS_IMETHOD CreateElementWithNameSpace(const nsString& aTagName, const nsString& aNameSpace, nsIDOMElement** aReturn)=0; + + NS_IMETHOD CreateRange(nsIDOMRange** aReturn)=0; }; #define NS_DECL_IDOMNSDOCUMENT \ NS_IMETHOD GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets); \ NS_IMETHOD CreateElementWithNameSpace(const nsString& aTagName, const nsString& aNameSpace, nsIDOMElement** aReturn); \ + NS_IMETHOD CreateRange(nsIDOMRange** aReturn); \ #define NS_FORWARD_IDOMNSDOCUMENT(_to) \ NS_IMETHOD GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets) { return _to GetStyleSheets(aStyleSheets); } \ NS_IMETHOD CreateElementWithNameSpace(const nsString& aTagName, const nsString& aNameSpace, nsIDOMElement** aReturn) { return _to CreateElementWithNameSpace(aTagName, aNameSpace, aReturn); } \ + NS_IMETHOD CreateRange(nsIDOMRange** aReturn) { return _to CreateRange(aReturn); } \ #endif // nsIDOMNSDocument_h__ diff --git a/dom/public/idl/coreDom/Document.idl b/dom/public/idl/coreDom/Document.idl index 364445dc366..965fcd21699 100644 --- a/dom/public/idl/coreDom/Document.idl +++ b/dom/public/idl/coreDom/Document.idl @@ -29,7 +29,7 @@ readonly attribute StyleSheetCollection styleSheets; Element createElementWithNameSpace(in DOMString tagName, in DOMString nameSpace) - raises(DOMException); + raises(DOMException); Range createRange(); }; interface EventCapturer : EventReceiver { diff --git a/dom/public/idl/range/Range.idl b/dom/public/idl/range/Range.idl index 02ab9324270..97cc34803e1 100644 --- a/dom/public/idl/range/Range.idl +++ b/dom/public/idl/range/Range.idl @@ -46,4 +46,5 @@ interface NSRange { /* IID: { 0xa6cf90f2, 0x15b3, 0x11d2, \ { 0x93, 0x2e, 0x00, 0x80, 0x5f, 0x8a, 0xdd, 0x32 } } */ void insertFragment(in DOMString fragment); + boolean isValidFragment(in DOMString fragment); }; diff --git a/dom/public/range/nsIDOMNSRange.h b/dom/public/range/nsIDOMNSRange.h index 5fe0d2c5c84..83a32840456 100644 --- a/dom/public/range/nsIDOMNSRange.h +++ b/dom/public/range/nsIDOMNSRange.h @@ -34,16 +34,20 @@ public: static const nsIID& GetIID() { static nsIID iid = NS_IDOMNSRANGE_IID; return iid; } NS_IMETHOD InsertFragment(const nsString& aFragment)=0; + + NS_IMETHOD IsValidFragment(const nsString& aFragment, PRBool* aReturn)=0; }; #define NS_DECL_IDOMNSRANGE \ NS_IMETHOD InsertFragment(const nsString& aFragment); \ + NS_IMETHOD IsValidFragment(const nsString& aFragment, PRBool* aReturn); \ #define NS_FORWARD_IDOMNSRANGE(_to) \ NS_IMETHOD InsertFragment(const nsString& aFragment) { return _to InsertFragment(aFragment); } \ + NS_IMETHOD IsValidFragment(const nsString& aFragment, PRBool* aReturn) { return _to IsValidFragment(aFragment, aReturn); } \ #endif // nsIDOMNSRange_h__ diff --git a/dom/src/coreDOM/nsJSDocument.cpp b/dom/src/coreDOM/nsJSDocument.cpp index c19bea8296f..611810106bb 100644 --- a/dom/src/coreDOM/nsJSDocument.cpp +++ b/dom/src/coreDOM/nsJSDocument.cpp @@ -39,6 +39,7 @@ #include "nsIDOMNSDocument.h" #include "nsIDOMComment.h" #include "nsIDOMDocumentFragment.h" +#include "nsIDOMRange.h" #include "nsIDOMEventCapturer.h" #include "nsIDOMNodeList.h" @@ -59,6 +60,7 @@ static NS_DEFINE_IID(kIEntityReferenceIID, NS_IDOMENTITYREFERENCE_IID); static NS_DEFINE_IID(kINSDocumentIID, NS_IDOMNSDOCUMENT_IID); static NS_DEFINE_IID(kICommentIID, NS_IDOMCOMMENT_IID); static NS_DEFINE_IID(kIDocumentFragmentIID, NS_IDOMDOCUMENTFRAGMENT_IID); +static NS_DEFINE_IID(kIRangeIID, NS_IDOMRANGE_IID); static NS_DEFINE_IID(kIEventCapturerIID, NS_IDOMEVENTCAPTURER_IID); static NS_DEFINE_IID(kINodeListIID, NS_IDOMNODELIST_IID); @@ -75,6 +77,7 @@ NS_DEF_PTR(nsIDOMEntityReference); NS_DEF_PTR(nsIDOMNSDocument); NS_DEF_PTR(nsIDOMComment); NS_DEF_PTR(nsIDOMDocumentFragment); +NS_DEF_PTR(nsIDOMRange); NS_DEF_PTR(nsIDOMEventCapturer); NS_DEF_PTR(nsIDOMNodeList); @@ -610,6 +613,46 @@ NSDocumentCreateElementWithNameSpace(JSContext *cx, JSObject *obj, uintN argc, j } +// +// Native method CreateRange +// +PR_STATIC_CALLBACK(JSBool) +NSDocumentCreateRange(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMDocument *privateThis = (nsIDOMDocument*)JS_GetPrivate(cx, obj); + nsIDOMNSDocument *nativeThis = nsnull; + if (NS_OK != privateThis->QueryInterface(kINSDocumentIID, (void **)&nativeThis)) { + JS_ReportError(cx, "Object must be of type NSDocument"); + return JS_FALSE; + } + + JSBool rBool = JS_FALSE; + nsIDOMRange* nativeRet; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 0) { + + if (NS_OK != nativeThis->CreateRange(&nativeRet)) { + return JS_FALSE; + } + + nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, rval); + } + else { + JS_ReportError(cx, "Function createRange requires 0 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + // // Native method CaptureEvent // @@ -740,6 +783,7 @@ static JSFunctionSpec DocumentMethods[] = {"createEntityReference", DocumentCreateEntityReference, 1}, {"getElementsByTagName", DocumentGetElementsByTagName, 1}, {"createElementWithNameSpace", NSDocumentCreateElementWithNameSpace, 2}, + {"createRange", NSDocumentCreateRange, 0}, {"captureEvent", EventCapturerCaptureEvent, 1}, {"releaseEvent", EventCapturerReleaseEvent, 1}, {0} diff --git a/dom/src/range/nsJSRange.cpp b/dom/src/range/nsJSRange.cpp index 28297441ba2..4532ea71a5b 100644 --- a/dom/src/range/nsJSRange.cpp +++ b/dom/src/range/nsJSRange.cpp @@ -942,6 +942,49 @@ NSRangeInsertFragment(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsv } +// +// Native method IsValidFragment +// +PR_STATIC_CALLBACK(JSBool) +NSRangeIsValidFragment(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMRange *privateThis = (nsIDOMRange*)JS_GetPrivate(cx, obj); + nsIDOMNSRange *nativeThis = nsnull; + if (NS_OK != privateThis->QueryInterface(kINSRangeIID, (void **)&nativeThis)) { + JS_ReportError(cx, "Object must be of type NSRange"); + return JS_FALSE; + } + + JSBool rBool = JS_FALSE; + PRBool nativeRet; + nsAutoString b0; + + *rval = JSVAL_NULL; + + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + if (argc >= 1) { + + nsJSUtils::nsConvertJSValToString(b0, cx, argv[0]); + + if (NS_OK != nativeThis->IsValidFragment(b0, &nativeRet)) { + return JS_FALSE; + } + + *rval = BOOLEAN_TO_JSVAL(nativeRet); + } + else { + JS_ReportError(cx, "Function isValidFragment requires 1 parameters"); + return JS_FALSE; + } + + return JS_TRUE; +} + + /***********************************************************************/ // // class for Range @@ -998,6 +1041,7 @@ static JSFunctionSpec RangeMethods[] = {"clone", RangeClone, 0}, {"toString", RangeToString, 0}, {"insertFragment", NSRangeInsertFragment, 1}, + {"isValidFragment", NSRangeIsValidFragment, 1}, {0} }; @@ -1079,7 +1123,7 @@ extern "C" NS_DOM nsresult NS_InitRangeClass(nsIScriptContext *aContext, void ** // // Method for creating a new Range JavaScript object // -NS_DOM nsresult NS_NewScriptRange(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn) +extern "C" NS_DOM nsresult NS_NewScriptRange(nsIScriptContext *aContext, nsISupports *aSupports, nsISupports *aParent, void **aReturn) { NS_PRECONDITION(nsnull != aContext && nsnull != aSupports && nsnull != aReturn, "null argument to NS_NewScriptRange"); JSObject *proto;