Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve scroll to text WPT coverage. #20105

Merged
merged 1 commit into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions scroll-to-text-fragment/scroll-to-text-fragment-api.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<title>Fragment directive API</title>
<meta charset=utf-8>
<link rel="help" href="https://wicg.github.io/ScrollToTextFragment/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
test(t => {
assert_equals(typeof(window.location.fragmentDirective), 'object', 'window.location.fragmentDirective is defined');
}, 'Scroll to text is feature detectable via window.location.fragmentDirective');

test(t =>{
window.location.fragmentDirective = 'text=test';
assert_equals(window.scrollY, 0, 'Setting window.location.fragmentDirective did not have an effect on scroll position');
assert_equals(typeof(window.location.fragmentDirective), 'object', 'window.location.fragmentDirective is still an object type');
assert_equals(Object.keys(window.location.fragmentDirective).length, 0, 'window.location.fragmentDirective has no properties');
}, 'Setting window.location.fragmentDirective has no effect');
</script>
<style>
body {
height: 3200px;
}
#text {
position: absolute;
top: 3000px;
}
</style>
<body>
<p id="text">This is a test page</p>
</body>
37 changes: 32 additions & 5 deletions scroll-to-text-fragment/scroll-to-text-fragment-target.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
position = 'element';
else if (isInView(document.getElementById('text')))
position = 'text';
else if (isInView(document.getElementById('more-text')))
position = 'more-text';
else if (isInView(document.getElementById('cross-node-context')))
position = 'cross-node-context';
else if (isInView(document.getElementById('text-directive-parameters')))
position = 'text-directive-parameters';

bc.postMessage({ scrollPosition: position, href: window.location.href });
bc.close();
Expand All @@ -24,18 +30,39 @@
</script>
<style>
body {
height: 3200px;
height: 6200px;
}
p {
#element {
position: absolute;
top: 2000px;
}
#text {
position: absolute;
top: 3000px;
}
#element {
#more-text {
position: absolute;
top: 2000px;
top: 4000px;
}
#cross-node-context {
position: absolute;
top: 5000px;
}
#text-directive-parameters {
position: absolute;
top: 6000px;
}
</style>
<body onload="window.requestAnimationFrame(checkScroll)">
<div id="element">Element</div>
<p id="text">This is a test page</p>
<p id="text">This is a test page !$'()*+./:;=?@_~ &,- &#x2665;</p>
<p id="more-text">More test page text</p>
<div id="cross-node-context">
<div>
<p>prefix</p>
<p>test page</p>
</div>
<div><p>suffix</p></div>
</div>
<p id="text-directive-parameters">this,is,test,page</p>
</body>
182 changes: 162 additions & 20 deletions scroll-to-text-fragment/scroll-to-text-fragment.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,168 @@
<script src="/resources/testdriver-vendor.js"></script>
<script>
let test_cases = [
{ fragment: '#', expect_position: 'top' },
{ fragment: '#:~:text=test', expect_position: 'text' },
{ fragment: '#:~:text=this,page', expect_position: 'text' },
{ fragment: '#:~:text=this-,is,test', expect_position: 'text' },
{ fragment: '#:~:text=this-,is,test,-page', expect_position: 'text' },
{ fragment: '#:~:text=this-,is,page,-none', expect_position: 'top' },
{ fragment: '#:~:text=this,test,-page', expect_position: 'text' },
{ fragment: '#:~:text=this%20is%20a%20test%20page', expect_position: 'text' },
{ fragment: '#:~:text=this&text=test,page', expect_position: 'text' },
{ fragment: '#:~:text=tes&text=age', expect_position: 'top' },
{ fragment: '#pagestate:~:text=test', expect_position: 'text' },
{ fragment: '#pagestate:~:text=nomatch', expect_position: 'top' },
{ fragment: '#element:~:text=nomatch', expect_position: 'element' },
{ fragment: '#element:~:directive', expect_position: 'element' },
// Test non-text fragment directives
{
fragment: '#',
expect_position: 'top',
description: 'Empty hash'
},
{
fragment: '#:~:text=this,is,test,page',
expect_position: 'top',
description: 'Text directive with invalid syntax, context terms without "-"'
},
{
fragment: '#element:~:directive',
expect_position: 'element',
description: 'Generic fragment directive with existing element fragment'
},
// Test exact text matching, with all combinations of context terms
{
fragment: '#:~:text=test',
expect_position: 'text',
description: 'Exact text with no context'
},
{
fragment: '#:~:text=this is a-,test',
expect_position: 'text',
description: 'Exact text with prefix'
},
{
fragment: '#:~:text=test,-page',
expect_position: 'text',
description: 'Exact text with suffix'
},
{
fragment: '#:~:text=this is a-,test,-page',
expect_position: 'text',
description: 'Exact text with prefix and suffix'
},
// Test text range matching, with all combinations of context terms
{
fragment: '#:~:text=this,page',
expect_position: 'text',
description: 'Text range with no context'
},
{
fragment: '#:~:text=this-,is,test',
expect_position: 'text',
description: 'Text range with prefix'
},
{
fragment: '#:~:text=this,test,-page',
expect_position: 'text',
description: 'Text range with suffix'
},
{
fragment: '#:~:text=this-,is,test,-page',
expect_position: 'text',
description: 'Text range with prefix and suffix'
},
// Test partially non-matching text ranges
{
fragment: '#:~:text=this,none',
expect_position: 'top',
description: 'Text range with non-matching endText'
},
{
fragment: '#:~:text=none,page',
expect_position: 'top',
description: 'Text range with non-matching startText'
},
// Test non-matching context terms
{
fragment: '#:~:text=this-,is,page,-none',
expect_position: 'top',
description: 'Text range with prefix and nonmatching suffix'
},
{
fragment: '#:~:text=none-,this,test,-page',
expect_position: 'top',
description: 'Text range with nonmatching prefix and matching suffix'
},
// Test percent encoded characters
{
fragment: '#:~:text=this%20is%20a%20test%20page',
expect_position: 'text',
description: 'Exact text with percent encoded spaces'
},
{
fragment: '#:~:text=test%20pag',
expect_position: 'top',
description: 'Non-whole-word exact text with spaces'
},
{
fragment: '#:~:text=%26%2C%2D',
expect_position: 'text',
description: 'Fragment directive with percent encoded syntactical characters "&,-"'
},
{
fragment: '#:~:text=%2665',
expect_position: 'text',
description: 'Fragment directive with percent encoded non-ASCII unicode character'
},
{
fragment: '#:~:text=!$\'()*+./:;=?@_~',
expect_position: 'text',
description: 'Fragment directive with all TextMatchChars'
},
// Test multiple text directives
{
fragment: '#:~:text=this&text=test,page',
expect_position: 'text',
description: 'Multiple matching exact texts'
},
{
fragment: '#:~:text=tes&text=age',
expect_position: 'top',
description: 'Multiple non-whole-word exact texts'
},
// Test text directive behavior when there's an element fragment identifier
{
fragment: '#element:~:text=test',
expect_position: 'text',
description: 'Text directive with existing element fragment'
},
{
fragment: '#pagestate:~:text=test',
expect_position: 'text',
description: 'Text directive with nonexistent element fragment'
},
{
fragment: '#element:~:text=nomatch',
expect_position: 'element',
description: 'Non-matching text directive with existing element fragment'
},
{
fragment: '#pagestate:~:text=nomatch',
expect_position: 'top',
description: 'Non-matching text directive with nonexistent element fragment'
},
// Test ambiguous text matches disambiguated by context terms
{
fragment: '#:~:text=more-,test%20page',
expect_position: 'more-text',
description: 'Multiple match text directive disambiguated by prefix'
},
{
fragment: '#:~:text=test%20page,-text',
expect_position: 'more-text',
description: 'Multiple match text directive disambiguated by suffix'
},
{
fragment: '#:~:text=more-,test%20page,-text',
expect_position: 'more-text',
description: 'Multiple match text directive disambiguated by prefix and suffix'
},
// Test context terms separated by node boundaries
{
fragment: '#:~:text=prefix-,test%20page,-suffix',
expect_position: 'cross-node-context',
description: 'Text directive with context terms separated by node boundaries'
},
];

test(t => {
assert_equals(typeof(window.location.fragmentDirective), 'object', 'window.location.fragmentDirective is defined');
}, 'Scroll to text is feature detectable via window.location.fragmentDirective');

for (const test_case of test_cases) {
promise_test(t => new Promise(resolve => {
let channel = new BroadcastChannel('scroll-to-text-fragment');
Expand All @@ -42,8 +184,8 @@
}).then(data => {
assert_equals(data.href.indexOf(':~:'), -1, 'Expected fragment directive to be stripped from the URL.');
assert_equals(data.scrollPosition, test_case.expect_position,
'Expected ' + test_case.fragment + ' to scroll to ' + test_case.expect_position);
}), 'Test navigation with text fragment directive ' + test_case.fragment);
`Expected ${test_case.fragment} (${test_case.description}) to scroll to ${test_case.expect_position}.`);
}), `Test navigation with fragment: ${test_case.description}`);
}

promise_test(t => new Promise(resolve => {
Expand Down