123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326 |
- const { toXML } = require("./dist/jstoxml");
- const assert = require("assert");
- describe("toXML", () => {
- describe("primitives", () => {
- const vals = ["foo", false, true, 4, 4.56];
- vals.forEach((val) => {
- it(`outputs ${val}`, () => {
- const result = toXML(val);
- const expectedResult = `${val}`;
- assert.equal(result, expectedResult);
- });
- });
- });
- describe("functions", () => {
- describe("primitive outputs", () => {
- const vals = [999, "foo", false, true];
- vals.forEach((val) => {
- it(`${val}`, () => {
- const result = toXML(() => val);
- const expectedResult = `${val}`;
- assert.equal(result, expectedResult);
- });
- });
- });
- it("fat arrow", () => {
- const val = 888;
- const result = toXML(() => val);
- const expectedResult = val;
- assert.equal(result, expectedResult);
- });
- it("accessing config within function", () => {
- const val = {
- foo: {
- depth: (config) => config.depth,
- },
- };
- const result = toXML(val);
- const expectedResult = "<foo><depth>2</depth></foo>";
- assert.equal(result, expectedResult);
- });
- it("converts nonprimitive output", () => {
- const val = { foo: "bar" };
- const result = toXML(() => val);
- const expectedResult = "<foo>bar</foo>";
- assert.equal(result, expectedResult);
- });
- it("converts nested nonprimitive output", () => {
- const val = { foo: { bar: { baz: 2 } } };
- const result = toXML(() => val);
- const expectedResult = "<foo><bar><baz>2</baz></bar></foo>";
- assert.equal(result, expectedResult);
- });
- it("converts nested nonprimitive output with indent", () => {
- const val = { foo: { bar: { baz: 2 } } };
- const config = { indent: " " };
- const result = toXML(() => val, config);
- const expectedResult =
- "<foo>\n <bar>\n <baz>2</baz>\n </bar>\n</foo>";
- assert.equal(result, expectedResult);
- });
- });
- describe("github issues", () => {
- it("issue 3", () => {
- const val = {
- foo: true,
- bar: "",
- foo2: false,
- ok: "This is ok",
- ok2: "false",
- ok3: "true",
- };
- const result = toXML(val);
- const expectedResult =
- "<foo>true</foo><bar/><foo2>false</foo2><ok>This is ok</ok><ok2>false</ok2><ok3>true</ok3>";
- assert.equal(result, expectedResult);
- });
- });
- describe("arrays", () => {
- it("1", () => {
- const val = [{ foo: "bar" }, { foo: "baz" }, { foo2: "bar2" }];
- const result = toXML(val);
- const expectedResult = "<foo>bar</foo><foo>baz</foo><foo2>bar2</foo2>";
- assert.equal(result, expectedResult);
- });
- it("attributes in subobject", () => {
- const val = [
- { foo: "bar" },
- { foo: "baz" },
- { foo: undefined },
- { foo: "" },
- { foo: null },
- {
- _name: "foo",
- _content: "bar",
- _attrs: {
- a: "b",
- c: "d",
- },
- },
- ];
- const result = toXML(val);
- const expectedResult =
- '<foo>bar</foo><foo>baz</foo><foo/><foo/>foo<foo a="b" c="d">bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("nesting with indent", () => {
- const val = {
- foo: [{ foo: "bar" }, { foo: "baz" }, { foo2: "bar2" }],
- };
- const config = { indent: " " };
- const result = toXML(val, config);
- const expectedResult = `<foo>
- <foo>bar</foo>
- <foo>baz</foo>
- <foo2>bar2</foo2>
- </foo>`;
- assert.equal(result, expectedResult);
- });
- });
- describe("special-objects", () => {
- it("1", () => {
- const val = {
- _name: "foo",
- _content: "bar",
- _attrs: {
- a: 1,
- b: 2,
- },
- };
- const result = toXML(val);
- const expectedResult = '<foo a="1" b="2">bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("2", () => {
- const val = {
- _name: "foo",
- _content: {
- foo: "bar",
- },
- _attrs: {
- a: 1,
- b: 2,
- },
- };
- const result = toXML(val);
- const expectedResult = '<foo a="1" b="2"><foo>bar</foo></foo>';
- assert.equal(result, expectedResult);
- });
- it("3", () => {
- const val = {
- _name: "foo",
- _content: () => 1 + 2,
- _attrs: {
- a: 1,
- b: 2,
- },
- };
- const result = toXML(val);
- const expectedResult = '<foo a="1" b="2">3</foo>';
- assert.equal(result, expectedResult);
- });
- });
- describe("objects", () => {
- it("1", () => {
- const val = {
- foo: "bar",
- foo2: "bar2",
- };
- const result = toXML(val);
- const expectedResult = "<foo>bar</foo><foo2>bar2</foo2>";
- assert.equal(result, expectedResult);
- });
- it("attributes", () => {
- const val = {
- _name: "a",
- _attrs: {
- foo: "bar",
- foo2: "bar2",
- },
- };
- const result = toXML(val);
- const expectedResult = '<a foo="bar" foo2="bar2"/>';
- assert.equal(result, expectedResult);
- });
- it("attributes 2", () => {
- const val = {
- _name: "a",
- _attrs: {
- foo: "bar",
- foo2: "bar2",
- },
- _content: "la dee da",
- };
- const result = toXML(val);
- const expectedResult = '<a foo="bar" foo2="bar2">la dee da</a>';
- assert.equal(result, expectedResult);
- });
- it("attributes nesting", () => {
- const val = {
- _name: "foo",
- _attrs: {
- a: "b",
- },
- _content: {
- _name: "bar",
- _attrs: {
- c: "d",
- },
- },
- };
- const result = toXML(val);
- const expectedResult = '<foo a="b"><bar c="d"/></foo>';
- assert.equal(result, expectedResult);
- });
- it("with mixed content", () => {
- const val = {
- blah: null,
- foo: "bar",
- "more blah": null,
- bar: 0,
- "more more blah": null,
- baz: false,
- };
- const result = toXML(val);
- const expectedResult =
- "blah<foo>bar</foo>more blah<bar>0</bar>more more blah<baz>false</baz>";
- assert.equal(result, expectedResult);
- });
- it("nesting with indent", () => {
- const val = {
- foo: {
- foo: "bar",
- foo2: "bar2",
- },
- };
- const config = { indent: " " };
- const result = toXML(val, config);
- const expectedResult = `<foo>
- <foo>bar</foo>
- <foo2>bar2</foo2>
- </foo>`;
- assert.equal(result, expectedResult);
- });
- it("deep nesting", () => {
- const val = {
- a: {
- b: {
- c: {
- d: {
- e: {
- f: {
- g: {
- h: {
- i: {
- j: {
- k: {
- l: {
- m: {
- foo: "bar",
- },
- },
- },
- },
- },
- },
- },
- },
- },
- },
- },
- },
- },
- };
- const result = toXML(val);
- const expectedResult =
- "<a><b><c><d><e><f><g><h><i><j><k><l><m><foo>bar</foo></m></l></k></j></i></h></g></f></e></d></c></b></a>";
- assert.equal(result, expectedResult);
- });
- });
- describe("header", () => {
- it("default header", () => {
- const val = {
- foo: "bar",
- };
- const config = {
- header: true,
- };
- const result = toXML(val, config);
- const expectedResult =
- '<?xml version="1.0" encoding="UTF-8"?><foo>bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("no header", () => {
- const val = {
- foo: "bar",
- };
- const config = {
- header: false,
- };
- const result = toXML(val, config);
- const expectedResult = "<foo>bar</foo>";
- assert.equal(result, expectedResult);
- });
- it("no header by default", () => {
- const val = {
- foo: "bar",
- };
- const result = toXML(val);
- const expectedResult = "<foo>bar</foo>";
- assert.equal(result, expectedResult);
- });
- it("default header with indent", () => {
- const val = {
- foo: "bar",
- };
- const config = {
- header: true,
- indent: " ",
- };
- const result = toXML(val, config);
- const expectedResult =
- '<?xml version="1.0" encoding="UTF-8"?>\n<foo>bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("custom header", () => {
- const val = {
- foo: "bar",
- };
- const config = {
- header: '<?FOO BAR="123" BAZ="XX"?>',
- };
- const result = toXML(val, config);
- const expectedResult = '<?FOO BAR="123" BAZ="XX"?><foo>bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("custom header 2", () => {
- const val = [
- {
- row: 'bar'
- },
- {
- row: 'bar2'
- }
- ];
- const config = {
- header: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?>',
- indent: ' '
- };
- const result = toXML(val, config);
- const expectedResult = `<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
- <row>bar</row>
- <row>bar2</row>`;
- assert.equal(result, expectedResult);
- })
- });
- describe("filtering", () => {
- it("values", () => {
- const val = {
- foo: "<a>",
- bar: '"b"',
- baz: "'&whee'",
- };
- const config = {
- filter: {
- "<": "<",
- ">": ">",
- '"': """,
- "'": "'",
- "&": "&",
- },
- };
- const result = toXML(val, config);
- const expectedResult =
- "<foo><a></foo><bar>"b"</bar><baz>'&whee'</baz>";
- assert.equal(result, expectedResult);
- });
- it("attributes", () => {
- const val = {
- _name: "foo",
- _attrs: { a: '<"\'&"foo>' },
- };
- const config = {
- attributesFilter: {
- "<": "<",
- ">": ">",
- '"': """,
- "'": "'",
- "&": "&",
- },
- };
- const result = toXML(val, config);
- const expectedResult = '<foo a="<"'&"foo>"/>';
- assert.equal(result, expectedResult);
- });
- const entities = {
- "&": "&",
- "<": "<",
- ">": ">",
- };
- Object.entries(entities).forEach(([entity, entityEncoded]) => {
- it(`filters '${entity}' entities by default`, () => {
- const val = {
- _name: "foo",
- _attrs: { a: `aaa ${entity} bbb` },
- _content: `foo ${entity} bar`,
- };
- const result = toXML(val);
- const expectedResult = `<foo a="aaa ${entityEncoded} bbb">foo ${entityEncoded} bar</foo>`;
- assert.equal(result, expectedResult);
- });
- });
- it(`filters entities by default 2`, () => {
- const val = {
- foo: '1 < 2 & 2 > 1'
- };
- const result = toXML(val);
- const expectedResult = `<foo>1 < 2 & 2 > 1</foo>`;
- assert.equal(result, expectedResult);
- });
- it("does not double encode", () => {
- const val = {
- _name: "foo",
- _attrs: { a: "baz & > < bat" },
- _content: "foo & > < bar",
- };
- const result = toXML(val);
- const expectedResult =
- '<foo a="baz & > < bat">foo & > < bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("does not double encode 2", () => {
- const val = {
- _name: "foo",
- _attrs: { a: "baz && &> &< bat" },
- _content: "foo && &> &< bar",
- };
- const result = toXML(val);
- const expectedResult =
- '<foo a="baz && &> &< bat">foo && &> &< bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("does not double encode 3", () => {
- const val = {
- _name: "foo",
- _attrs: { a: "¢ ¢ € € &eu ro;" },
- _content: "¢ ¢ € € &eu ro;",
- };
- const result = toXML(val);
- const expectedResult =
- '<foo a="¢ ¢ € € &eu ro;">¢ ¢ € € &eu ro;</foo>';
- assert.equal(result, expectedResult);
- });
- it("escapes quotes in attributes by default", () => {
- const val = {
- _name: "foo",
- _attrs: { a: '"bat"' },
- };
- const result = toXML(val);
- const expectedResult = '<foo a=""bat""/>';
- assert.equal(result, expectedResult);
- });
- it(`turns off attributes filter`, () => {
- const val = {
- _name: "foo",
- _attrs: { a: "baz & < > \" bat" },
- _content: "foo & < > bar",
- };
- const result = toXML(val, { attributesFilter: false });
- const expectedResult = `<foo a="baz & < > \" bat">foo & < > bar</foo>`;
- assert.equal(result, expectedResult);
- });
- it(`turns off filter`, () => {
- const val = {
- _name: "foo",
- _attrs: { a: "baz & < > \" bat" },
- _content: "foo & < > bar",
- };
- const result = toXML(val, { filter: false });
- const expectedResult = `<foo a="baz & < > " bat">foo & < > bar</foo>`;
- assert.equal(result, expectedResult);
- });
- it(`turns off both filter and attributesFilter`, () => {
- const val = {
- _name: "foo",
- _attrs: { a: "baz & < > \" bat" },
- _content: "foo & < > bar",
- };
- const result = toXML(val, { filter: false, attributesFilter: false });
- const expectedResult = `<foo a="baz & < > \" bat">foo & < > bar</foo>`;
- assert.equal(result, expectedResult);
- });
- });
- describe("misc", () => {
- it("outputs <_content> if it has no tag name", () => {
- const val = {
- _content: "foo",
- };
- const result = toXML(val);
- const expectedResult = "<_content>foo</_content>";
- assert.equal(result, expectedResult);
- });
- it("outputs emoji attributes", () => {
- const val = {
- html: {
- _attrs: [{ "⚡": true }, { lang: "en" }, { lang: "klingon" }],
- },
- };
- const result = toXML(val, { attributesFilter: {} });
- const expectedResult = '<html ⚡ lang="en" lang="klingon"/>';
- assert.equal(result, expectedResult);
- });
- it("outputs emoji attributes 2", () => {
- const val = {
- html: {
- _attrs: { "⚡": true, lang: "en" },
- },
- };
- const result = toXML(val, { attributesFilter: {} });
- const expectedResult = '<html ⚡ lang="en"/>';
- assert.equal(result, expectedResult);
- });
- it("does not force self close if tag has content", () => {
- const val = {
- _name: "foo",
- _selfCloseTag: true,
- _content: "bar",
- };
- const result = toXML(val);
- const expectedResult = "<foo>bar</foo>";
- assert.equal(result, expectedResult);
- });
- it("nested elements with self-closing sibling", () => {
- const val = {
- people: {
- students: [
- {
- student: { name: "Joe" },
- },
- {
- student: { name: "Jane" },
- },
- ],
- teacher: {
- _selfCloseTag: true,
- _attrs: {
- name: "Yoda",
- },
- },
- },
- };
- const result = toXML(val);
- const expectedResult =
- '<people><students><student><name>Joe</name></student><student><name>Jane</name></student></students><teacher name="Yoda"/></people>';
- assert.equal(result, expectedResult);
- });
- it("sibling _content tag", () => {
- const val = {
- foo: {
- bar: "baz",
- _content: {
- bar2: "baz2",
- },
- },
- };
- const result = toXML(val);
- const expectedResult = "<foo><bar>baz</bar><bar2>baz2</bar2></foo>";
- assert.equal(result, expectedResult);
- });
- });
- describe("examples", () => {
- it("1 simple object", () => {
- const val = {
- foo: "bar",
- foo2: "bar2",
- };
- const result = toXML(val);
- const expectedResult = "<foo>bar</foo><foo2>bar2</foo2>";
- assert.equal(result, expectedResult);
- });
- it("2 simple array", () => {
- const val = [{ foo: "bar" }, { foo: "bar2" }];
- const result = toXML(val);
- const expectedResult = "<foo>bar</foo><foo>bar2</foo>";
- assert.equal(result, expectedResult);
- });
- it("3 simple function", () => {
- const date = new Date();
- const val = {
- currentTime: () => date,
- };
- const result = toXML(val);
- const expectedResult = `<currentTime>${date}</currentTime>`;
- assert.equal(result, expectedResult);
- });
- it("4 attributes", () => {
- const val = {
- _name: "foo",
- _content: "bar",
- _attrs: {
- a: "b",
- c: "d",
- },
- };
- const result = toXML(val);
- const expectedResult = '<foo a="b" c="d">bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("5 tags with mixed content", () => {
- const val = {
- text1: null,
- foo: "bar",
- text2: null,
- };
- const result = toXML(val);
- const expectedResult = "text1<foo>bar</foo>text2";
- assert.equal(result, expectedResult);
- });
- it("6 nested tags with indent", () => {
- const val = {
- a: {
- foo: "bar",
- foo2: "bar2",
- },
- };
- const config = {
- header: false,
- indent: " ",
- };
- const result = toXML(val, config);
- const expectedResult = `<a>
- <foo>bar</foo>
- <foo2>bar2</foo2>
- </a>`;
- assert.equal(result, expectedResult);
- });
- it("7 nested tags attributes", () => {
- const val = {
- ooo: {
- _name: "foo",
- _attrs: {
- a: "b",
- },
- _content: {
- _name: "bar",
- _attrs: {
- c: "d",
- },
- },
- },
- };
- const config = {
- header: false,
- indent: " ",
- };
- const result = toXML(val, config);
- const expectedResult = `<ooo>
- <foo a="b">
- <bar c="d"/>
- </foo>
- </ooo>`;
- assert.equal(result, expectedResult);
- });
- it("8 complex functions", () => {
- const val = {
- someNestedXML: () => ({ foo: "bar" }),
- };
- const result = toXML(val);
- const expectedResult = "<someNestedXML><foo>bar</foo></someNestedXML>";
- assert.equal(result, expectedResult);
- });
- it("9 RSS feed", () => {
- const date = new Date();
- const val = {
- _name: "rss",
- _attrs: {
- version: "2.0",
- },
- _content: {
- channel: [
- { title: "RSS Example" },
- { description: "Description" },
- { link: "google.com" },
- { lastBuildDate: () => date },
- { pubDate: () => date },
- { language: "en" },
- {
- item: {
- title: "Item title",
- link: "Item link",
- description: "Item Description",
- pubDate: () => date,
- },
- },
- {
- item: {
- title: "Item2 title",
- link: "Item2 link",
- description: "Item2 Description",
- pubDate: () => date,
- },
- },
- ],
- },
- };
- const config = {
- header: true,
- indent: " ",
- };
- const result = toXML(val, config);
- const expectedResult = `<?xml version="1.0" encoding="UTF-8"?>
- <rss version="2.0">
- <channel>
- <title>RSS Example</title>
- <description>Description</description>
- <link>google.com</link>
- <lastBuildDate>${date}</lastBuildDate>
- <pubDate>${date}</pubDate>
- <language>en</language>
- <item>
- <title>Item title</title>
- <link>Item link</link>
- <description>Item Description</description>
- <pubDate>${date}</pubDate>
- </item>
- <item>
- <title>Item2 title</title>
- <link>Item2 link</link>
- <description>Item2 Description</description>
- <pubDate>${date}</pubDate>
- </item>
- </channel>
- </rss>`;
- assert.equal(result, expectedResult);
- });
- it("10 podcast RSS", () => {
- const val = {
- _name: "rss",
- _attrs: {
- "xmlns:itunes": "http://www.itunes.com/dtds/podcast-1.0.dtd",
- version: "2.0",
- },
- _content: {
- channel: [
- { title: "Title" },
- { link: "google.com" },
- { language: "en-us" },
- { copyright: "Copyright 2011" },
- { "itunes:subtitle": "Subtitle" },
- { "itunes:author": "Author" },
- { "itunes:summary": "Summary" },
- { description: "Description" },
- {
- "itunes:owner": {
- "itunes:name": "Name",
- "itunes:email": "Email",
- },
- },
- {
- _name: "itunes:image",
- _attrs: {
- href: "image.jpg",
- },
- },
- {
- _name: "itunes:category",
- _attrs: {
- text: "Technology",
- },
- _content: {
- _name: "itunes:category",
- _attrs: {
- text: "Gadgets",
- },
- },
- },
- {
- _name: "itunes:category",
- _attrs: {
- text: "TV & Film",
- },
- },
- {
- item: [
- { title: "Podcast Title" },
- { "itunes:author": "Author" },
- { "itunes:subtitle": "Subtitle" },
- { "itunes:summary": "Summary" },
- { "itunes:image": "image.jpg" },
- {
- _name: "enclosure",
- _attrs: {
- url: "http://example.com/podcast.m4a",
- length: "8727310",
- type: "audio/x-m4a",
- },
- },
- { guid: "http://example.com/archive/aae20050615.m4a" },
- { pubDate: "Wed, 15 Jun 2011 19:00:00 GMT" },
- { "itunes:duration": "7:04" },
- { "itunes:keywords": "salt, pepper, shaker, exciting" },
- ],
- },
- {
- item: [
- { title: "Podcast2 Title" },
- { "itunes:author": "Author2" },
- { "itunes:subtitle": "Subtitle2" },
- { "itunes:summary": "Summary2" },
- { "itunes:image": "image2.jpg" },
- {
- _name: "enclosure",
- _attrs: {
- url: "http://example.com/podcast2.m4a",
- length: "655555",
- type: "audio/x-m4a",
- },
- },
- { guid: "http://example.com/archive/aae2.m4a" },
- { pubDate: "Wed, 15 Jul 2011 19:00:00 GMT" },
- { "itunes:duration": "11:20" },
- { "itunes:keywords": "foo, bar" },
- ],
- },
- ],
- },
- };
- const config = {
- header: true,
- indent: " ",
- };
- const result = toXML(val, config);
- const expectedResult = `<?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
- <channel>
- <title>Title</title>
- <link>google.com</link>
- <language>en-us</language>
- <copyright>Copyright 2011</copyright>
- <itunes:subtitle>Subtitle</itunes:subtitle>
- <itunes:author>Author</itunes:author>
- <itunes:summary>Summary</itunes:summary>
- <description>Description</description>
- <itunes:owner>
- <itunes:name>Name</itunes:name>
- <itunes:email>Email</itunes:email>
- </itunes:owner>
- <itunes:image href="image.jpg"/>
- <itunes:category text="Technology">
- <itunes:category text="Gadgets"/>
- </itunes:category>
- <itunes:category text="TV & Film"/>
- <item>
- <title>Podcast Title</title>
- <itunes:author>Author</itunes:author>
- <itunes:subtitle>Subtitle</itunes:subtitle>
- <itunes:summary>Summary</itunes:summary>
- <itunes:image>image.jpg</itunes:image>
- <enclosure url="http://example.com/podcast.m4a" length="8727310" type="audio/x-m4a"/>
- <guid>http://example.com/archive/aae20050615.m4a</guid>
- <pubDate>Wed, 15 Jun 2011 19:00:00 GMT</pubDate>
- <itunes:duration>7:04</itunes:duration>
- <itunes:keywords>salt, pepper, shaker, exciting</itunes:keywords>
- </item>
- <item>
- <title>Podcast2 Title</title>
- <itunes:author>Author2</itunes:author>
- <itunes:subtitle>Subtitle2</itunes:subtitle>
- <itunes:summary>Summary2</itunes:summary>
- <itunes:image>image2.jpg</itunes:image>
- <enclosure url="http://example.com/podcast2.m4a" length="655555" type="audio/x-m4a"/>
- <guid>http://example.com/archive/aae2.m4a</guid>
- <pubDate>Wed, 15 Jul 2011 19:00:00 GMT</pubDate>
- <itunes:duration>11:20</itunes:duration>
- <itunes:keywords>foo, bar</itunes:keywords>
- </item>
- </channel>
- </rss>`;
- assert.equal(result, expectedResult);
- });
- it("11 filter", () => {
- const val = {
- foo: "<a>",
- bar: '"b"',
- baz: "'&whee'",
- };
- const config = {
- filter: {
- "<": "<",
- ">": ">",
- '"': """,
- "'": "'",
- "&": "&",
- },
- };
- const result = toXML(val, config);
- const expectedResult =
- "<foo><a></foo><bar>"b"</bar><baz>'&whee'</baz>";
- assert.equal(result, expectedResult);
- });
- it("11b attributes filter", () => {
- const val = {
- _name: "foo",
- _content: "bar",
- _attrs: {
- a: "http://example.com/?test='1'&foo=<bar>&whee=\"sha\"",
- b: "http://example2.com/?test='2'&md=<5>&sum=\"sha\"",
- },
- };
- const config = {
- attributesFilter: {
- "<": "<",
- ">": ">",
- '"': """,
- "'": "'",
- "&": "&",
- },
- };
- const result = toXML(val, config);
- const expectedResult =
- '<foo a="http://example.com/?test='1'&foo=<bar>&whee="sha"" b="http://example2.com/?test='2'&md=<5>&sum="sha"">bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("12 avoiding self closing tags", () => {
- const val = [
- {
- _name: "foo",
- _content: "",
- _selfCloseTag: false,
- },
- {
- _name: "bar",
- _content: undefined,
- _selfCloseTag: false,
- },
- ];
- const result = toXML(val);
- const expectedResult = "<foo></foo><bar></bar>";
- assert.equal(result, expectedResult);
- });
- it("13 custom xml header", () => {
- const val = {
- foo: "bar",
- };
- const config = {
- header: '<?xml version="1.0" encoding="UTF-16" standalone="yes"?>',
- };
- const result = toXML(val, config);
- const expectedResult =
- '<?xml version="1.0" encoding="UTF-16" standalone="yes"?><foo>bar</foo>';
- assert.equal(result, expectedResult);
- });
- it("14 emoji attributes", () => {
- const val = {
- html: {
- _attrs: {
- "⚡": true,
- },
- },
- };
- const result = toXML(val);
- const expectedResult = "<html ⚡/>";
- assert.equal(result, expectedResult);
- });
- it("15 duplicate attribute keys", () => {
- const val = {
- html: {
- _attrs: [{ lang: "en" }, { lang: "klingon" }],
- },
- };
- const result = toXML(val);
- const expectedResult = '<html lang="en" lang="klingon"/>';
- assert.equal(result, expectedResult);
- });
- });
- describe("issues", () => {
- it("issue #33: array of primitives", () => {
- const val = {
- x: [1, 2, 3],
- };
- const result = toXML(val);
- const expectedResult = "<x>1</x><x>2</x><x>3</x>";
- assert.equal(result, expectedResult);
- });
- it("issue #33: array of primitives 2", () => {
- const val = {
- a: {
- x: [1, 2, 3],
- },
- };
- const result = toXML(val);
- const expectedResult = "<a><x>1</x><x>2</x><x>3</x></a>";
- assert.equal(result, expectedResult);
- });
- it("issue #33: array of primitives 2 with indent", () => {
- const val = {
- a: {
- x: [1, 2, 3],
- },
- };
- const config = { indent: " " };
- const result = toXML(val, config);
- const expectedResult = "<a>\n <x>1</x>\n <x>2</x>\n <x>3</x>\n</a>";
- assert.equal(result, expectedResult);
- });
- it("issue #33: array of objects", () => {
- const val = {
- a: {
- x: [
- { b: 1, c: 2 },
- { d: 3, e: 4 },
- { f: 5, g: 6 },
- ],
- },
- };
- const result = toXML(val);
- const expectedResult =
- "<a><x><b>1</b><c>2</c><d>3</d><e>4</e><f>5</f><g>6</g></x></a>";
- assert.equal(result, expectedResult);
- });
- it("issue #33: array of objects jstoxml format", () => {
- const val = {
- a: [
- {
- _name: "foo",
- _content: "1",
- },
- {
- _name: "foo",
- _content: "2",
- },
- ],
- };
- const result = toXML(val);
- const expectedResult = "<a><foo>1</foo><foo>2</foo></a>";
- assert.equal(result, expectedResult);
- });
- it("issue #34: array of array", () => {
- const val = {
- Response: [
- [
- {
- _name: "Play",
- _content: "first sound",
- },
- {
- _name: "Play",
- _content: "second sound",
- },
- ],
- ],
- };
- const result = toXML(val);
- const expectedResult =
- "<Response><Play>first sound</Play><Play>second sound</Play></Response>";
- assert.equal(result, expectedResult);
- });
- it("issue #34", () => {
- const val = { t: [{ foo: "bar" }, { foo: "bar2" }] };
- const result = toXML(val);
- const expectedResult = "<t><foo>bar</foo><foo>bar2</foo></t>";
- assert.equal(result, expectedResult);
- });
- it("issue #34", () => {
- const val = {
- t: [
- { _name: "foo", _content: "bar" },
- { _name: "foo", _content: "bar2" },
- ],
- };
- const result = toXML(val);
- const expectedResult = "<t><foo>bar</foo><foo>bar2</foo></t>";
- assert.equal(result, expectedResult);
- });
- it("issue #38", () => {
- const getFooVal = (iteration) => iteration;
- const getCurrentTime = (iterations) => {
- return Array(iterations)
- .fill(null)
- .map((foo, index) => {
- return {
- currentTime: {
- foo: getFooVal.bind(null, index + 1),
- },
- };
- });
- };
- const val = {
- invoice1: [
- {
- invoice: "a",
- },
- getCurrentTime.bind(null, 3),
- {
- foo2: "a",
- },
- ],
- };
- const config = { indent: " " };
- const result = toXML(val, config);
- const expectedResult = `<invoice1>
- <invoice>a</invoice>
- <currentTime>
- <foo>1</foo>
- </currentTime>
- <currentTime>
- <foo>2</foo>
- </currentTime>
- <currentTime>
- <foo>3</foo>
- </currentTime>
- <foo2>a</foo2>
- </invoice1>`;
- assert.equal(result, expectedResult);
- });
- it("issue #40 forced separator, no indent", () => {
- const val = [{ a: "A Value" }, "\n", { b: "B Value" }];
- const result = toXML(val);
- const expectedResult = `<a>A Value</a>
- <b>B Value</b>`;
- assert.equal(result, expectedResult);
- });
- it("issue #40 array with indent", () => {
- const val = [{ a: "A Value" }, { b: "B Value" }];
- const result = toXML(val, { indent: " " });
- const expectedResult = `<a>A Value</a>
- <b>B Value</b>`;
- assert.equal(result, expectedResult);
- });
- it("issue #40 array without indent", () => {
- const val = [{ a: "A Value" }, { b: "B Value" }];
- const result = toXML(val);
- const expectedResult = `<a>A Value</a><b>B Value</b>`;
- assert.equal(result, expectedResult);
- });
- it("issue #40 object with indent", () => {
- const val = {
- a: "A Value",
- b: "B Value",
- };
- const result = toXML(val, { indent: " " });
- const expectedResult = `<a>A Value</a>
- <b>B Value</b>`;
- assert.equal(result, expectedResult);
- });
- it("issue #40 object without indent", () => {
- const val = {
- a: "A Value",
- b: "B Value",
- };
- const result = toXML(val);
- const expectedResult = `<a>A Value</a><b>B Value</b>`;
- assert.equal(result, expectedResult);
- });
- it("comments 1", () => {
- const val = {
- _comment: "test comment",
- a: "foo"
- };
- const result = toXML(val);
- const expectedResult = `<!-- test comment --><a>foo</a>`;
- assert.equal(result, expectedResult);
- });
- it("comments 2", () => {
- const val = {
- _comment: "test comment",
- a: "foo"
- };
- const result = toXML(val, { indent: ' ' });
- const expectedResult = `<!-- test comment -->
- <a>foo</a>`;
- assert.equal(result, expectedResult);
- });
- it("comments 3", () => {
- const val = {
- _comment: "comment 1",
- b: {
- _comment: "comment 2",
- a: "foo"
- }
- };
- const result = toXML(val, { indent: ' ' });
- const expectedResult = `<!-- comment 1 -->
- <b>
- <!-- comment 2 -->
- <a>foo</a>
- </b>`;
- assert.equal(result, expectedResult);
- });
- it("comments 4", () => {
- const val = {
- _comment: "comment 1",
- b: [
- { _comment: "comment 2" },
- { _comment: "comment 3" },
- { a: "foo" }
- ]
- };
- const result = toXML(val, { indent: ' ' });
- const expectedResult = `<!-- comment 1 -->
- <b>
- <!-- comment 2 -->
- <!-- comment 3 -->
- <a>foo</a>
- </b>`;
- assert.equal(result, expectedResult);
- });
- });
- it("comments 5", () => {
- const val = {
- _comment: 'Some important comment',
- a: {
- b: [1, 2, 3]
- }
- };
- const result = toXML(val, { indent: ' ' });
- const expectedResult = `<!-- Some important comment -->
- <a>
- <b>1</b>
- <b>2</b>
- <b>3</b>
- </a>`;
- assert.equal(result, expectedResult);
- });
- it("comments 6", () => {
- const val = [
- { _comment: 'Some important comment' },
- { _comment: 'This is a very long comment!' },
- { _comment: 'More important exposition!' },
- { a: { b: [1, 2, 3] } }
- ];
- const result = toXML(val, { indent: ' ' });
- const expectedResult = `<!-- Some important comment -->
- <!-- This is a very long comment! -->
- <!-- More important exposition! -->
- <a>
- <b>1</b>
- <b>2</b>
- <b>3</b>
- </a>`;
- assert.equal(result, expectedResult);
- });
- });
|