diff --git a/content/base/public/nsIDocument.h b/content/base/public/nsIDocument.h index bc9dd916ae34..2e86ba6ad645 100644 --- a/content/base/public/nsIDocument.h +++ b/content/base/public/nsIDocument.h @@ -138,6 +138,8 @@ public: virtual PRInt32 GetNumberOfStyleSheets() = 0; virtual nsIStyleSheet* GetStyleSheetAt(PRInt32 aIndex) = 0; virtual void AddStyleSheet(nsIStyleSheet* aSheet) = 0; + virtual void SetStyleSheetDisabledState(nsIStyleSheet* aSheet, + PRBool mDisabled) = 0; /** * Set the object from which a document can get a script context. diff --git a/content/base/public/nsIDocumentObserver.h b/content/base/public/nsIDocumentObserver.h index d7d8f885fc23..72311cf023bd 100644 --- a/content/base/public/nsIDocumentObserver.h +++ b/content/base/public/nsIDocumentObserver.h @@ -187,6 +187,22 @@ public: NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet) = 0; + /** + * A StyleSheet has just disabled or enabled. + * This method is called automatically when the disabled state + * of a StyleSheet gets changed. The style sheet passes this + * notification to the document. The notification is passed on + * to all of the document observers. + * + * @param aDocument The document being observed + * @param aStyleSheet the StyleSheet that has been added + * @param aDisabled PR_TRUE if the sheet is disabled, PR_FALSE if + * it is enabled + */ + NS_IMETHOD StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled) = 0; + /** * The document is in the process of being destroyed. * This method is called automatically during document diff --git a/content/base/src/nsContentList.h b/content/base/src/nsContentList.h index 1c178ac0ca81..cd4a280d889c 100644 --- a/content/base/src/nsContentList.h +++ b/content/base/src/nsContentList.h @@ -89,7 +89,10 @@ public: nsIContent* aChild, PRInt32 aIndexInContainer); NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, - nsIStyleSheet* aStyleSheet) { return NS_OK; } + nsIStyleSheet* aStyleSheet) { return NS_OK; } + NS_IMETHOD StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled) { return NS_OK; } NS_IMETHOD DocumentWillBeDestroyed(nsIDocument *aDocument); protected: diff --git a/content/base/src/nsDocument.cpp b/content/base/src/nsDocument.cpp index 77bb55d573b6..29b488086b9e 100644 --- a/content/base/src/nsDocument.cpp +++ b/content/base/src/nsDocument.cpp @@ -23,6 +23,7 @@ #include "nsIURLGroup.h" #include "nsString.h" #include "nsIContent.h" +#include "nsIDocumentObserver.h" #include "nsIStyleSet.h" #include "nsIStyleSheet.h" #include "nsIPresShell.h" @@ -37,6 +38,8 @@ #include "nsIEventStateManager.h" #include "nsContentList.h" #include "nsIDOMEventListener.h" +#include "nsIDOMStyleSheet.h" +#include "nsIDOMStyleSheetCollection.h" #include "nsCSSPropIDs.h" #include "nsCSSProps.h" @@ -56,6 +59,7 @@ static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID); #include "nsIDOMElement.h" static NS_DEFINE_IID(kIDOMDocumentIID, NS_IDOMDOCUMENT_IID); +static NS_DEFINE_IID(kIDOMNSDocumentIID, NS_IDOMNSDOCUMENT_IID); static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID); static NS_DEFINE_IID(kIDOMElementIID, NS_IDOMELEMENT_IID); static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID); @@ -66,6 +70,246 @@ static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID); static NS_DEFINE_IID(kIPrivateDOMEventIID, NS_IPRIVATEDOMEVENT_IID); static NS_DEFINE_IID(kIEventListenerManagerIID, NS_IEVENTLISTENERMANAGER_IID); static NS_DEFINE_IID(kIPostDataIID, NS_IPOSTDATA_IID); +static NS_DEFINE_IID(kIDOMStyleSheetCollectionIID, NS_IDOMSTYLESHEETCOLLECTION_IID); +static NS_DEFINE_IID(kIDOMStyleSheetIID, NS_IDOMSTYLESHEET_IID); +static NS_DEFINE_IID(kIDocumentObserverIID, NS_IDOCUMENT_OBSERVER_IID); +static NS_DEFINE_IID(kICSSStyleSheetIID, NS_ICSS_STYLE_SHEET_IID); + +class nsDOMStyleSheetCollection : public nsIDOMStyleSheetCollection, + public nsIScriptObjectOwner, + public nsIDocumentObserver +{ +public: + nsDOMStyleSheetCollection(nsIDocument *aDocument); + ~nsDOMStyleSheetCollection(); + + NS_DECL_ISUPPORTS + NS_DECL_IDOMSTYLESHEETCOLLECTION + + NS_IMETHOD BeginUpdate(nsIDocument *aDocument) { return NS_OK; } + NS_IMETHOD EndUpdate(nsIDocument *aDocument) { return NS_OK; } + NS_IMETHOD BeginLoad(nsIDocument *aDocument) { return NS_OK; } + NS_IMETHOD EndLoad(nsIDocument *aDocument) { return NS_OK; } + NS_IMETHOD BeginReflow(nsIDocument *aDocument, + nsIPresShell* aShell) { return NS_OK; } + NS_IMETHOD EndReflow(nsIDocument *aDocument, + nsIPresShell* aShell) { return NS_OK; } + NS_IMETHOD ContentChanged(nsIDocument *aDocument, + nsIContent* aContent, + nsISupports* aSubContent) { return NS_OK; } + NS_IMETHOD AttributeChanged(nsIDocument *aDocument, + nsIContent* aContent, + nsIAtom* aAttribute, + PRInt32 aHint) { return NS_OK; } + NS_IMETHOD ContentAppended(nsIDocument *aDocument, + nsIContent* aContainer, + PRInt32 aNewIndexInContainer) + { return NS_OK; } + NS_IMETHOD ContentInserted(nsIDocument *aDocument, + nsIContent* aContainer, + nsIContent* aChild, + PRInt32 aIndexInContainer) { return NS_OK; } + NS_IMETHOD ContentReplaced(nsIDocument *aDocument, + nsIContent* aContainer, + nsIContent* aOldChild, + nsIContent* aNewChild, + PRInt32 aIndexInContainer) { return NS_OK; } + NS_IMETHOD ContentRemoved(nsIDocument *aDocument, + nsIContent* aContainer, + nsIContent* aChild, + PRInt32 aIndexInContainer) { return NS_OK; } + NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet); + NS_IMETHOD StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled) { return NS_OK; } + NS_IMETHOD DocumentWillBeDestroyed(nsIDocument *aDocument); + + // nsIScriptObjectOwner interface + NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject); + NS_IMETHOD SetScriptObject(void* aScriptObject); + +protected: + PRInt32 mLength; + nsIDocument* mDocument; + void* mScriptObject; +}; + +nsDOMStyleSheetCollection::nsDOMStyleSheetCollection(nsIDocument *aDocument) +{ + NS_INIT_REFCNT(); + mLength = -1; + // Not reference counted to avoid circular references. + // The document will tell us when its going away. + mDocument = aDocument; + mDocument->AddObserver(this); + mScriptObject = nsnull; +} + +nsDOMStyleSheetCollection::~nsDOMStyleSheetCollection() +{ + if (nsnull != mDocument) { + mDocument->RemoveObserver(this); + } + mDocument = nsnull; +} + +NS_IMPL_ADDREF(nsDOMStyleSheetCollection) +NS_IMPL_RELEASE(nsDOMStyleSheetCollection) + +nsresult +nsDOMStyleSheetCollection::QueryInterface(REFNSIID aIID, void** aInstancePtrResult) +{ + if (NULL == aInstancePtrResult) { + return NS_ERROR_NULL_POINTER; + } + + static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + if (aIID.Equals(kIDOMStyleSheetCollectionIID)) { + nsIDOMStyleSheetCollection *tmp = this; + *aInstancePtrResult = (void*) tmp; + AddRef(); + return NS_OK; + } + if (aIID.Equals(kIScriptObjectOwnerIID)) { + nsIScriptObjectOwner *tmp = this; + *aInstancePtrResult = (void*) tmp; + AddRef(); + return NS_OK; + } + if (aIID.Equals(kIDocumentObserverIID)) { + nsIDocumentObserver *tmp = this; + *aInstancePtrResult = (void*) tmp; + AddRef(); + return NS_OK; + } + if (aIID.Equals(kISupportsIID)) { + nsIDOMStyleSheetCollection *tmp = this; + nsISupports *tmp2 = tmp; + *aInstancePtrResult = (void*) tmp2; + AddRef(); + return NS_OK; + } + return NS_NOINTERFACE; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::GetLength(PRUint32* aLength) +{ + if (nsnull != mDocument) { + // XXX Find the number and then cache it. We'll use the + // observer notification to figure out if new ones have + // been added or removed. + if (-1 == mLength) { + PRUint32 count = 0; + PRInt32 i, imax = mDocument->GetNumberOfStyleSheets(); + + for (i = 0; i < imax; i++) { + nsIStyleSheet *sheet = mDocument->GetStyleSheetAt(i); + nsIDOMStyleSheet *domss; + + if (NS_OK == sheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { + count++; + NS_RELEASE(domss); + } + + NS_RELEASE(sheet); + } + mLength = count; + } + *aLength = mLength; + } + else { + *aLength = 0; + } + + return NS_OK; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::Item(PRUint32 aIndex, nsIDOMStyleSheet** aReturn) +{ + *aReturn = nsnull; + if (nsnull != mDocument) { + PRUint32 count = 0; + PRInt32 i, imax = mDocument->GetNumberOfStyleSheets(); + + // XXX Not particularly efficient, but does anyone care? + for (i = 0; (i < imax) && (nsnull == *aReturn); i++) { + nsIStyleSheet *sheet = mDocument->GetStyleSheetAt(i); + nsIDOMStyleSheet *domss; + + if (NS_OK == sheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { + if (count++ == aIndex) { + *aReturn = domss; + NS_ADDREF(domss); + } + NS_RELEASE(domss); + } + + NS_RELEASE(sheet); + } + } + + return NS_OK; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) +{ + nsresult res = NS_OK; + + if (nsnull == mScriptObject) { + nsISupports *supports = (nsISupports *)(nsIDOMStyleSheetCollection *)this; + nsISupports *parent = (nsISupports *)mDocument; + + // XXX Should be done through factory + res = NS_NewScriptStyleSheetCollection(aContext, + supports, + parent, + (void**)&mScriptObject); + } + *aScriptObject = mScriptObject; + + return res; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::SetScriptObject(void* aScriptObject) +{ + mScriptObject = aScriptObject; + return NS_OK; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::StyleSheetAdded(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet) +{ + if (-1 != mLength) { + nsIDOMStyleSheet *domss; + if (NS_OK == aStyleSheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { + mLength++; + NS_RELEASE(domss); + } + } + + return NS_OK; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::DocumentWillBeDestroyed(nsIDocument *aDocument) +{ + if (nsnull != mDocument) { + aDocument->RemoveObserver(this); + mDocument = nsnull; + } + + return NS_OK; +} + +// ================================================================== +// = +// ================================================================== NS_LAYOUT nsresult NS_NewPostData(PRBool aIsFile, char* aData, @@ -153,6 +397,7 @@ nsDocument::nsDocument() if (NS_OK != NS_NewSelection(&mSelection)) { printf("*************** Error: nsDocument::nsDocument - Creation of Selection failed!\n"); } + mDOMStyleSheets = nsnull; Init();/* XXX */ } @@ -193,6 +438,12 @@ nsDocument::~nsDocument() index = mStyleSheets.Count(); while (--index >= 0) { nsIStyleSheet* sheet = (nsIStyleSheet*) mStyleSheets.ElementAt(index); + nsICSSStyleSheet* css; + nsresult rv = sheet->QueryInterface(kICSSStyleSheetIID, (void **)&css); + if (NS_SUCCEEDED(rv)) { + css->SetDocument(nsnull); + NS_RELEASE(css); + } NS_RELEASE(sheet); } @@ -200,7 +451,7 @@ nsDocument::~nsDocument() NS_IF_RELEASE(mSelection); NS_IF_RELEASE(mScriptContextOwner); NS_IF_RELEASE(mListenerManager); - + NS_IF_RELEASE(mDOMStyleSheets); } nsresult nsDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) @@ -220,6 +471,12 @@ nsresult nsDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) NS_ADDREF_THIS(); return NS_OK; } + if (aIID.Equals(kIDOMNSDocumentIID)) { + nsIDOMNSDocument* tmp = this; + *aInstancePtr = (void*) tmp; + NS_ADDREF_THIS(); + return NS_OK; + } if (aIID.Equals(kIScriptObjectOwnerIID)) { nsIScriptObjectOwner* tmp = this; *aInstancePtr = (void*) tmp; @@ -284,6 +541,12 @@ nsDocument::StartDocumentLoad(nsIURL *aURL, PRInt32 index = mStyleSheets.Count(); while (--index >= 0) { nsIStyleSheet* sheet = (nsIStyleSheet*) mStyleSheets.ElementAt(index); + nsICSSStyleSheet* css; + nsresult rv = sheet->QueryInterface(kICSSStyleSheetIID, (void **)&css); + if (NS_SUCCEEDED(rv)) { + css->SetDocument(nsnull); + NS_RELEASE(css); + } NS_RELEASE(sheet); } mStyleSheets.Clear(); @@ -455,6 +718,12 @@ void nsDocument::AddStyleSheet(nsIStyleSheet* aSheet) NS_PRECONDITION(nsnull != aSheet, "null arg"); mStyleSheets.AppendElement(aSheet); NS_ADDREF(aSheet); + nsICSSStyleSheet* css; + nsresult rv = aSheet->QueryInterface(kICSSStyleSheetIID, (void **)&css); + if (NS_SUCCEEDED(rv)) { + css->SetDocument(this); + NS_RELEASE(css); + } PRInt32 count = mPresShells.Count(); PRInt32 index; @@ -474,6 +743,38 @@ void nsDocument::AddStyleSheet(nsIStyleSheet* aSheet) } } +void nsDocument::SetStyleSheetDisabledState(nsIStyleSheet* aSheet, + PRBool mDisabled) +{ + NS_PRECONDITION(nsnull != aSheet, "null arg"); + PRInt32 count; + PRInt32 index = mStyleSheets.IndexOf((void *)aSheet); + // If we're actually in the document style sheet list + if (-1 != index) { + count = mPresShells.Count(); + PRInt32 index; + for (index = 0; index < count; index++) { + nsIPresShell* shell = (nsIPresShell*)mPresShells.ElementAt(index); + nsIStyleSet* set = shell->GetStyleSet(); + if (nsnull != set) { + if (mDisabled) { + set->RemoveDocStyleSheet(aSheet); + } + else { + AddStyleSheetToSet(aSheet, set); + } + NS_RELEASE(set); + } + } + } + + count = mObservers.Count(); + for (index = 0; index < count; index++) { + nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(index); + observer->StyleSheetDisabledStateChanged(this, aSheet, mDisabled); + } +} + nsIScriptContextOwner *nsDocument::GetScriptContextOwner() { if (nsnull != mScriptContextOwner) { @@ -758,6 +1059,23 @@ nsDocument::GetElementsByTagName(const nsString& aTagname, return NS_OK; } +NS_IMETHODIMP +nsDocument::GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets) +{ + if (nsnull == mDOMStyleSheets) { + mDOMStyleSheets = new nsDOMStyleSheetCollection(this); + if (nsnull == mDOMStyleSheets) { + return NS_ERROR_OUT_OF_MEMORY; + } + NS_ADDREF(mDOMStyleSheets); + } + + *aStyleSheets = mDOMStyleSheets; + NS_ADDREF(mDOMStyleSheets); + + return NS_OK; +} + // // nsIDOMNode methods // diff --git a/content/base/src/nsDocument.h b/content/base/src/nsDocument.h index ea1f51e67e02..d901bc974581 100644 --- a/content/base/src/nsDocument.h +++ b/content/base/src/nsDocument.h @@ -21,6 +21,7 @@ #include "nsIDocument.h" #include "nsVoidArray.h" #include "nsIDOMDocument.h" +#include "nsIDOMNSDocument.h" #include "nsIScriptObjectOwner.h" #include "nsIScriptContextOwner.h" #include "nsIDOMEventCapturer.h" @@ -30,6 +31,7 @@ class nsISelection; class nsIEventListenerManager; +class nsDOMStyleSheetCollection; class nsPostData : public nsIPostData { public: @@ -51,8 +53,12 @@ protected: // Base class for our document implementations -class nsDocument : public nsIDocument, public nsIDOMDocument, public nsIScriptObjectOwner, - public nsIDOMEventCapturer, public nsIJSScriptObject { +class nsDocument : public nsIDocument, + public nsIDOMDocument, + public nsIDOMNSDocument, + public nsIScriptObjectOwner, + public nsIDOMEventCapturer, + public nsIJSScriptObject { public: NS_DECL_ISUPPORTS @@ -123,6 +129,8 @@ public: virtual PRInt32 GetNumberOfStyleSheets(); virtual nsIStyleSheet* GetStyleSheetAt(PRInt32 aIndex); virtual void AddStyleSheet(nsIStyleSheet* aSheet); + virtual void SetStyleSheetDisabledState(nsIStyleSheet* aSheet, + PRBool mDisabled); /** * Set the object from which a document can get a script context. @@ -213,7 +221,8 @@ public: NS_IMETHOD CreateAttribute(const nsString& aName, nsIDOMAttr** aReturn); NS_IMETHOD CreateEntityReference(const nsString& aName, nsIDOMEntityReference** aReturn); NS_IMETHOD GetElementsByTagName(const nsString& aTagname, nsIDOMNodeList** aReturn); - + NS_IMETHOD GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets); + // nsIDOMNode interface NS_IMETHOD GetNodeName(nsString& aNodeName); NS_IMETHOD GetNodeValue(nsString& aNodeValue); @@ -299,6 +308,7 @@ protected: nsIEventListenerManager* mListenerManager; PRBool mDisplaySelection; PRBool mInDestructor; + nsDOMStyleSheetCollection *mDOMStyleSheets; }; #endif /* nsDocument_h___ */ diff --git a/content/base/src/nsStyleSet.cpp b/content/base/src/nsStyleSet.cpp index c537621c5093..2a62721ab2e7 100644 --- a/content/base/src/nsStyleSet.cpp +++ b/content/base/src/nsStyleSet.cpp @@ -91,6 +91,10 @@ public: nsIContent* aContent, nsIFrame* aParentFrame, nsIFrame*& aFrameSubTree); + NS_IMETHOD ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree); NS_IMETHOD ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer); @@ -483,6 +487,8 @@ nsIStyleContext* StyleSetImpl::ResolveStyleFor(nsIPresContext* aPresContext, } if (nsnull != rules) { + // XXX Stop-gap fix to prevent ua.css rules from being applied + // to XML elements nsIHTMLContent *htmlContent; nsresult rv = aContent->QueryInterface(kIHTMLContentIID, (void **)&htmlContent); PRInt32 ruleCount = 0; @@ -655,6 +661,16 @@ NS_IMETHODIMP StyleSetImpl::ConstructFrame(nsIPresContext* aPresContext, aParentFrame, aFrameSubTree); } +NS_IMETHODIMP +StyleSetImpl::ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree) +{ + return mFrameConstructor->ReconstructFrames(aPresContext, aContent, + aParentFrame, aFrameSubTree); +} + NS_IMETHODIMP StyleSetImpl::ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer) @@ -710,6 +726,7 @@ StyleSetImpl::AttributeChanged(nsIPresContext* aPresContext, aAttribute, aHint); } + // xxx style rules enumeration void StyleSetImpl::List(FILE* out, PRInt32 aIndent, nsISupportsArray* aSheets) diff --git a/content/html/document/src/nsHTMLDocument.cpp b/content/html/document/src/nsHTMLDocument.cpp index 11f80712b653..11326cd7b96c 100644 --- a/content/html/document/src/nsHTMLDocument.cpp +++ b/content/html/document/src/nsHTMLDocument.cpp @@ -23,7 +23,6 @@ #include "nsIHTMLStyleSheet.h" #include "nsIHTMLCSSStyleSheet.h" #include "nsIStyleSet.h" -#include "nsIDocumentObserver.h" #include "nsHTMLAtoms.h" #include "nsIPresShell.h" #include "nsIPresContext.h" @@ -49,8 +48,6 @@ #include "nsIFormManager.h" #include "nsRepository.h" #include "nsParserCIID.h" -#include "nsIDOMStyleSheet.h" -#include "nsIDOMStyleSheetCollection.h" #include "nsIDOMHTMLElement.h" @@ -79,241 +76,10 @@ static NS_DEFINE_IID(kIDOMHTMLDocumentIID, NS_IDOMHTMLDOCUMENT_IID); static NS_DEFINE_IID(kIDOMNSHTMLDocumentIID, NS_IDOMNSHTMLDOCUMENT_IID); static NS_DEFINE_IID(kINetServiceIID, NS_INETSERVICE_IID); static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID); -static NS_DEFINE_IID(kIDOMStyleSheetCollectionIID, NS_IDOMSTYLESHEETCOLLECTION_IID); -static NS_DEFINE_IID(kIDOMStyleSheetIID, NS_IDOMSTYLESHEET_IID); static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); -static NS_DEFINE_IID(kIDocumentObserverIID, NS_IDOCUMENT_OBSERVER_IID); static NS_DEFINE_IID(kIHTMLContentContainerIID, NS_IHTMLCONTENTCONTAINER_IID); static NS_DEFINE_IID(kIDOMHTMLElementIID, NS_IDOMHTMLELEMENT_IID); -class nsDOMStyleSheetCollection : public nsIDOMStyleSheetCollection, - public nsIScriptObjectOwner, - public nsIDocumentObserver -{ -public: - nsDOMStyleSheetCollection(nsIDocument *aDocument); - ~nsDOMStyleSheetCollection(); - - NS_DECL_ISUPPORTS - NS_DECL_IDOMSTYLESHEETCOLLECTION - - NS_IMETHOD BeginUpdate(nsIDocument *aDocument) { return NS_OK; } - NS_IMETHOD EndUpdate(nsIDocument *aDocument) { return NS_OK; } - NS_IMETHOD BeginLoad(nsIDocument *aDocument) { return NS_OK; } - NS_IMETHOD EndLoad(nsIDocument *aDocument) { return NS_OK; } - NS_IMETHOD BeginReflow(nsIDocument *aDocument, - nsIPresShell* aShell) { return NS_OK; } - NS_IMETHOD EndReflow(nsIDocument *aDocument, - nsIPresShell* aShell) { return NS_OK; } - NS_IMETHOD ContentChanged(nsIDocument *aDocument, - nsIContent* aContent, - nsISupports* aSubContent) { return NS_OK; } - NS_IMETHOD AttributeChanged(nsIDocument *aDocument, - nsIContent* aContent, - nsIAtom* aAttribute, - PRInt32 aHint) { return NS_OK; } - NS_IMETHOD ContentAppended(nsIDocument *aDocument, - nsIContent* aContainer, - PRInt32 aNewIndexInContainer) - { return NS_OK; } - NS_IMETHOD ContentInserted(nsIDocument *aDocument, - nsIContent* aContainer, - nsIContent* aChild, - PRInt32 aIndexInContainer) { return NS_OK; } - NS_IMETHOD ContentReplaced(nsIDocument *aDocument, - nsIContent* aContainer, - nsIContent* aOldChild, - nsIContent* aNewChild, - PRInt32 aIndexInContainer) { return NS_OK; } - NS_IMETHOD ContentRemoved(nsIDocument *aDocument, - nsIContent* aContainer, - nsIContent* aChild, - PRInt32 aIndexInContainer) { return NS_OK; } - NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, - nsIStyleSheet* aStyleSheet); - NS_IMETHOD DocumentWillBeDestroyed(nsIDocument *aDocument); - - // nsIScriptObjectOwner interface - NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject); - NS_IMETHOD SetScriptObject(void* aScriptObject); - -protected: - PRInt32 mLength; - nsIDocument* mDocument; - void* mScriptObject; -}; - -nsDOMStyleSheetCollection::nsDOMStyleSheetCollection(nsIDocument *aDocument) -{ - NS_INIT_REFCNT(); - mLength = -1; - // Not reference counted to avoid circular references. - // The document will tell us when its going away. - mDocument = aDocument; - mDocument->AddObserver(this); - mScriptObject = nsnull; -} - -nsDOMStyleSheetCollection::~nsDOMStyleSheetCollection() -{ - if (nsnull != mDocument) { - mDocument->RemoveObserver(this); - } - mDocument = nsnull; -} - -NS_IMPL_ADDREF(nsDOMStyleSheetCollection) -NS_IMPL_RELEASE(nsDOMStyleSheetCollection) - -nsresult -nsDOMStyleSheetCollection::QueryInterface(REFNSIID aIID, void** aInstancePtrResult) -{ - if (NULL == aInstancePtrResult) { - return NS_ERROR_NULL_POINTER; - } - - static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); - if (aIID.Equals(kIDOMStyleSheetCollectionIID)) { - nsIDOMStyleSheetCollection *tmp = this; - *aInstancePtrResult = (void*) tmp; - AddRef(); - return NS_OK; - } - if (aIID.Equals(kIScriptObjectOwnerIID)) { - nsIScriptObjectOwner *tmp = this; - *aInstancePtrResult = (void*) tmp; - AddRef(); - return NS_OK; - } - if (aIID.Equals(kIDocumentObserverIID)) { - nsIDocumentObserver *tmp = this; - *aInstancePtrResult = (void*) tmp; - AddRef(); - return NS_OK; - } - if (aIID.Equals(kISupportsIID)) { - nsIDOMStyleSheetCollection *tmp = this; - nsISupports *tmp2 = tmp; - *aInstancePtrResult = (void*) tmp2; - AddRef(); - return NS_OK; - } - return NS_NOINTERFACE; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::GetLength(PRUint32* aLength) -{ - if (nsnull != mDocument) { - // XXX Find the number and then cache it. We'll use the - // observer notification to figure out if new ones have - // been added or removed. - if (-1 == mLength) { - PRUint32 count = 0; - PRInt32 i, imax = mDocument->GetNumberOfStyleSheets(); - - for (i = 0; i < imax; i++) { - nsIStyleSheet *sheet = mDocument->GetStyleSheetAt(i); - nsIDOMStyleSheet *domss; - - if (NS_OK == sheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { - count++; - NS_RELEASE(domss); - } - - NS_RELEASE(sheet); - } - mLength = count; - } - *aLength = mLength; - } - else { - *aLength = 0; - } - - return NS_OK; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::Item(PRUint32 aIndex, nsIDOMStyleSheet** aReturn) -{ - *aReturn = nsnull; - if (nsnull != mDocument) { - PRUint32 count = 0; - PRInt32 i, imax = mDocument->GetNumberOfStyleSheets(); - - // XXX Not particularly efficient, but does anyone care? - for (i = 0; (i < imax) && (nsnull == *aReturn); i++) { - nsIStyleSheet *sheet = mDocument->GetStyleSheetAt(i); - nsIDOMStyleSheet *domss; - - if (NS_OK == sheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { - if (count++ == aIndex) { - *aReturn = domss; - NS_ADDREF(domss); - } - NS_RELEASE(domss); - } - - NS_RELEASE(sheet); - } - } - - return NS_OK; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) -{ - nsresult res = NS_OK; - - if (nsnull == mScriptObject) { - nsISupports *supports = (nsISupports *)(nsIDOMStyleSheetCollection *)this; - nsISupports *parent = (nsISupports *)mDocument; - - // XXX Should be done through factory - res = NS_NewScriptStyleSheetCollection(aContext, - supports, - parent, - (void**)&mScriptObject); - } - *aScriptObject = mScriptObject; - - return res; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::SetScriptObject(void* aScriptObject) -{ - mScriptObject = aScriptObject; - return NS_OK; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::StyleSheetAdded(nsIDocument *aDocument, - nsIStyleSheet* aStyleSheet) -{ - if (-1 != mLength) { - nsIDOMStyleSheet *domss; - if (NS_OK == aStyleSheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { - mLength++; - NS_RELEASE(domss); - } - } - - return NS_OK; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::DocumentWillBeDestroyed(nsIDocument *aDocument) -{ - if (nsnull != mDocument) { - aDocument->RemoveObserver(this); - mDocument = nsnull; - } - - return NS_OK; -} NS_LAYOUT nsresult NS_NewHTMLDocument(nsIDocument** aInstancePtrResult) @@ -334,7 +100,6 @@ nsHTMLDocument::nsHTMLDocument() mAnchors = nsnull; mNamedItems = nsnull; mParser = nsnull; - mDOMStyleSheets = nsnull; nsHTMLAtoms::AddrefAtoms(); mDTDMode = eDTDMode_NoQuirks; @@ -366,7 +131,6 @@ nsHTMLDocument::~nsHTMLDocument() NS_IF_RELEASE(mEmbeds); NS_IF_RELEASE(mLinks); NS_IF_RELEASE(mAnchors); - NS_IF_RELEASE(mDOMStyleSheets); NS_IF_RELEASE(mAttrStyleSheet); NS_IF_RELEASE(mStyleAttrStyleSheet); NS_IF_RELEASE(mParser); @@ -1098,22 +862,6 @@ nsHTMLDocument::GetLastModified(nsString& aLastModified) return NS_ERROR_NOT_IMPLEMENTED; } -NS_IMETHODIMP -nsHTMLDocument::GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets) -{ - if (nsnull == mDOMStyleSheets) { - mDOMStyleSheets = new nsDOMStyleSheetCollection(this); - if (nsnull == mDOMStyleSheets) { - return NS_ERROR_OUT_OF_MEMORY; - } - NS_ADDREF(mDOMStyleSheets); - } - - *aStyleSheets = mDOMStyleSheets; - NS_ADDREF(mDOMStyleSheets); - - return NS_OK; -} NS_IMETHODIMP nsHTMLDocument::GetEmbeds(nsIDOMHTMLCollection** aEmbeds) diff --git a/content/html/document/src/nsHTMLDocument.h b/content/html/document/src/nsHTMLDocument.h index 639f99755270..c7867aaa4fc1 100644 --- a/content/html/document/src/nsHTMLDocument.h +++ b/content/html/document/src/nsHTMLDocument.h @@ -33,7 +33,6 @@ class nsContentList; class nsIContentViewerContainer; class nsIParser; class BlockText; -class nsDOMStyleSheetCollection; class nsHTMLDocument : public nsMarkupDocument, public nsIHTMLDocument, public nsIDOMHTMLDocument, public nsIDOMNSHTMLDocument, public nsIHTMLContentContainer { public: @@ -159,7 +158,6 @@ protected: nsContentList *mLinks; nsContentList *mAnchors; nsContentList *mForms; - nsDOMStyleSheetCollection *mDOMStyleSheets; PLHashTable *mNamedItems; diff --git a/content/html/style/src/nsCSSStyleSheet.cpp b/content/html/style/src/nsCSSStyleSheet.cpp index b593d52e97ac..b87a220207dc 100644 --- a/content/html/style/src/nsCSSStyleSheet.cpp +++ b/content/html/style/src/nsCSSStyleSheet.cpp @@ -567,6 +567,8 @@ public: virtual PRInt32 StyleSheetCount(); virtual nsresult GetStyleSheetAt(PRInt32 aIndex, nsICSSStyleSheet*& aSheet); + virtual void SetDocument(nsIDocument *aDocument); + virtual void List(FILE* out = stdout, PRInt32 aIndent = 0) const; // nsIDOMStyleSheet interface @@ -614,6 +616,8 @@ protected: RuleHash* mRuleHash; CSSStyleRuleCollectionImpl* mRuleCollection; CSSImportsCollectionImpl* mImportsCollection; + nsIDocument* mDocument; + PRBool mDisabled; void * mScriptObject; }; @@ -668,6 +672,8 @@ CSSStyleSheetImpl::CSSStyleSheetImpl(nsIURL* aURL) mParent = nsnull; mRuleCollection = nsnull; mImportsCollection = nsnull; + mDocument = nsnull; + mDisabled = PR_FALSE; mScriptObject = nsnull; #ifdef DEBUG_REFS ++gInstanceCount; @@ -711,6 +717,9 @@ CSSStyleSheetImpl::~CSSStyleSheetImpl() mOrderedRules->EnumerateForwards(DropStyleSheetReference, nsnull); } ClearHash(); + // XXX The document reference is not reference counted and should + // not be released. The document will let us know when it is going + // away. } NS_IMPL_ADDREF(CSSStyleSheetImpl) @@ -1234,6 +1243,13 @@ nsresult CSSStyleSheetImpl::GetStyleSheetAt(PRInt32 aIndex, nsICSSStyleSheet*& a return NS_OK; } +void CSSStyleSheetImpl::SetDocument(nsIDocument *aDocument) +{ + // This reference is not reference counted and should not be + // released. The document will tell us when it goes away. + mDocument = aDocument; +} + void CSSStyleSheetImpl::List(FILE* out, PRInt32 aIndent) const { nsAutoString buffer; @@ -1290,15 +1306,20 @@ void CSSStyleSheetImpl::BuildHash(void) NS_IMETHODIMP CSSStyleSheetImpl::GetDisabled(PRBool* aDisabled) { - // XXX TBI - *aDisabled = PR_FALSE; + *aDisabled = mDisabled; return NS_OK; } NS_IMETHODIMP CSSStyleSheetImpl::SetDisabled(PRBool aDisabled) { - // XXX TBI + + if ((nsnull != mDocument) && (mDisabled != aDisabled)) { + mDocument->SetStyleSheetDisabledState(this, aDisabled); + } + + mDisabled = aDisabled; + return NS_OK; } diff --git a/content/html/style/src/nsHTMLStyleSheet.cpp b/content/html/style/src/nsHTMLStyleSheet.cpp index 54fe2f01a6e7..290239f8bb30 100644 --- a/content/html/style/src/nsHTMLStyleSheet.cpp +++ b/content/html/style/src/nsHTMLStyleSheet.cpp @@ -246,6 +246,11 @@ public: nsIFrame* aParentFrame, nsIFrame*& aFrameSubTree); + NS_IMETHODIMP ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree); + NS_IMETHOD ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer); @@ -300,6 +305,12 @@ protected: nsIStyleContext* aStyleContext, nsIFrame*& aNewFrame); + nsresult ConstructXMLRootDescendants(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIStyleContext* aRootPseudoStyle, + nsIFrame* aParentFrame, + nsIFrame*& aNewFrame); + nsresult ConstructXMLRootFrame(nsIPresContext* aPresContext, nsIContent* aContent, nsIStyleContext* aStyleContext, @@ -1213,6 +1224,53 @@ HTMLStyleSheetImpl::ConstructRootFrame(nsIPresContext* aPresContext, return rv; } +nsresult +HTMLStyleSheetImpl::ConstructXMLRootDescendants(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIStyleContext* aRootPseudoStyle, + nsIFrame* aParentFrame, + nsIFrame*& aNewFrame) +{ + nsresult rv = NS_OK; + // Create a scroll frame. + // XXX Use the rootPseudoStyle overflow style information to decide whether + // we create a scroll frame or just a body wrapper frame... + nsIFrame* scrollFrame = nsnull; + + rv = NS_NewScrollFrame(nsnull, aParentFrame, scrollFrame); + + if (NS_SUCCEEDED(rv)) { + // The scroll frame gets the root pseudo style context, and the scrolled + // frame gets a SCROLLED-CONTENT pseudo element style context. + // XXX We should probably use a different pseudo style context... + scrollFrame->SetStyleContext(aPresContext, aRootPseudoStyle); + + nsIStyleContext* scrolledPseudoStyle; + nsIFrame* wrapperFrame; + + scrolledPseudoStyle = aPresContext->ResolvePseudoStyleContextFor + (nsnull, nsHTMLAtoms::scrolledContentPseudo, + aRootPseudoStyle); + + // Create a body frame to wrap the document element + NS_NewBodyFrame(nsnull, scrollFrame, + wrapperFrame, NS_BODY_THE_BODY|NS_BODY_SHRINK_WRAP); + wrapperFrame->SetStyleContext(aPresContext, scrolledPseudoStyle); + + // Construct a frame for the document element and process its children + nsIFrame* docElementFrame; + ConstructFrame(aPresContext, aContent, wrapperFrame, docElementFrame); + wrapperFrame->SetInitialChildList(*aPresContext, nsnull, docElementFrame); + + // Set the scroll frame's initial child list + scrollFrame->SetInitialChildList(*aPresContext, nsnull, wrapperFrame); + } + + aNewFrame = scrollFrame; + + return rv; +} + nsresult HTMLStyleSheetImpl::ConstructXMLRootFrame(nsIPresContext* aPresContext, nsIContent* aContent, @@ -1253,36 +1311,16 @@ HTMLStyleSheetImpl::ConstructXMLRootFrame(nsIPresContext* aPresContext, // we create a scroll frame or just a body wrapper frame... nsIFrame* scrollFrame; - if (NS_SUCCEEDED(NS_NewScrollFrame(nsnull, aNewFrame, scrollFrame))) { - // The scroll frame gets the root pseudo style context, and the scrolled - // frame gets a SCROLLED-CONTENT pseudo element style context. - // XXX We should probably use a different pseudo style context... - scrollFrame->SetStyleContext(aPresContext, rootPseudoStyle); - - nsIStyleContext* scrolledPseudoStyle; - nsIFrame* wrapperFrame; - - scrolledPseudoStyle = aPresContext->ResolvePseudoStyleContextFor - (nsnull, nsHTMLAtoms::scrolledContentPseudo, - rootPseudoStyle); - - // Create a body frame to wrap the document element - NS_NewBodyFrame(nsnull, scrollFrame, wrapperFrame, NS_BODY_THE_BODY|NS_BODY_SHRINK_WRAP); - wrapperFrame->SetStyleContext(aPresContext, scrolledPseudoStyle); - - // Construct a frame for the document element and process its children - nsIFrame* docElementFrame; - ConstructFrame(aPresContext, aContent, wrapperFrame, docElementFrame); - wrapperFrame->SetInitialChildList(*aPresContext, nsnull, docElementFrame); - - // Set the scroll frame's initial child list - scrollFrame->SetInitialChildList(*aPresContext, nsnull, wrapperFrame); - } + rv = ConstructXMLRootDescendants(aPresContext, aContent, rootPseudoStyle, + aNewFrame, scrollFrame); // initialize the root frame - aNewFrame->SetInitialChildList(*aPresContext, nsnull, scrollFrame); + if (NS_SUCCEEDED(rv)) { + aNewFrame->SetInitialChildList(*aPresContext, nsnull, scrollFrame); + } } + NS_IF_RELEASE(rootPseudoStyle); return rv; } @@ -1750,6 +1788,69 @@ HTMLStyleSheetImpl::ConstructFrame(nsIPresContext* aPresContext, return rv; } +NS_IMETHODIMP +HTMLStyleSheetImpl::ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree) +{ + nsresult rv = NS_OK; + + nsIDocument* document; + rv = aContent->GetDocument(document); + if (NS_FAILED(rv)) { + return rv; + } + + // XXX Currently we only know how to do this for XML documents + if (nsnull != document) { + nsIPresShell* shell = aPresContext->GetShell(); + nsIXMLDocument* xmlDocument; + rv = document->QueryInterface(kIXMLDocumentIID, (void **)&xmlDocument); + + if (NS_SUCCEEDED(rv)) { + nsIReflowCommand* reflowCmd; + + rv = NS_NewHTMLReflowCommand(&reflowCmd, aParentFrame, + nsIReflowCommand::FrameRemoved, + aFrameSubTree); + + if (NS_SUCCEEDED(rv)) { + shell->AppendReflowCommand(reflowCmd); + NS_RELEASE(reflowCmd); + + nsIFrame *newChild; + // XXX See ConstructXMLContents for an explanation of why + // we create this pseudostyle + nsIStyleContext* rootPseudoStyle; + rootPseudoStyle = aPresContext->ResolvePseudoStyleContextFor(nsnull, + nsHTMLAtoms::xmlRootPseudo, nsnull); + + rv = ConstructXMLRootDescendants(aPresContext, aContent, + rootPseudoStyle, aParentFrame, + newChild); + if (NS_SUCCEEDED(rv)) { + rv = NS_NewHTMLReflowCommand(&reflowCmd, aParentFrame, + nsIReflowCommand::FrameInserted, + newChild); + + if (NS_SUCCEEDED(rv)) { + shell->AppendReflowCommand(reflowCmd); + NS_RELEASE(reflowCmd); + } + } + NS_IF_RELEASE(rootPseudoStyle); + } + NS_RELEASE(xmlDocument); + } + NS_RELEASE(document); + NS_RELEASE(shell); + } + + return rv; + +} + nsIFrame* HTMLStyleSheetImpl::GetFrameFor(nsIPresShell* aPresShell, nsIPresContext* aPresContext, nsIContent* aContent) diff --git a/layout/base/nsPresShell.cpp b/layout/base/nsPresShell.cpp index c17282780ae4..56153e429467 100644 --- a/layout/base/nsPresShell.cpp +++ b/layout/base/nsPresShell.cpp @@ -34,6 +34,7 @@ #include "nsContainerFrame.h" #include "nsHTMLIIDs.h" #include "nsIDeviceContext.h" +#include "nsHTMLParts.h" static PRBool gsNoisyRefs = PR_FALSE; #undef NOISY @@ -187,6 +188,9 @@ public: PRInt32 aIndexInContainer); NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet); + NS_IMETHOD StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled); NS_IMETHOD DocumentWillBeDestroyed(nsIDocument *aDocument); // nsIPresShell @@ -874,6 +878,31 @@ PresShell::StyleSheetAdded(nsIDocument *aDocument, return NS_OK; } +NS_IMETHODIMP +PresShell::StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled) +{ + nsresult rv = NS_OK; + if (nsnull != mRootFrame) { + nsIFrame* childFrame; + rv = mRootFrame->FirstChild(nsnull, childFrame); + + if (nsnull != mDocument) { + nsIContent* root = mDocument->GetRootContent(); + if (nsnull != root) { + + EnterReflowLock(); + rv = mStyleSet->ReconstructFrames(mPresContext, root, + mRootFrame, childFrame); + ExitReflowLock(); + NS_RELEASE(root); + } + } + } + return rv; +} + NS_IMETHODIMP PresShell::DocumentWillBeDestroyed(nsIDocument *aDocument) { diff --git a/layout/base/public/nsIDocument.h b/layout/base/public/nsIDocument.h index bc9dd916ae34..2e86ba6ad645 100644 --- a/layout/base/public/nsIDocument.h +++ b/layout/base/public/nsIDocument.h @@ -138,6 +138,8 @@ public: virtual PRInt32 GetNumberOfStyleSheets() = 0; virtual nsIStyleSheet* GetStyleSheetAt(PRInt32 aIndex) = 0; virtual void AddStyleSheet(nsIStyleSheet* aSheet) = 0; + virtual void SetStyleSheetDisabledState(nsIStyleSheet* aSheet, + PRBool mDisabled) = 0; /** * Set the object from which a document can get a script context. diff --git a/layout/base/public/nsIDocumentObserver.h b/layout/base/public/nsIDocumentObserver.h index d7d8f885fc23..72311cf023bd 100644 --- a/layout/base/public/nsIDocumentObserver.h +++ b/layout/base/public/nsIDocumentObserver.h @@ -187,6 +187,22 @@ public: NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet) = 0; + /** + * A StyleSheet has just disabled or enabled. + * This method is called automatically when the disabled state + * of a StyleSheet gets changed. The style sheet passes this + * notification to the document. The notification is passed on + * to all of the document observers. + * + * @param aDocument The document being observed + * @param aStyleSheet the StyleSheet that has been added + * @param aDisabled PR_TRUE if the sheet is disabled, PR_FALSE if + * it is enabled + */ + NS_IMETHOD StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled) = 0; + /** * The document is in the process of being destroyed. * This method is called automatically during document diff --git a/layout/base/public/nsIStyleFrameConstruction.h b/layout/base/public/nsIStyleFrameConstruction.h index 33f87761bc7e..49ecf4e2304a 100644 --- a/layout/base/public/nsIStyleFrameConstruction.h +++ b/layout/base/public/nsIStyleFrameConstruction.h @@ -38,6 +38,14 @@ public: nsIFrame* aParentFrame, nsIFrame*& aFrameSubTree) = 0; + // Causes reconstruction of a frame hierarchy rooted by the + // frame aFrameSubTree. This is often called when radical style + // change precludes incremental reflow. + NS_IMETHOD ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree) = 0; + NS_IMETHOD ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer) = 0; diff --git a/layout/base/public/nsIStyleSet.h b/layout/base/public/nsIStyleSet.h index 3261ecff62de..08769a514ae9 100644 --- a/layout/base/public/nsIStyleSet.h +++ b/layout/base/public/nsIStyleSet.h @@ -98,6 +98,14 @@ public: nsIFrame* aParentFrame, nsIFrame*& aFrameSubTree) = 0; + // Causes reconstruction of a frame hierarchy rooted by the + // frame aFrameSubTree. This is often called when radical style + // change precludes incremental reflow. + NS_IMETHOD ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree) = 0; + // Notifications of changes to the content mpodel NS_IMETHOD ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, diff --git a/layout/base/src/nsContentList.h b/layout/base/src/nsContentList.h index 1c178ac0ca81..cd4a280d889c 100644 --- a/layout/base/src/nsContentList.h +++ b/layout/base/src/nsContentList.h @@ -89,7 +89,10 @@ public: nsIContent* aChild, PRInt32 aIndexInContainer); NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, - nsIStyleSheet* aStyleSheet) { return NS_OK; } + nsIStyleSheet* aStyleSheet) { return NS_OK; } + NS_IMETHOD StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled) { return NS_OK; } NS_IMETHOD DocumentWillBeDestroyed(nsIDocument *aDocument); protected: diff --git a/layout/base/src/nsDocument.cpp b/layout/base/src/nsDocument.cpp index 77bb55d573b6..29b488086b9e 100644 --- a/layout/base/src/nsDocument.cpp +++ b/layout/base/src/nsDocument.cpp @@ -23,6 +23,7 @@ #include "nsIURLGroup.h" #include "nsString.h" #include "nsIContent.h" +#include "nsIDocumentObserver.h" #include "nsIStyleSet.h" #include "nsIStyleSheet.h" #include "nsIPresShell.h" @@ -37,6 +38,8 @@ #include "nsIEventStateManager.h" #include "nsContentList.h" #include "nsIDOMEventListener.h" +#include "nsIDOMStyleSheet.h" +#include "nsIDOMStyleSheetCollection.h" #include "nsCSSPropIDs.h" #include "nsCSSProps.h" @@ -56,6 +59,7 @@ static NS_DEFINE_IID(kIDocumentIID, NS_IDOCUMENT_IID); #include "nsIDOMElement.h" static NS_DEFINE_IID(kIDOMDocumentIID, NS_IDOMDOCUMENT_IID); +static NS_DEFINE_IID(kIDOMNSDocumentIID, NS_IDOMNSDOCUMENT_IID); static NS_DEFINE_IID(kIContentIID, NS_ICONTENT_IID); static NS_DEFINE_IID(kIDOMElementIID, NS_IDOMELEMENT_IID); static NS_DEFINE_IID(kIJSScriptObjectIID, NS_IJSSCRIPTOBJECT_IID); @@ -66,6 +70,246 @@ static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID); static NS_DEFINE_IID(kIPrivateDOMEventIID, NS_IPRIVATEDOMEVENT_IID); static NS_DEFINE_IID(kIEventListenerManagerIID, NS_IEVENTLISTENERMANAGER_IID); static NS_DEFINE_IID(kIPostDataIID, NS_IPOSTDATA_IID); +static NS_DEFINE_IID(kIDOMStyleSheetCollectionIID, NS_IDOMSTYLESHEETCOLLECTION_IID); +static NS_DEFINE_IID(kIDOMStyleSheetIID, NS_IDOMSTYLESHEET_IID); +static NS_DEFINE_IID(kIDocumentObserverIID, NS_IDOCUMENT_OBSERVER_IID); +static NS_DEFINE_IID(kICSSStyleSheetIID, NS_ICSS_STYLE_SHEET_IID); + +class nsDOMStyleSheetCollection : public nsIDOMStyleSheetCollection, + public nsIScriptObjectOwner, + public nsIDocumentObserver +{ +public: + nsDOMStyleSheetCollection(nsIDocument *aDocument); + ~nsDOMStyleSheetCollection(); + + NS_DECL_ISUPPORTS + NS_DECL_IDOMSTYLESHEETCOLLECTION + + NS_IMETHOD BeginUpdate(nsIDocument *aDocument) { return NS_OK; } + NS_IMETHOD EndUpdate(nsIDocument *aDocument) { return NS_OK; } + NS_IMETHOD BeginLoad(nsIDocument *aDocument) { return NS_OK; } + NS_IMETHOD EndLoad(nsIDocument *aDocument) { return NS_OK; } + NS_IMETHOD BeginReflow(nsIDocument *aDocument, + nsIPresShell* aShell) { return NS_OK; } + NS_IMETHOD EndReflow(nsIDocument *aDocument, + nsIPresShell* aShell) { return NS_OK; } + NS_IMETHOD ContentChanged(nsIDocument *aDocument, + nsIContent* aContent, + nsISupports* aSubContent) { return NS_OK; } + NS_IMETHOD AttributeChanged(nsIDocument *aDocument, + nsIContent* aContent, + nsIAtom* aAttribute, + PRInt32 aHint) { return NS_OK; } + NS_IMETHOD ContentAppended(nsIDocument *aDocument, + nsIContent* aContainer, + PRInt32 aNewIndexInContainer) + { return NS_OK; } + NS_IMETHOD ContentInserted(nsIDocument *aDocument, + nsIContent* aContainer, + nsIContent* aChild, + PRInt32 aIndexInContainer) { return NS_OK; } + NS_IMETHOD ContentReplaced(nsIDocument *aDocument, + nsIContent* aContainer, + nsIContent* aOldChild, + nsIContent* aNewChild, + PRInt32 aIndexInContainer) { return NS_OK; } + NS_IMETHOD ContentRemoved(nsIDocument *aDocument, + nsIContent* aContainer, + nsIContent* aChild, + PRInt32 aIndexInContainer) { return NS_OK; } + NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet); + NS_IMETHOD StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled) { return NS_OK; } + NS_IMETHOD DocumentWillBeDestroyed(nsIDocument *aDocument); + + // nsIScriptObjectOwner interface + NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject); + NS_IMETHOD SetScriptObject(void* aScriptObject); + +protected: + PRInt32 mLength; + nsIDocument* mDocument; + void* mScriptObject; +}; + +nsDOMStyleSheetCollection::nsDOMStyleSheetCollection(nsIDocument *aDocument) +{ + NS_INIT_REFCNT(); + mLength = -1; + // Not reference counted to avoid circular references. + // The document will tell us when its going away. + mDocument = aDocument; + mDocument->AddObserver(this); + mScriptObject = nsnull; +} + +nsDOMStyleSheetCollection::~nsDOMStyleSheetCollection() +{ + if (nsnull != mDocument) { + mDocument->RemoveObserver(this); + } + mDocument = nsnull; +} + +NS_IMPL_ADDREF(nsDOMStyleSheetCollection) +NS_IMPL_RELEASE(nsDOMStyleSheetCollection) + +nsresult +nsDOMStyleSheetCollection::QueryInterface(REFNSIID aIID, void** aInstancePtrResult) +{ + if (NULL == aInstancePtrResult) { + return NS_ERROR_NULL_POINTER; + } + + static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); + if (aIID.Equals(kIDOMStyleSheetCollectionIID)) { + nsIDOMStyleSheetCollection *tmp = this; + *aInstancePtrResult = (void*) tmp; + AddRef(); + return NS_OK; + } + if (aIID.Equals(kIScriptObjectOwnerIID)) { + nsIScriptObjectOwner *tmp = this; + *aInstancePtrResult = (void*) tmp; + AddRef(); + return NS_OK; + } + if (aIID.Equals(kIDocumentObserverIID)) { + nsIDocumentObserver *tmp = this; + *aInstancePtrResult = (void*) tmp; + AddRef(); + return NS_OK; + } + if (aIID.Equals(kISupportsIID)) { + nsIDOMStyleSheetCollection *tmp = this; + nsISupports *tmp2 = tmp; + *aInstancePtrResult = (void*) tmp2; + AddRef(); + return NS_OK; + } + return NS_NOINTERFACE; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::GetLength(PRUint32* aLength) +{ + if (nsnull != mDocument) { + // XXX Find the number and then cache it. We'll use the + // observer notification to figure out if new ones have + // been added or removed. + if (-1 == mLength) { + PRUint32 count = 0; + PRInt32 i, imax = mDocument->GetNumberOfStyleSheets(); + + for (i = 0; i < imax; i++) { + nsIStyleSheet *sheet = mDocument->GetStyleSheetAt(i); + nsIDOMStyleSheet *domss; + + if (NS_OK == sheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { + count++; + NS_RELEASE(domss); + } + + NS_RELEASE(sheet); + } + mLength = count; + } + *aLength = mLength; + } + else { + *aLength = 0; + } + + return NS_OK; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::Item(PRUint32 aIndex, nsIDOMStyleSheet** aReturn) +{ + *aReturn = nsnull; + if (nsnull != mDocument) { + PRUint32 count = 0; + PRInt32 i, imax = mDocument->GetNumberOfStyleSheets(); + + // XXX Not particularly efficient, but does anyone care? + for (i = 0; (i < imax) && (nsnull == *aReturn); i++) { + nsIStyleSheet *sheet = mDocument->GetStyleSheetAt(i); + nsIDOMStyleSheet *domss; + + if (NS_OK == sheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { + if (count++ == aIndex) { + *aReturn = domss; + NS_ADDREF(domss); + } + NS_RELEASE(domss); + } + + NS_RELEASE(sheet); + } + } + + return NS_OK; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) +{ + nsresult res = NS_OK; + + if (nsnull == mScriptObject) { + nsISupports *supports = (nsISupports *)(nsIDOMStyleSheetCollection *)this; + nsISupports *parent = (nsISupports *)mDocument; + + // XXX Should be done through factory + res = NS_NewScriptStyleSheetCollection(aContext, + supports, + parent, + (void**)&mScriptObject); + } + *aScriptObject = mScriptObject; + + return res; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::SetScriptObject(void* aScriptObject) +{ + mScriptObject = aScriptObject; + return NS_OK; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::StyleSheetAdded(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet) +{ + if (-1 != mLength) { + nsIDOMStyleSheet *domss; + if (NS_OK == aStyleSheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { + mLength++; + NS_RELEASE(domss); + } + } + + return NS_OK; +} + +NS_IMETHODIMP +nsDOMStyleSheetCollection::DocumentWillBeDestroyed(nsIDocument *aDocument) +{ + if (nsnull != mDocument) { + aDocument->RemoveObserver(this); + mDocument = nsnull; + } + + return NS_OK; +} + +// ================================================================== +// = +// ================================================================== NS_LAYOUT nsresult NS_NewPostData(PRBool aIsFile, char* aData, @@ -153,6 +397,7 @@ nsDocument::nsDocument() if (NS_OK != NS_NewSelection(&mSelection)) { printf("*************** Error: nsDocument::nsDocument - Creation of Selection failed!\n"); } + mDOMStyleSheets = nsnull; Init();/* XXX */ } @@ -193,6 +438,12 @@ nsDocument::~nsDocument() index = mStyleSheets.Count(); while (--index >= 0) { nsIStyleSheet* sheet = (nsIStyleSheet*) mStyleSheets.ElementAt(index); + nsICSSStyleSheet* css; + nsresult rv = sheet->QueryInterface(kICSSStyleSheetIID, (void **)&css); + if (NS_SUCCEEDED(rv)) { + css->SetDocument(nsnull); + NS_RELEASE(css); + } NS_RELEASE(sheet); } @@ -200,7 +451,7 @@ nsDocument::~nsDocument() NS_IF_RELEASE(mSelection); NS_IF_RELEASE(mScriptContextOwner); NS_IF_RELEASE(mListenerManager); - + NS_IF_RELEASE(mDOMStyleSheets); } nsresult nsDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) @@ -220,6 +471,12 @@ nsresult nsDocument::QueryInterface(REFNSIID aIID, void** aInstancePtr) NS_ADDREF_THIS(); return NS_OK; } + if (aIID.Equals(kIDOMNSDocumentIID)) { + nsIDOMNSDocument* tmp = this; + *aInstancePtr = (void*) tmp; + NS_ADDREF_THIS(); + return NS_OK; + } if (aIID.Equals(kIScriptObjectOwnerIID)) { nsIScriptObjectOwner* tmp = this; *aInstancePtr = (void*) tmp; @@ -284,6 +541,12 @@ nsDocument::StartDocumentLoad(nsIURL *aURL, PRInt32 index = mStyleSheets.Count(); while (--index >= 0) { nsIStyleSheet* sheet = (nsIStyleSheet*) mStyleSheets.ElementAt(index); + nsICSSStyleSheet* css; + nsresult rv = sheet->QueryInterface(kICSSStyleSheetIID, (void **)&css); + if (NS_SUCCEEDED(rv)) { + css->SetDocument(nsnull); + NS_RELEASE(css); + } NS_RELEASE(sheet); } mStyleSheets.Clear(); @@ -455,6 +718,12 @@ void nsDocument::AddStyleSheet(nsIStyleSheet* aSheet) NS_PRECONDITION(nsnull != aSheet, "null arg"); mStyleSheets.AppendElement(aSheet); NS_ADDREF(aSheet); + nsICSSStyleSheet* css; + nsresult rv = aSheet->QueryInterface(kICSSStyleSheetIID, (void **)&css); + if (NS_SUCCEEDED(rv)) { + css->SetDocument(this); + NS_RELEASE(css); + } PRInt32 count = mPresShells.Count(); PRInt32 index; @@ -474,6 +743,38 @@ void nsDocument::AddStyleSheet(nsIStyleSheet* aSheet) } } +void nsDocument::SetStyleSheetDisabledState(nsIStyleSheet* aSheet, + PRBool mDisabled) +{ + NS_PRECONDITION(nsnull != aSheet, "null arg"); + PRInt32 count; + PRInt32 index = mStyleSheets.IndexOf((void *)aSheet); + // If we're actually in the document style sheet list + if (-1 != index) { + count = mPresShells.Count(); + PRInt32 index; + for (index = 0; index < count; index++) { + nsIPresShell* shell = (nsIPresShell*)mPresShells.ElementAt(index); + nsIStyleSet* set = shell->GetStyleSet(); + if (nsnull != set) { + if (mDisabled) { + set->RemoveDocStyleSheet(aSheet); + } + else { + AddStyleSheetToSet(aSheet, set); + } + NS_RELEASE(set); + } + } + } + + count = mObservers.Count(); + for (index = 0; index < count; index++) { + nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(index); + observer->StyleSheetDisabledStateChanged(this, aSheet, mDisabled); + } +} + nsIScriptContextOwner *nsDocument::GetScriptContextOwner() { if (nsnull != mScriptContextOwner) { @@ -758,6 +1059,23 @@ nsDocument::GetElementsByTagName(const nsString& aTagname, return NS_OK; } +NS_IMETHODIMP +nsDocument::GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets) +{ + if (nsnull == mDOMStyleSheets) { + mDOMStyleSheets = new nsDOMStyleSheetCollection(this); + if (nsnull == mDOMStyleSheets) { + return NS_ERROR_OUT_OF_MEMORY; + } + NS_ADDREF(mDOMStyleSheets); + } + + *aStyleSheets = mDOMStyleSheets; + NS_ADDREF(mDOMStyleSheets); + + return NS_OK; +} + // // nsIDOMNode methods // diff --git a/layout/base/src/nsDocument.h b/layout/base/src/nsDocument.h index ea1f51e67e02..d901bc974581 100644 --- a/layout/base/src/nsDocument.h +++ b/layout/base/src/nsDocument.h @@ -21,6 +21,7 @@ #include "nsIDocument.h" #include "nsVoidArray.h" #include "nsIDOMDocument.h" +#include "nsIDOMNSDocument.h" #include "nsIScriptObjectOwner.h" #include "nsIScriptContextOwner.h" #include "nsIDOMEventCapturer.h" @@ -30,6 +31,7 @@ class nsISelection; class nsIEventListenerManager; +class nsDOMStyleSheetCollection; class nsPostData : public nsIPostData { public: @@ -51,8 +53,12 @@ protected: // Base class for our document implementations -class nsDocument : public nsIDocument, public nsIDOMDocument, public nsIScriptObjectOwner, - public nsIDOMEventCapturer, public nsIJSScriptObject { +class nsDocument : public nsIDocument, + public nsIDOMDocument, + public nsIDOMNSDocument, + public nsIScriptObjectOwner, + public nsIDOMEventCapturer, + public nsIJSScriptObject { public: NS_DECL_ISUPPORTS @@ -123,6 +129,8 @@ public: virtual PRInt32 GetNumberOfStyleSheets(); virtual nsIStyleSheet* GetStyleSheetAt(PRInt32 aIndex); virtual void AddStyleSheet(nsIStyleSheet* aSheet); + virtual void SetStyleSheetDisabledState(nsIStyleSheet* aSheet, + PRBool mDisabled); /** * Set the object from which a document can get a script context. @@ -213,7 +221,8 @@ public: NS_IMETHOD CreateAttribute(const nsString& aName, nsIDOMAttr** aReturn); NS_IMETHOD CreateEntityReference(const nsString& aName, nsIDOMEntityReference** aReturn); NS_IMETHOD GetElementsByTagName(const nsString& aTagname, nsIDOMNodeList** aReturn); - + NS_IMETHOD GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets); + // nsIDOMNode interface NS_IMETHOD GetNodeName(nsString& aNodeName); NS_IMETHOD GetNodeValue(nsString& aNodeValue); @@ -299,6 +308,7 @@ protected: nsIEventListenerManager* mListenerManager; PRBool mDisplaySelection; PRBool mInDestructor; + nsDOMStyleSheetCollection *mDOMStyleSheets; }; #endif /* nsDocument_h___ */ diff --git a/layout/base/src/nsStyleSet.cpp b/layout/base/src/nsStyleSet.cpp index c537621c5093..2a62721ab2e7 100644 --- a/layout/base/src/nsStyleSet.cpp +++ b/layout/base/src/nsStyleSet.cpp @@ -91,6 +91,10 @@ public: nsIContent* aContent, nsIFrame* aParentFrame, nsIFrame*& aFrameSubTree); + NS_IMETHOD ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree); NS_IMETHOD ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer); @@ -483,6 +487,8 @@ nsIStyleContext* StyleSetImpl::ResolveStyleFor(nsIPresContext* aPresContext, } if (nsnull != rules) { + // XXX Stop-gap fix to prevent ua.css rules from being applied + // to XML elements nsIHTMLContent *htmlContent; nsresult rv = aContent->QueryInterface(kIHTMLContentIID, (void **)&htmlContent); PRInt32 ruleCount = 0; @@ -655,6 +661,16 @@ NS_IMETHODIMP StyleSetImpl::ConstructFrame(nsIPresContext* aPresContext, aParentFrame, aFrameSubTree); } +NS_IMETHODIMP +StyleSetImpl::ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree) +{ + return mFrameConstructor->ReconstructFrames(aPresContext, aContent, + aParentFrame, aFrameSubTree); +} + NS_IMETHODIMP StyleSetImpl::ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer) @@ -710,6 +726,7 @@ StyleSetImpl::AttributeChanged(nsIPresContext* aPresContext, aAttribute, aHint); } + // xxx style rules enumeration void StyleSetImpl::List(FILE* out, PRInt32 aIndent, nsISupportsArray* aSheets) diff --git a/layout/generic/nsHTMLFrame.cpp b/layout/generic/nsHTMLFrame.cpp index d112e19fa738..560bdabe325e 100644 --- a/layout/generic/nsHTMLFrame.cpp +++ b/layout/generic/nsHTMLFrame.cpp @@ -103,34 +103,41 @@ RootFrame::Reflow(nsIPresContext& aPresContext, NS_FRAME_TRACE_REFLOW_IN("RootFrame::Reflow"); aStatus = NS_FRAME_COMPLETE; + // XXX Copy the reflow state so that we can change the + // reason in cases where we're inserting and deleting + // frames from the root. Troy may want to reconstruct + // this routine. + nsHTMLReflowState reflowState(aReflowState); - if (eReflowReason_Incremental == aReflowState.reason) { + if (eReflowReason_Incremental == reflowState.reason) { // See if we're the target frame nsIFrame* targetFrame; - aReflowState.reflowCommand->GetTarget(targetFrame); + reflowState.reflowCommand->GetTarget(targetFrame); if (this == targetFrame) { nsIReflowCommand::ReflowType reflowType; nsIFrame* childFrame; // Get the reflow type - aReflowState.reflowCommand->GetType(reflowType); + reflowState.reflowCommand->GetType(reflowType); if ((nsIReflowCommand::FrameAppended == reflowType) || (nsIReflowCommand::FrameInserted == reflowType)) { // Insert the frame into the child list - aReflowState.reflowCommand->GetChildFrame(childFrame); + reflowState.reflowCommand->GetChildFrame(childFrame); if (nsnull == mFirstChild) { mFirstChild = childFrame; } else { nsIFrame* lastChild = LastFrame(mFirstChild); lastChild->SetNextSibling(childFrame); } - + // XXX This is wrong. Only the new child should be reflowed + // with this reason. The rest should just be resized. + reflowState.reason = eReflowReason_Initial; } else if (nsIReflowCommand::FrameRemoved == reflowType) { nsIFrame* deletedFrame; // Get the child frame we should delete - aReflowState.reflowCommand->GetChildFrame(deletedFrame); + reflowState.reflowCommand->GetChildFrame(deletedFrame); // Remove it from the child list if (deletedFrame == mFirstChild) { @@ -157,12 +164,13 @@ RootFrame::Reflow(nsIPresContext& aPresContext, // Delete the frame deletedFrame->DeleteFrame(aPresContext); + reflowState.reason = eReflowReason_Resize; } } else { nsIFrame* nextFrame; // Get the next frame in the reflow chain - aReflowState.reflowCommand->GetNext(nextFrame); + reflowState.reflowCommand->GetNext(nextFrame); NS_ASSERTION(nextFrame == mFirstChild, "unexpected next reflow command frame"); } } @@ -175,11 +183,11 @@ RootFrame::Reflow(nsIPresContext& aPresContext, nsMargin borderPadding; spacing->CalcBorderPaddingFor(this, borderPadding); - nsSize kidMaxSize(aReflowState.maxSize); + nsSize kidMaxSize(reflowState.maxSize); kidMaxSize.width -= borderPadding.left + borderPadding.right; kidMaxSize.height -= borderPadding.top + borderPadding.bottom; nsHTMLReflowMetrics desiredSize(nsnull); - nsHTMLReflowState kidReflowState(aPresContext, mFirstChild, aReflowState, + nsHTMLReflowState kidReflowState(aPresContext, mFirstChild, reflowState, kidMaxSize); // XXX HACK kidReflowState.widthConstraint = eHTMLFrameConstraint_Fixed; @@ -204,17 +212,17 @@ RootFrame::Reflow(nsIPresContext& aPresContext, // then do a repaint to make sure we repaint correctly. // XXX We could be smarter about only damaging the border/padding area // that was affected by the resize... - if ((eReflowReason_Resize == aReflowState.reason) && + if ((eReflowReason_Resize == reflowState.reason) && ((borderPadding.left != 0) || (borderPadding.top != 0) || (borderPadding.right != 0) || (borderPadding.bottom) != 0)) { - nsRect damageRect(0, 0, aReflowState.maxSize.width, aReflowState.maxSize.height); + nsRect damageRect(0, 0, reflowState.maxSize.width, reflowState.maxSize.height); Invalidate(damageRect, PR_FALSE); } } // Return the max size as our desired size - aDesiredSize.width = aReflowState.maxSize.width; - aDesiredSize.height = aReflowState.maxSize.height; + aDesiredSize.width = reflowState.maxSize.width; + aDesiredSize.height = reflowState.maxSize.height; aDesiredSize.ascent = aDesiredSize.height; aDesiredSize.descent = 0; diff --git a/layout/generic/nsTextFrame.cpp b/layout/generic/nsTextFrame.cpp index b6d0b8594708..a9b44637a3e1 100644 --- a/layout/generic/nsTextFrame.cpp +++ b/layout/generic/nsTextFrame.cpp @@ -1597,11 +1597,18 @@ TextFrame::Reflow(nsIPresContext& aPresContext, const nsHTMLReflowState& aReflowState, nsReflowStatus& aStatus) { - NS_PRECONDITION(nsnull != aReflowState.lineLayout, "no line layout"); + // NS_PRECONDITION(nsnull != aReflowState.lineLayout, "no line layout"); NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS, ("enter TextFrame::Reflow: aMaxSize=%d,%d", aReflowState.maxSize.width, aReflowState.maxSize.height)); + // XXX If there's no line layout, we shouldn't even have created this + // frame. This may happen if, for example, this is text inside a table + // but not inside a cell. For now, just don't reflow. + if (nsnull == aReflowState.lineLayout) { + return NS_OK; + } + // Get starting offset into the content PRInt32 startingOffset = 0; if (nsnull != mPrevInFlow) { diff --git a/layout/html/base/src/nsHTMLFrame.cpp b/layout/html/base/src/nsHTMLFrame.cpp index d112e19fa738..560bdabe325e 100644 --- a/layout/html/base/src/nsHTMLFrame.cpp +++ b/layout/html/base/src/nsHTMLFrame.cpp @@ -103,34 +103,41 @@ RootFrame::Reflow(nsIPresContext& aPresContext, NS_FRAME_TRACE_REFLOW_IN("RootFrame::Reflow"); aStatus = NS_FRAME_COMPLETE; + // XXX Copy the reflow state so that we can change the + // reason in cases where we're inserting and deleting + // frames from the root. Troy may want to reconstruct + // this routine. + nsHTMLReflowState reflowState(aReflowState); - if (eReflowReason_Incremental == aReflowState.reason) { + if (eReflowReason_Incremental == reflowState.reason) { // See if we're the target frame nsIFrame* targetFrame; - aReflowState.reflowCommand->GetTarget(targetFrame); + reflowState.reflowCommand->GetTarget(targetFrame); if (this == targetFrame) { nsIReflowCommand::ReflowType reflowType; nsIFrame* childFrame; // Get the reflow type - aReflowState.reflowCommand->GetType(reflowType); + reflowState.reflowCommand->GetType(reflowType); if ((nsIReflowCommand::FrameAppended == reflowType) || (nsIReflowCommand::FrameInserted == reflowType)) { // Insert the frame into the child list - aReflowState.reflowCommand->GetChildFrame(childFrame); + reflowState.reflowCommand->GetChildFrame(childFrame); if (nsnull == mFirstChild) { mFirstChild = childFrame; } else { nsIFrame* lastChild = LastFrame(mFirstChild); lastChild->SetNextSibling(childFrame); } - + // XXX This is wrong. Only the new child should be reflowed + // with this reason. The rest should just be resized. + reflowState.reason = eReflowReason_Initial; } else if (nsIReflowCommand::FrameRemoved == reflowType) { nsIFrame* deletedFrame; // Get the child frame we should delete - aReflowState.reflowCommand->GetChildFrame(deletedFrame); + reflowState.reflowCommand->GetChildFrame(deletedFrame); // Remove it from the child list if (deletedFrame == mFirstChild) { @@ -157,12 +164,13 @@ RootFrame::Reflow(nsIPresContext& aPresContext, // Delete the frame deletedFrame->DeleteFrame(aPresContext); + reflowState.reason = eReflowReason_Resize; } } else { nsIFrame* nextFrame; // Get the next frame in the reflow chain - aReflowState.reflowCommand->GetNext(nextFrame); + reflowState.reflowCommand->GetNext(nextFrame); NS_ASSERTION(nextFrame == mFirstChild, "unexpected next reflow command frame"); } } @@ -175,11 +183,11 @@ RootFrame::Reflow(nsIPresContext& aPresContext, nsMargin borderPadding; spacing->CalcBorderPaddingFor(this, borderPadding); - nsSize kidMaxSize(aReflowState.maxSize); + nsSize kidMaxSize(reflowState.maxSize); kidMaxSize.width -= borderPadding.left + borderPadding.right; kidMaxSize.height -= borderPadding.top + borderPadding.bottom; nsHTMLReflowMetrics desiredSize(nsnull); - nsHTMLReflowState kidReflowState(aPresContext, mFirstChild, aReflowState, + nsHTMLReflowState kidReflowState(aPresContext, mFirstChild, reflowState, kidMaxSize); // XXX HACK kidReflowState.widthConstraint = eHTMLFrameConstraint_Fixed; @@ -204,17 +212,17 @@ RootFrame::Reflow(nsIPresContext& aPresContext, // then do a repaint to make sure we repaint correctly. // XXX We could be smarter about only damaging the border/padding area // that was affected by the resize... - if ((eReflowReason_Resize == aReflowState.reason) && + if ((eReflowReason_Resize == reflowState.reason) && ((borderPadding.left != 0) || (borderPadding.top != 0) || (borderPadding.right != 0) || (borderPadding.bottom) != 0)) { - nsRect damageRect(0, 0, aReflowState.maxSize.width, aReflowState.maxSize.height); + nsRect damageRect(0, 0, reflowState.maxSize.width, reflowState.maxSize.height); Invalidate(damageRect, PR_FALSE); } } // Return the max size as our desired size - aDesiredSize.width = aReflowState.maxSize.width; - aDesiredSize.height = aReflowState.maxSize.height; + aDesiredSize.width = reflowState.maxSize.width; + aDesiredSize.height = reflowState.maxSize.height; aDesiredSize.ascent = aDesiredSize.height; aDesiredSize.descent = 0; diff --git a/layout/html/base/src/nsPresShell.cpp b/layout/html/base/src/nsPresShell.cpp index c17282780ae4..56153e429467 100644 --- a/layout/html/base/src/nsPresShell.cpp +++ b/layout/html/base/src/nsPresShell.cpp @@ -34,6 +34,7 @@ #include "nsContainerFrame.h" #include "nsHTMLIIDs.h" #include "nsIDeviceContext.h" +#include "nsHTMLParts.h" static PRBool gsNoisyRefs = PR_FALSE; #undef NOISY @@ -187,6 +188,9 @@ public: PRInt32 aIndexInContainer); NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet); + NS_IMETHOD StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled); NS_IMETHOD DocumentWillBeDestroyed(nsIDocument *aDocument); // nsIPresShell @@ -874,6 +878,31 @@ PresShell::StyleSheetAdded(nsIDocument *aDocument, return NS_OK; } +NS_IMETHODIMP +PresShell::StyleSheetDisabledStateChanged(nsIDocument *aDocument, + nsIStyleSheet* aStyleSheet, + PRBool aDisabled) +{ + nsresult rv = NS_OK; + if (nsnull != mRootFrame) { + nsIFrame* childFrame; + rv = mRootFrame->FirstChild(nsnull, childFrame); + + if (nsnull != mDocument) { + nsIContent* root = mDocument->GetRootContent(); + if (nsnull != root) { + + EnterReflowLock(); + rv = mStyleSet->ReconstructFrames(mPresContext, root, + mRootFrame, childFrame); + ExitReflowLock(); + NS_RELEASE(root); + } + } + } + return rv; +} + NS_IMETHODIMP PresShell::DocumentWillBeDestroyed(nsIDocument *aDocument) { diff --git a/layout/html/base/src/nsTextFrame.cpp b/layout/html/base/src/nsTextFrame.cpp index b6d0b8594708..a9b44637a3e1 100644 --- a/layout/html/base/src/nsTextFrame.cpp +++ b/layout/html/base/src/nsTextFrame.cpp @@ -1597,11 +1597,18 @@ TextFrame::Reflow(nsIPresContext& aPresContext, const nsHTMLReflowState& aReflowState, nsReflowStatus& aStatus) { - NS_PRECONDITION(nsnull != aReflowState.lineLayout, "no line layout"); + // NS_PRECONDITION(nsnull != aReflowState.lineLayout, "no line layout"); NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS, ("enter TextFrame::Reflow: aMaxSize=%d,%d", aReflowState.maxSize.width, aReflowState.maxSize.height)); + // XXX If there's no line layout, we shouldn't even have created this + // frame. This may happen if, for example, this is text inside a table + // but not inside a cell. For now, just don't reflow. + if (nsnull == aReflowState.lineLayout) { + return NS_OK; + } + // Get starting offset into the content PRInt32 startingOffset = 0; if (nsnull != mPrevInFlow) { diff --git a/layout/html/document/src/nsHTMLDocument.cpp b/layout/html/document/src/nsHTMLDocument.cpp index 11f80712b653..11326cd7b96c 100644 --- a/layout/html/document/src/nsHTMLDocument.cpp +++ b/layout/html/document/src/nsHTMLDocument.cpp @@ -23,7 +23,6 @@ #include "nsIHTMLStyleSheet.h" #include "nsIHTMLCSSStyleSheet.h" #include "nsIStyleSet.h" -#include "nsIDocumentObserver.h" #include "nsHTMLAtoms.h" #include "nsIPresShell.h" #include "nsIPresContext.h" @@ -49,8 +48,6 @@ #include "nsIFormManager.h" #include "nsRepository.h" #include "nsParserCIID.h" -#include "nsIDOMStyleSheet.h" -#include "nsIDOMStyleSheetCollection.h" #include "nsIDOMHTMLElement.h" @@ -79,241 +76,10 @@ static NS_DEFINE_IID(kIDOMHTMLDocumentIID, NS_IDOMHTMLDOCUMENT_IID); static NS_DEFINE_IID(kIDOMNSHTMLDocumentIID, NS_IDOMNSHTMLDOCUMENT_IID); static NS_DEFINE_IID(kINetServiceIID, NS_INETSERVICE_IID); static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID); -static NS_DEFINE_IID(kIDOMStyleSheetCollectionIID, NS_IDOMSTYLESHEETCOLLECTION_IID); -static NS_DEFINE_IID(kIDOMStyleSheetIID, NS_IDOMSTYLESHEET_IID); static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); -static NS_DEFINE_IID(kIDocumentObserverIID, NS_IDOCUMENT_OBSERVER_IID); static NS_DEFINE_IID(kIHTMLContentContainerIID, NS_IHTMLCONTENTCONTAINER_IID); static NS_DEFINE_IID(kIDOMHTMLElementIID, NS_IDOMHTMLELEMENT_IID); -class nsDOMStyleSheetCollection : public nsIDOMStyleSheetCollection, - public nsIScriptObjectOwner, - public nsIDocumentObserver -{ -public: - nsDOMStyleSheetCollection(nsIDocument *aDocument); - ~nsDOMStyleSheetCollection(); - - NS_DECL_ISUPPORTS - NS_DECL_IDOMSTYLESHEETCOLLECTION - - NS_IMETHOD BeginUpdate(nsIDocument *aDocument) { return NS_OK; } - NS_IMETHOD EndUpdate(nsIDocument *aDocument) { return NS_OK; } - NS_IMETHOD BeginLoad(nsIDocument *aDocument) { return NS_OK; } - NS_IMETHOD EndLoad(nsIDocument *aDocument) { return NS_OK; } - NS_IMETHOD BeginReflow(nsIDocument *aDocument, - nsIPresShell* aShell) { return NS_OK; } - NS_IMETHOD EndReflow(nsIDocument *aDocument, - nsIPresShell* aShell) { return NS_OK; } - NS_IMETHOD ContentChanged(nsIDocument *aDocument, - nsIContent* aContent, - nsISupports* aSubContent) { return NS_OK; } - NS_IMETHOD AttributeChanged(nsIDocument *aDocument, - nsIContent* aContent, - nsIAtom* aAttribute, - PRInt32 aHint) { return NS_OK; } - NS_IMETHOD ContentAppended(nsIDocument *aDocument, - nsIContent* aContainer, - PRInt32 aNewIndexInContainer) - { return NS_OK; } - NS_IMETHOD ContentInserted(nsIDocument *aDocument, - nsIContent* aContainer, - nsIContent* aChild, - PRInt32 aIndexInContainer) { return NS_OK; } - NS_IMETHOD ContentReplaced(nsIDocument *aDocument, - nsIContent* aContainer, - nsIContent* aOldChild, - nsIContent* aNewChild, - PRInt32 aIndexInContainer) { return NS_OK; } - NS_IMETHOD ContentRemoved(nsIDocument *aDocument, - nsIContent* aContainer, - nsIContent* aChild, - PRInt32 aIndexInContainer) { return NS_OK; } - NS_IMETHOD StyleSheetAdded(nsIDocument *aDocument, - nsIStyleSheet* aStyleSheet); - NS_IMETHOD DocumentWillBeDestroyed(nsIDocument *aDocument); - - // nsIScriptObjectOwner interface - NS_IMETHOD GetScriptObject(nsIScriptContext *aContext, void** aScriptObject); - NS_IMETHOD SetScriptObject(void* aScriptObject); - -protected: - PRInt32 mLength; - nsIDocument* mDocument; - void* mScriptObject; -}; - -nsDOMStyleSheetCollection::nsDOMStyleSheetCollection(nsIDocument *aDocument) -{ - NS_INIT_REFCNT(); - mLength = -1; - // Not reference counted to avoid circular references. - // The document will tell us when its going away. - mDocument = aDocument; - mDocument->AddObserver(this); - mScriptObject = nsnull; -} - -nsDOMStyleSheetCollection::~nsDOMStyleSheetCollection() -{ - if (nsnull != mDocument) { - mDocument->RemoveObserver(this); - } - mDocument = nsnull; -} - -NS_IMPL_ADDREF(nsDOMStyleSheetCollection) -NS_IMPL_RELEASE(nsDOMStyleSheetCollection) - -nsresult -nsDOMStyleSheetCollection::QueryInterface(REFNSIID aIID, void** aInstancePtrResult) -{ - if (NULL == aInstancePtrResult) { - return NS_ERROR_NULL_POINTER; - } - - static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); - if (aIID.Equals(kIDOMStyleSheetCollectionIID)) { - nsIDOMStyleSheetCollection *tmp = this; - *aInstancePtrResult = (void*) tmp; - AddRef(); - return NS_OK; - } - if (aIID.Equals(kIScriptObjectOwnerIID)) { - nsIScriptObjectOwner *tmp = this; - *aInstancePtrResult = (void*) tmp; - AddRef(); - return NS_OK; - } - if (aIID.Equals(kIDocumentObserverIID)) { - nsIDocumentObserver *tmp = this; - *aInstancePtrResult = (void*) tmp; - AddRef(); - return NS_OK; - } - if (aIID.Equals(kISupportsIID)) { - nsIDOMStyleSheetCollection *tmp = this; - nsISupports *tmp2 = tmp; - *aInstancePtrResult = (void*) tmp2; - AddRef(); - return NS_OK; - } - return NS_NOINTERFACE; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::GetLength(PRUint32* aLength) -{ - if (nsnull != mDocument) { - // XXX Find the number and then cache it. We'll use the - // observer notification to figure out if new ones have - // been added or removed. - if (-1 == mLength) { - PRUint32 count = 0; - PRInt32 i, imax = mDocument->GetNumberOfStyleSheets(); - - for (i = 0; i < imax; i++) { - nsIStyleSheet *sheet = mDocument->GetStyleSheetAt(i); - nsIDOMStyleSheet *domss; - - if (NS_OK == sheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { - count++; - NS_RELEASE(domss); - } - - NS_RELEASE(sheet); - } - mLength = count; - } - *aLength = mLength; - } - else { - *aLength = 0; - } - - return NS_OK; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::Item(PRUint32 aIndex, nsIDOMStyleSheet** aReturn) -{ - *aReturn = nsnull; - if (nsnull != mDocument) { - PRUint32 count = 0; - PRInt32 i, imax = mDocument->GetNumberOfStyleSheets(); - - // XXX Not particularly efficient, but does anyone care? - for (i = 0; (i < imax) && (nsnull == *aReturn); i++) { - nsIStyleSheet *sheet = mDocument->GetStyleSheetAt(i); - nsIDOMStyleSheet *domss; - - if (NS_OK == sheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { - if (count++ == aIndex) { - *aReturn = domss; - NS_ADDREF(domss); - } - NS_RELEASE(domss); - } - - NS_RELEASE(sheet); - } - } - - return NS_OK; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject) -{ - nsresult res = NS_OK; - - if (nsnull == mScriptObject) { - nsISupports *supports = (nsISupports *)(nsIDOMStyleSheetCollection *)this; - nsISupports *parent = (nsISupports *)mDocument; - - // XXX Should be done through factory - res = NS_NewScriptStyleSheetCollection(aContext, - supports, - parent, - (void**)&mScriptObject); - } - *aScriptObject = mScriptObject; - - return res; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::SetScriptObject(void* aScriptObject) -{ - mScriptObject = aScriptObject; - return NS_OK; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::StyleSheetAdded(nsIDocument *aDocument, - nsIStyleSheet* aStyleSheet) -{ - if (-1 != mLength) { - nsIDOMStyleSheet *domss; - if (NS_OK == aStyleSheet->QueryInterface(kIDOMStyleSheetIID, (void **)&domss)) { - mLength++; - NS_RELEASE(domss); - } - } - - return NS_OK; -} - -NS_IMETHODIMP -nsDOMStyleSheetCollection::DocumentWillBeDestroyed(nsIDocument *aDocument) -{ - if (nsnull != mDocument) { - aDocument->RemoveObserver(this); - mDocument = nsnull; - } - - return NS_OK; -} NS_LAYOUT nsresult NS_NewHTMLDocument(nsIDocument** aInstancePtrResult) @@ -334,7 +100,6 @@ nsHTMLDocument::nsHTMLDocument() mAnchors = nsnull; mNamedItems = nsnull; mParser = nsnull; - mDOMStyleSheets = nsnull; nsHTMLAtoms::AddrefAtoms(); mDTDMode = eDTDMode_NoQuirks; @@ -366,7 +131,6 @@ nsHTMLDocument::~nsHTMLDocument() NS_IF_RELEASE(mEmbeds); NS_IF_RELEASE(mLinks); NS_IF_RELEASE(mAnchors); - NS_IF_RELEASE(mDOMStyleSheets); NS_IF_RELEASE(mAttrStyleSheet); NS_IF_RELEASE(mStyleAttrStyleSheet); NS_IF_RELEASE(mParser); @@ -1098,22 +862,6 @@ nsHTMLDocument::GetLastModified(nsString& aLastModified) return NS_ERROR_NOT_IMPLEMENTED; } -NS_IMETHODIMP -nsHTMLDocument::GetStyleSheets(nsIDOMStyleSheetCollection** aStyleSheets) -{ - if (nsnull == mDOMStyleSheets) { - mDOMStyleSheets = new nsDOMStyleSheetCollection(this); - if (nsnull == mDOMStyleSheets) { - return NS_ERROR_OUT_OF_MEMORY; - } - NS_ADDREF(mDOMStyleSheets); - } - - *aStyleSheets = mDOMStyleSheets; - NS_ADDREF(mDOMStyleSheets); - - return NS_OK; -} NS_IMETHODIMP nsHTMLDocument::GetEmbeds(nsIDOMHTMLCollection** aEmbeds) diff --git a/layout/html/document/src/nsHTMLDocument.h b/layout/html/document/src/nsHTMLDocument.h index 639f99755270..c7867aaa4fc1 100644 --- a/layout/html/document/src/nsHTMLDocument.h +++ b/layout/html/document/src/nsHTMLDocument.h @@ -33,7 +33,6 @@ class nsContentList; class nsIContentViewerContainer; class nsIParser; class BlockText; -class nsDOMStyleSheetCollection; class nsHTMLDocument : public nsMarkupDocument, public nsIHTMLDocument, public nsIDOMHTMLDocument, public nsIDOMNSHTMLDocument, public nsIHTMLContentContainer { public: @@ -159,7 +158,6 @@ protected: nsContentList *mLinks; nsContentList *mAnchors; nsContentList *mForms; - nsDOMStyleSheetCollection *mDOMStyleSheets; PLHashTable *mNamedItems; diff --git a/layout/html/style/src/nsCSSStyleSheet.cpp b/layout/html/style/src/nsCSSStyleSheet.cpp index b593d52e97ac..b87a220207dc 100644 --- a/layout/html/style/src/nsCSSStyleSheet.cpp +++ b/layout/html/style/src/nsCSSStyleSheet.cpp @@ -567,6 +567,8 @@ public: virtual PRInt32 StyleSheetCount(); virtual nsresult GetStyleSheetAt(PRInt32 aIndex, nsICSSStyleSheet*& aSheet); + virtual void SetDocument(nsIDocument *aDocument); + virtual void List(FILE* out = stdout, PRInt32 aIndent = 0) const; // nsIDOMStyleSheet interface @@ -614,6 +616,8 @@ protected: RuleHash* mRuleHash; CSSStyleRuleCollectionImpl* mRuleCollection; CSSImportsCollectionImpl* mImportsCollection; + nsIDocument* mDocument; + PRBool mDisabled; void * mScriptObject; }; @@ -668,6 +672,8 @@ CSSStyleSheetImpl::CSSStyleSheetImpl(nsIURL* aURL) mParent = nsnull; mRuleCollection = nsnull; mImportsCollection = nsnull; + mDocument = nsnull; + mDisabled = PR_FALSE; mScriptObject = nsnull; #ifdef DEBUG_REFS ++gInstanceCount; @@ -711,6 +717,9 @@ CSSStyleSheetImpl::~CSSStyleSheetImpl() mOrderedRules->EnumerateForwards(DropStyleSheetReference, nsnull); } ClearHash(); + // XXX The document reference is not reference counted and should + // not be released. The document will let us know when it is going + // away. } NS_IMPL_ADDREF(CSSStyleSheetImpl) @@ -1234,6 +1243,13 @@ nsresult CSSStyleSheetImpl::GetStyleSheetAt(PRInt32 aIndex, nsICSSStyleSheet*& a return NS_OK; } +void CSSStyleSheetImpl::SetDocument(nsIDocument *aDocument) +{ + // This reference is not reference counted and should not be + // released. The document will tell us when it goes away. + mDocument = aDocument; +} + void CSSStyleSheetImpl::List(FILE* out, PRInt32 aIndent) const { nsAutoString buffer; @@ -1290,15 +1306,20 @@ void CSSStyleSheetImpl::BuildHash(void) NS_IMETHODIMP CSSStyleSheetImpl::GetDisabled(PRBool* aDisabled) { - // XXX TBI - *aDisabled = PR_FALSE; + *aDisabled = mDisabled; return NS_OK; } NS_IMETHODIMP CSSStyleSheetImpl::SetDisabled(PRBool aDisabled) { - // XXX TBI + + if ((nsnull != mDocument) && (mDisabled != aDisabled)) { + mDocument->SetStyleSheetDisabledState(this, aDisabled); + } + + mDisabled = aDisabled; + return NS_OK; } diff --git a/layout/html/style/src/nsHTMLStyleSheet.cpp b/layout/html/style/src/nsHTMLStyleSheet.cpp index 54fe2f01a6e7..290239f8bb30 100644 --- a/layout/html/style/src/nsHTMLStyleSheet.cpp +++ b/layout/html/style/src/nsHTMLStyleSheet.cpp @@ -246,6 +246,11 @@ public: nsIFrame* aParentFrame, nsIFrame*& aFrameSubTree); + NS_IMETHODIMP ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree); + NS_IMETHOD ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer); @@ -300,6 +305,12 @@ protected: nsIStyleContext* aStyleContext, nsIFrame*& aNewFrame); + nsresult ConstructXMLRootDescendants(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIStyleContext* aRootPseudoStyle, + nsIFrame* aParentFrame, + nsIFrame*& aNewFrame); + nsresult ConstructXMLRootFrame(nsIPresContext* aPresContext, nsIContent* aContent, nsIStyleContext* aStyleContext, @@ -1213,6 +1224,53 @@ HTMLStyleSheetImpl::ConstructRootFrame(nsIPresContext* aPresContext, return rv; } +nsresult +HTMLStyleSheetImpl::ConstructXMLRootDescendants(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIStyleContext* aRootPseudoStyle, + nsIFrame* aParentFrame, + nsIFrame*& aNewFrame) +{ + nsresult rv = NS_OK; + // Create a scroll frame. + // XXX Use the rootPseudoStyle overflow style information to decide whether + // we create a scroll frame or just a body wrapper frame... + nsIFrame* scrollFrame = nsnull; + + rv = NS_NewScrollFrame(nsnull, aParentFrame, scrollFrame); + + if (NS_SUCCEEDED(rv)) { + // The scroll frame gets the root pseudo style context, and the scrolled + // frame gets a SCROLLED-CONTENT pseudo element style context. + // XXX We should probably use a different pseudo style context... + scrollFrame->SetStyleContext(aPresContext, aRootPseudoStyle); + + nsIStyleContext* scrolledPseudoStyle; + nsIFrame* wrapperFrame; + + scrolledPseudoStyle = aPresContext->ResolvePseudoStyleContextFor + (nsnull, nsHTMLAtoms::scrolledContentPseudo, + aRootPseudoStyle); + + // Create a body frame to wrap the document element + NS_NewBodyFrame(nsnull, scrollFrame, + wrapperFrame, NS_BODY_THE_BODY|NS_BODY_SHRINK_WRAP); + wrapperFrame->SetStyleContext(aPresContext, scrolledPseudoStyle); + + // Construct a frame for the document element and process its children + nsIFrame* docElementFrame; + ConstructFrame(aPresContext, aContent, wrapperFrame, docElementFrame); + wrapperFrame->SetInitialChildList(*aPresContext, nsnull, docElementFrame); + + // Set the scroll frame's initial child list + scrollFrame->SetInitialChildList(*aPresContext, nsnull, wrapperFrame); + } + + aNewFrame = scrollFrame; + + return rv; +} + nsresult HTMLStyleSheetImpl::ConstructXMLRootFrame(nsIPresContext* aPresContext, nsIContent* aContent, @@ -1253,36 +1311,16 @@ HTMLStyleSheetImpl::ConstructXMLRootFrame(nsIPresContext* aPresContext, // we create a scroll frame or just a body wrapper frame... nsIFrame* scrollFrame; - if (NS_SUCCEEDED(NS_NewScrollFrame(nsnull, aNewFrame, scrollFrame))) { - // The scroll frame gets the root pseudo style context, and the scrolled - // frame gets a SCROLLED-CONTENT pseudo element style context. - // XXX We should probably use a different pseudo style context... - scrollFrame->SetStyleContext(aPresContext, rootPseudoStyle); - - nsIStyleContext* scrolledPseudoStyle; - nsIFrame* wrapperFrame; - - scrolledPseudoStyle = aPresContext->ResolvePseudoStyleContextFor - (nsnull, nsHTMLAtoms::scrolledContentPseudo, - rootPseudoStyle); - - // Create a body frame to wrap the document element - NS_NewBodyFrame(nsnull, scrollFrame, wrapperFrame, NS_BODY_THE_BODY|NS_BODY_SHRINK_WRAP); - wrapperFrame->SetStyleContext(aPresContext, scrolledPseudoStyle); - - // Construct a frame for the document element and process its children - nsIFrame* docElementFrame; - ConstructFrame(aPresContext, aContent, wrapperFrame, docElementFrame); - wrapperFrame->SetInitialChildList(*aPresContext, nsnull, docElementFrame); - - // Set the scroll frame's initial child list - scrollFrame->SetInitialChildList(*aPresContext, nsnull, wrapperFrame); - } + rv = ConstructXMLRootDescendants(aPresContext, aContent, rootPseudoStyle, + aNewFrame, scrollFrame); // initialize the root frame - aNewFrame->SetInitialChildList(*aPresContext, nsnull, scrollFrame); + if (NS_SUCCEEDED(rv)) { + aNewFrame->SetInitialChildList(*aPresContext, nsnull, scrollFrame); + } } + NS_IF_RELEASE(rootPseudoStyle); return rv; } @@ -1750,6 +1788,69 @@ HTMLStyleSheetImpl::ConstructFrame(nsIPresContext* aPresContext, return rv; } +NS_IMETHODIMP +HTMLStyleSheetImpl::ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree) +{ + nsresult rv = NS_OK; + + nsIDocument* document; + rv = aContent->GetDocument(document); + if (NS_FAILED(rv)) { + return rv; + } + + // XXX Currently we only know how to do this for XML documents + if (nsnull != document) { + nsIPresShell* shell = aPresContext->GetShell(); + nsIXMLDocument* xmlDocument; + rv = document->QueryInterface(kIXMLDocumentIID, (void **)&xmlDocument); + + if (NS_SUCCEEDED(rv)) { + nsIReflowCommand* reflowCmd; + + rv = NS_NewHTMLReflowCommand(&reflowCmd, aParentFrame, + nsIReflowCommand::FrameRemoved, + aFrameSubTree); + + if (NS_SUCCEEDED(rv)) { + shell->AppendReflowCommand(reflowCmd); + NS_RELEASE(reflowCmd); + + nsIFrame *newChild; + // XXX See ConstructXMLContents for an explanation of why + // we create this pseudostyle + nsIStyleContext* rootPseudoStyle; + rootPseudoStyle = aPresContext->ResolvePseudoStyleContextFor(nsnull, + nsHTMLAtoms::xmlRootPseudo, nsnull); + + rv = ConstructXMLRootDescendants(aPresContext, aContent, + rootPseudoStyle, aParentFrame, + newChild); + if (NS_SUCCEEDED(rv)) { + rv = NS_NewHTMLReflowCommand(&reflowCmd, aParentFrame, + nsIReflowCommand::FrameInserted, + newChild); + + if (NS_SUCCEEDED(rv)) { + shell->AppendReflowCommand(reflowCmd); + NS_RELEASE(reflowCmd); + } + } + NS_IF_RELEASE(rootPseudoStyle); + } + NS_RELEASE(xmlDocument); + } + NS_RELEASE(document); + NS_RELEASE(shell); + } + + return rv; + +} + nsIFrame* HTMLStyleSheetImpl::GetFrameFor(nsIPresShell* aPresShell, nsIPresContext* aPresContext, nsIContent* aContent) diff --git a/layout/html/style/src/nsICSSStyleSheet.h b/layout/html/style/src/nsICSSStyleSheet.h index 2fb5e56341f7..cebe76e6ea52 100644 --- a/layout/html/style/src/nsICSSStyleSheet.h +++ b/layout/html/style/src/nsICSSStyleSheet.h @@ -22,6 +22,7 @@ #include "nsIStyleSheet.h" class nsICSSStyleRule; +class nsIDocument; // IID for the nsICSSStyleSheet interface {8f83b0f0-b21a-11d1-8031-006008159b5a} #define NS_ICSS_STYLE_SHEET_IID \ @@ -43,6 +44,7 @@ public: virtual PRInt32 StyleSheetCount() = 0; virtual nsresult GetStyleSheetAt(PRInt32 aIndex, nsICSSStyleSheet*& aSheet) = 0; + virtual void SetDocument(nsIDocument* aDocument) = 0; }; extern NS_HTML nsresult diff --git a/layout/style/nsCSSStyleSheet.cpp b/layout/style/nsCSSStyleSheet.cpp index b593d52e97ac..b87a220207dc 100644 --- a/layout/style/nsCSSStyleSheet.cpp +++ b/layout/style/nsCSSStyleSheet.cpp @@ -567,6 +567,8 @@ public: virtual PRInt32 StyleSheetCount(); virtual nsresult GetStyleSheetAt(PRInt32 aIndex, nsICSSStyleSheet*& aSheet); + virtual void SetDocument(nsIDocument *aDocument); + virtual void List(FILE* out = stdout, PRInt32 aIndent = 0) const; // nsIDOMStyleSheet interface @@ -614,6 +616,8 @@ protected: RuleHash* mRuleHash; CSSStyleRuleCollectionImpl* mRuleCollection; CSSImportsCollectionImpl* mImportsCollection; + nsIDocument* mDocument; + PRBool mDisabled; void * mScriptObject; }; @@ -668,6 +672,8 @@ CSSStyleSheetImpl::CSSStyleSheetImpl(nsIURL* aURL) mParent = nsnull; mRuleCollection = nsnull; mImportsCollection = nsnull; + mDocument = nsnull; + mDisabled = PR_FALSE; mScriptObject = nsnull; #ifdef DEBUG_REFS ++gInstanceCount; @@ -711,6 +717,9 @@ CSSStyleSheetImpl::~CSSStyleSheetImpl() mOrderedRules->EnumerateForwards(DropStyleSheetReference, nsnull); } ClearHash(); + // XXX The document reference is not reference counted and should + // not be released. The document will let us know when it is going + // away. } NS_IMPL_ADDREF(CSSStyleSheetImpl) @@ -1234,6 +1243,13 @@ nsresult CSSStyleSheetImpl::GetStyleSheetAt(PRInt32 aIndex, nsICSSStyleSheet*& a return NS_OK; } +void CSSStyleSheetImpl::SetDocument(nsIDocument *aDocument) +{ + // This reference is not reference counted and should not be + // released. The document will tell us when it goes away. + mDocument = aDocument; +} + void CSSStyleSheetImpl::List(FILE* out, PRInt32 aIndent) const { nsAutoString buffer; @@ -1290,15 +1306,20 @@ void CSSStyleSheetImpl::BuildHash(void) NS_IMETHODIMP CSSStyleSheetImpl::GetDisabled(PRBool* aDisabled) { - // XXX TBI - *aDisabled = PR_FALSE; + *aDisabled = mDisabled; return NS_OK; } NS_IMETHODIMP CSSStyleSheetImpl::SetDisabled(PRBool aDisabled) { - // XXX TBI + + if ((nsnull != mDocument) && (mDisabled != aDisabled)) { + mDocument->SetStyleSheetDisabledState(this, aDisabled); + } + + mDisabled = aDisabled; + return NS_OK; } diff --git a/layout/style/nsHTMLStyleSheet.cpp b/layout/style/nsHTMLStyleSheet.cpp index 54fe2f01a6e7..290239f8bb30 100644 --- a/layout/style/nsHTMLStyleSheet.cpp +++ b/layout/style/nsHTMLStyleSheet.cpp @@ -246,6 +246,11 @@ public: nsIFrame* aParentFrame, nsIFrame*& aFrameSubTree); + NS_IMETHODIMP ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree); + NS_IMETHOD ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer); @@ -300,6 +305,12 @@ protected: nsIStyleContext* aStyleContext, nsIFrame*& aNewFrame); + nsresult ConstructXMLRootDescendants(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIStyleContext* aRootPseudoStyle, + nsIFrame* aParentFrame, + nsIFrame*& aNewFrame); + nsresult ConstructXMLRootFrame(nsIPresContext* aPresContext, nsIContent* aContent, nsIStyleContext* aStyleContext, @@ -1213,6 +1224,53 @@ HTMLStyleSheetImpl::ConstructRootFrame(nsIPresContext* aPresContext, return rv; } +nsresult +HTMLStyleSheetImpl::ConstructXMLRootDescendants(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIStyleContext* aRootPseudoStyle, + nsIFrame* aParentFrame, + nsIFrame*& aNewFrame) +{ + nsresult rv = NS_OK; + // Create a scroll frame. + // XXX Use the rootPseudoStyle overflow style information to decide whether + // we create a scroll frame or just a body wrapper frame... + nsIFrame* scrollFrame = nsnull; + + rv = NS_NewScrollFrame(nsnull, aParentFrame, scrollFrame); + + if (NS_SUCCEEDED(rv)) { + // The scroll frame gets the root pseudo style context, and the scrolled + // frame gets a SCROLLED-CONTENT pseudo element style context. + // XXX We should probably use a different pseudo style context... + scrollFrame->SetStyleContext(aPresContext, aRootPseudoStyle); + + nsIStyleContext* scrolledPseudoStyle; + nsIFrame* wrapperFrame; + + scrolledPseudoStyle = aPresContext->ResolvePseudoStyleContextFor + (nsnull, nsHTMLAtoms::scrolledContentPseudo, + aRootPseudoStyle); + + // Create a body frame to wrap the document element + NS_NewBodyFrame(nsnull, scrollFrame, + wrapperFrame, NS_BODY_THE_BODY|NS_BODY_SHRINK_WRAP); + wrapperFrame->SetStyleContext(aPresContext, scrolledPseudoStyle); + + // Construct a frame for the document element and process its children + nsIFrame* docElementFrame; + ConstructFrame(aPresContext, aContent, wrapperFrame, docElementFrame); + wrapperFrame->SetInitialChildList(*aPresContext, nsnull, docElementFrame); + + // Set the scroll frame's initial child list + scrollFrame->SetInitialChildList(*aPresContext, nsnull, wrapperFrame); + } + + aNewFrame = scrollFrame; + + return rv; +} + nsresult HTMLStyleSheetImpl::ConstructXMLRootFrame(nsIPresContext* aPresContext, nsIContent* aContent, @@ -1253,36 +1311,16 @@ HTMLStyleSheetImpl::ConstructXMLRootFrame(nsIPresContext* aPresContext, // we create a scroll frame or just a body wrapper frame... nsIFrame* scrollFrame; - if (NS_SUCCEEDED(NS_NewScrollFrame(nsnull, aNewFrame, scrollFrame))) { - // The scroll frame gets the root pseudo style context, and the scrolled - // frame gets a SCROLLED-CONTENT pseudo element style context. - // XXX We should probably use a different pseudo style context... - scrollFrame->SetStyleContext(aPresContext, rootPseudoStyle); - - nsIStyleContext* scrolledPseudoStyle; - nsIFrame* wrapperFrame; - - scrolledPseudoStyle = aPresContext->ResolvePseudoStyleContextFor - (nsnull, nsHTMLAtoms::scrolledContentPseudo, - rootPseudoStyle); - - // Create a body frame to wrap the document element - NS_NewBodyFrame(nsnull, scrollFrame, wrapperFrame, NS_BODY_THE_BODY|NS_BODY_SHRINK_WRAP); - wrapperFrame->SetStyleContext(aPresContext, scrolledPseudoStyle); - - // Construct a frame for the document element and process its children - nsIFrame* docElementFrame; - ConstructFrame(aPresContext, aContent, wrapperFrame, docElementFrame); - wrapperFrame->SetInitialChildList(*aPresContext, nsnull, docElementFrame); - - // Set the scroll frame's initial child list - scrollFrame->SetInitialChildList(*aPresContext, nsnull, wrapperFrame); - } + rv = ConstructXMLRootDescendants(aPresContext, aContent, rootPseudoStyle, + aNewFrame, scrollFrame); // initialize the root frame - aNewFrame->SetInitialChildList(*aPresContext, nsnull, scrollFrame); + if (NS_SUCCEEDED(rv)) { + aNewFrame->SetInitialChildList(*aPresContext, nsnull, scrollFrame); + } } + NS_IF_RELEASE(rootPseudoStyle); return rv; } @@ -1750,6 +1788,69 @@ HTMLStyleSheetImpl::ConstructFrame(nsIPresContext* aPresContext, return rv; } +NS_IMETHODIMP +HTMLStyleSheetImpl::ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree) +{ + nsresult rv = NS_OK; + + nsIDocument* document; + rv = aContent->GetDocument(document); + if (NS_FAILED(rv)) { + return rv; + } + + // XXX Currently we only know how to do this for XML documents + if (nsnull != document) { + nsIPresShell* shell = aPresContext->GetShell(); + nsIXMLDocument* xmlDocument; + rv = document->QueryInterface(kIXMLDocumentIID, (void **)&xmlDocument); + + if (NS_SUCCEEDED(rv)) { + nsIReflowCommand* reflowCmd; + + rv = NS_NewHTMLReflowCommand(&reflowCmd, aParentFrame, + nsIReflowCommand::FrameRemoved, + aFrameSubTree); + + if (NS_SUCCEEDED(rv)) { + shell->AppendReflowCommand(reflowCmd); + NS_RELEASE(reflowCmd); + + nsIFrame *newChild; + // XXX See ConstructXMLContents for an explanation of why + // we create this pseudostyle + nsIStyleContext* rootPseudoStyle; + rootPseudoStyle = aPresContext->ResolvePseudoStyleContextFor(nsnull, + nsHTMLAtoms::xmlRootPseudo, nsnull); + + rv = ConstructXMLRootDescendants(aPresContext, aContent, + rootPseudoStyle, aParentFrame, + newChild); + if (NS_SUCCEEDED(rv)) { + rv = NS_NewHTMLReflowCommand(&reflowCmd, aParentFrame, + nsIReflowCommand::FrameInserted, + newChild); + + if (NS_SUCCEEDED(rv)) { + shell->AppendReflowCommand(reflowCmd); + NS_RELEASE(reflowCmd); + } + } + NS_IF_RELEASE(rootPseudoStyle); + } + NS_RELEASE(xmlDocument); + } + NS_RELEASE(document); + NS_RELEASE(shell); + } + + return rv; + +} + nsIFrame* HTMLStyleSheetImpl::GetFrameFor(nsIPresShell* aPresShell, nsIPresContext* aPresContext, nsIContent* aContent) diff --git a/layout/style/nsStyleSet.cpp b/layout/style/nsStyleSet.cpp index c537621c5093..2a62721ab2e7 100644 --- a/layout/style/nsStyleSet.cpp +++ b/layout/style/nsStyleSet.cpp @@ -91,6 +91,10 @@ public: nsIContent* aContent, nsIFrame* aParentFrame, nsIFrame*& aFrameSubTree); + NS_IMETHOD ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree); NS_IMETHOD ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer); @@ -483,6 +487,8 @@ nsIStyleContext* StyleSetImpl::ResolveStyleFor(nsIPresContext* aPresContext, } if (nsnull != rules) { + // XXX Stop-gap fix to prevent ua.css rules from being applied + // to XML elements nsIHTMLContent *htmlContent; nsresult rv = aContent->QueryInterface(kIHTMLContentIID, (void **)&htmlContent); PRInt32 ruleCount = 0; @@ -655,6 +661,16 @@ NS_IMETHODIMP StyleSetImpl::ConstructFrame(nsIPresContext* aPresContext, aParentFrame, aFrameSubTree); } +NS_IMETHODIMP +StyleSetImpl::ReconstructFrames(nsIPresContext* aPresContext, + nsIContent* aContent, + nsIFrame* aParentFrame, + nsIFrame* aFrameSubTree) +{ + return mFrameConstructor->ReconstructFrames(aPresContext, aContent, + aParentFrame, aFrameSubTree); +} + NS_IMETHODIMP StyleSetImpl::ContentAppended(nsIPresContext* aPresContext, nsIContent* aContainer, PRInt32 aNewIndexInContainer) @@ -710,6 +726,7 @@ StyleSetImpl::AttributeChanged(nsIPresContext* aPresContext, aAttribute, aHint); } + // xxx style rules enumeration void StyleSetImpl::List(FILE* out, PRInt32 aIndent, nsISupportsArray* aSheets)