From 9bda12367ba2ab014c1ea1a7cef257cc04d3e5a1 Mon Sep 17 00:00:00 2001 From: Jared Wein Date: Thu, 10 Nov 2011 15:08:07 -0800 Subject: [PATCH] Bug 700854 - {Media,Image,Video}Document should reference an external stylesheet for use in styling their content. r=roc --- browser/installer/package-manifest.in | 2 + content/html/document/src/ImageDocument.cpp | 27 ++++++------ content/html/document/src/MediaDocument.cpp | 48 +++++++++++++-------- content/html/document/src/MediaDocument.h | 2 + content/html/document/src/VideoDocument.cpp | 7 ++- layout/style/Makefile.in | 2 + layout/style/TopLevelImageDocument.css | 38 ++++++++++++++++ layout/style/TopLevelVideoDocument.css | 38 ++++++++++++++++ 8 files changed, 128 insertions(+), 36 deletions(-) create mode 100644 layout/style/TopLevelImageDocument.css create mode 100644 layout/style/TopLevelVideoDocument.css diff --git a/browser/installer/package-manifest.in b/browser/installer/package-manifest.in index 60bfd61fc88b..11334bfaaa9d 100644 --- a/browser/installer/package-manifest.in +++ b/browser/installer/package-manifest.in @@ -444,6 +444,8 @@ @BINPATH@/res/EditorOverride.css @BINPATH@/res/contenteditable.css @BINPATH@/res/designmode.css +@BINPATH@/res/TopLevelImageDocument.css +@BINPATH@/res/TopLevelVideoDocument.css @BINPATH@/res/table-add-column-after-active.gif @BINPATH@/res/table-add-column-after-hover.gif @BINPATH@/res/table-add-column-after.gif diff --git a/content/html/document/src/ImageDocument.cpp b/content/html/document/src/ImageDocument.cpp index ea6307bbaf71..d75e151c2f94 100644 --- a/content/html/document/src/ImageDocument.cpp +++ b/content/html/document/src/ImageDocument.cpp @@ -652,23 +652,22 @@ ImageDocument::CreateSyntheticDocument() // the size of the paper and cannot break into continuations along // multiple pages. Element* head = GetHeadElement(); - if (!head) { - NS_WARNING("no head on image document!"); - return NS_ERROR_FAILURE; - } + NS_ENSURE_TRUE(head, NS_ERROR_FAILURE); nsCOMPtr nodeInfo; - nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::style, nsnull, - kNameSpaceID_XHTML, - nsIDOMNode::ELEMENT_NODE); - NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY); - nsRefPtr styleContent = NS_NewHTMLStyleElement(nodeInfo.forget()); - if (!styleContent) { - return NS_ERROR_OUT_OF_MEMORY; - } + if (nsContentUtils::IsChildOfSameType(this)) { + nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::style, nsnull, + kNameSpaceID_XHTML, + nsIDOMNode::ELEMENT_NODE); + NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY); + nsRefPtr styleContent = NS_NewHTMLStyleElement(nodeInfo.forget()); + NS_ENSURE_TRUE(styleContent, NS_ERROR_OUT_OF_MEMORY); - styleContent->SetTextContent(NS_LITERAL_STRING("img { display: block; }")); - head->AppendChildTo(styleContent, false); + styleContent->SetTextContent(NS_LITERAL_STRING("img { display: block; }")); + head->AppendChildTo(styleContent, false); + } else { + LinkStylesheet(NS_LITERAL_STRING("resource://gre/res/TopLevelImageDocument.css")); + } // Add the image element Element* body = GetBodyElement(); diff --git a/content/html/document/src/MediaDocument.cpp b/content/html/document/src/MediaDocument.cpp index 0ac78cc9780a..bbd94f0bce33 100644 --- a/content/html/document/src/MediaDocument.cpp +++ b/content/html/document/src/MediaDocument.cpp @@ -243,9 +243,7 @@ MediaDocument::CreateSyntheticDocument() NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY); nsRefPtr root = NS_NewHTMLHtmlElement(nodeInfo.forget()); - if (!root) { - return NS_ERROR_OUT_OF_MEMORY; - } + NS_ENSURE_TRUE(root, NS_ERROR_OUT_OF_MEMORY); NS_ASSERTION(GetChildCount() == 0, "Shouldn't have any kids"); rv = AppendChildTo(root, false); @@ -258,20 +256,15 @@ MediaDocument::CreateSyntheticDocument() // Create a so our title has somewhere to live nsRefPtr head = NS_NewHTMLHeadElement(nodeInfo.forget()); - if (!head) { - return NS_ERROR_OUT_OF_MEMORY; - } + NS_ENSURE_TRUE(head, NS_ERROR_OUT_OF_MEMORY); - nsCOMPtr nodeInfoMeta; - nodeInfoMeta = mNodeInfoManager->GetNodeInfo(nsGkAtoms::meta, nsnull, - kNameSpaceID_XHTML, - nsIDOMNode::ELEMENT_NODE); - NS_ENSURE_TRUE(nodeInfoMeta, NS_ERROR_OUT_OF_MEMORY); + nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::meta, nsnull, + kNameSpaceID_XHTML, + nsIDOMNode::ELEMENT_NODE); + NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY); - nsRefPtr metaContent = NS_NewHTMLMetaElement(nodeInfoMeta.forget()); - if (!metaContent) { - return NS_ERROR_OUT_OF_MEMORY; - } + nsRefPtr metaContent = NS_NewHTMLMetaElement(nodeInfo.forget()); + NS_ENSURE_TRUE(metaContent, NS_ERROR_OUT_OF_MEMORY); metaContent->SetAttr(kNameSpaceID_None, nsGkAtoms::name, NS_LITERAL_STRING("viewport"), true); @@ -289,9 +282,7 @@ MediaDocument::CreateSyntheticDocument() NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY); nsRefPtr body = NS_NewHTMLBodyElement(nodeInfo.forget()); - if (!body) { - return NS_ERROR_OUT_OF_MEMORY; - } + NS_ENSURE_TRUE(body, NS_ERROR_OUT_OF_MEMORY); root->AppendChildTo(body, false); @@ -354,6 +345,27 @@ MediaDocument::GetFileName(nsAString& aResult) } } +nsresult +MediaDocument::LinkStylesheet(const nsAString& aStylesheet) +{ + nsCOMPtr nodeInfo; + nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::link, nsnull, + kNameSpaceID_XHTML, + nsIDOMNode::ELEMENT_NODE); + NS_ENSURE_TRUE(nodeInfo, NS_ERROR_OUT_OF_MEMORY); + + nsRefPtr link = NS_NewHTMLLinkElement(nodeInfo.forget()); + NS_ENSURE_TRUE(link, NS_ERROR_OUT_OF_MEMORY); + + link->SetAttr(kNameSpaceID_None, nsGkAtoms::rel, + NS_LITERAL_STRING("stylesheet"), true); + + link->SetAttr(kNameSpaceID_None, nsGkAtoms::href, aStylesheet, true); + + Element* head = GetHeadElement(); + return head->AppendChildTo(link, false); +} + void MediaDocument::UpdateTitleAndCharset(const nsACString& aTypeStr, const char* const* aFormatNames, diff --git a/content/html/document/src/MediaDocument.h b/content/html/document/src/MediaDocument.h index ccedd9feff6f..fee8cd9fe401 100644 --- a/content/html/document/src/MediaDocument.h +++ b/content/html/document/src/MediaDocument.h @@ -72,6 +72,8 @@ protected: void GetFileName(nsAString& aResult); + nsresult LinkStylesheet(const nsAString& aStylesheet); + // |aFormatNames[]| needs to have four elements in the following order: // a format name with neither dimension nor file, a format name with // filename but w/o dimension, a format name with dimension but w/o filename, diff --git a/content/html/document/src/VideoDocument.cpp b/content/html/document/src/VideoDocument.cpp index 9854e7d0a95f..8a480d74c44b 100644 --- a/content/html/document/src/VideoDocument.cpp +++ b/content/html/document/src/VideoDocument.cpp @@ -135,10 +135,9 @@ VideoDocument::CreateSyntheticVideoDocument(nsIChannel* aChannel, true); } else { Element* head = GetHeadElement(); - if (!head) { - NS_WARNING("no head on video document!"); - return NS_ERROR_FAILURE; - } + NS_ENSURE_TRUE(head, NS_ERROR_FAILURE); + + LinkStylesheet(NS_LITERAL_STRING("resource://gre/res/TopLevelVideoDocument.css")); nodeInfo = mNodeInfoManager->GetNodeInfo(nsGkAtoms::style, nsnull, kNameSpaceID_XHTML, diff --git a/layout/style/Makefile.in b/layout/style/Makefile.in index 7f3f6f08c508..4e8a9c2cb042 100644 --- a/layout/style/Makefile.in +++ b/layout/style/Makefile.in @@ -167,6 +167,8 @@ LOCAL_INCLUDES = \ _FILES = \ contenteditable.css \ designmode.css \ + TopLevelImageDocument.css \ + TopLevelVideoDocument.css \ $(NULL) GARBAGE += $(addprefix $(DIST)/bin/res/,$(_FILES)) diff --git a/layout/style/TopLevelImageDocument.css b/layout/style/TopLevelImageDocument.css new file mode 100644 index 000000000000..54151c22e8c5 --- /dev/null +++ b/layout/style/TopLevelImageDocument.css @@ -0,0 +1,38 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla.org code. + * + * Contributor(s): + * Carlo Alberto Ferraris + * Jared Wein + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/* + This CSS stylesheet defines the rules to be applied to ImageDocuments that + are top level (e.g. not iframes). +*/ diff --git a/layout/style/TopLevelVideoDocument.css b/layout/style/TopLevelVideoDocument.css new file mode 100644 index 000000000000..ebf0ba3310c1 --- /dev/null +++ b/layout/style/TopLevelVideoDocument.css @@ -0,0 +1,38 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Mozilla.org code. + * + * Contributor(s): + * Carlo Alberto Ferraris + * Jared Wein + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/* + This CSS stylesheet defines the rules to be applied to VideoDocuments that + are top level (e.g. not iframes). +*/