Add "tree construction dispatcher" and token handling stub

https://html.spec.whatwg.org/multipage/syntax.html#tree-construction
This commit is contained in:
iska
2015-02-21 14:30:44 +01:00
parent 4afa6d11e1
commit f9c8743106
4 changed files with 91 additions and 0 deletions
+2
View File
@@ -7,9 +7,11 @@
//
#import <Foundation/Foundation.h>
#import "HTMLNamespaces.h"
@interface HTMLElement : NSObject
@property (nonatomic, strong, readonly) NSString *tagName;
@property (nonatomic, assign, readonly) HTMLNamespace namespace;
@end
+27
View File
@@ -0,0 +1,27 @@
//
// HTMLElementTypes.h
// HTMLKit
//
// Created by Iska on 19/02/15.
// Copyright (c) 2015 BrainCookie. All rights reserved.
//
#import "HTMLElement.h"
#import "HTMLNamespaces.h"
#define matches(x, ...) [[NSSet setWithObjects:__VA_ARGS__, nil] containsObject:x]
NS_INLINE BOOL IsNodeMathMLTextIntegrationPoint(HTMLElement *node)
{
return (node.namespace == HTMLNamespaceMathML && matches(node.tagName, @"mi", @"mo", @"mn", @"ms", @"mtext"));
}
NS_INLINE BOOL IsNodeHTMLIntegrationPoint(HTMLElement *node)
{
if (node.namespace == HTMLNamespaceMathML && [node.tagName isEqualToString:@"annotation-xml"]) {
#warning Implement HTML Element Attributes
} else if (node.namespace == HTMLNamespaceSVG) {
return matches(node.tagName, @"foreignObject", @"desc", @"title");
}
return NO;
}
+60
View File
@@ -24,4 +24,64 @@
@implementation HTMLParser
#pragma mark - Parse
- (id)parse
{
for (HTMLToken *token in _tokenizer) {
[self handleToken:token];
}
return nil;
}
- (void)handleToken:(HTMLToken *)token
{
BOOL (^ treeConstructionDispatcher)(HTMLElement *node) = ^BOOL(HTMLElement *node){
if (node == nil) {
return YES;
}
if (node.namespace == HTMLNamespaceHTML) {
return YES;
}
if (IsNodeMathMLTextIntegrationPoint(node)) {
if (token.type == HTMLTokenTypeStartTag) {
return !matches([(HTMLStartTagToken *)token tagName], @"mglyph", @"malignmark");
}
if (token.type == HTMLTokenTypeCharacter) {
return YES;
}
}
if (node.namespace == HTMLNamespaceMathML && [node.tagName isEqualToString:@"annotation-xml"]) {
if (token.type == HTMLTokenTypeStartTag) {
return [[(HTMLStartTagToken *)token tagName] isEqualToString:@"svg"];
}
}
if (IsNodeHTMLIntegrationPoint(node)) {
return token.type == HTMLTokenTypeStartTag || token.type == HTMLTokenTypeCharacter;
}
if (token.type == HTMLTokenTypeEOF) {
return YES;
}
return NO;
};
if (treeConstructionDispatcher(self.adjustedCurrentNode)) {
[self handleToken:token byApplyingRulesForInsertionMode:_insertionMode];
} else {
[self handleTokenByApplyingRulesForParsingTokensInForeignContent:token];
}
}
- (void)handleToken:(HTMLToken *)token byApplyingRulesForInsertionMode:(HTMLInsertionMode)insertionMode
{
}
- (void)handleTokenByApplyingRulesForParsingTokensInForeignContent:(HTMLToken *)token
{
}
@end