Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Correct image loading (queue)

cloneNode corrections and optimization
Modification handling nodes id
Catch errors onreadystatechange (xhr)
Remove ew parser
  • Loading branch information...
commit 1969e1c6124a3bd6cfff600da98822a589ad6a49 1 parent 3469355
root authored
Showing with 123 additions and 79 deletions.
  1. +112 −56 lib/browser/index.js
  2. +3 −9 lib/dom.js
  3. +8 −14 lib/ew/ew.js
View
168 lib/browser/index.js 100644 → 100755
@@ -1,5 +1,5 @@
/*
-Copyright (c) 2011 Naïs - Aymeric Vitte - MIT license
+Copyright (c) 2011 Naïs - Aymeric Vitte - MIT license
Some parts inspired from tmpvar/jsdom (MIT license)
*/
@@ -11,6 +11,8 @@ var domToHtml = require('./domtohtml.js').domToHtml,
https = require('https'),
default_style = require('./style.js').default_style;
+var nbloop=0;
+
Function.prototype.De=function(obj,_name,_node) {
var f=this;
if (typeof(_name)=="undefined") {
@@ -80,6 +82,15 @@ var canon=function(attr){ //border-color --> borderColor
return attr.replace(/-\D/g,function(match){return match.charAt(1).toUpperCase();});
};
+var __cc=function(node) {
+ var str = node.__data.toString().replace(/^\s+/g,'').replace(/\s+$/g,'') ;
+ var reprix0 = /руб|\$||£||¥|Ұ|||||EUR|USD|CHF/g;
+ if (str.match(reprix0)){
+ return true;
+ };
+ return false;
+};
+
var NOT_IMPLEMENTED=function(val) {return function() {return val}};
var NOSTYLE={script:'',style:'',head:'',meta:'',title:'',link:'',base:'',noscript:''};
@@ -102,11 +113,11 @@ core.findParent = function(element, parent) {
core.HtmlToDom = function(page,elt) {
var doc=elt._document;
- if (doc._ew!=='') {
- return doc._parser.ParseHtml(page,doc._ew, doc._ewList);
- } else {
+ //if (doc._ew!=='') {
+ // return doc._parser.ParseHtml(page,doc._ew, doc._ewList);
+ //} else {
return doc._parser.ParseHtml(page);
- };
+ //};
};
core.HtmlToStyle = function(data) {//data= border-width:3px !important
@@ -313,36 +324,39 @@ core.resourceLoader = {
//this.enqueue(element);
//do not queue images
+ //console.log(url.href);
+
var tmp=doc._downloadedImages.loaded(url.href);
//console.log(doc._downloadedImages);
//console.log(tmp.val[1]);
- if (!tmp.val[1]) {
- //If not already in downloadedImages or in downloadedImages but size not known
- if (!tmp.val[0]) {//if url not in loadedimages load it
- doc._downloadedImages.push([url.href,0,0,[]]);
-
- im.identify(['-format', '%wx%h', url.href], function(err, output){
- if (!err) {
- var res=output.replace('\n','');
- doc._downloadedImages.push([url.href,res.split('x')[0],res.split('x')[1]]);
- element.___data=res;
- //if (doc.readyState=='complete') {//Execute right away once queue empty
- self.execute(element);
- //}
- }
- });
- } else { //queue img
- doc._downloadedImages[tmp.index][3].unshift(element);
- };
- } else {
- //Size known
- element.___data=tmp.val[1]+'x'+tmp.val[2];
- //if (doc.readyState=='complete') {//Execute right away once queue empty
- self.execute(element);
- //}
+ if (!tmp.val[1]) {
+ //If not already in downloadedImages or in downloadedImages but size not known
+ if (!tmp.val[0]) {//if url not in loadedimages load it
+ doc._downloadedImages.push([url.href,0,0,[]]);
+
+ im.identify(['-format', '%wx%h', url.href], function(err, output){
+ if (!err) {
+ var res=output.replace('\n','');
+ doc._downloadedImages.push([url.href,res.split('x')[0],res.split('x')[1]]);
+ element.___data=res;
+ //if (doc.readyState=='complete') {//Execute right away once queue empty
+ self.execute(element);
+ //}
+ }
+ });
+ } else { //queue img
+ //console.log('queue '+tmp.index);
+ doc._downloadedImages[tmp.index][3].unshift(element);
+ };
+ } else {
+ //Size known
+ element.___data=tmp.val[1]+'x'+tmp.val[2];
+ //if (doc.readyState=='complete') {//Execute right away once queue empty
+ self.execute(element);
+ //}
}
}
},
@@ -431,15 +445,13 @@ core.resourceLoader = {
h=core.resourceLoader.resolve(element._document,element.src),
size=element.___data.split('x'), win=doc._parentNode,
width=win.screen.width,height=win.screen.height,
- tmp=doc._downloadedImages.loaded(h.href),
+ tmp=doc._downloadedImages.loaded(h),
queue=doc._downloadedImages[tmp.index][3];
queue.unshift(element);
var l=queue.length;
- //console.log('resume img '+queue.length);
-
while (queue.length) { //resume queue for same image
var elem=queue[0];
if (elem.style.getPropertyValue('width')==width) {
@@ -708,6 +720,14 @@ core.Node.prototype={
}
};
+ if ((node.__type=='text')&&(this._document.readyState!='complete')) {
+ if (!(this.__name in NOCONT)) {
+ if (__cc(node)) {
+ this._document._parentNode._ewList.push(node);
+ };
+ };
+ };
+
return node;
},
replaceChild : function(node,before) {
@@ -785,15 +805,48 @@ core.Node.prototype={
}
return fragment;
} else {
- var div=this._document.createElement('div'); //dummy div
- div.innerHTML=this.outerHTML;
- div=div.firstChild;
if (!bool) {
- while (div.firstChild) {
- div.removeChild(div.firstChild)
+ //console.log('cloneNode false');
+ var clone=this._document.createElement(this.__name);
+ var prop=Object.keys(this);
+ var l=prop.length;
+ for (var i=0;i<l;i++) {
+ var p=prop[i];
+ clone.setAttribute(p,this[p]);
+ };
+
+ if (this.style) {
+ prop=Object.keys(this.style);
+ l=prop.length;
+ for (var i=0;i<l;i++) {
+ p=prop[i];
+ clone.style[p]=this.style[p];
+ }
}
- };
- return div;
+ return clone;
+ } else {
+ //TODO : optimization
+ //console.log('cloneNode true');
+ var div=this._document.createElement('div'); //dummy div
+ div.innerHTML=this.outerHTML;
+ div=div.firstChild;
+ return div;
+ //var clone=this.cloneNode(false);
+ //var recurse=function(node,parent) {
+ // var children=node.__children;
+ // var l=children.length;
+ // for (var i=0;i<l;i++) {
+ // var child=children[i];
+ // var clone_=child.cloneNode(false);
+ // if (parent) {
+ // parent.appendChild(clone_);
+ // };
+ // recurse(child,clone_)
+ // }
+ //};
+ //recurse(this,clone);
+ //return clone;
+ }
}
},
cloneStyle : function(style) { //not w3c
@@ -877,6 +930,9 @@ core.Node.prototype={
},
addToDom : function(node) {
node.__dom=true;
+ if (node._id) {
+ node.id=node._id; //set id in document._ids list for getElementById when adding the node to DOM tree
+ };
this.updateNode(node); //update Nodelists
var children=node.__children;
var l=children.length;
@@ -1259,33 +1315,21 @@ core.HTMLElement.prototype = {
return domToHtml(this.__children,this._document._features.removeScript);
},
get outerHTML() {
- //var ini_=new Date().valueOf();
- //console.log('out1-0');
var res=domToHtml(this,this._document._features.removeScript);
-
- //var tmp_=new Date().valueOf()-ini_;
- //console.log('out2-'+tmp_);
return res;
},
set innerHTML(val) {
//See performance issue with CSSStyleDeclaration
- //var ini_=new Date().valueOf();
- //console.log('in1-0');
-
- var _node=core.HtmlToDom(val,this);;
+ var _node=core.HtmlToDom(val,this);
var tmpdom=_node.dom;
var l=tmpdom.length;
this.children=[];
for (var i=0;i<l;i++) {
this.children.push(tmpdom[i]);
};
- //var tmp_=new Date().valueOf()-ini_;
- //console.log('in1b-'+tmp_);
this._document.setTree.call(this,null);
- //var tmp_=new Date().valueOf()-ini_;
- //console.log('in2-'+tmp_);
},
set outerHTML(val) {
var _node=core.HtmlToDom(val,this);
@@ -1500,6 +1544,7 @@ core.CSSStyleDeclaration.prototype={
//Ex: style = 'border-right'
//Simplification for now
//Mainly interested by width/height
+ nbloop++;
var win=this.node._document._parentNode;
var camel=canon(style);
var prop=Object.keys(this);
@@ -1749,6 +1794,14 @@ var DOMWindow=function(document) {
var tmp=header.replace(/^.|(-\D)/g,function(match){return match.toUpperCase();});
osetRequestHeader.call(this,tmp,val);
};
+ var osend=xhr.send;
+ xhr.send=function(data) { //catch errors for onreadystatechange - crash testcastorama to investigate
+ if (this.onreadystatechange) {
+ var o=this.onreadystatechange;
+ this.onreadystatechange=function() {try {o.call(this)} catch(ee) {console.log('onreadystatechange error');}};
+ };
+ osend.call(this,data);
+ };
return xhr;
};
@@ -1769,6 +1822,7 @@ var DOMWindow=function(document) {
this.contentWindow=this;
this.self=this;
this.top=this;
+ this._ewList=[];
};
DOMWindow.prototype = {
@@ -1914,7 +1968,6 @@ core.HTMLDocument=function(parser,options) {
this.body=false;
this.head=false;
this._base='';
- this._ewList=[];
this._options=options;
this._features=options.features||{FetchExternalResources:{},ProcessExternalResources:{},removeScript:false};
this._URL=options.url.href||'';
@@ -2010,17 +2063,18 @@ core.HTMLDocument.prototype = {
this.title=Element.__data;
};
-
if (attribs) {
if ('style' in attribs) {
element.style.cssText=attribs['style'];
};
+ delete attribs['style'];
+
for (var n in attribs) { //assign properties
var m=n.toLowerCase();
if ((m=='href')&&(element.__name=='base')) {element._document._base=attribs[m];};
- if (n=='class') {m='className';} else if (m=='href') {m='__href';} else if (m=='src') {m='__src'};
+ if (m=='class') {m='className';} else if (m=='href') {m='__href';} else if (m=='src') {m='__src'};
element.setAttribute(m,attribs[n]);
};
};
@@ -2050,7 +2104,9 @@ core.HTMLDocument.prototype = {
}
},
_addid : function(element,attr) {
- this._ids[attr]=element; //id list
+ if (element.__dom) { //if in dom tree
+ this._ids[attr]=element; //id list
+ }
},
_addname : function(element,attr) {
if (attr) {
View
12 lib/dom.js 100644 → 100755
@@ -22,9 +22,9 @@ var dom = exports.dom = function(page,level,options) {
features=options.features,
ew=typeof(features.ew)=="undefined"?'':features.ew;
- if (ew!=='') {
- parser=require('./ew/ew.js').parser;
- };
+ //if (ew!=='') {
+ // parser=require('./ew/ew.js').parser;
+ //};
var document=new DOCUMENT(parser,options);
@@ -97,12 +97,6 @@ var dom = exports.dom = function(page,level,options) {
var tmpi=new Date().valueOf()-ini;
- //specific ew
- if (ew!=='') {
- window._ewList=tmp._ewList;
- };
- //specific ew
-
document.close(); //resume resources, readystate complete, fires onload for elements
//fire timers (not used)
View
22 lib/ew/ew.js 100644 → 100755
@@ -5,25 +5,19 @@ Copyright (c) 2011 Naïs - Aymeric Vitte - MIT license
var parser=exports.parser=require('../browser/htmltodom.js').parser,
//createProxy=require('../proxy/proxy.js').createProxy,
HTMLDecode=require('../browser/htmlencoding.js').HTMLDecode;
-
-var TrimJS=function(s) {
-var st = s.toString();
-st = st.replace(/^\s*|\s*$/,"");
-return st;
-};
var __cc=function(tmp) {
var node = tmp;
var str = node._nodeValue.toString().replace(/^\s+/g,'').replace(/\s+$/g,'') ;
-//var reprix0 = /^(руб|\$|€|£|₤|¥|Ұ|¥|円|元|圓|EUR|USD|CHF)$/g;
-var reprix0=['руб','$','','£','','¥','Ұ','','','','','EUR','USD','CHF'];
-//if (str.match(reprix0)){
-
-for (var n in reprix0) {
- if (str.indexOf(n)!=-1) {
+var reprix0 = /руб|\$||£||¥|Ұ|||||EUR|USD|CHF/g;
+//var reprix0=['руб','$','€','£','₤','¥','Ұ','¥','円','元','圓','EUR','USD','CHF'];
+if (str.match(reprix0)){
+//for (var n in reprix0) {
+ //if (str.indexOf(n)!=-1) {
return true;
- }
+ //}
}
+
return false;
};
@@ -32,7 +26,7 @@ parser.ParseHtml = function(rawHtml,ew,_ewList){
var owriteText=this.DefaultHandler.prototype.writeText;
this.DefaultHandler.prototype.writeText = function DefaultHandler$writeText (element) {
if (ew==0) {
- if (TrimJS(element.data)!='') {
+ if (element.data.trim()!='') {
element._nodeValue=HTMLDecode(element.data); //adaptation for parser
if (__cc(element)) {
_ewList.push(element);
Please sign in to comment.
Something went wrong with that request. Please try again.