chore: Remove radix from parseInt, simplify Blockly.utils.dom methods, use Unicode characters. (#6441)

* chore: remove radix from parseInt

Previously any number starting with '0' would be parsed as octal if the radix was left blank.  But this was changed years ago.  It is no longer needed to specify a radix.

* chore: 'ID' is identification

'id' is a part of Freud's brain.

* Use Unicode characters instead of codes

This is in line with the current style guide.

* Simplify Blockly.utils.dom methods.

classList add/remove/has supports SVG elements in all browsers Blockly supports (i.e. not IE).
This commit is contained in:
Neil Fraser
2022-09-22 15:59:24 +02:00
committed by GitHub
parent c84febbe07
commit e5dcb766bd
24 changed files with 45 additions and 72 deletions

View File

@@ -84,7 +84,6 @@ export function createSvgElement<T extends SVGElement>(
/**
* Add a CSS class to a element.
* Similar to Closure's goog.dom.classes.add, except it handles SVG elements.
*
* @param element DOM element to add class to.
* @param className Name of class to add.
@@ -92,14 +91,10 @@ export function createSvgElement<T extends SVGElement>(
* @alias Blockly.utils.dom.addClass
*/
export function addClass(element: Element, className: string): boolean {
let classes = element.getAttribute('class') || '';
if ((' ' + classes + ' ').indexOf(' ' + className + ' ') !== -1) {
if (element.classList.contains(className)) {
return false;
}
if (classes) {
classes += ' ';
}
element.setAttribute('class', classes + className);
element.classList.add(className);
return true;
}
@@ -119,7 +114,6 @@ export function removeClasses(element: Element, classNames: string) {
/**
* Remove a CSS class from a element.
* Similar to Closure's goog.dom.classes.remove, except it handles SVG elements.
*
* @param element DOM element to remove class from.
* @param className Name of class to remove.
@@ -127,28 +121,15 @@ export function removeClasses(element: Element, classNames: string) {
* @alias Blockly.utils.dom.removeClass
*/
export function removeClass(element: Element, className: string): boolean {
const classes = element.getAttribute('class');
if ((' ' + classes + ' ').indexOf(' ' + className + ' ') === -1) {
if (!element.classList.contains(className)) {
return false;
}
const classList = classes!.split(/\s+/);
for (let i = 0; i < classList.length; i++) {
if (!classList[i] || classList[i] === className) {
classList.splice(i, 1);
i--;
}
}
if (classList.length) {
element.setAttribute('class', classList.join(' '));
} else {
element.removeAttribute('class');
}
element.classList.remove(className);
return true;
}
/**
* Checks if an element has the specified CSS class.
* Similar to Closure's goog.dom.classes.has, except it handles SVG elements.
*
* @param element DOM element to check.
* @param className Name of class to check.
@@ -156,8 +137,7 @@ export function removeClass(element: Element, className: string): boolean {
* @alias Blockly.utils.dom.hasClass
*/
export function hasClass(element: Element, className: string): boolean {
const classes = element.getAttribute('class');
return (' ' + classes + ' ').indexOf(' ' + className + ' ') !== -1;
return element.classList.contains(className);
}
/**

View File

@@ -52,7 +52,7 @@ let nextId = 0;
/**
* Generate the next unique element IDs.
* IDs are compatible with the HTML4 id attribute restrictions:
* IDs are compatible with the HTML4 'id' attribute restrictions:
* Use only ASCII letters, digits, '_', '-' and '.'
*
* For UUIDs use genUid (below) instead; this ID generator should

View File

@@ -55,10 +55,10 @@ export function getRelativeXY(element: Element): Coordinate {
const x = (element as any).x && element.getAttribute('x');
const y = (element as any).y && element.getAttribute('y');
if (x) {
xy.x = parseInt(x, 10);
xy.x = parseInt(x);
}
if (y) {
xy.y = parseInt(y, 10);
xy.y = parseInt(y);
}
// Second, check for transform="translate(...)" attribute.
const transform = element.getAttribute('transform');