Backed out 2 changesets (bug 1886730) for causing detekt & klint failures CLOSED...
[gecko.git] / parser / html / nsHtml5TokenizerLoopPolicies.h
blob973b591c03fd3b23d42d3a129e0a71d6f7be92b3
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/. */
5 #ifndef nsHtml5TokenizerLoopPolicies_h
6 #define nsHtml5TokenizerLoopPolicies_h
8 /**
9 * This policy does not report tokenizer transitions anywhere and does not
10 * track line and column numbers. To be used for innerHTML.
12 struct nsHtml5FastestPolicy {
13 static const bool reportErrors = false;
14 static int32_t transition(nsHtml5Highlighter* aHighlighter, int32_t aState,
15 bool aReconsume, int32_t aPos) {
16 return aState;
18 static void completedNamedCharacterReference(
19 nsHtml5Highlighter* aHighlighter) {}
21 static char16_t checkChar(nsHtml5Tokenizer* aTokenizer, char16_t* buf,
22 int32_t pos) {
23 return buf[pos];
26 static void silentCarriageReturn(nsHtml5Tokenizer* aTokenizer) {
27 aTokenizer->lastCR = true;
30 static void silentLineFeed(nsHtml5Tokenizer* aTokenizer) {}
33 /**
34 * This policy does not report tokenizer transitions anywhere. To be used
35 * when _not_ viewing source and when not parsing innerHTML (or other
36 * script execution-preventing fragment).
38 struct nsHtml5LineColPolicy {
39 static const bool reportErrors = false;
40 static int32_t transition(nsHtml5Highlighter* aHighlighter, int32_t aState,
41 bool aReconsume, int32_t aPos) {
42 return aState;
44 static void completedNamedCharacterReference(
45 nsHtml5Highlighter* aHighlighter) {}
47 static char16_t checkChar(nsHtml5Tokenizer* aTokenizer, char16_t* buf,
48 int32_t pos) {
49 // The name of this method comes from the validator.
50 // We aren't checking a char here. We read the next
51 // UTF-16 code unit and, before returning it, adjust
52 // the line and column numbers.
53 char16_t c = buf[pos];
54 if (MOZ_UNLIKELY(aTokenizer->nextCharOnNewLine)) {
55 // By changing the line and column here instead
56 // of doing so eagerly when seeing the line break
57 // causes the line break itself to be considered
58 // column-wise at the end of a line.
59 aTokenizer->line++;
60 aTokenizer->col = 1;
61 aTokenizer->nextCharOnNewLine = false;
62 } else if (MOZ_LIKELY(!NS_IS_LOW_SURROGATE(c))) {
63 // SpiderMonkey wants to count scalar values
64 // instead of UTF-16 code units. We omit low
65 // surrogates from the count so that only the
66 // high surrogate increments the count for
67 // two-code-unit scalar values.
69 // It's somewhat questionable from the performance
70 // perspective to make the human-perceivable column
71 // count correct for non-BMP characters in the case
72 // where there is a single scalar value per extended
73 // grapheme cluster when even on the BMP there are
74 // various cases where the scalar count doesn't make
75 // much sense as a human-perceived "column count" due
76 // to extended grapheme clusters consisting of more
77 // than one scalar value.
78 aTokenizer->col++;
80 return c;
83 static void silentCarriageReturn(nsHtml5Tokenizer* aTokenizer) {
84 aTokenizer->nextCharOnNewLine = true;
85 aTokenizer->lastCR = true;
88 static void silentLineFeed(nsHtml5Tokenizer* aTokenizer) {
89 aTokenizer->nextCharOnNewLine = true;
93 /**
94 * This policy reports the tokenizer transitions to a highlighter. To be used
95 * when viewing source.
97 struct nsHtml5ViewSourcePolicy {
98 static const bool reportErrors = true;
99 static int32_t transition(nsHtml5Highlighter* aHighlighter, int32_t aState,
100 bool aReconsume, int32_t aPos) {
101 return aHighlighter->Transition(aState, aReconsume, aPos);
103 static void completedNamedCharacterReference(
104 nsHtml5Highlighter* aHighlighter) {
105 aHighlighter->CompletedNamedCharacterReference();
108 static char16_t checkChar(nsHtml5Tokenizer* aTokenizer, char16_t* buf,
109 int32_t pos) {
110 return buf[pos];
113 static void silentCarriageReturn(nsHtml5Tokenizer* aTokenizer) {
114 aTokenizer->line++;
115 aTokenizer->lastCR = true;
118 static void silentLineFeed(nsHtml5Tokenizer* aTokenizer) {
119 aTokenizer->line++;
123 #endif // nsHtml5TokenizerLoopPolicies_h