Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
findandreplacedomtext
Advanced tools
findAndReplaceDOMText
searches for regular expression matches in a given DOM node and replaces or wraps each match with a node or piece of text that you can specify.
For example:
<p id="t">
123 456 Hello
</p>
findAndReplaceDOMText(document.getElementById('t'), {
find: /Hello/,
wrap: 'em'
});
This would result in:
<p id="t">
123 456 <em>Hello</em>
</p>
And it also works when matches are spread across multiple nodes! E.g.
<p id="t">
123 456 Hell<span>o Goodbye</span>
</p>
findAndReplaceDOMText(document.getElementById('t'), {
find: /Hello/,
wrap: 'em'
});
This would result in:
<p id="t">
123 456 <em>Hell</em><span><em>o</em> Goodbye</span>
</p>
The EM
element has been added twice, to cover both portions of the match.
Grab the latest findAndReplaceDOMText.js (or npm install findandreplacedomtext
) and include it as a <script>
on your page.
findAndReplaceDOMText
has the following argument signature:
findAndReplaceDOMText(
element, // (Element) The element or text-node to search within
options // (Object) Explained below
);
The options
object includes:
RegExp | String
): Something to search for. A string will perform a global search by default (looking for all matches), but a RegExp will only do so if you include the global (/.../g
) flag.String | Function
): A String of text to replace matches with, or a Function which should return replacement Node or String. If you use a string, it can contain various tokens:$n
to represent the nth captured group of a regular expression (i.e. $1
, $2
, ...)$0
or $&
to represent the entire match$`
to represent everything to the left of the match.$'
to represent everything to the right of the match.String | Node
): A string representing the node-name of an element that will be wrapped around matches (e.g. span
or em
). Or a Node (i.e. a stencil node) that we will clone for each match portion.String
): A string representing the class name to be assigned to the wrapping element (e.g. <span class="myClass">found text</span>
). If the wrap
option is not specified, then this option is ignored.String
, one of "retain"
or "first"
): Indicates whether to re-use existing node boundaries when replacing a match with text (i.e. the default, "retain"
), or whether to instead place the entire replacement in the first-found match portion's node. Most of the time you'll want the default.Function
): A function to be called on every element encountered by findAndReplaceDOMText
. If the function returns false the element will be altogether ignored.Function | Boolean
): A boolean or a boolean-returning function that'll be called on every element to determine if it should be considered as its own matching context. See below under Contexts for more info.String
): Currently there's only one preset: prose
. See below.preset:prose
The most common usage of findAndReplaceDOMText
is to replace text found in regular prose, not all DOM nodes. To make this easier there is a preset that you can use to instruct it to:
<script>
, <svg>
, <optgroup>
, <textarea>
, etc.)<p>
and <div>
so that matches cannot cross element borders.<em>
, <span>
, etc.)To enable this preset:
findAndReplaceDOMText(element, {
preset: 'prose',
find: 'something',
replace: 'something else'
})
A portion or "match portion" is a part of a match that is delimited by node boundaries. Not all matches occur within a single text-node, so findAndReplaceDOMText
has to be able to deal with cross-boundary matches (e.g. when matching /foo/
in "<em>f</em>oo"
).
A portion object has the following properties:
node
: The DOM node pertaining to the portion. Note that this node might not fully encapsulate part of a match, e.g. the node might contain additional text.index
: The index of the portion (0
is the first portion of the match, etc.)text
: The text of the portion relevant to the matchindexInMatch
: The index of the portion within the matchindexInNode
: The index of the portion text within the nodereplace
FunctionIf you pass a function to the replace
option your function will be called on every portion of every match and is expected to return a DOM Node (a Text or Element node). Your function will be passed both the portion and the encapsulating match of that portion.
E.g.
Input HTML
<div id="container">
Explaining how to write a replace <em>fun</em>ction
</div>
JS
findAndReplaceDOMText(document.getElementById('container'), {
find: 'function',
replace: function(portion, match) {
return '[[' + portion.index + ']]';
}
});
Output HTML
<div id="container">
Explaining how to write a replace <em>[[0]]</em>[[1]]
</div>
wrap
OptionIf you pass a string to the wrap
option, every matching text segment will be wrapped in that element. If you also specify the wrapClass
option, the wrapping element will be assigned that class after it is created. This is useful for attaching various styles from your css.
E.g.
Input HTML
<div id="container">
Explaining how to wrap text in elements with and without classes assigned.
</div>
JS
findAndReplaceDOMText(document.getElementById('container'), {
find: 'without',
wrap: 'em'
});
findAndReplaceDOMText(document.getElementById('container'), {
find: 'with ',
wrap: 'em',
wrapClass: 'shiny'
});
CSS
.shiny {
background-color: yellow;
}
Output HTML
<div id="container">
Explaining how to wrap text in elements <em class="shiny">with </em>and <em>without</em> classes assigned.
</div>
Calling findAndReplaceDOMText
returns an instance of an internal Finder constructor -- the API on the object is limited, at the moment, to reverting:
var finder = findAndReplaceDOMText(...);
// Later:
finder.revert();
Note: Reversion will only work if the nodes have not been tampered with after the initial replacement -- if there have been removals, movements or normalisations then the reversion is not guarenteed to work. In this case it's best to retain your own clone of the target node(s) in order to run your own reversion.
Matching, by default, will occur on all elements and across all element borders. E.g.
Before:
<div id="test">
<p>ama</p><p>zing</p>
</div>
findAndReplaceDOMText(document.getElementById('test'), {
find: 'amazing',
wrap: 'em'
});
After:
<div id="test">
<p><em>ama</em></p><p><em>zing</em></p>
</div>
This is a useful feature for inline elements, but is undesirable in many other cases, so to stop it from happening you can choose to "force a context" on those particular elements. In this case we want to force a context on <p>
elements:
findAndReplaceDOMText(document.getElementById('test'), {
find: 'amazing',
wrap: 'em',
forceContext: function(el) {
// Using https://developer.mozilla.org/en-US/docs/Web/API/Element/matches
return el.matches('p');
}
});
Internally, the prose
preset uses this feature:
exposed.PRESETS = {
prose: {
forceContext: exposed.NON_INLINE_PROSE,
filterElements: function(el) {
return !hasOwn.call(exposed.NON_PROSE_ELEMENTS, el.nodeName.toLowerCase());
}
}
};
NON_CONTIGUOUS_PROSE_ELEMENTS
. Expose library via UMD (See #32).preset:prose
and forceContext
features. See #29.findAndReplaceDOMText(node, options)
, plus the ability to replace a match with text or wrap it with a DOM Node.findAndReplaceDOMText()
(see issue #5)This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
FAQs
findAndReplaceDOMText: DOM find/replace utility
We found that findandreplacedomtext demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.