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
24
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.1.3 to 0.2.0

lib/Element.js

33

index.js
'use strict'
var ltx = require('ltx')
var tag = require('./lib/tag')
const x = require('./lib/x')
const Element = require('./lib/Element')
const Parser = require('./lib/Parser')
const {escapeXML, unescapeXML, escapeXMLText, unescapeXMLText} = require('ltx/lib/escape')
exports = module.exports = function xml () {
return tag.apply(null, arguments)
function xml(...args) {
return x(...args)
}
Object.assign(exports, ltx)
exports = module.exports = xml
exports.IQ = require('./lib/IQ')
exports.Message = require('./lib/Message')
exports.Presence = require('./lib/Presence')
exports.Stanza = require('./lib/Stanza')
exports.createStanza = require('./lib/createStanza')
exports.parse = require('./lib/parse')
exports.Parser = require('./lib/Parser')
exports.tag = require('./lib/tag')
exports.ltx = ltx
Object.assign(exports, {
x,
Element,
Parser,
escapeXML,
unescapeXML,
escapeXMLText,
unescapeXMLText,
})
'use strict'
var Parser = require('./Parser')
var ltxParse = require('ltx').parse
const Parser = require('./Parser')
module.exports = function parse (data) {
return ltxParse(data, Parser)
module.exports = function parse(data) {
const p = new Parser()
let result = null
let error = null
p.on('end', tree => {
result = tree
})
p.on('error', err => {
error = err
})
p.write(data)
p.end()
if (error) {
throw error
} else {
return result
}
}
'use strict'
var inherits = require('inherits')
var createStanza = require('./createStanza')
var LtxParser = require('ltx').Parser
const LtxParser = require('ltx/lib/parsers/ltx')
const Element = require('./Element')
const EventEmitter = require('events')
function Parser (options) {
LtxParser.call(this, options)
class XMLError extends Error {
constructor(...args) {
super(...args)
this.name = 'XMLError'
}
}
inherits(Parser, LtxParser)
Parser.prototype.DefaultElement = createStanza
class Parser extends EventEmitter {
constructor() {
super()
const parser = new LtxParser()
const stack = []
let cursor
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('text', str => {
this.onText(str, cursor)
})
this.parser = parser
}
onStartElement(element, cursor) {
if (!cursor) {
this.emit('start', element)
}
}
onEndElement(element, length) {
if (length === 2) {
this.emit('element', element)
} else if (length === 1) {
this.emit('end', element)
}
}
onText(str, element) {
if (!element) {
this.emit('error', new XMLError(`${str} must be a child.`))
return
}
element.t(str)
}
write(data) {
this.parser.write(data)
}
end(data) {
if (data) {
this.parser.write(data)
}
}
}
Parser.XMLError = XMLError
module.exports = Parser

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

"bugs": "http://github.com/node-xmpp/node-xmpp/issues",
"version": "0.1.3",
"version": "0.2.0",
"license": "ISC",

@@ -18,5 +18,4 @@ "keywords": [

"dependencies": {
"inherits": "^2.0.3",
"ltx": "^2.6.2"
}
}

@@ -5,1 +5,22 @@ XML

XMPP XML for JavaScript.
## Install
```
npm install @xmpp/xml
```
## Usage
```javascript
const xml = require('@xmpp/xml')
const body = ' hello '
const stanza = xml`
<message>
<body>${body}</body>
</message>
`
console.log(stanza.toString())
// <message><body> hello </body></message>
```
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