Commit 7fc711d9 authored by Hayato Ito's avatar Hayato Ito Committed by Commit Bot

Apply clang-format to *.js files in LayoutTests/shadow-dom

Format js files by "clang-format -i <file>".

TBR=kochi@chromium.org

Change-Id: I28f5a0885688928dcb8b40e8f0abcedfdc6a30c5
Reviewed-on: https://chromium-review.googlesource.com/748921Reviewed-by: default avatarHayato Ito <hayato@chromium.org>
Commit-Queue: Hayato Ito <hayato@chromium.org>
Cr-Commit-Position: refs/heads/master@{#513090}
parent ced5430e
'use strict'; 'use strict';
function innermostActiveElement(element) function innermostActiveElement(element) {
{ element = element || document.activeElement;
element = element || document.activeElement; if (isIFrameElement(element)) {
if (isIFrameElement(element)) { if (element.contentDocument.activeElement)
if (element.contentDocument.activeElement) return innermostActiveElement(element.contentDocument.activeElement);
return innermostActiveElement(element.contentDocument.activeElement);
return element;
}
if (isShadowHost(element)) {
let shadowRoot = window.internals.oldestShadowRoot(element);
while (shadowRoot) {
if (shadowRoot.activeElement)
return innermostActiveElement(shadowRoot.activeElement);
shadowRoot = window.internals.youngerShadowRoot(shadowRoot);
}
}
return element; return element;
}
if (isShadowHost(element)) {
let shadowRoot = window.internals.oldestShadowRoot(element);
while (shadowRoot) {
if (shadowRoot.activeElement)
return innermostActiveElement(shadowRoot.activeElement);
shadowRoot = window.internals.youngerShadowRoot(shadowRoot);
}
}
return element;
} }
function isInnermostActiveElement(path) function isInnermostActiveElement(path) {
{ const element = getNodeInComposedTree(path);
const element = getNodeInComposedTree(path); if (!element)
if (!element) return false;
return false; return element === innermostActiveElement();
return element === innermostActiveElement();
} }
function shouldNavigateFocus(from, to, direction) function shouldNavigateFocus(from, to, direction) {
{ const fromElement = getNodeInComposedTree(from);
const fromElement = getNodeInComposedTree(from); if (!fromElement)
if (!fromElement) return false;
return false;
fromElement.focus(); fromElement.focus();
if (!isInnermostActiveElement(from)) if (!isInnermostActiveElement(from))
return false; return false;
if (direction == 'forward') if (direction == 'forward')
navigateFocusForward(); navigateFocusForward();
else else
navigateFocusBackward(); navigateFocusBackward();
return true; return true;
} }
function navigateFocusForward() function navigateFocusForward() {
{ if (window.eventSender)
if (window.eventSender) eventSender.keyDown('\t');
eventSender.keyDown('\t');
} }
function navigateFocusBackward() function navigateFocusBackward() {
{ if (window.eventSender)
if (window.eventSender) eventSender.keyDown('\t', ['shiftKey']);
eventSender.keyDown('\t', ['shiftKey']);
} }
function assert_focus_navigation(from, to, direction) function assert_focus_navigation(from, to, direction) {
{ const result = shouldNavigateFocus(from, to, direction);
const result = shouldNavigateFocus(from, to, direction); assert_true(result, 'Failed to focus ' + from);
assert_true(result, 'Failed to focus ' + from); const message =
const message = 'Focus should move ' + direction + 'Focus should move ' + direction + ' from ' + from + ' to ' + to;
' from ' + from + ' to ' + to; var toElement = getNodeInComposedTree(to);
var toElement = getNodeInComposedTree(to); assert_equals(innermostActiveElement(), toElement, message);
assert_equals(innermostActiveElement(), toElement, message);
} }
function assert_focus_navigation_forward(elements) function assert_focus_navigation_forward(elements) {
{ assert_true(
assert_true(elements.length >= 2, elements.length >= 2,
'length of elements should be greater than or equal to 2.'); 'length of elements should be greater than or equal to 2.');
for (var i = 0; i + 1 < elements.length; ++i) for (var i = 0; i + 1 < elements.length; ++i)
assert_focus_navigation(elements[i], elements[i + 1], 'forward'); assert_focus_navigation(elements[i], elements[i + 1], 'forward');
} }
function assert_focus_navigation_backward(elements) function assert_focus_navigation_backward(elements) {
{ assert_true(
assert_true(elements.length >= 2, elements.length >= 2,
'length of elements should be greater than or equal to 2.'); 'length of elements should be greater than or equal to 2.');
for (var i = 0; i + 1 < elements.length; ++i) for (var i = 0; i + 1 < elements.length; ++i)
assert_focus_navigation(elements[i], elements[i + 1], 'backward'); assert_focus_navigation(elements[i], elements[i + 1], 'backward');
} }
function removeWhiteSpaceOnlyTextNodes(node) function removeWhiteSpaceOnlyTextNodes(node) {
{
for (var i = 0; i < node.childNodes.length; i++) { for (var i = 0; i < node.childNodes.length; i++) {
var child = node.childNodes[i]; var child = node.childNodes[i];
if (child.nodeType === Node.TEXT_NODE && child.nodeValue.trim().length == 0) { if (child.nodeType === Node.TEXT_NODE &&
child.nodeValue.trim().length == 0) {
node.removeChild(child); node.removeChild(child);
i--; i--;
} else if (child.nodeType === Node.ELEMENT_NODE || child.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { } else if (
child.nodeType === Node.ELEMENT_NODE ||
child.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
removeWhiteSpaceOnlyTextNodes(child); removeWhiteSpaceOnlyTextNodes(child);
} }
} }
...@@ -15,60 +17,56 @@ function removeWhiteSpaceOnlyTextNodes(node) ...@@ -15,60 +17,56 @@ function removeWhiteSpaceOnlyTextNodes(node)
} }
function convertTemplatesToShadowRootsWithin(node) { function convertTemplatesToShadowRootsWithin(node) {
var nodes = node.querySelectorAll("template"); var nodes = node.querySelectorAll('template');
for (var i = 0; i < nodes.length; ++i) { for (var i = 0; i < nodes.length; ++i) {
var template = nodes[i]; var template = nodes[i];
var mode = template.getAttribute("data-mode"); var mode = template.getAttribute('data-mode');
var delegatesFocus = template.hasAttribute("data-delegatesFocus"); var delegatesFocus = template.hasAttribute('data-delegatesFocus');
var parent = template.parentNode; var parent = template.parentNode;
parent.removeChild(template); parent.removeChild(template);
var shadowRoot; var shadowRoot;
if (!mode || mode == 'v0'){ if (!mode || mode == 'v0') {
shadowRoot = parent.createShadowRoot(); shadowRoot = parent.createShadowRoot();
} else { } else {
shadowRoot = parent.attachShadow({'mode': mode, shadowRoot =
'delegatesFocus': delegatesFocus}); parent.attachShadow({'mode': mode, 'delegatesFocus': delegatesFocus});
}
var expose = template.getAttribute("data-expose-as");
if (expose)
window[expose] = shadowRoot;
if (template.id)
shadowRoot.id = template.id;
var fragments = document.importNode(template.content, true);
shadowRoot.appendChild(fragments);
convertTemplatesToShadowRootsWithin(shadowRoot);
} }
var expose = template.getAttribute('data-expose-as');
if (expose)
window[expose] = shadowRoot;
if (template.id)
shadowRoot.id = template.id;
var fragments = document.importNode(template.content, true);
shadowRoot.appendChild(fragments);
convertTemplatesToShadowRootsWithin(shadowRoot);
}
} }
function isShadowHost(node) function isShadowHost(node) {
{ return node && node.nodeType == Node.ELEMENT_NODE && node.shadowRoot;
return node && node.nodeType == Node.ELEMENT_NODE && node.shadowRoot;
} }
function isIFrameElement(element) function isIFrameElement(element) {
{ return element && element.nodeName == 'IFRAME';
return element && element.nodeName == 'IFRAME';
} }
// Returns node from shadow/iframe tree "path". // Returns node from shadow/iframe tree "path".
function getNodeInComposedTree(path) function getNodeInComposedTree(path) {
{ var ids = path.split('/');
var ids = path.split('/'); var node = document.getElementById(ids[0]);
var node = document.getElementById(ids[0]); for (var i = 1; node != null && i < ids.length; ++i) {
for (var i = 1; node != null && i < ids.length; ++i) { if (isIFrameElement(node))
if (isIFrameElement(node)) node = node.contentDocument.getElementById(ids[i]);
node = node.contentDocument.getElementById(ids[i]); else if (isShadowHost(node))
else if (isShadowHost(node)) node = node.shadowRoot.getElementById(ids[i]);
node = node.shadowRoot.getElementById(ids[i]); else
else return null;
return null; }
} return node;
return node;
} }
function createTestTree(node) { function createTestTree(node) {
let ids = {}; let ids = {};
function attachShadowFromTemplate(template) { function attachShadowFromTemplate(template) {
...@@ -79,7 +77,8 @@ function createTestTree(node) { ...@@ -79,7 +77,8 @@ function createTestTree(node) {
// For legacy Shadow DOM // For legacy Shadow DOM
shadowRoot = parent.createShadowRoot(); shadowRoot = parent.createShadowRoot();
} else { } else {
shadowRoot = parent.attachShadow({mode: template.getAttribute('data-mode')}); shadowRoot =
parent.attachShadow({mode: template.getAttribute('data-mode')});
} }
let id = template.id; let id = template.id;
if (id) { if (id) {
...@@ -107,7 +106,6 @@ function createTestTree(node) { ...@@ -107,7 +106,6 @@ function createTestTree(node) {
} }
function dispatchEventWithLog(nodes, target, event) { function dispatchEventWithLog(nodes, target, event) {
function labelFor(e) { function labelFor(e) {
return e.id || e.tagName; return e.id || e.tagName;
} }
...@@ -125,12 +123,13 @@ function dispatchEventWithLog(nodes, target, event) { ...@@ -125,12 +123,13 @@ function dispatchEventWithLog(nodes, target, event) {
attachedNodes.push(node); attachedNodes.push(node);
node.addEventListener(event.type, (e) => { node.addEventListener(event.type, (e) => {
// Record [currentTarget, target, relatedTarget, composedPath()] // Record [currentTarget, target, relatedTarget, composedPath()]
log.push([id, log.push([
labelFor(e.target), id, labelFor(e.target),
e.relatedTarget ? labelFor(e.relatedTarget) : null, e.relatedTarget ? labelFor(e.relatedTarget) : null,
e.composedPath().map((n) => { e.composedPath().map((n) => {
return labelFor(n); return labelFor(n);
})]); })
]);
}); });
} }
} }
...@@ -140,7 +139,9 @@ function dispatchEventWithLog(nodes, target, event) { ...@@ -140,7 +139,9 @@ function dispatchEventWithLog(nodes, target, event) {
function debugEventLog(log) { function debugEventLog(log) {
for (let i = 0; i < log.length; i++) { for (let i = 0; i < log.length; i++) {
console.log('[' + i + '] currentTarget: ' + log[i][0] + ' target: ' + log[i][1] + ' relatedTarget: ' + log[i][2] + ' composedPath(): ' + log[i][3]); console.log(
'[' + i + '] currentTarget: ' + log[i][0] + ' target: ' + log[i][1] +
' relatedTarget: ' + log[i][2] + ' composedPath(): ' + log[i][3]);
} }
} }
...@@ -154,15 +155,22 @@ function debugCreateTestTree(nodes) { ...@@ -154,15 +155,22 @@ function debugCreateTestTree(nodes) {
function assert_event_path_equals(actual, expected) { function assert_event_path_equals(actual, expected) {
assert_equals(actual.length, expected.length); assert_equals(actual.length, expected.length);
for (let i = 0; i < actual.length; ++i) { for (let i = 0; i < actual.length; ++i) {
assert_equals(actual[i][0], expected[i][0], 'currentTarget at ' + i + ' should be same'); assert_equals(
assert_equals(actual[i][1], expected[i][1], 'target at ' + i + ' should be same'); actual[i][0], expected[i][0],
assert_equals(actual[i][2], expected[i][2], 'relatedTarget at ' + i + ' should be same'); 'currentTarget at ' + i + ' should be same');
assert_array_equals(actual[i][3], expected[i][3], 'composedPath at ' + i + ' should be same'); assert_equals(
actual[i][1], expected[i][1], 'target at ' + i + ' should be same');
assert_equals(
actual[i][2], expected[i][2],
'relatedTarget at ' + i + ' should be same');
assert_array_equals(
actual[i][3], expected[i][3],
'composedPath at ' + i + ' should be same');
} }
} }
function assert_background_color(path, color) function assert_background_color(path, color) {
{ assert_equals(
assert_equals(window.getComputedStyle(getNodeInComposedTree(path)).backgroundColor, color, window.getComputedStyle(getNodeInComposedTree(path)).backgroundColor,
'backgroundColor for ' + path + ' should be ' + color); color, 'backgroundColor for ' + path + ' should be ' + color);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment