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 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
501 */ | 501 */ |
502 serializeInternal(node) { | 502 serializeInternal(node) { |
503 if (isElement(node)) | 503 if (isElement(node)) |
504 return this.handleElementNode(node); | 504 return this.handleElementNode(node); |
505 if (isCharacterData(node)) | 505 if (isCharacterData(node)) |
506 return this.handleCharacterData(node); | 506 return this.handleCharacterData(node); |
507 throw new Error(`Unexpected node ${node}`); | 507 throw new Error(`Unexpected node ${node}`); |
508 } | 508 } |
509 } | 509 } |
510 | 510 |
| 511 /** |
| 512 * @this {!DOMSelection} |
| 513 * @param {string} html |
| 514 * @param {string=} opt_text |
| 515 */ |
| 516 function setClipboardData(html, opt_text) { |
| 517 assert_not_equals(window.internals, undefined, |
| 518 'This test requests clipboard access from JavaScript.'); |
| 519 function computeTextData() { |
| 520 if (opt_text !== undefined) |
| 521 return opt_text; |
| 522 const element = document.createElement('div'); |
| 523 element.innerHTML = html; |
| 524 return element.textContent; |
| 525 } |
| 526 function copyHandler(event) { |
| 527 const clipboardData = event.clipboardData; |
| 528 clipboardData.setData('text/plain', computeTextData()); |
| 529 clipboardData.setData('text/html', html); |
| 530 event.preventDefault(); |
| 531 } |
| 532 document.addEventListener('copy', copyHandler); |
| 533 document.execCommand('copy'); |
| 534 document.removeEventListener('copy', copyHandler); |
| 535 } |
| 536 |
511 class Sample { | 537 class Sample { |
512 /** | 538 /** |
513 * @public | 539 * @public |
514 * @param {string} sampleText | 540 * @param {string} sampleText |
515 */ | 541 */ |
516 constructor(sampleText) { | 542 constructor(sampleText) { |
517 /** @const @type {!HTMLIFame} */ | 543 /** @const @type {!HTMLIFame} */ |
518 this.iframe_ = document.createElement('iframe'); | 544 this.iframe_ = document.createElement('iframe'); |
519 if (!document.body) | 545 if (!document.body) |
520 document.body = document.createElement("body"); | 546 document.body = document.createElement("body"); |
521 document.body.appendChild(this.iframe_); | 547 document.body.appendChild(this.iframe_); |
522 /** @const @type {!HTMLDocument} */ | 548 /** @const @type {!HTMLDocument} */ |
523 this.document_ = this.iframe_.contentDocument; | 549 this.document_ = this.iframe_.contentDocument; |
524 /** @const @type {!Selection} */ | 550 /** @const @type {!Selection} */ |
525 this.selection_ = this.iframe_.contentWindow.getSelection(); | 551 this.selection_ = this.iframe_.contentWindow.getSelection(); |
526 this.selection_.document = this.document_; | 552 this.selection_.document = this.document_; |
527 this.selection_.document.offsetLeft = this.iframe_.offsetLeft; | 553 this.selection_.document.offsetLeft = this.iframe_.offsetLeft; |
528 this.selection_.document.offsetTop = this.iframe_.offsetTop; | 554 this.selection_.document.offsetTop = this.iframe_.offsetTop; |
| 555 this.selection_.setClipboardData = setClipboardData; |
529 | 556 |
530 this.load(sampleText); | 557 this.load(sampleText); |
531 } | 558 } |
532 | 559 |
533 /** @return {!HTMLDocument} */ | 560 /** @return {!HTMLDocument} */ |
534 get document() { return this.document_; } | 561 get document() { return this.document_; } |
535 | 562 |
536 /** @return {!Selection} */ | 563 /** @return {!Selection} */ |
537 get selection() { return this.selection_; } | 564 get selection() { return this.selection_; } |
538 | 565 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 throw new Error(`${description}\n` + | 707 throw new Error(`${description}\n` + |
681 `\t expected ${expectedText},\n` + | 708 `\t expected ${expectedText},\n` + |
682 `\t but got ${actualText},\n` + | 709 `\t but got ${actualText},\n` + |
683 `\t sameupto ${commonPrefixOf(expectedText, actualText)}`); | 710 `\t sameupto ${commonPrefixOf(expectedText, actualText)}`); |
684 } | 711 } |
685 | 712 |
686 // Export symbols | 713 // Export symbols |
687 window.Sample = Sample; | 714 window.Sample = Sample; |
688 window.assert_selection = assertSelection; | 715 window.assert_selection = assertSelection; |
689 })(); | 716 })(); |
OLD | NEW |