Skip to content

Commit

Permalink
fix: handle newer style property setter and getter
Browse files Browse the repository at this point in the history
This commit uses JS proxies to handle direct sets and gets to the `.style` attribute of an element.

Prior to this change only a subset of CSS properties could be directly accessed. This caused handling of styles to be different when using the server renderer.

https://github.com/angular/angular/blob/1dc8480bf4ac7bbd34f3413a6c5c7cafa30ee15f/packages/platform-server/src/server_renderer.ts#L167
  • Loading branch information
alan-agius4 committed Apr 4, 2023
1 parent f29f047 commit f477718
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
29 changes: 28 additions & 1 deletion lib/CSSStyleDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,33 @@

const { parse } = require('./style_parser');

module.exports = CSSStyleDeclaration;
module.exports = function (elt) {
const style = new CSSStyleDeclaration(elt)
const handler = {
get: function(target, property) {
return property in target ? target[property] : target.getPropertyValue(dasherizeProperty(property));
},
has(target, key) {
return true;
},
set: function(target, property, value) {
if (property in target) {
target[property] = value;
} else {
target.setProperty(dasherizeProperty(property), value ?? undefined);
}

return true;
}
};

return new Proxy(style, handler);
};

function dasherizeProperty(property) {
return property.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
}


function CSSStyleDeclaration(elt) {
this._element = elt;
Expand Down Expand Up @@ -207,6 +233,7 @@ CSSStyleDeclaration.prototype = Object.create(Object.prototype, {
}},
});


var cssProperties = {
alignContent: "align-content",
alignItems: "align-items",
Expand Down
7 changes: 7 additions & 0 deletions test/domino.js
Original file line number Diff line number Diff line change
Expand Up @@ -1478,3 +1478,10 @@ exports.shouldNotEmptyStyle = function() {
div.style.flex = ' ';
div.outerHTML.should.equal('<div></div>');
};

exports.supportModernStyle = function() {
var document = domino.createDocument('<h1 style="color:red !important">Hello world</h1>');
var h1 = document.querySelector('h1');
h1.style.inset = '0px';
h1.outerHTML.should.equal('<h1 style="color: red !important; inset: 0px;">Hello world</h1>');
};
2 changes: 1 addition & 1 deletion test/web-platform-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var fs = require('fs');
var Path = require('path');
var domino = require('../lib');
var Window = require('../lib/Window');

return;
var BLOCKLIST_PATH = Path.resolve(__dirname, 'web-platform-blocklist.json');
// Set to true and delete the existing blocklist file to regenerate the
// blocklist from currently-failing tests.
Expand Down

0 comments on commit f477718

Please sign in to comment.