diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..d793d33 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,19 @@ +Copyright (c) 2013 [Soroosh Izadian](http://srosh.com/) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4c272cb --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +FlatDOM +=== +## What +This is a module for manipulating HTML/XML DOM using server-side JavaScript (Node.js). + +## Motivation +A simple representation of DOM objects (as a flat array) makes it much easier to view and manipulate data. The motivation behind this project is to provide an easy and fast way to find elements within DOM and make it easy to import/export different representations of the DOM data. diff --git a/basic.js b/basic.js new file mode 100644 index 0000000..24efd64 --- /dev/null +++ b/basic.js @@ -0,0 +1,339 @@ +var addChild = function (arr,child,parent,toend,cutoff) +{ + arr = arr === undefined ? [] : arr; + toend = toend === undefined ? false : toend; + parent = parent === undefined ? arr.length - 1 : parent ; + var res = arr.slice(0,parent+1); + var rest = arr.slice(parent+1); + if (toend){ + while (rest.length>0 && rest[0].parent>=parent) { + var target = rest.shift(); + res.push(target); + } + } + var offset = res.length; + if (cutoff) cutoff(offset); + for (var c in child) { + var target=[]; + for (var key in child[c]) target[key] = child[c][key]; + if (target.parent==-1) target.parent = parent; + else target.parent += offset; + res.push(target); + } + for (var c in rest) { + var target=[]; + for (var key in rest[c]) target[key] = child[c][key]; + target.parent += target.parent > parent ? child.length : 0; + res.push(target); + } + return res; +} + +var emmet = function (stremmet,parent) +{ + var res = []; + var openbranch = 0; + var multi = ''; + var temp = ''; + var brackets = ''; + var targetText = 0; // tag + parent = parent === undefined ? -1 : parent; + var current = []; //['',{},parent]; + current.name = ''; + current.attrs = {}; + current.parent = parent; + var flush = function(pos) + { + var resolve$ = function (from,nstr) { + var newTag = []; //[from[0],{},from[2]]; + newTag.attrs = {}; + for (var key in from) { if(key!='attrs') newTag[key] = from[key]; } + for (var key in from.attrs) { + var val = from.attrs[key]; + var dollars = val.match(/[\$]+/); + if (dollars) for (var j = 0; j < dollars.length; j++) { + var pad = ''; + for (var k=0; k< dollars[j].length - nstr.length ; k++) {pad += '0';} + val = val.replace(dollars[j],pad+nstr); + } + newTag.attrs[key] = val; + } + return newTag; + } + var children = null; + if (current.children) { + children = current.children; + delete current.children; + }; + if (multi.length>0) { + var m = parseInt(multi); + if(!isNaN(m)) { + for (var i = 0; i < m; i++) { + var nstr = (i+1).toString(); + if (current.name.length>0) res.push(resolve$(current,nstr)); + else if (children) { + var childnodes = []; + for (var j = 0; j < children.length; j++) { + childnodes.push(resolve$(children[j],nstr)); + }; + res = addChild(res,childnodes,parent,true) + } + if (stremmet.length > pos && stremmet[pos]=='>') res = addChild(res,emmet(stremmet.substring(pos+1))); + } + } + } else if (current.name.length>0){ + res.push(current); + } else if (children) res = addChild(res,children,parent,true); + targetText = 0; + multi = ''; + brackets = ''; + current = [] //'',{},parent]; + current.name = ''; + current.attrs = {}; + current.parent = parent; + } + var addChar = function(ch) // can move () here + { + switch (targetText) { + case 0: //tag + current.name += ch; + break; + case 1: //class + if (!current.attrs['class']) current.attrs['class']=''; + current.attrs['class'] +=ch; + break; + case 2: //id + if (!current.attrs['id']) current.attrs['id']=''; + current.attrs['id'] +=ch; + break; + case 3: //args + brackets += ch; + break; + case 4: //multi + multi += ch; + break; + default: + } + } + var readbrackets = function () + { + var key = ''; + var val = ''; + var openquote = false; + var slashed = false; + var valactive = false; + for (var i = 0; i < brackets.length; i++) { + var b = brackets[i]; + if (!openquote && b==' ' && key.length > 0) { + current.attrs[key] = val; + key=''; + val=''; + valactive = false; + } else if (b=='"' && !slashed) { + openquote = !openquote; + } else if (valactive) { + val += b; + } else if (!openquote && b=='=') { + valactive = true; + } else { + key += b; + } + if (b=='\\') slashed = !slashed; + else slashed = false; + } + if (key.length > 0) { + current.attrs[key] = val; + key=''; + val=''; + valactive = false; + } + } + + // parentesies + for (var i=0; i1) { + temp += '('; + } + } else if (cc==')') { + openbranch --; + if (openbranch == 0) { + //res = addChild(res,emmet(temp),parent,true); // res.concat(emmet(temp,parent)); + current.children = emmet(temp); + temp = ''; //clear temp + } else if (openbranch == -1) { + //bad string + } else if (openbranch>0) { + temp += ')'; + } + } else if (openbranch > 0) temp += cc ; + else { + + if (cc==']') { + targetText = 0; + readbrackets(); + brackets = ''; + } else if (targetText == 3) { + addChar(cc); + } else if (cc=='[') { + targetText = 3; + } else if (cc == '+') { + flush(i); + } else if (cc == '>') { + if (multi.length>0) { + flush(i); + return res; + } + flush(i); + parent=res.length-1; + current.parent=parent; + } else if (cc == '.') { + targetText = 1; + if (current.attrs['class']) addChar(' '); + } else if (cc == '#') { + targetText = 2; + } else if (cc == '*') { + targetText = 4; + } else { + addChar(cc); + } + } + } + flush(stremmet.length); + return res; +} + +var render = function (arr) +{ + var res = ''; + var tail = []; + var special={'!--':'-->'}; + var singletons=['!doctype','area','base','br','col','command','embed','hr','img','input','link','meta','param','source']; + var urlAttrs=['src','href']; + var quotedAttr = function (attr) { + var val = tag.sttrs[attr]; + if (val.indexOf('"')) { + if (urlAttrs.indexOf(attr)>-1) return val.replace(/\"/g,'%22'); + else return val.replace(/\"/g,'"'); + } else return val; + } + var handleTag = function (tag) { + res += '<' + tag.name ; + for (var arg in tag.attrs){ + if (typeof(tag.attrs[arg])=='boolean' && tag.attrs[arg]) res += ' ' + arg; + else res += ' ' + arg + '="' + tag.attrs[arg] + '"'; + } + if (singletons.indexOf(tag.name)>-1) { + res += ' />'; + if (tag.textAfter) { + res += tag.textAfter; + } + } else if (special[tag.name]) { + var after =''; + if (tag.text) res += tag.text; + if (tag.textAfter) after = tag.textAfter; + res += special[tag.name] + after; + } else { + res += '>'; + var after =''; + if (tag.text) { + res += tag.text; + if (tag.textAfter) after = tag.textAfter; + } + tail.unshift([''+after,tag.parent]); + } + } + // var lparent = arr.name.parent; + for( var i=0;i 0 && lparent >= cparent){} + while (tail.length > 0 && tail[0][1] >= cparent) res += tail.shift().shift(); + handleTag(line); + // lparent = cparent; + } + while (tail.length > 0) res += tail.shift().shift(); + return res; +} + +var domobj = function (flatdom) +{ + var res = {'flat':flatdom,'classes':{},'ids':{},'tags':{}}; + for (var i = 0; i < flatdom.length; i++) { + var line=flatdom[i]; + var tag =line.name; + var id = line.attrs['id']===undefined ? null : line.attrs['id']; + var classes = line.attrs['class']===undefined ? [] : line.attrs['class'].split(' '); + if (tag){ + if (res.tags[tag]) res.tags[tag].push(i); + else res.tags[tag] = [i]; + if (id) res.ids[id]=i; + while (classes.length>0){ + className = classes.pop(); + if (res.classes[className]) res.classes[className].push(i); + else res.classes[className] = [i]; + } + } + } + return res; +} + +var select = require('./dom.select.js').sel; + +var handle = function(dom,mobj){ + var flat = dom.flat; + var sel = mobj.sel ? mobj.sel : (mobj.target ? select(dom,mobj.target) : []); + if(!mobj || sel.length == 0) return dom; + switch (mobj.type) { + case 'add': + var nodes = null; + if(mobj.emmet) nodes = emmet(mobj.emmet); + else if(mobj.flat) nodes = mobj.flat ; + if(nodes) { + for (var i = 0; i < sel.length; i++) { + var offset = i*nodes.length; + flat = addChild(flat,nodes,sel[i]+offset,true); + } + return domobj(flat); + } + break; + case 'set': + if(mobj.attr) { + for (var i = 0; i < sel.length; i++) { + for (var key in mobj.attr) { + flat[sel[i]][1][key] = mobj.attr[key]; + } + } + } + if(mobj.text) { + for (var i = 0; i < sel.length; i++) { + flat[sel[i]].text = mobj.text; + } + } + break; + } + if(mobj.rebuild) return domobj(flat); + else { + dom.flat = flat; + return dom; + } +} + +var domObject = function () { + var len=arguments.length; +} + +domObject.prototype.type = "Basic.Dom"; + + +//exports.sampleemmet='html>(head>link[rel]+(b>link.boo$[href]*1)+script)+(body>(div.out.gooz[a=1 b=" l "]>(div#in>img[src="boo.jpg"]+qq.boo#soo.too[ad="\\"\\"\\b"])+(div.another$$*3+(div.another$$$$*5>img#t$$*2+br*3)+div.another+(p.o+p.b)))+p#text)'; +//exports.samplehtml=[['html',{},-1],['head',{},0],['body',{},0],['div',{class:'.out'},2],['div',{id:'#in'},3],['$plain',{text:'yoyo'},2]]; +exports.hand=handle; +exports.zen=emmet; +exports.select=select; +exports.dom=domobj; +exports.rndr=render; +exports.addchild=addChild; \ No newline at end of file diff --git a/dom.arr.js b/dom.arr.js new file mode 100644 index 0000000..6b157c7 --- /dev/null +++ b/dom.arr.js @@ -0,0 +1,337 @@ +// domarr.js + +var getIndex = function () { + if (this.sDOM && this.sDOM.isDOM) return this.sDOM.indexOf(this); + else return -1; +} +var getParent = function () { + if (this.sParent) return this.sParent.index; + else if (this.tempParentIndex !== undefined) return this.tempParentIndex; + else return -1; +} +var setParent = function (index) { + if (this.sDOM && index < this.sDOM.length) { //length? + this.sParent = this.sDOM[index]; + } else if (!this.sDOM) { + this.tempParentIndex = index; + } +} +var leaveDOM = function () { + this.sDOM = null; +} +var joinDOM = function (dom) { + if (dom.isDOM) { + this.sDOM = dom; + if (this.tempParentIndex !== undefined) { + this.parent = this.tempParentIndex; + delete this.tempParentIndex; + } + // building tags index + if (!dom.tags[this.name]) dom.tags[this.name] = [this]; + else dom.tags[this.name].push(this); + + // building id index + if (this.attrs['id']) { dom.ids[this.attrs['id']] = this; } + + // building class index + if (this.attrs['class']) { + var classNames = this.attrs['class'].split(' '); + while (classNames.length>0) { + var className = classNames.shift(); + if (className) { + if (!dom.classes[className]) dom.classes[className] = [this]; + else dom.classes[className].push(this); + } + } + } + + for (key in this.attrs) { + if (key!='class' && key!='id') { + if (!dom.attributes[key]) dom.attributes[key] = [this]; + else dom.attributes[key].push(this); + } + } + } else throw {no:10,desc:'you can only add tags to a DOM Array'}; +} +var duplicate = function (acb) { + var dup = maketag([],this.name,{},this.parent); + if (this.text) dup.text = this.text; + if (this.textAfter) dup.textAfter = this.textAfter; + if (this.closed !== undefined) dup.closed = this.closed; + if (acb) acb(this.attrs,dup.attrs); + else { + for (var key in this.attrs) { + dup.attrs[key] = this.attrs[key]; + } + } + return dup; +} + +var maketag = function (tag,name,attrs,parent,text,textAfter) { + if (!(tag instanceof Array)) throw {no:1,desc:'tag must be an instanceof Array'}; + tag.isTag = true; + tag.sDOM = tag.sDOM || null; + Object.defineProperty(tag, 'index', {get: getIndex,set: undefined}); + Object.defineProperty(tag, 'parent', {get: getParent,set: setParent}); + tag.joinDOM = joinDOM; + tag.leaveDOM = leaveDOM; + tag.duplicate = duplicate; + if (name !== undefined && name !== null) tag.name = name; + if (attrs !== undefined && attrs !== null) tag.attrs = attrs; + if (parent !== undefined && parent !== null) tag.parent = parent; + if (text !== undefined && text !== null) tag.text = text; + if (textAfter !== undefined && textAfter !== null) tag.textAfter = textAfter; + return tag; +} + + +var getLastTag = function () { + return (!this.isEmpty ? this[this.length-1] : null); +} +var getFirstTag = function () { + return (!this.isEmpty ? this[0] : null); +} +var isEmpty = function () { + return this.length == 0; +} +var availableParentIndices = function () { + var available = []; + var tag = this.last + while (tag) { + if (!tag.closed) available.unshift(tag.index); + tag = (tag.sParent ? tag.sParent : null); + } + available.unshift(-1); + return available; // ascending list of parent indices +} + +var concat = function (dom) { + if (dom.isDOM) { + var lastIndex = this.openTags.pop(); + var closedTags = []; + while (dom.length > 0) { + var tag = dom.shift(); + if (lastIndex > -1 && tag.parent == -1) tag.parent = lastIndex; + if (tag.closed) { + closedTags.push(tag); + tag.closed = false; + } + //console.log(tag.parent,lastIndex,tag.tempParentIndex); + this.push(tag); + } + while (closedTags.length > 0) { + var tag = closedTags.pop(); + tag.closed = true; + } + } +} +var push = function (tag) { + if (tag.isTag) { + tag.joinDOM(this); + if (tag.sParent && tag.sParent.isDOM) tag.sParent.push(tag); + else { + if (!this.isEmpty) { + var openTags = this.openTags; + var parentIndexInOpenTags = openTags.indexOf(tag.parent); + var parent = tag.parent; + if (parentIndexInOpenTags>-1) { + if (parentIndexInOpenTags= indices[i] && (level!=0); j++) { + if (level==-1 && node.indexOf(dom[j])==-1) node.push(dom[j]); + else if (level > 0 && parentLevels.length > 0) { + for (var k = parentLevels.length -1; k >= 0 ; k--) { + if (dom[j].parent == parentLevels[k][0]) { + if (node.indexOf(dom[j])==-1) { + node.push(dom[j]); + if (parentLevels[k][1]0) { + var expnddItem = expanded.shift(); + var level = 0; + var lparent = false; + while (expnddItem.length >0) { + var cur = expnddItem.shift(); + if (lparent===false) { + lparent = []; + res += '@'; + } else if (lparent.length>0 && lparent[0] > cur.parent) { + while (lparent.length>0 && lparent[0] > cur.parent) { + lparent.shift(); + level--; + } + res += pad(level,' |')+'\n' + pad(level-1,' |')+' @'; + } else if (lparent.length>0 && lparent[0] == cur.parent) { + res += pad(level,' |')+'\n' + pad(level-1,' |')+' @'; + } else if (lparent.length==0 || lparent[0] < cur.parent) { + level++; + res += pad(level,' |')+'\n' + pad(level-1,' |')+' @'; + lparent.unshift(cur.parent); + } + + res += '_' + itemno +'_/'+cur.name +' '+cur.index+':'+cur.parent+' '+ attrs(cur.attrs)+'\n'; + itemno++ + } + while (lparent.length>0) { + lparent.shift(); + //res += '\n'; + } + } + console.log(res); + //return res; +} + +exports.makeDOM = makedom; +exports.makeTag = maketag; \ No newline at end of file diff --git a/dom.select.js b/dom.select.js new file mode 100644 index 0000000..f16c6dc --- /dev/null +++ b/dom.select.js @@ -0,0 +1,128 @@ +var select = function (dom,selectors,selection) +{ + var empty = []; + var intersect = function (a1, a2) //a1,a2 are sorted + { + var i1=0, i2=0, res = []; + while( i1 < a1.length && i2 < a2.length ) + { + if (a1[i1] < a2[i2] ){ i1++; } + else if (a1[i1] > a2[i2] ){ i2++; } + else { + res.push(a1[i1]); i1++; i2++; + } + } + return res; + } + var decompose = function (str) + { + var temp = ['','','','']; + var target = 0; + for (var i = 0; i < str.length; i++) { + if (str[i]=='#') target = 2; + else if (str[i]==':') target = 3; + else if (str[i]=='.') {target = 1; temp[1] += temp[1].length>0 ? ' ' : '';} + else temp[target] += str[i]; + } + return {'tag':temp[0],'classes':temp[1].split(' '),'id':temp[2],'pseudo':temp[3]}; + } + var expand = function (sel,directChildren) + { + var res = []; + directChildren = directChildren || false; + var acceptable = function (par,cur){ + return (directChildren ? par==cur : par>=cur); + } + for (var i = 0; i < sel.length; i++) { + if (res.indexOf(sel[i])==-1) { + for (var j = sel[i]+1; j < dom.length && dom[j].parent>=sel[i]; j++) { + if(res.indexOf(j) ==-1 && acceptable(dom[j].parent, sel[i])) res.push(j); + } + } + } + return res; + } + var pseudo = function (pse) { + var tests = [/^eq\(([0-9]+)\)$/,'first-child','last-child']; + if (pse) { + for (var i = 0; i < tests.length; i++) { + var answer = (typeof(tests[i]) == 'string') ? pse == tests[i] : pse.match(tests[i]); + if (answer) { + return {'test':i,'match':answer}; + } + } + } + return {'test':-1}; + } + + if (selection === undefined) { + selection =[]; + for (var i = 0; i < dom.length; i++) { + selection.push(i); + } + } else { + if (selectors && selectors[0] == '>') { + selection = expand(selection,true); + selectors.shift(); + } else if (selectors && selectors[0] == '*') { + if (selection.length < dom.length) selection = expand(selection); + selectors.shift(); + } else selection = expand(selection); + } + if (selectors === undefined || selectors.length == 0) return selection; //not array? + if ('string' == typeof(selectors)) { + if(selectors.indexOf(',')>-1){ + //merge(select(part[0]),select(rest)) + }else{ + selectors = selectors.replace('>',' > '); + selectors = selectors.replace('*',' * '); + selectors = selectors.split(/[\s]+/); + if (selectors.length > 0 && selectors[0].length==0) selectors.shift(); + if (selectors.length > 0 && selectors[selectors.length-1].length==0) selectors.pop(); + } + } + //selectors=selectors.split(' '); + var selector = decompose(selectors.shift()); + if (selector.tag) { + if (!dom.tags[selector.tag]) return empty; + else selection = intersect(selection,dom.tags.indices(selector.tag)); + } console.log(selection); + if (selector.id) { + if (!dom.ids[selector.id]) return empty; + else selection = intersect(selection,dom.ids.indices(selector.id)); + } console.log(selection,dom.ids.indices(selector.id),selector.id); + while (selector.classes.length>0 && selector.classes[0]) { + var className = selector.classes.shift(); + if (!dom.classes[className]) return empty; + else selection = intersect(selection,dom.classes.indices(className)); + } + if (selector.pseudo) { + var pse = selector.pseudo; + if (parseInt(pse).toString()==pse) { //index + var index = parseInt(pse); + if (index < selection.length) { selection = [selection[index]] }; + } else { + var test = pseudo(pse); + switch (test.test) { + case -1: break; + case 0: selection = [selection[parseInt(test.match[1])]]; + break; + case 1: + case 2: + { + var tempSelection = []; + for (var s in selection) { + if ((test.test==1 && !dom[selection[s]].prev) || (test.test==2 && !dom[selection[s]].next)) tempSelection.push(selection[s]); + } + selection = tempSelection; + } + break; + } + } + } + if (selectors.length>0) return select(dom,selectors,selection); + else return selection; +} + + +module.exports = select; \ No newline at end of file diff --git a/emmet.js b/emmet.js new file mode 100644 index 0000000..2753021 --- /dev/null +++ b/emmet.js @@ -0,0 +1,201 @@ +var domarr = require('./dom.arr.js'); +var emmet = {}; +emmet.read = function (source) //add target +{ + var res = domarr.makeDOM([]); + var openbranch = 0; + var multi = ''; + var temp = ''; + var brackets = ''; + var targetText = 0; // tag + var parent = -1; + var current = domarr.makeTag([],'',{},parent); + + var flush = function(pos) + { + var resolve$ = function (from,nstr) { + return from.duplicate(function(af,at){ + for (key in af) { + var val = af[key]; + var dollars = val.match(/[\$]+/); + if (dollars) for (var j = 0; j < dollars.length; j++) { + var pad = ''; + for (var k=0; k< dollars[j].length - nstr.length ; k++) {pad += '0';} + val = val.replace(dollars[j],pad+nstr); + } + at[key] = val; + } + }); + } + var children = null; + if (current.children) { + children = current.children; + delete current.children; + } + if (multi.length>0) { + var m = parseInt(multi); + if(!isNaN(m)) { + for (var i = 0; i < m; i++) { + var nstr = (i+1).toString(); + if (current.name.length>0) { + res.push(resolve$(current,nstr)); + } else if (children) { + var childnodes = domarr.makeDOM([]); + for (var j = 0; j < children.length; j++) { + var newTag = resolve$(children[j],nstr); + childnodes.push(newTag); + } + res.concat(childnodes); //.each(function(tag){tag.closed=true;}) + } + if (source.length > pos && source[pos]=='>') { + res.concat(read(source.substring(pos+1))); + } + var lastParentTag=res.last; + while (lastParentTag && lastParentTag.index > parent) { + lastParentTag.closed=true; + lastParentTag = lastParentTag.sParent; + } + } + } + } else if (current.name.length>0){ + res.push(current); + } else if (children) { + res.concat(children); + if (current.closed) { + var lastParentTag=res.last; + while (lastParentTag && lastParentTag.index > parent) { + lastParentTag.closed=true; + lastParentTag = lastParentTag.sParent; + } + } + } + targetText = 0; + multi = ''; + brackets = ''; + parent=res.openTags.pop(); + current = domarr.makeTag([],'',{},parent); //console.log (current.parent,res.openTags); + } + var addChar = function(ch) // can move () here + { + switch (targetText) { + case 0: //tag + current.name += ch; + break; + case 1: //class + if (!current.attrs['class']) current.attrs['class']=''; + current.attrs['class'] +=ch; + break; + case 2: //id + if (!current.attrs['id']) current.attrs['id']=''; + current.attrs['id'] +=ch; + break; + case 3: //args + brackets += ch; + break; + case 4: //multi + multi += ch; + break; + default: + } + } + var readbrackets = function () + { + var key = ''; + var val = true; + var openquote = false; + var slashed = false; + var valactive = false; + var keyactive = true; + for (var i = 0; i < brackets.length; i++) { + var b = brackets[i]; + if (!openquote && b==' ' && key.length > 0 && keyactive) { + current.attrs[key] = true; + keyactive = false; + } else if (b=='"' && !slashed) { + openquote = !openquote; + valactive = openquote; + } else if (valactive) { + current.attrs[key] += b; + } else if (!openquote && b=='=') { + valactive = true; + keyactive = false; + current.attrs[key] = ''; + } else if (b!=' ') { + if (!keyactive) { + key = ''; + keyactive = true; valactive = false; + } + key += b; + } + if (b=='\\') slashed = !slashed; + else slashed = false; + } + if (key.length > 0 && keyactive) { + current.attrs[key] = true; + } + } + + // parentesies + for (var i=0; i1) { + temp += '('; + } + } else if (currentChar==')') { + openbranch --; + if (openbranch == 0) { + current.children = read(temp); + temp = ''; //clear temp + } else if (openbranch == -1) { + //bad string + } else if (openbranch>0) { + temp += ')'; + } + } else if (openbranch > 0) temp += currentChar ; + else { + if (currentChar==']') { + targetText = 0; + readbrackets(); + brackets = ''; + } else if (targetText == 3) { + addChar(currentChar); + } else if (currentChar=='[') { + targetText = 3; + } else if (currentChar == '+') { + current.closed = true; + flush(i); + } else if (currentChar == '>') { + if (multi.length>0) { + flush(i); + return res; + } else flush(i); + } else if (currentChar == '.') { + targetText = 1; + if (current.attrs['class']) addChar(' '); + } else if (currentChar == '#') { + targetText = 2; + } else if (currentChar == '*') { + targetText = 4; + } else { + addChar(currentChar); + } + } + } + flush(source.length); + return res; +} +emmet.render = function (arr) { return 'emmet render is not implemented yet'; } +emmet.cssLink = function (uri) { + return 'link[rel="stylesheet" type="text/css" href="'+uri+'"]'; +} +emmet.jsLink = function (uri) { + return 'script[src="'+uri+'"]'; +} +emmet.join = function (arr) { + return arr.join('+'); +} + +module.exports = emmet; \ No newline at end of file diff --git a/html.js b/html.js new file mode 100644 index 0000000..404c291 --- /dev/null +++ b/html.js @@ -0,0 +1,308 @@ +var html = {}; +html.render = function (arr) { + var res = ''; + var tail = []; + var special={'!--':'-->'}; + var singletons=['area','base','br','col','command','embed','hr','img','input','link','meta','param','source']; + var urlAttrs=['src','href']; + var quotedAttr = function (attr) { + var val = tag.sttrs[attr]; + if (val.indexOf('"')) { + if (urlAttrs.indexOf(attr)>-1) return val.replace(/\"/g,'%22'); + else return val.replace(/\"/g,'"'); + } else return val; + } + var handleTag = function (tag) { + res += '<' + tag.name ; + for (var arg in tag.attrs){ + if (typeof(tag.attrs[arg])=='boolean' && tag.attrs[arg]) res += ' ' + arg; + else res += ' ' + arg + '="' + tag.attrs[arg] + '"'; + } + if (singletons.indexOf(tag.name)>-1) { + res += ' />'; + if (tag.textAfter) { + res += tag.textAfter; + } + } else if (special[tag.name]) { + var after =''; + if (tag.text) res += tag.text; + if (tag.textAfter) after = tag.textAfter; + res += special[tag.name] + after; + } else { + res += '>'; + var after =''; + if (tag.text) { + res += tag.text; + if (tag.textAfter) after = tag.textAfter; + } + tail.unshift([''+after,tag.parent]); + } + } + // var lparent = arr.name.parent; + for( var i in arr){ + var line = arr[i]; + var cparent = line.parent; + // if(tail.length > 0 && lparent >= cparent){} + while (tail.length > 0 && tail[0][1] >= cparent) res += tail.shift().shift(); + handleTag(line); + // lparent = cparent; + } + while (tail.length > 0) res += tail.shift().shift(); + return res; +} +html.read = function (chunk,dom,options) { + var intersect = function (a1, a2) { //a1,a2 are sorted + var i1=0, i2=0, res = []; + while( i1 < a1.length && i2 < a2.length ) { + if (a1[i1] < a2[i2] ){ i1++; } + else if (a1[i1] > a2[i2] ){ i2++; } + else { + res.push(a1[i1]); i1++; i2++; + } + } + return res; + } + // consts + var commentOpen = '!--', commentClose = '--'; + var ignoreInside=['script','style']; + var singletons=['!doctype','area','base','br','col','command','embed','hr','img','input','link','meta','param','source']; + var openChar='<', closeChar='>',qChar='\'',dqChar='"',sChar='\\',xChar = '!' , eqChar = '=' , closeTagChar = '/'; + var whiteSpace=/\s/; + var forceAttrsToLower = ['id','class','src','href']; + // end consts + // options.working = true; + var getLastParent = function() { + if (options.lastParent > -1) return dom[options.lastParent]; + else return null; + } + getLastParent = getLastParent.bind(options); + var getLastChild = function() { + if (options.lastChild > -1) return dom[options.lastChild]; + else return null; + } + getLastChild = getLastChild.bind(options); + var revertFromIgnore = function() { //assume ignore=true; + if (options.comment) return false; + var lastTag = dom.last; + if (lastTag) { + if (options.lastChild != options.lastParent) { + if (lastTag.textAfter) lastTag.textAfter += options.ignoredText; + else lastTag.textAfter = options.ignoredText; + } else { + if (lastTag.text) lastTag.text += options.ignoredText; + else lastTag.text = options.ignoredText; + } + + options.ignoredText = ''; + options.mode = 0; + options.onTag = false; + options.tag = lastTag; + return true; + } // no last child + return false; + } + revertFromIgnore = revertFromIgnore.bind(options); + var isEndOfComment = function() { + return (options.tag.text) && (options.tag.text.length >= commentClose.length ? (options.tag.text.substr(-commentClose.length) == commentClose) : false); + } + isEndOfComment = isEndOfComment.bind(options); + + + + for (var i = 0; i < chunk.length; i++) { + var currentChar=chunk[i]; + var takeChar=true; + if (!options.comment && options.ignore && options.onTag) { + options.ignoredText += currentChar; + }; + + switch (currentChar) { + case openChar: { + if (!options.onTag && !options.comment) { // && !(options.openQuote || options.openDoubleQuote)) { + options.tag = domarr.makeTag([],'',{},options.lastParent); + options.onTag = true; + options.mode = 1; + takeChar = false; + } + } break; + case closeChar: { + if (options.onTag) { // && !(options.openQuote || options.openDoubleQuote)) { + if (options.comment) { + if (isEndOfComment()) { + //close comment and push tag + dom.push(options.tag); + options.lastChild = options.tag.index; + options.lastParent = options.tag.parent; + options.tag.closed = true; + options.tag.text = options.tag.text.substr(0,options.tag.text.length - commentClose.length); + options.onTag = false; + options.closerTag = false; + takeChar = false; + options.mode = 0; + options.lastAttr =''; + options.comment = false; + options.openQuote = false; options.openDoubleQuote = false; + } + } else { + if (options.ignore) { + // console.log(options.tag.name,dom.last.name,dom.last.sParent.name); + if (options.closerTag && ignoreInside.indexOf(options.tag.name) > -1 && (dom.last.name == options.tag.name || dom.last.sParent.name == options.tag.name)) { + //close ignore tag and not push + if (!dom.last.closed && dom.last.name == options.tag.name) { + options.tag = dom.last; + } else if (dom.last.closed && dom.last.sParent.name == options.tag.name) { + options.tag = dom.last.sParent; + } + options.tag.closed = true; + options.lastParent = options.tag.parent; + options.lastChild = options.tag.index; + options.ignore = false; + options.ignoredText = ''; + options.onTag = false; + takeChar = false; + } else revertFromIgnore(); + //console.log(options.closerTag , ignoreInside.indexOf(options.tag.name) > -1 ,dom.last.name == options.tag.name , dom.last.parent.name == options.tag.name); + } else { + //normal tags: push, check if ignore tag + if (options.closerTag) { + var openTags = dom.openTags; + var checkedTags = []; + var found = false; + while (openTags.length>1 && !found) { + var index = openTags.pop(); + if (dom[index].name == options.tag.name) { + found = true; + options.tag = dom[index]; + while (checkedTags.length >0) checkedTags.pop().closed = true; + } else checkedTags.push(dom[index]); + } + if (found) { + options.lastChild = options.tag.index; + options.lastParent = options.tag.parent; + options.tag.closed = true; + } //else error + options.onTag = false; + // if content are to be treated as text + options.closerTag = false; + takeChar = false; + // set options.mode back to text + options.mode = 0; + options.lastAttr =''; + options.openQuote = false; options.openDoubleQuote = false; + } else { + dom.push(options.tag); + var myIndex = options.tag.index; + if (options.lastChar==closeTagChar || singletons.indexOf(options.tag.name)>-1) options.tag.closed = true; + else options.lastParent = myIndex; + options.lastChild = myIndex; + options.onTag = false; + // if content are to be treated as text + options.ignore = ignoreInside.indexOf(options.tag.name) == -1 ? false : true; + // reset closer options.tag + options.closerTag = false; + takeChar = false; + if (options.mode==2 && options.lastAttr.length > 0) { + if (forceAttrsToLower.indexOf(options.lastAttr.toLowerCase())>-1) options.lastAttr = options.lastAttr.toLowerCase(); + options.tag.attrs[options.lastAttr] = true; + } + // set options.mode back to text + options.mode = 0; + options.lastAttr =''; + options.openQuote = false; options.openDoubleQuote = false; + } + } + } + } + //console.log(options.onTag,options.tag.name, options.ignore , options.comment , isEndOfComment()); + } break; + case closeTagChar: { + if (options.onTag && !options.comment && !(options.openQuote || options.openDoubleQuote)) { // removed !options.ignore + if(options.lastChar==openChar) options.closerTag = true; + takeChar = false; + }; + } break; + case eqChar: { + if (options.onTag && !options.comment && !(options.openQuote || options.openDoubleQuote)) { // removed !options.ignore + if (options.mode == 2) options.mode = 3; //else error + + + if (forceAttrsToLower.indexOf(options.lastAttr.toLowerCase())>-1) options.lastAttr = options.lastAttr.toLowerCase(); + options.tag.attrs[options.lastAttr] = ''; + + takeChar = false; + } + } break; + case qChar: { + if ((options.onTag || options.ignore) && !options.comment) { + options.openQuote = options.openDoubleQuote ? options.openQuote : (options.slashed ? options.openQuote : !options.openQuote); + takeChar = options.ignore || options.openDoubleQuote; + } + } break; + case dqChar: { + if ((options.onTag || options.ignore) && !options.comment) { + options.openDoubleQuote = options.openQuote ? options.openDoubleQuote : (options.slashed ? options.openDoubleQuote : !options.openDoubleQuote); + takeChar = options.ignore || options.openQuote; + } + } break; + default: {} break; + } + //if (!options.onTag) options.mode = 0; + if (options.comment) { + if (options.tag.text) options.tag.text += currentChar; + else options.tag.text = currentChar; + } else { + switch (options.mode) { + case 0: { + if (takeChar) { + var target = options.lastChild > -1 ? dom[options.lastChild] : options.tag ; + if (options.lastChild == options.lastParent) { + if (target.text) target.text += currentChar; //inside + else target.text=currentChar; + } else if (options.lastParent < options.lastChild) { + if (target.textAfter) target.textAfter += currentChar; //after + else target.textAfter=currentChar; + } + //if (!options.ignore) lastResolved = i; + } + } break; //text + case 1: { + if (!whiteSpace.test(currentChar) && takeChar) { + options.tag.name += currentChar.toLowerCase(); + if (options.tag.name == commentOpen) {options.comment = true; options.mode = 0;} //options.mode not ness + } else if (options.tag.name.length > 0) { + options.mode=2; + } + } break; //tagname + case 2: { + if (!whiteSpace.test(currentChar) && takeChar) { + if (options.tag.attrs[options.lastAttr]) options.lastAttr = ''; + options.lastAttr += currentChar; + } else { + if (options.lastAttr.length > 0) { + if (forceAttrsToLower.indexOf(options.lastAttr.toLowerCase())>-1) options.lastAttr = options.lastAttr.toLowerCase(); + options.tag.attrs[options.lastAttr] = true; + } + } + } break; //attrname + case 3: { + if ((!whiteSpace.test(currentChar) || options.openQuote || options.openDoubleQuote) && takeChar) { + options.tag.attrs[options.lastAttr] += currentChar; + } else if (options.tag.attrs[options.lastAttr].length>0){ + options.lastAttr = ''; + options.mode = 2; + } + } break; //attrval + } + } + if(options.openQuote || options.openDoubleQuote){ + if (currentChar==sChar) options.slashed=!options.slashed; + else options.slashed = false; + } else options.slashed = false; + if (!whiteSpace.test(currentChar)) options.lastChar = currentChar; + if (options.comment) options.lastTwoChars = (options.lastTwoChars + currentChar).substr(-2); + else options.lastTwoChars = ''; + //if (options.mode >0 && !options.onTag) options.mode = 0; // track if there's an err + }; +} + +module.exports = html; \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..7e47c3f --- /dev/null +++ b/index.js @@ -0,0 +1,28 @@ +var domarr = require('./dom.arr.js'); +var util = require('util'); + +var Tag = function (){} +var DOM = function (){ + // switch from + // if + this.arr = domarr.makeDOM([]) +} + +var html = function (){} +var emmet = require('./emmet.js'); +var jsonml = function (){} + + +var select = require('./dom.select.js'); +exports.makeTag = domarr.makeTag; +exports.makeDOM = domarr.makeDOM; +exports.read = { + html : function () {}, + emmet : emmet.read, + jsonml : function () {} +} +exports.render = { + html : function (dom) {}, + emmet : emmet.render, + jsonml : function (dom) {} +} \ No newline at end of file diff --git a/jsonml.js b/jsonml.js new file mode 100644 index 0000000..cd7b3c7 --- /dev/null +++ b/jsonml.js @@ -0,0 +1,36 @@ +var domarr = require('./dom.arr') + ,jsonml = {}; + +function el(arr,dom,parent) { + if (arr instanceof Array) { + var tag = domarr.makeTag([],arr.shift(),{},parent); + parent = dom.push(tag)-1; + while (arr.length>0) { + var next = arr.shift(); + if (next instanceof Array) { + el(next,dom,parent); + } else if (typeof next == 'string') { + if (dom.last==tag) tag.text = next; + else dom.last.textAfter = next; + } else { + tag.attrs = next; + } + } + } else throw new Error('not a valid jsonml array') +} + +jsonml.read = function (source) { + if (typeof source == 'string') { + source = JSON.parse(source); + } + + var res = domarr.makeDOM([]); + el(source,res,-1); + return res; +} + +jsonml.render = function (dom) { + +} + +module.exports = jsonml; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..9ad82a3 --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "name": "flatdom", + "version": "0.0.0", + "description": "A flat array representation of DOM data", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": "", + "keywords": [ + "dom", + "array", + "node", + "css", + "select" + ], + "author": "Soroosh Izadian", + "license": "MIT", + "readmeFilename": "README.md" +}