Bug 1067325 - Control syntax highlighting via a class. r=hsivonen
authorJ. Ryan Stinnett <jryans@gmail.com>
Mon, 18 May 2015 11:10:41 -0500
changeset 265170 7feb1cfb0182b53725d57fc8eb3780dde251f3cb
parent 265169 9581ecb223e721eb01729f2e74264595b11dcc07
child 265171 4f686a4f83ddd276e7a5ebd391a7b120171769aa
push id2101
push userjryans@gmail.com
push dateMon, 18 May 2015 16:11:00 +0000
reviewershsivonen
bugs1067325
milestone41.0a1
Bug 1067325 - Control syntax highlighting via a class. r=hsivonen
layout/style/viewsource.css
parser/html/nsHtml5Highlighter.cpp
parser/html/nsHtml5Highlighter.h
parser/html/nsHtml5ViewSourceUtils.cpp
--- a/layout/style/viewsource.css
+++ b/layout/style/viewsource.css
@@ -35,62 +35,62 @@ span[id]:before {
   display: inline-block;
   width: 5ch;
   margin: 0 0 0 -5ch;
   text-align: right;
   color: #ccc;
   font-weight: normal;
   font-style: normal;
 }
-.start-tag {
+.highlight .start-tag {
  color: purple;
  font-weight: bold;
 }
-.end-tag {
+.highlight .end-tag {
  color: purple;
  font-weight: bold;
 }
-.comment {
+.highlight .comment {
  color: green;
  font-style: italic;
 }
-.cdata {
+.highlight .cdata {
  color: #CC0066;
 }
-.doctype {
+.highlight .doctype {
  color: steelblue;
  font-style: italic;
 }
-.pi {
+.highlight .pi {
  color: orchid;
  font-style: italic;
 }
-.entity {
- color:#FF4500;
+.highlight .entity {
+ color: #FF4500;
  font-weight: normal;
 }
-.text {
+.highlight .text {
   font-weight: normal;
 }
-.attribute-name {
+.highlight .attribute-name {
  color: black;
  font-weight: bold;
 }
-.attribute-value {
+.highlight .attribute-value {
  color: blue;
  font-weight: normal;
 }
-.markupdeclaration {
+.highlight .markupdeclaration {
  color: steelblue;
  font-style: italic;
 }
 span:not(.error), a:not(.error) {
  unicode-bidi: embed;
 }
 span[id] {
  unicode-bidi: -moz-isolate;
 }
-.error, 
-.error > :-moz-any(.start-tag, .end-tag, .comment, .cdata, .doctype, .pi,
-                   .entity, .attribute-name, .attribute-value) {
+.highlight .error,
+.highlight .error > :-moz-any(.start-tag, .end-tag, .comment, .cdata, .doctype,
+                              .pi, .entity, .attribute-name, .attribute-value) {
   color: red;
   font-weight: bold;
 }
--- a/parser/html/nsHtml5Highlighter.cpp
+++ b/parser/html/nsHtml5Highlighter.cpp
@@ -48,18 +48,16 @@ char16_t nsHtml5Highlighter::sPi[] =
 nsHtml5Highlighter::nsHtml5Highlighter(nsAHtml5TreeOpSink* aOpSink)
  : mState(NS_HTML5TOKENIZER_DATA)
  , mCStart(INT32_MAX)
  , mPos(0)
  , mLineNumber(1)
  , mInlinesOpen(0)
  , mInCharacters(false)
  , mBuffer(nullptr)
- , mSyntaxHighlight(Preferences::GetBool("view_source.syntax_highlight",
-                                         true))
  , mOpSink(aOpSink)
  , mCurrentRun(nullptr)
  , mAmpersand(nullptr)
  , mSlash(nullptr)
  , mHandles(new nsIContent*[NS_HTML5_HIGHLIGHTER_HANDLE_ARRAY_LENGTH])
  , mHandlesUsed(0)
  , mSeenBase(false)
 {
@@ -708,19 +706,16 @@ nsHtml5Highlighter::AppendCharacters(con
                                  aLength,
                                  CurrentNode());
 }
 
 
 void
 nsHtml5Highlighter::AddClass(const char16_t* aClass)
 {
-  if (!mSyntaxHighlight) {
-    return;
-  }
   mOpQueue.AppendElement()->InitAddClass(CurrentNode(), aClass);
 }
 
 void
 nsHtml5Highlighter::AddViewSourceHref(const nsString& aValue)
 {
   char16_t* bufferCopy = new char16_t[aValue.Length() + 1];
   memcpy(bufferCopy, aValue.get(), aValue.Length() * sizeof(char16_t));
@@ -746,78 +741,60 @@ nsHtml5Highlighter::AddBase(const nsStri
   mOpQueue.AppendElement()->Init(eTreeOpAddViewSourceBase,
                                  bufferCopy,
                                  aValue.Length());
 }
 
 void
 nsHtml5Highlighter::AddErrorToCurrentNode(const char* aMsgId)
 {
-  if (!mSyntaxHighlight) {
-    return;
-  }
   nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement();
   NS_ASSERTION(treeOp, "Tree op allocation failed.");
   treeOp->Init(CurrentNode(), aMsgId);
 }
 
 void
 nsHtml5Highlighter::AddErrorToCurrentRun(const char* aMsgId)
 {
-  if (!mSyntaxHighlight) {
-    return;
-  }
   NS_PRECONDITION(mCurrentRun, "Adding error to run without one!");
   nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement();
   NS_ASSERTION(treeOp, "Tree op allocation failed.");
   treeOp->Init(mCurrentRun, aMsgId);
 }
 
 void
 nsHtml5Highlighter::AddErrorToCurrentRun(const char* aMsgId,
                                          nsIAtom* aName)
 {
-  if (!mSyntaxHighlight) {
-    return;
-  }
   NS_PRECONDITION(mCurrentRun, "Adding error to run without one!");
   nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement();
   NS_ASSERTION(treeOp, "Tree op allocation failed.");
   treeOp->Init(mCurrentRun, aMsgId, aName);
 }
 
 void
 nsHtml5Highlighter::AddErrorToCurrentRun(const char* aMsgId,
                                          nsIAtom* aName,
                                          nsIAtom* aOther)
 {
-  if (!mSyntaxHighlight) {
-    return;
-  }
   NS_PRECONDITION(mCurrentRun, "Adding error to run without one!");
   nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement();
   NS_ASSERTION(treeOp, "Tree op allocation failed.");
   treeOp->Init(mCurrentRun, aMsgId, aName, aOther);
 }
 
 void
 nsHtml5Highlighter::AddErrorToCurrentAmpersand(const char* aMsgId)
 {
-  if (!mSyntaxHighlight) {
-    return;
-  }
   NS_PRECONDITION(mAmpersand, "Adding error to ampersand without one!");
   nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement();
   NS_ASSERTION(treeOp, "Tree op allocation failed.");
   treeOp->Init(mAmpersand, aMsgId);
 }
 
 void
 nsHtml5Highlighter::AddErrorToCurrentSlash(const char* aMsgId)
 {
-  if (!mSyntaxHighlight) {
-    return;
-  }
   NS_PRECONDITION(mSlash, "Adding error to slash without one!");
   nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement();
   NS_ASSERTION(treeOp, "Tree op allocation failed.");
   treeOp->Init(mSlash, aMsgId);
 }
--- a/parser/html/nsHtml5Highlighter.h
+++ b/parser/html/nsHtml5Highlighter.h
@@ -309,21 +309,16 @@ class nsHtml5Highlighter
     bool mInCharacters;
 
     /**
      * The current buffer being tokenized.
      */
     nsHtml5UTF16Buffer* mBuffer;
 
     /**
-     * Whether to highlight syntax visibly initially.
-     */
-    bool mSyntaxHighlight;
-
-    /**
      * The outgoing tree op queue.
      */
     nsTArray<nsHtml5TreeOperation> mOpQueue;
 
     /**
      * The tree op stage for the tree op executor.
      */
     nsAHtml5TreeOpSink* mOpSink;
--- a/parser/html/nsHtml5ViewSourceUtils.cpp
+++ b/parser/html/nsHtml5ViewSourceUtils.cpp
@@ -10,18 +10,24 @@
 // static
 nsHtml5HtmlAttributes*
 nsHtml5ViewSourceUtils::NewBodyAttributes()
 {
   nsHtml5HtmlAttributes* bodyAttrs = new nsHtml5HtmlAttributes(0);
   nsString* id = new nsString(NS_LITERAL_STRING("viewsource"));
   bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_ID, id);
 
+  nsString* klass = new nsString();
   if (mozilla::Preferences::GetBool("view_source.wrap_long_lines", true)) {
-    nsString* klass = new nsString(NS_LITERAL_STRING("wrap"));
+    klass->Append(NS_LITERAL_STRING("wrap "));
+  }
+  if (mozilla::Preferences::GetBool("view_source.syntax_highlight", true)) {
+    klass->Append(NS_LITERAL_STRING("highlight"));
+  }
+  if (!klass->IsEmpty()) {
     bodyAttrs->addAttribute(nsHtml5AttributeName::ATTR_CLASS, klass);
   }
 
   int32_t 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);