2012-05-29 19:52:43 +04:00
|
|
|
/* 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/. */
|
2013-08-23 19:07:10 +04:00
|
|
|
#ifndef nsHtml5Highlighter_h
|
|
|
|
#define nsHtml5Highlighter_h
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsHtml5TreeOperation.h"
|
|
|
|
#include "nsHtml5UTF16Buffer.h"
|
|
|
|
#include "nsHtml5TreeOperation.h"
|
|
|
|
#include "nsAHtml5TreeOpSink.h"
|
|
|
|
|
|
|
|
#define NS_HTML5_HIGHLIGHTER_HANDLE_ARRAY_LENGTH 512
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A state machine for generating HTML for display in View Source based on
|
|
|
|
* the transitions the tokenizer makes on the source being viewed.
|
|
|
|
*/
|
|
|
|
class nsHtml5Highlighter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* The constructor.
|
|
|
|
*
|
|
|
|
* @param aOpSink the sink for the tree ops generated by this highlighter
|
|
|
|
*/
|
2014-09-02 02:04:52 +04:00
|
|
|
explicit nsHtml5Highlighter(nsAHtml5TreeOpSink* aOpSink);
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The destructor.
|
|
|
|
*/
|
|
|
|
~nsHtml5Highlighter();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the generated document.
|
|
|
|
*/
|
2011-11-30 21:44:31 +04:00
|
|
|
void Start(const nsAutoString& aTitle);
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report a tokenizer state transition.
|
|
|
|
*
|
|
|
|
* @param aState the state being transitioned to
|
|
|
|
* @param aReconsume whether this is a reconsuming transition
|
|
|
|
* @param aPos the tokenizer's current position into the buffer
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t Transition(int32_t aState, bool aReconsume, int32_t aPos);
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Report end of file.
|
|
|
|
*/
|
|
|
|
void End();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the current buffer being tokenized
|
|
|
|
*/
|
|
|
|
void SetBuffer(nsHtml5UTF16Buffer* aBuffer);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Let go of the buffer being tokenized but first, flush text from it.
|
|
|
|
*
|
|
|
|
* @param aPos the first UTF-16 code unit not to flush
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
void DropBuffer(int32_t aPos);
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush the tree ops into the sink.
|
|
|
|
*
|
|
|
|
* @return true if there were ops to flush
|
|
|
|
*/
|
|
|
|
bool FlushOps();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Linkify the current attribute value if the attribute name is one of
|
|
|
|
* known URL attributes. (When executing tree ops, javascript: URLs will
|
|
|
|
* not be linkified, though.)
|
|
|
|
*
|
|
|
|
* @param aName the name of the attribute
|
|
|
|
* @param aValue the value of the attribute
|
|
|
|
*/
|
|
|
|
void MaybeLinkifyAttributeValue(nsHtml5AttributeName* aName,
|
|
|
|
nsString* aValue);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inform the highlighter that the tokenizer successfully completed a
|
|
|
|
* named character reference.
|
|
|
|
*/
|
|
|
|
void CompletedNamedCharacterReference();
|
|
|
|
|
2011-11-01 15:33:11 +04:00
|
|
|
/**
|
|
|
|
* Adds an error annotation to the node that's currently on top of
|
|
|
|
* mStack.
|
2011-11-01 15:33:11 +04:00
|
|
|
*
|
|
|
|
* @param aMsgId the id of the message in the property file
|
2011-11-01 15:33:11 +04:00
|
|
|
*/
|
|
|
|
void AddErrorToCurrentNode(const char* aMsgId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an error annotation to the node that corresponds to the most
|
2011-11-01 15:33:11 +04:00
|
|
|
* recently opened markup declaration/tag span, character reference or
|
|
|
|
* run of text.
|
|
|
|
*
|
|
|
|
* @param aMsgId the id of the message in the property file
|
|
|
|
*/
|
|
|
|
void AddErrorToCurrentRun(const char* aMsgId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an error annotation to the node that corresponds to the most
|
|
|
|
* recently opened markup declaration/tag span, character reference or
|
|
|
|
* run of text with one atom to use when formatting the message.
|
|
|
|
*
|
|
|
|
* @param aMsgId the id of the message in the property file
|
|
|
|
* @param aName the atom
|
|
|
|
*/
|
|
|
|
void AddErrorToCurrentRun(const char* aMsgId, nsIAtom* aName);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an error annotation to the node that corresponds to the most
|
|
|
|
* recently opened markup declaration/tag span, character reference or
|
|
|
|
* run of text with two atoms to use when formatting the message.
|
|
|
|
*
|
|
|
|
* @param aMsgId the id of the message in the property file
|
|
|
|
* @param aName the first atom
|
|
|
|
* @param aOther the second atom
|
2011-11-01 15:33:11 +04:00
|
|
|
*/
|
2011-11-01 15:33:11 +04:00
|
|
|
void AddErrorToCurrentRun(const char* aMsgId,
|
|
|
|
nsIAtom* aName,
|
|
|
|
nsIAtom* aOther);
|
2011-11-01 15:33:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an error annotation to the node that corresponds to the most
|
|
|
|
* recent potentially character reference-starting ampersand.
|
2011-11-01 15:33:11 +04:00
|
|
|
*
|
|
|
|
* @param aMsgId the id of the message in the property file
|
2011-11-01 15:33:11 +04:00
|
|
|
*/
|
|
|
|
void AddErrorToCurrentAmpersand(const char* aMsgId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an error annotation to the node that corresponds to the most
|
|
|
|
* recent potentially self-closing slash.
|
2011-11-01 15:33:11 +04:00
|
|
|
*
|
|
|
|
* @param aMsgId the id of the message in the property file
|
2011-11-01 15:33:11 +04:00
|
|
|
*/
|
|
|
|
void AddErrorToCurrentSlash(const char* aMsgId);
|
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a span with no class.
|
|
|
|
*/
|
|
|
|
void StartSpan();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a <span> and sets the class attribute on it.
|
|
|
|
*
|
|
|
|
* @param aClass the class to set (MUST be a static string that does not
|
|
|
|
* need to be released!)
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
void StartSpan(const char16_t* aClass);
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
2011-11-01 15:33:11 +04:00
|
|
|
* End the current <span> or <a> in the highlighter output.
|
2010-07-30 14:15:38 +04:00
|
|
|
*/
|
2011-11-01 15:33:11 +04:00
|
|
|
void EndSpanOrA();
|
2010-07-30 14:15:38 +04:00
|
|
|
|
2011-11-01 15:33:11 +04:00
|
|
|
/**
|
|
|
|
* Starts a wrapper around a run of characters.
|
|
|
|
*/
|
|
|
|
void StartCharacters();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends a wrapper around a run of characters.
|
|
|
|
*/
|
2012-04-17 15:53:52 +04:00
|
|
|
void EndCharactersAndStartMarkupRun();
|
2011-11-01 15:33:11 +04:00
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
/**
|
|
|
|
* Starts an <a>.
|
|
|
|
*/
|
|
|
|
void StartA();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flushes characters up to but not including the current one.
|
|
|
|
*/
|
|
|
|
void FlushChars();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flushes characters up to and including the current one.
|
|
|
|
*/
|
|
|
|
void FlushCurrent();
|
|
|
|
|
|
|
|
/**
|
2011-11-01 15:33:11 +04:00
|
|
|
* Finishes highlighting a tag in the input data by closing the open
|
|
|
|
* <span> and <a> elements in the highlighter output and then starts
|
|
|
|
* another <span> for potentially highlighting characters potentially
|
|
|
|
* appearing next.
|
2010-07-30 14:15:38 +04:00
|
|
|
*/
|
|
|
|
void FinishTag();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a class attribute to the current node.
|
|
|
|
*
|
|
|
|
* @param aClass the class to set (MUST be a static string that does not
|
|
|
|
* need to be released!)
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
void AddClass(const char16_t* aClass);
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a handle for an element.
|
|
|
|
*
|
2011-11-01 15:33:11 +04:00
|
|
|
* See the documentation for nsHtml5TreeBuilder::AllocateContentHandle()
|
|
|
|
* in nsHtml5TreeBuilderHSupplement.h.
|
|
|
|
*
|
2010-07-30 14:15:38 +04:00
|
|
|
* @return the handle
|
|
|
|
*/
|
|
|
|
nsIContent** AllocateContentHandle();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueues an element creation tree operation.
|
|
|
|
*
|
|
|
|
* @param aName the name of the element
|
|
|
|
* @param aAttributes the attribute holder (ownership will be taken) or
|
2012-07-30 18:20:58 +04:00
|
|
|
* nullptr for no attributes
|
2010-07-30 14:15:38 +04:00
|
|
|
* @return the handle for the element that will be created
|
|
|
|
*/
|
|
|
|
nsIContent** CreateElement(nsIAtom* aName,
|
|
|
|
nsHtml5HtmlAttributes* aAttributes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the handle for the current node. May be called only after the
|
|
|
|
* root element has been set.
|
|
|
|
*
|
|
|
|
* @return the handle for the current node
|
|
|
|
*/
|
|
|
|
nsIContent** CurrentNode();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an element and push it (its handle) on the stack.
|
|
|
|
*
|
|
|
|
* @param aName the name of the element
|
|
|
|
* @param aAttributes the attribute holder (ownership will be taken) or
|
2012-07-30 18:20:58 +04:00
|
|
|
* nullptr for no attributes
|
2010-07-30 14:15:38 +04:00
|
|
|
*/
|
|
|
|
void Push(nsIAtom* aName, nsHtml5HtmlAttributes* aAttributes);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pops the current node off the stack.
|
|
|
|
*/
|
|
|
|
void Pop();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends text content to the current node.
|
|
|
|
*
|
|
|
|
* @param aBuffer the buffer to copy from
|
|
|
|
* @param aStart the index of the first code unit to copy
|
|
|
|
* @param aLength the number of code units to copy
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
void AppendCharacters(const char16_t* aBuffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aStart,
|
|
|
|
int32_t aLength);
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueues a tree op for adding an href attribute with the view-source:
|
|
|
|
* URL scheme to the current node.
|
|
|
|
*
|
|
|
|
* @param aValue the (potentially relative) URL to link to
|
|
|
|
*/
|
|
|
|
void AddViewSourceHref(const nsString& aValue);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The state we are transitioning away from.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mState;
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The index of the first UTF-16 code unit in mBuffer that hasn't been
|
|
|
|
* flushed yet.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mCStart;
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The position of the code unit in mBuffer that caused the current
|
|
|
|
* transition.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mPos;
|
2010-07-30 14:15:38 +04:00
|
|
|
|
2011-11-01 15:33:11 +04:00
|
|
|
/**
|
|
|
|
* The current line number.
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mLineNumber;
|
2011-11-01 15:33:11 +04:00
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
/**
|
2011-11-01 15:33:11 +04:00
|
|
|
* The number of inline elements open inside the <pre> excluding the
|
|
|
|
* span potentially wrapping a run of characters.
|
2010-07-30 14:15:38 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mInlinesOpen;
|
2010-07-30 14:15:38 +04:00
|
|
|
|
2011-11-01 15:33:11 +04:00
|
|
|
/**
|
|
|
|
* Whether there's a span wrapping a run of characters (excluding CDATA
|
|
|
|
* section) open.
|
|
|
|
*/
|
|
|
|
bool mInCharacters;
|
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2011-11-01 15:33:11 +04:00
|
|
|
/**
|
2011-11-01 15:33:11 +04:00
|
|
|
* The most recently opened markup declaration/tag or run of characters.
|
2011-11-01 15:33:11 +04:00
|
|
|
*/
|
2011-11-01 15:33:11 +04:00
|
|
|
nsIContent** mCurrentRun;
|
2011-11-01 15:33:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The most recent ampersand in a place where character references were
|
|
|
|
* allowed.
|
|
|
|
*/
|
|
|
|
nsIContent** mAmpersand;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The most recent slash that might become a self-closing slash.
|
|
|
|
*/
|
|
|
|
nsIContent** mSlash;
|
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
/**
|
|
|
|
* Memory for element handles.
|
|
|
|
*/
|
|
|
|
nsAutoArrayPtr<nsIContent*> mHandles;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of handles used in mHandles
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mHandlesUsed;
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A holder for old contents of mHandles
|
|
|
|
*/
|
|
|
|
nsTArray<nsAutoArrayPtr<nsIContent*> > mOldHandles;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The element stack.
|
|
|
|
*/
|
|
|
|
nsTArray<nsIContent**> mStack;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "comment"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sComment[];
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "cdata"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sCdata[];
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "start-tag"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sStartTag[];
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "attribute-name"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sAttributeName[];
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "attribute-value"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sAttributeValue[];
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "end-tag"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sEndTag[];
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "doctype"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sDoctype[];
|
2010-07-30 14:15:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "entity"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sEntity[];
|
2011-11-01 15:33:11 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The string "pi"
|
|
|
|
*/
|
2014-01-04 19:02:17 +04:00
|
|
|
static char16_t sPi[];
|
2010-07-30 14:15:38 +04:00
|
|
|
};
|
|
|
|
|
2013-08-23 19:07:10 +04:00
|
|
|
#endif // nsHtml5Highlighter_h
|