Comparing version 0.3.0 to 0.5.0
@@ -6,2 +6,14 @@ 'use strict' | ||
class Element extends _Element { | ||
setAttrs(attrs) { | ||
if (typeof attrs === 'string') { | ||
this.attrs.xmlns = attrs | ||
} else if (attrs) { | ||
Object.keys(attrs).forEach(function(key) { | ||
const val = attrs[key] | ||
if (val !== undefined && val !== null) | ||
this.attrs[key.toString()] = val.toString() | ||
}, this) | ||
} | ||
} | ||
append(nodes) { | ||
@@ -15,2 +27,3 @@ nodes = Array.isArray(nodes) ? nodes : [nodes] | ||
}) | ||
return this | ||
} | ||
@@ -26,2 +39,3 @@ | ||
}) | ||
return this | ||
} | ||
@@ -28,0 +42,0 @@ } |
@@ -11,5 +11,8 @@ 'use strict' | ||
p.on('end', tree => { | ||
result = tree | ||
p.on('start', el => { | ||
result = el | ||
}) | ||
p.on('element', el => { | ||
result.append(el) | ||
}) | ||
p.on('error', err => { | ||
@@ -16,0 +19,0 @@ error = err |
@@ -18,53 +18,65 @@ 'use strict' | ||
const parser = new LtxParser() | ||
const stack = [] | ||
let cursor | ||
this.root = null | ||
this.cursor = null | ||
parser.on('startElement', (name, attrs) => { | ||
const child = new Element(name, attrs) | ||
if (cursor) { | ||
cursor.append(child) | ||
} | ||
this.onStartElement(child, cursor) | ||
this.emit('startElement', child) | ||
stack.push(cursor) | ||
cursor = child | ||
}) | ||
parser.on('endElement', name => { | ||
if (name === cursor.name) { | ||
this.onEndElement(cursor, stack.length) | ||
this.emit('endElement', cursor) | ||
cursor = stack.pop() | ||
} else { | ||
// <foo></bar> | ||
this.emit('error', new XMLError(`${cursor.name} must be closed.`)) | ||
} | ||
}) | ||
parser.on('startElement', this.onStartElement.bind(this)) | ||
parser.on('endElement', this.onEndElement.bind(this)) | ||
parser.on('text', this.onText.bind(this)) | ||
parser.on('text', str => { | ||
this.onText(str, cursor) | ||
}) | ||
this.parser = parser | ||
} | ||
onStartElement(element, cursor) { | ||
if (!cursor) { | ||
onStartElement(name, attrs) { | ||
const element = new Element(name, attrs) | ||
const {root, cursor} = this | ||
if (!root) { | ||
this.root = element | ||
this.emit('start', element) | ||
} else if (cursor !== root) { | ||
cursor.append(element) | ||
} | ||
this.cursor = element | ||
} | ||
onEndElement(element, length) { | ||
if (length === 2) { | ||
this.emit('element', element) | ||
} else if (length === 1) { | ||
this.emit('end', element) | ||
onEndElement(name) { | ||
const {root, cursor} = this | ||
if (name !== cursor.name) { | ||
// <foo></bar> | ||
this.emit('error', new XMLError(`${cursor.name} must be closed.`)) | ||
return | ||
} | ||
if (cursor === root) { | ||
this.emit('end', root) | ||
return | ||
} | ||
if (!cursor.parent) { | ||
if (cursor.name.startsWith('stream:')) { | ||
cursor.attrs['xmlns:stream'] = root.attrs['xmlns:stream'] | ||
} | ||
this.emit('element', cursor) | ||
this.cursor = root | ||
return | ||
} | ||
this.cursor = cursor.parent | ||
} | ||
onText(str, element) { | ||
if (!element) { | ||
onText(str) { | ||
const {cursor} = this | ||
if (!cursor) { | ||
this.emit('error', new XMLError(`${str} must be a child.`)) | ||
return | ||
} | ||
element.t(str) | ||
cursor.t(str) | ||
} | ||
write(data) { | ||
this.parser.write(data) | ||
} | ||
end(data) { | ||
@@ -71,0 +83,0 @@ if (data) { |
@@ -7,3 +7,3 @@ { | ||
"bugs": "http://github.com/xmppjs/xmpp.js/issues", | ||
"version": "0.3.0", | ||
"version": "0.5.0", | ||
"license": "ISC", | ||
@@ -18,8 +18,12 @@ "keywords": [ | ||
"dependencies": { | ||
"ltx": "^2.7.1" | ||
"ltx": "^2.8.0" | ||
}, | ||
"engines": { | ||
"node": ">= 6", | ||
"npm": ">= 2" | ||
} | ||
"node": ">= 10.0.0", | ||
"yarn": ">= 1.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "896f432d22dd726d158bd39a46aed7d53a2b7b1b" | ||
} |
192
README.md
@@ -1,25 +0,189 @@ | ||
XML | ||
=== | ||
# xml | ||
XMPP XML for JavaScript. | ||
## Install | ||
Note, if you're using `@xml/client` or `@xml/component`, you don't need to install `@xmpp/xml` yourself. | ||
`npm install @xmpp/xml` or `yarn add @xmpp/xml` | ||
```js | ||
const xml = require('@xmpp/xml') | ||
const {xml} = require('@xmpp/client') | ||
const {xml} = require('@xmpp/component') | ||
``` | ||
npm install @xmpp/xml | ||
## Writing | ||
There's 2 methods for writing XML with xmpp.js | ||
### factory | ||
```js | ||
const xml = require('@xmpp/xml') | ||
const recipient = 'user@example.com' | ||
const days = ['Monday', 'Tuesday'] | ||
const message = xml( | ||
'message', | ||
{to: recipient}, | ||
xml('body', {}, 1 + 2), | ||
xml('days', days.map(day => xml('day', {}, day))) | ||
) | ||
``` | ||
## Usage | ||
Used in xmpp.js source code. | ||
```javascript | ||
### JSX | ||
```js | ||
/** @jsx xml */ | ||
const xml = require('@xmpp/xml') | ||
const body = ' hello ' | ||
const stanza = xml` | ||
<message> | ||
<body>${body}</body> | ||
const recipient = 'user@example.com' | ||
const days = ['Monday', 'Tuesday'] | ||
const message = ( | ||
<message to={recipient}> | ||
<body>{1 + 2}</body> | ||
<days> | ||
{days.map(day => ( | ||
<day>${day}</day> | ||
))} | ||
</days> | ||
</message> | ||
` | ||
console.log(stanza.toString()) | ||
// <message><body> hello </body></message> | ||
) | ||
``` | ||
Used in xmpp.js tests. | ||
Requires a [preprocessor](https://www.npmjs.com/package/babel-plugin-transform-react-jsx) but if you're already using [Babel](http://babeljs.io/) and/or need to write big chunks of XML it's a good choice. See our [.babelrc](/.babelrc) for a configuration example. | ||
## Reading | ||
### attributes | ||
The `attrs` properties holds xml attributes for an element. | ||
```js | ||
message.attrs.to // user@example.com | ||
``` | ||
### text | ||
Returns the text value of an element | ||
```js | ||
message.getChild('body').text() // '3' | ||
``` | ||
### getChild | ||
Get child element by name. | ||
```js | ||
message.getChild('body').toString() // <body>3</body> | ||
``` | ||
### getChildren | ||
Get children elements by name. | ||
```js | ||
message.getChild('days').getChildren('day') // [...] | ||
``` | ||
### getChildText | ||
Get child element text value. | ||
```js | ||
message.getChildText('body') // '3' | ||
``` | ||
## Editing | ||
### attributes | ||
The `attrs` properties holds xml attributes for an element. | ||
```js | ||
message.attrs.type = 'chat' | ||
Object.assign(message.attrs, {type: 'chat'}) | ||
``` | ||
### text | ||
Set the text value of an element | ||
```js | ||
message.getChild('body').text('Hello world') | ||
``` | ||
### append | ||
Adds text or element nodes to the last position. | ||
Returns the parent. | ||
```js | ||
message.append(xml('foo')) | ||
message.append('bar') | ||
message.append(days.map(day => xml('day', {}, day))) | ||
// <message> | ||
// ... | ||
// <foo/> | ||
// bar | ||
// <day>Monday</day> | ||
// <day>Tuesday</day> | ||
// </message> | ||
``` | ||
### prepend | ||
Adds text or element nodes to the first position. | ||
Returns the parent. | ||
```js | ||
message.append(xml('foo')) | ||
message.append('bar') | ||
message.append(days.map(day => xml('day', {}, day))) | ||
// <message> | ||
// <day>Tuesday</day> | ||
// <day>Monday</day> | ||
// bar | ||
// <foo/> | ||
// ... | ||
// </message> | ||
``` | ||
### remove | ||
Removes a child element. | ||
```js | ||
const body = message.getChild('body') | ||
message.remove(body) | ||
``` | ||
## JSON | ||
You can embed JSON anywhere but it is recommended to use an appropriate semantic. | ||
```js | ||
/** @jsx xml */ | ||
// write | ||
message.append( | ||
<myevent xmlns="xmpp:example.org"> | ||
<json xmlns="urn:xmpp:json:0">{JSON.stringify(days)}</json> | ||
</myevent> | ||
) | ||
// read | ||
JSON.parse( | ||
message | ||
.getChild('myevent', 'xmpp:example.org') | ||
.getChildText('json', 'urn:xmpp:json:0') | ||
) | ||
``` | ||
See [JSON Containers](https://xmpp.org/extensions/xep-0335.html) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9645
10
219
190
Updatedltx@^2.8.0