Fix SAXParser startTag attrs type.

update ASTAttribute to include optional prefix field
update target version
improve tests
This commit is contained in:
Meirion Hughes
2016-08-18 14:59:41 +01:00
parent 9b94d577a8
commit 227ee0d1f5
2 changed files with 34 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
// Type definitions for parse5 2.1.5
// Project: https://github.com/inikulin/parse5
// Definitions by: Nico Jansen <https://github.com/nicojs>
// Definitions by: Nico Jansen <https://github.com/nicojs>, Meirion Hughes <https://github.com/MeirionHughes>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="./parse5.d.ts" />
@@ -9,33 +9,56 @@ import * as parse5 from 'parse5';
// parse5.SAXParser()
var parser = new parse5.SAXParser({ locationInfo: true });
var _wasCalled = false;
parser.on('startTag', (name, attrs, selfClosing, location) => {
console.log(name, attrs, selfClosing, location);
console.log(attrs[0].name);
console.log(attrs[0].prefix);
console.log(attrs[0].value);
if(name == "use")
if(attrs[0].prefix === undefined)
throw "prefix wasn't defined on known attr"
_wasCalled = true;
});
parser.on('text', (text, location) => {
console.log(text, location);
});
parser.write('some text');
parser.write('<svg class="icon"><use xlink:href="icons.svg#some_selector"></use></svg>');
if(!_wasCalled)
throw "parser.on 'startTag' wasn't called";
// parse5.parse()
parse5.parse('html', { locationInfo: true, treeAdapter: parse5.treeAdapters.default });
parse5.parse('<html></html>', { locationInfo: true, treeAdapter: parse5.treeAdapters.default });
parse5.parse('html', {});
parse5.parse('html');
parse5.parse('');
// parse5.ParserStream()
var parserStream = new parse5.ParserStream({ locationInfo: true, treeAdapter: parse5.treeAdapters.htmlparser2 });
parserStream = new parse5.ParserStream({ });
parserStream = new parse5.ParserStream();
parserStream.write("<html></html>");
var node = parserStream.document.childNodes[0];
node.parentNode.attrs = [{name: '', value: ''}];
// parse5.parseFragment()
var fragment = parse5.parseFragment('');
fragment = parse5.parseFragment('', {locationInfo: true});
var fragment = parse5.parseFragment('<div></div>');
fragment = parse5.parseFragment('<div></div>', {locationInfo: true});
// parse5.ASTNode
fragment.quirksMode = true;
fragment.namespaceURI = '';
fragment.nodeName = '';
fragment.value = '';
fragment = fragment.parentNode;
fragment = fragment.childNodes[0];
fragment = fragment.parentNode;

9
parse5/parse5.d.ts vendored
View File

@@ -1,6 +1,6 @@
// Type definitions for parse5 2.1.5
// Type definitions for parse5 2.2.0
// Project: https://github.com/inikulin/parse5
// Definitions by: Nico Jansen <https://github.com/nicojs>
// Definitions by: Nico Jansen <https://github.com/nicojs>, Meirion Hughes <https://github.com/MeirionHughes>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../node/node.d.ts" />
@@ -45,6 +45,7 @@ declare module 'parse5' {
export interface ASTAttribute {
name: string;
value: string;
prefix?: string;
}
export interface Attribute {
@@ -126,10 +127,10 @@ declare module 'parse5' {
/**
* Raised when the parser encounters a start tag.
* Listener function has 4 parameters:
* Tag name, List of attributes in the { key: String, value: String } form, selfClosing boolean
* Tag name, List of attributes in the { name: String, value: String, prefix?: String } form, selfClosing boolean
* and start tag source code location info. Available if location info is enabled in SAXParserOptions.
*/
on(event: 'startTag', listener: (name: string, attrs: Attribute[], selfClosing: boolean, location?: StartTagLocationInfo) => void): this;
on(event: 'startTag', listener: (name: string, attrs: ASTAttribute[], selfClosing: boolean, location?: StartTagLocationInfo) => void): this;
/**
* Raised when parser encounters an end tag.
* Listener function has 2 parameters: