2013-01-21 1 views
1

타임 라인에서 TextField의 * .htmlText 속성에 액세스하려면 어떻게합니까? 런타임시 ActionScript 3.0처럼 모든 서식 지정 정보를 반환 할 수있는 무언가를 찾고 있습니다.JSFL의 TextField에서 HTML 형식 문자열을 가져 오는 방법

예 :

<TEXTFORMAT LEADING="2"> 
    <P ALIGN="CENTER"> 
     <FONT FACE="Verdana" 
       SIZE="64" 
       COLOR="#FF0000" 
       LETTERSPACING="0" 
       KERNING="1"> 
      <B>This is a </B> 
      <FONT COLOR="#000000"> 
       <B>bold</B> 
       <FONT SIZE="33"> 
        <B>example</B> 
       </FONT> 
      </FONT> 
     </FONT> 
    </P> 
</TEXTFORMAT> 
+0

해결책을 내놓고 ... 아직 다시! 완료되면 솔루션을 게시합니다. – bigp

답변

2

처음부터 그것을 작성했다! 그래서 여기있다 :

/* 

    Usage: 
    var labelHTML = HTMLUtils.convertToHTML(someLabel); 
    trace("The HTML of the selected label in the IDE is:\n" + labelHTML); 

*/ 

var _TAG_TEMPLATE = "<$0$1>$2</$0>"; 

HTMLUtils = { 

    convertToHTML: function(pTextField) { 
     var runs = pTextField.textRuns, 
      run, content, output, 
      leading; 

     this.rootNode = new HTMLElement("ROOT"); 

     for (var r=0, rLen=runs.length; r<rLen; r++) { 
      run = runs[r]; 
      content = run.characters; 

      this.convertAttrsToHTML(run.textAttrs, content); 
     } 

     this.currentTextFormat = null; 

     return this.rootNode.toHTML(true); 
    }, 

    convertAttrsToHTML: function(pTextAttrs, pContent) { 
     var contentLines = pContent.split("\r"); 
     var masterFontNode; 

     if(!this.currentTextFormat) { 
      masterFontNode = this.createNewTextFormat(pTextAttrs); 
     } else { 
      masterFontNode = this.currentTextFormat.childAt(0,0); 
     } 

     var fontNode = new HTMLFont(); 
     fontNode.addNode(new HTMLText(pContent)); 
     this.assignFontAttributes(fontNode, pTextAttrs); 

     masterFontNode.addNode(fontNode); 

     //trace(pTextAttrs.toTrace()); 

     this.currentTextFormat.attributes.leading = String(pTextAttrs.lineSpacing); 
     this.currentTextFormat.children[0].attributes.align = String(pTextAttrs.alignment); 

     if(contentLines.length>1) { 
      this.currentTextFormat = null; // 
     } 
    }, 

    createNewTextFormat: function(pTextAttrs) { 
     this.currentTextFormat = new HTMLElement("TEXTFORMAT"); 
     this.rootNode.addNode(this.currentTextFormat); 

     var paragraph = new HTMLElement("P"); 
     this.currentTextFormat.addNode(paragraph); 

     var fontNode = new HTMLFont(); 
     paragraph.addNode(fontNode); 

     this.assignFontAttributes(fontNode, pTextAttrs); 

     return fontNode; 
    }, 

    assignFontAttributes: function(pFontNode, pTextAttrs) { 
     pFontNode.attributes.face = String(pTextAttrs.face); 
     pFontNode.attributes.size = String(pTextAttrs.size); 
     pFontNode.attributes.letterSpacing = String(pTextAttrs.letterSpacing); 
     pFontNode.attributes.color = String(pTextAttrs.fillColor); 
     pFontNode.isBold =  pTextAttrs.bold; 
     pFontNode.isItalic = pTextAttrs.italic; 
    } 
}; 


HTMLElement = Class.extend({ 
    init: function(pName) { 
     this.name = pName; 
     this.children = []; 
     this.parent = null; 
     this.attributes = {}; 
    }, 

    clone: function(pOnlyThis) { 
     var theClone = new HTMLElement(this.name); 
     theClone.attributes = this.attributes.copy(); 
     return theClone; 
    }, 

    addNode: function(pNode) { 
     this.children.push(pNode); 
     pNode.parent = this; 

     return pNode; 
    }, 

    childAt: function() { 
     var current = this; 
     for (var a=0, aLen=arguments.length; a<aLen; a++) { 
      var index = arguments[a]; 

      current = current.children[index]; 
     } 

     return current; 
    }, 

    parentOfType: function(pName) { 
     var currentNode = this.parent; 
     while(currentNode && currentNode.name!=pName) { 
      currentNode = currentNode.parent; 
     } 

     return currentNode; 
    }, 

    childrenHTML: function() { 
     var theHTML = ""; 
     var theChildren = this.children, 
      theChild; 

     for (var c=0, cLen=theChildren.length; c<cLen; c++) { 
      theChild = theChildren[c]; 
      theHTML += theChild.toHTML(); 
     } 

     return theHTML; 
    }, 

    toHTML: function(pInnerOnly) { 
     var theHTML = this.childrenHTML(); 

     if(pInnerOnly) { 
      return theHTML; 
     } 

     var theAttributes = []; 
     var theAttrProperties = this.attributes.getProperties(); 

     for(var a=0, aLen=theAttrProperties.length; a<aLen; a++) { 
      var attr =  theAttrProperties[a]; 
      var attrBIG = attr.toUpperCase(); 
      var attrValue = this.attributes[attr]; 
      theAttributes.push(attrBIG + "=\"" + attrValue + "\""); 
     } 

     if(theAttributes.length==0) { 
      theAttributes = ""; 
     } else { 
      theAttributes = " " + theAttributes.join(" "); 
     } 

     return _TAG_TEMPLATE.inject(this.name, theAttributes, theHTML); 
    } 
}); 

HTMLFont = HTMLElement.extend({ 
    init: function() { 
     this._super("FONT"); 
    }, 
    toHTML: function(pInnerOnly) { 
     var parentFont = this.parentOfType("FONT"); 
     if(parentFont) { 
      //Find differences in attributes: 
      var parentAttrs = parentFont.attributes; 
      var myAttrs =  this.attributes; 

      var theAttrProperties = myAttrs.getProperties(); 
      var differentAttrs = []; 

      for (var a=0, aLen=theAttrProperties.length; a<aLen; a++) { 
       var attr =   theAttrProperties[a]; 
       var attrValue =  myAttrs[attr]; 
       var parentValue = parentAttrs[attr]; 

       if(parentValue==null || parentValue==attrValue) { 
        continue; 
       } 

       differentAttrs.push(attr.toUpperCase() + "=\"" + attrValue + "\""); 
      } 

      var theHTML = this.childrenHTML(); 

      if(this.isBold) { theHTML = "<B>" + theHTML + "</B>"; } 
      if(this.isItalic) { theHTML = "<I>" + theHTML + "</I>"; } 

      if(differentAttrs.length==0) { 
       return theHTML; 
      } else { 
       differentAttrs = " " + differentAttrs.join(" "); 
      } 

      return _TAG_TEMPLATE.inject(this.name, differentAttrs, theHTML); 
     } 
     return this._super(pInnerOnly); 
    } 
}); 

HTMLText = HTMLElement.extend({ 
    init: function(pContent) { 
     this._super("TEXT"); 
     this._content = pContent; 
    }, 
    toHTML: function() { 
     return this._content; 
    } 
}); 

참고 : 섹션에 대한그들을 확장 클래스를 정의하고,이 사이트에서 해당 기능을 얻을 수 있습니다 John Resig's Inheritance Script for JavaScript - based on Prototype합니다. 모든 자바 스크립트 기반 언어의 핵심에있는 훌륭한 스크립트이며, OOP를 너무 단순하게 만듭니다!

+0

내가 잊어 버린 일부 종속성이있을 수 있지만, 기본적으로 모든 객체 반복 (예 : '속성'속성을 거침)을 위해, 자바 스크립트에서 단순히 for-in 루프를 수행 할 수 있습니다. 여기서 키는 속성 이름, object [key]는 값입니다. – bigp

+0

동일한 기능을 Java로 찾았습니까? – tymspy

+0

이 코드를 만진 이후로 꽤 오래되었습니다./플래시와 HTML에서 HTML 텍스트를 다룰 수는 없었습니다. (Java와 비슷한 솔루션이 있는지 미안합니다. – bigp

관련 문제