Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 139 lines (107 sloc) 4.493 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
//
// NSCharacterSet+HTML.m
// DTCoreText
//
// Created by Oliver Drobnik on 1/15/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//

#import "NSCharacterSet+HTML.h"
#import "DTCoreTextConstants.h"

static NSCharacterSet *_tagNameCharacterSet = nil;
static NSCharacterSet *_ignorableWhitespaceCharacterSet = nil;
static NSCharacterSet *_tagAttributeNameCharacterSet = nil;
static NSCharacterSet *_quoteCharacterSet = nil;
static NSCharacterSet *_nonQuotedAttributeEndCharacterSet = nil;
static NSCharacterSet *_cssStyleAttributeNameCharacterSet = nil;
static NSCharacterSet *_cssLengthValueCharacterSet = nil;
static NSCharacterSet *_cssLengthUnitCharacterSet = nil;



@implementation NSCharacterSet (HTML)

+ (NSCharacterSet *)tagNameCharacterSet
{
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
_tagNameCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"];
});

return _tagNameCharacterSet;
}

+ (NSCharacterSet *)tagAttributeNameCharacterSet
{
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
_tagAttributeNameCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"-_:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"];
});

return _tagAttributeNameCharacterSet;
}

+ (NSCharacterSet *)ignorableWhitespaceCharacterSet
{
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
NSMutableCharacterSet *tmpSet = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];

// remove all special unicode space characters
[tmpSet removeCharactersInString:UNICODE_NON_BREAKING_SPACE];
[tmpSet removeCharactersInString:UNICODE_OGHAM_SPACE_MARK];
[tmpSet removeCharactersInString:UNICODE_MONGOLIAN_VOWEL_SEPARATOR];
[tmpSet removeCharactersInString:UNICODE_EN_QUAD];
[tmpSet removeCharactersInString:UNICODE_EM_QUAD];
[tmpSet removeCharactersInString:UNICODE_EN_SPACE];
[tmpSet removeCharactersInString:UNICODE_EM_SPACE];
[tmpSet removeCharactersInString:UNICODE_THREE_PER_EM_SPACE];
[tmpSet removeCharactersInString:UNICODE_FOUR_PER_EM_SPACE];
[tmpSet removeCharactersInString:UNICODE_SIX_PER_EM_SPACE];
[tmpSet removeCharactersInString:UNICODE_FIGURE_SPACE];
[tmpSet removeCharactersInString:UNICODE_PUNCTUATION_SPACE];
[tmpSet removeCharactersInString:UNICODE_THIN_SPACE];
[tmpSet removeCharactersInString:UNICODE_HAIR_SPACE];
[tmpSet removeCharactersInString:UNICODE_ZERO_WIDTH_SPACE];
[tmpSet removeCharactersInString:UNICODE_NARROW_NO_BREAK_SPACE];
[tmpSet removeCharactersInString:UNICODE_MEDIUM_MATHEMATICAL_SPACE];
[tmpSet removeCharactersInString:UNICODE_IDEOGRAPHIC_SPACE];
[tmpSet removeCharactersInString:UNICODE_ZERO_WIDTH_NO_BREAK_SPACE];

_ignorableWhitespaceCharacterSet = [tmpSet copy];
});

return _ignorableWhitespaceCharacterSet;
}


+ (NSCharacterSet *)quoteCharacterSet
{
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
_quoteCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"'\""];
});

return _quoteCharacterSet;
}

+ (NSCharacterSet *)nonQuotedAttributeEndCharacterSet
{
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
NSMutableCharacterSet *tmpCharacterSet = [NSMutableCharacterSet characterSetWithCharactersInString:@"/>"];
[tmpCharacterSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

_nonQuotedAttributeEndCharacterSet = [tmpCharacterSet copy];
});

return _nonQuotedAttributeEndCharacterSet;
}

// NOTE: cannot contain : because otherwise this messes up parsing of CSS style attributes
+ (NSCharacterSet *)cssStyleAttributeNameCharacterSet
{
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
_cssStyleAttributeNameCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"];
});
return _cssStyleAttributeNameCharacterSet;
}


+ (NSCharacterSet *)cssLengthValueCharacterSet
{
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
_cssLengthValueCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
});
return _cssLengthValueCharacterSet;
}

+ (NSCharacterSet *)cssLengthUnitCharacterSet
{
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
_cssLengthUnitCharacterSet = [NSCharacterSet characterSetWithCharactersInString:@"pxtem"];
});
return _cssLengthUnitCharacterSet;
}

@end
Something went wrong with that request. Please try again.