| 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)| assertion | 7 // This file provides |assert_selection(sample, tester, expectedText)| assertion |
| 8 // to W3C test harness to write editing test cases easier. | 8 // 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 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 } | 455 } |
| 456 this.serializeChildren(element); | 456 this.serializeChildren(element); |
| 457 this.emit(`</${tagName}>`); | 457 this.emit(`</${tagName}>`); |
| 458 } | 458 } |
| 459 | 459 |
| 460 /** | 460 /** |
| 461 * @public | 461 * @public |
| 462 * @param {!HTMLDocument} document | 462 * @param {!HTMLDocument} document |
| 463 */ | 463 */ |
| 464 serialize(document) { | 464 serialize(document) { |
| 465 if (this.selection_.isNone) | |
| 466 return document.body.firstChild.outerHTML; | |
| 467 this.serializeChildren(document.body); | 465 this.serializeChildren(document.body); |
| 468 return this.strings_.join(''); | 466 return this.strings_.join(''); |
| 469 } | 467 } |
| 470 | 468 |
| 471 /** | 469 /** |
| 472 * @private | 470 * @private |
| 473 * @param {!HTMLElement} element | 471 * @param {!HTMLElement} element |
| 474 */ | 472 */ |
| 475 serializeChildren(element) { | 473 serializeChildren(element) { |
| 476 /** @type {!Array<!Node>} */ | 474 /** @type {!Array<!Node>} */ |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 /** @return {!Selection} */ | 525 /** @return {!Selection} */ |
| 528 get selection() { return this.selection_; } | 526 get selection() { return this.selection_; } |
| 529 | 527 |
| 530 /** | 528 /** |
| 531 * @private | 529 * @private |
| 532 * @param {string} sampleText | 530 * @param {string} sampleText |
| 533 */ | 531 */ |
| 534 load(sampleText) { | 532 load(sampleText) { |
| 535 const anchorMarker = sampleText.indexOf('^'); | 533 const anchorMarker = sampleText.indexOf('^'); |
| 536 const focusMarker = sampleText.indexOf('|'); | 534 const focusMarker = sampleText.indexOf('|'); |
| 537 if (focusMarker < 0) { | 535 if (focusMarker < 0 && anchorMarker >= 0) { |
| 538 throw new Error(`You should specify caret position in "${sampleText}".`); | 536 throw new Error(`You should specify caret position in "${sampleText}".`); |
| 539 } | 537 } |
| 540 if (focusMarker != sampleText.lastIndexOf('|')) { | 538 if (focusMarker != sampleText.lastIndexOf('|')) { |
| 541 throw new Error( | 539 throw new Error( |
| 542 `You should have at least one focus marker "|" in "${sampleText}".`); | 540 `You should have at least one focus marker "|" in "${sampleText}".`); |
| 543 } | 541 } |
| 544 if (anchorMarker != sampleText.lastIndexOf('^')) { | 542 if (anchorMarker != sampleText.lastIndexOf('^')) { |
| 545 throw new Error( | 543 throw new Error( |
| 546 `You should have at most one anchor marker "^" in "${sampleText}".`); | 544 `You should have at most one anchor marker "^" in "${sampleText}".`); |
| 547 } | 545 } |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 // case. | 650 // case. |
| 653 if (actualText === expectedText) | 651 if (actualText === expectedText) |
| 654 sample.remove(); | 652 sample.remove(); |
| 655 assert_equals(actualText, expectedText, description); | 653 assert_equals(actualText, expectedText, description); |
| 656 } | 654 } |
| 657 | 655 |
| 658 // Export symbols | 656 // Export symbols |
| 659 window.Sample = Sample; | 657 window.Sample = Sample; |
| 660 window.assert_selection = assertSelection; | 658 window.assert_selection = assertSelection; |
| 661 })(); | 659 })(); |
| OLD | NEW |