Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@xmpp/xml

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xmpp/xml - npm Package Compare versions

Comparing version 0.11.0 to 0.12.0

20

index.js

@@ -1,6 +0,6 @@

'use strict'
"use strict";
const x = require('./lib/x')
const Element = require('./lib/Element')
const Parser = require('./lib/Parser')
const x = require("./lib/x");
const Element = require("./lib/Element");
const Parser = require("./lib/Parser");
const {

@@ -11,12 +11,12 @@ escapeXML,

unescapeXMLText,
} = require('ltx/lib/escape')
const XMLError = require('./lib/XMLError')
} = require("ltx/lib/escape");
const XMLError = require("./lib/XMLError");
function xml(...args) {
return x(...args)
return x(...args);
}
exports = module.exports = xml
module.exports = xml;
Object.assign(exports, {
Object.assign(module.exports, {
x,

@@ -30,2 +30,2 @@ Element,

XMLError,
})
});

@@ -1,2 +0,2 @@

'use strict'
"use strict";

@@ -7,8 +7,8 @@ // https://github.com/xmppjs/ltx/issues/

const _clone = require('ltx/lib/clone')
const _clone = require("ltx/lib/clone");
module.exports = function clone(element) {
const c = _clone(element)
c.attrs.xmlns = element.getNS()
return c
}
const c = _clone(element);
c.attrs.xmlns = element.getNS();
return c;
};

@@ -1,18 +0,18 @@

'use strict'
"use strict";
const _Element = require('ltx/lib/Element')
const _Element = require("ltx/lib/Element");
class Element extends _Element {
setAttrs(attrs) {
if (typeof attrs === 'string') {
this.attrs.xmlns = attrs
if (typeof attrs === "string") {
this.attrs.xmlns = attrs;
} else if (attrs) {
Object.keys(attrs).forEach(function(key) {
Object.keys(attrs).forEach((key) => {
// https://github.com/facebook/react/pull/4596
// https://www.npmjs.com/package/babel-plugin-transform-react-jsx-source
if (key === '__source' || key === '__self') return
const val = attrs[key]
if (key === "__source" || key === "__self") return;
const val = attrs[key];
if (val !== undefined && val !== null)
this.attrs[key.toString()] = val.toString()
}, this)
this.attrs[key.toString()] = val.toString();
}, this);
}

@@ -22,24 +22,24 @@ }

append(nodes) {
nodes = Array.isArray(nodes) ? nodes : [nodes]
nodes.forEach(node => {
this.children.push(node)
if (typeof node === 'object') {
node.parent = this
nodes = Array.isArray(nodes) ? nodes : [nodes];
nodes.forEach((node) => {
this.children.push(node);
if (typeof node === "object") {
node.parent = this;
}
})
return this
});
return this;
}
prepend(nodes) {
nodes = Array.isArray(nodes) ? nodes : [nodes]
nodes.forEach(node => {
this.children.unshift(node)
if (typeof node === 'object') {
node.parent = this
nodes = Array.isArray(nodes) ? nodes : [nodes];
nodes.forEach((node) => {
this.children.unshift(node);
if (typeof node === "object") {
node.parent = this;
}
})
return this
});
return this;
}
}
module.exports = Element
module.exports = Element;

@@ -1,29 +0,29 @@

'use strict'
"use strict";
const Parser = require('./Parser')
const Parser = require("./Parser");
module.exports = function parse(data) {
const p = new Parser()
const p = new Parser();
let result = null
let error = null
let result = null;
let error = null;
p.on('start', el => {
result = el
})
p.on('element', el => {
result.append(el)
})
p.on('error', err => {
error = err
})
p.on("start", (el) => {
result = el;
});
p.on("element", (el) => {
result.append(el);
});
p.on("error", (err) => {
error = err;
});
p.write(data)
p.end()
p.write(data);
p.end();
if (error) {
throw error
throw error;
} else {
return result
return result;
}
}
};

@@ -1,72 +0,72 @@

'use strict'
"use strict";
const LtxParser = require('ltx/lib/parsers/ltx')
const Element = require('./Element')
const EventEmitter = require('events')
const XMLError = require('./XMLError')
const LtxParser = require("ltx/lib/parsers/ltx");
const Element = require("./Element");
const EventEmitter = require("events");
const XMLError = require("./XMLError");
class Parser extends EventEmitter {
constructor() {
super()
const parser = new LtxParser()
this.root = null
this.cursor = null
super();
const parser = new LtxParser();
this.root = null;
this.cursor = null;
parser.on('startElement', this.onStartElement.bind(this))
parser.on('endElement', this.onEndElement.bind(this))
parser.on('text', this.onText.bind(this))
parser.on("startElement", this.onStartElement.bind(this));
parser.on("endElement", this.onEndElement.bind(this));
parser.on("text", this.onText.bind(this));
this.parser = parser
this.parser = parser;
}
onStartElement(name, attrs) {
const element = new Element(name, attrs)
const element = new Element(name, attrs);
const {root, cursor} = this
const { root, cursor } = this;
if (!root) {
this.root = element
this.emit('start', element)
this.root = element;
this.emit("start", element);
} else if (cursor !== root) {
cursor.append(element)
cursor.append(element);
}
this.cursor = element
this.cursor = element;
}
onEndElement(name) {
const {root, cursor} = this
const { root, cursor } = this;
if (name !== cursor.name) {
// <foo></bar>
this.emit('error', new XMLError(`${cursor.name} must be closed.`))
return
this.emit("error", new XMLError(`${cursor.name} must be closed.`));
return;
}
if (cursor === root) {
this.emit('end', root)
return
this.emit("end", root);
return;
}
if (!cursor.parent) {
cursor.parent = root
this.emit('element', cursor)
this.cursor = root
return
cursor.parent = root;
this.emit("element", cursor);
this.cursor = root;
return;
}
this.cursor = cursor.parent
this.cursor = cursor.parent;
}
onText(str) {
const {cursor} = this
const { cursor } = this;
if (!cursor) {
this.emit('error', new XMLError(`${str} must be a child.`))
return
this.emit("error", new XMLError(`${str} must be a child.`));
return;
}
cursor.t(str)
cursor.t(str);
}
write(data) {
this.parser.write(data)
this.parser.write(data);
}

@@ -76,3 +76,3 @@

if (data) {
this.parser.write(data)
this.parser.write(data);
}

@@ -82,4 +82,4 @@ }

Parser.XMLError = XMLError
Parser.XMLError = XMLError;
module.exports = Parser
module.exports = Parser;

@@ -1,5 +0,5 @@

'use strict'
"use strict";
const serialize = require('ltx/lib/stringify')
const serialize = require("ltx/lib/stringify");
module.exports = serialize
module.exports = serialize;

@@ -1,13 +0,13 @@

'use strict'
"use strict";
const Element = require('./Element')
const Element = require("./Element");
function append(el, child) {
if (child === false || child === null || child === undefined) return
if (child === false || child === null || child === undefined) return;
if (child instanceof Element) {
el.append(child)
el.append(child);
} else if (Array.isArray(child)) {
child.forEach(c => append(el, c))
child.forEach((c) => append(el, c));
} else {
el.append(String(child))
el.append(String(child));
}

@@ -17,11 +17,11 @@ }

function x(name, attrs, ...children) {
const el = new Element(name, attrs)
const el = new Element(name, attrs);
// eslint-disable-next-line unicorn/no-for-loop
for (let i = 0; i < children.length; i++) {
append(el, children[i])
append(el, children[i]);
}
return el
return el;
}
module.exports = x
module.exports = x;

@@ -1,8 +0,8 @@

'use strict'
"use strict";
module.exports = class XMLError extends Error {
constructor(...args) {
super(...args)
this.name = 'XMLError'
super(...args);
this.name = "XMLError";
}
}
};

@@ -7,3 +7,3 @@ {

"bugs": "http://github.com/xmppjs/xmpp.js/issues",
"version": "0.11.0",
"version": "0.12.0",
"license": "ISC",

@@ -18,6 +18,6 @@ "keywords": [

"dependencies": {
"ltx": "^2.9.2"
"ltx": "^2.10.0"
},
"engines": {
"node": ">= 10.0.0",
"node": ">= 12.4.0",
"yarn": ">= 1.0.0"

@@ -28,3 +28,3 @@ },

},
"gitHead": "c0548a598826ae55cf195f296058b344064af7fd"
"gitHead": "75f7bdf7805dced03f616b66dc16e2a067b46480"
}

@@ -10,5 +10,5 @@ # xml

```js
const xml = require('@xmpp/xml')
const {xml} = require('@xmpp/client')
const {xml} = require('@xmpp/component')
const xml = require("@xmpp/xml");
const { xml } = require("@xmpp/client");
const { xml } = require("@xmpp/component");
```

@@ -23,12 +23,16 @@

```js
const xml = require('@xmpp/xml')
const xml = require("@xmpp/xml");
const recipient = 'user@example.com'
const days = ['Monday', 'Tuesday', 'Wednesday']
const recipient = "user@example.com";
const days = ["Monday", "Tuesday", "Wednesday"];
const message = xml(
'message',
{to: recipient},
xml('body', {}, 1 + 2),
xml('days', {}, days.map((day, idx) => xml('day', {idx}, day)))
)
"message",
{ to: recipient },
xml("body", {}, 1 + 2),
xml(
"days",
{},
days.map((day, idx) => xml("day", { idx }, day)),
),
);
```

@@ -40,4 +44,4 @@

// both are equivalent
xml('time', 'urn:xmpp:time')
xml('time', {xmlns: 'urn:xmpp:time'})
xml("time", "urn:xmpp:time");
xml("time", { xmlns: "urn:xmpp:time" });
```

@@ -50,6 +54,6 @@

const xml = require('@xmpp/xml')
const xml = require("@xmpp/xml");
const recipient = 'user@example.com'
const days = ['Monday', 'Tuesday']
const recipient = "user@example.com";
const days = ["Monday", "Tuesday"];
const message = (

@@ -64,3 +68,3 @@ <message to={recipient}>

</message>
)
);
```

@@ -77,3 +81,3 @@

```js
message.attrs.to // user@example.com
message.attrs.to; // user@example.com
```

@@ -86,3 +90,3 @@

```js
message.getChild('body').text() // '3'
message.getChild("body").text(); // '3'
```

@@ -95,3 +99,3 @@

```js
message.getChild('body').toString() // '<body>3</body>'
message.getChild("body").toString(); // '<body>3</body>'
```

@@ -104,3 +108,3 @@

```js
message.getChildText('body') // '3'
message.getChildText("body"); // '3'
```

@@ -113,3 +117,3 @@

```js
message.getChild('days').getChildren('day') // [...]
message.getChild("days").getChildren("day"); // [...]
```

@@ -120,10 +124,10 @@

```js
const days = message.getChild('days').getChildren('day')
const days = message.getChild("days").getChildren("day");
// Find Monday element
days.find(day => day.text() === 'Monday')
days.find(day => day.attrs.idx === 0)
days.find((day) => day.text() === "Monday");
days.find((day) => day.attrs.idx === 0);
// Find all days after Tuesday
days.filter(day => day.attrs.idx > 2)
days.filter((day) => day.attrs.idx > 2);
```

@@ -136,3 +140,3 @@

```js
console.log(message.getChild('days').parent === message)
console.log(message.getChild("days").parent === message);
```

@@ -145,3 +149,3 @@

```js
console.log(message.getChild('days').root() === message)
console.log(message.getChild("days").root() === message);
```

@@ -156,4 +160,4 @@

```js
message.attrs.type = 'chat'
Object.assign(message.attrs, {type: 'chat'})
message.attrs.type = "chat";
Object.assign(message.attrs, { type: "chat" });
```

@@ -166,3 +170,3 @@

```js
message.getChild('body').text('Hello world')
message.getChild("body").text("Hello world");
```

@@ -176,5 +180,5 @@

```js
message.append(xml('foo'))
message.append('bar')
message.append(days.map(day => xml('day', {}, day)))
message.append(xml("foo"));
message.append("bar");
message.append(days.map((day) => xml("day", {}, day)));
// <message>

@@ -195,5 +199,5 @@ // ...

```js
message.prepend(xml('foo'))
message.prepend('bar')
message.prepend(days.map(day => xml('day', {}, day)))
message.prepend(xml("foo"));
message.prepend("bar");
message.prepend(days.map((day) => xml("day", {}, day)));
// <message>

@@ -213,4 +217,4 @@ // <day>Tuesday</day>

```js
const body = message.getChild('body')
message.remove(body)
const body = message.getChild("body");
message.remove(body);
```

@@ -229,4 +233,4 @@

<json xmlns="urn:xmpp:json:0">{JSON.stringify(days)}</json>
</myevent>
)
</myevent>,
);

@@ -236,7 +240,7 @@ // read

message
.getChild('myevent', 'xmpp:example.org')
.getChildText('json', 'urn:xmpp:json:0')
)
.getChild("myevent", "xmpp:example.org")
.getChildText("json", "urn:xmpp:json:0"),
);
```
See [JSON Containers](https://xmpp.org/extensions/xep-0335.html)

@@ -1,11 +0,11 @@

'use strict'
"use strict";
const test = require('ava')
const xml = require('..')
const clone = require('../lib/clone')
const test = require("ava");
const xml = require("..");
const clone = require("../lib/clone");
test('adopts parent namespace', t => {
const el = xml('foo', {xmlns: 'bar'}, xml('bar'))
test("adopts parent namespace", (t) => {
const el = xml("foo", { xmlns: "bar" }, xml("bar"));
t.deepEqual(clone(el.getChild('bar')), xml('bar', {xmlns: 'bar'}))
})
t.deepEqual(clone(el.getChild("bar")), xml("bar", { xmlns: "bar" }));
});

@@ -1,17 +0,17 @@

'use strict'
"use strict";
const test = require('ava')
const Element = require('../lib/Element')
const test = require("ava");
const Element = require("../lib/Element");
// TODO probably better to ignore in serialization instead
test('ignore __self and __source attributes', t => {
const el = new Element('foo', {
__source: 'source',
__self: 'self',
foo: 'bar',
})
test("ignore __self and __source attributes", (t) => {
const el = new Element("foo", {
__source: "source",
__self: "self",
foo: "bar",
});
t.is(el.attrs.foo, 'bar')
t.false('__source' in el.attrs)
t.false('__self' in el.attrs)
})
t.is(el.attrs.foo, "bar");
t.false("__source" in el.attrs);
t.false("__self" in el.attrs);
});

@@ -1,30 +0,30 @@

'use strict'
"use strict";
const test = require('ava')
const Parser = require('../lib/Parser')
const test = require("ava");
const Parser = require("../lib/Parser");
test.cb('stream parser', t => {
const parser = new Parser()
test.cb("stream parser", (t) => {
const parser = new Parser();
t.plan(5)
t.plan(5);
let startElement
let startElement;
parser.on('start', el => {
t.is(el.toString(), '<foo/>')
startElement = el
})
parser.on("start", (el) => {
t.is(el.toString(), "<foo/>");
startElement = el;
});
parser.on('element', el => {
t.is(el.parent, startElement)
t.is(startElement.children.length, 0)
t.is(el.toString(), '<bar>hello</bar>')
})
parser.on("element", (el) => {
t.is(el.parent, startElement);
t.is(startElement.children.length, 0);
t.is(el.toString(), "<bar>hello</bar>");
});
parser.on('end', el => {
t.is(el.toString(), '<foo/>')
t.end()
})
parser.on("end", (el) => {
t.is(el.toString(), "<foo/>");
t.end();
});
parser.write('<foo><bar>hello</bar></foo>')
})
parser.write("<foo><bar>hello</bar></foo>");
});

@@ -1,22 +0,22 @@

'use strict'
"use strict";
const test = require('ava')
const x = require('../lib/x')
const test = require("ava");
const x = require("../lib/x");
test('ignore false children', t => {
const el = x('foo', {}, false)
test("ignore false children", (t) => {
const el = x("foo", {}, false);
t.is(el.children.length, 0)
})
t.is(el.children.length, 0);
});
test('ignore null children', t => {
const el = x('foo', {}, null)
test("ignore null children", (t) => {
const el = x("foo", {}, null);
t.is(el.children.length, 0)
})
t.is(el.children.length, 0);
});
test('ignore undefined children', t => {
const el = x('foo', {}, undefined)
test("ignore undefined children", (t) => {
const el = x("foo", {}, undefined);
t.is(el.children.length, 0)
})
t.is(el.children.length, 0);
});

@@ -1,8 +0,8 @@

'use strict'
"use strict";
const test = require('ava')
const xml = require('..')
const x = require('../lib/x')
const Element = require('../lib/Element')
const Parser = require('../lib/Parser')
const test = require("ava");
const xml = require("..");
const x = require("../lib/x");
const Element = require("../lib/Element");
const Parser = require("../lib/Parser");
const {

@@ -13,21 +13,21 @@ escapeXML,

unescapeXMLText,
} = require('ltx/lib/escape')
} = require("ltx/lib/escape");
test('exports x', t => {
t.is(xml.x, x)
})
test("exports x", (t) => {
t.is(xml.x, x);
});
test('exports Parser', t => {
t.is(xml.Parser, Parser)
})
test("exports Parser", (t) => {
t.is(xml.Parser, Parser);
});
test('exports Element', t => {
t.is(xml.Element, Element)
})
test("exports Element", (t) => {
t.is(xml.Element, Element);
});
test('exports escape methods', t => {
t.is(xml.escapeXML, escapeXML)
t.is(xml.unescapeXML, unescapeXML)
t.is(xml.escapeXMLText, escapeXMLText)
t.is(xml.unescapeXMLText, unescapeXMLText)
})
test("exports escape methods", (t) => {
t.is(xml.escapeXML, escapeXML);
t.is(xml.unescapeXML, unescapeXML);
t.is(xml.escapeXMLText, escapeXMLText);
t.is(xml.unescapeXMLText, unescapeXMLText);
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc