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