Skip to content

Affixes for date variables in <else> are mistakenly applied to <if> in the first run #280

Description

@YDX-2147483647

In the example below, we create a CSL style that

  • outputs submitted without any affix if the variable exists, and
  • outputs issued with prefix- & -suffix otherwise.

We test the style against the same entry twice.
This entry sets submitted to a date range, so there shouldn't be any affix.
However, the result of the first run contains prefix- & -suffix.

import CSL from "citeproc"; // v2.4.63

const sys = {
  retrieveLocale: (_lang: string) => false,
  retrieveItem(id: string) {
    return { id, submitted: { "date-parts": [[1957], [1990]] } };
  },
};
const style = `
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0">
  <bibliography>
    <layout>
      <choose>
        <if variable="submitted">
          <text value="if: "/>
          <date variable="submitted">
            <date-part name="year"/>
          </date>
        </if>
        <else>
          <text value="else: "/>
          <date variable="issued" prefix="prefix-" suffix="-suffix">
            <date-part name="year"/>
          </date>
        </else>
      </choose>
    </layout>
  </bibliography>
</style>
`;

const citeproc = new CSL.Engine(sys, style);
citeproc.updateUncitedItems(["a", "b"]);

const [_params, body] = citeproc.makeBibliography();
console.log(body.join(""));

Actual output:

  <div class="csl-entry">if: 1957–prefix-1990-suffix</div>
  <div class="csl-entry">if: 1957–1990</div>

Expected output:

  <div class="csl-entry">if: 1957–1990</div>
  <div class="csl-entry">if: 1957–1990</div>

It looks like the state for affixes is corrupted in the first run?

Original background: zotero-chinese/styles#696

Further tests

Omitted lines are the same as the main test code above.
Only the actual outputs are given.

else with date, if, if ⇒ OK

// …
const sys = {
  // …
  retrieveItem(id: string) {
    if (id === "a") {
      return { id, issued: { "date-parts": [[1957], [1990]] } }
    }
    return { id, submitted: { "date-parts": [[1957], [1990]] } };
  },
};
// …
citeproc.updateUncitedItems(["a", "b", "c"]);
// …
  <div class="csl-entry">else: prefix-1957–prefix-1990-suffix-suffix</div>
  <div class="csl-entry">if: 1957–1990</div>
  <div class="csl-entry">if: 1957–1990</div>

else without date, if, if ⇒ The first if is wrong

// …
const sys = {
  // …
  retrieveItem(id: string) {
    if (id === "a") {
      return { id }
    }
    return { id, submitted: { "date-parts": [[1957], [1990]] } };
  },
};
// …
citeproc.updateUncitedItems(["a", "b", "c"]);
// …
  <div class="csl-entry">else: </div>
  <div class="csl-entry">if: 1957–prefix-1990-suffix</div>
  <div class="csl-entry">if: 1957–1990</div>

else without date, if without date, if with date, if with date ⇒ The second if is wrong

// …
const sys = {
  // …
  retrieveItem(id: string) {
    if (id === "a") {
      return { id, type: "article" };
    }
    if (id === "b") {
      return { id, type: "book" };
    }
    return { id, type: "book", submitted: { "date-parts": [[1957], [1990]] } };
  },
};
const style = `
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0">
  <bibliography>
    <layout>
      <choose>
        <if type="book">
          <text value="if: "/>
          <date variable="submitted">
            <date-part name="year"/>
          </date>
        </if>
        <else>
          <text value="else: "/>
          <date variable="issued" prefix="prefix-" suffix="-suffix">
            <date-part name="year"/>
          </date>
        </else>
      </choose>
    </layout>
  </bibliography>
</style>
`;
// …
citeproc.updateUncitedItems(["a", "b", "c", "d"]);
// …
  <div class="csl-entry">else: </div>
  <div class="csl-entry">if: </div>
  <div class="csl-entry">if: 1957–prefix-1990-suffix</div>
  <div class="csl-entry">if: 1957–1990</div>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions