-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid tag cycles when keeping track of parent paths for blocks #363
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ | |
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; | ||
|
@@ -58,7 +59,7 @@ | |
|
||
public class JinjavaInterpreter { | ||
|
||
private final Multimap<String, List<? extends Node>> blocks = ArrayListMultimap.create(); | ||
private final Multimap<String, BlockInfo> blocks = ArrayListMultimap.create(); | ||
private final LinkedList<Node> extendParentRoots = new LinkedList<>(); | ||
|
||
private Context context; | ||
|
@@ -113,8 +114,8 @@ public void addExtendParentRoot(Node root) { | |
extendParentRoots.add(root); | ||
} | ||
|
||
public void addBlock(String name, LinkedList<? extends Node> value) { | ||
blocks.put(name, value); | ||
public void addBlock(String name, BlockInfo blockInfo) { | ||
blocks.put(name, blockInfo); | ||
} | ||
|
||
/** | ||
|
@@ -282,17 +283,28 @@ private void resolveBlockStubs(OutputList output, Stack<String> blockNames) { | |
for (BlockPlaceholderOutputNode blockPlaceholder : output.getBlocks()) { | ||
|
||
if (!blockNames.contains(blockPlaceholder.getBlockName())) { | ||
Collection<List<? extends Node>> blockChain = blocks.get(blockPlaceholder.getBlockName()); | ||
List<? extends Node> block = Iterables.getFirst(blockChain, null); | ||
Collection<BlockInfo> blockChain = blocks.get(blockPlaceholder.getBlockName()); | ||
BlockInfo block = Iterables.getFirst(blockChain, null); | ||
|
||
if (block != null) { | ||
List<? extends Node> superBlock = Iterables.get(blockChain, 1, null); | ||
if (block != null && block.getNodes() != null) { | ||
List<? extends Node> superBlock = Optional.ofNullable(Iterables.get(blockChain, 1, null)) | ||
.map(BlockInfo::getNodes).orElse(null); | ||
context.setSuperBlock(superBlock); | ||
|
||
OutputList blockValueBuilder = new OutputList(config.getMaxOutputSize()); | ||
|
||
for (Node child : block) { | ||
for (Node child : block.getNodes()) { | ||
boolean pushedParentPathOntoStack = false; | ||
if (block.getParentPath().isPresent() && !getContext().getCurrentPathStack().contains(block.getParentPath().get())) { | ||
getContext().getCurrentPathStack().push(block.getParentPath().get(), lineNumber, position); | ||
pushedParentPathOntoStack = true; | ||
} | ||
|
||
blockValueBuilder.addNode(child.render(this)); | ||
|
||
if (pushedParentPathOntoStack) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
getContext().getCurrentPathStack().pop(); | ||
} | ||
} | ||
|
||
blockNames.push(blockPlaceholder.getBlockName()); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/hubspot/jinjava/tree/output/BlockInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.hubspot.jinjava.tree.output; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import com.hubspot.jinjava.tree.Node; | ||
|
||
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") | ||
public class BlockInfo { | ||
|
||
private final List<? extends Node> nodes; | ||
|
||
private final Optional<String> parentPath; | ||
|
||
public BlockInfo(List<? extends Node> nodes, Optional<String> parentPath) { | ||
this.nodes = nodes; | ||
this.parentPath = parentPath; | ||
} | ||
|
||
public List<? extends Node> getNodes() { | ||
return nodes; | ||
} | ||
|
||
public Optional<String> getParentPath() { | ||
return parentPath; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello |
3 changes: 3 additions & 0 deletions
3
src/test/resources/tags/extendstag/relative-block-2-base.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% block test %} | ||
{% include "./hello.html" %} | ||
{% endblock test %} |
1 change: 1 addition & 0 deletions
1
src/test/resources/tags/extendstag/relative/relative-block-2.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends "./../relative-block-2-base.jinja" %} |
4 changes: 4 additions & 0 deletions
4
src/test/resources/tags/extendstag/relative/relative-block.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% extends "./../super-base.html" %} | ||
{% block sidebar %} | ||
{% include "./../hello.html" %} | ||
{% endblock %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.