Giter Club home page Giter Club logo

htmlstring's Introduction

HTMLString Swift 5.0 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

This package requires Swift 5 and Xcode 12.

  • iOS 9.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", from: "6.0.0")

CocoaPods

Add this line to your Podfile:

pod 'HTMLString', '~> 6.0'

Carthage

Add this line to your Cartfile:

github "alexaubry/HTMLString" ~> 6.0

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 function to escape for ASCII-encoded content
  • Use the addingUnicodeEntities function 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 function.

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 interoperability, 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"

Author

License

HTMLString is available under the MIT license. See the LICENSE file for more info.

htmlstring's People

Contributors

alexisakers avatar jcsoohwancho avatar tomcheung avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

htmlstring's Issues

Carthage failed with exit code 128

I'm try using carthage, run carthage update command in bash, then error shows:

A shell task (/usr/bin/env git clone --bare --quiet https://github.com/alexaurby/HTMLString.git /Users/virus1993/Library/Caches/org.carthage.CarthageKit/dependencies/HTMLString) failed with exit code 128:
remote: Repository not found.
fatal: repository 'https://github.com/alexaurby/HTMLString.git/' not found

My Carthage file:

github "alexaurby/HTMLString" ~> 4.0

Oooops!

It should be:

github "alexaubry/HTMLString" ~> 4.0

Your should correct README.md. it almost let me give up this nice framework.

Thanks to your framework, save lots of my time to Unescape some horrible text.

Have a nice day!

CocoaPod version 5.0 is not properly tagged

The README.md suggests that "~> 5.0" is the current Pod version but the tag for it does not exist (I didn't check the master repo if that is ok though).

Your readme pod badge also suggests that the latest version is 4.0.2.

removingHTMLEntities will throw fatal error if decode JSONSerialization string

I am using library with Xcode 10.2.1 (Swift 5), and found following code will throw Fatal error inside removeHTMLEntities

let jsonString = "{\"sepcialCharacter\": \"𝟸𝟺𝟢°\"}"
let json = try! JSONSerialization.jsonObject(with: jsonString.data(using: .utf8)!, options: []) as! [String:String]

let sepcialCharacter = json["sepcialCharacter"]!
sepcialCharacter.removingHTMLEntities

After investigation, seem this error may cause by NSString to swift String bridging with following code, not sure this is the bug in swift or the library itself

let nsSepcialCharacter = NSString("𝟸𝟺𝟢°")
let sepcialCharacter = nsSepcialCharacter as String
sepcialCharacter.removingHTMLEntities

Infinite loop in -[NSString stringByRemovingHTMLEntities]

I'm using HTMLStrings 6.0.0. Looks like simply calling -[NSString stringByRemovingHTMLEntities] is causing an infinite loop. See attached.

Screen Shot 2020-09-28 at 12 17 56 PM

Example invocation context:

NSString *const htmlEncodedString = @"Apple's Tim Cook on a "giant leap" in social progress;
NSString *const noEntities = [htmlEncodedString stringByRemovingHTMLEntities]; // Hangs

Should HTMLString handle curly quotes and ellipsis?

I may be using it incorrectly, but shouldn't HTMLString encode β€œβ€ (left and right curly quotes) into β€œ and ” and ellipsis … into …? I do this manually now before calling addingUnicodeEntities. Or do I misunderstand how it should work?

Xcode 10.2

it's really strange but Xcode 10.2 freeze on HTMLString during a creation of the archive.

sporadic crashes on iOS 13 devices when using the removingHTMLEntities property

This issue is iOS 13 specific.

We use the removingHTMLEntities property on long html strings. We see this crash on rare occasions - about 800 cases per 1 million sessions. It also seems that similar calls on the same string would not result in a crash. So we believe this is a threading issue.

Crashlytics data:

crash_info:
Fatal error: file /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Binaries/swiftlang/install/TempContent/Objects/BNI_iOS/swift-macosx-x86_64/stdlib/public/core/8/UnsafeBufferPointer.swift, line 886

Stack trace:

Crashed: com.apple.main-thread
0  libswiftCore.dylib             0x1932103bc closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 456
1  libswiftCore.dylib             0x19320f834 _assertionFailure(_:_:file:line:flags:) + 472
2  libswiftCore.dylib             0x19320fb3c _fatalErrorMessage(_:_:file:line:flags:) + 44
3  libswiftCore.dylib             0x19335aa08 _scalarAlign(_:_:) + 1010
4  libswiftCore.dylib             0x19335a21c _stringCompareInternal(_:_:expecting:) + 612
5  HTMLString                     0x103345f18 $sSS10HTMLStringE18removeHTMLEntitiesyyF + 236
6  HTMLString                     0x103346ebc $sSS10HTMLStringE21addingUnicodeEntitiesSSvgTm + 40

removingHTMLEntities doesn't work if text includes non-encoded & character

I'm using the library to remove HTML entities from Strings on my tvOS project and found out that a string like: Mary & Joe are getting "Married\", due to the & between Mary and Joe words. It returns the String as is without replacing any HTML entities.
If I remove that or even encode it as HTML entities, than calling removingHTMLEntities works as expected.

Broken Dash link

Hi! the Dash link is pointing to dash-feed://https%3A%2F%2Falexaubry%2Egithub%2Eio%2Fdocsets%2FHTMLString%2Exml (ironically this link has many html escaped characters)(https://alexaubry.github.io/docsets/HTMLString.xml) instead of https://alexaubry.github.io/HTMLString/docsets/HTMLString.xml.

And the xml file looks like:

<entry>
    <version>4.0.0</version>
    <url>https://alexaubry.github.io/docsets/HTMLString.tgz</url>
</entry>

instead of <url>https://alexaubry.github.io/HTMLString/docsets/HTMLString.tgz</url>

This can be fixed in a pr, but maybe it's better to fix this in docs generate script thing?

Privacy Manifest

Hi,

I was wondering if there was a plan to do an update to include Privacy Manifest file?

Looking at the latest commits, I wonder if the project is still maintained too 😐

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.