|
1 | 1 | # encoding: UTF-8 |
2 | 2 | require 'test/unit' |
3 | | -require './test_helper' |
| 3 | +require 'test_helper' |
4 | 4 |
|
5 | 5 | class TestStylesheet < Test::Unit::TestCase |
6 | 6 | def setup |
@@ -103,4 +103,112 @@ def test_result_ownership |
103 | 103 | GC.start |
104 | 104 | end |
105 | 105 | end |
| 106 | + |
| 107 | + #RAF# |
| 108 | + def test_stylesheet_string |
| 109 | + filename = File.join(File.dirname(__FILE__), 'files/params.xsl') |
| 110 | + style = File.open(filename).readline(nil) |
| 111 | + stylesheet = XSLT::Stylesheet.string(style) |
| 112 | + assert_instance_of(XSLT::Stylesheet, stylesheet) |
| 113 | + end |
| 114 | + |
| 115 | + def test_stylesheet_file |
| 116 | + filename = File.join(File.dirname(__FILE__), 'files/params.xsl') |
| 117 | + stylesheet = XSLT::Stylesheet.file(filename) |
| 118 | + assert_instance_of(XSLT::Stylesheet, stylesheet) |
| 119 | + end |
| 120 | + |
| 121 | + def test_stylesheet_io |
| 122 | + filename = File.join(File.dirname(__FILE__), 'files/params.xsl') |
| 123 | + stylesheet = XSLT::Stylesheet.io(File.open(filename)) |
| 124 | + assert_instance_of(XSLT::Stylesheet, stylesheet) |
| 125 | + end |
| 126 | + |
| 127 | + def test_output |
| 128 | + filename = File.join(File.dirname(__FILE__), 'files/fuzface.xsl') |
| 129 | + sdoc = XML::Document.file(filename) |
| 130 | + stylesheet = XSLT::Stylesheet.new(sdoc) |
| 131 | + |
| 132 | + rdoc = stylesheet.apply(doc) |
| 133 | + |
| 134 | + xml = stylesheet.output(rdoc) |
| 135 | + |
| 136 | + # output method is html -> no xml decl, empty tags not closed... |
| 137 | + assert xml =~ /^<html>/ |
| 138 | + assert xml =~ /<meta http-equiv="Content-Type" content="text\/html; charset=UTF-8">\n<title>/ |
| 139 | + end |
| 140 | + |
| 141 | + def test_transform |
| 142 | + filename = File.join(File.dirname(__FILE__), 'files/fuzface.xsl') |
| 143 | + sdoc = XML::Document.file(filename) |
| 144 | + stylesheet = XSLT::Stylesheet.new(sdoc) |
| 145 | + |
| 146 | + xml = stylesheet.transform(doc) |
| 147 | + |
| 148 | + assert xml =~ /<meta http-equiv="Content-Type" content="text\/html; charset=UTF-8">\n<title>/ |
| 149 | + end |
| 150 | + |
| 151 | + def test_entities |
| 152 | + style = <<EOF |
| 153 | +<!DOCTYPE xsl:stylesheet [ |
| 154 | +<!ENTITY foo 'bar'> |
| 155 | +]> |
| 156 | +<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 157 | + <xsl:template match="a"> |
| 158 | + <out><xsl:text>&foo;</xsl:text> |
| 159 | + <xsl:apply-templates/></out> |
| 160 | + </xsl:template> |
| 161 | +</xsl:stylesheet> |
| 162 | +EOF |
| 163 | + |
| 164 | + styledoc = LibXML::XML::Parser.string(style, :options => XSLT::Stylesheet::PARSE_OPTIONS).parse |
| 165 | + stylesheet = XSLT::Stylesheet.new(styledoc) |
| 166 | + |
| 167 | + xml = "<!DOCTYPE a [<!ENTITY bla 'fasel'>]><a>&bla;</a>" |
| 168 | + doc = XML::Parser.string(xml, :options => XSLT::Stylesheet::PARSE_OPTIONS).parse |
| 169 | + |
| 170 | + out = stylesheet.apply( doc ) |
| 171 | + dump = stylesheet.output( out ) |
| 172 | + assert_match( /<out>barfasel<\/out>/, dump) |
| 173 | + |
| 174 | + # no entity replacement in document |
| 175 | + doc = XML::Parser.string(xml, :options => 0).parse |
| 176 | + out = stylesheet.apply( doc ) |
| 177 | + dump = stylesheet.output( out ) |
| 178 | + |
| 179 | + assert_match(/<out>bar<\/out>/, dump) # entity content is missing |
| 180 | + |
| 181 | + # note: having entities in your stylesheet that are not replaced during |
| 182 | + # parse, will crash libxslt (segfault) |
| 183 | + # seems to be a libxslt problem; you should not do that anyway |
| 184 | + # styledoc = LibXML::XML::Parser.string(style, : options => 0).parse |
| 185 | + # stylesheet = XSLT::Stylesheet.new(styledoc) |
| 186 | + end |
| 187 | + |
| 188 | + def test_cdatasection |
| 189 | + doc = XML::Parser.string("<a/>").parse |
| 190 | + |
| 191 | + style = <<EOF |
| 192 | +<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 193 | + <xsl:template match="a"> |
| 194 | + <out><xsl:text disable-output-escaping="yes"><![CDATA[<>]]></xsl:text></out> |
| 195 | + </xsl:template> |
| 196 | +</xsl:stylesheet> |
| 197 | +EOF |
| 198 | + |
| 199 | + styledoc = LibXML::XML::Parser.string(style, :options => XSLT::Stylesheet::PARSE_OPTIONS).parse |
| 200 | + stylesheet = XSLT::Stylesheet.new(styledoc) |
| 201 | + |
| 202 | + out = stylesheet.apply( doc ) |
| 203 | + dump = stylesheet.output( out ) |
| 204 | + assert_match( /<out><><\/out>/, dump) |
| 205 | + |
| 206 | + # without propper parse options (result is wrong from an xml/xslt point of view) |
| 207 | + styledoc = LibXML::XML::Parser.string(style).parse |
| 208 | + stylesheet = XSLT::Stylesheet.new(styledoc) |
| 209 | + |
| 210 | + out = stylesheet.apply( doc ) |
| 211 | + dump = stylesheet.output( out ) |
| 212 | + assert_match( /<out><><\/out>/, dump) |
| 213 | + end |
106 | 214 | end |
0 commit comments