2010-11-07 20:14:05 +00:00
|
|
|
/**
|
|
|
|
* Package: svgedit.sanitize
|
|
|
|
*
|
2012-09-16 18:53:27 +00:00
|
|
|
* Licensed under the MIT License
|
2010-11-07 20:14:05 +00:00
|
|
|
*
|
|
|
|
* Copyright(c) 2010 Alexis Deveria
|
|
|
|
* Copyright(c) 2010 Jeff Schiller
|
|
|
|
*/
|
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
import {getReverseNS, NS} from './svgedit.js';
|
|
|
|
import {isGecko} from './browser.js';
|
|
|
|
import {getHref, setHref, getUrlFromAttr} from './svgutils.js';
|
2010-11-07 20:14:05 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const REVERSE_NS = getReverseNS();
|
2010-11-07 20:14:05 +00:00
|
|
|
|
|
|
|
// this defines which elements and attributes that we support
|
2018-05-18 03:25:45 +00:00
|
|
|
const svgWhiteList_ = {
|
2018-05-18 04:02:30 +00:00
|
|
|
// SVG Elements
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
a: ['class', 'clip-path', 'clip-rule', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'mask', 'opacity', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'xlink:href', 'xlink:title'],
|
|
|
|
circle: ['class', 'clip-path', 'clip-rule', 'cx', 'cy', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'mask', 'opacity', 'r', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform'],
|
|
|
|
clipPath: ['class', 'clipPathUnits', 'id'],
|
|
|
|
defs: [],
|
|
|
|
style: ['type'],
|
|
|
|
desc: [],
|
|
|
|
ellipse: ['class', 'clip-path', 'clip-rule', 'cx', 'cy', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'mask', 'opacity', 'requiredFeatures', 'rx', 'ry', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform'],
|
|
|
|
feGaussianBlur: ['class', 'color-interpolation-filters', 'id', 'requiredFeatures', 'stdDeviation'],
|
|
|
|
filter: ['class', 'color-interpolation-filters', 'filterRes', 'filterUnits', 'height', 'id', 'primitiveUnits', 'requiredFeatures', 'width', 'x', 'xlink:href', 'y'],
|
|
|
|
foreignObject: ['class', 'font-size', 'height', 'id', 'opacity', 'requiredFeatures', 'style', 'transform', 'width', 'x', 'y'],
|
|
|
|
g: ['class', 'clip-path', 'clip-rule', 'id', 'display', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'mask', 'opacity', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'font-family', 'font-size', 'font-style', 'font-weight', 'text-anchor'],
|
|
|
|
image: ['class', 'clip-path', 'clip-rule', 'filter', 'height', 'id', 'mask', 'opacity', 'requiredFeatures', 'style', 'systemLanguage', 'transform', 'width', 'x', 'xlink:href', 'xlink:title', 'y'],
|
|
|
|
line: ['class', 'clip-path', 'clip-rule', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'x1', 'x2', 'y1', 'y2'],
|
|
|
|
linearGradient: ['class', 'id', 'gradientTransform', 'gradientUnits', 'requiredFeatures', 'spreadMethod', 'systemLanguage', 'x1', 'x2', 'xlink:href', 'y1', 'y2'],
|
|
|
|
marker: ['id', 'class', 'markerHeight', 'markerUnits', 'markerWidth', 'orient', 'preserveAspectRatio', 'refX', 'refY', 'systemLanguage', 'viewBox'],
|
|
|
|
mask: ['class', 'height', 'id', 'maskContentUnits', 'maskUnits', 'width', 'x', 'y'],
|
|
|
|
metadata: ['class', 'id'],
|
|
|
|
path: ['class', 'clip-path', 'clip-rule', 'd', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform'],
|
|
|
|
pattern: ['class', 'height', 'id', 'patternContentUnits', 'patternTransform', 'patternUnits', 'requiredFeatures', 'style', 'systemLanguage', 'viewBox', 'width', 'x', 'xlink:href', 'y'],
|
|
|
|
polygon: ['class', 'clip-path', 'clip-rule', 'id', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'id', 'class', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'points', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform'],
|
|
|
|
polyline: ['class', 'clip-path', 'clip-rule', 'id', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'opacity', 'points', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform'],
|
|
|
|
radialGradient: ['class', 'cx', 'cy', 'fx', 'fy', 'gradientTransform', 'gradientUnits', 'id', 'r', 'requiredFeatures', 'spreadMethod', 'systemLanguage', 'xlink:href'],
|
|
|
|
rect: ['class', 'clip-path', 'clip-rule', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'height', 'id', 'mask', 'opacity', 'requiredFeatures', 'rx', 'ry', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'width', 'x', 'y'],
|
|
|
|
stop: ['class', 'id', 'offset', 'requiredFeatures', 'stop-color', 'stop-opacity', 'style', 'systemLanguage'],
|
|
|
|
svg: ['class', 'clip-path', 'clip-rule', 'filter', 'id', 'height', 'mask', 'preserveAspectRatio', 'requiredFeatures', 'style', 'systemLanguage', 'viewBox', 'width', 'x', 'xmlns', 'xmlns:se', 'xmlns:xlink', 'y'],
|
|
|
|
switch: ['class', 'id', 'requiredFeatures', 'systemLanguage'],
|
|
|
|
symbol: ['class', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'font-family', 'font-size', 'font-style', 'font-weight', 'id', 'opacity', 'preserveAspectRatio', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'transform', 'viewBox'],
|
|
|
|
text: ['class', 'clip-path', 'clip-rule', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'font-family', 'font-size', 'font-style', 'font-weight', 'id', 'mask', 'opacity', 'requiredFeatures', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'text-anchor', 'transform', 'x', 'xml:space', 'y'],
|
|
|
|
textPath: ['class', 'id', 'method', 'requiredFeatures', 'spacing', 'startOffset', 'style', 'systemLanguage', 'transform', 'xlink:href'],
|
|
|
|
title: [],
|
|
|
|
tspan: ['class', 'clip-path', 'clip-rule', 'dx', 'dy', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'font-family', 'font-size', 'font-style', 'font-weight', 'id', 'mask', 'opacity', 'requiredFeatures', 'rotate', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'systemLanguage', 'text-anchor', 'textLength', 'transform', 'x', 'xml:space', 'y'],
|
|
|
|
use: ['class', 'clip-path', 'clip-rule', 'fill', 'fill-opacity', 'fill-rule', 'filter', 'height', 'id', 'mask', 'stroke', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-linecap', 'stroke-linejoin', 'stroke-miterlimit', 'stroke-opacity', 'stroke-width', 'style', 'transform', 'width', 'x', 'xlink:href', 'y'],
|
2013-02-16 15:02:26 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// MathML Elements
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
annotation: ['encoding'],
|
2018-05-18 04:02:30 +00:00
|
|
|
'annotation-xml': ['encoding'],
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
maction: ['actiontype', 'other', 'selection'],
|
|
|
|
math: ['class', 'id', 'display', 'xmlns'],
|
|
|
|
menclose: ['notation'],
|
|
|
|
merror: [],
|
|
|
|
mfrac: ['linethickness'],
|
|
|
|
mi: ['mathvariant'],
|
|
|
|
mmultiscripts: [],
|
|
|
|
mn: [],
|
|
|
|
mo: ['fence', 'lspace', 'maxsize', 'minsize', 'rspace', 'stretchy'],
|
|
|
|
mover: [],
|
|
|
|
mpadded: ['lspace', 'width', 'height', 'depth', 'voffset'],
|
|
|
|
mphantom: [],
|
|
|
|
mprescripts: [],
|
|
|
|
mroot: [],
|
|
|
|
mrow: ['xlink:href', 'xlink:type', 'xmlns:xlink'],
|
|
|
|
mspace: ['depth', 'height', 'width'],
|
|
|
|
msqrt: [],
|
|
|
|
mstyle: ['displaystyle', 'mathbackground', 'mathcolor', 'mathvariant', 'scriptlevel'],
|
|
|
|
msub: [],
|
|
|
|
msubsup: [],
|
|
|
|
msup: [],
|
|
|
|
mtable: ['align', 'columnalign', 'columnlines', 'columnspacing', 'displaystyle', 'equalcolumns', 'equalrows', 'frame', 'rowalign', 'rowlines', 'rowspacing', 'width'],
|
|
|
|
mtd: ['columnalign', 'columnspan', 'rowalign', 'rowspan'],
|
|
|
|
mtext: [],
|
|
|
|
mtr: ['columnalign', 'rowalign'],
|
|
|
|
munder: [],
|
|
|
|
munderover: [],
|
|
|
|
none: [],
|
|
|
|
semantics: []
|
2010-11-07 20:14:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Produce a Namespace-aware version of svgWhitelist
|
2018-05-18 03:25:45 +00:00
|
|
|
const svgWhiteListNS_ = {};
|
|
|
|
Object.entries(svgWhiteList_).forEach(function ([elt, atts]) {
|
|
|
|
const attNS = {};
|
|
|
|
Object.entries(atts).forEach(function ([i, att]) {
|
|
|
|
if (att.includes(':')) {
|
|
|
|
const v = att.split(':');
|
2018-05-18 04:02:30 +00:00
|
|
|
attNS[v[1]] = NS[(v[0]).toUpperCase()];
|
|
|
|
} else {
|
|
|
|
attNS[att] = att === 'xmlns' ? NS.XMLNS : null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
svgWhiteListNS_[elt] = attNS;
|
2010-11-07 20:14:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Function: svgedit.sanitize.sanitizeSvg
|
2018-05-18 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Sanitizes the input node and its children
|
|
|
|
* It only keeps what is allowed from our whitelist defined above
|
|
|
|
* @param node - The DOM element to be checked (we'll also check its children)
|
|
|
|
*/
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
export const sanitizeSvg = function (node) {
|
2018-05-18 04:02:30 +00:00
|
|
|
// Cleanup text nodes
|
2018-05-18 03:25:45 +00:00
|
|
|
if (node.nodeType === 3) { // 3 === TEXT_NODE
|
2018-05-18 04:02:30 +00:00
|
|
|
// Trim whitespace
|
|
|
|
node.nodeValue = node.nodeValue.replace(/^\s+|\s+$/g, '');
|
|
|
|
// Remove if empty
|
2018-05-18 03:25:45 +00:00
|
|
|
if (!node.nodeValue.length) {
|
2018-05-18 04:02:30 +00:00
|
|
|
node.parentNode.removeChild(node);
|
|
|
|
}
|
|
|
|
}
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// We only care about element nodes.
|
|
|
|
// Automatically return for all non-element nodes, such as comments, etc.
|
|
|
|
if (node.nodeType !== 1) { // 1 == ELEMENT_NODE
|
|
|
|
return;
|
|
|
|
}
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const doc = node.ownerDocument;
|
|
|
|
const parent = node.parentNode;
|
2018-05-18 06:41:43 +00:00
|
|
|
// can parent ever be null here? I think the root node's parent is the document...
|
2018-05-18 04:02:30 +00:00
|
|
|
if (!doc || !parent) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
const allowedAttrs = svgWhiteList_[node.nodeName];
|
|
|
|
const allowedAttrsNS = svgWhiteListNS_[node.nodeName];
|
2018-05-18 04:02:30 +00:00
|
|
|
// if this element is supported, sanitize it
|
|
|
|
if (typeof allowedAttrs !== 'undefined') {
|
2018-05-18 03:25:45 +00:00
|
|
|
const seAttrs = [];
|
|
|
|
let i = node.attributes.length;
|
2018-05-18 04:02:30 +00:00
|
|
|
while (i--) {
|
|
|
|
// if the attribute is not in our whitelist, then remove it
|
|
|
|
// could use jQuery's inArray(), but I don't know if that's any better
|
2018-05-18 03:25:45 +00:00
|
|
|
const attr = node.attributes.item(i);
|
|
|
|
const attrName = attr.nodeName;
|
|
|
|
const attrLocalName = attr.localName;
|
|
|
|
const attrNsURI = attr.namespaceURI;
|
2018-05-18 04:02:30 +00:00
|
|
|
// Check that an attribute with the correct localName in the correct namespace is on
|
|
|
|
// our whitelist or is a namespace declaration for one of our allowed namespaces
|
|
|
|
if (!(allowedAttrsNS.hasOwnProperty(attrLocalName) && attrNsURI === allowedAttrsNS[attrLocalName] && attrNsURI !== NS.XMLNS) &&
|
|
|
|
!(attrNsURI === NS.XMLNS && REVERSE_NS[attr.value])) {
|
|
|
|
// TODO(codedread): Programmatically add the se: attributes to the NS-aware whitelist.
|
|
|
|
// Bypassing the whitelist to allow se: prefixes.
|
|
|
|
// Is there a more appropriate way to do this?
|
2018-05-18 03:25:45 +00:00
|
|
|
if (attrName.startsWith('se:') || attrName.startsWith('data-')) {
|
2018-05-18 04:02:30 +00:00
|
|
|
seAttrs.push([attrName, attr.value]);
|
|
|
|
}
|
|
|
|
node.removeAttributeNS(attrNsURI, attrLocalName);
|
|
|
|
}
|
2010-11-07 20:14:05 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// Add spaces before negative signs where necessary
|
2018-05-18 03:25:45 +00:00
|
|
|
if (isGecko()) {
|
2018-05-18 04:02:30 +00:00
|
|
|
switch (attrName) {
|
|
|
|
case 'transform':
|
|
|
|
case 'gradientTransform':
|
|
|
|
case 'patternTransform':
|
2018-05-18 03:25:45 +00:00
|
|
|
const val = attr.value.replace(/(\d)-/g, '$1 -');
|
2018-05-18 04:02:30 +00:00
|
|
|
node.setAttribute(attrName, val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// For the style attribute, rewrite it in terms of XML presentational attributes
|
|
|
|
if (attrName === 'style') {
|
2018-05-18 03:25:45 +00:00
|
|
|
const props = attr.value.split(';');
|
|
|
|
let p = props.length;
|
2018-05-18 04:02:30 +00:00
|
|
|
while (p--) {
|
- Breaking change: Rename config file to `svgedit-config-iife.js` (or for the module version, `svgedit-config-es.js`);
also expect one directory higher; incorporates #207 (@iuyiuy)
- Breaking change: Separate `extIconsPath` from `extPath` (not copying over icons)
- Breaking change: Don't reference `custom.css` in HTML; can instead be referenced in JavaScript through
the config file (provided in `svgedit-config-sample-iife.js`/`svgedit-config-sample-es.js` as `svgedit-custom.css` for
better namespacing); incorporates #207 (@iuyiuy)
- Breaking change: Remove minified jgraduate/spinbtn files (minified within Rollup routine)
- Fix: Zoom when scrolled; incorporates #169 (@AndrolGenhald), adapting for conventions; also allow avoidance when shift key pressed
- Fix: Update Atom feed reference in HTML
- Fixes related to recent commits: Some path and method name fixes needed, function order, missing methods, variable scope declaration, no need for DOMContentLoaded listeners in modules, switch back to non-default export, avoid trimming nullish, deal with mock tests, fix `math.matrixMultiply`, use jquery-svg where needed for array/SVG attributes; add babel-polyfill and defer script to imagelib; other misc. fixes
- Enhancement: Move config-sample.js out of `editor` directory
- Enhancement: For `callback`-style extensions, also provide config object; add following
to that object: buildCanvgCallback, canvg, decode64, encode64, executeAfterLoads, getTypeMap, isChrome, ieIE, NS, text2xml
- Enhancement: Complete ES6 modules work (extensions, locales, tests), along with Babel;
make Node build routine for converting modular source to non-modular,
use `loadStylesheets` for modular stylehsheet defining (but parallel loading);
- Enhancement: Add `stylesheets` config for modular but parallel stylesheet loading with `@default` option for simple inclusion/exclusion of defaults (if not going with default).
- Refactoring: Clean up `svg-editor.html`: consistent indents; avoid extra lbs, avoid long lines
- Refactoring: Avoid embedded API adding inline JavaScript listener
- Refactoring: Move layers and context code to `draw.js`
- Refactoring: Move `pathActions` from `svgcanvas.js` (though preserve aliases to these methods on `canvas`) and `convertPath` from `svgutils.js` to `path.js`
- Refactoring: Move `getStrokedBBox` from `svgcanvas.js` (while keeping an alias) to `svgutils.js` (as `getStrokedBBoxDefaultVisible` to avoid conflict with existing)
- Docs: Remove "dependencies" comments in code except where summarizing role of jQuery or a non-obvious dependency
- Refactoring/Linting: Enfore `no-extra-semi` and `quote-props` rules
- Refactoring: Further avoidance of quotes on properties (as possible)
- Refactoring: Use `class` in place of functions where intended as classes
- Refactoring: Consistency and granularity in extensions imports
- Testing: Update QUnit to 2.6.1 (node_modules) and Sinon to 5.0.8 (and add sinon-test at 2.1.3) and enforce eslint-plugin-qunit linting rules; update custom extensions
- Testing: Add node-static for automating (and accessing out-of-directory contents)
- Testing: Avoid HTML attributes for styling
- Testing: Add npm `test` script
- Testing: Comment out unused jQuery SVG test
- Testing: Add test1 and svgutils_performance_test to all tests page
- Testing: Due apparently to Path having not been a formal class, the test was calling it without `new`; refactored now with sufficient mock data to take into account it is a class
- npm: Update devDeps
- npm: Add html modules and config build to test script
2018-05-22 10:03:16 +00:00
|
|
|
const [name, val] = props[p].split(':');
|
|
|
|
const styleAttrName = (name || '').trim();
|
|
|
|
const styleAttrVal = (val || '').trim();
|
2018-05-18 04:02:30 +00:00
|
|
|
// Now check that this attribute is supported
|
2018-05-18 03:25:45 +00:00
|
|
|
if (allowedAttrs.includes(styleAttrName)) {
|
2018-05-18 04:02:30 +00:00
|
|
|
node.setAttribute(styleAttrName, styleAttrVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node.removeAttribute('style');
|
|
|
|
}
|
|
|
|
}
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 03:25:45 +00:00
|
|
|
Object.values(seAttrs).forEach(function (attr) {
|
2018-05-18 04:02:30 +00:00
|
|
|
node.setAttributeNS(NS.SE, attr[0], attr[1]);
|
|
|
|
});
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// for some elements that have a xlink:href, ensure the URI refers to a local element
|
|
|
|
// (but not for links)
|
2018-05-18 03:25:45 +00:00
|
|
|
const href = getHref(node);
|
2018-05-18 04:02:30 +00:00
|
|
|
if (href &&
|
|
|
|
['filter', 'linearGradient', 'pattern',
|
2018-05-18 03:25:45 +00:00
|
|
|
'radialGradient', 'textPath', 'use'].includes(node.nodeName)) {
|
2018-05-18 04:02:30 +00:00
|
|
|
// TODO: we simply check if the first character is a #, is this bullet-proof?
|
|
|
|
if (href[0] !== '#') {
|
|
|
|
// remove the attribute (but keep the element)
|
2018-05-18 03:25:45 +00:00
|
|
|
setHref(node, '');
|
2018-05-18 04:02:30 +00:00
|
|
|
node.removeAttributeNS(NS.XLINK, 'href');
|
|
|
|
}
|
|
|
|
}
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// Safari crashes on a <use> without a xlink:href, so we just remove the node here
|
2018-05-18 03:25:45 +00:00
|
|
|
if (node.nodeName === 'use' && !getHref(node)) {
|
2018-05-18 04:02:30 +00:00
|
|
|
parent.removeChild(node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// if the element has attributes pointing to a non-local reference,
|
|
|
|
// need to remove the attribute
|
2018-05-18 03:25:45 +00:00
|
|
|
Object.values(['clip-path', 'fill', 'filter', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'stroke'], function (attr) {
|
|
|
|
let val = node.getAttribute(attr);
|
2018-05-18 04:02:30 +00:00
|
|
|
if (val) {
|
2018-05-18 03:25:45 +00:00
|
|
|
val = getUrlFromAttr(val);
|
2018-05-18 04:02:30 +00:00
|
|
|
// simply check for first character being a '#'
|
|
|
|
if (val && val[0] !== '#') {
|
|
|
|
node.setAttribute(attr, '');
|
|
|
|
node.removeAttribute(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// recurse to children
|
|
|
|
i = node.childNodes.length;
|
2018-05-18 03:25:45 +00:00
|
|
|
while (i--) { sanitizeSvg(node.childNodes.item(i)); }
|
2018-05-18 04:02:30 +00:00
|
|
|
// else (element not supported), remove it
|
|
|
|
} else {
|
|
|
|
// remove all children from this node and insert them before this node
|
|
|
|
// FIXME: in the case of animation elements this will hardly ever be correct
|
2018-05-18 03:25:45 +00:00
|
|
|
const children = [];
|
2018-05-18 04:02:30 +00:00
|
|
|
while (node.hasChildNodes()) {
|
|
|
|
children.push(parent.insertBefore(node.firstChild, node));
|
|
|
|
}
|
2013-02-25 12:39:27 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// remove this node from the document altogether
|
|
|
|
parent.removeChild(node);
|
2010-11-07 20:14:05 +00:00
|
|
|
|
2018-05-18 04:02:30 +00:00
|
|
|
// call sanitizeSvg on each of those children
|
2018-05-18 03:25:45 +00:00
|
|
|
let i = children.length;
|
|
|
|
while (i--) { sanitizeSvg(children[i]); }
|
2018-05-18 04:02:30 +00:00
|
|
|
}
|
2010-11-07 20:14:05 +00:00
|
|
|
};
|