-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4993 from camptocamp/svg-url
Allows to have SVG URL
- Loading branch information
Showing
19 changed files
with
724 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
/contribs/gmf/build/ | ||
/contribs/gmf/examples/https.js | ||
/openlayers_src/ | ||
/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Initially get from https://github.com/tildeio/simple-html-tokenizer/blob/v0.1.1/lib/simple-html-tokenizer/generator.js | ||
|
||
const escape = (function() { | ||
const test = /[&<>"'`]/; | ||
const replace = /[&<>"'`]/g; | ||
const map = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
'\'': ''', | ||
'`': '`' | ||
}; | ||
function escapeChar(char) { | ||
return map[char]; | ||
} | ||
return function escape(string) { | ||
if (!test.test(string)) { | ||
return string; | ||
} | ||
return string.replace(replace, escapeChar); | ||
}; | ||
}()); | ||
|
||
function Generator() { | ||
this.escape = escape; | ||
} | ||
|
||
Generator.prototype = { | ||
generate: function(tokens) { | ||
let buffer = ''; | ||
for (let i = 0; i < tokens.length; i++) { | ||
const token = tokens[i]; | ||
buffer += this[token.type](token); | ||
} | ||
return buffer; | ||
}, | ||
|
||
escape: function(text) { | ||
const unsafeCharsMap = this.unsafeCharsMap; | ||
return text.replace(this.unsafeChars, function(char) { | ||
return unsafeCharsMap[char] || char; | ||
}); | ||
}, | ||
|
||
StartTag: function(token) { | ||
let out = '<'; | ||
out += token.tagName; | ||
|
||
if (token.attributes.length) { | ||
out += ' ' + this.Attributes(token.attributes); | ||
} | ||
|
||
out += token.selfClosing ? '/>' : '>'; | ||
|
||
return out; | ||
}, | ||
|
||
EndTag: function(token) { | ||
return '</' + token.tagName + '>'; | ||
}, | ||
|
||
Chars: function(token) { | ||
return this.escape(token.chars); | ||
}, | ||
|
||
Comment: function(token) { | ||
return '<!--' + token.chars + '-->'; | ||
}, | ||
|
||
Attributes: function(attributes) { | ||
const out = []; | ||
|
||
for (let i = 0, l = attributes.length; i < l; i++) { | ||
const attribute = attributes[i]; | ||
|
||
out.push(this.Attribute(attribute[0], attribute[1])); | ||
} | ||
|
||
return out.join(' '); | ||
}, | ||
|
||
Attribute: function(name, value) { | ||
let attrString = name; | ||
|
||
if (value) { | ||
value = this.escape(value); | ||
attrString += '="' + value + '"'; | ||
} | ||
|
||
return attrString; | ||
} | ||
}; | ||
|
||
module.exports = function(tokens) { | ||
const generator = new Generator(); | ||
return generator.generate(tokens); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,79 @@ | ||
const simpleHTMLTokenizer = require('simple-html-tokenizer'); | ||
const generate = require('./generate-xml-from-tokens.js'); | ||
|
||
module.exports = function(source) { | ||
this.cacheable(true); | ||
let tokens = simpleHTMLTokenizer.tokenize(source); | ||
|
||
tokens = tokens.map((tag) => { | ||
let width = undefined; | ||
let height = undefined; | ||
if (tag.type === 'StartTag' && tag.tagName === 'svg') { | ||
let width = undefined; | ||
let height = undefined; | ||
let x = 0; | ||
let y = 0; | ||
for (const attribute of tag.attributes) { | ||
if (attribute[0] === 'width') { | ||
width = parseFloat(attribute[1]); | ||
try { | ||
width = parseFloat(attribute[1]); | ||
} catch (e) { | ||
console.warn('Unable to read width: ' + attribute[1]); | ||
} | ||
} | ||
if (attribute[0] === 'height') { | ||
height = parseFloat(attribute[1]); | ||
try { | ||
height = parseFloat(attribute[1]); | ||
} catch (e) { | ||
console.warn('Unable to read height: ' + attribute[1]); | ||
} | ||
} | ||
if (attribute[0] === 'x') { | ||
try { | ||
x = parseFloat(attribute[1]); | ||
} catch (e) { | ||
console.warn('Unable to read x: ' + attribute[1]); | ||
} | ||
} | ||
if (attribute[0] === 'y') { | ||
try { | ||
y = parseFloat(attribute[1]); | ||
} catch (e) { | ||
console.warn('Unable to read y: ' + attribute[1]); | ||
} | ||
} | ||
if (attribute[0] === 'viewBox') { | ||
try { | ||
const attrs = attribute[1].split(' '); | ||
x = parseFloat(attrs[0]); | ||
y = parseFloat(attrs[1]); | ||
width = parseFloat(attrs[2]); | ||
height = parseFloat(attrs[3]); | ||
} catch (e) { | ||
console.warn('Unable to read viewbox: ' + attribute[1]); | ||
} | ||
} | ||
} | ||
if (width !== undefined && height != undefined) { | ||
tag.attributes = tag.attributes.filter((attribute) => { | ||
return attribute[0] !== 'width' && attribute[0] != 'height' | ||
}) | ||
tag.attributes.push(['viewBox', `0 0 ${width} ${height}`, true]); | ||
tag.attributes.push(['height', '1em', true]); | ||
tag.attributes.push(['width', `${width / height}em`, true]); | ||
return attribute[0] !== 'width' && attribute[0] != 'height' && attribute[0] != 'viewBox'; | ||
}); | ||
if (x) { | ||
tag.attributes.push(['x', x, true]); | ||
} | ||
if (y) { | ||
tag.attributes.push(['y', y, true]); | ||
} | ||
if (this.resourceQuery.search(/inline/) >= 0) { | ||
tag.attributes.push(['width', width, true]); | ||
tag.attributes.push(['height', height, true]); | ||
} else { | ||
tag.attributes.push(['viewBox', `0 0 ${width} ${height}`, true]); | ||
tag.attributes.push(['height', '1em', true]); | ||
tag.attributes.push(['width', `${width / height}em`, true]); | ||
} | ||
} | ||
} | ||
return tag; | ||
}) | ||
}); | ||
|
||
return simpleHTMLTokenizer.generate(tokens) | ||
return generate(tokens); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Oops, something went wrong.