|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.parser.iwork; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.xml.sax.helpers.AttributesImpl; |
| 23 | + |
| 24 | +import org.apache.tika.metadata.Metadata; |
| 25 | +import org.apache.tika.parser.ParseContext; |
| 26 | +import org.apache.tika.sax.StrictXHTMLValidator; |
| 27 | +import org.apache.tika.sax.ToXMLContentHandler; |
| 28 | +import org.apache.tika.sax.XHTMLContentHandler; |
| 29 | + |
| 30 | +public class KeynoteContentHandlerTest { |
| 31 | + |
| 32 | + /** |
| 33 | + * TIKA-4744: Keynote tables whose final row contains fewer cells than |
| 34 | + * numberOfColumns left {@code <tr>} open when {@code </sf:tabular-model>} |
| 35 | + * fired {@code </table>}. Drive the handler with a synthetic 3-column |
| 36 | + * table whose final row has only 2 cells and assert the XHTML stays |
| 37 | + * balanced through endDocument. |
| 38 | + */ |
| 39 | + @Test |
| 40 | + public void testIncompleteFinalRowClosesTr() throws Exception { |
| 41 | + Metadata md = new Metadata(); |
| 42 | + ToXMLContentHandler xml = new ToXMLContentHandler(); |
| 43 | + XHTMLContentHandler xhtml = new XHTMLContentHandler( |
| 44 | + new StrictXHTMLValidator(xml), md, new ParseContext()); |
| 45 | + KeynoteContentHandler h = new KeynoteContentHandler(xhtml, md); |
| 46 | + |
| 47 | + xhtml.startDocument(); |
| 48 | + |
| 49 | + // <key:slide> |
| 50 | + h.startElement("", "slide", "key:slide", new AttributesImpl()); |
| 51 | + // <sf:tabular-model sfa:ID="t1"> |
| 52 | + AttributesImpl tabAttrs = new AttributesImpl(); |
| 53 | + tabAttrs.addAttribute("", "sfa:ID", "sfa:ID", "CDATA", "t1"); |
| 54 | + h.startElement("", "tabular-model", "sf:tabular-model", tabAttrs); |
| 55 | + // <sf:columns sf:count="3"> |
| 56 | + AttributesImpl colAttrs = new AttributesImpl(); |
| 57 | + colAttrs.addAttribute("", "sf:count", "sf:count", "CDATA", "3"); |
| 58 | + h.startElement("", "columns", "sf:columns", colAttrs); |
| 59 | + h.endElement("", "columns", "sf:columns"); |
| 60 | + // Two cells (less than 3) -> incomplete final row. |
| 61 | + AttributesImpl cell = new AttributesImpl(); |
| 62 | + cell.addAttribute("", "sfa:s", "sfa:s", "CDATA", "A"); |
| 63 | + h.startElement("", "ct", "sf:ct", cell); |
| 64 | + h.endElement("", "ct", "sf:ct"); |
| 65 | + cell = new AttributesImpl(); |
| 66 | + cell.addAttribute("", "sfa:s", "sfa:s", "CDATA", "B"); |
| 67 | + h.startElement("", "ct", "sf:ct", cell); |
| 68 | + h.endElement("", "ct", "sf:ct"); |
| 69 | + // </sf:tabular-model> -- without the fix this emits </table> while |
| 70 | + // <tr> is still topmost and StrictXHTMLValidator throws. |
| 71 | + h.endElement("", "tabular-model", "sf:tabular-model"); |
| 72 | + // </key:slide> |
| 73 | + h.endElement("", "slide", "key:slide"); |
| 74 | + |
| 75 | + xhtml.endDocument(); |
| 76 | + |
| 77 | + String out = xml.toString(); |
| 78 | + // Sanity: the cells emitted, the row closed, and the table closed. |
| 79 | + assertTrue(out.contains("<td>A</td>"), "expected td A; got: " + out); |
| 80 | + assertTrue(out.contains("<td>B</td>"), "expected td B; got: " + out); |
| 81 | + assertTrue(out.contains("</tr>"), "expected </tr>; got: " + out); |
| 82 | + assertTrue(out.contains("</table>"), "expected </table>; got: " + out); |
| 83 | + } |
| 84 | +} |
0 commit comments