Skip to content
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
5 changes: 3 additions & 2 deletions Sources/Early/PageToolsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ struct PageFindBar: View {
var body: some View {
let tint = model.chromeTheme.focusBorder(isDark: colorScheme == .dark, increasedContrast: contrast == .increased).color
let status = Self.status(for: state)
let canStep = !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
HStack(spacing: 4) {
Image(systemName: "magnifyingglass")
.font(.system(size: 12, weight: .semibold))
Expand All @@ -638,9 +639,9 @@ struct PageFindBar: View {
.accessibilityIdentifier("find.status")
Divider().frame(height: 16).padding(.horizontal, 2)
control("chevron.up", "Previous Match\(model.preferences.shortcutHint(.findPrevious))", identifier: "find.previous") { step(backwards: true) }
.disabled(state.matchCount == 0)
.disabled(!canStep)
control("chevron.down", "Next Match\(model.preferences.shortcutHint(.findNext))", identifier: "find.next") { step(backwards: false) }
.disabled(state.matchCount == 0)
.disabled(!canStep)
control("xmark", "Close Find (Escape)", identifier: "find.close") { model.closeFind(tabID) }
}
.padding(.horizontal, 8)
Expand Down
9 changes: 9 additions & 0 deletions Sources/Early/TestPages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ final class TestPages: NSObject, WKURLSchemeHandler {
""")
return
}
if url.path == "/find" {
respond(to: task, url: url, html: """
<!doctype html><title>Page Find</title><body style="font:18px -apple-system;padding:32px">
<h1>Page Find</h1>
<button onclick="document.querySelectorAll('p').forEach(p => p.hidden = false)">Reveal matches</button>
<p hidden>Needle in the first paragraph.</p><p hidden>Needle in the second paragraph.</p></body>
""")
return
}
if url.path == "/article" {
// Long enough for Readability's article check; French for translation.
let paragraph = url.query == "lang=fr"
Expand Down
30 changes: 27 additions & 3 deletions Sources/EarlyWeb/MediaActivityObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,36 @@ final class MediaActivityObserver: NSObject, WKScriptMessageHandlerWithReply {
if (e.target instanceof HTMLMediaElement) { track(e.target); update(); }
}, true);
new MutationObserver(records => {
for (const record of records) for (const node of record.addedNodes) scan(node);
const added = new Set();
for (const record of records) for (const node of record.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) added.add(node);
}
// A batch can contain the same node or both a node and its ancestors.
// Cache ancestor lookups so a deep shared path is examined only once.
const covered = new Map();
for (const node of added) {
const path = [];
let parent = node.parentElement;
while (parent && !added.has(parent) && !covered.has(parent)) {
path.push(parent);
parent = parent.parentElement;
}
const hasAddedAncestor = parent !== null && (added.has(parent) || covered.get(parent));
for (const element of path) covered.set(element, hasAddedAncestor);
// Keep this in the mutation checkpoint, including detached players
// that can still be playing. A muted tab cannot wait for a timer.
if (!hasAddedAncestor) scan(node);
}
update();
}).observe(document, {subtree: true, childList: true});
document.addEventListener('DOMContentLoaded', () => { scan(document); update(); }, {once: true});
window.addEventListener('pagehide', () => { hidden = true; send(false, true); });
window.addEventListener('pageshow', () => { hidden = false; last = null; scan(document); update(); });
window.addEventListener('pageshow', () => {
const restored = hidden;
hidden = false;
last = null;
if (restored) scan(document);
update();
});
// Only the native owner in the isolated world can set tab mute state.
globalThis.__earlyMediaControl = {applyState, get documentToken() { return hidden ? null : token; }};
scan(document);
Expand Down
208 changes: 146 additions & 62 deletions Sources/EarlyWeb/PageFind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,40 @@ extension WebViewManager {
'DETAILS', 'DIALOG', 'DIV', 'DL', 'DT', 'FIELDSET', 'FIGCAPTION', 'FIGURE', 'FOOTER', 'FORM', 'H1', 'H2',
'H3', 'H4', 'H5', 'H6', 'HEADER', 'HTML', 'LABEL', 'LEGEND', 'LI', 'MAIN', 'NAV', 'OL', 'P', 'PRE',
'SECTION', 'SUMMARY', 'TABLE', 'TBODY', 'TD', 'TFOOT', 'TH', 'THEAD', 'TR', 'UL']);
let query = '', ranges = [], index = -1, capped = false, dirty = true, observer = null, sheet = null;
// The DOM Standard allows these HTML elements and custom elements to host a shadow root.
const shadowHostNames = new Set(['ARTICLE', 'ASIDE', 'BLOCKQUOTE', 'BODY', 'DIV', 'FOOTER', 'H1', 'H2',
'H3', 'H4', 'H5', 'H6', 'HEADER', 'MAIN', 'NAV', 'P', 'SECTION', 'SPAN']);
let query = '', ranges = [], index = -1, capped = false, dirty = true, sheet = null;
let hiddenRanges = [], hiddenOverflow = false;
let roots = new Set(), potentialHosts = [];
const invalidate = () => { dirty = true; };
const observer = new MutationObserver(invalidate);
const stylesheetLoaded = event => { if (event.target.localName === 'link') invalidate(); };

const observe = nextRoots => {
observer.disconnect();
for (const root of roots) {
if (nextRoots.has(root)) continue;
root.removeEventListener('load', stylesheetLoaded, true);
if (sheet && root.adoptedStyleSheets.includes(sheet)) {
root.adoptedStyleSheets = root.adoptedStyleSheets.filter(candidate => candidate !== sheet);
}
}
for (const root of nextRoots) {
// Any attribute can participate in a CSS selector that changes visibility.
observer.observe(root, { childList: true, characterData: true, attributes: true, subtree: true });
root.addEventListener('load', stylesheetLoaded, true);
}
roots = nextRoots;
if (roots.size) {
window.addEventListener('resize', invalidate);
window.addEventListener('pagehide', leave);
} else {
window.removeEventListener('resize', invalidate);
window.removeEventListener('pagehide', leave);
potentialHosts = [];
}
};

const blockOf = node => {
let element = node.parentElement;
Expand All @@ -142,27 +175,40 @@ extension WebViewManager {
// All visible text as one string, with an unmatchable separator between
// blocks so a match can span inline tags but not two paragraphs.
const collect = () => {
const segments = [];
let text = '', lastBlock;
const visit = root => {
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, {
acceptNode(node) {
if (node.nodeType !== Node.ELEMENT_NODE) return NodeFilter.FILTER_ACCEPT;
if (skipped.has(node.tagName)) return NodeFilter.FILTER_REJECT;
return node.shadowRoot ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
}
});
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
if (node.nodeType === Node.ELEMENT_NODE) { visit(node.shadowRoot); continue; }
if (!node.data) continue;
const block = blockOf(node);
if (text.length && block !== lastBlock) text += '\u0000';
lastBlock = block;
segments.push({ node, start: text.length });
text += node.data;
const segments = [], walkers = [], searchedRoots = new Set([document]);
let text = '', lastBlock, newRoot = false;
potentialHosts = [];
const walk = root => document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_TEXT, {
acceptNode: node => skipped.has(node.tagName) ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT
});
const visitHost = element => {
const root = element.shadowRoot;
if (root) {
walkers.push(walk(root));
searchedRoots.add(root);
newRoot = true;
} else if (element instanceof HTMLElement
&& (shadowHostNames.has(element.tagName) || element.localName.includes('-'))) {
potentialHosts.push(element);
}
};
if (document.body) visit(document.body);
if (document.body) {
walkers.push(walk(document.body));
visitHost(document.body);
}
while (walkers.length) {
const node = walkers[walkers.length - 1].nextNode();
if (!node) { walkers.pop(); newRoot = true; continue; }
if (node.nodeType === Node.ELEMENT_NODE) { visitHost(node); continue; }
if (!node.length) continue;
const block = blockOf(node);
if (text.length && (newRoot || block !== lastBlock)) text += '\u0000';
newRoot = false;
lastBlock = block;
segments.push({ node, start: text.length });
text += node.data;
}
observe(searchedRoots);
return { text, segments };
};
const pattern = value => {
Expand All @@ -180,11 +226,16 @@ extension WebViewManager {
return [segments[low].node, offset - segments[low].start];
};
const visible = range => {
const element = range.startContainer.parentElement;
if (!element) return false;
if (typeof element.checkVisibility === 'function'
&& !element.checkVisibility({ visibilityProperty: true, checkVisibilityCSS: true })) return false;
return range.getClientRects().length > 0;
const start = range.startContainer, end = range.endContainer;
const rendered = node => {
const element = node.parentElement || node.getRootNode().host;
if (!element || !node.isConnected) return false;
if (element.checkVisibility?.({ visibilityProperty: true, checkVisibilityCSS: true }) !== false) return true;
// display:contents has no box of its own but can render text.
const style = getComputedStyle(element);
return style.display === 'contents' && style.visibility === 'visible';
};
return !range.collapsed && rendered(start) && (end === start || rendered(end)) && range.getClientRects().length > 0;
};
const unpaint = () => {
try {
Expand All @@ -195,10 +246,12 @@ extension WebViewManager {
const search = limit => {
unpaint();
ranges = [];
hiddenRanges = [];
hiddenOverflow = false;
capped = false;
dirty = false;
const expression = pattern(query);
if (!expression) return;
if (!expression) { observe(new Set()); return; }
const { text, segments } = collect();
for (let match = expression.exec(text); match; match = expression.exec(text)) {
if (!match[0].length) { expression.lastIndex++; continue; }
Expand All @@ -209,28 +262,29 @@ extension WebViewManager {
range.setStart(startNode, startOffset);
range.setEnd(endNode, endOffset);
} catch (_) { continue; }
if (!visible(range)) continue;
if (!visible(range)) {
if (hiddenRanges.length < limit) hiddenRanges.push(range);
else hiddenOverflow = true;
continue;
}
if (ranges.length >= limit) { capped = true; break; }
ranges.push(range);
}
if (!observer) {
observer = new MutationObserver(() => { dirty = true; });
observer.observe(document.documentElement, { childList: true, characterData: true, subtree: true });
}
};
const firstInView = () => {
const found = ranges.findIndex(range => range.getBoundingClientRect().bottom >= 0);
return found < 0 ? 0 : found;
};
const paint = () => {
if (!ranges.length) return;
try {
if (!sheet) {
sheet = new CSSStyleSheet();
sheet.replaceSync('::highlight(early-find){background-color:rgba(255,212,0,.45);color:inherit}'
+ '::highlight(early-find-current){background-color:#ff9500;color:#000}');
}
if (!document.adoptedStyleSheets.includes(sheet)) {
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet];
for (const root of roots) {
if (!root.adoptedStyleSheets.includes(sheet)) root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
}
CSS.highlights.set('early-find', new Highlight(...ranges));
if (index >= 0) {
Expand All @@ -251,7 +305,7 @@ extension WebViewManager {
};
const reveal = () => {
const range = ranges[index];
const element = range && range.startContainer.parentElement;
const element = range && (range.startContainer.parentElement || range.startContainer.getRootNode().host);
if (!element) return;
const outside = rect => rect.top < 0 || rect.bottom > innerHeight || rect.left < 0 || rect.right > innerWidth;
if (!outside(range.getBoundingClientRect())) return;
Expand All @@ -261,6 +315,60 @@ extension WebViewManager {
};
const answer = () => { paint(); reveal(); return { count: ranges.length, index, capped }; };

// Positions in different trees cannot be compared directly. A shadow
// range sorts at the start of its host when viewed from the outer tree.
function* startPositions(range) {
for (let position = range; position;) {
const root = position.startContainer.getRootNode();
yield [root, position];
if (!root.host?.isConnected) return;
position = document.createRange();
position.setStart(root.host, 0);
}
}
const positionAfterSearch = (previous, ordinal, delta) => {
if (!previous) return -1;
const anchors = new Map(startPositions(previous));
for (const [next, range] of ranges.entries()) {
for (const [root, position] of startPositions(range)) {
const anchor = anchors.get(root);
if (!anchor) continue;
const order = position.compareBoundaryPoints(Range.START_TO_START, anchor);
if (order > 0) return delta > 0 ? next - 1 : next;
if (order === 0) return next;
break;
}
}
const next = previous.startContainer.isConnected ? ranges.length : Math.min(ordinal, ranges.length);
return delta > 0 ? next - 1 : next;
};
const needsSearch = () => dirty || observer.takeRecords().length > 0
// Attaching an open root to an existing host emits no mutation record.
|| potentialHosts.some(host => host.shadowRoot)
// In-place CSSOM changes can affect visibility without touching the DOM.
|| hiddenOverflow || hiddenRanges.some(visible) || ranges.some(range => !visible(range));
const clear = selectCurrent => {
const current = ranges[index];
unpaint();
observe(new Set());
if (selectCurrent && current && visible(current)) {
const text = current.toString();
const match = pattern(query)?.exec(text);
const selection = window.getSelection();
if (selection && match?.index === 0 && match[0].length === text.length) {
selection.removeAllRanges();
selection.addRange(current.cloneRange());
}
}
ranges = [];
hiddenRanges = [];
hiddenOverflow = false;
index = -1;
query = '';
dirty = true;
};
const leave = () => clear(false);

return {
update(value, delta, limit) {
query = value;
Expand All @@ -270,41 +378,17 @@ extension WebViewManager {
},
step(value, delta, limit) {
if (value !== query) { query = value; search(limit); index = -1; }
else if (dirty) {
// Keep the place in a changed page: resume at the old match.
const previous = ranges[index];
else if (needsSearch()) {
const previous = ranges[index], ordinal = index;
search(limit);
index = -1;
if (previous && ranges.length) {
let next = ranges.findIndex(range => {
try { return range.compareBoundaryPoints(Range.START_TO_START, previous) >= 0; }
catch (_) { return false; }
});
if (next < 0) next = ranges.length;
let same = false;
try { same = !!ranges[next] && ranges[next].compareBoundaryPoints(Range.START_TO_START, previous) === 0; }
catch (_) {}
index = same ? next : (delta > 0 ? next - 1 : next);
}
index = positionAfterSearch(previous, ordinal, delta);
}
if (!ranges.length) { index = -1; return answer(); }
if (index < 0 && delta < 0) index = ranges.length;
index = ((index + delta) % ranges.length + ranges.length) % ranges.length;
return answer();
},
clear(selectCurrent) {
const current = ranges[index];
unpaint();
if (observer) { observer.disconnect(); observer = null; }
if (selectCurrent && current) {
const selection = window.getSelection();
if (selection) { selection.removeAllRanges(); selection.addRange(current.cloneRange()); }
}
ranges = [];
index = -1;
query = '';
dirty = true;
}
clear
};
})();
"""#
Expand Down
Loading
Loading