| | 188 | def test_rebound_namespace(self): |
| | 189 | stream = Stream([ |
| | 190 | (Stream.START_NS, ('a', 'http://example.org/'), (None, -1, -1)), |
| | 191 | (Stream.START, (QName('http://example.org/}div'), Attrs()), (None, -1, -1)), |
| | 192 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 193 | (Stream.START_NS, ('b', 'http://example.org/'), (None, -1, -1)), |
| | 194 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
| | 195 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
| | 196 | (Stream.END_NS, 'b', (None, -1, -1)), |
| | 197 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 198 | (Stream.END, QName('http://example.org/}div'), (None, -1, -1)), |
| | 199 | (Stream.END_NS, 'a', (None, -1, -1)), |
| | 200 | ]) |
| | 201 | output = stream.render(XMLSerializer) |
| | 202 | self.assertEqual("""<a:div xmlns:a="http://example.org/"> |
| | 203 | <a:p/> |
| | 204 | </a:div>""", output) |
| | 205 | |
| | 206 | def test_rebound_namespace_prefix(self): |
| | 207 | stream = Stream([ |
| | 208 | (Stream.START_NS, ('x', 'http://example.org/a'), (None, -1, -1)), |
| | 209 | (Stream.START, (QName('http://example.org/a}div'), Attrs()), (None, -1, -1)), |
| | 210 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 211 | (Stream.START_NS, ('x', 'http://example.org/b'), (None, -1, -1)), |
| | 212 | (Stream.START, (QName('http://example.org/b}p'), Attrs()), (None, -1, -1)), |
| | 213 | (Stream.END, QName('http://example.org/b}p'), (None, -1, -1)), |
| | 214 | (Stream.END_NS, 'x', (None, -1, -1)), |
| | 215 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 216 | (Stream.END, QName('http://example.org/a}div'), (None, -1, -1)), |
| | 217 | (Stream.END_NS, 'x', (None, -1, -1)), |
| | 218 | ]) |
| | 219 | output = stream.render(XMLSerializer) |
| | 220 | self.assertEqual("""<x:div xmlns:x="http://example.org/a"> |
| | 221 | <x:p xmlns:x="http://example.org/b"/> |
| | 222 | </x:div>""", output) |
| | 223 | |
| | 224 | def test_rebound_namespace_with_rebound_namespace_prefix(self): |
| | 225 | stream = Stream([ |
| | 226 | (Stream.START_NS, ('x', 'http://example.org/a'), (None, -1, -1)), |
| | 227 | (Stream.START_NS, ('y', 'http://example.org/a'), (None, -1, -1)), |
| | 228 | (Stream.START, (QName('http://example.org/a}div'), Attrs()), (None, -1, -1)), |
| | 229 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 230 | (Stream.START_NS, ('x', 'http://example.org/b'), (None, -1, -1)), |
| | 231 | (Stream.START, (QName('http://example.org/b}p'), Attrs()), (None, -1, -1)), |
| | 232 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 233 | (Stream.START, (QName('http://example.org/a}span'), Attrs()), (None, -1, -1)), |
| | 234 | (Stream.END, QName('http://example.org/a}span'), (None, -1, -1)), |
| | 235 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 236 | (Stream.END, QName('http://example.org/b}p'), (None, -1, -1)), |
| | 237 | (Stream.END_NS, 'x', (None, -1, -1)), |
| | 238 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 239 | (Stream.END, QName('http://example.org/a}div'), (None, -1, -1)), |
| | 240 | (Stream.END_NS, 'y', (None, -1, -1)), |
| | 241 | (Stream.END_NS, 'x', (None, -1, -1)), |
| | 242 | ]) |
| | 243 | output = stream.render(XMLSerializer) |
| | 244 | self.assertEqual("""<x:div xmlns:x="http://example.org/a"> |
| | 245 | <x:p xmlns:x="http://example.org/b"> |
| | 246 | <y:span xmlns:y="http://example.org/a"/> |
| | 247 | </x:p> |
| | 248 | </x:div>""", output) |
| | 249 | |
| | 250 | def test_multiple_bound_default_namespace_with_attribute(self): |
| | 251 | stream = Stream([ |
| | 252 | (Stream.START_NS, ('', 'http://example.org/a'), (None, -1, -1)), |
| | 253 | (Stream.START, (QName('http://example.org/a}div'), Attrs()), (None, -1, -1)), |
| | 254 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 255 | (Stream.START_NS, ('a', 'http://example.org/a'), (None, -1, -1)), |
| | 256 | (Stream.START_NS, ('b', 'http://example.org/b'), (None, -1, -1)), |
| | 257 | (Stream.START, (QName('http://example.org/a}div'), Attrs()), (None, -1, -1)), |
| | 258 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 259 | (Stream.START, (QName('http://example.org/b}p'), Attrs([(QName('http://example.org/a}class'), 'name')])), (None, -1, -1)), |
| | 260 | (Stream.END, QName('http://example.org/b}p'), (None, -1, -1)), |
| | 261 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 262 | (Stream.END, QName('http://example.org/a}div'), (None, -1, -1)), |
| | 263 | (Stream.END_NS, 'b', (None, -1, -1)), |
| | 264 | (Stream.END_NS, 'a', (None, -1, -1)), |
| | 265 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 266 | (Stream.END, QName('http://example.org/a}div'), (None, -1, -1)), |
| | 267 | (Stream.END_NS, '', (None, -1, -1)), |
| | 268 | ]) |
| | 269 | output = stream.render(XMLSerializer) |
| | 270 | self.assertEqual("""<div xmlns="http://example.org/a"> |
| | 271 | <div xmlns:a="http://example.org/a" xmlns:b="http://example.org/b"> |
| | 272 | <b:p a:class="name"/> |
| | 273 | </div> |
| | 274 | </div>""", output) |
| | 275 | |
| | 276 | def test_prefer_default_namespace(self): |
| | 277 | stream = Stream([ |
| | 278 | (Stream.START_NS, ('a', 'http://example.org/'), (None, -1, -1)), |
| | 279 | (Stream.START_NS, ('', 'http://example.org/'), (None, -1, -1)), |
| | 280 | (Stream.START, (QName('http://example.org/}div'), Attrs()), (None, -1, -1)), |
| | 281 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 282 | (Stream.START, (QName('http://example.org/}p'), Attrs()), (None, -1, -1)), |
| | 283 | (Stream.END, QName('http://example.org/}p'), (None, -1, -1)), |
| | 284 | (Stream.TEXT, '\n ', (None, -1, -1)), |
| | 285 | (Stream.END, QName('http://example.org/}div'), (None, -1, -1)), |
| | 286 | (Stream.END_NS, '', (None, -1, -1)), |
| | 287 | (Stream.END_NS, 'a', (None, -1, -1)), |
| | 288 | ]) |
| | 289 | output = stream.render(XMLSerializer) |
| | 290 | self.assertEqual("""<div xmlns:a="http://example.org/" xmlns="http://example.org/"> |
| | 291 | <p/> |
| | 292 | </div>""", output) |
| | 293 | |