Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: site/try/src/html_to_text.dart

Issue 2232273004: Delete site/try (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 trydart.htmlToText;
6
7 import 'dart:math' show
8 max;
9
10 import 'dart:html' show
11 CharacterData,
12 Element,
13 Node,
14 NodeFilter,
15 ShadowRoot,
16 TreeWalker;
17
18 import 'selection.dart' show
19 TrySelection;
20
21 import 'shadow_root.dart' show
22 WALKER_NEXT,
23 WALKER_SKIP_NODE,
24 walkNodes;
25
26 /// Returns true if [node] is a block element, that is, not inline.
27 bool isBlockElement(Node node) {
28 if (node is! Element) return false;
29 Element element = node;
30
31 // TODO(ahe): Remove this line by changing code completion to avoid using a
32 // div element.
33 if (element.classes.contains('dart-code-completion')) return false;
34
35 var display = element.getComputedStyle().display;
36 return display != 'inline' && display != 'none';
37 }
38
39 /// Writes the text of [root] to [buffer]. Keeps track of [selection] and
40 /// returns the new anchorOffset from beginning of [buffer] or -1 if the
41 /// selection isn't in [root].
42 int htmlToText(Node root,
43 StringBuffer buffer,
44 TrySelection selection,
45 {bool treatRootAsInline: false}) {
46 int selectionOffset = -1;
47 walkNodes(root, (Node node) {
48 if (selection.anchorNode == node) {
49 selectionOffset = selection.anchorOffset + buffer.length;
50 }
51 switch (node.nodeType) {
52 case Node.CDATA_SECTION_NODE:
53 case Node.TEXT_NODE:
54 CharacterData text = node;
55 buffer.write(text.data.replaceAll('\xA0', ' '));
56 break;
57
58 default:
59 if (node.nodeName == 'BR') {
60 buffer.write('\n');
61 } else if (node != root && isBlockElement(node)) {
62 selectionOffset =
63 max(selectionOffset, htmlToText(node, buffer, selection));
64 return WALKER_SKIP_NODE;
65 }
66 break;
67 }
68
69 return WALKER_NEXT;
70 });
71
72 if (!treatRootAsInline && isBlockElement(root)) {
73 buffer.write('\n');
74 }
75
76 return selectionOffset;
77 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698