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
7 changes: 4 additions & 3 deletions ios/SourceBraidShare/SharePreprocessing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var ExtensionPreprocessingJS = function() {};

ExtensionPreprocessingJS.prototype = {
// Safari calls `run` on this global object before presenting the Share
// Extension. It must be an object, rather than a constructor whose `run`
// method only exists on its prototype.
var ExtensionPreprocessingJS = {
run: function(arguments) {
var selection = window.getSelection ? window.getSelection().toString() : "";
var article = document.querySelector("article") || document.querySelector("main") || document.body;
Expand Down
58 changes: 58 additions & 0 deletions tests/test_ios_share_preprocessing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import json
import subprocess
import unittest
from pathlib import Path


REPOSITORY_ROOT = Path(__file__).resolve().parents[1]
SCRIPT = REPOSITORY_ROOT / "ios" / "SourceBraidShare" / "SharePreprocessing.js"


class SafariSharePreprocessingTests(unittest.TestCase):
def test_exposes_a_global_object_that_returns_page_details(self):
program = r"""
const fs = require("fs");
const vm = require("vm");
const source = fs.readFileSync(process.argv[1], "utf8");
const sandbox = {
window: {
getSelection: () => ({ toString: () => "Selected excerpt" }),
location: { href: "https://example.com/article" }
},
document: {
title: "Example article",
location: { href: "https://example.com/article" },
querySelector: (selector) => selector === "article"
? { innerText: "Readable page text" }
: null
}
};
vm.createContext(sandbox);
vm.runInContext(source, sandbox);
if (typeof sandbox.ExtensionPreprocessingJS?.run !== "function") {
throw new Error("ExtensionPreprocessingJS must expose run()");
}
sandbox.ExtensionPreprocessingJS.run({
completionFunction: (value) => process.stdout.write(JSON.stringify(value))
});
"""
result = subprocess.run(
["node", "-e", program, str(SCRIPT)],
check=True,
capture_output=True,
text=True,
)

self.assertEqual(
json.loads(result.stdout),
{
"url": "https://example.com/article",
"title": "Example article",
"selectedText": "Selected excerpt",
"articleText": "Readable page text",
},
)


if __name__ == "__main__":
unittest.main()