|
| 1 | +package { |
| 2 | +import flash.display.*; |
| 3 | +import flash.text.*; |
| 4 | + |
| 5 | +public class Test extends Sprite { |
| 6 | + private var style:StyleSheet; |
| 7 | + |
| 8 | + public function Test() { |
| 9 | + runTests(); |
| 10 | + } |
| 11 | + |
| 12 | + function runTests() { |
| 13 | + resetStyle(); |
| 14 | + runHtmlTests(); |
| 15 | + } |
| 16 | + |
| 17 | + function resetStyle() { |
| 18 | + style = new StyleSheet(); |
| 19 | + } |
| 20 | + |
| 21 | + function runHtmlTests() { |
| 22 | + // display attribute on class |
| 23 | + var displayInline:Object = new Object(); |
| 24 | + displayInline.display = "inline"; |
| 25 | + var displayBlock:Object = new Object(); |
| 26 | + displayBlock.display = "block"; |
| 27 | + var displayNone:Object = new Object(); |
| 28 | + displayNone.display = "none"; |
| 29 | + style.setStyle(".di", displayInline); |
| 30 | + style.setStyle(".db", displayBlock); |
| 31 | + style.setStyle(".dn", displayNone); |
| 32 | + runHtmlTest('x<p class="di">a</p>y<span class="di">b</span>z<a class="di">c</a>s'); |
| 33 | + runHtmlTest('x<p class="db">a</p>y<span class="db">b</span>z<a class="db">c</a>s'); |
| 34 | + runHtmlTest('x<p class="dn">a</p>y<span class="dn">b</span>z<a class="dn">c</a>s'); |
| 35 | + resetStyle(); |
| 36 | + |
| 37 | + // display attribute on a custom tag |
| 38 | + var displayInline:Object = new Object(); |
| 39 | + displayInline.display = "inline"; |
| 40 | + var displayBlock:Object = new Object(); |
| 41 | + displayBlock.display = "block"; |
| 42 | + var displayNone:Object = new Object(); |
| 43 | + displayNone.display = "none"; |
| 44 | + style.setStyle("di", displayInline); |
| 45 | + style.setStyle("db", displayBlock); |
| 46 | + style.setStyle("dn", displayNone); |
| 47 | + runHtmlTest('<di>a</di>'); |
| 48 | + runHtmlTest('<db>a</db>'); |
| 49 | + runHtmlTest('<dn>a</dn>'); |
| 50 | + runHtmlTest('x<di></di>y'); |
| 51 | + runHtmlTest('x<db></db>y'); |
| 52 | + runHtmlTest('x<dn></dn>y'); |
| 53 | + runHtmlTest('x<di>y'); |
| 54 | + runHtmlTest('x<db>y'); |
| 55 | + runHtmlTest('x<dn>y'); |
| 56 | + runHtmlTest('x<di></p>y'); |
| 57 | + runHtmlTest('x<db></p>y'); |
| 58 | + runHtmlTest('x<dn></p>y'); |
| 59 | + runHtmlTest('x<di>a</di>y<db>b</db>z<dn>c</dn>s'); |
| 60 | + resetStyle(); |
| 61 | + |
| 62 | + // display attribute on known tags (inline) |
| 63 | + var displayInline:Object = new Object(); |
| 64 | + displayInline.display = "inline"; |
| 65 | + style.setStyle("p", displayInline); |
| 66 | + style.setStyle("li", displayInline); |
| 67 | + style.setStyle("a", displayInline); |
| 68 | + runHtmlTest('<p>a</p>'); |
| 69 | + runHtmlTest('<li>a</li>'); |
| 70 | + runHtmlTest('<a>a</a>'); |
| 71 | + runHtmlTest('x<p>a</p>y<li>b</li>z<a>c</a>s'); |
| 72 | + resetStyle(); |
| 73 | + |
| 74 | + // display attribute on known tags (block) |
| 75 | + var displayBlock:Object = new Object(); |
| 76 | + displayBlock.display = "block"; |
| 77 | + style.setStyle("p", displayBlock); |
| 78 | + style.setStyle("li", displayBlock); |
| 79 | + style.setStyle("a", displayBlock); |
| 80 | + runHtmlTest('<p>a</p>'); |
| 81 | + runHtmlTest('<li>a</li>'); |
| 82 | + runHtmlTest('<a>a</a>'); |
| 83 | + runHtmlTest('x<p>a</p>y<li>b</li>z<a>c</a>s'); |
| 84 | + resetStyle(); |
| 85 | + |
| 86 | + // display attribute on known tags (none) |
| 87 | + var displayNone:Object = new Object(); |
| 88 | + displayNone.display = "none"; |
| 89 | + style.setStyle("p", displayNone); |
| 90 | + style.setStyle("li", displayNone); |
| 91 | + style.setStyle("a", displayNone); |
| 92 | + runHtmlTest('<p>a</p>'); |
| 93 | + runHtmlTest('<li>a</li>'); |
| 94 | + runHtmlTest('<a>a</a>'); |
| 95 | + runHtmlTest('x<p>a</p>y<li>b</li>z<a>c</a>s'); |
| 96 | + resetStyle(); |
| 97 | + |
| 98 | + // display on tag without style |
| 99 | + runHtmlTest('<as>a</as>'); |
| 100 | + runHtmlTest('x<as>a</as>y<as>b</as>z'); |
| 101 | + resetStyle(); |
| 102 | + } |
| 103 | + |
| 104 | + function runHtmlTest(html: String) { |
| 105 | + var text = newTextField(); |
| 106 | + text.styleSheet = style; |
| 107 | + |
| 108 | + trace("======================================"); |
| 109 | + trace(" Setting HTML: " + escape(html)); |
| 110 | + text.htmlText = html; |
| 111 | + trace(" HTML get: " + escape(text.htmlText)); |
| 112 | + trace(" Text get: " + escape(text.text)); |
| 113 | + printTextRuns(text.getTextRuns()); |
| 114 | + text.styleSheet = null; |
| 115 | + trace(" Style reset: " + escape(text.htmlText)); |
| 116 | + printTextRuns(text.getTextRuns()); |
| 117 | + } |
| 118 | + |
| 119 | + private function printTextRuns(runs: Array) { |
| 120 | + trace(" Text runs (" + runs.length + "):"); |
| 121 | + for each (var run in runs) { |
| 122 | + trace(" from " + run.beginIndex + " to " + run.endIndex + ": " + describeTextFormat(run.textFormat)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private function describeTextFormat(tf: TextFormat): String { |
| 127 | + return "size=" + tf.size + |
| 128 | + ", blockIndent=" + tf.blockIndent + |
| 129 | + ", font=" + tf.font + |
| 130 | + ", align=" + tf.align + |
| 131 | + ", leading=" + tf.leading + |
| 132 | + ", display=" + tf.display + |
| 133 | + ", kerning=" + tf.kerning + |
| 134 | + ", leftMargin=" + tf.leftMargin + |
| 135 | + ", rightMargin=" + tf.rightMargin + |
| 136 | + ", color=" + tf.color + |
| 137 | + ", bold=" + tf.bold + |
| 138 | + ", italic=" + tf.italic + |
| 139 | + ", bullet=" + tf.bullet + |
| 140 | + ", underline=" + tf.underline; |
| 141 | + } |
| 142 | + |
| 143 | + private function escape(string: String): String { |
| 144 | + return string.replace(/\n/g, "\\n").replace(/\r/g, "\\r"); |
| 145 | + } |
| 146 | + |
| 147 | + private function newTextField(): TextField { |
| 148 | + var text = new TextField(); |
| 149 | + text.defaultTextFormat = new TextFormat("FontName", 10); |
| 150 | + return text; |
| 151 | + } |
| 152 | +} |
| 153 | +} |
0 commit comments