Index: core/src/com/icesoft/faces/webapp/command/UpdateElements.java =================================================================== +++ core/src/com/icesoft/faces/webapp/command/UpdateElements.java @@ -43,12 +43,15 @@ import java.util.Arrays; import java.util.regex.Pattern; public class UpdateElements extends AbstractCommand { private final static Pattern START_CDATA = Pattern.compile("<\\!\\[CDATA\\["); private final static Pattern END_CDATA = Pattern.compile("\\]\\]>"); + private final static String SCRIPT = "script"; + private final static String SEMI_COLON = ";"; + private final static String COMMENT_START = "//"; private Element[] updates; private boolean coalesce = true; public UpdateElements(boolean coalesce, Element[] updates) { this.updates = updates; this.coalesce = coalesce; @@ -62,12 +65,15 @@ Element previousUpdate = previousUpdates[i]; boolean overriden = false; //test if any of the new updates is replacing the same element for (int j = 0; j < updates.length; j++) { Element update = updates[j]; if (update.getAttribute("id").equals(previousUpdate.getAttribute("id"))) { + if (update.getNodeName().equals(SCRIPT)) { + coalesceScripts(previousUpdate, update); + } overriden = true; break; } } //drop overriden updates if (!overriden) { coallescedUpdates.add(previousUpdate); @@ -155,7 +161,39 @@ writer.write("]]>"); } writer.write(""); } writer.write(""); } + + private void coalesceScripts(Element previousUpdate, Element currentUpdate) { + String previousUpdateScript = previousUpdate.getFirstChild().getTextContent(); + StringBuilder sb = new StringBuilder(previousUpdateScript); + int lastCommentPos = previousUpdateScript.lastIndexOf(COMMENT_START); + int lastSemicolonPos = previousUpdateScript.lastIndexOf(SEMI_COLON); + if (lastCommentPos > lastSemicolonPos) { + sb.delete(lastCommentPos, previousUpdateScript.length()); + } + String[] currentCommands = currentUpdate.getFirstChild().getTextContent().split(SEMI_COLON); + boolean commandsAdded = false; + String lastComment = null; + + for (int i = 0; i < currentCommands.length; i++) { + String command = currentCommands[i]; + if (command.startsWith(COMMENT_START)) { + lastComment = command; + } else if (!(previousUpdateScript.contains(command))) { + sb.append(command); + sb.append(SEMI_COLON); + commandsAdded = true; + } + } + + if (lastComment != null) { + sb.append(lastComment); + } + + if (commandsAdded) { + currentUpdate.getFirstChild().setTextContent(sb.toString()); + } + } }