| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library trydart.htmlToText; | 5 library trydart.htmlToText; |
| 6 | 6 |
| 7 import 'dart:math' show | 7 import 'dart:math' show |
| 8 max; | 8 max; |
| 9 | 9 |
| 10 import 'dart:html' show | 10 import 'dart:html' show |
| 11 Element, | 11 Element, |
| 12 Node, | 12 Node, |
| 13 NodeFilter, | 13 NodeFilter, |
| 14 ShadowRoot, |
| 14 Text, | 15 Text, |
| 15 TreeWalker; | 16 TreeWalker; |
| 16 | 17 |
| 17 import 'selection.dart' show | 18 import 'selection.dart' show |
| 18 TrySelection; | 19 TrySelection; |
| 19 | 20 |
| 20 /// Returns true if [node] is a block element, that is, not inline. | 21 /// Returns true if [node] is a block element, that is, not inline. |
| 21 bool isBlockElement(Node node) { | 22 bool isBlockElement(Node node) { |
| 22 if (node is! Element) return false; | 23 if (node is! Element) return false; |
| 23 Element element = node; | 24 Element element = node; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 54 case Node.CDATA_SECTION_NODE: | 55 case Node.CDATA_SECTION_NODE: |
| 55 case Node.TEXT_NODE: | 56 case Node.TEXT_NODE: |
| 56 if (selection.anchorNode == node) { | 57 if (selection.anchorNode == node) { |
| 57 selectionOffset = selection.anchorOffset + buffer.length; | 58 selectionOffset = selection.anchorOffset + buffer.length; |
| 58 } | 59 } |
| 59 Text text = node; | 60 Text text = node; |
| 60 buffer.write(text.data.replaceAll('\xA0', ' ')); | 61 buffer.write(text.data.replaceAll('\xA0', ' ')); |
| 61 break; | 62 break; |
| 62 | 63 |
| 63 default: | 64 default: |
| 64 if (node.nodeName == 'BR') { | 65 if (!ShadowRoot.supported && |
| 66 node is Element && |
| 67 node.getAttribute('try-dart-shadow-root') != null) { |
| 68 skip(node, walker); |
| 69 } else if (node.nodeName == 'BR') { |
| 65 buffer.write('\n'); | 70 buffer.write('\n'); |
| 66 } else if (node != root && isBlockElement(node)) { | 71 } else if (node != root && isBlockElement(node)) { |
| 67 selectionOffset = | 72 selectionOffset = |
| 68 max(selectionOffset, htmlToText(node, buffer, selection)); | 73 max(selectionOffset, htmlToText(node, buffer, selection)); |
| 69 skip(node, walker); | 74 skip(node, walker); |
| 70 } | 75 } |
| 71 break; | 76 break; |
| 72 } | 77 } |
| 73 } | 78 } |
| 74 | 79 |
| 75 if (isBlockElement(root)) { | 80 if (isBlockElement(root)) { |
| 76 buffer.write('\n'); | 81 buffer.write('\n'); |
| 77 } | 82 } |
| 78 | 83 |
| 79 return selectionOffset; | 84 return selectionOffset; |
| 80 } | 85 } |
| OLD | NEW |