Skip to content

Commit

Permalink
Various changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Sep 10, 2015
1 parent 4d41636 commit 4c58e44
Show file tree
Hide file tree
Showing 28 changed files with 179 additions and 2,218 deletions.
5 changes: 3 additions & 2 deletions .gitignore
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
node_modules/

ltx.js
10 changes: 10 additions & 0 deletions .npmignore
@@ -0,0 +1,10 @@
test/

.editorconfig
.gitignore
.jshintrc
.npmignore
.travis.yml
benchmark.js
CONTRIBUTING.md
Gruntfile.js
2 changes: 1 addition & 1 deletion Gruntfile.js
Expand Up @@ -13,7 +13,7 @@ module.exports = function(grunt) {
browserify: {
dist: {
files: {
'ltx-browser.js': ['./lib/index-browserify.js']
'ltx.js': ['./lib/index.js']
},
options: {
debug: grunt.cli.options.debug
Expand Down
27 changes: 27 additions & 0 deletions benchmark.js
@@ -0,0 +1,27 @@
'use strict'

var benchmark = require('benchmark')
var ltx = require('./lib/index')
var parsers = require('./lib/parsers')

var XML = [
'<message to="foo@bar" from="bar@to" type="chat" id="foobar">',
'<body>Where there is love there is life.</body>',
'</message>'
].join('')

var suite = new benchmark.Suite('parse');

parsers.forEach(function(Parser) {
suite.add(Parser.name, function() {
ltx.parse(XML, {Parser: Parser})
})
})

suite.on('cycle', function(event) {
console.log(event.target.toString());
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
})
.run({ 'async': true });
1 change: 0 additions & 1 deletion benchmark/.gitignore

This file was deleted.

146 changes: 0 additions & 146 deletions benchmark/benchmark.js

This file was deleted.

16 changes: 0 additions & 16 deletions benchmark/build.js

This file was deleted.

8 changes: 0 additions & 8 deletions benchmark/index.html

This file was deleted.

3 changes: 0 additions & 3 deletions index.js

This file was deleted.

3 changes: 1 addition & 2 deletions lib/dom-element.js → lib/DOMElement.js
@@ -1,7 +1,7 @@
'use strict';

var util = require('util')
, Element = require('./element').Element
, Element = require('./Element')

function DOMElement(name, attrs) {
Element.call(this, name, attrs)
Expand Down Expand Up @@ -118,5 +118,4 @@ DOMElement.createElement = function(name, attrs /*, child1, child2, ...*/) {
return el
}


module.exports = DOMElement
30 changes: 8 additions & 22 deletions lib/element.js → lib/Element.js
@@ -1,5 +1,9 @@
'use strict';

var escape = require('./escape')
var escapeXML = escape.escapeXML
var escapeXMLText = escape.escapeXMLText

/**
* This cheap replica of DOM/Builder puts me to shame :-)
*
Expand Down Expand Up @@ -338,9 +342,9 @@ Element.prototype._addChildren = function(writer) {
if (child.write) {
child.write(writer)
} else if (typeof child === 'string') {
writer(escapeXmlText(child))
writer(escapeXMLText(child))
} else if (child.toString) {
writer(escapeXmlText(child.toString(10)))
writer(escapeXMLText(child.toString(10)))
}
}
}
Expand All @@ -361,7 +365,7 @@ Element.prototype.write = function(writer) {
if (typeof v !== 'string') {
v = v.toString(10)
}
writer(escapeXml(v))
writer(escapeXML(v))
writer('"')
}
}
Expand All @@ -383,22 +387,4 @@ Element.createElement = function(name, attrs /*, child1, child2, ...*/) {
return el
}


function escapeXml(s) {
return s.
replace(/\&/g, '&amp;').
replace(/</g, '&lt;').
replace(/>/g, '&gt;').
replace(/"/g, '&quot;').
replace(/"/g, '&apos;')
}

function escapeXmlText(s) {
return s.
replace(/\&/g, '&amp;').
replace(/</g, '&lt;').
replace(/>/g, '&gt;')
}

exports.Element = Element
exports.escapeXml = escapeXml
module.exports = Element
72 changes: 72 additions & 0 deletions lib/Parser.js
@@ -0,0 +1,72 @@
'use strict';

var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
var Element = require('./Element')
var LtxParser = require('./parsers/ltx')

var Parser = function(options) {
EventEmitter.call(this)

var ParserInterface = this.Parser = (options && options.Parser) || LtxParser
var ElementInterface = this.Element = (options && options.Element) || Element

if (!ParserInterface) {
throw new Error('No SAX parser available')
}
this.parser = new ParserInterface()

var el
, self = this
this.parser.on('startElement', function(name, attrs) {
var child = new ElementInterface(name, attrs)
if (!el) {
el = child
} else {
el = el.cnode(child)
}
})
this.parser.on('endElement', function(name) {
/* jshint -W035 */
if (!el) {
/* Err */
} else if (name === el.name) {
if (el.parent) {
el = el.parent
} else if (!self.tree) {
self.tree = el
el = undefined
}
}
/* jshint +W035 */
})
this.parser.on('text', function(str) {
if (el) {
el.t(str)
}
})
this.parser.on('error', function(e) {
self.error = e
self.emit('error', e)
})
}

inherits(Parser, EventEmitter)

Parser.prototype.write = function(data) {
this.parser.write(data)
}

Parser.prototype.end = function(data) {
this.parser.end(data)

if (!this.error) {
if (this.tree) {
this.emit('tree', this.tree)
} else {
this.emit('error', new Error('Incomplete document'))
}
}
}

module.exports = Parser
17 changes: 17 additions & 0 deletions lib/escape.js
@@ -0,0 +1,17 @@
'use strict';

module.exports.escapeXML = function escapeXML(s) {
return s.
replace(/\&/g, '&amp;').
replace(/</g, '&lt;').
replace(/>/g, '&gt;').
replace(/"/g, '&quot;').
replace(/"/g, '&apos;')
}

module.exports.escapeXMLText = function escapeXMLText(s) {
return s.
replace(/\&/g, '&amp;').
replace(/</g, '&lt;').
replace(/>/g, '&gt;')
}

0 comments on commit 4c58e44

Please sign in to comment.