cellml / libcellml

Repository for libCellML development.
https://libcellml.org
Apache License 2.0
16 stars 21 forks source link

Invalid Component Math evaluation fails to print Model #953

Open Moop204 opened 3 years ago

Moop204 commented 3 years ago

If any MathML in the Math is invalid then the Printer will fail to print out the model, instead producing an empty string. Is it possible for the Printer to include the content of the Math element without processing it?

    const m: Model = new cellml.Model();
    m.setName("m");
    const c1: Component = new cellml.Component();
    c1.setName("c1");
    // Passing an invalid MathML string where the <ci> tag is not ended correctly
    c1.setMath(`<math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:cellml="http://www.cellml.org/cellml/2.0#">
          <apply>
            <eq/>
              <ci>t</ci  
              <ci>x</ci>
          </apply>
        </math>`);
    m.addComponent(c1);
    const printer: Printer = new cellml.Printer();
    console.log(printer.printModel(m, true))

Expected the following.

<?xml version="1.0" encoding="UTF-8"?>
<model xmlns="http://www.cellml.org/cellml/2.0#" name="m" id="b4da55">
  <component name="c1" id="b4da56">
    <math xmlns="http://www.w3.org/1998/Math/MathML" xmlns:cellml="http://www.cellml.org/cellml/2.0#">
      <apply>
        <eq/>
        <ci>t</ci
        <ci>x</ci>
      </apply>
    </math>
  </component>
</model>
hsorby commented 3 years ago

I would have to say not, the final printing of the XML document is done by libXml2. I don't know if libXml2 allows you to output invalid XML. One of the assumptions that we are working on is that the MathML is valid MathML. I don't think we will look at relaxing this assumption.

An alternative is to maybe provide a printRawModel to skip the libXml2 stage of the printing process.

We will have to see what thoughts other people have about this.

nickerso commented 3 years ago

Keeping #339 in mind I'd be keen to avoid adding a printRawModel.

It seems that when we switched to using libXml2 for "pretty printing" models (#511) we always pretty print. If we made the pretty printing optional (default being on), then that would essentially give you the raw XML string - which in this example would be invalid but perhaps useful to help debug?