Skip to content

Commit

Permalink
Revert "Keep track of parent path for each block in order to resolve …
Browse files Browse the repository at this point in the history
…relative paths"
  • Loading branch information
liamrharwood authored Aug 15, 2019
1 parent 988f7cd commit 769da25
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import com.hubspot.jinjava.random.DeferredRandomNumberGenerator;
import com.hubspot.jinjava.tree.Node;
import com.hubspot.jinjava.tree.TreeParser;
import com.hubspot.jinjava.tree.output.BlockInfo;
import com.hubspot.jinjava.tree.output.BlockPlaceholderOutputNode;
import com.hubspot.jinjava.tree.output.OutputList;
import com.hubspot.jinjava.tree.output.OutputNode;
Expand All @@ -59,7 +58,7 @@

public class JinjavaInterpreter {

private final Multimap<String, BlockInfo> blocks = ArrayListMultimap.create();
private final Multimap<String, List<? extends Node>> blocks = ArrayListMultimap.create();
private final LinkedList<Node> extendParentRoots = new LinkedList<>();

private Context context;
Expand Down Expand Up @@ -114,8 +113,8 @@ public void addExtendParentRoot(Node root) {
extendParentRoots.add(root);
}

public void addBlock(String name, BlockInfo blockInfo) {
blocks.put(name, blockInfo);
public void addBlock(String name, LinkedList<? extends Node> value) {
blocks.put(name, value);
}

/**
Expand Down Expand Up @@ -283,22 +282,17 @@ private void resolveBlockStubs(OutputList output, Stack<String> blockNames) {
for (BlockPlaceholderOutputNode blockPlaceholder : output.getBlocks()) {

if (!blockNames.contains(blockPlaceholder.getBlockName())) {
Collection<BlockInfo> blockChain = blocks.get(blockPlaceholder.getBlockName());
BlockInfo block = Iterables.getFirst(blockChain, null);
Collection<List<? extends Node>> blockChain = blocks.get(blockPlaceholder.getBlockName());
List<? extends Node> block = Iterables.getFirst(blockChain, null);

if (block != null && block.getNodes() != null) {
List<? extends Node> superBlock = Optional.ofNullable(Iterables.get(blockChain, 1, null))
.map(BlockInfo::getNodes).orElse(null);
if (block != null) {
List<? extends Node> superBlock = Iterables.get(blockChain, 1, null);
context.setSuperBlock(superBlock);

OutputList blockValueBuilder = new OutputList(config.getMaxOutputSize());

for (Node child : block.getNodes()) {
block.getParentPath().ifPresent(path -> getContext().getCurrentPathStack().push(path, lineNumber, position));

for (Node child : block) {
blockValueBuilder.addNode(child.render(this));

block.getParentPath().ifPresent(path -> getContext().getCurrentPathStack().pop());
}

blockNames.push(blockPlaceholder.getBlockName());
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/hubspot/jinjava/lib/tag/BlockTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.tree.TagNode;
import com.hubspot.jinjava.tree.output.BlockInfo;
import com.hubspot.jinjava.tree.output.BlockPlaceholderOutputNode;
import com.hubspot.jinjava.tree.output.OutputNode;
import com.hubspot.jinjava.util.HelperStringTokenizer;
Expand Down Expand Up @@ -58,7 +57,7 @@ public OutputNode interpretOutput(TagNode tagNode, JinjavaInterpreter interprete

String blockName = WhitespaceUtils.unquote(tagData.next());

interpreter.addBlock(blockName, new BlockInfo(tagNode.getChildren(), interpreter.getContext().getCurrentPathStack().peek()));
interpreter.addBlock(blockName, tagNode.getChildren());

return new BlockPlaceholderOutputNode(blockName);
}
Expand Down
27 changes: 0 additions & 27 deletions src/main/java/com/hubspot/jinjava/tree/output/BlockInfo.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -15,7 +14,6 @@
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
import com.hubspot.jinjava.interpret.TemplateError.ErrorReason;
import com.hubspot.jinjava.tree.TextNode;
import com.hubspot.jinjava.tree.output.BlockInfo;
import com.hubspot.jinjava.tree.parse.TextToken;

public class JinjavaInterpreterTest {
Expand All @@ -42,16 +40,14 @@ public void resolveBlockStubsWithMissingNamedBlock() {

@Test
public void resolveBlockStubs() {
interpreter.addBlock("foobar", new BlockInfo(Lists.newLinkedList(Lists.newArrayList((new TextNode(new TextToken("sparta", -1, -1))))),
Optional.empty()));
interpreter.addBlock("foobar", Lists.newLinkedList(Lists.newArrayList((new TextNode(new TextToken("sparta", -1, -1))))));
String content = "this is {% block foobar %}foobar{% endblock %}!";
assertThat(interpreter.render(content)).isEqualTo("this is sparta!");
}

@Test
public void resolveBlockStubsWithSpecialChars() {
interpreter.addBlock("foobar", new BlockInfo(Lists.newLinkedList(Lists.newArrayList(new TextNode(new TextToken("$150.00", -1, -1)))),
Optional.empty()));
interpreter.addBlock("foobar", Lists.newLinkedList(Lists.newArrayList(new TextNode(new TextToken("$150.00", -1, -1)))));
String content = "this is {% block foobar %}foobar{% endblock %}!";
assertThat(interpreter.render(content)).isEqualTo("this is $150.00!");
}
Expand Down
14 changes: 0 additions & 14 deletions src/test/java/com/hubspot/jinjava/lib/tag/ExtendsTagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,6 @@ public void itExtendsViaRelativePath() throws IOException {
assertThat(result).contains("This is a relative path extends");
}

@Test
public void itHandlesRelativePathsInBlocksInExtendingTemplate() throws IOException {
jinjava.getGlobalContext().put(CURRENT_PATH_CONTEXT_KEY, "relative/relative-block.jinja");
String result = jinjava.render(locator.fixture("relative/relative-block.jinja"), new HashMap<>());
assertThat(result).contains("hello");
}

@Test
public void itHandlesRelativePathsInBlocksFromExtendedTemplate() throws IOException {
jinjava.getGlobalContext().put(CURRENT_PATH_CONTEXT_KEY, "relative/relative-block-2.jinja");
String result = jinjava.render(locator.fixture("relative/relative-block-2.jinja"), new HashMap<>());
assertThat(result).contains("hello");
}

private static class ExtendsTagTestResourceLocator implements ResourceLocator {
private RelativePathResolver relativePathResolver = new RelativePathResolver();

Expand Down
1 change: 0 additions & 1 deletion src/test/resources/tags/extendstag/hello.html

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 769da25

Please sign in to comment.