Skip to content

Commit

Permalink
Add support for parsing <!DOCTYPE html>
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Sep 12, 2019
1 parent 691662c commit 627a023
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface TokenBase<T extends TokenType> {
loc?: Location;
}

export interface DOCTYPE extends TokenBase<TokenType.DOCTYPE> {
documentType: string;
legacyString: string;
}

export interface StartTag extends TokenBase<TokenType.StartTag> {
tagName: string;
attributes: Attribute[];
Expand All @@ -50,6 +55,7 @@ export interface Comment extends TokenBase<TokenType.Comment> {
export type Token = StartTag | EndTag | Chars | Comment;

export const enum TokenType {
DOCTYPE = 'DOCTYPE',
StartTag = 'StartTag',
EndTag = 'EndTag',
Chars = 'Chars',
Expand Down
21 changes: 21 additions & 0 deletions tests/tokenizer-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
tokenize,
DOCTYPE,
StartTag,
EndTag,
Comment,
Expand All @@ -11,6 +12,18 @@ import {

QUnit.module('simple-html-tokenizer - tokenizer');

QUnit.test('DOCTYPE', function(assert) {
let tokens = tokenize('<!DOCTYPE html><html><body></body></html>');
assert.deepEqual(tokens, [
doctype(),
startTag('html'),
startTag('body'),
], 'DOCTYPE is included in tokens');

tokens = tokenize('<!-- comment --><!DOCTYPE html>');
assert.deepEqual(tokens, [comment(' comment '), doctype()], 'DOCTYPE after comments is valid');
});

QUnit.test('Simple content', function(assert) {
let tokens = tokenize('hello');
assert.deepEqual(tokens, [chars('hello')]);
Expand Down Expand Up @@ -363,6 +376,14 @@ function endTag(tagName: string): EndTag {
};
}

function doctype(legacyString: string = ''): DOCTYPE {
return {
type: 'DOCTYPE',
documentType: 'html',
legacyString: legacyString,
};
}

function locInfo(
token: Token,
startLine: number,
Expand Down

0 comments on commit 627a023

Please sign in to comment.