From dad38a973d7d2b7864010a4eccb2047c511cb433 Mon Sep 17 00:00:00 2001 From: "rpotts%netscape.com" Date: Thu, 7 Nov 2002 05:17:21 +0000 Subject: [PATCH] bug #174404 (r=adam, sr=jst) Support the optional FOR and EVENT attributes on the SCRIPT tag git-svn-id: svn://10.0.0.236/trunk@133264 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/content/base/public/MANIFEST | 2 + mozilla/content/base/public/MANIFEST_IDL | 1 + mozilla/content/base/public/Makefile.in | 2 + mozilla/content/base/public/nsIDocument.h | 3 + .../base/public/nsIScriptEventHandler.idl | 78 ++++ .../base/public/nsIScriptEventManager.idl | 83 +++++ mozilla/content/base/src/Makefile.in | 1 + mozilla/content/base/src/nsDocument.cpp | 15 + mozilla/content/base/src/nsDocument.h | 4 + .../content/base/src/nsScriptEventManager.cpp | 144 ++++++++ .../content/base/src/nsScriptEventManager.h | 63 ++++ .../content/html/content/src/nsHTMLAtomList.h | 1 + .../html/content/src/nsHTMLScriptElement.cpp | 333 ++++++++++++++++-- .../html/document/src/nsHTMLContentSink.cpp | 20 +- mozilla/content/macbuild/content.xml | 30 ++ mozilla/content/macbuild/contentIDL.xml | 60 ++++ .../content/shared/public/nsHTMLAtomList.h | 1 + .../xul/document/src/nsXULDocument.cpp | 7 + .../content/xul/document/src/nsXULDocument.h | 1 + 19 files changed, 814 insertions(+), 35 deletions(-) create mode 100644 mozilla/content/base/public/nsIScriptEventHandler.idl create mode 100644 mozilla/content/base/public/nsIScriptEventManager.idl create mode 100644 mozilla/content/base/src/nsScriptEventManager.cpp create mode 100644 mozilla/content/base/src/nsScriptEventManager.h diff --git a/mozilla/content/base/public/MANIFEST b/mozilla/content/base/public/MANIFEST index 245cbe5ec1d..d90bb661c86 100644 --- a/mozilla/content/base/public/MANIFEST +++ b/mozilla/content/base/public/MANIFEST @@ -29,3 +29,5 @@ nsITextContent.h nsIContentList.h nsIFrameLoader.h mozISanitizingSerializer.h +nsIScriptEventHandler.h +nsIScriptEventManager.h diff --git a/mozilla/content/base/public/MANIFEST_IDL b/mozilla/content/base/public/MANIFEST_IDL index 196b0702614..e679d3fc62b 100644 --- a/mozilla/content/base/public/MANIFEST_IDL +++ b/mozilla/content/base/public/MANIFEST_IDL @@ -15,3 +15,4 @@ nsIPrintStatusFeedback.idl nsIPrintProgressParams.idl nsIDragDropHandler.idl nsIDragDropOverride.idl +nsIScriptEventManager.idl diff --git a/mozilla/content/base/public/Makefile.in b/mozilla/content/base/public/Makefile.in index 364be3aef1c..d9fabef469e 100644 --- a/mozilla/content/base/public/Makefile.in +++ b/mozilla/content/base/public/Makefile.in @@ -78,6 +78,8 @@ XPIDLSRCS = \ nsIPrintStatusFeedback.idl \ nsIDragDropHandler.idl \ nsIDragDropOverride.idl \ + nsIScriptEventHandler.idl \ + nsIScriptEventManager.idl \ $(NULL) include $(topsrcdir)/config/rules.mk diff --git a/mozilla/content/base/public/nsIDocument.h b/mozilla/content/base/public/nsIDocument.h index 4c9a7009caa..49811f8fe61 100644 --- a/mozilla/content/base/public/nsIDocument.h +++ b/mozilla/content/base/public/nsIDocument.h @@ -81,6 +81,7 @@ class nsISupportsArray; class nsIScriptLoader; class nsString; class nsIContentSink; +class nsIScriptEventManager; // IID for the nsIDocument interface #define NS_IDOCUMENT_IID \ @@ -394,6 +395,8 @@ public: * Get the container (docshell) for this document. */ NS_IMETHOD GetContainer(nsISupports **aContainer) = 0; + + NS_IMETHOD GetScriptEventManager(nsIScriptEventManager **aResult) = 0; }; diff --git a/mozilla/content/base/public/nsIScriptEventHandler.idl b/mozilla/content/base/public/nsIScriptEventHandler.idl new file mode 100644 index 00000000000..08a3d15caa5 --- /dev/null +++ b/mozilla/content/base/public/nsIScriptEventHandler.idl @@ -0,0 +1,78 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Communicator + * + * The Initial Developer of the Original Code is + * Netscape Communications + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rick Potts + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsISupports.idl" + + +[uuid(375f787f-8d3a-4344-b540-293d5645c22c)] +interface nsIScriptEventHandler : nsISupports +{ + /** + * IsSameEvent: + * + * Determines if this script event handler is bound to the given + * object and event name. + * + * @param aObjectName Name of the object raising the event. + * + * @param aEventName Name of the event being raised. + * + * @param aArgCount Number of arguments expected by the event handler. + * + */ + boolean IsSameEvent(in AString aObjectName, + in AString aEventName, + in unsigned long aArgCount); + + /** + * Invoke: + * + * Executes this script handler. + * + * @param aTargetObject Object to which the script handler is bound. + * + * @param aArgs Array of arguments passed to the script handler. + * This is an array of jsvals. + * + * @param aArgCount Number of elements in the aArgs array. + * + */ + void Invoke(in nsISupports aTargetObject, + in voidPtr aArgs, + in unsigned long aArgCount); +}; diff --git a/mozilla/content/base/public/nsIScriptEventManager.idl b/mozilla/content/base/public/nsIScriptEventManager.idl new file mode 100644 index 00000000000..3c49e957ffd --- /dev/null +++ b/mozilla/content/base/public/nsIScriptEventManager.idl @@ -0,0 +1,83 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Communicator + * + * The Initial Developer of the Original Code is + * Netscape Communications + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rick Potts + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsISupports.idl" + + +[uuid(b6427ace-dc77-495b-ab21-43baeb52bc27)] +interface nsIScriptEventManager : nsISupports +{ + /* + * FindEventHandler: + * + * Returns a script event handler for the given object and event name. + * + * @param aObjectName Name of the object raising the event. + * + * @param aEventName Name of the event being raised. + * + * @param aArgCount Number of arguments expected by the event handler. + * + */ + nsISupports FindEventHandler(in AString aObjectName, + in AString aEventName, + in unsigned long aArgCount); + + /* + * + * InvokeEventHandler: + * + * Executes the script handler that was returned from FindEventHandler. + * + * @param aHandler Script event handler that was returned from + * FindEventHandler. + * + * @param aTargetObject Object to which the script handler is bound. + * + * @param aArgs Array of arguments passed to the script handler. + * This is an array of jsvals. + * + * @param aArgCount Number of elements in the aArgs array. + * + */ + void InvokeEventHandler(in nsISupports aHandler, + in nsISupports aTargetObject, + in voidPtr aArgs, + in unsigned long aArgCount); +}; + diff --git a/mozilla/content/base/src/Makefile.in b/mozilla/content/base/src/Makefile.in index 3cf4803b738..02fedbbe0aa 100644 --- a/mozilla/content/base/src/Makefile.in +++ b/mozilla/content/base/src/Makefile.in @@ -101,6 +101,7 @@ CPPSRCS = \ nsStyleLinkElement.cpp \ nsContentAreaDragDrop.cpp \ nsFrameLoader.cpp \ + nsScriptEventManager.cpp \ $(NULL) # we don't want the shared lib, but we want to force the creation of a diff --git a/mozilla/content/base/src/nsDocument.cpp b/mozilla/content/base/src/nsDocument.cpp index c2f154198b8..dbea7ffa105 100644 --- a/mozilla/content/base/src/nsDocument.cpp +++ b/mozilla/content/base/src/nsDocument.cpp @@ -146,6 +146,8 @@ static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID); #include "nsIHttpChannel.h" #include "nsIPref.h" +#include "nsScriptEventManager.h" + /** * A struct that holds all the information about a radio group. */ @@ -3751,6 +3753,19 @@ nsDocument::GetContainer(nsISupports **aContainer) return NS_OK; } +NS_IMETHODIMP +nsDocument::GetScriptEventManager(nsIScriptEventManager **aResult) +{ + if (!mScriptEventManager) { + mScriptEventManager = new nsScriptEventManager(this); + // automatically AddRefs + } + + *aResult = mScriptEventManager; + NS_IF_ADDREF(*aResult); + + return NS_OK; +} nsresult nsDocument::GetRadioGroup(const nsAString& aName, nsRadioGroupStruct **aRadioGroup) diff --git a/mozilla/content/base/src/nsDocument.h b/mozilla/content/base/src/nsDocument.h index bfc63430897..dd872b5fb69 100644 --- a/mozilla/content/base/src/nsDocument.h +++ b/mozilla/content/base/src/nsDocument.h @@ -71,6 +71,7 @@ #include "nsICSSLoader.h" #include "nsIDOMXPathEvaluator.h" #include "nsIRadioGroupContainer.h" +#include "nsIScriptEventManager.h" #include "pldhash.h" @@ -521,6 +522,7 @@ public: NS_IMETHOD RemoveReference(void *aKey, nsISupports **aOldReference); NS_IMETHOD SetContainer(nsISupports *aContainer); NS_IMETHOD GetContainer(nsISupports **aContainer); + NS_IMETHOD GetScriptEventManager(nsIScriptEventManager **aResult); // nsIRadioGroupContainer NS_IMETHOD WalkRadioGroup(const nsAString& aName, @@ -676,6 +678,8 @@ protected: nsString mContentLanguage; nsString mContentType; + nsCOMPtr mScriptEventManager; + private: // These are not implemented and not supported. nsDocument(const nsDocument& aOther); diff --git a/mozilla/content/base/src/nsScriptEventManager.cpp b/mozilla/content/base/src/nsScriptEventManager.cpp new file mode 100644 index 00000000000..a85eebfea84 --- /dev/null +++ b/mozilla/content/base/src/nsScriptEventManager.cpp @@ -0,0 +1,144 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Communicator + * + * The Initial Developer of the Original Code is + * Netscape Communications + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rick Potts + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsScriptEventManager.h" + +#include "nsString.h" +#include "nsReadableUtils.h" + +#include "nsIDOMNode.h" +#include "nsIDOMHTMLScriptElement.h" +#include "nsIDOMHTMLObjectElement.h" +#include "nsIDOMDocument.h" + +#include "nsIScriptElement.h" + +#include "nsIScriptEventHandler.h" +#include "nsIDocument.h" + +nsScriptEventManager::nsScriptEventManager(nsIDOMDocument *aDocument) +{ + nsresult rv; + + NS_INIT_ISUPPORTS(); + + rv = aDocument->GetElementsByTagName(NS_LITERAL_STRING("script"), + getter_AddRefs(mScriptElements)); + +} + + +nsScriptEventManager::~nsScriptEventManager() +{ +} + + +NS_IMPL_ISUPPORTS1(nsScriptEventManager, nsIScriptEventManager) + + +NS_IMETHODIMP +nsScriptEventManager::FindEventHandler(const nsAString &aObjectName, + const nsAString &aEventName, + PRUint32 aArgCount, + nsISupports **aScriptHandler) +{ + nsresult rv; + + if (!mScriptElements) { + return NS_ERROR_FAILURE; + } + + // Null out the return value... + if (!aScriptHandler) { + return NS_ERROR_NULL_POINTER; + } + *aScriptHandler = nsnull; + + // Get the number of script elements in the current document... + PRUint32 count = 0; + rv = mScriptElements->GetLength(&count); + if (NS_FAILED(rv)) { + return rv; + } + + // + // Iterate over all of the SCRIPT elements looking for a handler. + // + // Walk the list backwards in order to pick up the most recently + // defined script handler (if more than one is present)... + // + nsCOMPtr node; + nsCOMPtr handler; + + while (count--) { + rv = mScriptElements->Item(count, getter_AddRefs(node)); + if (NS_FAILED(rv)) break; + + // Check if the SCRIPT element is an event handler. + handler = do_QueryInterface(node, &rv); + if (NS_FAILED(rv)) continue; + + PRBool bFound = PR_FALSE; + rv = handler->IsSameEvent(aObjectName, aEventName, aArgCount, &bFound); + + if (NS_SUCCEEDED(rv) && bFound) { + *aScriptHandler = handler; + NS_ADDREF(*aScriptHandler); + + return NS_OK; + } + } + + // Errors just fall of the end and return 'rv' + return rv; +} + +NS_IMETHODIMP +nsScriptEventManager::InvokeEventHandler(nsISupports *aHandler, + nsISupports *aTargetObject, + void * aArgs, + PRUint32 aArgCount) +{ + nsCOMPtr handler(do_QueryInterface(aHandler)); + + if (!handler) { + return NS_ERROR_FAILURE; + } + + return handler->Invoke(aTargetObject, aArgs, aArgCount); +} diff --git a/mozilla/content/base/src/nsScriptEventManager.h b/mozilla/content/base/src/nsScriptEventManager.h new file mode 100644 index 00000000000..6232d0d7311 --- /dev/null +++ b/mozilla/content/base/src/nsScriptEventManager.h @@ -0,0 +1,63 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla Communicator + * + * The Initial Developer of the Original Code is + * Netscape Communications + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Rick Potts + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + + +#include "nsCOMPtr.h" +#include "nsAString.h" +#include "nsVoidArray.h" + +#include "nsIScriptEventManager.h" +#include "nsIDOMNodeList.h" +#include "nsIDOMDocument.h" + +class nsScriptEventManager : public nsIScriptEventManager +{ + + +public: + nsScriptEventManager(nsIDOMDocument *aDocument); + + NS_DECL_ISUPPORTS + NS_DECL_NSISCRIPTEVENTMANAGER + +protected: + virtual ~nsScriptEventManager(); + +private: + nsCOMPtr mScriptElements; +}; diff --git a/mozilla/content/html/content/src/nsHTMLAtomList.h b/mozilla/content/html/content/src/nsHTMLAtomList.h index b1789d1133e..0c64e08c5f7 100644 --- a/mozilla/content/html/content/src/nsHTMLAtomList.h +++ b/mozilla/content/html/content/src/nsHTMLAtomList.h @@ -144,6 +144,7 @@ HTML_ATOM(em, "em") HTML_ATOM(embed, "embed") HTML_ATOM(encoding, "encoding") HTML_ATOM(enctype, "enctype") +HTML_ATOM(_event, "event") HTML_ATOM(face, "face") HTML_ATOM(fieldset, "fieldset") HTML_ATOM(fieldsetContentPseudo, ":-moz-fieldset-content") diff --git a/mozilla/content/html/content/src/nsHTMLScriptElement.cpp b/mozilla/content/html/content/src/nsHTMLScriptElement.cpp index 4f5c54a27af..a30b07fe07b 100644 --- a/mozilla/content/html/content/src/nsHTMLScriptElement.cpp +++ b/mozilla/content/html/content/src/nsHTMLScriptElement.cpp @@ -51,6 +51,271 @@ #include "nsGUIEvent.h" #include "nsIURI.h" +#include "nsUnicharUtils.h" // for nsCaseInsensitiveCaseComparator() +#include "jsapi.h" +#include "nsIScriptContext.h" +#include "nsIScriptGlobalObject.h" +#include "nsIXPConnect.h" +#include "nsIServiceManagerUtils.h" +#include "nsIScriptEventHandler.h" +#include "nsIDOMDocument.h" + + +// +// Helper class used to support