Skip to content

Commit

Permalink
Merge branch 'release/2.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
iabudiab committed Apr 18, 2017
2 parents aaee859 + 76b3794 commit 4094f51
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .jazzy.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module: HTMLKit
module_version: 2.0.4
module_version: 2.0.5
author: Iskandar Abudiab
author_url: https://twitter.com/iabudiab
github_url: https://github.com/iabudiab/HTMLKit
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Change Log

## [2.0.5](https://github.com/iabudiab/HTMLKit/releases/tag/2.0.5)

Released on 2017.04.19

### Fixed
- Xcode 8.3 issue with modulemaps
- Temporary workaround (renamed modulemap file)
- Memory Leaks in `CSSInputStream`

### Added
- Minor memory consumption improvements
- Collections for child nodes or attributes of HTML Nodes or Elements are allocated lazily
- Underyling data string of `CharacterData` is allocated on first access
- Autorelease pool for the main `HTMLTokenizer` loop


## [2.0.4](https://github.com/iabudiab/HTMLKit/releases/tag/2.0.4)

Released on 2017.04.2
Expand Down
2 changes: 1 addition & 1 deletion HTMLKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "HTMLKit"
s.version = "2.0.4"
s.version = "2.0.5"
s.summary = "HTMLKit, an Objective-C framework for your everyday HTML needs."
s.license = "MIT"
s.homepage = "https://github.com/iabudiab/HTMLKit"
Expand Down
20 changes: 15 additions & 5 deletions Sources/CSSInputStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ - (void)consumeWhitespace

- (NSString *)consumeIdentifier
{
CFMutableStringRef value = CFStringCreateMutable(kCFAllocatorDefault, 0);

if (!isValidIdentifierStart([self inputCharacterPointAtOffset:0],
[self inputCharacterPointAtOffset:1],
[self inputCharacterPointAtOffset:2])) {
return nil;
}

CFMutableStringRef value = CFStringCreateMutable(kCFAllocatorDefault, 0);

while (YES) {
UTF32Char codePoint = [self consumeNextInputCharacter];
if (codePoint == EOF) {
Expand All @@ -47,7 +47,12 @@ - (NSString *)consumeIdentifier
}
}

return (__bridge NSString *)(CFStringGetLength(value) > 0 ? value : nil);
if (CFStringGetLength(value) > 0) {
return (__bridge_transfer NSString *)value;
}

CFRelease(value);
return nil;
}

- (NSString *)consumeStringWithEndingCodePoint:(UTF32Char)endingCodePoint
Expand Down Expand Up @@ -85,7 +90,12 @@ - (NSString *)consumeStringWithEndingCodePoint:(UTF32Char)endingCodePoint
}
}

return (__bridge NSString *)(CFStringGetLength(value) > 0 ? value : nil);
if (CFStringGetLength(value) > 0) {
return (__bridge_transfer NSString *)value;
}

CFRelease(value);
return nil;
}

- (UTF32Char)consumeEscapedCodePoint
Expand All @@ -105,7 +115,7 @@ - (UTF32Char)consumeEscapedCodePoint
[self consumeNextInputCharacter];
}

NSScanner *scanner = [NSScanner scannerWithString:(__bridge NSString *)(hexString)];
NSScanner *scanner = [NSScanner scannerWithString:(__bridge_transfer NSString *)(hexString)];
unsigned int number;
[scanner scanHexInt:&number];

Expand Down
19 changes: 15 additions & 4 deletions Sources/HTMLCharacterData.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,25 @@ - (instancetype)initWithName:(NSString *)name type:(HTMLNodeType)type data:(NSSt
{
self = [super initWithName:name type:type];
if (self) {
_data = [[NSMutableString alloc] initWithString:data ?: @""];
if (data) {
_data = [[NSMutableString alloc] initWithString:data];
}
}
return self;
}

- (NSString *)data
{
if (_data == nil) {
_data = [[NSMutableString alloc] initWithString:@""];
}

return _data;
}

- (NSString *)textContent
{
return [_data copy];
return [self.data copy];
}

- (void)setTextContent:(NSString *)textContent
Expand All @@ -41,7 +52,7 @@ - (void)setTextContent:(NSString *)textContent

- (NSUInteger)length
{
return _data.length;
return self.data.length;
}

#pragma mark - Data
Expand Down Expand Up @@ -81,7 +92,7 @@ - (void)replaceDataInRange:(NSRange)range withData:(NSString *)data

range.length = MIN(range.length, self.length - range.location);

[_data replaceCharactersInRange:range withString:data];
[(NSMutableString *)self.data replaceCharactersInRange:range withString:data];
[self.ownerDocument didRemoveCharacterDataInNode:self atOffset:range.location withLength:range.length];
[self.ownerDocument didAddCharacterDataToNode:self atOffset:range.location withLength:data.length];
}
Expand Down
30 changes: 20 additions & 10 deletions Sources/HTMLElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ - (instancetype)init

- (instancetype)initWithTagName:(NSString *)tagName
{
return [self initWithTagName:tagName attributes:@{}];
return [self initWithTagName:tagName attributes:nil];
}

- (instancetype)initWithTagName:(NSString *)tagName attributes:(NSDictionary *)attributes
Expand All @@ -45,8 +45,9 @@ - (instancetype)initWithTagName:(NSString *)tagName namespace:(HTMLNamespace)htm
self = [super initWithName:tagName type:HTMLNodeElement];
if (self) {
_tagName = [tagName copy];
_attributes = [HTMLOrderedDictionary new];
_attributes = nil;
if (attributes != nil) {
_attributes = [HTMLOrderedDictionary new];
[_attributes addEntriesFromDictionary:attributes];
}
_htmlNamespace = htmlNamespace;
Expand All @@ -56,24 +57,33 @@ - (instancetype)initWithTagName:(NSString *)tagName namespace:(HTMLNamespace)htm

#pragma mark - Special Attributes

- (NSMutableDictionary<NSString *,NSString *> *)attributes
{
if (_attributes == nil) {
_attributes = [HTMLOrderedDictionary new];
}

return _attributes;
}

- (NSString *)elementId
{
return _attributes[@"id"] ?: @"";
return self.attributes[@"id"] ?: @"";
}

- (void)setElementId:(NSString *)elementId
{
_attributes[@"id"] = elementId;
self.attributes[@"id"] = elementId;
}

- (NSString *)className
{
return _attributes[@"class"] ?: @"";
return self.attributes[@"class"] ?: @"";
}

- (void)setClassName:(NSString *)className
{
_attributes[@"class"] = className;
self.attributes[@"class"] = className;
}

- (HTMLDOMTokenList *)classList
Expand All @@ -85,22 +95,22 @@ - (HTMLDOMTokenList *)classList

- (BOOL)hasAttribute:(NSString *)name
{
return _attributes[name] != nil;
return self.attributes[name] != nil;
}

- (NSString *)objectForKeyedSubscript:(NSString *)name;
{
return _attributes[name];
return self.attributes[name];
}

- (void)setObject:(NSString *)value forKeyedSubscript:(NSString *)attribute
{
_attributes[attribute] = value;
self.attributes[attribute] = value;
}

- (void)removeAttribute:(NSString *)name
{
[_attributes removeObjectForKey:name];
[self.attributes removeObjectForKey:name];
}

- (NSString *)textContent
Expand Down
2 changes: 1 addition & 1 deletion Sources/HTMLKit-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.0.4</string>
<string>2.0.5</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
25 changes: 20 additions & 5 deletions Sources/HTMLNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ - (instancetype)initWithName:(NSString *)name type:(HTMLNodeType)type
if (self) {
_name = name;
_nodeType = type;
_childNodes = [NSMutableOrderedSet new];
_childNodes = nil;
}
return self;
}

#pragma mark - Properties

- (NSOrderedSet<HTMLNode *> *)childNodes
{
if (_childNodes == nil) {
_childNodes = [NSMutableOrderedSet new];
}

return _childNodes;
}

- (HTMLDocument *)ownerDocument
{
if (_nodeType == HTMLNodeDocument) {
Expand All @@ -55,7 +64,9 @@ - (HTMLDocument *)ownerDocument
- (void)setOwnerDocument:(HTMLDocument *)ownerDocument
{
_ownerDocument = ownerDocument;
[self.childNodes.array makeObjectsPerformSelector:@selector(setOwnerDocument:) withObject:ownerDocument];
for (HTMLNode *child in self.childNodes) {
[child setOwnerDocument:ownerDocument];
}
}

- (HTMLNode *)rootNode
Expand Down Expand Up @@ -262,7 +273,9 @@ - (HTMLNode *)insertNode:(HTMLNode *)node beforeChildNode:(HTMLNode *)child
[node removeAllChildNodes];
}

[nodes makeObjectsPerformSelector:@selector(setParentNode:) withObject:self];
for (HTMLNode *node in nodes) {
[node setParentNode:self];
}

return node;
}
Expand Down Expand Up @@ -322,15 +335,17 @@ - (HTMLNode *)removeChildNodeAtIndex:(NSUInteger)index

- (void)reparentChildNodesIntoNode:(HTMLNode *)node
{
for (HTMLNode *child in self.childNodes.array) {
for (HTMLNode *child in self.childNodes) {
[node appendNode:child];
}
[(NSMutableOrderedSet *)self.childNodes removeAllObjects];
}

- (void)removeAllChildNodes
{
[self.childNodes.array makeObjectsPerformSelector:@selector(setParentNode:) withObject:nil];
for (HTMLNode *child in self.childNodes) {
[child setParentNode:nil];
}
[(NSMutableOrderedSet *)self.childNodes removeAllObjects];
}

Expand Down
16 changes: 9 additions & 7 deletions Sources/HTMLTokenizer.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ - (NSString *)string

- (id)nextObject
{
while (_eof == NO && _tokens.count == 0) {
[self read];
}
HTMLToken *nextToken = [_tokens firstObject];
if (_tokens.count > 0) {
[_tokens removeObjectAtIndex:0];
@autoreleasepool {
while (_eof == NO && _tokens.count == 0) {
[self read];
}
HTMLToken *nextToken = [_tokens firstObject];
if (_tokens.count > 0) {
[_tokens removeObjectAtIndex:0];
}
return nextToken;
}
return nextToken;
}

- (void)read
Expand Down
4 changes: 2 additions & 2 deletions Sources/include/HTMLElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ NS_ASSUME_NONNULL_BEGIN
@param attributes The attributes.
@return A new HTML element.
*/
- (instancetype)initWithTagName:(NSString *)tagName attributes:(NSDictionary<NSString *, NSString *> *)attributes;
- (instancetype)initWithTagName:(NSString *)tagName attributes:(nullable NSDictionary<NSString *, NSString *> *)attributes;

/**
Initializes a new HTML element with the given tag name, namespace, and attributes.
Expand All @@ -85,7 +85,7 @@ NS_ASSUME_NONNULL_BEGIN
@param attributes The attributes.
@return A new HTML element.
*/
- (instancetype)initWithTagName:(NSString *)tagName namespace:(HTMLNamespace)htmlNamespace attributes:(NSDictionary<NSString *, NSString *> *)attributes;
- (instancetype)initWithTagName:(NSString *)tagName namespace:(HTMLNamespace)htmlNamespace attributes:(nullable NSDictionary<NSString *, NSString *> *)attributes;

/**
Checks whether this element has an attribute with the given name.
Expand Down
File renamed without changes.

0 comments on commit 4094f51

Please sign in to comment.