javascript - Text node numbering and replacement for database indexing not sure how robust this would be -
javascript - Text node numbering and replacement for database indexing not sure how robust this would be -
look ++++ if want see code - preamble context.
pre-amble / explanation
i building offline knowledge base of operations client (i.e. exclusively javascript off-line ability add together pages - can't utilize appcache page content).
i have wysiwyg editor , going store html version of document , stripped version (all html tags removed) separately in indexeddb/websql (with text version beingness used search key terms).
id html plain_text //schema simplified after bit of thought decided improve traverse text nodes , store each 1 of in database. replace node text ((x)) x number of node.
id pageid nodeid nodetext and sec table
pageid html this seems efficient way store text searches (as can store each text nodes text in separate row in database rather 1 huge block).
(for clarity)
<div id="cheese">magical content</div> <ul><li>mystical content</li></ul> would become
<div id="cheese">((0))</div> <ul><li>((1))</li></ul> with array containing
0 => "magical conten", 1 => "mystical content" that stored in database , used reconstruct page using sec version of html.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
the code bit if want skip preamble there illustration of 'why' (in case tells me idiot , has improve suggestion!)
easiest way see mean check fiddle
comment out 'convertnodeback(div); @ end see mean replacing text nodes ((0)), ((1)) etc.
check console see array of text elements.
then uncomment , re-run , text should replaced in right places. seems work ok , fast.
my question is
how robust solution - has tried similar , come across unusual cases / border cases??
are there cases won't work expected?
thanks in advance.
js incase happens fiddle (as hate link questions!!)
var start = new date().gettime(); var textnodes = []; //node, want single string output or array, want match empty nodes? function gettextnodesin(node, singlestring, includewhitespacenodes) { var nonwhitespacematcher = /\s/; //singlestringtext used single string page words. var singlestringtext = ""; //counter number of matches. var counter = 0; function gettextnodes(node) { if (node.nodetype == 3) { if (includewhitespacenodes || nonwhitespacematcher.test(node.nodevalue)) { //remove line breaks , trim string. var itemtext = node.textcontent.replace(/(\r\n|\n|\r)/gm,"").trim(); if(singlestring){ singlestringtext += itemtext + " "; }else{ textnodes[counter] = itemtext; node.nodevalue = "((" + counter + "))"; counter ++; } } } else { (var = 0, len = node.childnodes.length; < len; ++i) { gettextnodes(node.childnodes[i]); } } } gettextnodes(node); if(singlestring){ homecoming singlestringtext; }else{ homecoming textnodes; } } //converts given document original function convertnodesback(node, singlestring, includewhitespacenodes) { var nonwhitespacematcher = /\s/; var singlestringtext = ""; var counter = 0; function gettextnodes(node) { if (node.nodetype == 3) { if(node.nodevalue.match(/\(\(([^)]+)\)\)/g)){ var nodenumber = node.nodevalue.replace(/[()]/g,''); node.nodevalue = textnodes[counter]; counter ++; } } else { (var = 0, len = node.childnodes.length; < len; ++i) { gettextnodes(node.childnodes[i]); } } } gettextnodes(node); if(singlestring){ homecoming singlestringtext; }else{ homecoming textnodes; } } //convert page node text mode. var div = document.getelementbyid('bodytext'); gettextnodesin(div); //convert page text node text mode. var div = document.getelementbyid('bodytext'); convertnodesback(div); console.log(textnodes); console.log(new date().gettime() - start); javascript html dom text
Comments
Post a Comment