From 1164609bcf39404faf8a349bab09d5dcd7ca76f2 Mon Sep 17 00:00:00 2001 From: iska Date: Thu, 19 Mar 2015 01:30:47 +0100 Subject: [PATCH] Add implementation for inserting character tokens (strings) into the DOM https://html.spec.whatwg.org/multipage/syntax.html#insert-a-character --- HTMLKit/HTMLParser.m | 7 ++++++- HTMLKit/HTMLText.h | 2 ++ HTMLKit/HTMLText.m | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/HTMLKit/HTMLParser.m b/HTMLKit/HTMLParser.m index 941330e..333ce23 100644 --- a/HTMLKit/HTMLParser.m +++ b/HTMLKit/HTMLParser.m @@ -320,7 +320,12 @@ HTMLNode *adjustedInsertionLocation = [self appropriatePlaceForInsertingANodeWithOverrideTarget:nil beforeChildNode:&child]; if (adjustedInsertionLocation.type != HTMLNodeDocument) { -#warning Implement inserting string into node (https://html.spec.whatwg.org/multipage/syntax.html#insert-a-character) + if (adjustedInsertionLocation.lastChildNode.type == HTMLNodeText) { + [(HTMLText *)adjustedInsertionLocation.lastChildNode appendString:data]; + } else { + HTMLText *text = [[HTMLText alloc] initWithData:data]; + [adjustedInsertionLocation insertNode:text beforeChildNode:child]; + } } } diff --git a/HTMLKit/HTMLText.h b/HTMLKit/HTMLText.h index 469b11b..fcc6075 100644 --- a/HTMLKit/HTMLText.h +++ b/HTMLKit/HTMLText.h @@ -14,4 +14,6 @@ - (instancetype)initWithData:(NSString *)data; +- (void)appendString:(NSString *)string; + @end diff --git a/HTMLKit/HTMLText.m b/HTMLKit/HTMLText.m index 1150a08..06de445 100644 --- a/HTMLKit/HTMLText.m +++ b/HTMLKit/HTMLText.m @@ -35,6 +35,11 @@ [self.data setString:textContent ?: @""]; } +- (void)appendString:(NSString *)string +{ + [self.data appendString:string]; +} + #pragma mark - NSCopying - (id)copyWithZone:(NSZone *)zone