OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library sourcemap.html_parts; |
| 6 |
| 7 import 'sourcemap_html_helper.dart'; |
| 8 |
| 9 class HtmlPrintContext { |
| 10 final int lineNoWidth; |
| 11 final bool usePre; |
| 12 |
| 13 HtmlPrintContext({ |
| 14 this.lineNoWidth, |
| 15 this.usePre: true}); |
| 16 } |
| 17 |
| 18 enum HtmlPartKind { |
| 19 CODE, |
| 20 LINE, |
| 21 CONST, |
| 22 NEWLINE, |
| 23 TEXT, |
| 24 ANCHOR, |
| 25 } |
| 26 |
| 27 abstract class HtmlPart { |
| 28 void printHtmlOn(StringBuffer buffer, HtmlPrintContext context) {} |
| 29 |
| 30 toJson(); |
| 31 |
| 32 static HtmlPart fromJson(json) { |
| 33 if (json is String) { |
| 34 return new ConstHtmlPart(json); |
| 35 } else { |
| 36 switch (HtmlPartKind.values[json['kind']]) { |
| 37 case HtmlPartKind.LINE: |
| 38 return HtmlLine.fromJson(json); |
| 39 case HtmlPartKind.CODE: |
| 40 return CodeLine.fromJson(json); |
| 41 case HtmlPartKind.CONST: |
| 42 return ConstHtmlPart.fromJson(json); |
| 43 case HtmlPartKind.NEWLINE: |
| 44 return const NewLine(); |
| 45 case HtmlPartKind.TEXT: |
| 46 return HtmlText.fromJson(json); |
| 47 case HtmlPartKind.ANCHOR: |
| 48 return AnchorHtmlPart.fromJson(json); |
| 49 } |
| 50 } |
| 51 } |
| 52 } |
| 53 |
| 54 class ConstHtmlPart implements HtmlPart { |
| 55 final String html; |
| 56 |
| 57 const ConstHtmlPart(this.html); |
| 58 |
| 59 @override |
| 60 void printHtmlOn(StringBuffer buffer, HtmlPrintContext context) { |
| 61 buffer.write(html); |
| 62 } |
| 63 |
| 64 toJson() { |
| 65 return {'kind': HtmlPartKind.CONST.index, 'html': html}; |
| 66 } |
| 67 |
| 68 static ConstHtmlPart fromJson(Map json) { |
| 69 return new ConstHtmlPart(json['html']); |
| 70 } |
| 71 } |
| 72 |
| 73 class NewLine implements HtmlPart { |
| 74 const NewLine(); |
| 75 |
| 76 void printHtmlOn(StringBuffer buffer, HtmlPrintContext context) { |
| 77 if (context.usePre) { |
| 78 buffer.write('\n'); |
| 79 } else { |
| 80 buffer.write('<br/>'); |
| 81 } |
| 82 } |
| 83 |
| 84 toJson() { |
| 85 return {'kind': HtmlPartKind.NEWLINE.index}; |
| 86 } |
| 87 } |
| 88 |
| 89 class HtmlText implements HtmlPart { |
| 90 final String text; |
| 91 |
| 92 const HtmlText(this.text); |
| 93 |
| 94 void printHtmlOn(StringBuffer buffer, HtmlPrintContext context) { |
| 95 String escaped = escape(text); |
| 96 buffer.write(escaped); |
| 97 } |
| 98 |
| 99 toJson() { |
| 100 return {'kind': HtmlPartKind.TEXT.index, 'text': text}; |
| 101 } |
| 102 |
| 103 static HtmlText fromJson(Map json) { |
| 104 return new HtmlText(json['text']); |
| 105 } |
| 106 } |
| 107 |
| 108 class AnchorHtmlPart implements HtmlPart { |
| 109 final String color; |
| 110 final String name; |
| 111 final String href; |
| 112 final String title; |
| 113 final String onclick; |
| 114 final String onmouseover; |
| 115 final String onmouseout; |
| 116 |
| 117 AnchorHtmlPart({ |
| 118 this.color, |
| 119 this.name, |
| 120 this.href, |
| 121 this.title, |
| 122 this.onclick, |
| 123 this.onmouseover, |
| 124 this.onmouseout}); |
| 125 |
| 126 @override |
| 127 void printHtmlOn(StringBuffer buffer, HtmlPrintContext context) { |
| 128 buffer.write('<a'); |
| 129 if (href != null) { |
| 130 buffer.write(' href="${href}"'); |
| 131 } |
| 132 if (name != null) { |
| 133 buffer.write(' name="${name}"'); |
| 134 } |
| 135 if (title != null) { |
| 136 buffer.write(' title="${escape(title)}"'); |
| 137 } |
| 138 buffer.write(' style="${color}"'); |
| 139 if (onclick != null) { |
| 140 buffer.write(' onclick="${onclick}"'); |
| 141 } |
| 142 if (onmouseover != null) { |
| 143 buffer.write(' onmouseover="${onmouseover}"'); |
| 144 } |
| 145 if (onmouseout != null) { |
| 146 buffer.write(' onmouseout="${onmouseout}"'); |
| 147 } |
| 148 buffer.write('>'); |
| 149 } |
| 150 |
| 151 toJson() { |
| 152 return { |
| 153 'kind': HtmlPartKind.ANCHOR.index, |
| 154 'color': color, |
| 155 'name': name, |
| 156 'href': href, |
| 157 'title': title, |
| 158 'onclick': onclick, |
| 159 'onmouseover': onmouseover, |
| 160 'onmouseout': onmouseout}; |
| 161 } |
| 162 |
| 163 static AnchorHtmlPart fromJson(Map json) { |
| 164 return new AnchorHtmlPart( |
| 165 color: json['color'], |
| 166 name: json['name'], |
| 167 href: json['href'], |
| 168 title: json['title'], |
| 169 onclick: json['onclick'], |
| 170 onmouseover: json['onmouseover'], |
| 171 onmouseout: json['onmouseout']); |
| 172 } |
| 173 } |
| 174 |
| 175 class HtmlLine implements HtmlPart { |
| 176 final List<HtmlPart> htmlParts = <HtmlPart>[]; |
| 177 |
| 178 @override |
| 179 void printHtmlOn(StringBuffer htmlBuffer, HtmlPrintContext context) { |
| 180 for (HtmlPart part in htmlParts) { |
| 181 part.printHtmlOn(htmlBuffer, context); |
| 182 } |
| 183 } |
| 184 |
| 185 Map toJson() { |
| 186 return { |
| 187 'kind': HtmlPartKind.LINE.index, |
| 188 'html': htmlParts.map((p) => p.toJson()).toList(), |
| 189 }; |
| 190 } |
| 191 |
| 192 static CodeLine fromJson(Map json) { |
| 193 HtmlLine line = new HtmlLine(); |
| 194 json['html'].forEach((part) => line.htmlParts.add(HtmlPart.fromJson(part))); |
| 195 return line; |
| 196 } |
| 197 } |
| 198 |
| 199 class CodeLine extends HtmlLine { |
| 200 final int lineNo; |
| 201 final int offset; |
| 202 final StringBuffer codeBuffer = new StringBuffer(); |
| 203 final List<HtmlPart> htmlParts = <HtmlPart>[]; |
| 204 // TODO(johnniwinther): Make annotations serializable. |
| 205 final List<Annotation> annotations = <Annotation>[]; |
| 206 String _code; |
| 207 |
| 208 CodeLine(this.lineNo, this.offset); |
| 209 |
| 210 String get code { |
| 211 if (_code == null) { |
| 212 _code = codeBuffer.toString(); |
| 213 } |
| 214 return _code; |
| 215 } |
| 216 |
| 217 @override |
| 218 void printHtmlOn(StringBuffer htmlBuffer, HtmlPrintContext context) { |
| 219 htmlBuffer.write(lineNumber( |
| 220 lineNo, width: context.lineNoWidth, useNbsp: !context.usePre)); |
| 221 for (HtmlPart part in htmlParts) { |
| 222 part.printHtmlOn(htmlBuffer, context); |
| 223 } |
| 224 } |
| 225 |
| 226 Map toJson() { |
| 227 return { |
| 228 'kind': HtmlPartKind.CODE.index, |
| 229 'lineNo': lineNo, |
| 230 'offset': offset, |
| 231 'code': code, |
| 232 'html': htmlParts.map((p) => p.toJson()).toList(), |
| 233 }; |
| 234 } |
| 235 |
| 236 static CodeLine fromJson(Map json) { |
| 237 CodeLine line = new CodeLine(json['lineNo'], json['offset']); |
| 238 line.codeBuffer.write(json['code']); |
| 239 json['html'].forEach((part) => line.htmlParts.add(HtmlPart.fromJson(part))); |
| 240 return line; |
| 241 } |
| 242 } |
| 243 |
OLD | NEW |