NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.49k stars 302 forks source link

Invalid DOCTYPE when parsing svg file #546

Closed gartcimore closed 1 year ago

gartcimore commented 1 year ago

Description

Library is-svg was using version 3.19.0. When upgrading to 4.1.2, some test cases are not successful anymore when parsing valid xml input samples Branch with the change can be found here

Input

test cases from is-svg library is using this svg file <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [ <!ENTITY Smile " <rect x='.5' y='.5' width='29' height='39' fill='black' stroke='red'/> <g transform='translate(0, 5)'> <circle cx='15' cy='15' r='10' fill='yellow'/><circle cx='12' cy='12' r='1.5' fill='black'/><circle cx='17' cy='12' r='1.5' fill='black'/><path d='M 10 19 L 15 23 20 19' stroke='black' stroke-width='2'/></g>"> ]><svg width="850px" height="700px" version="1.1" xmlns="http://www.w3.org/2000/svg"><g transform="matrix(16,0,0,16,0,0)">&Smile;</g></svg>

Code

input = input.toString().trim();

if (XMLValidator.validate(input) !== true) {
        return false;
    }

    let jsonObject;
    const parser = new XMLParser();

    try {
        jsonObject = parser.parse(input);
    } catch (_) {
        return false;
    }

    if (!jsonObject) {
        return false;
    }

    if (!('svg' in jsonObject)) {
        return false;
    }

Output

Error: Invalid DOCTYPE at readDocType (/Users/larustue/depots/is-svg/node_modules/fast-xml-parser/src/xmlparser/DocTypeReader.js:73:27) at OrderedObjParser.parseXml (/Users/larustue/depots/is-svg/node_modules/fast-xml-parser/src/xmlparser/OrderedObjParser.js:246:24) at XMLParser.parse (/Users/larustue/depots/is-svg/node_modules/fast-xml-parser/src/xmlparser/XMLParser.js:35:48) at isSvg (/Users/larustue/depots/is-svg/index.js:24:23) at /Users/larustue/depots/is-svg/test.js:66:14 at Test.callFn (/Users/larustue/depots/is-svg/node_modules/ava/lib/test.js:364:21) at Test.run (/Users/larustue/depots/is-svg/node_modules/ava/lib/test.js:377:23) at Runner.runSingle (/Users/larustue/depots/is-svg/node_modules/ava/lib/runner.js:259:19) at /Users/larustue/depots/is-svg/node_modules/ava/lib/runner.js:322:16 at processTicksAndRejections (node:internal/process/task_queues:95:5)

expected data

I guess it should not failed because the XML is valid and doctype used to be correct, there seems to be backward compatibility broken somewhere

Would you like to work on this issue?

Bookmark this repository for further updates.

github-actions[bot] commented 1 year ago

I'm glad you find this repository helpful. I'll try to address your issue ASAP. You can watch the repo for new changes or star it.

amitguptagwl commented 1 year ago

So many things were changed in version 3 and 4. Please check Changelog

gartcimore commented 1 year ago

I checked the changelog, nothing can be found about xml being invalid with entities in doctype. Is the provided example a valid xml in your opinion ? Should it trigger the exception of invalid doctype ?

amitguptagwl commented 1 year ago

After 4.0.0-beta.3 DOCTYPE are supported but with very limited features. You may probably want to set processEntities: false with XML parser.

gartcimore commented 1 year ago

I tried already with this option set, the behavior was the same, an error was still triggered

amitguptagwl commented 1 year ago

Have you tried with new version recently published?

gartcimore commented 1 year ago

I used the latest version 4.1.3 published, now everything is working perfectly, thanks