Skip to content

Commit

Permalink
Update Media upload processor to support self-closing block tags
Browse files Browse the repository at this point in the history
  • Loading branch information
fluiddot committed Apr 19, 2023
1 parent c161adc commit f9a686c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.regex.Matcher;

import static org.wordpress.android.ui.posts.mediauploadcompletionprocessors.MediaUploadCompletionProcessorPatterns.PATTERN_BLOCK_CAPTURES;
import static org.wordpress.android.ui.posts.mediauploadcompletionprocessors.MediaUploadCompletionProcessorPatterns.PATTERN_SELF_CLOSING_BLOCK_CAPTURES;

/**
* Abstract class to be extended for each enumerated {@link MediaBlockType}.
Expand Down Expand Up @@ -62,16 +63,18 @@ private Document parseHTML(String blockContent) {
return document;
}

private boolean splitBlock(String block) {
Matcher captures = PATTERN_BLOCK_CAPTURES.matcher(block);
private boolean splitBlock(String block, Boolean isSelfClosingTag) {
Matcher captures = (
isSelfClosingTag ? PATTERN_SELF_CLOSING_BLOCK_CAPTURES : PATTERN_BLOCK_CAPTURES
).matcher(block);

boolean capturesFound = captures.find();

if (capturesFound) {
mBlockName = captures.group(1);
mJsonAttributes = parseJson(captures.group(2));
mBlockContentDocument = parseHTML(captures.group(3));
mClosingComment = captures.group(4);
mBlockContentDocument = isSelfClosingTag ? null : parseHTML(captures.group(3));
mClosingComment = isSelfClosingTag ? null : captures.group(4);
return true;
} else {
mBlockName = null;
Expand All @@ -87,12 +90,22 @@ private boolean splitBlock(String block) {
* method should return the original block contents unchanged.
*
* @param block The raw block contents
* @param isSelfClosingTag True if the block tag is self-closing (e.g. <!-- wp:videopress/video {"id":100} /-->)
* @return A string containing content with ids and urls replaced
*/
String processBlock(String block) {
if (splitBlock(block)) {
String processBlock(String block, Boolean isSelfClosingTag) {
if (splitBlock(block, isSelfClosingTag)) {
if (processBlockJsonAttributes(mJsonAttributes)) {
if (processBlockContentDocument(mBlockContentDocument)) {
if (isSelfClosingTag) {
// return injected block
return new StringBuilder()
.append("<!-- wp:")
.append(mBlockName)
.append(" ")
.append(mJsonAttributes) // json parser output
.append(" /-->")
.toString();
} else if (processBlockContentDocument(mBlockContentDocument)) {
// return injected block
return new StringBuilder()
.append("<!-- wp:")
Expand All @@ -112,6 +125,10 @@ String processBlock(String block) {
return block;
}

String processBlock(String block) {
return processBlock(block, false);
}

/**
* All concrete implementations must implement this method for the particular block type. The document represents
* the html contents of the block to be processed, and is to be mutated in place.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,30 @@ public String processContent(String content) {
if (headerMatcher.find()) {
positionBlockStart = headerMatcher.start();
String blockType = headerMatcher.group(1);
Matcher blockBoundaryMatcher = Pattern.compile(String.format(PATTERN_TEMPLATE_BLOCK_BOUNDARY, blockType),
Pattern.DOTALL).matcher(content.substring(headerMatcher.end()));
String blockTagSuffix = headerMatcher.group(2);
Boolean isSelfClosingTag = blockTagSuffix.equals("/-->");
if (isSelfClosingTag) {
positionBlockEnd = headerMatcher.end();
} else {
Matcher blockBoundaryMatcher =
Pattern.compile(String.format(PATTERN_TEMPLATE_BLOCK_BOUNDARY, blockType),
Pattern.DOTALL).matcher(content.substring(headerMatcher.end()));

int nestLevel = 1;
int nestLevel = 1;

while (0 < nestLevel && blockBoundaryMatcher.find()) {
if (blockBoundaryMatcher.group(1).equals("/")) {
positionBlockEnd = headerMatcher.end() + blockBoundaryMatcher.end();
nestLevel--;
} else {
nestLevel++;
while (0 < nestLevel && blockBoundaryMatcher.find()) {
if (blockBoundaryMatcher.group(1).equals("/")) {
positionBlockEnd = headerMatcher.end() + blockBoundaryMatcher.end();
nestLevel--;
} else {
nestLevel++;
}
}
}

return new StringBuilder()
.append(content.substring(0, positionBlockStart))
.append(processBlock(content.substring(positionBlockStart, positionBlockEnd)))
.append(processBlock(content.substring(positionBlockStart, positionBlockEnd), isSelfClosingTag))
.append(processContent(content.substring(positionBlockEnd)))
.toString();
} else {
Expand All @@ -70,12 +77,12 @@ public String processContent(String content) {
* @param block The raw block contents
* @return A string containing content with ids and urls replaced
*/
private String processBlock(String block) {
private String processBlock(String block, Boolean isSelfClosingTag) {
final MediaBlockType blockType = MediaBlockType.detectBlockType(block);
final BlockProcessor blockProcessor = mBlockProcessorFactory.getProcessorForMediaBlockType(blockType);

if (blockProcessor != null) {
return blockProcessor.processBlock(block);
return blockProcessor.processBlock(block, isSelfClosingTag);
}

return block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MediaUploadCompletionProcessorPatterns {
public static final Pattern PATTERN_BLOCK_HEADER = Pattern.compile(new StringBuilder()
.append(PATTERN_BLOCK_PREFIX)
.append(MediaBlockType.getMatchingGroup())
.append(").*? -->\n?")
.append(").*? (/?-->)\n?")
.toString(), Pattern.DOTALL);

/**
Expand Down Expand Up @@ -39,4 +39,11 @@ public class MediaUploadCompletionProcessorPatterns {
.append("(.*)") // group: html content
.append("(<!-- /wp:\\1 -->.*)") // group: closing-comment (name must match group 1: block type)
.toString(), Pattern.DOTALL);

public static final Pattern PATTERN_SELF_CLOSING_BLOCK_CAPTURES = Pattern.compile(new StringBuilder()
.append(PATTERN_BLOCK_PREFIX) // start-of-group: block type
.append(MediaBlockType.getMatchingGroup())
.append(")") // end-of-group: block type
.append(" (\\{.*?\\}) /-->\n?") // group: block header json
.toString(), Pattern.DOTALL);
}

0 comments on commit f9a686c

Please sign in to comment.