Bug 1392996 - Part 1 - Add meta viewport tag to view-source document. r=hsivonen
authorJan Henning <jh+bugzilla@buttercookie.de>
Tue, 23 Jan 2018 22:25:09 +0100
changeset 454861 fce65dcf661b404ff5b10a775693b6cea9f104d9
parent 454860 05ebdfb27cdccc634d4dde43aece53b2c8b2f199
child 454862 11e5e7079111ac64ec570c98f2a3ec058b320f04
push id8799
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 16:46:23 +0000
treeherdermozilla-beta@15334014dc67 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershsivonen
bugs1392996
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1392996 - Part 1 - Add meta viewport tag to view-source document. r=hsivonen Adding <meta name="viewport" content="width=device-width"/> to the view-source document achieves two things when used in a mobile browser, such as Fennec: 1. When word-wrapping is turned off, the page displays at a more readable initial zoom level. 2. As of now, font inflation (when enabled) kicks in on the document when word- wrapping is turned on, which leads to the line numbers appearing in a noticeably smaller font size than the rest of the page. Adding the above meta viewport header marks the document as "mobile-friendly" and suppresses font inflation, which means that line numbers will appear normally even with word-wrapping enabled. getMathMLSelection() in browser-content.js isn't actually used in Fennec at the moment, but for consistency we add the meta viewport tag there as well. MozReview-Commit-ID: K9KVHh7g7TF
parser/html/nsHtml5Highlighter.cpp
parser/html/nsHtml5TreeBuilderCppSupplement.h
parser/html/nsHtml5ViewSourceUtils.cpp
parser/html/nsHtml5ViewSourceUtils.h
toolkit/content/browser-content.js
--- a/parser/html/nsHtml5Highlighter.cpp
+++ b/parser/html/nsHtml5Highlighter.cpp
@@ -84,16 +84,21 @@ nsHtml5Highlighter::Start(const nsAutoSt
   nsIContent** root =
     CreateElement(nsGkAtoms::html, nullptr, nullptr, NS_NewHTMLSharedElement);
   mOpQueue.AppendElement()->Init(eTreeOpAppendToDocument, root);
   mStack.AppendElement(root);
 
   // <head> uses NS_NewHTMLSharedElement creator
   Push(nsGkAtoms::head, nullptr, NS_NewHTMLSharedElement);
 
+  Push(nsGkAtoms::meta,
+       nsHtml5ViewSourceUtils::NewMetaViewportAttributes(),
+       NS_NewHTMLMetaElement);
+  Pop(); // meta
+
   Push(nsGkAtoms::title, nullptr, NS_NewHTMLTitleElement);
   // XUL will add the "Source of: " prefix.
   uint32_t length = aTitle.Length();
   if (length > INT32_MAX) {
     length = INT32_MAX;
   }
   AppendCharacters(aTitle.BeginReading(), 0, (int32_t)length);
   Pop(); // title
--- a/parser/html/nsHtml5TreeBuilderCppSupplement.h
+++ b/parser/html/nsHtml5TreeBuilderCppSupplement.h
@@ -1310,16 +1310,21 @@ nsHtml5TreeBuilder::MarkAsBrokenFromPort
   mBroken = aRv;
   requestSuspension();
 }
 
 void
 nsHtml5TreeBuilder::StartPlainTextViewSource(const nsAutoString& aTitle)
 {
   MOZ_ASSERT(!mBuilder, "Must not view source with builder.");
+
+  startTag(nsHtml5ElementName::ELT_META,
+           nsHtml5ViewSourceUtils::NewMetaViewportAttributes(),
+           false);
+
   startTag(nsHtml5ElementName::ELT_TITLE,
            nsHtml5HtmlAttributes::EMPTY_ATTRIBUTES,
            false);
 
   // XUL will add the "Source of: " prefix.
   uint32_t length = aTitle.Length();
   if (length > INT32_MAX) {
     length = INT32_MAX;
--- a/parser/html/nsHtml5ViewSourceUtils.cpp
+++ b/parser/html/nsHtml5ViewSourceUtils.cpp
@@ -49,8 +49,20 @@ nsHtml5ViewSourceUtils::NewLinkAttribute
   linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_REL, rel, -1);
   nsHtml5String type = nsHtml5Portability::newStringFromLiteral("text/css");
   linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_TYPE, type, -1);
   nsHtml5String href = nsHtml5Portability::newStringFromLiteral(
     "resource://content-accessible/viewsource.css");
   linkAttrs->addAttribute(nsHtml5AttributeName::ATTR_HREF, href, -1);
   return linkAttrs;
 }
+
+// static
+nsHtml5HtmlAttributes*
+nsHtml5ViewSourceUtils::NewMetaViewportAttributes()
+{
+  nsHtml5HtmlAttributes* metaVpAttrs = new nsHtml5HtmlAttributes(0);
+  nsHtml5String name = nsHtml5Portability::newStringFromLiteral("viewport");
+  metaVpAttrs->addAttribute(nsHtml5AttributeName::ATTR_NAME, name, -1);
+  nsHtml5String content = nsHtml5Portability::newStringFromLiteral("width=device-width");
+  metaVpAttrs->addAttribute(nsHtml5AttributeName::ATTR_CONTENT, content, -1);
+  return metaVpAttrs;
+}
--- a/parser/html/nsHtml5ViewSourceUtils.h
+++ b/parser/html/nsHtml5ViewSourceUtils.h
@@ -7,11 +7,12 @@
 
 #include "nsHtml5HtmlAttributes.h"
 
 class nsHtml5ViewSourceUtils
 {
   public:
     static nsHtml5HtmlAttributes* NewBodyAttributes();
     static nsHtml5HtmlAttributes* NewLinkAttributes();
+    static nsHtml5HtmlAttributes* NewMetaViewportAttributes();
 };
 
 #endif // nsHtml5ViewSourceUtils_h
--- a/toolkit/content/browser-content.js
+++ b/toolkit/content/browser-content.js
@@ -1397,17 +1397,18 @@ var ViewSelectionSource = {
     const BUNDLE_URL = "chrome://global/locale/viewSource.properties";
 
     let bundle = Services.strings.createBundle(BUNDLE_URL);
     var title = bundle.GetStringFromName("viewMathMLSourceTitle");
     var wrapClass = this.wrapLongLines ? ' class="wrap"' : "";
     var source =
       "<!DOCTYPE html>"
     + "<html>"
-    + "<head><title>" + title + "</title>"
+    + '<head><meta name="viewport" content="width=device-width"/>'
+    + "<title>" + title + "</title>"
     + '<link rel="stylesheet" type="text/css" href="' + VIEW_SOURCE_CSS + '">'
     + '<style type="text/css">'
     + "#target { border: dashed 1px; background-color: lightyellow; }"
     + "</style>"
     + "</head>"
     + '<body id="viewsource"' + wrapClass
     + ' onload="document.title=\'' + title + '\'; document.getElementById(\'target\').scrollIntoView(true)">'
     + "<pre>"