Skip to content

Commit 756e6ff

Browse files
authored
Mozilla bug 1857221 - Avoid tracking the line and column number when parsing innerHTML. r=smaug (#88)
Differential Revision: https://phabricator.services.mozilla.com/D191270
1 parent 0052f05 commit 756e6ff

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed

src/nu/validator/htmlparser/impl/Tokenizer.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,9 +1401,19 @@ protected void startErrorReporting() throws SAXException {
14011401
public void start() throws SAXException {
14021402
initializeWithoutStarting();
14031403
tokenHandler.startTokenization(this);
1404-
// CPPONLY: line = 0;
1405-
// CPPONLY: col = 1;
1406-
// CPPONLY: nextCharOnNewLine = true;
1404+
// CPPONLY: if (mViewSource) {
1405+
// CPPONLY: line = 1;
1406+
// CPPONLY: col = -1;
1407+
// CPPONLY: nextCharOnNewLine = false;
1408+
// CPPONLY: } else if (tokenHandler.WantsLineAndColumn()) {
1409+
// CPPONLY: line = 0;
1410+
// CPPONLY: col = 1;
1411+
// CPPONLY: nextCharOnNewLine = true;
1412+
// CPPONLY: } else {
1413+
// CPPONLY: line = -1;
1414+
// CPPONLY: col = -1;
1415+
// CPPONLY: nextCharOnNewLine = false;
1416+
// CPPONLY: }
14071417
// [NOCPP[
14081418
startErrorReporting();
14091419
// ]NOCPP]
@@ -1469,6 +1479,8 @@ public boolean tokenizeBuffer(UTF16Buffer buffer) throws SAXException {
14691479
// CPPONLY: mViewSource.SetBuffer(buffer);
14701480
// CPPONLY: pos = stateLoop(state, c, pos, buffer.getBuffer(), false, returnState, buffer.getEnd());
14711481
// CPPONLY: mViewSource.DropBuffer((pos == buffer.getEnd()) ? pos : pos + 1);
1482+
// CPPONLY: } else if (tokenHandler.WantsLineAndColumn()) {
1483+
// CPPONLY: pos = stateLoop(state, c, pos, buffer.getBuffer(), false, returnState, buffer.getEnd());
14721484
// CPPONLY: } else {
14731485
// CPPONLY: pos = stateLoop(state, c, pos, buffer.getBuffer(), false, returnState, buffer.getEnd());
14741486
// CPPONLY: }
@@ -6320,24 +6332,24 @@ private void initDoctypeFields() {
63206332
forceQuirks = false;
63216333
}
63226334

6323-
@Inline private void adjustDoubleHyphenAndAppendToStrBufCarriageReturn()
6335+
private void adjustDoubleHyphenAndAppendToStrBufCarriageReturn()
63246336
throws SAXException {
63256337
silentCarriageReturn();
63266338
adjustDoubleHyphenAndAppendToStrBufAndErr('\n', false);
63276339
}
63286340

6329-
@Inline private void adjustDoubleHyphenAndAppendToStrBufLineFeed()
6341+
private void adjustDoubleHyphenAndAppendToStrBufLineFeed()
63306342
throws SAXException {
63316343
silentLineFeed();
63326344
adjustDoubleHyphenAndAppendToStrBufAndErr('\n', false);
63336345
}
63346346

6335-
@Inline private void appendStrBufLineFeed() {
6347+
private void appendStrBufLineFeed() {
63366348
silentLineFeed();
63376349
appendStrBuf('\n');
63386350
}
63396351

6340-
@Inline private void appendStrBufCarriageReturn() {
6352+
private void appendStrBufCarriageReturn() {
63416353
silentCarriageReturn();
63426354
appendStrBuf('\n');
63436355
}

translator-src/nu/validator/htmlparser/cpptranslate/CppTypes.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class CppTypes {
9696
"nsHtml5ArrayCopy", "nsHtml5AtomTable", "nsHtml5DocumentMode",
9797
"nsHtml5Highlighter", "nsHtml5Macros", "nsHtml5NamedCharacters",
9898
"nsHtml5NamedCharactersAccel", "nsHtml5String",
99-
"nsHtml5TokenizerLoopPolicies", "nsIContent", "nsTraceRefcnt" };
99+
"nsIContent", "nsTraceRefcnt" };
100100

101101
private static final String[] STACK_NODE_INCLUDES = { "nsAtom", "nsHtml5AtomTable",
102102
"nsHtml5HtmlAttributes", "nsHtml5String", "nsNameSpaceManager", "nsIContent",
@@ -125,7 +125,7 @@ public class CppTypes {
125125
"Tokenizer", "TreeBuilder", "UTF16Buffer", };
126126

127127
private static final String[] STATE_LOOP_POLICIES = {
128-
"nsHtml5ViewSourcePolicy", "nsHtml5SilentPolicy" };
128+
"nsHtml5ViewSourcePolicy", "nsHtml5LineColPolicy", "nsHtml5FastestPolicy" };
129129

130130
private final Map<String, String> atomMap = new HashMap<String, String>();
131131

@@ -394,6 +394,17 @@ public String[] namedCharactersIncludes() {
394394
public String[] boilerplateForwardDeclarations() {
395395
return FORWARD_DECLARATIONS;
396396
}
397+
398+
public boolean requiresTemplateParameter(String methodName) {
399+
return "stateLoop".equals(methodName)
400+
|| "adjustDoubleHyphenAndAppendToStrBufCarriageReturn".equals(
401+
methodName)
402+
|| "adjustDoubleHyphenAndAppendToStrBufLineFeed".equals(
403+
methodName)
404+
|| "appendStrBufLineFeed".equals(methodName)
405+
|| "appendStrBufCarriageReturn".equals(methodName)
406+
|| "emitCarriageReturn".equals(methodName);
407+
}
397408

398409
public String documentModeHandlerType() {
399410
return "nsHtml5TreeBuilder*";
@@ -467,6 +478,18 @@ public String transition() {
467478
return "P::transition";
468479
}
469480

481+
public String checkChar() {
482+
return "P::checkChar";
483+
}
484+
485+
public String silentLineFeed() {
486+
return "P::silentLineFeed";
487+
}
488+
489+
public String silentCarriageReturn() {
490+
return "P::silentCarriageReturn";
491+
}
492+
470493
public String tokenizerErrorCondition() {
471494
return "P::reportErrors";
472495
}
@@ -502,4 +525,8 @@ public String releaseAssertionMacro() {
502525
public String crashMacro() {
503526
return "MOZ_CRASH";
504527
}
528+
529+
public String loopPolicyInclude() {
530+
return "nsHtml5TokenizerLoopPolicies";
531+
}
505532
}

translator-src/nu/validator/htmlparser/cpptranslate/CppVisitor.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,16 @@ protected void startClassDeclaration() {
439439
printer.print(className);
440440
printer.printLn(".h\"");
441441
printer.printLn();
442+
443+
if ("Tokenizer".equals(javaClassName)) {
444+
String loopPolicyInclude = cppTypes.loopPolicyInclude();
445+
if (loopPolicyInclude != null) {
446+
printer.print("#include \"");
447+
printer.print(loopPolicyInclude);
448+
printer.printLn(".h\"");
449+
printer.printLn();
450+
}
451+
}
442452
}
443453

444454
public void visit(EmptyTypeDeclaration n, LocalSymbolTable arg) {
@@ -1280,6 +1290,15 @@ public void visit(MethodCallExpr n, LocalSymbolTable arg) {
12801290
} else if ("transition".equals(n.getName())
12811291
&& n.getScope() == null) {
12821292
visitTransition(n, arg);
1293+
} else if ("checkChar".equals(n.getName())
1294+
&& n.getScope() == null) {
1295+
visitCheckChar(n, arg);
1296+
} else if ("silentCarriageReturn".equals(n.getName())
1297+
&& n.getScope() == null) {
1298+
visitSilentCarriageReturn(n, arg);
1299+
} else if ("silentLineFeed".equals(n.getName())
1300+
&& n.getScope() == null) {
1301+
visitSilentLineFeed(n, arg);
12831302
} else if ("arraycopy".equals(n.getName())
12841303
&& "System".equals(n.getScope().toString())) {
12851304
printer.print(cppTypes.arrayCopy());
@@ -1364,6 +1383,10 @@ public void visit(MethodCallExpr n, LocalSymbolTable arg) {
13641383
printer.print(cppTypes.stateLoopPolicies()[stateLoopCallCount]);
13651384
printer.print(">");
13661385
stateLoopCallCount++;
1386+
} else if (cppTypes.requiresTemplateParameter(n.getName())
1387+
&& "Tokenizer".equals(javaClassName)
1388+
&& cppTypes.stateLoopPolicies().length > 0) {
1389+
printer.print("<P>");
13671390
}
13681391
printer.print("(");
13691392
if (n.getArgs() != null) {
@@ -1582,7 +1605,7 @@ protected void printMethodDeclaration(MethodDeclaration n,
15821605
printModifiers(n.getModifiers());
15831606
}
15841607

1585-
if ("stateLoop".equals(currentMethod)
1608+
if (cppTypes.requiresTemplateParameter(currentMethod)
15861609
&& "Tokenizer".equals(javaClassName)
15871610
&& cppTypes.stateLoopPolicies().length > 0) {
15881611
printer.print("template<class P>");
@@ -1882,6 +1905,26 @@ private void visitTransition(MethodCallExpr call, LocalSymbolTable arg) {
18821905
}
18831906
}
18841907

1908+
private void visitCheckChar(MethodCallExpr call, LocalSymbolTable arg) {
1909+
List<Expression> args = call.getArgs();
1910+
printer.print(cppTypes.checkChar());
1911+
printer.print("(this, ");
1912+
args.get(0).accept(this, arg);
1913+
printer.print(", ");
1914+
args.get(1).accept(this, arg);
1915+
printer.print(")");
1916+
}
1917+
1918+
private void visitSilentLineFeed(MethodCallExpr call, LocalSymbolTable arg) {
1919+
printer.print(cppTypes.silentLineFeed());
1920+
printer.print("(this)");
1921+
}
1922+
1923+
private void visitSilentCarriageReturn(MethodCallExpr call, LocalSymbolTable arg) {
1924+
printer.print(cppTypes.silentCarriageReturn());
1925+
printer.print("(this)");
1926+
}
1927+
18851928
private boolean isTokenizerErrorReportingExpression(Expression e) {
18861929
if (!reportTransitions) {
18871930
return false;

0 commit comments

Comments
 (0)