Skip to content

Commit

Permalink
feat(editor-libs): extend the feature detection for CSS functions (#1270
Browse files Browse the repository at this point in the history
)

* Check also property value's validity

* Fix

* Pass choice text to `isPropertySupported()`

* Lint

* Use `isCodeSupported()`

* Naming

* Refactor

* Remove unused import

* Remove unused import x2

---------

Co-authored-by: Niedziolka Michal <niedziolek@hotmail.com>
  • Loading branch information
yarusome and NiedziolkaMichal authored May 4, 2023
1 parent 4cd03fb commit 5de231c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
7 changes: 5 additions & 2 deletions editor/js/editable-css.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as clippy from "./editor-libs/clippy.js";
import * as mceEvents from "./editor-libs/events.js";
import * as mceUtils from "./editor-libs/mce-utils.js";
import * as cssEditorUtils from "./editor-libs/css-editor-utils.js";
import {
initCodeEditor,
Expand All @@ -14,6 +13,10 @@ import "../css/editable-css.css";
(function () {
const exampleChoiceList = document.getElementById("example-choice-list");
const exampleChoices = exampleChoiceList.querySelectorAll(".example-choice");
const exampleDeclarations = Array.from(
exampleChoices,
(choice) => choice.querySelector("code").textContent
);
const editorWrapper = document.getElementById("editor-wrapper");
const output = document.getElementById("output");
const warningNoSupport = document.getElementById("warning-no-support");
Expand Down Expand Up @@ -138,7 +141,7 @@ import "../css/editable-css.css";
is a non standard object available only in IE10 and older,
this will stop JS from executing in those versions. */
if (
mceUtils.isPropertySupported(exampleChoiceList.dataset) &&
cssEditorUtils.isAnyDeclarationSetSupported(exampleDeclarations) &&
!document.all
) {
enableLiveEditor();
Expand Down
9 changes: 9 additions & 0 deletions editor/js/editor-libs/css-editor-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export function applyCode(code, choice, targetElement, immediateInvalidChange) {
}
}

/**
* Creates a temporary element and tests whether any of the provided CSS sets of declarations are fully supported by the user's browser
* @param {Array} declarationSets - Array in which every element is one or multiple declarations separated by semicolons
*/
export function isAnyDeclarationSetSupported(declarationSets) {
const tmpElem = document.createElement("div");
return declarationSets.some(isCodeSupported.bind(null, tmpElem));
}

/**
* Checks if every passed declaration is supported by the browser.
* In case browser recognizes property with vendor prefix(like -webkit-), lacking support for unprefixed property is ignored.
Expand Down
29 changes: 0 additions & 29 deletions editor/js/editor-libs/mce-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,6 @@ export function findParentChoiceElem(element) {
}
return parent;
}

/**
* Creates a temporary element and tests whether the passed
* property exists on the `style` property of the element.
* @param {Object} dataset = The dataset from which to get the property
*/
export function isPropertySupported(dataset) {
/* If there are no 'property' attributes,
there is nothing to test, so return true. */
if (dataset["property"] === undefined) {
return true;
}

// `property` may be a space-separated list of properties.
const properties = dataset["property"].split(" ");
/* Iterate through properties: if any of them apply,
the browser supports this example. */
const tmpElem = document.createElement("div");
let supported = false;

for (let i = 0, l = properties.length; i < l; i++) {
if (tmpElem.style[properties[i]] !== undefined) {
supported = true;
}
}

return supported;
}

/**
* Interrupts the default click event on external links inside
* the iframe and opens them in a new tab instead
Expand Down

0 comments on commit 5de231c

Please sign in to comment.