Skip to content

Commit 825b0cb

Browse files
committed
Enabled default attribute extraction from RelaxNG
1 parent 084b43c commit 825b0cb

4 files changed

Lines changed: 27 additions & 23 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ dist/
44
.vscode/
55
.scannerwork/
66
ts/test.ts
7+
ts/testRelaxNG.ts
78
test.xml
8-
/tests/
9+
/tests/
10+
catalog/

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "typesxml",
33
"productName": "TypesXML",
4-
"version": "1.13.1",
4+
"version": "1.13.2",
55
"description": "Open source XML library written in TypeScript",
66
"keywords": [
77
"XML",

ts/SAXParser.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { dirname, isAbsolute, resolve } from "node:path";
1515
import { Readable } from "node:stream";
1616
import { fileURLToPath } from "node:url";
1717
import { Catalog } from "./Catalog";
18+
import { Constants } from "./Constants";
1819
import { ContentHandler } from "./ContentHandler";
1920
import { FileReader } from "./FileReader";
2021
import { NeedMoreDataError } from "./NeedMoreDataError";
@@ -942,31 +943,22 @@ export class SAXParser {
942943
this.pointer = 0;
943944
this.contentHandler?.processingInstruction(target, data);
944945

945-
// TODO enable RelaxNG parsing from xml-model processing instruction
946-
947-
/*
948946
if (target === 'xml-model') {
949-
// Extract default attributes from RelaxNG schemas
950-
let atts: Map<string, string> = this.parseAttributes(data);
951-
let href: string = '';
952-
let schemaType: string = '';
953-
for (let [key, value] of atts.entries()) {
954-
if (key === 'href') {
955-
href = value;
956-
}
957-
if (key === 'schematypens') {
958-
schemaType = value;
959-
}
960-
}
961-
if (href !== '' && Constants.RELAXNG_NS_URI === schemaType) {
947+
const attributesFromPi: Map<string, string> = this.parseAttributes(data);
948+
const href: string | undefined = attributesFromPi.get('href');
949+
const schemaType: string | undefined = attributesFromPi.get('schematypens');
950+
if (href && schemaType === Constants.RELAXNG_NS_URI) {
962951
try {
963952
this.parseRelaxNG(href);
964-
} catch (e: Error | any) {
965-
// do nothing
953+
} catch (error) {
954+
if (this.validating) {
955+
throw error;
956+
}
957+
const message: string = error instanceof Error ? error.message : String(error);
958+
console.warn(`Warning: Could not load RelaxNG defaults from ${href}: ${message}`);
966959
}
967960
}
968961
}
969-
*/
970962
}
971963

972964
parseRelaxNG(href: string) {
@@ -1762,6 +1754,16 @@ export class SAXParser {
17621754
const parsed: number = Number.parseInt(entityName.substring(1), 10);
17631755
XMLUtils.ensureValidXmlCodePoint(this.xmlVersion, parsed, `character reference &#${entityName.substring(1)}; in attribute value`);
17641756
result += String.fromCodePoint(parsed);
1757+
} else if (entityName === 'lt') {
1758+
result += '<';
1759+
} else if (entityName === 'gt') {
1760+
result += '>';
1761+
} else if (entityName === 'amp') {
1762+
result += '&';
1763+
} else if (entityName === 'apos') {
1764+
result += '\'';
1765+
} else if (entityName === 'quot') {
1766+
result += '"';
17651767
} else {
17661768
const replacement: string | undefined = grammar?.resolveEntity(entityName);
17671769
if (replacement !== undefined) {

0 commit comments

Comments
 (0)