8000 Isolate functions only used by tests by tushuhei · Pull Request #906 · google/budoux · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Isolate functions only used by tests #906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2025
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
4 changes: 3 additions & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
},
"browser": {
"./dist/dom.js": "./dist/dom-browser.js",
"./module/dom.js": "./module/dom-browser.js"
"./module/dom.js": "./module/dom-browser.js",
"./dist/tests/testutils.js": "./dist/tests/testutils-browser.js",
"./module/tests/testutils.js": "./module/tests/testutils-browser.js"
},
"bin": {
"budoux": "./bin/budoux.js"
Expand Down
28 changes: 0 additions & 28 deletions javascript/src/dom-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
* limitations under the License.
*/

/**
* This file is a collection of risky functions that interact with elements.
* BudouX does not apply any HTML sanitization by default, but this is the place
* to install a sanitizer if needed.
*/

/**
* Parses an html string and returns a parsed html document.
* @param html An HTML string.
Expand All @@ -28,25 +22,3 @@
export const parseFromString = (html: string) => {
return new window.DOMParser().parseFromString(html, 'text/html');
};

/**
* Sets an innerHTML on a given Element or ShadowRoot.
* @param element An Element or ShadowRoot.
* @param html An HTML string to set.
*/
export const setInnerHtml = (element: Element | ShadowRoot, html: string) => {
element.innerHTML = html;
};

/**
* Creates an HTML document.
* @returns Document
*/
export const createDocument = () => {
return window.document;
};

/**
* Whether the running environment is a Web browser.
*/
export const isBrowser = true;
30 changes: 1 addition & 29 deletions javascript/src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

/**
* This file is a collection of risky functions that interact with elements.
* BudouX does not apply any HTML sanitization by default, but this is the place
* to install a sanitizer if needed.
*/
import {DOMParser, parseHTML} from 'linkedom';
import {DOMParser} from 'linkedom';

/**
* Parses an html string and returns a parsed html document.
Expand All @@ -32,26 +27,3 @@ export const parseFromString = (html: string) => {
'text/html'
);
};

/**
* Sets an innerHTML on a given Element or ShadowRoot.
* @param element An Element or ShadowRoot.
* @param html An HTML string to set.
*/
export const setInnerHtml = (element: Element | ShadowRoot, html: string) => {
element.innerHTML = html;
};

/**
* Creates an HTML document.
* @returns Document
*/
export const createDocument = () => {
const {document} = parseHTML('<!doctype html><html></html>');
return document;
};

/**
* Whether the running environment is a Web browser.
*/
export const isBrowser = false;
8 changes: 2 additions & 6 deletions javascript/src/tests/test_html_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ import {
NodeOrTextForTesting,
ParagraphForTesting,
} from '../html_processor.js';
import {
parseFromString,
setInnerHtml,
createDocument,
isBrowser,
} from '../dom.js';
import {parseFromString} from '../dom.js';
import {setInnerHtml, createDocument, isBrowser} from './testutils.js';

const parser = loadDefaultJapaneseParser();

Expand Down
37 changes: 37 additions & 0 deletions javascript/src/tests/testutils-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @license
* Copyright 2025 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Sets an innerHTML on a given Element.
* @param element An Element.
* @param html An HTML string to set.
*/
export const setInnerHtml = (element: Element, html: string) => {
element.innerHTML = html;
};

/**
* Creates an HTML document.
* @returns Document
*/
export const createDocument = () => {
return window.document;
};

/**
* Whether the running environment is a Web browser.
*/
export const isBrowser = true;
40 changes: 40 additions & 0 deletions javascript/src/tests/testutils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license
* Copyright 2025 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {parseHTML} from 'linkedom';

/**
* Sets an innerHTML on a given Element.
* @param element An Element.
* @param html An HTML string to set.
*/
export const setInnerHtml = (element: Element, html: string) => {
element.innerHTML = html;
};

/**
* Creates an HTML document.
* @returns Document
*/
export const createDocument = () => {
const {document} = parseHTML('<!doctype html><html></html>');
return document;
};

/**
* Whether the running environment is a Web browser.
*/
export const isBrowser = false;
0