| OLD | NEW |
| (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'; |
| 11 |
| 12 /// Returns true if [node] is a block element, that is, not inline. |
| 13 bool isBlockElement(Node node) { |
| 14 if (node is! Element) return false; |
| 15 Element element = node; |
| 16 return element.getComputedStyle().display != 'inline'; |
| 17 } |
| 18 |
| 19 /// Position [walker] at the last predecessor (that is, child of child of |
| 20 /// child...) of [node]. The next call to walker.nextNode will return the first |
| 21 /// node after [node]. |
| 22 void skip(Node node, TreeWalker walker) { |
| 23 if (walker.nextSibling() != null) { |
| 24 walker.previousNode(); |
| 25 return; |
| 26 } |
| 27 for (Node current = walker.nextNode(); |
| 28 current != null; |
| 29 current = walker.nextNode()) { |
| 30 if (!node.contains(current)) { |
| 31 walker.previousNode(); |
| 32 return; |
| 33 } |
| 34 } |
| 35 } |
| 36 |
| 37 /// Writes the text of [root] to [buffer]. Keeps track of [selection] and |
| 38 /// returns the new anchorOffset from beginning of [buffer] or -1 if the |
| 39 /// selection isn't in [root]. |
| 40 int htmlToText(Node root, StringBuffer buffer, Selection selection) { |
| 41 int selectionOffset = -1; |
| 42 TreeWalker walker = new TreeWalker(root, NodeFilter.SHOW_ALL); |
| 43 |
| 44 for (Node node = root; node != null; node = walker.nextNode()) { |
| 45 switch (node.nodeType) { |
| 46 case Node.CDATA_SECTION_NODE: |
| 47 case Node.TEXT_NODE: |
| 48 if (selection.isCollapsed && selection.anchorNode == node) { |
| 49 selectionOffset = selection.anchorOffset + buffer.length; |
| 50 } |
| 51 Text text = node; |
| 52 buffer.write(text.data.replaceAll('\xA0', ' ')); |
| 53 break; |
| 54 |
| 55 default: |
| 56 if (node.nodeName == 'BR') { |
| 57 buffer.write('\n'); |
| 58 } else if (node != root && isBlockElement(node)) { |
| 59 selectionOffset = |
| 60 max(selectionOffset, htmlToText(node, buffer, selection)); |
| 61 skip(node, walker); |
| 62 } |
| 63 break; |
| 64 } |
| 65 } |
| 66 |
| 67 if (isBlockElement(root)) { |
| 68 buffer.write('\n'); |
| 69 } |
| 70 |
| 71 return selectionOffset; |
| 72 } |
| OLD | NEW |