Skip to content

Commit

Permalink
Keep track of style and event attribute changes in SVGElement
Browse files Browse the repository at this point in the history
Fixes #2594.

Co-authored-by: Timothy Gu <timothygu99@gmail.com>
  • Loading branch information
2 people authored and domenic committed May 28, 2019
1 parent 1951a19 commit 9f6b190
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/jsdom/living/nodes/ElementCSSInlineStyle-impl.js
Expand Up @@ -3,6 +3,7 @@ const cssstyle = require("cssstyle");

class ElementCSSInlineStyle {
_initElementCSSInlineStyle() {
this._settingCssText = false;
this._style = new cssstyle.CSSStyleDeclaration(newCssText => {
if (!this._settingCssText) {
this._settingCssText = true;
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/nodes/HTMLElement-impl.js
Expand Up @@ -16,7 +16,6 @@ class HTMLElementImpl extends ElementImpl {
this._initElementCSSInlineStyle();
this._initGlobalEvents();

this._settingCssText = false;
this._clickInProgress = false;

// <summary> uses HTMLElement and has activation behavior
Expand Down Expand Up @@ -116,6 +115,7 @@ class HTMLElementImpl extends ElementImpl {
this.setAttributeNS(null, "dir", value);
}

// Keep in sync with SVGElement. https://github.com/jsdom/jsdom/issues/2599
_attrModified(name, value, oldValue) {
if (name === "style" && value !== oldValue && !this._settingCssText) {
this._settingCssText = true;
Expand Down
13 changes: 13 additions & 0 deletions lib/jsdom/living/nodes/SVGElement-impl.js
Expand Up @@ -17,6 +17,19 @@ class SVGElementImpl extends ElementImpl {
this._initGlobalEvents();
}

// Keep in sync with HTMLElement. https://github.com/jsdom/jsdom/issues/2599
_attrModified(name, value, oldValue) {
if (name === "style" && value !== oldValue && !this._settingCssText) {
this._settingCssText = true;
this._style.cssText = value;
this._settingCssText = false;
} else if (name.startsWith("on")) {
this._globalEventChanged(name.substring(2));
}

super._attrModified.apply(this, arguments);
}

get className() {
return SVGAnimatedString.createImpl([], {
element: this,
Expand Down
27 changes: 24 additions & 3 deletions test/web-platform-tests/to-upstream/svg/element.html
Expand Up @@ -6,7 +6,7 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<svg>
<svg style="font-size: 100px" onclick="onClickTestVar = 1">
<unknownElement/>
</svg>

Expand All @@ -15,7 +15,7 @@

test(() => {
const svg = document.querySelector("svg");
assert_true(svg instanceof SVGElement);
assert_true(svg instanceof SVGSVGElement);
const unknownElement = svg.children[0];
assert_true(unknownElement instanceof SVGElement);
assert_equals(unknownElement.namespaceURI, "http://www.w3.org/2000/svg");
Expand All @@ -24,14 +24,35 @@
assert_equals(unknownElement.viewportElement, svg);
}, "Inheritance");

test(() => {
const svg = document.querySelector("svg");
assert_true(svg instanceof SVGSVGElement);
assert_true(svg.style instanceof CSSStyleDeclaration);
assert_equals(svg.getAttribute("style"), "font-size: 100px");
assert_equals(svg.style.fontSize, "100px");
svg.setAttribute("style", "background-color: yellow;");
assert_equals(svg.getAttribute("style"), "background-color: yellow;");
assert_equals(svg.style.backgroundColor, "yellow");
assert_equals(svg.style.fontSize, "");
}, "style data attribute");

test(() => {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
assert_true(svg.style instanceof CSSStyleDeclaration);
assert_equals(svg.style.backgroundColor, "");
svg.style.backgroundColor = "black";
assert_equals(svg.style.backgroundColor, "black");
assert_equals(svg.getAttribute("style"), "background-color: black;");
}, "style attribute");
}, "style IDL attribute");

test(() => {
const svg = document.querySelector("svg");
assert_true(svg instanceof SVGSVGElement);
assert_true(typeof svg.onclick === "function");
window.onClickTestVar = 0;
svg.onclick();
assert_equals(window.onClickTestVar, 1);
}, "event handler data attribute");

test(() => {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
Expand Down

0 comments on commit 9f6b190

Please sign in to comment.