Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Object model for manipulating natural language in JavaScript.
npm:
$ npm install textom
Component:
$ component install wooorm/textom
Bower:
$ bower install textom
var TextOMConstructor = require('textom');
/**
* Construct a new ``document''.
*/
var TextOM = new TextOMConstructor();
/**
* Construct a new root node.
*/
var root = new TextOM.RootNode();
See below for IDL definitions.
Let’s say all following examples start with below code.
Any changes made by below examples are discarded upon their ending.
var TextOMConstructor = require('textom');
/* Construct a new ``document''. */
var TextOM = new TextOMConstructor();
/* Construct a root node. */
var root = new TextOM.RootNode();
/* Add a paragraph node. */
var paragraph = new TextOM.ParagraphNode();
root.append(paragraph);
/* Add a sentence node. */
var sentence = new TextOM.SentenceNode();
paragraph.append(sentence);
/* Add words, symbols, punctuation, and white space. */
var dogs = sentence.append(new TextOM.WordNode()),
space0 = sentence.append(new TextOM.WhiteSpaceNode()),
ampersand = sentence.append(new TextOM.SymbolNode()),
space1 = sentence.append(new TextOM.WhiteSpaceNode()),
cats = sentence.append(new TextOM.WordNode()),
fullStop = sentence.append(new TextOM.PunctuationNode());
/* Add content. */
var dogsText = dogs.append(new TextOM.TextNode('Dogs')),
space0Text = space0.append(new TextOM.TextNode(' ')),
ampersandText = ampersand.append(new TextOM.TextNode('&')),
space1Text = space1.append(new TextOM.TextNode(' ')),
catsText = cats.append(new TextOM.TextNode('cats')),
fullStopText = fullStop.append(new TextOM.TextNode('.'));
/* Check root's content. */
root.toString(); // 'Dogs & cats.'
Object.
Constructor.
TextOM.RootNode.on('someeventname', function () {});
Subscribe listener
to name
events on instances of Node
.
TextOM.WordNode.off('someeventname');
off(name, listener)
: Unsubscribe listener
from name
events on instances of Node
;off(name)
: Unsubscribe from name
events on instances of Node
;off()
: Unsubscribe from events on instances of Node
.root.on('someeventname', function () {});
Subscribe listener
to name
events on node
.
dogs.off('someeventname');
off(name, listener)
: Unsubscribe listener
from name
events on node
;off(name)
: Unsubscribe from name
events on node
;off()
: Unsubscribe from events on node
.TextOM.WordNode.on('someeventname', function () {
this; // dogs
});
dogs.emit('someeventname');
emit(name, parameters...)
: Fire a name
event with parameters
on node
;emit(name)
: Fire a name
event on node
.Bubbles through node
s constructors. In the case of dogs
: WordNode
, Element
, Child
, Parent
, Node
.
root.on('someeventnameinside', function () {
this; // dogs
});
dogsText.trigger('someeventname', dogs);
trigger(name, context, parameters...)
: Fire a name
event with parameters
on node
;trigger(name, context)
: Fire a name
event on node
;trigger(name)
: Same as TextOM\.Node#emit(name)
.emit
s an event, and triggers name + "inside"
events on context and its parents, and their constructor.
In the case of dogsText
: someeventname
is emitted on dogsText
and TextNode
, and someeventname
is triggered on dogs
and WordNode
; sentence
and SentenceNode
; paragraph
and ParagraphNode
; root
and RootNode
.
Identifier for Nodes.
root.TextOM === TextOM; // true
TextOM object associated with node
.
Identifier for RootNodes.
Identifier for ParagraphNodes.
Identifier for SentenceNodes.
Identifier for WordNodes.
Identifier for SymbolNodes.
Identifier for PunctuationNodes.
Identifier for WhiteSpaceNodes.
Identifier for SourceNodes.
Identifier for TextNodes.
Identifier for Nodes.
Identifier for Parents.
Identifier for Elements.
Identifier for Childs.
Identifier for Texts.
Constructor (Node).
Identifier for Parents.
paragraph.head; // sentence
sentence.head; // dogs
First Child
of parent
or null
.
paragraph.tail; // null (see description below);
sentence.tail; // fullStop
Last Child
of parent
(if more than one child exists) or null
.
root.length; // 1
sentence.length; // 6
Number of children in parent
.
sentence.head; // dogs
sentence.prepend(fullStop);
sentence.head; // fullStop
Insert child
as parent
s first child.
sentence.tail; // fullStop
sentence.append(dogs);
sentence.tail; // dogs
Insert child
as parent
s last child.
root.item(); // paragraph
sentence.item(0); // dogs
sentence.item(6); // fullStop
sentence.item(7); // null
root.toString(); // "Dogs & cats."
'' + sentence; // "Dogs & cats."
Get parent
s content.
dogs.valueOf();
/**
* {
* "type": "WordNode",
* "children": [
* {
* "type": "TextNode",
* "value": "Dogs"
* }
* ]
* }
*/
Get parent
s NLCST representation.
Constructor (Node).
Identifier for Childs.
dogs.parent; // sentence
sentence.parent; // paragraph
paragraph.parent; // root
child
s Parent
or null
.
dogs.prev; // null
space1.prev; // dogs
child
s preceding sibling (Child
) or null
.
cats.next; // fullStop
fullStop.next; // null
child
s following sibling (Child
) or null
.
dogs.prev; // null
dogs.before(cats);
dogs.prev; // cats
Insert sibling
(Child
) as child
s preceding sibling in parent
.
cats.next; // null
cats.before(dogs);
cats.next; // dogs
Insert sibling
(Child
) as child
s following sibling in parent
.
root.toString(); // "Dogs & cats."
fullStop.remove();
root.toString(); // "Dogs & cats"
Remove child
from parent
.
root.toString(); // "Dogs & cats."
cats.replace(dogs);
root.toString(); // " & Dogs"
Replace child
with sibling
(Child
) in parent
.
Constructor (Parent and Child
).
Identifier for Elements.
sentence.prev; // null
sentence.toString(); // "Dogs & cats."
sentence.split(2);
sentence.toString(); // "& cats"
sentence.prev.toString(); // "Dogs "
Split element
in two.
split(position)
: A new node, prependee
(a new instance of element
s constructor), is inserted before element
in parent
. prependee
receives the children from 0 to position
(not including). element
receives the children from position
(including);split()
: A new node, prependee
(a new instance of element
s constructor), is inserted before element
in parent
.Constructor (Child).
Identifier for Texts.
dogsText.toString(); // "Dogs"
space1Text.toString(); // " "
fullStopText.toString(); // "."
Get text
s value.
dogsText.valueOf();
/**
* {
* "type": "TextNode",
* "value": "Dogs"
* }
*/
Get text
s NLCST representation.
root.toString(); // "Dogs & cats."
catsText.fromString();
root.toString(); // "Dogs & ."
catsText.fromString("Lions");
root.toString(); // "Dogs & Lions."
fromString(value)
: Set text
s value to value
;fromString()
: Remove text
s value.catsText.prev; // null
catsText.toString(); // "cats"
catsText.split(2);
catsText.toString(); // "ts"
catsText.prev.toString(); // "ca"
Split text
in two.
split(position)
: A new node, prependee
(a new instance of text
s constructor), is inserted before text
in parent
. prependee
receives the value from 0 to position
(not including). text
receives the value from position
(including);split()
: A new node, prependee
(a new instance of text
s constructor), is inserted before text
in parent
.Constructor (Parent).
Identifier for RootNodes.
Constructor (Element).
Identifier for ParagraphNodes.
Constructor (Element).
Identifier for SentenceNodes.
Constructor (Element).
Identifier for WordNodes.
Constructor (Element).
Identifier for SymbolNodes.
Constructor (SymbolNode).
Identifier for PunctuationNodes.
Constructor (SymbolNode).
Identifier for WhiteSpaceNodes.
Constructor (Text).
Identifier for SourceNodes.
Constructor (Text).
Identifier for TextNodes.
The below IDL-like document gives a short view of the defined interfaces by TextOM.
module textom
{
[Constructor]
interface Node {
const string nodeName = "Node"
const string NODE = "Node"
const string PARENT = "Parent"
const string ELEMENT = "Element"
const string CHILD = "Child"
const string TEXT = "Text"
const string ROOT_NODE = "RootNode"
const string PARAGRAPH_NODE = "ParagraphNode"
const string SENTENCE_NODE = "SentenceNode"
const string WORD_NODE = "WordNode"
const string SYMBOL_NODE = "SymbolNode"
const string PUNCTUATION_NODE = "PunctuationNode"
const string WHITE_SPACE_NODE = "WhiteSpaceNode"
const string SOURCE_NODE = "SourceNode"
const string TEXT_NODE = "TextNode"
void on(String type, Function callback);
void off(optional String type = null, optional Function callback = null);
};
[Constructor,
ArrayClass]
interface Parent {
readonly attribute string nodeName = "Parent";
getter Child? item(unsigned long index);
readonly attribute unsigned long length;
readonly attribute Child? head;
readonly attribute Child? tail;
Child prepend(Child child);
Child append(Child child);
[NewObject] Object valueOf();
string toString();
};
Parent implements Node;
[Constructor]
interface Child {
readonly attribute nodeName = "Child"
readonly attribute Parent? parent;
readonly attribute Child? prev;
readonly attribute Child? next;
Child before(Child child);
Child after(Child child);
Child replace(Child child);
Child remove(Child child);
};
Child implements Node;
[Constructor]
interface Element {
readonly attribute nodeName = "Element"
[NewObject] Element split(unsigned long position);
};
Element implements Child;
Element implements Parent;
[Constructor(optional String value = "")]
interface Text {
readonly attribute nodeName = "Text"
[NewObject] Object valueOf();
string toString();
string fromString(String value);
[NewObject] Text split(unsigned long position);
};
Text implements Child;
[Constructor]
interface RootNode {
readonly attribute string type = "RootNode";
};
RootNode implements Parent;
[Constructor]
interface ParagraphNode {
readonly attribute string type = "ParagraphNode";
};
ParagraphNode implements Element;
[Constructor]
interface SentenceNode {
readonly attribute string type = "SentenceNode";
};
SentenceNode implements Element;
[Constructor]
interface WordNode {
readonly attribute string type = "WordNode";
};
WordNode implements Element;
[Constructor]
interface SymbolNode {
readonly attribute string type = "SymbolNode";
};
SymbolNode implements Element;
[Constructor]
interface PunctuationNode {
readonly attribute string type = "PunctuationNode";
};
PunctuationNode implements SymbolNode;
[Constructor]
interface WhiteSpaceNode {
readonly attribute string type = "WhiteSpaceNode";
};
WhiteSpaceNode implements SymbolNode;
[Constructor(optional String value = "")]
interface TextNode {
readonly attribute string type = "TextNode";
};
[Constructor(optional String value = "")]
interface SourceNode {
readonly attribute string type = "SourceNode";
};
SourceNode implements Text;
}
TextOM provides events which can be subscribed to, to get notified when something changes.
Event can be subscribed to through on()
methods, and unsubscribed to through off()
methods. These methods exist on every instance and on every constructor.
When subscribing to an instance's events, listener
is invoked for changes to that specific instance. When subscribing to a constructor's events, listener
is invoked for changes to any of constructor's instances.
dogs.on('remove', function (previous) {
this === dogs; // true
previous === sentence; // true
});
dogs.remove();
Fires when a Child
is removed from previousParent
.
dogs.on('insert', function () {
this === dogs; // true
});
sentence.append(dogs);
Fires when a Child
is inserted into a Parent
.
Child
.dogsText.on('changetext', function (current, previous) {
this === dogsText; // true
current === 'Poodles'; // true
previous === 'Dogs'; // true
});
dogsText.fromString('Poodles');
Fires when a Text
changes value.
Text
;cats.on('changeprev', function (current, previous) {
this === cats; // true
current === ampersand; // true
previousNode === space1; // true
});
space1.remove();
Fires when a preceding sibling (Child
) changes.
Child
following the changed sibling;cats.on('changenext', function (current, previous) {
this === cats; // true
current === null; // true
previousNode === fullStop; // true
});
fullStop.remove();
Fires when a following sibling (Child
) changes.
Child
preceding the changed sibling;root.on('changetextinside', function (node, current, previous) {
this === root; // true
node === catsText; // true
current === 'lions'; // true
previous === 'cats'; // true
});
catsText.fromString('lions');
Fires when a Text
inside an ancestor.
Text
;Text
;sentence.on('insertinside', function (node) {
this === sentence; // true
node === ampersand; // true
});
sentence.append(ampersand);
Fires when a Child
is inserted inside an ancestor.
root.on('removeinside', function (node, previous) {
this === root; // true
node === dogs; // true
previous === sentence; // true;
});
dogs.remove();
Fires when a Child
is removed inside an ancestor.
TextOM provides two types of events: Bubbling and non-bubbling. In API terms, bubbling event names end with "inside"
.
Normal events fire on instances of Child and do not fire on ancestors. They additionally fire on all constructors of the instance.
Let’s say we have the example code given in API, and add the following line to it:
dogsText.fromString('Poodles');
A "changetext"
event fires on dogsText
. Because dogsText
is a TextNode
, the event fires on TextNode
too. Because TextNode
inherits from Text
, the event also fires on Text
, continuing with Child
, and finally Node
.
Bubbling events start on a Parent
and continue through its ancestors. These events also fire on the ancestors constructor.
Let’s say we have the example code given in API, and add the following line to it:
dogsText.fromString('Wolves');
A "changetextinside"
event fires on dogsText
parent, dogs
, and because dogs
is a WordNode
, the event fires on WordNode
too, continuing with sentence
and SentenceNode
, paragraph
and ParagraphNode
, and finally root
and RootNode
.
MIT © Titus Wormer
FAQs
Deprecated. I don’t believe in DOM-like models anymore. [**retext**](https://github.com/wooorm/retext) lives on without them.
The npm package textom receives a total of 23 weekly downloads. As such, textom popularity was classified as not popular.
We found that textom 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.