Skip to content

code28/HTMLString

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

140 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

HTMLString Swift 4.0 Build Status CocoaPods Carthage compatible Contact : @_alexaubry

HTMLString is a library written in Swift that allows your program to add and remove 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
πŸ“š Documented
πŸ€– Compatible with Objective-C

Supported Platforms

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

Installation

HTMLString version vs Swift version

Below is a table that shows which version of HTMLString you should use for your Swift version.

Swift version HTMLString Version
4.X >= 4.0.0
3.X >= 3.0.0

Swift Package Manager

Add this line to your Package.swift :

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

CocoaPods

Add this line to your Podfile:

pod 'HTMLString', '~> 4.0'

Carthage

Add this line to your Cartfile:

github "alexaurby/HTMLString" ~> 4.0

Manual

Copy the Sources/HTMLString/ directory into your project.

Usage

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

πŸ” Adding HTML Entities (Escape)

When a character is not supported into the specified encoding, the library will replace it with a decimal entity (supported by all browsers supporting HTML 4 and later).

For instance, the & character will be replaced by &.

You can choose between ASCII and Unicode escaping:

  • Use the addingASCIIEntities property to escape for ASCII-encoded content
  • Use the addingUnicodeEntities property to escape for Unicode-compatible content

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

Example

import HTMLString

let emoji = "My favorite emoji is πŸ™ƒ"
let escapedEmoji = emoji.addingASCIIEntities // "My favorite emoji is 🙃"
let noNeedToEscapeThatEmoji = emoji.addingUnicodeEntities // "My favorite emoji is πŸ™ƒ"

let snack = "Fish & Chips"
let escapedSnack = snack.addingASCIIEntities // "Fish & Chips"
let weAlsoNeedToEscapeThisSnack = snack.addingUnicodeEntities // "Fish & Chips"

πŸ“ Removing HTML Entities (Unescape)

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

Example

import HTMLString

let escapedEmoji = "My favorite emoji is 🙃"
let emoji = escapedEmoji.removingHTMLEntities // "My favorite emoji is πŸ™ƒ"

let escapedSnack = "Fish & 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:

  • -[NSString stringByAddingUnicodeEntities]; : Replaces every character incompatible with HTML Unicode encoding by a decimal HTML entitiy.
  • -[NSString stringByAddingASCIIEntities]; : Replaces every character incompatible with HTML ASCII encoding by a decimal HTML entitiy.
  • -[NSString 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 🙃"

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

Unescaping Examples

@import HTMLString;

NSString *escapedEmoji = @"My favorite emoji is 🙃";
NSString *emoji = [escapedEmoji stringByRemovingHTMLEntities]; // "My favorite emoji is πŸ™ƒ"

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

License

This project is licenced under the MIT License.

The MIT License (MIT)

Copyright (c) 2016-2017 Alexis Aubry Radanovic <me at alexaubry dot fr>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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 OR COPYRIGHT HOLDERS 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.

See the LICENSE file.

πŸ’― Acknowledgements

Thanks @google

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

About

Escape and unescape HTML entities in Swift

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Swift 99.3%
  • Other 0.7%