Skip to content

The headless browser for AI agents and web scraping

Return rendered text from innerText

SGavrl55e76f0cee75
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rs
index beeda896..420afc9f 100644
--- a/crates/obscura-browser/src/page.rs
+++ b/crates/obscura-browser/src/page.rs
@@ -8372,6 +8372,50 @@ mod tests {
page
}
+ #[cfg(feature = "render")]
+ #[test]
+ fn inner_text_matches_rendered_text_semantics() {
+ let mut page = page_with_transport_and_body(
+ "inner-text",
+ "http://example.test/",
+ r#"<div id="visible">A<span style="display:none">B</span><span hidden>C</span><span style="visibility:hidden">H<span style="visibility:visible">D</span></span><script>window.__not_visible = true</script><style>.unused{}</style><p>E</p><br>F</div><div id="blocks">A<div>B</div>C</div><div id="paragraph">A<p>B</p></div><div id="br">A<br>B</div>"#,
+ );
+ let actual = page.evaluate(
+ r#"(() => {
+ const detached = document.createElement('div');
+ detached.textContent = 'Q';
+ const script = document.createElement('script');
+ script.textContent = 'R';
+ detached.append(script);
+ const hidden = document.createElement('span');
+ hidden.hidden = true;
+ hidden.textContent = 'S';
+ detached.append(hidden);
+
+ const hiddenRoot = document.createElement('div');
+ hiddenRoot.style.display = 'none';
+ hiddenRoot.textContent = 'T';
+ const hiddenScript = document.createElement('script');
+ hiddenScript.textContent = 'U';
+ hiddenRoot.append(hiddenScript);
+ document.body.append(hiddenRoot);
+
+ return [
+ visible.innerText,
+ blocks.innerText,
+ paragraph.innerText,
+ br.innerText,
+ detached.innerText,
+ hiddenRoot.innerText,
+ ];
+ })()"#,
+ );
+ assert_eq!(
+ actual,
+ serde_json::json!(["AD\n\nE\n\n\nF", "A\nB\nC", "A\n\nB", "A\nB", "QRS", "TU"]),
+ );
+ }
+
#[cfg(feature = "render")]
#[tokio::test(flavor = "current_thread")]
async fn renderer_misses_cover_script_fonts_and_shadow_root_styles() {
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index fca7b9f2..b3465948 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -3461,6 +3461,62 @@ function _animationsForTarget(target) {
});
}
+const _innerTextBlockDisplays = new Set([
+ 'block', 'flow-root', 'flex', 'grid', 'list-item', 'table', 'table-caption',
+]);
+
+function _collectInnerText(node, items, inheritedVisibility = 'visible') {
+ if (node.nodeType === 3) {
+ if (inheritedVisibility === 'visible') items.push(node.data ?? '');
+ return;
+ }
+ if (node.nodeType !== 1) return;
+
+ const style = getComputedStyle(node);
+ const display = style.display;
+ if (display === 'none') return;
+ const visibility = style.visibility || inheritedVisibility;
+ if (visibility === 'visible' && node.tagName === 'BR') {
+ items.push('\n');
+ return;
+ }
+ const breaks = visibility === 'visible'
+ ? (node.tagName === 'P' ? 2 : (_innerTextBlockDisplays.has(display) ? 1 : 0))
+ : 0;
+ if (breaks) items.push(breaks);
+ for (const child of node.childNodes) {
+ _collectInnerText(child, items, visibility);
+ }
+ if (visibility !== 'visible') return;
+ if (display === 'table-cell') items.push('\t');
+ if (display === 'table-row') items.push('\n');
+ if (breaks) items.push(breaks);
+}
+
+function _renderedInnerText(element) {
+ const items = [];
+ for (const child of element.childNodes) {
+ _collectInnerText(child, items);
+ }
+ let result = '';
+ let pendingBreaks = 0;
+ for (const item of items) {
+ if (typeof item === 'number') {
+ pendingBreaks = Math.max(pendingBreaks, item);
+ continue;
+ }
+ const text = item === '\n' || item === '\t'
+ ? item
+ : String(item).replace(/[\t\n\f\r ]+/g, ' ');
+ if (!text || (pendingBreaks && text === ' ')) continue;
+ if (pendingBreaks && result) result += '\n'.repeat(pendingBreaks);
+ pendingBreaks = 0;
+ result += text;
+ }
+ return result.replace(/ +/g, ' ').replace(/ *\n */g, '\n')
+ .replace(/^[ \n\t]+|[ \n\t]+$/g, '');
+}
+
class Element extends Node {
constructor(nid) {
const entry = _customElementConstructionStack[_customElementConstructionStack.length - 1];
@@ -3569,7 +3625,12 @@ class Element extends Node {
}
}
get outerHTML() { return _domParse("outer_html", this._nid) ?? ""; }
- get innerText() { return this.textContent; }
+ get innerText() {
+ if (!this.isConnected
+ || typeof __obscuraCore.ops.op_computed_style !== 'function'
+ || getComputedStyle(this).display === 'none') return this.textContent;
+ return _renderedInnerText(this);
+ }
set innerText(v) { this.textContent = v; }
get children() {
const ids = _domParse("element_children", this._nid) || [];