]>
git.proxmox.com Git - rustc.git/blob - src/vendor/pulldown-cmark/src/escape.rs
1 // Copyright 2015 Google Inc. All rights reserved.
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 //! Utility functions for HTML escaping
23 use std
::str::from_utf8
;
25 static HREF_SAFE
: [u8; 128] = [
26 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
29 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
30 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
31 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
32 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
33 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
36 static HEX_CHARS
: &'
static [u8] = b
"0123456789ABCDEF";
38 pub fn escape_href(ob
: &mut String
, s
: &str) {
41 let c
= s
.as_bytes()[i
];
42 if c
>= 0x80 || HREF_SAFE
[c
as usize] == 0 {
43 // character needing escape
45 // write partial substring up to mark
47 ob
.push_str(&s
[mark
..i
]);
54 ob
.push_str("'");
57 let mut buf
= [0u8; 3];
59 buf
[1] = HEX_CHARS
[((c
as usize) >> 4) & 0xF];
60 buf
[2] = HEX_CHARS
[(c
as usize) & 0xF];
61 ob
.push_str(from_utf8(&buf
).unwrap());
64 mark
= i
+ 1; // all escaped characters are ASCII
67 ob
.push_str(&s
[mark
..]);
70 static HTML_ESCAPE_TABLE
: [u8; 256] = [
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 5, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 static HTML_ESCAPES
: [&'
static str; 6] = [
98 pub fn escape_html(ob
: &mut String
, s
: &str, secure
: bool
) {
100 let bytes
= s
.as_bytes();
104 match bytes
[i
..].iter().position(|&c
| HTML_ESCAPE_TABLE
[c
as usize] != 0) {
111 let escape
= HTML_ESCAPE_TABLE
[c
as usize];
112 if escape
!= 0 && (secure
|| c
!= b'
/'
) {
113 ob
.push_str(&s
[mark
..i
]);
114 ob
.push_str(HTML_ESCAPES
[escape
as usize]);
115 mark
= i
+ 1; // all escaped characters are ASCII
119 ob
.push_str(&s
[mark
..]);