Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Locate the current position of the caret. A plain JavaScript version of Caret.js.
Get the position/offset of the caret/cursor from a textarea, contentedtiable, or an iframe body.
The reason for the iframe is that WYSIWYG editors are often inside of one.
This can also set the position in an editable field.
This is a port of ichord's Caret.js. The motivation for porting this was to drop the jQuery dependency and drop support for older browsers. This library won't work with any browser that doesn't have support for the Selection API. Check caniuse.com for browser support.
Thanks to ichord for all his hard work.
Simply import position
and/or offset
from cart-pos
.
import { position, offset } from 'caret-pos';
Use one of the functions to get the value.
const input = document.querySelector('.foo');
const pos = position(input); // { left: 15, top: 30, height: 20, pos: 15 }
const off = offset(input); // { left: 15, top: 30, height: 20 }
Pass an integer to set the position in the input.
position(input, 11);
In order to get the correct values for an iframe, we need to pass it in the settings so that it can get a reference to the iframe.
const frame = document.getElementById('iframe');
const body = frame.contentDocument.body;
const pos = position(body, { iframe: frame });
const off = offset(body, { iframe: frame });
You may also need to get the offset of the iframe to position things correctly. For this there is utility that you can use to get the offset.
import { getOffset } from 'caret-pos';
const frameOffset = getOffset(frame);
off.left += frameOffset.left;
off.top += frameOffset.top;
When getting the offset, in certain cases a "shadow caret" is temporarily created and destroyed to facilitate calculations.
If one does not wish to mutate the DOM in this way, one can include the noShadowCaret
option in the offset:
import { offset } from 'caret-pos';
offset(el, { noShadowCaret: true });
Note that doing this might make the offset calculation less accurate in some edge cases.
Passing the customPos
option allows specifying a custom cursor position in the element when getting the offset.
This will not change the position, but calculate the offset from the custom position rather than the current one.
This works for both contentEditable and textarea.
import { offset } from 'caret-pos';
offset(el, { customPos: 2 });
Version 2.0.0 - 2020-06-02
caret-pos
to caretPos
FAQs
Locate the current position of the caret. A plain JavaScript version of Caret.js.
The npm package caret-pos receives a total of 3,959 weekly downloads. As such, caret-pos popularity was classified as popular.
We found that caret-pos 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.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.