npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serialize-elem

v2.4.0

Published

Manipulate block elements as if they were text.

Downloads

23

Readme

Serialize

Manipulate block elements as if they were text.

Installation

$ npm install serialize-elem

Motivation

Serialize’s primary use case is in-browser rich text editing. Serialize enables a powerful method of making edits to the DOM by abstracting away its difficulties and normalizing certain behaviours. For example, consider the following fragments of HTML:

<strong><em>Baggins</em></strong>
<em><strong>Baggins</strong></em>
<em><strong>Bagg</strong><strong>ins</strong></em>
<em><strong>Bagg</strong></em><strong><em>ins</em></strong>

Although nothing visually distinguishes these snippets once rendered, your editor needs to know how to recognize and deal with all of the different forms; this is hard to reason about, and prone to error. Serialize eliminates these frustrations by representing block elements as text; inline stylings are stored as markups that can be applied to the text. By merging similar adjacent or overlapping markups, and applying those markups in a consistent manner when converting back to DOM elements, Serialize eliminates these inconsistencies.

If you’d like to gain a better understanding of the motivations behind Serialize, see here.

API

var s = new Serialize( element )

Serializes the element into an object. Example:

<p>Some <b>bold</b> and <em>italic</em> text</p>

gets converted to:

{
    type: 'p'
    text: 'Some bold and italic text',
    length: 25,
    markups: [{
        type: Serialize.types.bold,
        start: 5,
        end: 9
    }, {
        type: Serialize.types.italic,
        start: 14,
        end: 20
    }]
}

Where Serialize.types.* is a number. Currently, only code, link, bold, and italic types are supported. If you need to support a broader range of markups, adding support is trivial.

Note: Serialize use getters and setters to automagically update the length property of a serialization when its text is changed; in most cases, there should be no reason to set the length of a serialization.

Serialize#addMarkup( markup )

Adds the given markup to the serialization. Markups are ordered first by type, then by start index. If identical markups would overlap, they are merged into one. If adding a link, existing links will be truncated or removed so as to avoid overlapping with the link being added.

Returns the context to allow chaining.

Serialize#addMarkups( markups )

As above, but with an array of markups.

Serialize#removeMarkup( markup )

Removes or truncates a serialization’s markups such that no markups of the same type as the given markup overlap the given markup’s range.

Note: for the link type, this method does not check the href.

Serialize#substr( start [, length] )

Works the same as String#substr. Returns a new serialization representing the duplicated substring, complete with the appropriate markups.

Serialize#substring( start [, end] )

Like String#substring. Returns a new serialization.

Serialize#replace( pattern, substr )

Works like String#replace on the serialization’s text, but updates the markups appropriately. Same signature as String#replace. Returns a new serialization.

If a match overlaps a markup, that markup is truncated so as to make it smaller. Consider the following element:

<p>One..<em>. two</em></p>

This gets serialized as:

{
    type: 'p',
    text: 'One... two',
    length: 10,
    markups: [{
        type: Serialize.types.italic,
        start: 5,
        end: 10
    }]
}

Replacing adjacent periods with ellipses by calling

.replace(/\.\.\./, '…')

on this serialization results in the following:

{
    type: 'p',
    text: 'One… two',
    length: 8,
    markups: [{
        type: Serialize.types.italic,
        start: 4,
        end: 8
    }]
}

which, when converted back to en element, will look like:

<p>One…<em> two</em></p>

Serialize#append( other )

Returns a new serialization which results from appending the serialization other to the current serialization. This is similar to how the addition operator works on strings.

If other is a string, the returned serialization will have that string appended to its text, and any markups that previously terminated at the end of the serialization are extended such that they terminate at the end of the new serialization. For example:

<p><em>1</em><strong>2</strong></p>
<!-- Serializing, then calling .append('345'), results in: -->
<p><em>1</em><strong>2345</strong></p>

If you wish to avoid extending markups, simply add your text directly to the serialization’s.

Serialize#equals( other )

Returns a boolean; true if the serializations are equivalent (i.e. they would produce identical elements by calling Serialize#toElement), false otherwise. Continuing the comparison to Strings, Serialize#equals is like the equality operator.

Serialize#toElement( )

Return a new element resembling the one that was serialized. See below for a description of how markups are applied when converting serializations back to elements.

Serialize#toString( )

This overrides the inherited toString to return the outerHTML of the Serialization’s equivalent element. For example:

var p = document.createElement('p')
p.innerHTML = 'A <em>B</em> C'

var s = new Serialize(p)
console.log('' + s)
// "<p>A <em>B</em> C</p>"

Serialize.fromText( text [, tag] )

Creates a serialization with the given String. The serialization will have no markups. The type property of the returned instance can optionally be specified using the tag argument, defaulting to p.

Serialize.fromJSON( json )

If you have previously JSON.stringify’d a serialization, you can convert it to a “live” instance of Serialize by using this method.

Serialize.types

An object with the numeric constants corresponding to the various markup types supported by Serialize. They are:

Serialize.types = {
  link: 1,
  code: 2,
  bold: 3,
  italic: 4
}

Usage of Serialize.types.* in your code is preferred over their numeric equivalents; doing so will not only result in clearer code, but it will make your code future-proof should these numbers ever change.

When converting serializations into DOM elements, markups are applied in order of ascending type; markups with a lower type are prioritized over those with a higher type. For example, consider the following element:

<p><strong><a href="/foo">Some</a></strong><a href="/foo"> text</a></p>

Serializing, then deserializing this element will merge the identical, adjacent links, resulting in:

<p><a href="/foo"><strong>Some</strong> text</a></p>

License

MIT