Bug 1392996 - Part 1 - Add meta viewport tag to view-source document. r?smaug draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Tue, 23 Jan 2018 22:25:09 +0100
changeset 748111 a576f806f01176c12d5afaebf2076e649b5bd47d
parent 748059 a2f377b2396c530f898f853ca1ec62b83069b01d
child 748112 b519ced8c8b20d8c1b00b0d81e03f4258e83302a
push id97074
push usermozilla@buttercookie.de
push dateSun, 28 Jan 2018 15:21:37 +0000
reviewerssmaug
bugs1392996
milestone60.0a1
Bug 1392996 - Part 1 - Add meta viewport tag to view-source document. r?smaug 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>"