aboutsummaryrefslogtreecommitdiff
path: root/node_modules/dom-converter/scripts/coffee
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-03 15:35:00 +0200
commitde98e0b232509d5f40c135d540a70e415272ff85 (patch)
treea79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/dom-converter/scripts/coffee
parente0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff)
node_modules
Diffstat (limited to 'node_modules/dom-converter/scripts/coffee')
-rw-r--r--node_modules/dom-converter/scripts/coffee/lib/domConverter.coffee32
-rw-r--r--node_modules/dom-converter/scripts/coffee/lib/domToMarkup.coffee0
-rw-r--r--node_modules/dom-converter/scripts/coffee/lib/objectToSaneObject.coffee80
-rw-r--r--node_modules/dom-converter/scripts/coffee/lib/saneObjectToDom.coffee148
-rw-r--r--node_modules/dom-converter/scripts/coffee/test/_prepare.coffee5
-rw-r--r--node_modules/dom-converter/scripts/coffee/test/domConverter.coffee17
-rw-r--r--node_modules/dom-converter/scripts/coffee/test/objectToSaneObject.coffee73
-rw-r--r--node_modules/dom-converter/scripts/coffee/test/saneObjectToDom.coffee79
8 files changed, 434 insertions, 0 deletions
diff --git a/node_modules/dom-converter/scripts/coffee/lib/domConverter.coffee b/node_modules/dom-converter/scripts/coffee/lib/domConverter.coffee
new file mode 100644
index 000000000..d7dce7303
--- /dev/null
+++ b/node_modules/dom-converter/scripts/coffee/lib/domConverter.coffee
@@ -0,0 +1,32 @@
+objectToSaneObject = require './objectToSaneObject'
+saneObjectToDom = require './saneObjectToDom'
+domToMarkup = require './domToMarkup'
+{object} = require 'utila'
+
+module.exports = self =
+
+ objectToDom: (o) ->
+
+ o = self._object2SaneObject o
+
+ saneObjectToDom.convert o
+
+ object2markup: (o) ->
+
+ dom = self.toDom o
+
+ domToMarkup.convert dom
+
+ domToMarkup: (dom) ->
+
+ domToMarkup.convert dom
+
+ _object2SaneObject: (o) ->
+
+ unless Array.isArray o
+
+ unless object.isBareObject o
+
+ throw Error "toDom() only accepts arrays and bare objects as input"
+
+ objectToSaneObject.sanitize o \ No newline at end of file
diff --git a/node_modules/dom-converter/scripts/coffee/lib/domToMarkup.coffee b/node_modules/dom-converter/scripts/coffee/lib/domToMarkup.coffee
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/node_modules/dom-converter/scripts/coffee/lib/domToMarkup.coffee
diff --git a/node_modules/dom-converter/scripts/coffee/lib/objectToSaneObject.coffee b/node_modules/dom-converter/scripts/coffee/lib/objectToSaneObject.coffee
new file mode 100644
index 000000000..74f114aa1
--- /dev/null
+++ b/node_modules/dom-converter/scripts/coffee/lib/objectToSaneObject.coffee
@@ -0,0 +1,80 @@
+{object} = require 'utila'
+
+module.exports = self =
+
+ sanitize: (val) ->
+
+ self._toChildren val
+
+ _toChildren: (val) ->
+
+ if object.isBareObject val
+
+ return self._objectToChildren val
+
+ else if Array.isArray val
+
+ return self._arrayToChildren val
+
+ else if val is null or typeof val is 'undefined'
+
+ return []
+
+ else if typeof val in ['string', 'number']
+
+ return [String val]
+
+ else
+
+ throw Error "not a valid child node: `#{val}"
+
+ _objectToChildren: (o) ->
+
+ a = []
+
+ for own key, val of o
+
+ cur = {}
+
+ cur[key] = self.sanitize val
+
+ a.push cur
+
+ a
+
+ _arrayToChildren: (a) ->
+
+ ret = []
+
+ for v in a
+
+ ret.push self._toNode v
+
+ ret
+
+ _toNode: (o) ->
+
+ if typeof o in ['string', 'number']
+
+ return String o
+
+ else if object.isBareObject o
+
+ keys = Object.keys(o)
+
+ if keys.length isnt 1
+
+ throw Error "a node must only have one key as tag name"
+
+ key = keys[0]
+
+ obj = {}
+
+ obj[key] = self._toChildren o[key]
+
+ return obj
+
+ else
+
+ throw Error "not a valid node: `#{o}`"
+
diff --git a/node_modules/dom-converter/scripts/coffee/lib/saneObjectToDom.coffee b/node_modules/dom-converter/scripts/coffee/lib/saneObjectToDom.coffee
new file mode 100644
index 000000000..58519b9c2
--- /dev/null
+++ b/node_modules/dom-converter/scripts/coffee/lib/saneObjectToDom.coffee
@@ -0,0 +1,148 @@
+module.exports = self =
+
+ convert: (obj) ->
+
+ self._arrayToChildren obj
+
+ _arrayToChildren: (a, parent = null) ->
+
+ children = []
+
+ prev = null
+
+ for v in a
+
+ if typeof v is 'string'
+
+ node = self._getTextNodeFor v
+
+ else
+
+ node = self._objectToNode v, parent
+
+ node.prev = null
+ node.next = null
+ node.parent = parent
+
+ if prev?
+
+ node.prev = prev
+ prev.next = node
+
+ prev = node
+
+ children.push node
+
+ children
+
+ _objectToNode: (o) ->
+
+ i = 0
+
+ for own k, v of o
+
+ if i > 0
+
+ throw Error "_objectToNode() only accepts an object with one key/value"
+
+ key = k
+ val = v
+
+ i++
+
+ node = {}
+
+ if typeof key isnt 'string'
+
+ throw Error "_objectToNode()'s key must be a string of tag name and classes"
+
+ if typeof val is 'string'
+
+ children = [self._getTextNodeFor(val)]
+
+ else if Array.isArray val
+
+ children = self._arrayToChildren val, node
+
+ else
+
+ inspect o
+ throw Error "_objectToNode()'s key's value must only be a string or an array"
+
+ node.type = 'tag'
+ {name, attribs} = self._parseTag key
+ node.name = name
+ node.attribs = attribs
+ node.children = children
+
+ node
+
+ _getTextNodeFor: (s) ->
+
+ {type: 'text', data: s}
+
+ _nameRx: /^[a-zA-Z\-\_]{1}[a-zA-Z0-9\-\_]*$/
+
+ _parseTag: (k) ->
+
+ # validate
+ if not k.match(/^[a-zA-Z0-9\#\-\_\.\[\]\"\'\=\,\s]+$/) or k.match(/^[0-9]+/)
+
+ throw Error "cannot parse tag `#{k}`"
+
+ attribs = {}
+
+ parts =
+
+ name: ''
+
+ attribs: attribs
+
+ # tag name
+ if m = k.match /^([^\.#]+)/
+
+ name = m[1]
+
+ unless name.match self._nameRx
+
+ throw Error "tag name `#{name}` is not valid"
+
+ parts.name = name
+
+ k = k.substr name.length, k.length
+
+ # tag id
+ if m = k.match /^#([a-zA-Z0-9\-]+)/
+
+ id = m[1]
+
+ unless id.match self._nameRx
+
+ throw Error "tag id `#{id}` is not valid"
+
+ attribs.id = id
+
+ k = k.substr id.length + 1, k.length
+
+ classes = []
+
+ # the class attrib
+ while m = k.match /\.([a-zA-Z0-9\-\_]+)/
+
+ cls = m[1]
+
+ unless cls.match self._nameRx
+
+ throw Error "tag class `#{cls}` is not valid"
+
+ classes.push cls
+
+ k = k.replace '.' + cls, ''
+
+ if classes.length
+
+ attribs.class = classes.join " "
+
+ # TODO: match attributes like [a=b]
+
+ parts \ No newline at end of file
diff --git a/node_modules/dom-converter/scripts/coffee/test/_prepare.coffee b/node_modules/dom-converter/scripts/coffee/test/_prepare.coffee
new file mode 100644
index 000000000..e2b854968
--- /dev/null
+++ b/node_modules/dom-converter/scripts/coffee/test/_prepare.coffee
@@ -0,0 +1,5 @@
+path = require 'path'
+
+pathToLib = path.resolve __dirname, '../../js/lib'
+
+require('little-popo')(pathToLib, no) \ No newline at end of file
diff --git a/node_modules/dom-converter/scripts/coffee/test/domConverter.coffee b/node_modules/dom-converter/scripts/coffee/test/domConverter.coffee
new file mode 100644
index 000000000..62d13ad11
--- /dev/null
+++ b/node_modules/dom-converter/scripts/coffee/test/domConverter.coffee
@@ -0,0 +1,17 @@
+require './_prepare'
+
+domConverter = mod 'domConverter'
+
+describe "input types"
+
+it "should work with objects", ->
+
+ domConverter.objectToDom {}
+
+it "should work with arrays", ->
+
+ domConverter.objectToDom []
+
+it "should not work with other types", ->
+
+ (-> domConverter.objectToDom 'a').should.throw Error \ No newline at end of file
diff --git a/node_modules/dom-converter/scripts/coffee/test/objectToSaneObject.coffee b/node_modules/dom-converter/scripts/coffee/test/objectToSaneObject.coffee
new file mode 100644
index 000000000..4673f2c57
--- /dev/null
+++ b/node_modules/dom-converter/scripts/coffee/test/objectToSaneObject.coffee
@@ -0,0 +1,73 @@
+require './_prepare'
+
+objectToSaneObject = mod 'objectToSaneObject'
+
+describe "sanitize()"
+
+test "case: 'text'", ->
+
+ input = 'text'
+
+ expectation = ['text']
+
+ ret = objectToSaneObject.sanitize input
+
+ ret.should.be.like expectation
+
+test "case: ['text']", ->
+
+ input = ['text']
+
+ expectation = ['text']
+
+ ret = objectToSaneObject.sanitize input
+
+ ret.should.be.like expectation
+
+test "case: {a:b}", ->
+
+ input = a: 'b'
+
+ expectation = [{a: ['b']}]
+
+ ret = objectToSaneObject.sanitize input
+
+ ret.should.be.like expectation
+
+test "case: {a:[b: 'c']}", ->
+
+ input = a: [b: 'c']
+
+ expectation = [{a: [{b: ['c']}]}]
+
+ ret = objectToSaneObject.sanitize input
+
+ ret.should.be.like expectation
+
+test "case: {a:b: 'c'}", ->
+
+ input = a: b: 'c'
+
+ expectation = [{
+ a: [{
+ b: ['c']
+ }]
+ }]
+
+ ret = objectToSaneObject.sanitize input
+
+ ret.should.be.like expectation
+
+test "case: {a:b: ['c', d: 'e']}", ->
+
+ input = a: b: ['c', d: 'e']
+
+ expectation = [{
+ a: [{
+ b: ['c', {d: ['e']}]
+ }]
+ }]
+
+ ret = objectToSaneObject.sanitize input
+
+ ret.should.be.like expectation \ No newline at end of file
diff --git a/node_modules/dom-converter/scripts/coffee/test/saneObjectToDom.coffee b/node_modules/dom-converter/scripts/coffee/test/saneObjectToDom.coffee
new file mode 100644
index 000000000..661db8fcf
--- /dev/null
+++ b/node_modules/dom-converter/scripts/coffee/test/saneObjectToDom.coffee
@@ -0,0 +1,79 @@
+require './_prepare'
+
+s2d = mod 'saneObjectToDom'
+
+describe "_arrayToChildren()"
+
+it "should work", ->
+
+ ret = s2d._arrayToChildren [
+
+ {
+ a: 'text'
+ }
+ {
+ 'b.someClass': ['b1', 'b2']
+ }
+ {
+ c: [
+
+ {d: 'text'}
+ {e: []}
+
+ ]
+ }
+
+ ]
+
+ ret.should.be.an 'array'
+ ret.should.have.length.of 3
+
+ for node in ret
+
+ node.should.be.an 'object'
+ node.should.have
+ .keys ['type', 'name', 'attribs', 'children', 'next', 'prev', 'parent']
+
+ a = ret[0]
+
+ a.children.should.be.an 'array'
+ a.children.should.have.length.of 1
+
+ aChild = a.children[0]
+ aChild.should.be.an 'object'
+ aChild.should.be.like {type: 'text', data: 'text'}
+
+ expect(a.prev).to.equal null
+ expect(a.parent).to.equal null
+
+ b = ret[1]
+
+ a.next.should.equal b
+ b.prev.should.equal a
+ b.attribs.should.be.like
+
+ class: 'someClass'
+
+ bChildren = b.children
+
+ bChildren[0].should.be.like {type: 'text', data: 'b1'}
+ bChildren[1].should.be.like {type: 'text', data: 'b2'}
+
+ ret.should.have.deep.property '[2].children[1].name', 'e'
+
+
+describe "_parseTag"
+
+it "should work", ->
+
+ s2d.
+ _parseTag('tagName#id.c1.c2[a=b, d="1 2 3"]')
+ .should.be.like
+
+ name: 'tagName'
+
+ attribs:
+
+ id: 'id'
+
+ class: 'c1 c2'