Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
Reactor cleanup (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathankap authored Sep 25, 2024
1 parent eaaa9da commit cb5f88f
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 41 deletions.
4 changes: 2 additions & 2 deletions packages/reactor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export type {
Highlighter,
Modification,
ModificationRequest,
} from "./interfaces";
} from "./interfaces.js";

import Reactor from "./reactor";
import Reactor from "./reactor.js";

export { Reactor };
2 changes: 1 addition & 1 deletion packages/reactor/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { generateRandomString } from "./utils";
import { generateRandomString } from "./utils.js";

export type Modification = {
selector?: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/reactor/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type {
AppliedModifications,
DomJsonExportNode,
ModificationRequest,
} from "./interfaces";
import { generateModifications } from "./modifications";
import { parseRequest } from "./utils";
} from "./interfaces.js";
import { generateModifications } from "./modifications.js";
import { parseRequest } from "./utils.js";

export async function modifyHtml(
htmlString: string,
Expand Down
24 changes: 12 additions & 12 deletions packages/reactor/modifications.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { AdjacentHTMLModification } from "./modifications/adjacentHTML";
import { HighlightModification } from "./modifications/highlight";
import { NoopModification } from "./modifications/noop";
import { RemoveModification } from "./modifications/remove";
import { ReplaceModification } from "./modifications/replace";
import { ReplaceAllModification } from "./modifications/replaceAll";
import { SwapImageModification } from "./modifications/swapImage";
import { ToastModification } from "./modifications/toast";
import { AdjacentHTMLModification } from "./modifications/adjacentHTML.js";
import { HighlightModification } from "./modifications/highlight.js";
import { NoopModification } from "./modifications/noop.js";
import { RemoveModification } from "./modifications/remove.js";
import { ReplaceModification } from "./modifications/replace.js";
import { ReplaceAllModification } from "./modifications/replaceAll.js";
import { SwapImageModification } from "./modifications/swapImage.js";
import { ToastModification } from "./modifications/toast.js";

import type {
AppliableModification,
AppliedModifications,
Highlighter,
Modification,
ModificationRequest,
} from "./interfaces";
} from "./interfaces.js";

export class AppliedModificationsImpl implements AppliedModifications {
modificationRequest: ModificationRequest;
Expand Down Expand Up @@ -70,9 +70,9 @@ export async function generateModifications(
// construct a new NodeListOf<Element> from items found by the xpath
elements = [];
if (!mod.xpath.startsWith("//html")) {
mod.xpath = `//html/${mod.xpath}`;
mod.xpath = `//html${mod.xpath}`;
}
const xpath = document.evaluate(
const xpath = window.document.evaluate(
mod.xpath,
doc,
null,
Expand Down Expand Up @@ -201,7 +201,7 @@ export function matchesSelector(element: Element, mod: Modification): boolean {
if (mod.selector) {
return element.matches(mod.selector);
} else if (mod.xpath) {
const xpathResult = document.evaluate(mod.xpath, document, null, XPathResult.BOOLEAN_TYPE, null);
const xpathResult = window.document.evaluate(mod.xpath, window.document, null, XPathResult.BOOLEAN_TYPE, null);
return xpathResult.booleanValue;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/modifications/adjacentHTML.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppliableModification } from "../interfaces";
import { AppliableModification } from "../interfaces.js";

export class AdjacentHTMLModification extends AppliableModification {
elementId: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/modifications/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppliableModification } from "../interfaces";
import { AppliableModification } from "../interfaces.js";
import * as cssSelector from "css-selector-generator";

export class HighlightModification extends AppliableModification {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/modifications/noop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppliableModification } from "../interfaces";
import { AppliableModification } from "../interfaces.js";

export class NoopModification extends AppliableModification {
action: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/modifications/remove.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppliableModification } from "../interfaces";
import { AppliableModification } from "../interfaces.js";
import * as cssSelector from "css-selector-generator";

export class RemoveModification extends AppliableModification {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/modifications/replace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppliableModification } from "../interfaces";
import { AppliableModification } from "../interfaces.js";
import * as cssSelector from "css-selector-generator";

export class ReplaceModification extends AppliableModification {
Expand Down
6 changes: 3 additions & 3 deletions packages/reactor/modifications/replaceAll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppliableModification } from "../interfaces";
import { AppliableModification } from "../interfaces.js";

export class ReplaceAllModification extends AppliableModification {
element: Element;
Expand Down Expand Up @@ -106,7 +106,7 @@ function walkTree(
const changeNodes: Node[] = [];
const changes: TreeChange[] = [];

const treeWalker = document.createTreeWalker(
const treeWalker = window.document.createTreeWalker(
rootElement,
NodeFilter.SHOW_TEXT,
(node) => {
Expand Down Expand Up @@ -195,7 +195,7 @@ function replaceText(

for (let i = 0; i < split.length; i++) {
if (typeof split[i] !== "undefined") {
const textNode = document.createTextNode(split[i] || "");
const textNode = window.document.createTextNode(split[i] || "");
parentElement.insertBefore(textNode, nextSibling);

if (i % 2 !== 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/modifications/swapImage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppliableModification } from "../interfaces";
import { AppliableModification } from "../interfaces.js";
import * as cssSelector from "css-selector-generator";

export class SwapImageModification extends AppliableModification {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/modifications/toast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AppliableModification } from "../interfaces";
import { AppliableModification } from "../interfaces.js";

export class ToastModification extends AppliableModification {
message: string;
Expand Down
10 changes: 5 additions & 5 deletions packages/reactor/mutationObserver.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Reactor from "./reactor";
import { AppliedModificationsImpl } from "./modifications";
import { applyModification, matchesSelector } from "./modifications";
import Reactor from "./reactor.js";
import { AppliedModificationsImpl } from "./modifications.js";
import { applyModification, matchesSelector } from "./modifications.js";

export class ReactorMutationObserver {
private reactor: Reactor;
Expand Down Expand Up @@ -42,7 +42,7 @@ export class ReactorMutationObserver {
}

walkAddedElements(element: Element) {
const treeWalker = document.createTreeWalker(
const treeWalker = window.document.createTreeWalker(
element,
NodeFilter.SHOW_ELEMENT,
null
Expand All @@ -68,7 +68,7 @@ export class ReactorMutationObserver {
}

walkRemovedElements(element: Element) {
const treeWalker = document.createTreeWalker(
const treeWalker = window.document.createTreeWalker(
element,
NodeFilter.SHOW_ELEMENT,
null
Expand Down
8 changes: 4 additions & 4 deletions packages/reactor/reactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import type {
DomJsonExportNode,
Highlighter,
ModificationRequest,
} from "./interfaces";
import { htmlElementToJson } from "./main";
} from "./interfaces.js";
import { htmlElementToJson } from "./main.js";
import {
AppliedModificationsImpl,
generateModifications,
} from "./modifications";
import { ReactorMutationObserver } from "./mutationObserver";
} from "./modifications.js";
import { ReactorMutationObserver } from "./mutationObserver.js";

/**
* Reactor applied modifications to the current page. Modifications
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/tests/modifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("Utils", () => {

// Vitest beforeEach function for setup
beforeEach(() => {
doc = document.implementation.createHTMLDocument("Test Document");
doc = window.document.implementation.createHTMLDocument("Test Document");
});

describe("applyModification", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/tests/mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("test mutation listeners", {}, () => {

// Vitest beforeEach function for setup
beforeEach(() => {
doc = document.implementation.createHTMLDocument("Test Document");
doc = window.document.implementation.createHTMLDocument("Test Document");

reactor = new Reactor();
reactor.attach(doc, {
Expand Down
4 changes: 3 additions & 1 deletion packages/reactor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"module": "ESNext",
"target": "ESNext",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true
},
"include": ["index.ts", "receivers", "utils", "commands"],
"exclude": ["node_modules", "dist", "test"]
Expand Down
2 changes: 1 addition & 1 deletion packages/reactor/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {
ModificationRequest,
} from "./interfaces";
} from "./interfaces.js";

export function parseRequest(userRequest: string): ModificationRequest {
try {
Expand Down

0 comments on commit cb5f88f

Please sign in to comment.