Giter Club home page Giter Club logo

htmlstring's Introduction

HTMLString Swift 3.0.2 Build Status CocoaPods Carthage compatible Code coverage Twitter : @leksantoine

HTMLString is a library written in Swift that enables your app to escape and unescape HTML entities in Strings.

Main features
๐Ÿ” Adds entities for ASCII and UTF-8/UTF-16 encodings
๐Ÿ“ Removes more than 2100 named entities (like &)
๐Ÿ”ข Supports removing decimal and hexadecimal entities
๐Ÿฃ Designed to support Swift Extended Grapheme Clusters (โ†’ 100% emoji-proof)
โœ… Fully unit tested
โšก Fast
๐Ÿ“š 100% documented
๐Ÿค– Compatible with Objective-C

Supported Platforms

  • iOS 8.0+
  • macOS 10.10+
  • tvOS 9.0+
  • watchOS 2.0+
  • Linux

Installation

Swift Package Manager

Add this line to your Package.swift :

.Package(url: "https://github.com/alexaubry/HTMLString", majorVersion: 3, minor: 0)

CocoaPods

Add this line to your Podfile:

pod 'HTMLString'

Carthage

Add this line to your Cartfile:

github "alexaurby/HTMLString"

Manual

Copy the Sources/HTMLString/ directory into your project.

Usage

HTMLString allows you to add and remove HTML entities from a String.

๐Ÿ” Add HTML Entities (Escape)

When a character is not supported into the specified encoding, the library replaces it with a decimal entitiy, such as &#038; <=> & (compatible with HTML 4+).

You can choose between ASCII and Unicode escaping.

๐Ÿ’ก Pro Tip: When your content supports UTF-8 or UTF-16, use Unicode escaping as it is faster and yields a less bloated output.

Docs

Example

import HTMLString

let emoji = "My favorite emoji is ๐Ÿ™ƒ"
let escapedEmoji = emoji.addingASCIIEntities // "My favorite emoji is &#128579;"
let noNeedToEscapeThatEmoji = emoji.addingUnicodeEntities // "My favorite emoji is ๐Ÿ™ƒ"

let snack = "Fish & Chips"
let escapedSnack = snack.addingUnicodeEntities // "Fish &#038; Chips"

๐Ÿ“ Remove HTML Entities (Unescape)

To remove HTML entities from a String, use the removingHTMLEntities property.

Docs

Example

import HTMLString

let escapedEmoji = "My favorite emoji is &#x1F643;"
let emoji = escapedEmoji.removingHTMLEntities // "My favorite emoji is ๐Ÿ™ƒ"

let escapedSnack = "Fish &amp; Chips"
let snack = escapedSnack.removingHTMLEntities // "Fish & Chips"

Objective-C API

With Obj-C Mix and Match, you can import and use the HTMLString module from in Objective-C code.

The library introduces a set of Objective-C specific APIs as categories on the NSString type:

  • [aString stringByAddingUnicodeEntities]; : Replaces every character incompatible with HTML Unicode encoding by a decimal HTML entitiy.
  • [aString stringByAddingASCIIEntities]; : Replaces every character incompatible with HTML ASCII encoding by a decimal HTML entitiy.
  • [aString stringByRemovingHTMLEntities]; : Replaces every HTML entity with the matching Unicode character.

Escaping Examples

@import HTMLString;

NSString *emoji = @"My favorite emoji is ๐Ÿ™ƒ";
NSString *escapedEmoji = [emoji stringByAddingASCIIEntities]; // "My favorite emoji is &#128579;"

NSString *snack = @"Fish & Chips";
NSString *escapedSnack = [snack stringByAddingUnicodeEntities]; // "Fish &#038; Chips"

Unescaping Examples

@import HTMLString;

NSString *escapedEmoji = @"My favorite emoji is &#x1F643;";
NSString *emoji = [escapedEmoji stringByRemovingHTMLEntities]; // "My favorite emoji is ๐Ÿ™ƒ"

NSString *escapedSnack = @"Fish &amp; Chips";
NSString *snack = [escapedSnack stringByRemovingHTMLEntities]; // "Fish & Chips"

๐Ÿ’ฏ Acknowledgements

Thanks @google

This library was inspired by @google's Toolbox for Mac.

htmlstring's People

Contributors

alexisakers avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.