This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a bunch of Rollbar bugs in preparation for MOC-19 (#57)
* run harlight tests on ci * add dodom package with initial implementation * add dodom tests to ci * add types to dodom in order for it to build correctly * fix https://app.rollbar.com/a/mocksi/fix/item/mocksi-lite/36 * lint
- Loading branch information
Showing
20 changed files
with
434 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
name: Run Vitest | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
build_extension: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v3 | ||
- name: Setup node | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 18 | ||
- uses: pnpm/action-setup@v4 | ||
name: Install pnpm | ||
with: | ||
run_install: false | ||
- name: Get pnpm store directory | ||
shell: bash | ||
run: | | ||
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV | ||
- uses: actions/cache@v4 | ||
name: Setup pnpm cache | ||
with: | ||
path: ${{ env.STORE_PATH }} | ||
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | ||
restore-keys: | | ||
${{ runner.os }}-pnpm-store- | ||
- name: Install dependencies | ||
run: pnpm install --no-frozen-lockfile | ||
- name: Run harlight tests | ||
run: pnpm --prefix packages/harlight exec vitest run | ||
- name: Run dodom tests | ||
run: pnpm --prefix packages/dodom exec vitest run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# DoDomCommander | ||
|
||
DoDomCommander is a TypeScript package for command pattern-based DOM manipulation and network mock management. It supports shadow DOM operations, snapshotting, undo/redo functionality, and network mocking. | ||
|
||
## Features | ||
|
||
- Find nodes by selector | ||
- Clone nodes with auto-generated UUIDs | ||
- Replace text content | ||
- Replace images with data URIs | ||
- Wrap text with overlays | ||
- Manage network mocks | ||
- Snapshot the DOM state | ||
- Undo/Redo operations | ||
- Revert to specific snapshots | ||
|
||
## Usage | ||
|
||
### Replacing an Image with a Data URI | ||
|
||
```typescript | ||
import { ShadowDOMManipulator } from './receivers/ShadowDOMManipulator'; | ||
import { ReplaceImageCommand } from './commands/ReplaceImageCommand'; | ||
|
||
// Example of using the system | ||
const shadowRoot = document.querySelector('#my-shadow-root')?.shadowRoot as ShadowRoot; | ||
const shadowDOMManipulator = new ShadowDOMManipulator(shadowRoot); | ||
|
||
console.log('Before replacement:', shadowRoot.innerHTML); | ||
// Output: <img src="https://example.com/old.jpg" alt="Old Image 1"> | ||
|
||
|
||
shadowDOMManipulator.replaceImage('https://example.com/old.jpg', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...='); | ||
|
||
console.log('After replacement:', shadowRoot.innerHTML); | ||
// <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...=" alt="Old Image 1" data-mocksi-id="mocksi-1245"> | ||
|
||
console.log(shadowDOMManipulator.modifiedNodes); | ||
// Output: ["mocksi-1245"] | ||
|
||
shadowDOMManipulator.undo(); | ||
|
||
console.log('After undo:', shadowRoot.innerHTML); | ||
// Output: <img src="https://example.com/old.jpg" alt="Old Image 1"> | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Command { | ||
execute(): void; | ||
undo(): void; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Command } from './Command'; | ||
import { ShadowDOMManipulator } from '../receivers/ShadowDOMManipulator'; | ||
|
||
export class ReplaceImageCommand implements Command { | ||
private manipulator: ShadowDOMManipulator; | ||
private oldSrc: string; | ||
private newSrc: string; | ||
private nodeId: string | null; | ||
|
||
constructor(manipulator: ShadowDOMManipulator, oldSrc: string, newSrc: string) { | ||
this.manipulator = manipulator; | ||
this.oldSrc = oldSrc; | ||
this.newSrc = newSrc; | ||
this.nodeId = null; | ||
} | ||
|
||
execute(): void { | ||
this.nodeId = this.manipulator.replaceImage(this.oldSrc, this.newSrc); | ||
} | ||
|
||
undo(): void { | ||
if (this.nodeId) { | ||
this.manipulator.replaceImage(this.newSrc, this.oldSrc, this.nodeId); | ||
} | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "@repo/dodom", | ||
"version": "1.0.0", | ||
"description": "A command pattern-based DOM manipulation and network mock manager package.", | ||
"private": true, | ||
"main": "src/index.ts", | ||
"scripts": { | ||
"build": "tsc", | ||
"lint": "eslint . --max-warnings 0", | ||
"test": "pnpm exec vitest" | ||
}, | ||
"devDependencies": { | ||
"@next/eslint-plugin-next": "^14.0.4", | ||
"@repo/eslint-config": "workspace:*", | ||
"@repo/typescript-config": "workspace:*", | ||
"@types/eslint": "^8.56.1", | ||
"@types/jsdom": "^21.1.7", | ||
"@types/node": "^20.10.6", | ||
"@types/uuid": "^9.0.8", | ||
"eslint": "^8.56.0", | ||
"jsdom": "^24.1.0", | ||
"typescript": "^5.3.3", | ||
"vitest": "^1.6.0" | ||
}, | ||
"dependencies": { | ||
"uuid": "^9.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { UUIDGenerator } from '../utils/UUIDGenerator'; | ||
|
||
export class ShadowDOMManipulator { | ||
private shadowRoot: ShadowRoot; | ||
private snapshots: string[] = []; | ||
private modifiedNodes: Map<string, HTMLElement> = new Map(); | ||
private uuidGenerator: UUIDGenerator; | ||
|
||
constructor(shadowRoot: ShadowRoot, uuidGenerator?: UUIDGenerator) { | ||
this.shadowRoot = shadowRoot; | ||
this.uuidGenerator = uuidGenerator || new UUIDGenerator(); | ||
} | ||
|
||
replaceImage(oldSrc: string, newSrc: string, nodeId?: string): string { | ||
const img = this.shadowRoot.querySelector(`img[src="${oldSrc}"]`) as HTMLImageElement; | ||
if (img) { | ||
if (!nodeId) { | ||
nodeId = this.uuidGenerator.generate(); | ||
img.setAttribute('data-mocksi-id', nodeId); | ||
} else { | ||
img.setAttribute('data-mocksi-id', nodeId); | ||
} | ||
this.snapshots.push(this.shadowRoot.innerHTML); | ||
img.src = newSrc; | ||
this.modifiedNodes.set(nodeId, img); | ||
return nodeId; | ||
} | ||
throw new Error('Image with the specified source not found.'); | ||
} | ||
|
||
undo(): void { | ||
if (this.snapshots.length > 0) { | ||
const lastSnapshot = this.snapshots.pop()!; | ||
this.shadowRoot.innerHTML = lastSnapshot; | ||
} | ||
} | ||
|
||
getModifiedNodes(): string[] { | ||
return Array.from(this.modifiedNodes.keys()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { ShadowDOMManipulator } from '../../receivers/ShadowDOMManipulator'; | ||
import { ReplaceImageCommand } from '../../commands/ReplaceImageCommand'; | ||
import { UUIDGenerator } from '../../utils/UUIDGenerator'; | ||
|
||
describe('ReplaceImageCommand', () => { | ||
it('should replace image source and undo the replacement', () => { | ||
const shadowHost = document.createElement('div'); | ||
shadowHost.id = 'my-shadow-root'; | ||
document.body.appendChild(shadowHost); | ||
|
||
const shadowRoot = shadowHost.attachShadow({ mode: 'open' }); | ||
shadowRoot.innerHTML = '<img src="https://example.com/old.jpg" alt="Old Image 1">'; | ||
|
||
const mockUUIDGenerator = { | ||
generate: () => 'mocksi-1234' | ||
} as UUIDGenerator; | ||
|
||
const manipulator = new ShadowDOMManipulator(shadowRoot, mockUUIDGenerator); | ||
const command = new ReplaceImageCommand(manipulator, 'https://example.com/old.jpg', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...='); | ||
|
||
command.execute(); | ||
expect(shadowRoot.innerHTML).toContain('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...='); | ||
expect(shadowRoot.innerHTML).toContain('mocksi-1234'); | ||
|
||
command.undo(); | ||
expect(shadowRoot.innerHTML).toContain('https://example.com/old.jpg'); | ||
}); | ||
}); |
Oops, something went wrong.