2010. 4. 20. 11:32

env.js 의 몇가지 부족한 점이 있어서 수정한 부분에 대해 포스팅..

원본: 


원본 파일에서 내게 필요했던 부분 (innerHTML) 중에 잘못 구현되어 있거나, 부족했던 부분이 아래와 같이 있어서 수정하였다.
  1. html 에서 inline style 이 나타나지 않는 문제.
  2. 내용이 없는 경우 닫는 태그가 없는 문제.
  3. 결과물 문자열을 만드는 방식이 문자열 변수에 계속 += 연산자를 사용하는 방식이라서 비효율적으로 동작하는 문제(조금 복잡한 DOM을 다루게 되면 innerHTML을 한번 구하는데 1분 이상 걸리는 등 심각한 성능 문제가 나타남).
env.rhino.js 파일의 6404 번째 라인쯤에 있는 xhtml getter 함수를 다음과 같이 수정합니다.

get xhtml() {
// HTMLDocument.xhtml is non-standard
// This is exactly like Document.xml except the tagName has to be 
// lower cased.  I dont like to duplicate this but its really not
// a simple work around between xml and html serialization via
// XMLSerializer (which uppercases html tags) and innerHTML (which
// lowercases tags)
var ret = "",
ns = "",
name = (this.tagName+"").toLowerCase(),
attrs,
attrstring = "",
i,
retArray = [],
childRet;

// serialize namespace declarations
if (this.namespaceURI){
if((this === this.ownerDocument.documentElement) ||
(!this.parentNode)||
(this.parentNode && 
(this.parentNode.namespaceURI !== this.namespaceURI)))
ns = ' xmlns'+(this.prefix?(':'+this.prefix):'')+
'="'+this.namespaceURI+'"';
}
// serialize Attribute declarations
attrs = this.attributes;
for(i=0;i< attrs.length;i++){
attrstring += " "+attrs[i].name+'="'+attrs[i].xml+'"';
}
var _toCamel = function(name) {
if(name){
return name.replace(/\-(\w)/g, function(all, letter){
return letter.toUpperCase();
});
}
return name;
};
if (this.style && this.style.length > 0) {
attrstring += " style=\"";
var so = this.style;
var i, sl = so.length;
for(i = 0; i < sl; ++i){
attrstring += so[i] + ":" + so[_toCamel(so[i])] + ";";
}
attrstring += "\"";
}
if(this.hasChildNodes()){
// serialize this Element
retArray.push("<");
retArray.push(name);
retArray.push(ns);
retArray.push(attrstring);
retArray.push(">");
for(i=0;i< this.childNodes.length;i++){
childRet = this.childNodes[i].xhtml;
retArray.push(childRet? childRet: this.childNodes[i].xml);
}
retArray.push("</");
retArray.push(name);
retArray.push(">");
}else{
retArray.push("<");
retArray.push(name);
retArray.push(ns);
retArray.push(attrstring);
retArray.push("></");
retArray.push(name);
retArray.push(">");
}
return retArray.join("");
}

수정한 파일:

Posted by 아즈키