Skip to content
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

Nested html tags use styles of all elements in stack #433

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
.classpath
/target
/.repository/
.idea
*.iml
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,13 @@
import fr.opensagres.xdocreport.document.textstyling.AbstractDocumentHandler;
import fr.opensagres.xdocreport.document.textstyling.properties.Color;
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.HeaderProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ListItemProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ListProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ParagraphProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.SpanProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.TableCellProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.TableProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.TableRowProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.TextAlignment;
import fr.opensagres.xdocreport.template.IContext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
Expand All @@ -69,7 +64,7 @@ public class DocxDocumentHandler

private Stack<ContainerProperties> paragraphsStack;

private Stack<SpanProperties> spansStack;
private Stack<ContainerProperties> spansStack;

private HyperlinkRegistry hyperlinkRegistry;

Expand Down Expand Up @@ -108,7 +103,7 @@ public void startDocument()
this.subscripting = false;
this.superscripting = false;
this.paragraphsStack = new Stack<ContainerProperties>();
this.spansStack = new Stack<SpanProperties>();
this.spansStack = new Stack<ContainerProperties>();
this.addLineBreak = 0;
}

Expand Down Expand Up @@ -233,7 +228,7 @@ public void handleString( String content )
boolean subscript = subscripting;
boolean superscript = superscripting;
Color color = null;
SpanProperties properties = getCurrentSpanProperties();
ContainerProperties properties = getCurrentProperties();
if ( properties != null )
{
// override properties declared in the span.
Expand Down Expand Up @@ -359,7 +354,7 @@ private void internalStartParagraph( ContainerProperties properties )
}
}

public void startParagraph( ParagraphProperties properties )
public void startParagraph( ContainerProperties properties )
throws IOException
{
processPageBreakBefore( properties );
Expand Down Expand Up @@ -410,7 +405,7 @@ public void endParagraph()
processPageBreakAfter( paragraphsStack.pop() );
}

public void startListItem( ListItemProperties properties )
public void startListItem( ContainerProperties properties )
throws IOException
{
// Close current paragraph
Expand Down Expand Up @@ -517,7 +512,7 @@ public void endListItem()
// endParagraph();
}

public void startHeading( int level, HeaderProperties properties )
public void startHeading( int level, ContainerProperties properties )
throws IOException
{
// Close current paragraph
Expand All @@ -543,7 +538,7 @@ public void endHeading( int level )
insideHeader = false;
}

public void startSpan( SpanProperties properties )
public void startSpan( ContainerProperties properties )
throws IOException
{
spansStack.push( properties );
Expand All @@ -557,11 +552,26 @@ public void startSpan( SpanProperties properties )
// super.write( "</w:r>" );
}

private SpanProperties getCurrentSpanProperties()
/**
* Collects all active span properties
* @return
*/
private ContainerProperties getCurrentProperties()
{
if ( !spansStack.isEmpty() )
{
return spansStack.peek();
// block elements first, then inline elements
List<ContainerProperties> propertiesList = new ArrayList<ContainerProperties>(paragraphsStack);
propertiesList.addAll( spansStack );
ContainerProperties result = null;
for(ContainerProperties properties : propertiesList) {
if (result == null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incnsistent formatting (whitespace around braces, curly braces in new line)

result = properties;
} else {
result = ContainerProperties.combine( result, properties );
}
}
return result;
}
return null;
}
Expand All @@ -573,7 +583,7 @@ public void endSpan()
}

@Override
protected void doStartOrderedList( ListProperties properties )
protected void doStartOrderedList( ContainerProperties properties )
throws IOException
{
if(this.addLineBreak>0)
Expand All @@ -593,7 +603,7 @@ protected void doStartOrderedList( ListProperties properties )
}

@Override
protected void doStartUnorderedList( ListProperties properties )
protected void doStartUnorderedList( ContainerProperties properties )
throws IOException
{
if(this.addLineBreak>0)
Expand Down Expand Up @@ -767,7 +777,7 @@ public void doEndTable( TableProperties properties )
super.write( "<w:p/>" );
}

public void doStartTableRow( TableRowProperties properties )
public void doStartTableRow( ContainerProperties properties )
throws IOException
{
super.write( "<w:tr>" );
Expand All @@ -779,7 +789,7 @@ public void doEndTableRow()
super.write( "</w:tr>" );
}

public void doStartTableCell( TableCellProperties properties )
public void doStartTableCell( ContainerProperties properties )
throws IOException
{
super.write( "<w:tc>" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,22 @@ public void testStrikeWithStrike()
Assert.assertEquals( "", handler.getTextEnd() );
}

@Test
public void testBoldAndItalicSpan()
throws Exception
{
IContext context = new MockContext();

ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE;
IDocumentHandler handler = new DocxDocumentHandler( null, context, "word/document.xml" );
formatter.transform( "<span style=\"font-weight:bold;\"><span style=\"font-style: italic;\">text</span></span>", handler );

Assert.assertEquals( "", handler.getTextBefore() );
Assert.assertEquals( "<w:r><w:rPr><w:b /><w:i /></w:rPr><w:t xml:space=\"preserve\" >text</w:t></w:r>",
handler.getTextBody() );
Assert.assertEquals( "", handler.getTextEnd() );
}

@Test
public void testHyperlinkByUsingXDocReport_HyperlinkStyle()
throws Exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.regex.Pattern;

import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties.ContainerType;
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerType;
import fr.opensagres.xdocreport.document.textstyling.properties.TextAlignment;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,8 @@
import fr.opensagres.xdocreport.document.odt.template.ODTContextHelper;
import fr.opensagres.xdocreport.document.preprocessor.sax.BufferedElement;
import fr.opensagres.xdocreport.document.textstyling.AbstractDocumentHandler;
import fr.opensagres.xdocreport.document.textstyling.IDocumentHandler.TextLocation;
import fr.opensagres.xdocreport.document.textstyling.properties.HeaderProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ListItemProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ListProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ParagraphProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.SpanProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.TableCellProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.TableProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.TableRowProperties;
import fr.opensagres.xdocreport.template.IContext;

public class ODTDocumentHandler
Expand Down Expand Up @@ -263,7 +256,7 @@ private void startParagraphIfNeeded()
}
}

public void startParagraph( ParagraphProperties properties )
public void startParagraph( ContainerProperties properties )
throws IOException
{
if ( paragraphsStack.isEmpty() || !paragraphsStack.peek() )
Expand All @@ -283,7 +276,7 @@ public void endParagraph()
// }
}

private void internalStartParagraph( boolean containerIsList, ParagraphProperties properties )
private void internalStartParagraph( boolean containerIsList, ContainerProperties properties )
throws IOException
{
String styleName = null;
Expand Down Expand Up @@ -350,7 +343,7 @@ private void internalEndParagraph()
}
}

public void startHeading( int level, HeaderProperties properties )
public void startHeading( int level, ContainerProperties properties )
throws IOException
{
endParagraphIfNeeded();
Expand All @@ -371,7 +364,7 @@ public void endHeading( int level )
}

@Override
protected void doStartOrderedList( ListProperties properties )
protected void doStartOrderedList( ContainerProperties properties )
throws IOException
{
internalStartList( styleGen.getOLStyleName() );
Expand All @@ -385,7 +378,7 @@ protected void doEndOrderedList()
}

@Override
protected void doStartUnorderedList( ListProperties properties )
protected void doStartUnorderedList( ContainerProperties properties )
throws IOException
{
internalStartList( styleGen.getULStyleName() );
Expand Down Expand Up @@ -440,7 +433,7 @@ protected void internalEndList()
}
}

public void startListItem( ListItemProperties properties )
public void startListItem( ContainerProperties properties )
throws IOException
{
if ( itemStyle != null )
Expand Down Expand Up @@ -470,7 +463,7 @@ public void endListItem()
super.write( "</text:list-item>" );
}

public void startSpan( SpanProperties properties )
public void startSpan( ContainerProperties properties )
throws IOException
{
internalStartSpan( styleGen.getTextStyleName( properties ), true );
Expand Down Expand Up @@ -532,7 +525,7 @@ public void doEndTable( TableProperties properties )
super.write( "</table:table>" );
}

protected void doStartTableRow( TableRowProperties properties )
protected void doStartTableRow( ContainerProperties properties )
throws IOException
{
super.write( "<table:table-row>" );
Expand All @@ -544,7 +537,7 @@ protected void doEndTableRow()
super.write( "</table:table-row>" );
}

protected void doStartTableCell( TableCellProperties properties )
protected void doStartTableCell( ContainerProperties properties )
throws IOException
{
super.write( "<table:table-cell>" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.logging.Logger;

import fr.opensagres.xdocreport.document.textstyling.properties.ContainerProperties;
import org.wikimodel.wem.EmptyWemListener;
import org.wikimodel.wem.IWemConstants;
import org.wikimodel.wem.WikiFormat;
Expand All @@ -37,9 +38,6 @@

import fr.opensagres.xdocreport.core.logging.LogUtils;
import fr.opensagres.xdocreport.document.textstyling.IDocumentHandler;
import fr.opensagres.xdocreport.document.textstyling.properties.ListItemProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ListProperties;
import fr.opensagres.xdocreport.document.textstyling.properties.ParagraphProperties;

/**
* Wiki Event Model Adaptor to call methods of {@link IDocumentHandler}.
Expand Down Expand Up @@ -136,7 +134,7 @@ public void beginList( WikiParameters params, boolean ordered )
{
try
{
ListProperties properties = null;
ContainerProperties properties = null;
if ( ordered )
{
documentHandler.startOrderedList( properties );
Expand All @@ -157,7 +155,7 @@ public void beginListItem()
{
try
{
ListItemProperties properties = null;
ContainerProperties properties = null;
documentHandler.startListItem( properties );
}
catch ( IOException e )
Expand Down Expand Up @@ -230,7 +228,7 @@ public void beginParagraph( WikiParameters params )
{
try
{
ParagraphProperties properties = null;
ContainerProperties properties = null;
documentHandler.startParagraph( properties );
}
catch ( IOException e )
Expand Down
Loading