Skip to content

Commit

Permalink
<xsl:include> base implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonelsanchesdasilva committed May 7, 2024
1 parent d065a3d commit 7f37a37
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
50 changes: 34 additions & 16 deletions src/xslt/xslt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import fetch, { Headers, Request, Response } from 'node-fetch';
import {
XDocument,
XNode,
XmlParser,
domAppendChild,
domAppendTransformedChild,
domCreateCDATASection,
Expand Down Expand Up @@ -71,6 +72,7 @@ import { MatchResolver } from '../xpath/match-resolver';
*/
export class Xslt {
xPath: XPath;
xmlParser: XmlParser;
matchResolver: MatchResolver;
options: XsltOptions;
decimalFormatSettings: XsltDecimalFormatSettings;
Expand All @@ -89,6 +91,7 @@ export class Xslt {
}
) {
this.xPath = new XPath();
this.xmlParser = new XmlParser();
this.matchResolver = new MatchResolver();
this.options = {
cData: options.cData === true,
Expand Down Expand Up @@ -397,22 +400,8 @@ export class Xslt {
case 'import':
throw new Error(`not implemented: ${template.localName}`);
case 'include':
// We need to test here whether `window.fetch` is available or not.
// If it is a browser environemnt, it should be.
// Otherwise, we will need to import an equivalent library, like 'node-fetch'.
if (!global.globalThis.fetch) {
global.globalThis.fetch = fetch as any;
global.globalThis.Headers = Headers as any;
global.globalThis.Request = Request as any;
global.globalThis.Response = Response as any;
}

const fetchTest = await global.globalThis.fetch(
'https://raw.githubusercontent.com/DesignLiquido/xslt-processor/xsl-include/examples/head.xsl'
);
const fetchResponse = await fetchTest.text();
console.log(fetchResponse);
throw new Error(`not implemented: ${template.localName}`);
await this.xsltInclude(context, template, output);
break;
case 'key':
throw new Error(`not implemented: ${template.localName}`);
case 'message':
Expand Down Expand Up @@ -618,6 +607,35 @@ export class Xslt {
}
}

/**
* Implements `xsl:include`.
* @param input The Expression Context.
* @param template The template.
* @param output The output.
*/
protected async xsltInclude(context: ExprContext, template: XNode, output: XNode) {
// We need to test here whether `window.fetch` is available or not.
// If it is a browser environemnt, it should be.
// Otherwise, we will need to import an equivalent library, like 'node-fetch'.
if (!global.globalThis.fetch) {
global.globalThis.fetch = fetch as any;
global.globalThis.Headers = Headers as any;
global.globalThis.Request = Request as any;
global.globalThis.Response = Response as any;
}

const hrefAttributeFind = template.childNodes.filter(n => n.nodeName === 'href');
if (hrefAttributeFind.length <= 0) {
throw new Error('<xsl:include> with no href attribute defined.');

Check warning on line 629 in src/xslt/xslt.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 630 in src/xslt/xslt.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
const hrefAttribute = hrefAttributeFind[0];

const fetchTest = await global.globalThis.fetch(hrefAttribute.nodeValue);
const fetchResponse = await fetchTest.text();
const includedXslt = this.xmlParser.xmlParse(fetchResponse);
await this.xsltChildNodes(context, includedXslt.childNodes[0], output);
}

/**
* Orders the current node list in the input context according to the
* sort order specified by xsl:sort child nodes of the current
Expand Down
3 changes: 1 addition & 2 deletions tests/xslt/include.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('xsl:include', () => {
const xml = xmlParser.xmlParse(xmlSource);
const xslt = xmlParser.xmlParse(xsltSource);
const resultingXml = await xsltClass.xsltProcess(xml, xslt);
// assert.equal(html, '<h1><D>Hello</D>-<D>World</D></h1>');
assert.ok(resultingXml)
assert.equal(resultingXml, '<html><head><link rel="stylesheet" type="text/css" href="style.css"><title/></head><body><div id="container"><div id="header"><div id="menu"><ul><li><a href="#" class="active">Home</a></li><li><a href="#">about</a></li></ul></div></div></div></body></html>');
});
});

0 comments on commit 7f37a37

Please sign in to comment.