| 422 | return s |
| 423 | |
| 424 | class NavigableString(unicode, PageElement): |
| 425 | |
| 426 | def __new__(cls, value): |
| 427 | """Create a new NavigableString. |
| 428 | |
| 429 | When unpickling a NavigableString, this method is called with |
| 430 | the string in DEFAULT_OUTPUT_ENCODING. That encoding needs to be |
| 431 | passed in to the superclass's __new__ or the superclass won't know |
| 432 | how to handle non-ASCII characters. |
| 433 | """ |
| 434 | if isinstance(value, unicode): |
| 435 | return unicode.__new__(cls, value) |
| 436 | return unicode.__new__(cls, value, DEFAULT_OUTPUT_ENCODING) |
| 437 | |
| 438 | def __getnewargs__(self): |
| 439 | return (NavigableString.__str__(self),) |
| 440 | |
| 441 | def __getattr__(self, attr): |
| 442 | """text.string gives you text. This is for backwards |
| 443 | compatibility for Navigable*String, but for CData* it lets you |
| 444 | get the string without the CData wrapper.""" |
| 445 | if attr == 'string': |
| 446 | return self |
| 447 | else: |
| 448 | raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__, attr) |
| 449 | |
| 450 | def __unicode__(self): |
| 451 | return str(self).decode(DEFAULT_OUTPUT_ENCODING) |
| 452 | |
| 453 | def __str__(self, encoding=DEFAULT_OUTPUT_ENCODING): |
| 454 | if encoding: |
| 455 | return self.encode(encoding) |
| 456 | else: |
| 457 | return self |
| 458 | |
| 459 | class CData(NavigableString): |
| 460 | |