| 447 | |
| 448 | |
| 449 | class NavigableString(unicode, PageElement): |
| 450 | |
| 451 | def __new__(cls, value): |
| 452 | """Create a new NavigableString. |
| 453 | |
| 454 | When unpickling a NavigableString, this method is called with |
| 455 | the string in DEFAULT_OUTPUT_ENCODING. That encoding needs to be |
| 456 | passed in to the superclass's __new__ or the superclass won't know |
| 457 | how to handle non-ASCII characters. |
| 458 | """ |
| 459 | if isinstance(value, unicode): |
| 460 | return unicode.__new__(cls, value) |
| 461 | return unicode.__new__(cls, value, DEFAULT_OUTPUT_ENCODING) |
| 462 | |
| 463 | def __getnewargs__(self): |
| 464 | return (NavigableString.__str__(self),) |
| 465 | |
| 466 | def __getattr__(self, attr): |
| 467 | """text.string gives you text. This is for backwards |
| 468 | compatibility for Navigable*String, but for CData* it lets you |
| 469 | get the string without the CData wrapper.""" |
| 470 | if attr == 'string': |
| 471 | return self |
| 472 | else: |
| 473 | raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__, attr) |
| 474 | |
| 475 | def __unicode__(self): |
| 476 | return str(self).decode(DEFAULT_OUTPUT_ENCODING) |
| 477 | |
| 478 | def __str__(self, encoding=DEFAULT_OUTPUT_ENCODING): |
| 479 | # Substitute outgoing XML entities. |
| 480 | data = self.BARE_AMPERSAND_OR_BRACKET.sub(self._sub_entity, self) |
| 481 | if encoding: |
| 482 | return data.encode(encoding) |
| 483 | else: |
| 484 | return data |
| 485 | |
| 486 | class CData(NavigableString): |
| 487 |
no outgoing calls
no test coverage detected
searching dependent graphs…