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