FROM : Douglas Davidson
DATE : Thu Jul 20 18:36:57 2006
On Jul 19, 2006, at 9:25 PM, David Dunham wrote:
> I'm trying to output a string suitable for using in HTML -- no
> troublesome characters like < and >.
>
> For some reason there's no stringByAddingHTMLEntities, so I came up
> with
>
> NSArray* excluded = [NSArray arrayWithObjects: @"doctype",
> @"html", @"head", @"body", @"xml", @"font", @"span", @"p", nil];
> NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys:
> NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute,
> aTitle, NSTitleDocumentAttribute,
> excluded, NSExcludedElementsDocumentAttribute,
> [NSNumber numberWithInt: NSASCIIStringEncoding],
> NSCharacterEncodingDocumentAttribute,
> nil];
>
> NSAttributedString* as = [[NSAttributedString alloc]
> initWithString: tString];
> NSData* tData = [as dataFromRange: NSMakeRange(0, [tString
> length]) documentAttributes: attr error: nil];
>
> which almost works. However, a quote " is not converted to "
> like I'd expect. Is there an element I missed here, or do I just
> have to my own pass on the string? (OK, it's probably a single
> replaceOccurrencesOfString:withString:options:range: message, but...)
>
Using NSAttributedString's HTML generation should work, although it's
a bit heavyweight if you don't actually want any markup.
The use of " for quotation marks is not required in HTML--the
standard says (http://www.w3.org/TR/REC-html40/charset.html)
Some authors use the character entity reference """ to
encode instances of the double quote mark (") since that character
may be used to delimit attribute values.
The source HTML in the document uses plain double quote marks rather
than ", as follows:
<p>Some authors use the character entity reference "&quot;"
to encode instances of the double quote mark (") since that character
may be used to delimit attribute values.</p>
Quotation marks are required to delimit attribute values in XHTML,
but they can still be used normally elsewhere. You would need to
perform an additional substitution pass if you wished to replace
quotation marks with ".
Douglas Davidson
DATE : Thu Jul 20 18:36:57 2006
On Jul 19, 2006, at 9:25 PM, David Dunham wrote:
> I'm trying to output a string suitable for using in HTML -- no
> troublesome characters like < and >.
>
> For some reason there's no stringByAddingHTMLEntities, so I came up
> with
>
> NSArray* excluded = [NSArray arrayWithObjects: @"doctype",
> @"html", @"head", @"body", @"xml", @"font", @"span", @"p", nil];
> NSDictionary* attr = [NSDictionary dictionaryWithObjectsAndKeys:
> NSHTMLTextDocumentType, NSDocumentTypeDocumentAttribute,
> aTitle, NSTitleDocumentAttribute,
> excluded, NSExcludedElementsDocumentAttribute,
> [NSNumber numberWithInt: NSASCIIStringEncoding],
> NSCharacterEncodingDocumentAttribute,
> nil];
>
> NSAttributedString* as = [[NSAttributedString alloc]
> initWithString: tString];
> NSData* tData = [as dataFromRange: NSMakeRange(0, [tString
> length]) documentAttributes: attr error: nil];
>
> which almost works. However, a quote " is not converted to "
> like I'd expect. Is there an element I missed here, or do I just
> have to my own pass on the string? (OK, it's probably a single
> replaceOccurrencesOfString:withString:options:range: message, but...)
>
Using NSAttributedString's HTML generation should work, although it's
a bit heavyweight if you don't actually want any markup.
The use of " for quotation marks is not required in HTML--the
standard says (http://www.w3.org/TR/REC-html40/charset.html)
Some authors use the character entity reference """ to
encode instances of the double quote mark (") since that character
may be used to delimit attribute values.
The source HTML in the document uses plain double quote marks rather
than ", as follows:
<p>Some authors use the character entity reference "&quot;"
to encode instances of the double quote mark (") since that character
may be used to delimit attribute values.</p>
Quotation marks are required to delimit attribute values in XHTML,
but they can still be used normally elsewhere. You would need to
perform an additional substitution pass if you wished to replace
quotation marks with ".
Douglas Davidson
| Related mails | Author | Date |
|---|---|---|
| David Dunham | Jul 20, 06:25 | |
| Douglas Davidson | Jul 20, 18:36 | |
| David Dunham | Jul 20, 19:18 | |
| Douglas Davidson | Jul 20, 20:12 | |
| Peter Bierman | Jul 20, 20:41 | |
| Norman Gray | Jul 21, 00:13 | |
| David Dunham | Jul 21, 00:26 | |
| Norman Gray | Jul 21, 11:40 |






Cocoa mail archive

