OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 // This file provides |assert_selection(sample, tester, expectedText, options)| | 7 // This file provides |assert_selection(sample, tester, expectedText, options)| |
8 // assertion to W3C test harness to write editing test cases easier. | 8 // assertion to W3C test harness to write editing test cases easier. |
9 // | 9 // |
10 // |sample| is an HTML fragment text which is inserted as |innerHTML|. It should | 10 // |sample| is an HTML fragment text which is inserted as |innerHTML|. It should |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 // TODO(yosin): Please use "clang-format -style=Chromium -i" for formatting | 53 // TODO(yosin): Please use "clang-format -style=Chromium -i" for formatting |
54 // this file. | 54 // this file. |
55 | 55 |
56 (function() { | 56 (function() { |
57 /** @enum{string} */ | 57 /** @enum{string} */ |
58 const DumpAs = { | 58 const DumpAs = { |
59 DOM_TREE: 'domtree', | 59 DOM_TREE: 'domtree', |
60 FLAT_TREE: 'flattree', | 60 FLAT_TREE: 'flattree', |
61 }; | 61 }; |
62 | 62 |
| 63 // border-size of IFRAME which hosts sample HTML. This value comes from |
| 64 // "core/css/html.css". |
| 65 const kIFrameBorderSize = 2; |
| 66 |
63 /** @const @type {string} */ | 67 /** @const @type {string} */ |
64 const kTextArea = 'TEXTAREA'; | 68 const kTextArea = 'TEXTAREA'; |
65 | 69 |
66 class Traversal { | 70 class Traversal { |
67 /** | 71 /** |
68 * @param {!Node} node | 72 * @param {!Node} node |
69 * @return {Node} | 73 * @return {Node} |
70 */ | 74 */ |
71 firstChildOf(node) { throw new Error('You should implement firstChildOf'); } | 75 firstChildOf(node) { throw new Error('You should implement firstChildOf'); } |
72 | 76 |
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
657 serializeInternal(node) { | 661 serializeInternal(node) { |
658 if (isElement(node)) | 662 if (isElement(node)) |
659 return this.handleElementNode(node); | 663 return this.handleElementNode(node); |
660 if (isCharacterData(node)) | 664 if (isCharacterData(node)) |
661 return this.handleCharacterData(node); | 665 return this.handleCharacterData(node); |
662 throw new Error(`Unexpected node ${node}`); | 666 throw new Error(`Unexpected node ${node}`); |
663 } | 667 } |
664 } | 668 } |
665 | 669 |
666 /** | 670 /** |
| 671 * @param {!HTMLElement} element |
| 672 * @return {number} |
| 673 */ |
| 674 function computeLeft(element) { |
| 675 let left = kIFrameBorderSize + element.ownerDocument.offsetLeft; |
| 676 for (let runner = element; runner; runner = runner.offsetParent) |
| 677 left += runner.offsetLeft; |
| 678 return left; |
| 679 } |
| 680 |
| 681 /** |
| 682 * @param {!HTMLElement} element |
| 683 * @return {number} |
| 684 */ |
| 685 function computeTop(element) { |
| 686 let top = kIFrameBorderSize + element.ownerDocument.offsetTop; |
| 687 for (let runner = element; runner; runner = runner.offsetParent) |
| 688 top += runner.offsetTop; |
| 689 return top; |
| 690 } |
| 691 |
| 692 /** |
667 * @this {!DOMSelection} | 693 * @this {!DOMSelection} |
668 * @param {string} html | 694 * @param {string} html |
669 * @param {string=} opt_text | 695 * @param {string=} opt_text |
670 */ | 696 */ |
671 function setClipboardData(html, opt_text) { | 697 function setClipboardData(html, opt_text) { |
672 assert_not_equals(window.internals, undefined, | 698 assert_not_equals(window.internals, undefined, |
673 'This test requests clipboard access from JavaScript.'); | 699 'This test requests clipboard access from JavaScript.'); |
674 function computeTextData() { | 700 function computeTextData() { |
675 if (opt_text !== undefined) | 701 if (opt_text !== undefined) |
676 return opt_text; | 702 return opt_text; |
(...skipping 28 matching lines...) Expand all Loading... |
705 | 731 |
706 // Set focus to sample IFRAME to make |eventSender| and | 732 // Set focus to sample IFRAME to make |eventSender| and |
707 // |testRunner.execCommand()| to work on sample rather than main frame. | 733 // |testRunner.execCommand()| to work on sample rather than main frame. |
708 this.iframe_.focus(); | 734 this.iframe_.focus(); |
709 /** @const @type {!Selection} */ | 735 /** @const @type {!Selection} */ |
710 this.selection_ = this.iframe_.contentWindow.getSelection(); | 736 this.selection_ = this.iframe_.contentWindow.getSelection(); |
711 this.selection_.document = this.document_; | 737 this.selection_.document = this.document_; |
712 this.selection_.document.offsetLeft = this.iframe_.offsetLeft; | 738 this.selection_.document.offsetLeft = this.iframe_.offsetLeft; |
713 this.selection_.document.offsetTop = this.iframe_.offsetTop; | 739 this.selection_.document.offsetTop = this.iframe_.offsetTop; |
714 this.selection_.setClipboardData = setClipboardData; | 740 this.selection_.setClipboardData = setClipboardData; |
| 741 this.selection_.computeLeft = computeLeft; |
| 742 this.selection_.computeTop = computeTop; |
715 this.load(sampleText); | 743 this.load(sampleText); |
716 } | 744 } |
717 | 745 |
718 /** @return {!HTMLDocument} */ | 746 /** @return {!HTMLDocument} */ |
719 get document() { return this.document_; } | 747 get document() { return this.document_; } |
720 | 748 |
721 /** @return {!Selection} */ | 749 /** @return {!Selection} */ |
722 get selection() { return this.selection_; } | 750 get selection() { return this.selection_; } |
723 | 751 |
724 /** | 752 /** |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 throw new Error(`${description}\n` + | 958 throw new Error(`${description}\n` + |
931 `\t expected ${expectedText},\n` + | 959 `\t expected ${expectedText},\n` + |
932 `\t but got ${actualText},\n` + | 960 `\t but got ${actualText},\n` + |
933 `\t sameupto ${commonPrefixOf(expectedText, actualText)}`); | 961 `\t sameupto ${commonPrefixOf(expectedText, actualText)}`); |
934 } | 962 } |
935 | 963 |
936 // Export symbols | 964 // Export symbols |
937 window.Sample = Sample; | 965 window.Sample = Sample; |
938 window.assert_selection = assertSelection; | 966 window.assert_selection = assertSelection; |
939 })(); | 967 })(); |
OLD | NEW |