Comparing version 0.4.3-experimental.1 to 0.4.3
@@ -11,11 +11,5 @@ // Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. | ||
const i = qName.indexOf(':'); | ||
if (i < 0) { | ||
this._prefix = ''; | ||
this._localPart = qName; | ||
} | ||
else { | ||
const q = qName.split(':'); | ||
this._prefix = q[0]; | ||
this._localPart = q[1]; | ||
} | ||
const q = i < 0 ? [ '', qName ] : qName.split(':'); | ||
this._prefix = q[0]; | ||
this._localPart = q[1]; | ||
} | ||
@@ -51,3 +45,2 @@ | ||
private _attributes: Attribute[] = []; | ||
private _xmlnsAttribute: boolean = false; | ||
private _parent?: Element; | ||
@@ -70,5 +63,2 @@ | ||
this._attributes.push(attribute); | ||
if (attribute.prefix === 'xmlns') { | ||
this._xmlnsAttribute = true; | ||
} | ||
} | ||
@@ -88,6 +78,3 @@ | ||
get prefixMappings(): { ns: string, uri: string }[] | null { | ||
if (!this._xmlnsAttribute) { | ||
return null; | ||
} | ||
get prefixMappings(): { ns: string, uri: string }[] { | ||
const filterd = this._attributes.filter((attr) => (attr.prefix === 'xmlns')); | ||
@@ -201,5 +188,5 @@ return filterd.map((attr) => ({ ns: attr.localPart, uri: attr.value })); | ||
const element = this._elementStack.pop(); | ||
element?.prefixMappings?.forEach(({ns}) => { | ||
element?.prefixMappings.forEach(({ns}) => { | ||
this._namespaces[ns] = undefined; | ||
}); | ||
}) | ||
return element; | ||
@@ -206,0 +193,0 @@ } |
@@ -17,3 +17,2 @@ export declare class QName { | ||
private _attributes; | ||
private _xmlnsAttribute; | ||
private _parent?; | ||
@@ -30,3 +29,3 @@ uri?: string; | ||
uri: string; | ||
}[] | null; | ||
}[]; | ||
} | ||
@@ -33,0 +32,0 @@ /** information of parsed attribute, readonly. */ |
@@ -6,11 +6,5 @@ // Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. | ||
const i = qName.indexOf(':'); | ||
if (i < 0) { | ||
this._prefix = ''; | ||
this._localPart = qName; | ||
} | ||
else { | ||
const q = qName.split(':'); | ||
this._prefix = q[0]; | ||
this._localPart = q[1]; | ||
} | ||
const q = i < 0 ? ['', qName] : qName.split(':'); | ||
this._prefix = q[0]; | ||
this._localPart = q[1]; | ||
} | ||
@@ -41,3 +35,2 @@ get qName() { | ||
this._attributes = []; | ||
this._xmlnsAttribute = false; | ||
this.emptyElement = false; | ||
@@ -52,5 +45,2 @@ this._parent = parent; | ||
this._attributes.push(attribute); | ||
if (attribute.prefix === 'xmlns') { | ||
this._xmlnsAttribute = true; | ||
} | ||
} | ||
@@ -68,5 +58,2 @@ peekAttribute() { | ||
get prefixMappings() { | ||
if (!this._xmlnsAttribute) { | ||
return null; | ||
} | ||
const filterd = this._attributes.filter((attr) => (attr.prefix === 'xmlns')); | ||
@@ -148,3 +135,3 @@ return filterd.map((attr) => ({ ns: attr.localPart, uri: attr.value })); | ||
const element = this._elementStack.pop(); | ||
element?.prefixMappings?.forEach(({ ns }) => { | ||
element?.prefixMappings.forEach(({ ns }) => { | ||
this._namespaces[ns] = undefined; | ||
@@ -151,0 +138,0 @@ }); |
@@ -12,3 +12,3 @@ // Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. | ||
export function resolveEntity(text) { | ||
return (text | ||
let result = (text | ||
.replaceAll('&', '&') | ||
@@ -19,2 +19,15 @@ .replaceAll('>', '>') | ||
.replaceAll(''', '\'')); | ||
[ | ||
{ | ||
reg: /&#(\d{1,4});/g, | ||
repl: (_, digits) => String.fromCodePoint(parseInt(digits, 10)), | ||
}, | ||
{ | ||
reg: /&#x([\da-fA-F]{1,4});/g, | ||
repl: (_, digits) => String.fromCodePoint(parseInt(digits, 16)), | ||
}, | ||
].forEach(({ reg, repl }) => { | ||
result = result.replace(reg, repl); | ||
}); | ||
return result; | ||
} | ||
@@ -221,6 +234,6 @@ // BEFORE_DOCUMENT; FOUND_LT, Error | ||
const element = cx.peekElement(); | ||
element.prefixMappings?.forEach(({ ns, uri }) => { | ||
for (const { ns, uri } of element.prefixMappings) { | ||
cx.registerNamespace(ns, uri); | ||
events.push(['start_prefix_mapping', ns, uri]); | ||
}); | ||
} | ||
// Setting Namespace URI to this element and all attributes. | ||
@@ -287,5 +300,5 @@ element.uri = cx.getNamespaceURI(element.prefix); | ||
events = [['end_element', new ElementInfo(element)]]; | ||
element.prefixMappings?.forEach(({ ns, uri }) => { | ||
for (const { ns, uri } of element.prefixMappings) { | ||
events.push(['end_prefix_mapping', ns, uri]); | ||
}); | ||
} | ||
return events; | ||
@@ -292,0 +305,0 @@ } |
@@ -7,4 +7,4 @@ // Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. | ||
this.pending = 0; | ||
this.prevChunkData = ""; | ||
this.currentChunkStart = 0; | ||
this.data = ""; | ||
this.currentChunkOffset = 0; | ||
} | ||
@@ -16,6 +16,6 @@ } | ||
this._collect = new CollectionState(); | ||
this._collect.currentChunkStart = this._index + 1; | ||
this._collect.data = this._chunk; | ||
} | ||
this._collect.pending += 1; | ||
const startOffset = this._collect.prevChunkData.length + (this._index + 1 - this._collect.currentChunkStart); | ||
const startOffset = this._collect.currentChunkOffset + (this._index + 1); | ||
return startOffset; | ||
@@ -27,4 +27,3 @@ } | ||
} | ||
const collectedAll = this._collect.prevChunkData + this._chunk.substring(this._collect.currentChunkStart, this._index + 1); | ||
const collectedThis = collectedAll.substring(startOffset); | ||
const collected = this._collect.data.substring(startOffset, this._collect.currentChunkOffset + this._index + 1); | ||
this._collect.pending -= 1; | ||
@@ -34,3 +33,3 @@ if (this._collect.pending === 0) { | ||
} | ||
return collectedThis; | ||
return collected; | ||
} | ||
@@ -111,9 +110,7 @@ /* | ||
set chunk(chunk) { | ||
this._chunk = chunk; | ||
if (this._collect !== null) { | ||
// Save previous chunk data | ||
this._collect.prevChunkData += this._chunk; | ||
// On the new chunk we collect from the beginning | ||
this._collect.currentChunkStart = 0; | ||
this._collect.currentChunkOffset = this._collect.data.length; | ||
this._collect.data += chunk; | ||
} | ||
this._chunk = chunk; | ||
this._index = -1; | ||
@@ -120,0 +117,0 @@ } |
@@ -45,2 +45,5 @@ // Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. | ||
assertEquals(resolveEntity(''ab''), '\'ab\''); | ||
assertEquals(resolveEntity("abc"), "abc"); | ||
assertEquals(resolveEntity("nop"), "nop"); | ||
assertEquals(resolveEntity("nop"), "nop"); | ||
}); | ||
@@ -47,0 +50,0 @@ |
@@ -22,3 +22,3 @@ // Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. | ||
export function resolveEntity(text: string): string { | ||
return ( | ||
let result = ( | ||
text | ||
@@ -31,2 +31,15 @@ .replaceAll('&', '&') | ||
); | ||
[ | ||
{ | ||
reg: /&#(\d{1,4});/g, | ||
repl: (_: string, digits: string) => String.fromCodePoint(parseInt(digits, 10)), | ||
}, | ||
{ | ||
reg: /&#x([\da-fA-F]{1,4});/g, | ||
repl: (_: string, digits: string) => String.fromCodePoint(parseInt(digits, 16)), | ||
}, | ||
].forEach(({ reg, repl }) => { | ||
result = result.replace(reg, repl); | ||
}); | ||
return result; | ||
} | ||
@@ -227,6 +240,6 @@ | ||
const element = cx.peekElement()!; | ||
element.prefixMappings?.forEach(({ ns, uri }) => { | ||
for (const { ns, uri } of element.prefixMappings) { | ||
cx.registerNamespace(ns, uri); | ||
events.push(['start_prefix_mapping', ns, uri]); | ||
}); | ||
} | ||
// Setting Namespace URI to this element and all attributes. | ||
@@ -290,5 +303,5 @@ element.uri = cx.getNamespaceURI(element.prefix); | ||
events = [['end_element', new ElementInfo(element)]]; | ||
element.prefixMappings?.forEach(({ ns, uri }) => { | ||
for(const { ns, uri } of element.prefixMappings) { | ||
events.push(['end_prefix_mapping', ns, uri]); | ||
}); | ||
} | ||
return events; | ||
@@ -295,0 +308,0 @@ } |
{ | ||
"name": "@tejp/xmlp", | ||
"version": "0.4.3-experimental.1", | ||
"version": "0.4.3", | ||
"description": "Typescript XML parser (SAX and PullParser)", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/sth/xmlp.git", |
@@ -150,2 +150,23 @@ // Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. | ||
Deno.test('SAXParser entity resolution', () => { | ||
const parser = new SAXParser(); | ||
let flag_text = false; | ||
let flag_attr = false; | ||
parser.on('text', (text) => { | ||
flag_text = true; | ||
assertEquals(text, "text&text"); | ||
}); | ||
parser.on('start_element', (element) => { | ||
for (const attr of element.attributes) { | ||
if (attr.qName == "attr") { | ||
flag_attr = true; | ||
assertEquals(attr.value, "attr&attr"); | ||
} | ||
} | ||
}); | ||
parser.parse('<xml attr="attr&attr">text&text</xml>'); | ||
assertEquals(flag_text, true); | ||
assertEquals(flag_attr, true); | ||
}); | ||
Deno.test('marshallEvent', () => { | ||
@@ -152,0 +173,0 @@ class TestParser extends PullParser { |
@@ -42,4 +42,4 @@ // Copyright 2020 Masataka Kurihara. All rights reserved. MIT license. | ||
pending: number = 0; | ||
prevChunkData: string = ""; | ||
currentChunkStart: number = 0; | ||
data: string = ""; | ||
currentChunkOffset: number = 0; | ||
} | ||
@@ -59,6 +59,6 @@ | ||
this._collect = new CollectionState(); | ||
this._collect.currentChunkStart = this._index+1; | ||
this._collect.data = this._chunk; | ||
} | ||
this._collect.pending += 1; | ||
const startOffset = this._collect.prevChunkData.length + (this._index+1 - this._collect.currentChunkStart); | ||
const startOffset = this._collect.currentChunkOffset + (this._index+1); | ||
return startOffset; | ||
@@ -71,4 +71,3 @@ } | ||
} | ||
const collectedAll = this._collect.prevChunkData + this._chunk.substring(this._collect.currentChunkStart, this._index+1); | ||
const collectedThis = collectedAll.substring(startOffset); | ||
const collected = this._collect.data.substring(startOffset, this._collect.currentChunkOffset + this._index+1); | ||
this._collect.pending -= 1; | ||
@@ -78,3 +77,3 @@ if (this._collect.pending === 0) { | ||
} | ||
return collectedThis; | ||
return collected; | ||
} | ||
@@ -154,9 +153,7 @@ | ||
protected set chunk(chunk: string) { | ||
this._chunk = chunk; | ||
if (this._collect !== null) { | ||
// Save previous chunk data | ||
this._collect.prevChunkData += this._chunk; | ||
// On the new chunk we collect from the beginning | ||
this._collect.currentChunkStart = 0; | ||
this._collect.currentChunkOffset = this._collect.data.length; | ||
this._collect.data += chunk; | ||
} | ||
this._chunk = chunk; | ||
this._index = -1; | ||
@@ -163,0 +160,0 @@ } |
128371
33
3536