parser/html/nsHtml5Highlighter.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     4 #ifndef nsHtml5Highlighter_h
     5 #define nsHtml5Highlighter_h
     7 #include "nsCOMPtr.h"
     8 #include "nsHtml5TreeOperation.h"
     9 #include "nsHtml5UTF16Buffer.h"
    10 #include "nsHtml5TreeOperation.h"
    11 #include "nsAHtml5TreeOpSink.h"
    13 #define NS_HTML5_HIGHLIGHTER_HANDLE_ARRAY_LENGTH 512
    15 /**
    16  * A state machine for generating HTML for display in View Source based on
    17  * the transitions the tokenizer makes on the source being viewed.
    18  */
    19 class nsHtml5Highlighter
    20 {
    21   public:
    22     /**
    23      * The constructor.
    24      *
    25      * @param aOpSink the sink for the tree ops generated by this highlighter
    26      */
    27     nsHtml5Highlighter(nsAHtml5TreeOpSink* aOpSink);
    29     /**
    30      * The destructor.
    31      */
    32     ~nsHtml5Highlighter();
    34     /**
    35      * Starts the generated document.
    36      */
    37     void Start(const nsAutoString& aTitle);
    39     /**
    40      * Report a tokenizer state transition.
    41      *
    42      * @param aState the state being transitioned to
    43      * @param aReconsume whether this is a reconsuming transition
    44      * @param aPos the tokenizer's current position into the buffer
    45      */
    46     int32_t Transition(int32_t aState, bool aReconsume, int32_t aPos);
    48     /**
    49      * Report end of file.
    50      */
    51     void End();
    53     /**
    54      * Set the current buffer being tokenized
    55      */
    56     void SetBuffer(nsHtml5UTF16Buffer* aBuffer);
    58     /**
    59      * Let go of the buffer being tokenized but first, flush text from it.
    60      *
    61      * @param aPos the first UTF-16 code unit not to flush
    62      */
    63     void DropBuffer(int32_t aPos);
    65     /**
    66      * Flush the tree ops into the sink.
    67      *
    68      * @return true if there were ops to flush
    69      */
    70     bool FlushOps();
    72     /**
    73      * Linkify the current attribute value if the attribute name is one of
    74      * known URL attributes. (When executing tree ops, javascript: URLs will
    75      * not be linkified, though.)
    76      *
    77      * @param aName the name of the attribute
    78      * @param aValue the value of the attribute
    79      */
    80     void MaybeLinkifyAttributeValue(nsHtml5AttributeName* aName,
    81                                     nsString* aValue);
    83     /**
    84      * Inform the highlighter that the tokenizer successfully completed a
    85      * named character reference.
    86      */
    87     void CompletedNamedCharacterReference();
    89     /**
    90      * Adds an error annotation to the node that's currently on top of
    91      * mStack.
    92      *
    93      * @param aMsgId the id of the message in the property file
    94      */
    95     void AddErrorToCurrentNode(const char* aMsgId);
    97     /**
    98      * Adds an error annotation to the node that corresponds to the most
    99      * recently opened markup declaration/tag span, character reference or
   100      * run of text.
   101      *
   102      * @param aMsgId the id of the message in the property file
   103      */
   104     void AddErrorToCurrentRun(const char* aMsgId);
   106     /**
   107      * Adds an error annotation to the node that corresponds to the most
   108      * recently opened markup declaration/tag span, character reference or
   109      * run of text with one atom to use when formatting the message.
   110      *
   111      * @param aMsgId the id of the message in the property file
   112      * @param aName the atom
   113      */
   114     void AddErrorToCurrentRun(const char* aMsgId, nsIAtom* aName);
   116     /**
   117      * Adds an error annotation to the node that corresponds to the most
   118      * recently opened markup declaration/tag span, character reference or
   119      * run of text with two atoms to use when formatting the message.
   120      *
   121      * @param aMsgId the id of the message in the property file
   122      * @param aName the first atom
   123      * @param aOther the second atom
   124      */
   125     void AddErrorToCurrentRun(const char* aMsgId,
   126                               nsIAtom* aName,
   127                               nsIAtom* aOther);
   129     /**
   130      * Adds an error annotation to the node that corresponds to the most
   131      * recent potentially character reference-starting ampersand.
   132      *
   133      * @param aMsgId the id of the message in the property file
   134      */
   135     void AddErrorToCurrentAmpersand(const char* aMsgId);
   137     /**
   138      * Adds an error annotation to the node that corresponds to the most
   139      * recent potentially self-closing slash.
   140      *
   141      * @param aMsgId the id of the message in the property file
   142      */
   143     void AddErrorToCurrentSlash(const char* aMsgId);
   145   private:
   147     /**
   148      * Starts a span with no class.
   149      */
   150     void StartSpan();
   152     /**
   153      * Starts a <span> and sets the class attribute on it.
   154      *
   155      * @param aClass the class to set (MUST be a static string that does not
   156      *        need to be released!)
   157      */
   158     void StartSpan(const char16_t* aClass);
   160     /**
   161      * End the current <span> or <a> in the highlighter output.
   162      */
   163     void EndSpanOrA();
   165     /**
   166      * Starts a wrapper around a run of characters.
   167      */
   168     void StartCharacters();
   170     /**
   171      * Ends a wrapper around a run of characters.
   172      */
   173     void EndCharactersAndStartMarkupRun();
   175     /**
   176      * Starts an <a>.
   177      */
   178     void StartA();
   180     /**
   181      * Flushes characters up to but not including the current one.
   182      */
   183     void FlushChars();
   185     /**
   186      * Flushes characters up to and including the current one.
   187      */
   188     void FlushCurrent();
   190     /**
   191      * Finishes highlighting a tag in the input data by closing the open
   192      * <span> and <a> elements in the highlighter output and then starts
   193      * another <span> for potentially highlighting characters potentially
   194      * appearing next.
   195      */
   196     void FinishTag();
   198     /**
   199      * Adds a class attribute to the current node.
   200      *
   201      * @param aClass the class to set (MUST be a static string that does not
   202      *        need to be released!)
   203      */
   204     void AddClass(const char16_t* aClass);
   206     /**
   207      * Allocates a handle for an element.
   208      *
   209      * See the documentation for nsHtml5TreeBuilder::AllocateContentHandle()
   210      * in nsHtml5TreeBuilderHSupplement.h.
   211      *
   212      * @return the handle
   213      */
   214     nsIContent** AllocateContentHandle();
   216     /**
   217      * Enqueues an element creation tree operation.
   218      *
   219      * @param aName the name of the element
   220      * @param aAttributes the attribute holder (ownership will be taken) or
   221      *        nullptr for no attributes
   222      * @return the handle for the element that will be created
   223      */
   224     nsIContent** CreateElement(nsIAtom* aName,
   225                                nsHtml5HtmlAttributes* aAttributes);
   227     /**
   228      * Gets the handle for the current node. May be called only after the
   229      * root element has been set.
   230      *
   231      * @return the handle for the current node
   232      */
   233     nsIContent** CurrentNode();
   235     /**
   236      * Create an element and push it (its handle) on the stack.
   237      *
   238      * @param aName the name of the element
   239      * @param aAttributes the attribute holder (ownership will be taken) or
   240      *        nullptr for no attributes
   241      */
   242     void Push(nsIAtom* aName, nsHtml5HtmlAttributes* aAttributes);
   244     /**
   245      * Pops the current node off the stack.
   246      */
   247     void Pop();
   249     /**
   250      * Appends text content to the current node.
   251      *
   252      * @param aBuffer the buffer to copy from
   253      * @param aStart the index of the first code unit to copy
   254      * @param aLength the number of code units to copy
   255      */
   256     void AppendCharacters(const char16_t* aBuffer,
   257                           int32_t aStart,
   258                           int32_t aLength);
   260     /**
   261      * Enqueues a tree op for adding an href attribute with the view-source:
   262      * URL scheme to the current node.
   263      *
   264      * @param aValue the (potentially relative) URL to link to
   265      */
   266     void AddViewSourceHref(const nsString& aValue);
   268     /**
   269      * The state we are transitioning away from.
   270      */
   271     int32_t mState;
   273     /**
   274      * The index of the first UTF-16 code unit in mBuffer that hasn't been
   275      * flushed yet.
   276      */
   277     int32_t mCStart;
   279     /**
   280      * The position of the code unit in mBuffer that caused the current
   281      * transition.
   282      */
   283     int32_t mPos;
   285     /**
   286      * The current line number.
   287      */
   288     int32_t mLineNumber;
   290     /**
   291      * The number of inline elements open inside the <pre> excluding the
   292      * span potentially wrapping a run of characters.
   293      */
   294     int32_t mInlinesOpen;
   296     /**
   297      * Whether there's a span wrapping a run of characters (excluding CDATA
   298      * section) open.
   299      */
   300     bool mInCharacters;
   302     /**
   303      * The current buffer being tokenized.
   304      */
   305     nsHtml5UTF16Buffer* mBuffer;
   307     /**
   308      * Whether to highlight syntax visibly initially.
   309      */
   310     bool mSyntaxHighlight;
   312     /**
   313      * The outgoing tree op queue.
   314      */
   315     nsTArray<nsHtml5TreeOperation> mOpQueue;
   317     /**
   318      * The tree op stage for the tree op executor.
   319      */
   320     nsAHtml5TreeOpSink* mOpSink;
   322     /**
   323      * The most recently opened markup declaration/tag or run of characters.
   324      */
   325     nsIContent** mCurrentRun;
   327     /**
   328      * The most recent ampersand in a place where character references were
   329      * allowed.
   330      */
   331     nsIContent** mAmpersand;
   333     /**
   334      * The most recent slash that might become a self-closing slash.
   335      */
   336     nsIContent** mSlash;
   338     /**
   339      * Memory for element handles.
   340      */
   341     nsAutoArrayPtr<nsIContent*> mHandles;
   343     /**
   344      * Number of handles used in mHandles
   345      */
   346     int32_t mHandlesUsed;
   348     /**
   349      * A holder for old contents of mHandles
   350      */
   351     nsTArray<nsAutoArrayPtr<nsIContent*> > mOldHandles;
   353     /**
   354      * The element stack.
   355      */
   356     nsTArray<nsIContent**> mStack;
   358     /**
   359      * The string "comment"
   360      */
   361     static char16_t sComment[];
   363     /**
   364      * The string "cdata"
   365      */
   366     static char16_t sCdata[];
   368     /**
   369      * The string "start-tag"
   370      */
   371     static char16_t sStartTag[];
   373     /**
   374      * The string "attribute-name"
   375      */
   376     static char16_t sAttributeName[];
   378     /**
   379      * The string "attribute-value"
   380      */
   381     static char16_t sAttributeValue[];
   383     /**
   384      * The string "end-tag"
   385      */
   386     static char16_t sEndTag[];
   388     /**
   389      * The string "doctype"
   390      */
   391     static char16_t sDoctype[];
   393     /**
   394      * The string "entity"
   395      */
   396     static char16_t sEntity[];
   398     /**
   399      * The string "pi"
   400      */
   401     static char16_t sPi[];
   402 };
   404 #endif // nsHtml5Highlighter_h

mercurial