Bug 703965 - Use View Source styling when viewing plain text source. r=smaug.

This commit is contained in:
Henri Sivonen 2011-11-30 19:44:31 +02:00
parent 960819c184
commit bd1b9e065c
13 changed files with 179 additions and 38 deletions

View File

@ -748,7 +748,11 @@ nsHTMLDocument::StartDocumentLoad(const char* aCommand,
if (loadAsHtml5) {
mParser = nsHtml5Module::NewHtml5Parser();
if (plainText) {
mParser->MarkAsNotScriptCreated("plain-text");
if (viewSource) {
mParser->MarkAsNotScriptCreated("view-source-plain");
} else {
mParser->MarkAsNotScriptCreated("plain-text");
}
} else if (viewSource && !contentType.EqualsLiteral("text/html")) {
mParser->MarkAsNotScriptCreated("view-source-xml");
} else {

View File

@ -76,6 +76,7 @@ EXPORTS = \
nsHtml5UTF16BufferHSupplement.h \
nsHtml5DependentUTF16Buffer.h \
nsHtml5OwningUTF16Buffer.h \
nsHtml5ViewSourceUtils.h \
$(NULL)
CPPSRCS = \
@ -108,6 +109,7 @@ CPPSRCS = \
nsHtml5SpeculativeLoad.cpp \
nsHtml5SVGLoadDispatcher.cpp \
nsHtml5Highlighter.cpp \
nsHtml5ViewSourceUtils.cpp \
$(NULL)
FORCE_STATIC_LIB = 1

View File

@ -41,6 +41,7 @@
#include "nsHtml5AttributeName.h"
#include "nsString.h"
#include "nsThreadUtils.h"
#include "nsHtml5ViewSourceUtils.h"
#include "mozilla/Preferences.h"
using namespace mozilla;
@ -88,8 +89,6 @@ nsHtml5Highlighter::nsHtml5Highlighter(nsAHtml5TreeOpSink* aOpSink)
, mBuffer(nsnull)
, mSyntaxHighlight(Preferences::GetBool("view_source.syntax_highlight",
true))
, mWrapLongLines(Preferences::GetBool("view_source.wrap_long_lines", true))
, mTabSize(Preferences::GetInt("view_source.tab_size", 4))
, mOpSink(aOpSink)
, mCurrentRun(nsnull)
, mAmpersand(nsnull)
@ -128,15 +127,7 @@ nsHtml5Highlighter::Start(const nsAutoString& aTitle)
AppendCharacters(aTitle.get(), 0, (PRInt32)length);
Pop(); // title
nsHtml5HtmlAttributes* linkAttrs = new nsHtml5HtmlAttributes(0);
nsString* rel = new nsString(NS_LITERAL_STRING("stylesheet"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_REL, rel);
nsString* type = new nsString(NS_LITERAL_STRING("text/css"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TYPE, type);
nsString* href = new nsString(
NS_LITERAL_STRING("resource://gre-resources/viewsource.css"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_HREF, href);
Push(nsGkAtoms::link, linkAttrs);
Push(nsGkAtoms::link, nsHtml5ViewSourceUtils::NewLinkAttributes());
mOpQueue.AppendElement()->Init(eTreeOpUpdateStyleSheet, CurrentNode());
@ -144,22 +135,7 @@ nsHtml5Highlighter::Start(const nsAutoString& aTitle)
Pop(); // head
nsHtml5HtmlAttributes* bodyAttrs = new nsHtml5HtmlAttributes(0);
nsString* id = new nsString(NS_LITERAL_STRING("viewsource"));
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_ID, id);
if (mWrapLongLines) {
nsString* klass = new nsString(NS_LITERAL_STRING("wrap"));
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_CLASS, klass);
}
if (mTabSize > 0) {
nsString* style = new nsString(NS_LITERAL_STRING("-moz-tab-size: "));
style->AppendInt(mTabSize);
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_STYLE, style);
}
Push(nsGkAtoms::body, bodyAttrs);
Push(nsGkAtoms::body, nsHtml5ViewSourceUtils::NewBodyAttributes());
nsHtml5HtmlAttributes* preAttrs = new nsHtml5HtmlAttributes(0);
nsString* preId = new nsString(NS_LITERAL_STRING("line1"));

View File

@ -349,16 +349,6 @@ class nsHtml5Highlighter
*/
bool mSyntaxHighlight;
/**
* Whether to wrap long lines.
*/
bool mWrapLongLines;
/**
* The tab size pref.
*/
PRInt32 mTabSize;
/**
* The outgoing tree op queue.
*/

View File

@ -714,6 +714,8 @@ nsHtml5Parser::MarkAsNotScriptCreated(const char* aCommand)
mode = VIEW_SOURCE_HTML;
} else if (!nsCRT::strcmp(aCommand, "view-source-xml")) {
mode = VIEW_SOURCE_XML;
} else if (!nsCRT::strcmp(aCommand, "view-source-plain")) {
mode = VIEW_SOURCE_PLAIN;
} else if (!nsCRT::strcmp(aCommand, "plain-text")) {
mode = PLAIN_TEXT;
} else if (!nsCRT::strcmp(aCommand, kLoadAsData)) {

View File

@ -922,6 +922,9 @@ nsHtml5StreamParser::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
if (mMode == PLAIN_TEXT) {
mTreeBuilder->StartPlainText();
mTokenizer->StartPlainText();
} else if (mMode == VIEW_SOURCE_PLAIN) {
mTreeBuilder->StartPlainTextViewSource(NS_ConvertUTF8toUTF16(mViewSourceTitle));
mTokenizer->StartPlainText();
}
/*

View File

@ -76,6 +76,11 @@ enum eParserMode {
*/
VIEW_SOURCE_XML,
/**
* View document as plain text source
*/
VIEW_SOURCE_PLAIN,
/**
* View document as plain text
*/

View File

@ -52,6 +52,7 @@
#include "nsHtml5StreamParser.h"
#include "nsAHtml5TreeBuilderState.h"
#include "nsHtml5Highlighter.h"
#include "nsHtml5ViewSourceUtils.h"
#include "nsHtml5Tokenizer.h"
#include "nsHtml5MetaScanner.h"

View File

@ -53,6 +53,7 @@
#include "nsHtml5StreamParser.h"
#include "nsAHtml5TreeBuilderState.h"
#include "nsHtml5Highlighter.h"
#include "nsHtml5ViewSourceUtils.h"
class nsHtml5StreamParser;

View File

@ -700,6 +700,32 @@ nsHtml5TreeBuilder::MarkAsBroken()
mOpQueue.AppendElement()->Init(eTreeOpMarkAsBroken);
}
void
nsHtml5TreeBuilder::StartPlainTextViewSource(const nsAutoString& aTitle)
{
startTag(nsHtml5ElementName::ELT_TITLE,
nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES,
false);
// XUL will add the "Source of: " prefix.
PRUint32 length = aTitle.Length();
if (length > PR_INT32_MAX) {
length = PR_INT32_MAX;
}
characters(aTitle.get(), 0, (PRInt32)length);
endTag(nsHtml5ElementName::ELT_TITLE);
startTag(nsHtml5ElementName::ELT_LINK,
nsHtml5ViewSourceUtils::NewLinkAttributes(),
false);
startTag(nsHtml5ElementName::ELT_BODY,
nsHtml5ViewSourceUtils::NewBodyAttributes(),
false);
StartPlainText();
}
void
nsHtml5TreeBuilder::StartPlainText()
{

View File

@ -95,6 +95,8 @@
~nsHtml5TreeBuilder();
void StartPlainTextViewSource(const nsAutoString& aTitle);
void StartPlainText();
bool HasScript();

View File

@ -0,0 +1,79 @@
/* ***** 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 HTML5 View Source code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsHtml5ViewSourceUtils.h"
#include "nsHtml5AttributeName.h"
#include "mozilla/Preferences.h"
// static
nsHtml5HtmlAttributes*
nsHtml5ViewSourceUtils::NewBodyAttributes()
{
nsHtml5HtmlAttributes* bodyAttrs = new nsHtml5HtmlAttributes(0);
nsString* id = new nsString(NS_LITERAL_STRING("viewsource"));
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_ID, id);
if (mozilla::Preferences::GetBool("view_source.wrap_long_lines", true)) {
nsString* klass = new nsString(NS_LITERAL_STRING("wrap"));
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_CLASS, klass);
}
PRInt32 tabSize = mozilla::Preferences::GetInt("view_source.tab_size", 4);
if (tabSize > 0) {
nsString* style = new nsString(NS_LITERAL_STRING("-moz-tab-size: "));
style->AppendInt(tabSize);
bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_STYLE, style);
}
return bodyAttrs;
}
// static
nsHtml5HtmlAttributes*
nsHtml5ViewSourceUtils::NewLinkAttributes()
{
nsHtml5HtmlAttributes* linkAttrs = new nsHtml5HtmlAttributes(0);
nsString* rel = new nsString(NS_LITERAL_STRING("stylesheet"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_REL, rel);
nsString* type = new nsString(NS_LITERAL_STRING("text/css"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TYPE, type);
nsString* href = new nsString(
NS_LITERAL_STRING("resource://gre-resources/viewsource.css"));
linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_HREF, href);
return linkAttrs;
}

View File

@ -0,0 +1,50 @@
/* ***** 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 HTML5 View Source code.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Henri Sivonen <hsivonen@iki.fi>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsHtml5ViewSourceUtils_h_
#define nsHtml5ViewSourceUtils_h_
#include "nsHtml5HtmlAttributes.h"
class nsHtml5ViewSourceUtils
{
public:
static nsHtml5HtmlAttributes* NewBodyAttributes();
static nsHtml5HtmlAttributes* NewLinkAttributes();
};
#endif // nsHtml5ViewSourceUtils_h_