Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape < and > characters #13

Merged
merged 2 commits into from
Apr 11, 2021
Merged

Conversation

daniloc
Copy link
Contributor

@daniloc daniloc commented Mar 18, 2021

Hello,

Thanks for Parma, it's terrific!

I did discover that because XML is used under the hood, content with < and > causes problems with the output, essentially truncating anything before the first <.

This pull adds a function to escape these characters. An enum is provided to allow easy addition of future escaped characters:

enum EscapedCharacters: String, CaseIterable {
            
            case leftAngleBracket = "<",
                 rightAngleBracket = ">"
            
            func replacement() -> String {
                switch self {
                case .leftAngleBracket:
                    return "&lt;"
                case .rightAngleBracket:
                    return "&gt;"
                }
            }
            
            static func escapeString(_ string: String) -> String {
                var escapedValue = string
                
                self.allCases.forEach {
                    escapedValue = escapedValue.replacingOccurrences(of: $0.rawValue, with: $0.replacement())
                }
                
                return escapedValue
            }
        }

I hope you will find this addition useful, and open to any feedback if you'd prefer this to live elsewhere in the project than ParmaCore.

@dasautoooo
Copy link
Owner

Thanks, @daniloc , that's actually helpful!

@dasautoooo dasautoooo changed the base branch from main to escape-character April 11, 2021 07:57
@dasautoooo dasautoooo merged commit fa0e36b into dasautoooo:escape-character Apr 11, 2021
@dasautoooo
Copy link
Owner

Hi @daniloc , I've merged this into a new branch, will make some small tweaks and merge it back to main.
Thank you!

@dasautoooo
Copy link
Owner

The changes are in Parma 0.3.0. Thanks again 🙌.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants