Comparing version 1.1.6 to 1.2.0
@@ -15,20 +15,29 @@ export declare class SaxEventType { | ||
} | ||
export interface Position { | ||
declare abstract class Reader<T> { | ||
constructor(buf: Uint8Array, ptr: number); | ||
protected abstract read(buf: Uint8Array, ptr: number): void; | ||
} | ||
export declare class Position { | ||
line: number; | ||
character: number; | ||
constructor(line: number, character: number); | ||
} | ||
export interface Attribute { | ||
export declare class Attribute extends Reader<string | number | Position> { | ||
static BYTES_IN_DESCRIPTOR: number; | ||
nameEnd: Position; | ||
nameStart: Position; | ||
valueEnd: Position; | ||
valueStart: Position; | ||
name: string; | ||
value: string; | ||
nameStart: Position; | ||
nameEnd: Position; | ||
valueStart: Position; | ||
valueEnd: Position; | ||
protected read(buf: Uint8Array, ptr: number): void; | ||
} | ||
export interface Text { | ||
export declare class Text extends Reader<string | Position> { | ||
static BYTES_IN_DESCRIPTOR: number; | ||
end: Position; | ||
start: Position; | ||
value: string; | ||
start: Position; | ||
end: Position; | ||
protected read(buf: Uint8Array, ptr: number): void; | ||
} | ||
export interface Tag { | ||
export declare class Tag extends Reader<Attribute[] | Text[] | Position | string | number | boolean> { | ||
name: string; | ||
@@ -42,7 +51,9 @@ attributes: Attribute[]; | ||
closeEnd: Position; | ||
protected read(buf: Uint8Array, ptr: number): void; | ||
} | ||
export declare class SAXParser { | ||
static textDecoder: TextDecoder; | ||
static textEncoder: TextEncoder; | ||
events: number; | ||
eventHandler: (type: SaxEventType, detail: Tag | Attribute | Position | Text | string) => void; | ||
eventHandler: (type: SaxEventType, detail: Reader<any> | Position | string) => void; | ||
private wasmSaxParser; | ||
@@ -55,1 +66,2 @@ constructor(events?: number); | ||
} | ||
export {}; |
@@ -30,15 +30,95 @@ "use strict"; | ||
exports.SaxEventType = SaxEventType; | ||
const jsonFlag = SaxEventType.Text | | ||
SaxEventType.Attribute | | ||
SaxEventType.OpenTagStart | | ||
SaxEventType.OpenTag | | ||
SaxEventType.CloseTag | | ||
SaxEventType.OpenCDATA | | ||
SaxEventType.CloseCDATA; | ||
class Reader { | ||
constructor(buf, ptr) { | ||
this.read(buf, ptr); | ||
} | ||
} | ||
class Position { | ||
constructor(line, character) { | ||
this.line = line; | ||
this.character = character; | ||
} | ||
} | ||
exports.Position = Position; | ||
class Attribute extends Reader { | ||
read(buf, ptr) { | ||
const namePtr = readU32(buf, ptr); | ||
const nameLen = readU32(buf, ptr + 4); | ||
const valuePtr = readU32(buf, ptr + 24); | ||
const valueLen = readU32(buf, ptr + 28); | ||
this.nameEnd = readPosition(buf, ptr + 8); | ||
this.nameStart = readPosition(buf, ptr + 16); // 8 bytes | ||
this.valueEnd = readPosition(buf, ptr + 32); // 8 bytes | ||
this.valueStart = readPosition(buf, ptr + 40); // 8 bytes | ||
this.name = readString(buf.buffer, namePtr, nameLen); | ||
this.value = readString(buf.buffer, valuePtr, valueLen); | ||
} | ||
} | ||
Attribute.BYTES_IN_DESCRIPTOR = 48; | ||
exports.Attribute = Attribute; | ||
class Text extends Reader { | ||
read(buf, ptr) { | ||
const valuePtr = readU32(buf, ptr + 16); | ||
const valueLen = readU32(buf, ptr + 20); | ||
this.end = readPosition(buf, ptr); | ||
this.start = readPosition(buf, ptr + 8); | ||
this.value = readString(buf.buffer, valuePtr, valueLen); | ||
} | ||
} | ||
Text.BYTES_IN_DESCRIPTOR = 24; | ||
exports.Text = Text; | ||
class Tag extends Reader { | ||
read(buf, ptr) { | ||
this.closeEnd = readPosition(buf, ptr); | ||
this.closeStart = readPosition(buf, ptr + 8); | ||
this.openEnd = readPosition(buf, ptr + 16); | ||
this.openStart = readPosition(buf, ptr + 24); | ||
const namePtr = readU32(buf, ptr + 32); | ||
const nameLen = readU32(buf, ptr + 36); | ||
this.name = readString(buf.buffer, namePtr, nameLen); | ||
this.selfClosing = !!buf[ptr + 40]; | ||
let offset = ptr + 41; | ||
const attributes = []; | ||
let numAttrs = readU32(buf, offset); | ||
offset += 4; | ||
for (let i = 0; i < numAttrs; i++) { | ||
attributes[i] = new Attribute(buf, offset); | ||
offset += Attribute.BYTES_IN_DESCRIPTOR; | ||
} | ||
this.attributes = attributes; | ||
const textNodes = []; | ||
let numNodes = readU32(buf, offset); | ||
offset += 4; | ||
for (let i = 0; i < numNodes; i++) { | ||
textNodes[i] = new Text(buf, offset); | ||
offset += Text.BYTES_IN_DESCRIPTOR; | ||
} | ||
this.textNodes = textNodes; | ||
} | ||
} | ||
exports.Tag = Tag; | ||
class SAXParser { | ||
constructor(events = 0) { | ||
this.eventTrap = (event, ptr, len) => { | ||
const { memory } = this.wasmSaxParser; | ||
const rawUtf8String = uint8ToUtf8(memory.buffer, ptr, len); | ||
const payload = event & jsonFlag ? JSON.parse(rawUtf8String) : rawUtf8String; | ||
const buffer = this.wasmSaxParser.memory.buffer; | ||
let payload; | ||
switch (event) { | ||
case SaxEventType.Attribute: | ||
payload = new Attribute(new Uint8Array(buffer), ptr); | ||
break; | ||
case SaxEventType.OpenTag: | ||
case SaxEventType.CloseTag: | ||
case SaxEventType.OpenTagStart: | ||
payload = new Tag(new Uint8Array(buffer), ptr); | ||
break; | ||
case SaxEventType.Text: | ||
payload = new Text(new Uint8Array(buffer), ptr); | ||
break; | ||
case SaxEventType.OpenCDATA: | ||
payload = readPosition(new Uint8Array(buffer), ptr); | ||
break; | ||
default: | ||
payload = readString(buffer, ptr, len); | ||
break; | ||
} | ||
this.eventHandler(event, payload); | ||
@@ -93,18 +173,26 @@ }; | ||
// Node | ||
if ('Buffer' in env) { | ||
if (env.Buffer !== undefined) { | ||
return Buffer.from(value); | ||
} | ||
// Web | ||
return new TextEncoder().encode(value); | ||
return (SAXParser.textEncoder || (SAXParser.textEncoder = new TextEncoder())).encode(value); | ||
} | ||
function uint8ToUtf8(buffer, ptr, length) { | ||
function readPosition(data, ptr = 0) { | ||
const line = readU32(data, ptr); | ||
const character = readU32(data, ptr + 4); | ||
return new Position(line, character); | ||
} | ||
function readString(data, byteOffset, length) { | ||
const env = (global || window); | ||
// Node | ||
if ('Buffer' in env) { | ||
return Buffer.from(buffer, ptr, length).toString(); | ||
if (env.Buffer !== undefined) { | ||
return Buffer.from(data, byteOffset, length).toString(); | ||
} | ||
// Web | ||
return (SAXParser.textDecoder || (SAXParser.textDecoder = new TextDecoder())) | ||
.decode(new Uint8Array(buffer, ptr, length)); | ||
.decode(new Uint8Array(data, byteOffset, length)); | ||
} | ||
function readU32(buffer, ptr) { | ||
return buffer[ptr + 3] << 24 | buffer[ptr + 2] << 16 | buffer[ptr + 1] << 8 | buffer[ptr]; | ||
} | ||
//# sourceMappingURL=saxWasm.js.map |
{ | ||
"name": "sax-wasm", | ||
"version": "1.1.6", | ||
"version": "1.2.0", | ||
"repository": "https://github.com/justinwilaby/sax-wasm", | ||
@@ -5,0 +5,0 @@ "description": "An extremely fast JSX, HTML and XML parser written in Rust compiled to WebAssembly for Node and the Web", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48035
266