Skip to content

Commit

Permalink
Move orphanizeString() into text-utils module
Browse files Browse the repository at this point in the history
Another small step toward the goal of reducing dependency
on `µb`.

Related issue:
- uBlockOrigin/uBlock-issues#1664

text-iterators module has been renamed text-utils to better
reflect its content.
  • Loading branch information
gorhill committed Jul 31, 2021
1 parent 2eb02c5 commit cb72211
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion platform/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import './lib/punycode.js';

import globals from './js/globals.js';
import { FilteringContext } from './js/filtering-context.js';
import { LineIterator } from './js/text-iterators.js';
import { LineIterator } from './js/text-utils.js';
import { StaticFilteringParser } from './js/static-filtering-parser.js';
import { staticNetFilteringEngine } from './js/static-net-filtering.js';

Expand Down
2 changes: 1 addition & 1 deletion platform/nodejs/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import './lib/publicsuffixlist/publicsuffixlist.js';
import globals from './js/globals.js';
import snfe from './js/static-net-filtering.js';
import { FilteringContext } from './js/filtering-context.js';
import { LineIterator } from './js/text-iterators.js';
import { LineIterator } from './js/text-utils.js';
import { StaticFilteringParser } from './js/static-filtering-parser.js';

import {
Expand Down
2 changes: 1 addition & 1 deletion src/js/benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import scriptletFilteringEngine from './scriptlet-filtering.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import { FilteringContext } from './filtering-context.js';
import { LineIterator } from './text-utils.js';
import { sessionFirewall } from './dynamic-net-filtering.js';
import { LineIterator } from './text-iterators.js';

import {
domainFromHostname,
Expand Down
2 changes: 1 addition & 1 deletion src/js/dynamic-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import '../lib/punycode.js';

import globals from './globals.js';
import { LineIterator } from './text-iterators.js';
import { LineIterator } from './text-utils.js';

import {
decomposeHostname,
Expand Down
2 changes: 1 addition & 1 deletion src/js/hnswitches.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import '../lib/punycode.js';

import globals from './globals.js';
import { LineIterator } from './text-iterators.js';
import { LineIterator } from './text-utils.js';
import { decomposeHostname } from './uri-utils.js';

/******************************************************************************/
Expand Down
3 changes: 2 additions & 1 deletion src/js/pagestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import contextMenu from './contextmenu.js';
import logger from './logger.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import { orphanizeString } from './text-utils.js';
import { redirectEngine } from './redirect-engine.js';
import { sessionFirewall } from './dynamic-net-filtering.js';
import { sessionSwitches } from './hnswitches.js';
Expand Down Expand Up @@ -468,7 +469,7 @@ const PageStore = class {
) {
return false;
}
this.title = µb.orphanizeString(details.title.slice(0, 128));
this.title = orphanizeString(details.title.slice(0, 128));
return true;
}

Expand Down
9 changes: 6 additions & 3 deletions src/js/redirect-engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
/******************************************************************************/

import io from './assets.js';
import µb from './background.js';
import { LineIterator } from './text-iterators.js';

import {
LineIterator,
orphanizeString,
} from './text-utils.js';

/******************************************************************************/

Expand Down Expand Up @@ -409,7 +412,7 @@ RedirectEngine.prototype.resourcesFromString = function(text) {
// No more data, add the resource.
const name = this.aliases.get(fields[0]) || fields[0];
const mime = fields[1];
const content = µb.orphanizeString(
const content = orphanizeString(
fields.slice(2).join(encoded ? '' : '\n')
);
this.resources.set(
Expand Down
14 changes: 7 additions & 7 deletions src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import staticFilteringReverseLookup from './reverselookup.js';
import staticNetFilteringEngine from './static-net-filtering.js';
import µb from './background.js';
import { hostnameFromURI } from './uri-utils.js';
import { LineIterator } from './text-iterators.js';
import { permanentFirewall } from './dynamic-net-filtering.js';
import { permanentSwitches } from './hnswitches.js';
import { permanentURLFiltering } from './url-net-filtering.js';
Expand All @@ -50,6 +49,11 @@ import {
CompiledListWriter,
} from './static-filtering-io.js';

import {
LineIterator,
orphanizeString,
} from './text-utils.js';

/******************************************************************************/

µb.getBytesInUse = async function() {
Expand Down Expand Up @@ -898,10 +902,6 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {

// https://github.com/gorhill/uBlock/issues/3406
// Lower minimum update period to 1 day.
// https://bugs.chromium.org/p/v8/issues/detail?id=2869
// orphanizeString is to work around String.slice() potentially causing
// the whole raw filter list to be held in memory just because we cut out
// the title as a substring.

µb.extractFilterListMetadata = function(assetKey, raw) {
const listEntry = this.availableFilterLists[assetKey];
Expand All @@ -914,13 +914,13 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
let matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Title[\t ]*:([^\n]+)/i);
const title = matches && matches[1].trim() || '';
if ( title !== '' && title !== listEntry.title ) {
listEntry.title = this.orphanizeString(title);
listEntry.title = orphanizeString(title);
io.registerAssetSource(assetKey, { title });
}
matches = head.match(/(?:^|\n)(?:!|# )[\t ]*Homepage[\t ]*:[\t ]*(https?:\/\/\S+)\s/i);
const supportURL = matches && matches[1] || '';
if ( supportURL !== '' && supportURL !== listEntry.supportURL ) {
listEntry.supportURL = this.orphanizeString(supportURL);
listEntry.supportURL = orphanizeString(supportURL);
io.registerAssetSource(assetKey, { supportURL });
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/js/text-iterators.js → src/js/text-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@

/******************************************************************************/

// https://bugs.chromium.org/p/v8/issues/detail?id=2869
// orphanizeString is to work around String.slice() potentially causing
// the whole raw filter list to be held in memory just because we cut out
// the title as a substring.

function orphanizeString(s) {
return JSON.parse(JSON.stringify(s));
}

/******************************************************************************/

class LineIterator {
constructor(text, offset) {
this.text = text;
Expand Down Expand Up @@ -89,4 +100,8 @@ class FieldIterator {

/******************************************************************************/

export { LineIterator, FieldIterator };
export {
FieldIterator,
LineIterator,
orphanizeString,
};
2 changes: 1 addition & 1 deletion src/js/url-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/******************************************************************************/

import { LineIterator } from './text-iterators.js';
import { LineIterator } from './text-utils.js';
import { decomposeHostname } from './uri-utils.js';

/*******************************************************************************
Expand Down
8 changes: 0 additions & 8 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,6 @@ import µb from './background.js';

/******************************************************************************/

// TODO: evaluate using TextEncoder/TextDecoder

µb.orphanizeString = function(s) {
return JSON.parse(JSON.stringify(s));
};

/******************************************************************************/

µb.fireDOMEvent = function(name) {
if (
window instanceof Object &&
Expand Down

0 comments on commit cb72211

Please sign in to comment.