Skip to content

Commit

Permalink
Merge pull request #37 from nabedge/issue0011
Browse files Browse the repository at this point in the history
fix #11
  • Loading branch information
nabedge committed Apr 14, 2016
2 parents 0f57f0f + f04e016 commit 658a497
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/org/mixer2/Mixer2Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.commons.logging.LogFactory;
import org.mixer2.jaxb.exception.Mixer2JAXBException;
import org.mixer2.jaxb.xhtml.Html;
import org.mixer2.jaxb.xhtml.Iframe;
import org.mixer2.jaxb.xhtml.Script;
import org.mixer2.jaxb.xhtml.Textarea;
import org.mixer2.xhtml.AbstractJaxb;
Expand Down Expand Up @@ -239,6 +240,13 @@ public <T extends AbstractJaxb> void saveToStringWriter(T tag,
script.setContent(" ");
}
}

// add one white space into iframe tag having empty content.
for (Iframe iframe : tag.getDescendants(Iframe.class)) {
if (iframe.getContent() == null || iframe.getContent().size() < 1) {
iframe.getContent().add(" ");
}
}

// add one line break into textarea tag
// having empty content.
Expand Down Expand Up @@ -399,6 +407,17 @@ protected StringBuilder inputStreamToStringBuilder(InputStream inputStream)
return stringBuilder;
}

protected StringBuilder replaceIframeEndTag(StringBuilder sb) {
if (sb == null) {
return null;
}
String patternStr = "</iframe>";
String replaceStr = " </iframe>";
Pattern ptn = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE);
String result = ptn.matcher(sb).replaceAll(replaceStr);
return new StringBuilder(result);
}

/**
* <p>
* unmarshal from html string to html object.
Expand All @@ -410,6 +429,7 @@ protected StringBuilder inputStreamToStringBuilder(InputStream inputStream)
*/
protected Html unmarshal(StringBuilder sb) throws JAXBException {
Html html = null;
sb = replaceIframeEndTag(sb);
sb = removeDoctypeDeclaration(sb);
sb = replaceNamedEntity(sb);
StringReader stringReader = new StringReader(sb.toString());
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/org/mixer2/issues/Issue0011.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.mixer2.issues;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.mixer2.Mixer2Engine;
import org.mixer2.jaxb.xhtml.*;
import org.mixer2.xhtml.Mixer2EngineSingleton;
import org.mixer2.xhtml.TagCreator;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* see https://github.com/nabedge/mixer2/issues/11
*
* @author nabedge
*
*/
public class Issue0011 {

private static Log log = LogFactory.getLog(Issue0011.class);

private static Mixer2Engine m2e = Mixer2EngineSingleton.getInstance();

@Test
public void test() throws Exception {
StringBuilder sb = new StringBuilder();
sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
sb.append("<head>");
sb.append("<title>test</title>");
sb.append("</head>");
sb.append("<body>");
sb.append("<iframe>");
sb.append("</iframe>");
sb.append("</body>");
sb.append("</html>");
String str = sb.toString();
Html html = m2e.loadHtmlTemplate(str);
String result = m2e.saveToString(html);
//log.info(result);
assertThat(result.contains("<iframe>"), is(true));
assertThat(result.contains("</iframe>"), is(true));
}

@Test
public void test2() throws Exception {
StringBuilder sb = new StringBuilder();
sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
sb.append("<head>");
sb.append("<title>test</title>");
sb.append("</head>");
sb.append("<body>");
sb.append("<iframe id=\"fooId\" src=\"barSrc\"><p>foo</p></iframe>");
sb.append("</body>");
sb.append("</html>");
String str = sb.toString();
Html html = m2e.loadHtmlTemplate(str);
String result = m2e.saveToString(html);
//log.info(result);
assertThat(result.contains("<iframe "), is(true));
assertThat(result.contains("id=\"fooId\""), is(true));
assertThat(result.contains("src=\"barSrc\""), is(true));
assertThat(result.contains("<p>foo</p>"), is(true));
assertThat(result.contains("</iframe>"), is(true));
}

@Test
public void test3() throws Exception {
Iframe iframe = TagCreator.iframe();
String iframeStr = m2e.saveToString(iframe);
//log.info(iframeStr);
assertThat(iframeStr.matches("<iframe>.*</iframe>"), is(true));
// assertThat(m2e.saveToString(iframe).matches("<iframe/>"), is(true));
}

@Test
public void test4() throws Exception {
Iframe iframe = TagCreator.iframe();
iframe.setId("foo");
String iframeStr = m2e.saveToString(iframe);
assertThat(iframeStr.matches("<iframe id=\"foo\">.*</iframe>"), is(true));
// assertThat(m2e.saveToString(iframe).matches("<iframe/>"), is(true));
}

}

0 comments on commit 658a497

Please sign in to comment.