From d1623e977ad50caad0f991e6801757a4b175d346 Mon Sep 17 00:00:00 2001 From: Jared Wein Date: Wed, 6 Mar 2013 23:45:08 -0500 Subject: [PATCH] Bug 253564 - Plain text (text/plain, text/javascript, text/css, etc) documents should word-wrap. r=bz --- browser/app/profile/firefox.js | 3 ++ layout/style/jar.mn | 1 + layout/style/plaintext.css | 8 ++++ modules/libpref/src/init/all.js | 3 ++ parser/html/Makefile.in | 2 + parser/html/nsHtml5PlainTextUtils.cpp | 39 +++++++++++++++++++ parser/html/nsHtml5PlainTextUtils.h | 16 ++++++++ parser/html/nsHtml5TreeBuilder.h | 1 + parser/html/nsHtml5TreeBuilderCppSupplement.h | 15 ++++++- parser/html/nsHtml5TreeBuilderHSupplement.h | 2 + .../en-US/chrome/global/browser.properties | 2 + 11 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 layout/style/plaintext.css create mode 100644 parser/html/nsHtml5PlainTextUtils.cpp create mode 100644 parser/html/nsHtml5PlainTextUtils.h diff --git a/browser/app/profile/firefox.js b/browser/app/profile/firefox.js index ae59439638f3..523452cdb94f 100644 --- a/browser/app/profile/firefox.js +++ b/browser/app/profile/firefox.js @@ -1219,3 +1219,6 @@ pref("social.sidebar.unload_timeout_ms", 10000); pref("social.toast-notifications.enabled", true); pref("dom.identity.enabled", false); + +// Override the Gecko-default value of false for Firefox. +pref("plain_text.wrap_long_lines", true); diff --git a/layout/style/jar.mn b/layout/style/jar.mn index 88471575e7f6..1aa17b899824 100644 --- a/layout/style/jar.mn +++ b/layout/style/jar.mn @@ -7,6 +7,7 @@ toolkit.jar: res/html.css (html.css) res/quirk.css (quirk.css) res/full-screen-override.css (full-screen-override.css) + res/plaintext.css (plaintext.css) res/viewsource.css (viewsource.css) * res/forms.css (forms.css) res/arrow.gif (arrow.gif) diff --git a/layout/style/plaintext.css b/layout/style/plaintext.css new file mode 100644 index 000000000000..ca8839ad84bc --- /dev/null +++ b/layout/style/plaintext.css @@ -0,0 +1,8 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +pre { + white-space: pre-wrap; + word-wrap: break-word; +} diff --git a/modules/libpref/src/init/all.js b/modules/libpref/src/init/all.js index 836d1d90caf8..a1b3e41bb250 100644 --- a/modules/libpref/src/init/all.js +++ b/modules/libpref/src/init/all.js @@ -407,6 +407,9 @@ pref("view_source.editor.path", ""); // for jumping to a specific line (e.g. "/line:%LINE%" or "--goto %LINE%") pref("view_source.editor.args", ""); +// When true this will word-wrap plain text documents. +pref("plain_text.wrap_long_lines", false); + // dispatch left clicks only to content in browser (still allows clicks to chrome/xul) pref("nglayout.events.dispatchLeftClickOnly", true); diff --git a/parser/html/Makefile.in b/parser/html/Makefile.in index ad5800af3fa0..cbd3ec132dbd 100644 --- a/parser/html/Makefile.in +++ b/parser/html/Makefile.in @@ -48,6 +48,7 @@ EXPORTS = \ nsHtml5UTF16BufferHSupplement.h \ nsHtml5DependentUTF16Buffer.h \ nsHtml5OwningUTF16Buffer.h \ + nsHtml5PlainTextUtils.h \ nsHtml5ViewSourceUtils.h \ nsHtml5StringParser.h \ nsParserUtils.h \ @@ -83,6 +84,7 @@ CPPSRCS = \ nsHtml5SpeculativeLoad.cpp \ nsHtml5SVGLoadDispatcher.cpp \ nsHtml5Highlighter.cpp \ + nsHtml5PlainTextUtils.cpp \ nsHtml5ViewSourceUtils.cpp \ nsHtml5StringParser.cpp \ nsParserUtils.cpp \ diff --git a/parser/html/nsHtml5PlainTextUtils.cpp b/parser/html/nsHtml5PlainTextUtils.cpp new file mode 100644 index 000000000000..aac833bb870d --- /dev/null +++ b/parser/html/nsHtml5PlainTextUtils.cpp @@ -0,0 +1,39 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + + +#include "nsHtml5PlainTextUtils.h" +#include "nsHtml5AttributeName.h" +#include "nsIServiceManager.h" +#include "nsIStringBundle.h" + +// static +nsHtml5HtmlAttributes* +nsHtml5PlainTextUtils::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/plaintext.css")); + linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_HREF, href); + + nsresult rv; + nsCOMPtr bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); + NS_ASSERTION(NS_SUCCEEDED(rv) && bundleService, "The bundle service could not be loaded"); + nsCOMPtr bundle; + rv = bundleService->CreateBundle("chrome://global/locale/browser.properties", + getter_AddRefs(bundle)); + NS_ASSERTION(NS_SUCCEEDED(rv) && bundle, "chrome://global/locale/browser.properties could not be loaded"); + nsXPIDLString title; + if (bundle) { + bundle->GetStringFromName(NS_LITERAL_STRING("plainText.wordWrap").get(), getter_Copies(title)); + } + + nsString* titleCopy = new nsString(title); + linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TITLE, titleCopy); + return linkAttrs; +} diff --git a/parser/html/nsHtml5PlainTextUtils.h b/parser/html/nsHtml5PlainTextUtils.h new file mode 100644 index 000000000000..214c5b16834e --- /dev/null +++ b/parser/html/nsHtml5PlainTextUtils.h @@ -0,0 +1,16 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsHtml5PlainTextUtils_h_ +#define nsHtml5PlainTextUtils_h_ + +#include "nsHtml5HtmlAttributes.h" + +class nsHtml5PlainTextUtils +{ + public: + static nsHtml5HtmlAttributes* NewLinkAttributes(); +}; + +#endif // nsHtml5PlainTextUtils_h_ diff --git a/parser/html/nsHtml5TreeBuilder.h b/parser/html/nsHtml5TreeBuilder.h index 53bf89a8a3e2..df44dcb4dc9b 100644 --- a/parser/html/nsHtml5TreeBuilder.h +++ b/parser/html/nsHtml5TreeBuilder.h @@ -51,6 +51,7 @@ #include "nsHtml5StreamParser.h" #include "nsAHtml5TreeBuilderState.h" #include "nsHtml5Highlighter.h" +#include "nsHtml5PlainTextUtils.h" #include "nsHtml5ViewSourceUtils.h" #include "mozilla/Likely.h" diff --git a/parser/html/nsHtml5TreeBuilderCppSupplement.h b/parser/html/nsHtml5TreeBuilderCppSupplement.h index dbe53fe29b10..cbcb32823c79 100644 --- a/parser/html/nsHtml5TreeBuilderCppSupplement.h +++ b/parser/html/nsHtml5TreeBuilderCppSupplement.h @@ -12,6 +12,7 @@ #include "nsNodeUtils.h" #include "nsIFrame.h" #include "mozilla/Likely.h" +#include "mozilla/Preferences.h" class nsPresContext; @@ -722,11 +723,23 @@ nsHtml5TreeBuilder::StartPlainTextViewSource(const nsAutoString& aTitle) nsHtml5ViewSourceUtils::NewBodyAttributes(), false); - StartPlainText(); + StartPlainTextBody(); } void nsHtml5TreeBuilder::StartPlainText() +{ + if (mozilla::Preferences::GetBool("plain_text.wrap_long_lines", true)) { + startTag(nsHtml5ElementName::ELT_LINK, + nsHtml5PlainTextUtils::NewLinkAttributes(), + false); + } + + StartPlainTextBody(); +} + +void +nsHtml5TreeBuilder::StartPlainTextBody() { startTag(nsHtml5ElementName::ELT_PRE, nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES, diff --git a/parser/html/nsHtml5TreeBuilderHSupplement.h b/parser/html/nsHtml5TreeBuilderHSupplement.h index 4e20a42e1d57..e348f85a62b0 100644 --- a/parser/html/nsHtml5TreeBuilderHSupplement.h +++ b/parser/html/nsHtml5TreeBuilderHSupplement.h @@ -67,6 +67,8 @@ void StartPlainText(); + void StartPlainTextBody(); + bool HasScript(); void SetOpSink(nsAHtml5TreeOpSink* aOpSink) { diff --git a/toolkit/locales/en-US/chrome/global/browser.properties b/toolkit/locales/en-US/chrome/global/browser.properties index d05d839c2751..0e5409fd1cc0 100644 --- a/toolkit/locales/en-US/chrome/global/browser.properties +++ b/toolkit/locales/en-US/chrome/global/browser.properties @@ -6,3 +6,5 @@ browsewithcaret.checkMsg=Do not show me this dialog box again. browsewithcaret.checkWindowTitle=Caret Browsing browsewithcaret.checkLabel=Pressing F7 turns Caret Browsing on or off. This feature places a moveable cursor in web pages, allowing you to select text with the keyboard. Do you want to turn Caret Browsing on? browsewithcaret.checkButtonLabel=Yes + +plainText.wordWrap=Wrap Long Lines