Comparing version 0.1.3 to 0.2.0
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> | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
26
5261
9
173
2
- Removedinherits@^2.0.3