cleaned up style sheet ordering

support for selecting alternate style sheets
This commit is contained in:
peterl%netscape.com 1999-01-23 07:07:09 +00:00
Родитель ec711cef0c
Коммит bbbe75c6c2
4 изменённых файлов: 168 добавлений и 10 удалений

Просмотреть файл

@ -482,16 +482,60 @@ NS_IMETHODIMP nsHTMLDocument::GetInlineStyleSheet(nsIHTMLCSSStyleSheet** aResult
return NS_OK; return NS_OK;
} }
void nsHTMLDocument::AddStyleSheetToSet(nsIStyleSheet* aSheet, nsIStyleSet* aSet) void nsHTMLDocument::InternalAddStyleSheet(nsIStyleSheet* aSheet) // subclass hook for sheet ordering
{ {
if ((nsnull != mStyleAttrStyleSheet) && (aSheet != mStyleAttrStyleSheet)) { if (aSheet == mAttrStyleSheet) { // always first
aSet->InsertDocStyleSheetAfter(aSheet, mStyleAttrStyleSheet); mStyleSheets.InsertElementAt(aSheet, 0);
}
else if (aSheet == mStyleAttrStyleSheet) { // always last
mStyleSheets.AppendElement(aSheet);
} }
else { else {
aSet->InsertDocStyleSheetBefore(aSheet, nsnull); // put it in front if (mStyleAttrStyleSheet == mStyleSheets.ElementAt(mStyleSheets.Count() - 1)) {
// keep attr sheet last
mStyleSheets.InsertElementAt(aSheet, mStyleSheets.Count() - 1);
}
else {
mStyleSheets.AppendElement(aSheet);
}
} }
} }
NS_IMETHODIMP
nsHTMLDocument::InsertStyleSheetAt(nsIStyleSheet* aSheet, PRInt32 aIndex, PRBool aNotify)
{
NS_PRECONDITION(nsnull != aSheet, "null ptr");
mStyleSheets.InsertElementAt(aSheet, aIndex + 1); // offset one for the attr style sheet
NS_ADDREF(aSheet);
aSheet->SetOwningDocument(this);
PRBool enabled = PR_TRUE;
aSheet->GetEnabled(enabled);
PRInt32 count;
PRInt32 index;
if (enabled) {
count = mPresShells.Count();
for (index = 0; index < count; index++) {
nsIPresShell* shell = (nsIPresShell*)mPresShells.ElementAt(index);
nsIStyleSet* set = shell->GetStyleSet();
if (nsnull != set) {
set->AddDocStyleSheet(aSheet, this);
NS_RELEASE(set);
}
}
}
if (aNotify) { // notify here even if disabled, there may have been others that weren't notified
count = mObservers.Count();
for (index = 0; index < count; index++) {
nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(index);
observer->StyleSheetAdded(this, aSheet);
}
}
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
nsHTMLDocument::GetBaseURL(nsIURL*& aURL) const nsHTMLDocument::GetBaseURL(nsIURL*& aURL) const
@ -573,6 +617,37 @@ nsHTMLDocument::SetDTDMode(nsDTDMode aMode)
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsHTMLDocument::SetHeaderData(nsIAtom* aHeaderField, const nsString& aData)
{
nsresult result = nsMarkupDocument::SetHeaderData(aHeaderField, aData);
if (NS_SUCCEEDED(result)) {
if (aHeaderField == nsHTMLAtoms::headerDefaultStyle) {
// switch alternate style sheets based on default
nsAutoString type;
nsAutoString title;
nsAutoString textHtml("text/html");
PRInt32 index;
PRInt32 count = mStyleSheets.Count();
for (index = 0; index < count; index++) {
nsIStyleSheet* sheet = (nsIStyleSheet*)mStyleSheets.ElementAt(index);
sheet->GetType(type);
if (PR_FALSE == type.Equals(textHtml)) {
sheet->GetTitle(title);
if (0 < title.Length()) { // if sheet has title
PRBool disabled = ((0 == aData.Length()) ||
(PR_FALSE == aData.EqualsIgnoreCase(title)));
SetStyleSheetDisabledState(sheet, disabled);
}
}
}
}
}
return result;
}
NS_IMETHODIMP NS_IMETHODIMP
nsHTMLDocument::ContentAppended(nsIContent* aContainer, nsHTMLDocument::ContentAppended(nsIContent* aContainer,
PRInt32 aNewIndexInContainer) PRInt32 aNewIndexInContainer)

Просмотреть файл

@ -64,6 +64,8 @@ public:
NS_IMETHOD GetAttributeStyleSheet(nsIHTMLStyleSheet** aStyleSheet); NS_IMETHOD GetAttributeStyleSheet(nsIHTMLStyleSheet** aStyleSheet);
NS_IMETHOD GetInlineStyleSheet(nsIHTMLCSSStyleSheet** aStyleSheet); NS_IMETHOD GetInlineStyleSheet(nsIHTMLCSSStyleSheet** aStyleSheet);
NS_IMETHOD InsertStyleSheetAt(nsIStyleSheet* aSheet, PRInt32 aIndex, PRBool aNotify);
NS_IMETHOD GetBaseURL(nsIURL*& aURL) const; NS_IMETHOD GetBaseURL(nsIURL*& aURL) const;
NS_IMETHOD SetBaseURL(const nsString& aURLSpec); NS_IMETHOD SetBaseURL(const nsString& aURLSpec);
NS_IMETHOD GetBaseTarget(nsString& aTarget) const; NS_IMETHOD GetBaseTarget(nsString& aTarget) const;
@ -72,6 +74,8 @@ public:
NS_IMETHOD GetDTDMode(nsDTDMode& aMode); NS_IMETHOD GetDTDMode(nsDTDMode& aMode);
NS_IMETHOD SetDTDMode(nsDTDMode aMode); NS_IMETHOD SetDTDMode(nsDTDMode aMode);
NS_IMETHOD SetHeaderData(nsIAtom* aHeaderField, const nsString& aData);
NS_IMETHOD ContentAppended(nsIContent* aContainer, NS_IMETHOD ContentAppended(nsIContent* aContainer,
PRInt32 aNewIndexInContainer); PRInt32 aNewIndexInContainer);
NS_IMETHOD ContentInserted(nsIContent* aContainer, NS_IMETHOD ContentInserted(nsIContent* aContainer,
@ -155,7 +159,7 @@ protected:
void DeleteNamedItems(); void DeleteNamedItems();
nsIContent *MatchName(nsIContent *aContent, const nsString& aName); nsIContent *MatchName(nsIContent *aContent, const nsString& aName);
virtual void AddStyleSheetToSet(nsIStyleSheet* aSheet, nsIStyleSet* aSet); virtual void InternalAddStyleSheet(nsIStyleSheet* aSheet);
static PRBool MatchLinks(nsIContent *aContent); static PRBool MatchLinks(nsIContent *aContent);
static PRBool MatchAnchors(nsIContent *aContent); static PRBool MatchAnchors(nsIContent *aContent);

Просмотреть файл

@ -482,16 +482,60 @@ NS_IMETHODIMP nsHTMLDocument::GetInlineStyleSheet(nsIHTMLCSSStyleSheet** aResult
return NS_OK; return NS_OK;
} }
void nsHTMLDocument::AddStyleSheetToSet(nsIStyleSheet* aSheet, nsIStyleSet* aSet) void nsHTMLDocument::InternalAddStyleSheet(nsIStyleSheet* aSheet) // subclass hook for sheet ordering
{ {
if ((nsnull != mStyleAttrStyleSheet) && (aSheet != mStyleAttrStyleSheet)) { if (aSheet == mAttrStyleSheet) { // always first
aSet->InsertDocStyleSheetAfter(aSheet, mStyleAttrStyleSheet); mStyleSheets.InsertElementAt(aSheet, 0);
}
else if (aSheet == mStyleAttrStyleSheet) { // always last
mStyleSheets.AppendElement(aSheet);
} }
else { else {
aSet->InsertDocStyleSheetBefore(aSheet, nsnull); // put it in front if (mStyleAttrStyleSheet == mStyleSheets.ElementAt(mStyleSheets.Count() - 1)) {
// keep attr sheet last
mStyleSheets.InsertElementAt(aSheet, mStyleSheets.Count() - 1);
}
else {
mStyleSheets.AppendElement(aSheet);
}
} }
} }
NS_IMETHODIMP
nsHTMLDocument::InsertStyleSheetAt(nsIStyleSheet* aSheet, PRInt32 aIndex, PRBool aNotify)
{
NS_PRECONDITION(nsnull != aSheet, "null ptr");
mStyleSheets.InsertElementAt(aSheet, aIndex + 1); // offset one for the attr style sheet
NS_ADDREF(aSheet);
aSheet->SetOwningDocument(this);
PRBool enabled = PR_TRUE;
aSheet->GetEnabled(enabled);
PRInt32 count;
PRInt32 index;
if (enabled) {
count = mPresShells.Count();
for (index = 0; index < count; index++) {
nsIPresShell* shell = (nsIPresShell*)mPresShells.ElementAt(index);
nsIStyleSet* set = shell->GetStyleSet();
if (nsnull != set) {
set->AddDocStyleSheet(aSheet, this);
NS_RELEASE(set);
}
}
}
if (aNotify) { // notify here even if disabled, there may have been others that weren't notified
count = mObservers.Count();
for (index = 0; index < count; index++) {
nsIDocumentObserver* observer = (nsIDocumentObserver*)mObservers.ElementAt(index);
observer->StyleSheetAdded(this, aSheet);
}
}
return NS_OK;
}
NS_IMETHODIMP NS_IMETHODIMP
nsHTMLDocument::GetBaseURL(nsIURL*& aURL) const nsHTMLDocument::GetBaseURL(nsIURL*& aURL) const
@ -573,6 +617,37 @@ nsHTMLDocument::SetDTDMode(nsDTDMode aMode)
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP
nsHTMLDocument::SetHeaderData(nsIAtom* aHeaderField, const nsString& aData)
{
nsresult result = nsMarkupDocument::SetHeaderData(aHeaderField, aData);
if (NS_SUCCEEDED(result)) {
if (aHeaderField == nsHTMLAtoms::headerDefaultStyle) {
// switch alternate style sheets based on default
nsAutoString type;
nsAutoString title;
nsAutoString textHtml("text/html");
PRInt32 index;
PRInt32 count = mStyleSheets.Count();
for (index = 0; index < count; index++) {
nsIStyleSheet* sheet = (nsIStyleSheet*)mStyleSheets.ElementAt(index);
sheet->GetType(type);
if (PR_FALSE == type.Equals(textHtml)) {
sheet->GetTitle(title);
if (0 < title.Length()) { // if sheet has title
PRBool disabled = ((0 == aData.Length()) ||
(PR_FALSE == aData.EqualsIgnoreCase(title)));
SetStyleSheetDisabledState(sheet, disabled);
}
}
}
}
}
return result;
}
NS_IMETHODIMP NS_IMETHODIMP
nsHTMLDocument::ContentAppended(nsIContent* aContainer, nsHTMLDocument::ContentAppended(nsIContent* aContainer,
PRInt32 aNewIndexInContainer) PRInt32 aNewIndexInContainer)

Просмотреть файл

@ -64,6 +64,8 @@ public:
NS_IMETHOD GetAttributeStyleSheet(nsIHTMLStyleSheet** aStyleSheet); NS_IMETHOD GetAttributeStyleSheet(nsIHTMLStyleSheet** aStyleSheet);
NS_IMETHOD GetInlineStyleSheet(nsIHTMLCSSStyleSheet** aStyleSheet); NS_IMETHOD GetInlineStyleSheet(nsIHTMLCSSStyleSheet** aStyleSheet);
NS_IMETHOD InsertStyleSheetAt(nsIStyleSheet* aSheet, PRInt32 aIndex, PRBool aNotify);
NS_IMETHOD GetBaseURL(nsIURL*& aURL) const; NS_IMETHOD GetBaseURL(nsIURL*& aURL) const;
NS_IMETHOD SetBaseURL(const nsString& aURLSpec); NS_IMETHOD SetBaseURL(const nsString& aURLSpec);
NS_IMETHOD GetBaseTarget(nsString& aTarget) const; NS_IMETHOD GetBaseTarget(nsString& aTarget) const;
@ -72,6 +74,8 @@ public:
NS_IMETHOD GetDTDMode(nsDTDMode& aMode); NS_IMETHOD GetDTDMode(nsDTDMode& aMode);
NS_IMETHOD SetDTDMode(nsDTDMode aMode); NS_IMETHOD SetDTDMode(nsDTDMode aMode);
NS_IMETHOD SetHeaderData(nsIAtom* aHeaderField, const nsString& aData);
NS_IMETHOD ContentAppended(nsIContent* aContainer, NS_IMETHOD ContentAppended(nsIContent* aContainer,
PRInt32 aNewIndexInContainer); PRInt32 aNewIndexInContainer);
NS_IMETHOD ContentInserted(nsIContent* aContainer, NS_IMETHOD ContentInserted(nsIContent* aContainer,
@ -155,7 +159,7 @@ protected:
void DeleteNamedItems(); void DeleteNamedItems();
nsIContent *MatchName(nsIContent *aContent, const nsString& aName); nsIContent *MatchName(nsIContent *aContent, const nsString& aName);
virtual void AddStyleSheetToSet(nsIStyleSheet* aSheet, nsIStyleSet* aSet); virtual void InternalAddStyleSheet(nsIStyleSheet* aSheet);
static PRBool MatchLinks(nsIContent *aContent); static PRBool MatchLinks(nsIContent *aContent);
static PRBool MatchAnchors(nsIContent *aContent); static PRBool MatchAnchors(nsIContent *aContent);