Chromium Code Reviews| Index: dart/site/try/src/html_to_text.dart |
| diff --git a/dart/site/try/src/html_to_text.dart b/dart/site/try/src/html_to_text.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..df286ceb64a92642e5d6d31ba0facbe63797893b |
| --- /dev/null |
| +++ b/dart/site/try/src/html_to_text.dart |
| @@ -0,0 +1,73 @@ |
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library trydart.htmlToText; |
| + |
| +import 'dart:math' show |
| + max; |
|
kasperl
2014/03/25 14:41:22
So you use show on the (small) import of dart:math
ahe
2014/03/26 10:02:12
Yes. Generally, I have allowed myself to import da
|
| + |
| +import 'dart:html'; |
| + |
| +/// Returns true if [node] is a block element, that is, not inline. |
| +bool isBlockElement(Node node) { |
| + if (node is! Element) return false; |
| + Element element = node; |
| + return element.getComputedStyle().display != 'inline'; |
| +} |
| + |
| +/// Position [walker] at the last predecessor (that is, child of child of |
| +/// child...) of [node]. The next call to walker.nextNode will return the first |
| +/// node after [node]. |
| +void skip(Node node, TreeWalker walker) { |
| + if (walker.nextSibling() != null) { |
| + walker.previousNode(); |
| + return; |
| + } |
| + for (Node current = walker.nextNode(); |
| + current != null; |
| + current = walker.nextNode()) { |
| + if (!node.contains(current)) { |
| + walker.previousNode(); |
| + return; |
| + } |
| + } |
| +} |
| + |
| +/// Writes the text of [root] to [buffer]. Keeps track of [selection] and |
| +/// returns the new anchorOffset from beginning of [buffer] or -1 if the |
| +/// selection isn't in [root]. |
| +int htmlToText(Node root, StringBuffer buffer, Selection selection) { |
| + int selectionOffset = -1; |
| + TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_ALL); |
| + |
| + for (Node node = root; node != null; node = walker.nextNode()) { |
| + |
|
kasperl
2014/03/25 14:41:22
Remove newline?
ahe
2014/03/26 10:02:12
Done.
|
| + switch (node.nodeType) { |
| + case Node.CDATA_SECTION_NODE: |
| + case Node.TEXT_NODE: |
| + if (selection.isCollapsed && selection.anchorNode == node) { |
| + selectionOffset = selection.anchorOffset + buffer.length; |
| + } |
| + Text text = node; |
| + buffer.write(text.data.replaceAll('\xA0', ' ')); |
| + break; |
| + |
| + default: |
| + if (node.nodeName == 'BR') { |
| + buffer.write('\n'); |
| + } else if (node != root && isBlockElement(node)) { |
| + selectionOffset = |
| + max(htmlToText(node, buffer, selection), selectionOffset); |
|
kasperl
2014/03/25 14:41:22
I'd put selectorOffset as the first argument to ma
ahe
2014/03/26 10:02:12
Done. Much nicer :-)
|
| + skip(node, walker); |
| + } |
| + break; |
| + } |
| + } |
| + |
| + if (isBlockElement(root)) { |
| + buffer.write('\n'); |
| + } |
| + |
| + return selectionOffset; |
| +} |