TemplateParser { public void discardLogicWhitespace(TemplateModel model) { boolean withinContentClosure = false; for (int i = 0; i < model.getUnits().size(); i++) { TemplateUnit unit = model.getUnits().get(i); if (unit instanceof ContentClosureBegin) { withinContentClosure = true; } else if (unit instanceof ContentClosureEnd) { withinContentClosure = false; } if (withinContentClosure) { continue; } if (!unit.isBlockLevel()) { if (unit instanceof PlainText) { PlainText plain = (PlainText)unit; if (plain.isWhitespace()) { plain.trim(); } else { while (plain.chompLeadingWhitespaceToEndOfLine() > 0) { } break; } } else { break; } } } for (int i = 1; i < model.getUnits().size(); i++) { TemplateUnit unit = model.getUnits().get(i); if (!unit.isBlockLevel()) { continue; } int prevUnitTrailingWhitespaceLengthToStartOfLine = -1; PlainText prevPlain = null; TemplateUnit prevUnit = model.getUnits().get(i-1); if (prevUnit instanceof PlainText) { prevPlain = (PlainText)prevUnit; prevUnitTrailingWhitespaceLengthToStartOfLine = prevPlain.trailingWhitespaceLengthToStartOfLine(); } boolean lastUnit = ((i+1) == model.getUnits().size()); int nextUnitLeadingWhitespaceLengthToEndOfLine = -1; PlainText nextPlain = null; if (!lastUnit) { TemplateUnit nextUnit = model.getUnits().get(i+1); if (nextUnit instanceof PlainText) { nextPlain = (PlainText)nextUnit; nextUnitLeadingWhitespaceLengthToEndOfLine = nextPlain.leadingWhitespaceLengthToEndOfLine(); } } if ((prevPlain != null && prevUnitTrailingWhitespaceLengthToStartOfLine >= 0) && (lastUnit || (nextPlain != null && nextUnitLeadingWhitespaceLengthToEndOfLine >= 0))) { prevPlain.chompTrailingLength(prevUnitTrailingWhitespaceLengthToStartOfLine); if (nextPlain != null && nextUnitLeadingWhitespaceLengthToEndOfLine > 0) { nextPlain.chompLeadingLength(nextUnitLeadingWhitespaceLengthToEndOfLine); } } } for (int i = 0; i < model.getUnits().size(); ) { TemplateUnit unit = model.getUnits().get(i); if (unit instanceof PlainText) { PlainText plain = (PlainText)unit; if (plain.getText().isEmpty()) { model.getUnits().remove(i); continue; } } i++; } } TemplateParser(RockerConfiguration configuration); RockerConfiguration getConfiguration(); TemplateModel parse(File f); TemplateModel parse(File f, String packageName); TemplateModel parse(String source, String qualifiedName); static ParserException buildParserException(SourceRef sourceRef, String templatePath, String msg); static ParserException buildParserException(SourceRef sourceRef, String templatePath, String msg, Throwable cause); static ParserException unwrapParserRuntimeException(String templatePath, ParserRuntimeException e); void combineAdjacentPlain(TemplateModel model); void discardLogicWhitespace(TemplateModel model);  }
@Test public void discardLogicWhitespace() throws Exception { TemplateParser parser = createParser(); File f = findTemplate("rocker/parser/DiscardLogicWhitespace.rocker.html"); TemplateModel model = parser.parse(f); PlainText plainText = model.getUnit(0, PlainText.class); Assert.assertEquals("Hello\n", plainText.getText()); plainText = model.getUnit(2, PlainText.class); Assert.assertEquals(" if-block-true\n", plainText.getText()); plainText = model.getUnit(4, PlainText.class); Assert.assertEquals("if-block-false\n", plainText.getText()); }