From 9dd3c70fc3e015e4618284838d705a132f3e31fb Mon Sep 17 00:00:00 2001 From: iska Date: Thu, 10 May 2018 18:41:33 +0200 Subject: [PATCH] Remove foreign attributes adjustment Fixes #30 Foreign attributes were handled incorrectly because attribute names were implemented as pure strings without namespace prefix. This change keeps the attributes as strings and eliminates any handling in regards to namespaces. Spec note about attributes: "If designed today they would just have a name and value." https://dom.spec.whatwg.org/#attr --- Sources/HTMLParser.m | 3 -- Sources/include/HTMLElementAdjustment.h | 28 ------------------- .../HTML5LibTreeConstructionTest.m | 3 ++ 3 files changed, 3 insertions(+), 31 deletions(-) diff --git a/Sources/HTMLParser.m b/Sources/HTMLParser.m index e9603cc..5aa9cbc 100644 --- a/Sources/HTMLParser.m +++ b/Sources/HTMLParser.m @@ -1443,7 +1443,6 @@ } else if ([tagName isEqualToString:@"math"]) { [self reconstructActiveFormattingElements]; AdjustMathMLAttributes(token); - AdjustForeignAttributes(token); [self insertForeignElementForToken:token inNamespace:HTMLNamespaceMathML]; if (token.isSelfClosing) { [_stackOfOpenElements popCurrentNode]; @@ -1451,7 +1450,6 @@ } else if ([tagName isEqualToString:@"svg"]) { [self reconstructActiveFormattingElements]; AdjustSVGAttributes(token); - AdjustForeignAttributes(token); [self insertForeignElementForToken:token inNamespace:HTMLNamespaceSVG]; if (token.isSelfClosing) { [_stackOfOpenElements popCurrentNode]; @@ -2541,7 +2539,6 @@ AdjustSVGNameCase(token.asTagToken); AdjustSVGAttributes(token.asTagToken); } - AdjustForeignAttributes(token.asTagToken); [self insertForeignElementForToken:token.asTagToken inNamespace:self.adjustedCurrentNode.htmlNamespace]; if (token.asTagToken.selfClosing) { [_stackOfOpenElements popCurrentNode]; diff --git a/Sources/include/HTMLElementAdjustment.h b/Sources/include/HTMLElementAdjustment.h index 17b1864..907fa7e 100644 --- a/Sources/include/HTMLElementAdjustment.h +++ b/Sources/include/HTMLElementAdjustment.h @@ -140,31 +140,3 @@ NS_INLINE void AdjustSVGNameCase(HTMLTagToken *token) NSString *replacement = replacements[token.tagName] ?: token.tagName; token.tagName = replacement; } - -NS_INLINE void AdjustForeignAttributes(HTMLTagToken *token) -{ - if (token.attributes == nil) { - return; - } - - NSDictionary *replacements = @{ @"xlink:actuate": @"xlink actuate", - @"xlink:arcrole": @"xlink arcrole", - @"xlink:href": @"xlink href", - @"xlink:role": @"xlink role", - @"xlink:show": @"xlink show", - @"xlink:title": @"xlink title", - @"xlink:type": @"xlink type", - @"xml:base": @"xml base", - @"xml:lang": @"xml lang", - @"xml:space": @"xml space", - @"xmlns": @"xmlns", - @"xmlns:xlink": @"xmlns xlink"}; - - HTMLOrderedDictionary *adjusted = [HTMLOrderedDictionary new]; - for (id key in token.attributes) { - NSString *replacement = replacements[key] ?: key; - adjusted[replacement] = token.attributes[key]; - } - token.attributes = adjusted; -} - diff --git a/Tests/HTMLKitTests/HTML5LibTreeConstructionTest.m b/Tests/HTMLKitTests/HTML5LibTreeConstructionTest.m index 3b08d2c..2e9231b 100644 --- a/Tests/HTMLKitTests/HTML5LibTreeConstructionTest.m +++ b/Tests/HTMLKitTests/HTML5LibTreeConstructionTest.m @@ -9,6 +9,7 @@ #import "HTML5LibTreeConstructionTest.h" #import +#import "HTMLDOM.h" #import "HTMLDocumentType.h" #import "HTMLElement.h" #import "HTMLText.h" @@ -289,6 +290,8 @@ NS_INLINE NSArray * parseAttribute(NSString *str) NSRange range = [str rangeOfString:@"=" options:0]; NSString *key = [str substringToIndex:range.location]; + key = [key stringByReplacingOccurrencesOfString:@" " withString:@":"]; + NSString *value = [str substringFromIndex:range.location + 2]; value = [value substringToIndex:value.length - 1];