Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

parchment

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parchment - npm Package Compare versions

Comparing version 1.0.0-beta to 1.0.0-beta.1

src/blot/embed.ts

568

dist/parchment.js

@@ -66,12 +66,12 @@ (function webpackUniversalModuleDefinition(root, factory) {

var container_1 = __webpack_require__(2);
var format_1 = __webpack_require__(7);
var leaf_1 = __webpack_require__(9);
var embed_1 = __webpack_require__(10);
var scroll_1 = __webpack_require__(11);
var inline_1 = __webpack_require__(12);
var block_1 = __webpack_require__(13);
var text_1 = __webpack_require__(14);
var attributor_1 = __webpack_require__(6);
var class_1 = __webpack_require__(15);
var style_1 = __webpack_require__(16);
var format_1 = __webpack_require__(6);
var leaf_1 = __webpack_require__(11);
var scroll_1 = __webpack_require__(12);
var inline_1 = __webpack_require__(13);
var block_1 = __webpack_require__(14);
var embed_1 = __webpack_require__(15);
var text_1 = __webpack_require__(16);
var attributor_1 = __webpack_require__(7);
var class_1 = __webpack_require__(9);
var style_1 = __webpack_require__(10);
var store_1 = __webpack_require__(8);

@@ -94,3 +94,3 @@ var Registry = __webpack_require__(5);

Attributor: {
Attributor: attributor_1.default,
Attribute: attributor_1.default,
Class: class_1.default,

@@ -203,6 +203,10 @@ Style: style_1.default,

ContainerBlot.prototype.insertBefore = function (childBlot, refBlot) {
if (this.statics.allowedChildren != null && !this.statics.allowedChildren.some(function (child) {
return childBlot instanceof child;
})) {
throw new Registry.ParchmentError("Cannot insert " + childBlot.statics.blotName + " into " + this.statics.blotName);
}
childBlot.insertInto(this, refBlot);
};
ContainerBlot.prototype.length = function () {
// TODO can we use descendants?
return this.children.reduce(function (memo, child) {

@@ -220,4 +224,4 @@ return memo + child.length();

if (this.children.length === 0) {
if (this.statics.childless != null) {
var child = Registry.create(this.statics.childless);
if (this.statics.defaultChild != null) {
var child = Registry.create(this.statics.defaultChild);
this.appendChild(child);

@@ -463,10 +467,14 @@ child.optimize();

get: function () {
var statics = this.constructor;
return {
blotName: statics.blotName,
childless: statics.childless,
className: statics.className,
scope: statics.scope,
tagName: statics.tagName
};
var _this = this;
return [
'blotName', 'className', 'scope', 'tagName',
'defaultChild', 'allowedChildren',
'create', 'formats', 'value'
].reduce(function (memo, key) {
var value = _this.constructor[key];
if (value != null) {
memo[key] = value;
}
return memo;
}, {});
},

@@ -482,4 +490,7 @@ enumerable: true,

if (Array.isArray(this.tagName)) {
if (typeof value === 'string' && parseInt(value).toString() === value) {
value = parseInt(value);
if (typeof value === 'string') {
value = value.toUpperCase();
if (parseInt(value).toString() === value) {
value = parseInt(value);
}
}

@@ -520,8 +531,12 @@ if (typeof value === 'number') {

};
ShadowBlot.prototype.findOffset = function (node) {
return node === this.domNode ? 0 : -1;
};
ShadowBlot.prototype.formatAt = function (index, length, name, value) {
var blot = this.isolate(index, length);
blot.wrap(name, value);
if (Registry.query(name, Registry.Scope.BLOT) != null) {
blot.wrap(name, value);
}
else if (Registry.query(name, Registry.Scope.ATTRIBUTE) != null) {
var parent_1 = Registry.create(this.statics.scope);
blot.wrap(parent_1);
parent_1.format(name, value);
}
};

@@ -551,2 +566,6 @@ ShadowBlot.prototype.insertAt = function (index, value, def) {

};
ShadowBlot.prototype.length = function () {
return 1;
};
;
ShadowBlot.prototype.offset = function (root) {

@@ -559,3 +578,3 @@ if (root === void 0) { root = this.parent; }

ShadowBlot.prototype.optimize = function () {
// TODO clean up
// TODO clean up once we use WeakMap
if (this.domNode[Registry.DATA_KEY] != null) {

@@ -606,3 +625,3 @@ delete this.domNode[Registry.DATA_KEY].mutations;

/* 5 */
/***/ function(module, exports, __webpack_require__) {
/***/ function(module, exports) {

@@ -615,3 +634,2 @@ "use strict";

};
var attributor_1 = __webpack_require__(6);
var ParchmentError = (function (_super) {

@@ -651,9 +669,4 @@ __extends(ParchmentError, _super);

if (match == null) {
throw new ParchmentError("Unable to create " + input);
throw new ParchmentError("Unable to create " + input + " blot");
}
if (match instanceof attributor_1.default) {
var blot = create(match.scope & Scope.LEVEL);
blot.format(input, value);
return blot;
}
var BlotClass = match;

@@ -684,3 +697,3 @@ var node = input instanceof Node ? input : BlotClass.create(value);

}
if (typeof query === 'number') {
else if (typeof query === 'number') {
if (query & Scope.LEVEL & Scope.BLOCK) {

@@ -734,5 +747,12 @@ match = types['block'];

if (Definition.tagName != null) {
if (Array.isArray(Definition.tagName)) {
Definition.tagName = Definition.tagName.map(function (tagName) {
return tagName.toUpperCase();
});
}
else {
Definition.tagName = Definition.tagName.toUpperCase();
}
var tagNames = Array.isArray(Definition.tagName) ? Definition.tagName : [Definition.tagName];
tagNames.forEach(function (tag) {
tag = tag.toUpperCase();
if (tags[tag] == null || Definition.className == null) {

@@ -754,47 +774,2 @@ tags[tag] = Definition;

"use strict";
var Registry = __webpack_require__(5);
var Attributor = (function () {
function Attributor(attrName, keyName, options) {
if (options === void 0) { options = {}; }
this.attrName = attrName;
this.keyName = keyName;
var attributeBit = Registry.Scope.TYPE & Registry.Scope.ATTRIBUTE;
if (options.scope != null) {
// Ignore type bits, force attribute bit
this.scope = (options.scope & Registry.Scope.LEVEL) | attributeBit;
}
else {
this.scope = Registry.Scope.ATTRIBUTE;
}
if (options.whitelist != null)
this.whitelist = options.whitelist;
}
Attributor.prototype.add = function (node, value) {
node.setAttribute(this.keyName, value);
return true;
};
Attributor.prototype.canAdd = function (node, value) {
var match = Registry.query(node, Registry.Scope.BLOT & (this.scope | Registry.Scope.TYPE));
if (match != null && (this.whitelist == null || this.whitelist.indexOf(value) > -1)) {
return true;
}
return false;
};
Attributor.prototype.remove = function (node) {
node.removeAttribute(this.keyName);
};
Attributor.prototype.value = function (node) {
return node.getAttribute(this.keyName);
};
return Attributor;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Attributor;
/***/ },
/* 7 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var __extends = (this && this.__extends) || function (d, b) {

@@ -805,3 +780,3 @@ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];

};
var attributor_1 = __webpack_require__(6);
var attributor_1 = __webpack_require__(7);
var store_1 = __webpack_require__(8);

@@ -815,2 +790,11 @@ var container_1 = __webpack_require__(2);

}
FormatBlot.formats = function (domNode) {
if (typeof this.tagName === 'string') {
return true;
}
else if (Array.isArray(this.tagName)) {
return domNode.tagName.toLowerCase();
}
return undefined;
};
FormatBlot.prototype.attach = function () {

@@ -830,10 +814,8 @@ _super.prototype.attach.call(this);

}
else if (name === this.statics.blotName) {
this.replaceWith(Registry.create(this.statics.scope));
}
};
FormatBlot.prototype.formats = function () {
var formats = this.attributes.values();
if (Registry.query(this.statics.scope).blotName !== this.statics.blotName) {
formats[this.statics.blotName] = Array.isArray(this.statics.tagName) ? this.domNode.tagName.toLowerCase() : true;
var format = this.statics.formats(this.domNode);
if (format != null) {
formats[this.statics.blotName] = format;
}

@@ -870,2 +852,52 @@ return formats;

/***/ },
/* 7 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var Registry = __webpack_require__(5);
var Attributor = (function () {
function Attributor(attrName, keyName, options) {
if (options === void 0) { options = {}; }
this.attrName = attrName;
this.keyName = keyName;
var attributeBit = Registry.Scope.TYPE & Registry.Scope.ATTRIBUTE;
if (options.scope != null) {
// Ignore type bits, force attribute bit
this.scope = (options.scope & Registry.Scope.LEVEL) | attributeBit;
}
else {
this.scope = Registry.Scope.ATTRIBUTE;
}
if (options.whitelist != null)
this.whitelist = options.whitelist;
}
Attributor.keys = function (node) {
return [].map.call(node.attributes, function (item) {
return item.name;
});
};
Attributor.prototype.add = function (node, value) {
node.setAttribute(this.keyName, value);
return true;
};
Attributor.prototype.canAdd = function (node, value) {
var match = Registry.query(node, Registry.Scope.BLOT & (this.scope | Registry.Scope.TYPE));
if (match != null && (this.whitelist == null || this.whitelist.indexOf(value) > -1)) {
return true;
}
return false;
};
Attributor.prototype.remove = function (node) {
node.removeAttribute(this.keyName);
};
Attributor.prototype.value = function (node) {
return node.getAttribute(this.keyName);
};
return Attributor;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Attributor;
/***/ },
/* 8 */

@@ -875,3 +907,5 @@ /***/ function(module, exports, __webpack_require__) {

"use strict";
var attributor_1 = __webpack_require__(6);
var attributor_1 = __webpack_require__(7);
var class_1 = __webpack_require__(9);
var style_1 = __webpack_require__(10);
var Registry = __webpack_require__(5);

@@ -898,19 +932,5 @@ var AttributorStore = (function () {

this.attributes = {};
var attributes = [], classes = [], styles = [];
[].slice.call(this.domNode.attributes).forEach(function (item) {
if (item.name === 'class') {
classes = item.value.split(/\s+/).map(function (name) {
return name.split('-').slice(0, -1).join('-');
});
}
else if (item.name === 'style') {
styles = item.value.split(';').map(function (val) {
var arr = val.split(':');
return arr[0].trim();
});
}
else {
attributes.push(item.name);
}
});
var attributes = attributor_1.default.keys(this.domNode);
var classes = class_1.default.keys(this.domNode);
var styles = style_1.default.keys(this.domNode);
attributes.concat(classes).concat(styles).forEach(function (name) {

@@ -961,14 +981,43 @@ var attr = Registry.query(name, Registry.Scope.ATTRIBUTE);

};
var shadow_1 = __webpack_require__(4);
var Registry = __webpack_require__(5);
var LeafBlot = (function (_super) {
__extends(LeafBlot, _super);
function LeafBlot() {
var attributor_1 = __webpack_require__(7);
function match(node, prefix) {
var className = node.getAttribute('class') || '';
return className.split(/\s+/).filter(function (name) {
return name.indexOf(prefix + "-") === 0;
});
}
var ClassAttributor = (function (_super) {
__extends(ClassAttributor, _super);
function ClassAttributor() {
_super.apply(this, arguments);
}
LeafBlot.scope = Registry.Scope.INLINE_BLOT;
return LeafBlot;
}(shadow_1.default));
ClassAttributor.keys = function (node) {
return (node.getAttribute('class') || '').split(/\s+/).map(function (name) {
return name.split('-').slice(0, -1).join('-');
});
};
ClassAttributor.prototype.add = function (node, value) {
if (!this.canAdd(node, value))
return false;
this.remove(node);
node.classList.add(this.keyName + "-" + value);
return true;
};
ClassAttributor.prototype.remove = function (node) {
var matches = match(node, this.keyName);
matches.forEach(function (name) {
node.classList.remove(name);
});
if (node.classList.length === 0) {
node.removeAttribute('class');
}
};
ClassAttributor.prototype.value = function (node) {
var result = match(node, this.keyName)[0] || '';
return result.slice(this.keyName.length + 1); // +1 for hyphen
};
return ClassAttributor;
}(attributor_1.default));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = LeafBlot;
exports.default = ClassAttributor;

@@ -986,15 +1035,63 @@

};
var leaf_1 = __webpack_require__(9);
var EmbedBlot = (function (_super) {
__extends(EmbedBlot, _super);
function EmbedBlot() {
var attributor_1 = __webpack_require__(7);
function camelize(name) {
var parts = name.split('-');
var rest = parts.slice(1).map(function (part) {
return part[0].toUpperCase() + part.slice(1);
}).join('');
return parts[0] + rest;
}
var StyleAttributor = (function (_super) {
__extends(StyleAttributor, _super);
function StyleAttributor() {
_super.apply(this, arguments);
}
EmbedBlot.prototype.format = function (name, value) {
// Do nothing by default
StyleAttributor.keys = function (node) {
return (node.getAttribute('style') || '').split(';').map(function (value) {
var arr = value.split(':');
return arr[0].trim();
});
};
EmbedBlot.prototype.formats = function () {
return {};
StyleAttributor.prototype.add = function (node, value) {
if (!this.canAdd(node, value))
return false;
node.style[camelize(this.keyName)] = value;
return true;
};
EmbedBlot.prototype.index = function (node, offset) {
StyleAttributor.prototype.remove = function (node) {
node.style[camelize(this.keyName)] = '';
if (!node.getAttribute('style')) {
node.removeAttribute('style');
}
};
StyleAttributor.prototype.value = function (node) {
return node.style[camelize(this.keyName)];
};
return StyleAttributor;
}(attributor_1.default));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = StyleAttributor;
/***/ },
/* 11 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var shadow_1 = __webpack_require__(4);
var Registry = __webpack_require__(5);
var LeafBlot = (function (_super) {
__extends(LeafBlot, _super);
function LeafBlot() {
_super.apply(this, arguments);
}
LeafBlot.value = function (domNode) {
return true;
};
LeafBlot.prototype.index = function (node, offset) {
if (node !== this.domNode)

@@ -1004,6 +1101,3 @@ return -1;

};
EmbedBlot.prototype.length = function () {
return 1;
};
EmbedBlot.prototype.position = function (index, inclusive) {
LeafBlot.prototype.position = function (index, inclusive) {
var offset = [].indexOf.call(this.parent.domNode.childNodes, this.domNode);

@@ -1014,13 +1108,16 @@ if (index > 0)

};
EmbedBlot.prototype.value = function () {
return true;
LeafBlot.prototype.value = function () {
var value = {};
value[this.statics.blotName] = this.statics.value(this.domNode) || true;
return value;
};
return EmbedBlot;
}(leaf_1.default));
LeafBlot.scope = Registry.Scope.INLINE_BLOT;
return LeafBlot;
}(shadow_1.default));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = EmbedBlot;
exports.default = LeafBlot;
/***/ },
/* 11 */
/* 12 */
/***/ function(module, exports, __webpack_require__) {

@@ -1084,7 +1181,9 @@

_super.prototype.optimize.call(this);
mutations.push.apply(mutations, this.observer.takeRecords());
// TODO use WeakMap
mutations = mutations.concat(this.observer.takeRecords());
var mark = function (blot) {
if (blot == null || blot === _this)
return;
if (blot.domNode.parentNode == null)
return;
if (blot.domNode[Registry.DATA_KEY].mutations == null) {

@@ -1105,7 +1204,8 @@ blot.domNode[Registry.DATA_KEY].mutations = [];

};
for (var i = 0; mutations.length > 0; i += 1) {
var remaining = mutations;
for (var i = 0; remaining.length > 0; i += 1) {
if (i >= MAX_OPTIMIZE_ITERATIONS) {
throw new Error('[Parchment] Maximum optimize iterations reached');
}
mutations.forEach(function (mutation) {
remaining.forEach(function (mutation) {
var blot = Registry.find(mutation.target, true);

@@ -1125,3 +1225,4 @@ if (blot != null && blot.domNode === mutation.target && mutation.type === 'childList') {

this.children.forEach(optimize);
mutations = this.observer.takeRecords();
remaining = this.observer.takeRecords();
mutations.push.apply(mutations, remaining);
}

@@ -1146,15 +1247,13 @@ };

}).forEach(function (blot) {
if (blot == null)
if (blot == null || blot === _this)
return;
if (blot === _this) {
_super.prototype.update.call(_this, blot.domNode[Registry.DATA_KEY].mutations);
}
else {
blot.update(blot.domNode[Registry.DATA_KEY].mutations);
}
blot.update(blot.domNode[Registry.DATA_KEY].mutations || []);
});
if (this.domNode[Registry.DATA_KEY].mutations != null) {
_super.prototype.update.call(this, this.domNode[Registry.DATA_KEY].mutations);
}
this.optimize(mutations);
};
ScrollBlot.blotName = 'scroll';
ScrollBlot.childless = 'block';
ScrollBlot.defaultChild = 'block';
ScrollBlot.scope = Registry.Scope.BLOCK_BLOT;

@@ -1169,3 +1268,3 @@ ScrollBlot.tagName = 'DIV';

/***/ },
/* 12 */
/* 13 */
/***/ function(module, exports, __webpack_require__) {

@@ -1179,3 +1278,3 @@

};
var format_1 = __webpack_require__(7);
var format_1 = __webpack_require__(6);
var Registry = __webpack_require__(5);

@@ -1197,2 +1296,15 @@ // Shallow object comparison

}
InlineBlot.formats = function (domNode) {
if (domNode.tagName === InlineBlot.tagName)
return undefined;
return _super.formats.call(this, domNode);
};
InlineBlot.prototype.format = function (name, value) {
if (name === this.statics.blotName && !value) {
this.replaceWith(InlineBlot.blotName);
}
else {
_super.prototype.format.call(this, name, value);
}
};
InlineBlot.prototype.formatAt = function (index, length, name, value) {

@@ -1229,3 +1341,3 @@ if (this.formats()[name] != null || Registry.query(name, Registry.Scope.ATTRIBUTE)) {

/***/ },
/* 13 */
/* 14 */
/***/ function(module, exports, __webpack_require__) {

@@ -1239,3 +1351,3 @@

};
var format_1 = __webpack_require__(7);
var format_1 = __webpack_require__(6);
var Registry = __webpack_require__(5);

@@ -1247,2 +1359,15 @@ var BlockBlot = (function (_super) {

}
BlockBlot.formats = function (domNode) {
if (domNode.tagName === BlockBlot.tagName)
return undefined;
return _super.formats.call(this, domNode);
};
BlockBlot.prototype.format = function (name, value) {
if (name === this.statics.blotName && !value) {
this.replaceWith(BlockBlot.blotName);
}
else {
_super.prototype.format.call(this, name, value);
}
};
BlockBlot.prototype.formatAt = function (index, length, name, value) {

@@ -1258,2 +1383,3 @@ if (Registry.query(name, Registry.Scope.BLOCK) != null) {

if (def == null || Registry.query(value, Registry.Scope.INLINE) != null) {
// Insert text or inline
_super.prototype.insertAt.call(this, index, value, def);

@@ -1277,3 +1403,3 @@ }

/***/ },
/* 14 */
/* 15 */
/***/ function(module, exports, __webpack_require__) {

@@ -1287,3 +1413,34 @@

};
var leaf_1 = __webpack_require__(9);
var leaf_1 = __webpack_require__(11);
var EmbedBlot = (function (_super) {
__extends(EmbedBlot, _super);
function EmbedBlot() {
_super.apply(this, arguments);
}
EmbedBlot.formats = function (domNode) {
return undefined;
};
EmbedBlot.prototype.format = function (name, value) {
// Do nothing by default
};
EmbedBlot.prototype.formats = function () {
return this.statics.formats(this.domNode);
};
return EmbedBlot;
}(leaf_1.default));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = EmbedBlot;
/***/ },
/* 16 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var leaf_1 = __webpack_require__(11);
var Registry = __webpack_require__(5);

@@ -1294,3 +1451,3 @@ var TextBlot = (function (_super) {

_super.call(this, node);
this.text = this.domNode.data;
this.text = this.statics.value(this.domNode);
}

@@ -1300,2 +1457,5 @@ TextBlot.create = function (value) {

};
TextBlot.value = function (domNode) {
return domNode.data;
};
TextBlot.prototype.deleteAt = function (index, length) {

@@ -1324,3 +1484,3 @@ this.domNode.data = this.text = this.text.slice(0, index) + this.text.slice(index + length);

_super.prototype.optimize.call(this);
this.text = this.domNode.data;
this.text = this.statics.value(this.domNode);
if (this.text.length === 0) {

@@ -1348,3 +1508,3 @@ this.remove();

this.parent.insertBefore(after, this.next);
this.text = this.domNode.data;
this.text = this.statics.value(this.domNode);
return after;

@@ -1357,3 +1517,3 @@ };

})) {
this.text = this.domNode.data;
this.text = this.statics.value(this.domNode);
}

@@ -1372,94 +1532,2 @@ };

/***/ },
/* 15 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var attributor_1 = __webpack_require__(6);
function match(node, prefix) {
var className = node.getAttribute('class') || '';
return className.split(/\s+/).filter(function (name) {
return name.indexOf(prefix + "-") === 0;
});
}
var ClassAttributor = (function (_super) {
__extends(ClassAttributor, _super);
function ClassAttributor() {
_super.apply(this, arguments);
}
ClassAttributor.prototype.add = function (node, value) {
if (!this.canAdd(node, value))
return false;
this.remove(node);
node.classList.add(this.keyName + "-" + value);
return true;
};
ClassAttributor.prototype.remove = function (node) {
var matches = match(node, this.keyName);
matches.forEach(function (name) {
node.classList.remove(name);
});
if (node.classList.length === 0) {
node.removeAttribute('class');
}
};
ClassAttributor.prototype.value = function (node) {
var result = match(node, this.keyName)[0] || '';
return result.slice(this.keyName.length + 1); // +1 for hyphen
};
return ClassAttributor;
}(attributor_1.default));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = ClassAttributor;
/***/ },
/* 16 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var attributor_1 = __webpack_require__(6);
function camelize(name) {
var parts = name.split('-');
var rest = parts.slice(1).map(function (part) {
return part[0].toUpperCase() + part.slice(1);
}).join('');
return parts[0] + rest;
}
var StyleAttributor = (function (_super) {
__extends(StyleAttributor, _super);
function StyleAttributor() {
_super.apply(this, arguments);
}
StyleAttributor.prototype.add = function (node, value) {
if (!this.canAdd(node, value))
return false;
node.style[camelize(this.keyName)] = value;
return true;
};
StyleAttributor.prototype.remove = function (node) {
node.style[camelize(this.keyName)] = '';
if (!node.getAttribute('style')) {
node.removeAttribute('style');
}
};
StyleAttributor.prototype.value = function (node) {
return node.style[camelize(this.keyName)];
};
return StyleAttributor;
}(attributor_1.default));
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = StyleAttributor;
/***/ }

@@ -1466,0 +1534,0 @@ /******/ ])

{
"name": "parchment",
"version": "1.0.0-beta",
"version": "1.0.0-beta.1",
"description": "A document model for rich text editors",

@@ -22,8 +22,8 @@ "author": "Jason Chen <jhchen7@gmail.com>",

"karma-coverage": "~0.5.5",
"karma-jasmine": "~0.3.7",
"karma-jasmine": "~0.3.8",
"karma-sauce-launcher": "~0.3.1",
"karma-webpack": "^1.7.0",
"lodash": "^4.5.1",
"lodash": "^4.6.1",
"ts-loader": "^0.8.1",
"typescript": "^1.8.7",
"typescript": "^1.8.9",
"vinyl-source-stream": "^1.1.0",

@@ -30,0 +30,0 @@ "webpack": "^1.12.14"

@@ -21,3 +21,3 @@ # Parchment [![Build Status](https://travis-ci.org/quilljs/parchment.svg?branch=master)](http://travis-ci.org/quilljs/parchment) [![Coverage Status](https://coveralls.io/repos/github/quilljs/parchment/badge.svg?branch=master)](https://coveralls.io/github/quilljs/parchment?branch=master)

```javascript
```typescript
class Blot {

@@ -62,2 +62,7 @@ static blotName: string;

/** Leaf Blots only **/
// Returns the value represented by domNode if it is this Blot's type
// No checking that domNode can represent this Blot type is required so
// applications needing it should check externally before calling.
static value(domNode): any;

@@ -79,2 +84,8 @@ // Given location represented by node and offset from DOM Selection Range,

/** Parent blots only **/
// Whitelist array of Blots that can be direct children.
static allowedChildren: Blot[];
// Default child blot to be inserted if this blot becomes empty.
static defaultChild: string;

@@ -92,2 +103,6 @@ children: LinkedList<Blot>;

/** Formattable blots only **/
// Returns format values represented by domNode if it is this Blot's type
// No checking that domNode is this Blot's type is required.
static formats(domNode: Node);

@@ -106,3 +121,3 @@ // Apply format to blot. Should not pass onto child or other blot.

```javascript
```typescript
import Parchment from 'parchment';

@@ -118,2 +133,6 @@

}
static formats(domNode) {
return domNode.getAttribute('href') || true;
}

@@ -130,3 +149,3 @@ format(name, value) {

let formats = super.formats();
formats['link'] = this.domNode.getAttribute('href') || true;
formats['link'] = LinkBlot.formats(this.domNode);
return formats;

@@ -166,3 +185,3 @@ }

```js
```typescript
class Attributor {

@@ -212,3 +231,3 @@ attrName: string;

let Align = new Parchment.Attributor.Style('align', 'blot-align'};
let Align = new Parchment.Attributor.Class('align', 'blot-align'};
Parchment.register(Align);

@@ -242,3 +261,3 @@

```js
```typescript
// Creates a blot given a name or DOM node.

@@ -245,0 +264,0 @@ // When given just a scope, creates blot the same name as scope

@@ -15,2 +15,8 @@ import * as Registry from '../registry';

static keys(node: HTMLElement): string[] {
return [].map.call(node.attributes, function(item) {
return item.name;
});
}
constructor(attrName: string, keyName: string, options: AttributorOptions = {}) {

@@ -17,0 +23,0 @@ this.attrName = attrName;

@@ -12,2 +12,8 @@ import Attributor from './attributor';

class ClassAttributor extends Attributor {
static keys(node: HTMLElement): string[] {
return (node.getAttribute('class') || '').split(/\s+/).map(function(name) {
return name.split('-').slice(0, -1).join('-');
});
}
add(node: HTMLElement, value: string): boolean {

@@ -14,0 +20,0 @@ if (!this.canAdd(node, value)) return false;

import Attributor from './attributor';
import ClassAttributor from './class';
import StyleAttributor from './style';
import { Formattable } from '../blot/abstract/blot';

@@ -28,17 +30,5 @@ import * as Registry from '../registry';

this.attributes = {};
let attributes = [], classes = [], styles = [];
[].slice.call(this.domNode.attributes).forEach((item) => {
if (item.name === 'class') {
classes = item.value.split(/\s+/).map(function(name) {
return name.split('-').slice(0, -1).join('-');
});
} else if (item.name === 'style') {
styles = item.value.split(';').map(function(val) {
let arr = val.split(':');
return arr[0].trim();
});
} else {
attributes.push(item.name);
}
});
let attributes = Attributor.keys(this.domNode);
let classes = ClassAttributor.keys(this.domNode);
let styles = StyleAttributor.keys(this.domNode);
attributes.concat(classes).concat(styles).forEach((name) => {

@@ -45,0 +35,0 @@ let attr = Registry.query(name, Registry.Scope.ATTRIBUTE);

@@ -14,2 +14,9 @@ import Attributor from './attributor';

class StyleAttributor extends Attributor {
static keys(node): string[] {
return (node.getAttribute('style') || '').split(';').map(function(value) {
let arr = value.split(':');
return arr[0].trim();
});
}
add(node: HTMLElement, value: string): boolean {

@@ -16,0 +23,0 @@ if (!this.canAdd(node, value)) return false;

@@ -7,4 +7,5 @@ import { Blot, Parent, Leaf } from './blot';

abstract class ContainerBlot extends ShadowBlot implements Parent {
static child: string;
class ContainerBlot extends ShadowBlot implements Parent {
static defaultChild: string;
static allowedChildren: any[];

@@ -97,2 +98,7 @@ children: LinkedList<Blot>;

insertBefore(childBlot: Blot, refBlot?: Blot): void {
if (this.statics.allowedChildren != null && !this.statics.allowedChildren.some(function(child) {
return childBlot instanceof child;
})) {
throw new Registry.ParchmentError(`Cannot insert ${(<ShadowBlot>childBlot).statics.blotName} into ${this.statics.blotName}`);
}
childBlot.insertInto(this, refBlot);

@@ -102,3 +108,2 @@ }

length(): number {
// TODO can we use descendants?
return this.children.reduce(function(memo, child) {

@@ -118,4 +123,4 @@ return memo + child.length();

if (this.children.length === 0) {
if (this.statics.childless != null) {
let child = Registry.create(this.statics.childless);
if (this.statics.defaultChild != null) {
let child = Registry.create(this.statics.defaultChild);
this.appendChild(child);

@@ -122,0 +127,0 @@ child.optimize();

@@ -9,5 +9,14 @@ import Attributor from '../../attributor/attributor';

abstract class FormatBlot extends ContainerBlot implements Formattable {
class FormatBlot extends ContainerBlot implements Formattable {
protected attributes: AttributorStore;
static formats(domNode): any {
if (typeof this.tagName === 'string') {
return true;
} else if (Array.isArray(this.tagName)) {
return domNode.tagName.toLowerCase();
}
return undefined;
}
attach(): void {

@@ -26,4 +35,2 @@ super.attach();

}
} else if (name === this.statics.blotName) {
this.replaceWith(Registry.create(this.statics.scope));
}

@@ -34,4 +41,5 @@ }

let formats = this.attributes.values();
if ((<any>Registry.query(this.statics.scope)).blotName !== this.statics.blotName) {
formats[this.statics.blotName] = Array.isArray(this.statics.tagName) ? this.domNode.tagName.toLowerCase() : true;
let format = this.statics.formats(this.domNode);
if (format != null) {
formats[this.statics.blotName] = format;
}

@@ -38,0 +46,0 @@ return formats;

@@ -6,8 +6,25 @@ import { Formattable, Leaf } from './blot';

abstract class LeafBlot extends ShadowBlot implements Leaf {
class LeafBlot extends ShadowBlot implements Leaf {
static scope = Registry.Scope.INLINE_BLOT;
abstract index(node: Node, offset: number): number;
abstract position(index: number, inclusive: boolean): [Node, number];
abstract value(): any;
static value(domNode: Node): any {
return true;
}
index(node, offset): number {
if (node !== this.domNode) return -1;
return Math.min(offset, 1);
}
position(index: number, inclusive?: boolean): [Node, number] {
let offset = [].indexOf.call(this.parent.domNode.childNodes, this.domNode);
if (index > 0) offset += 1;
return [this.parent.domNode, offset];
}
value(): any {
let value = {};
value[this.statics.blotName] = this.statics.value(this.domNode) || true;
return value;
}
}

@@ -14,0 +31,0 @@

@@ -1,6 +0,6 @@

import { Blot, Parent } from './blot';
import { Blot, Parent, Formattable } from './blot';
import * as Registry from '../../registry';
abstract class ShadowBlot implements Blot {
class ShadowBlot implements Blot {
static blotName = 'abstract';

@@ -15,15 +15,15 @@ static className: string;

abstract length(): number;
// Hack for accessing inherited static methods
get statics(): any {
let statics = <any>this.constructor;
return {
blotName: statics.blotName,
childless: statics.childless,
className: statics.className,
scope: statics.scope,
tagName: statics.tagName
};
return [
'blotName', 'className', 'scope', 'tagName',
'defaultChild', 'allowedChildren',
'create', 'formats', 'value'
].reduce((memo, key) => {
let value = (<any>this.constructor)[key];
if (value != null) {
memo[key] = value;
}
return memo;
}, {});
}

@@ -37,4 +37,7 @@

if (Array.isArray(this.tagName)) {
if (typeof value === 'string' && parseInt(value).toString() === value) {
value = parseInt(value);
if (typeof value === 'string') {
value = value.toUpperCase();
if (parseInt(value).toString() === value) {
value = parseInt(value);
}
}

@@ -68,3 +71,3 @@ if (typeof value === 'number') {

let domNode = this.domNode.cloneNode();
return <Blot>Registry.create(domNode);
return Registry.create(domNode);
}

@@ -82,9 +85,11 @@

findOffset(node: Node): number {
return node === this.domNode ? 0 : -1;
}
formatAt(index: number, length: number, name: string, value: any): void {
let blot = this.isolate(index, length);
blot.wrap(name, value);
if (Registry.query(name, Registry.Scope.BLOT) != null) {
blot.wrap(name, value);
} else if (Registry.query(name, Registry.Scope.ATTRIBUTE) != null) {
let parent = <Parent & Formattable>Registry.create(this.statics.scope);
blot.wrap(parent);
parent.format(name, value);
}
}

@@ -118,2 +123,6 @@

length(): number {
return 1;
};
offset(root: Blot = this.parent): number {

@@ -125,3 +134,3 @@ if (this.parent == null || this == root) return 0;

optimize(): void {
// TODO clean up
// TODO clean up once we use WeakMap
if (this.domNode[Registry.DATA_KEY] != null) {

@@ -128,0 +137,0 @@ delete this.domNode[Registry.DATA_KEY].mutations;

@@ -10,2 +10,15 @@ import FormatBlot from './abstract/format';

static formats(domNode): any {
if (domNode.tagName === BlockBlot.tagName) return undefined;
return super.formats(domNode);
}
format(name: string, value: any) {
if (name === this.statics.blotName && !value) {
this.replaceWith(BlockBlot.blotName);
} else {
super.format(name, value);
}
}
formatAt(index: number, length: number, name: string, value: any): void {

@@ -20,3 +33,4 @@ if (Registry.query(name, Registry.Scope.BLOCK) != null) {

insertAt(index: number, value: string, def?: any): void {
if (def == null || Registry.query(value, Registry.Scope.INLINE) != null) { // Insert text or inline
if (def == null || Registry.query(value, Registry.Scope.INLINE) != null) {
// Insert text or inline
super.insertAt(index, value, def);

@@ -23,0 +37,0 @@ } else {

@@ -21,2 +21,15 @@ import FormatBlot from './abstract/format';

static formats(domNode): any {
if (domNode.tagName === InlineBlot.tagName) return undefined;
return super.formats(domNode);
}
format(name: string, value: any) {
if (name === this.statics.blotName && !value) {
this.replaceWith(InlineBlot.blotName);
} else {
super.format(name, value);
}
}
formatAt(index: number, length: number, name: string, value: any): void {

@@ -23,0 +36,0 @@ if (this.formats()[name] != null || Registry.query(name, Registry.Scope.ATTRIBUTE)) {

@@ -19,3 +19,3 @@ import { Blot } from './abstract/blot';

static blotName = 'scroll';
static childless = 'block';
static defaultChild = 'block';
static scope = Registry.Scope.BLOCK_BLOT;

@@ -66,6 +66,7 @@ static tagName = 'DIV';

super.optimize();
mutations.push.apply(mutations, this.observer.takeRecords());
// TODO use WeakMap
mutations = mutations.concat(this.observer.takeRecords());
let mark = (blot: Blot) => {
if (blot == null || blot === this) return;
if (blot.domNode.parentNode == null) return;
if (blot.domNode[Registry.DATA_KEY].mutations == null) {

@@ -85,7 +86,8 @@ blot.domNode[Registry.DATA_KEY].mutations = [];

}
for (let i = 0; mutations.length > 0; i += 1) {
let remaining = mutations;
for (let i = 0; remaining.length > 0; i += 1) {
if (i >= MAX_OPTIMIZE_ITERATIONS) {
throw new Error('[Parchment] Maximum optimize iterations reached');
}
mutations.forEach(function(mutation) {
remaining.forEach(function(mutation) {
let blot = Registry.find(mutation.target, true);

@@ -105,3 +107,4 @@ if (blot != null && blot.domNode === mutation.target && mutation.type === 'childList') {

this.children.forEach(optimize);
mutations = this.observer.takeRecords();
remaining = this.observer.takeRecords();
mutations.push.apply(mutations, remaining);
}

@@ -124,9 +127,8 @@ }

}).forEach((blot: Blot) => {
if (blot == null) return;
if (blot === this) {
super.update(blot.domNode[Registry.DATA_KEY].mutations);
} else {
blot.update(blot.domNode[Registry.DATA_KEY].mutations);
}
if (blot == null || blot === this) return;
blot.update(blot.domNode[Registry.DATA_KEY].mutations || []);
});
if (this.domNode[Registry.DATA_KEY].mutations != null) {
super.update(this.domNode[Registry.DATA_KEY].mutations);
}
this.optimize(mutations);

@@ -133,0 +135,0 @@ }

@@ -17,5 +17,9 @@ import { Blot, Leaf } from './abstract/blot';

static value(domNode: Text): string {
return domNode.data;
}
constructor(node: Node) {
super(node);
this.text = this.domNode.data;
this.text = this.statics.value(this.domNode);
}

@@ -49,3 +53,3 @@

super.optimize();
this.text = this.domNode.data;
this.text = this.statics.value(this.domNode);
if (this.text.length === 0) {

@@ -70,3 +74,3 @@ this.remove();

this.parent.insertBefore(after, this.next);
this.text = this.domNode.data;
this.text = this.statics.value(this.domNode);
return after;

@@ -79,3 +83,3 @@ }

})) {
this.text = this.domNode.data;
this.text = this.statics.value(this.domNode);
}

@@ -82,0 +86,0 @@ }

import ContainerBlot from './blot/abstract/container';
import FormatBlot from './blot/abstract/format';
import LeafBlot from './blot/abstract/leaf';
import EmbedBlot from './blot/abstract/embed';

@@ -9,2 +8,3 @@ import ScrollBlot from './blot/scroll';

import BlockBlot from './blot/block';
import EmbedBlot from './blot/embed';
import TextBlot from './blot/text';

@@ -39,3 +39,3 @@

Attributor: {
Attributor: Attributor,
Attribute: Attributor,
Class: ClassAttributor,

@@ -42,0 +42,0 @@ Style: StyleAttributor,

@@ -52,9 +52,4 @@ import Attributor from './attributor/attributor';

if (match == null) {
throw new ParchmentError(`Unable to create ${input}`);
throw new ParchmentError(`Unable to create ${input} blot`);
}
if (match instanceof Attributor) {
let blot = <Formattable>create(match.scope & Scope.LEVEL);
blot.format(<string>input, value);
return blot;
}
let BlotClass = <BlotConstructor>match;

@@ -78,3 +73,3 @@ let node = input instanceof Node ? input : BlotClass.create(value);

match = types['text'];
} if (typeof query === 'number') {
} else if (typeof query === 'number') {
if (query & Scope.LEVEL & Scope.BLOCK) {

@@ -97,3 +92,2 @@ match = types['block'];

export function register(...Definitions) {

@@ -119,5 +113,11 @@ if (Definitions.length > 1) {

if (Definition.tagName != null) {
if (Array.isArray(Definition.tagName)) {
Definition.tagName = Definition.tagName.map(function(tagName) {
return tagName.toUpperCase();
});
} else {
Definition.tagName = Definition.tagName.toUpperCase();
}
let tagNames = Array.isArray(Definition.tagName) ? Definition.tagName : [Definition.tagName];
tagNames.forEach(function(tag) {
tag = tag.toUpperCase();
if (tags[tag] == null || Definition.className == null) {

@@ -124,0 +124,0 @@ tags[tag] = Definition;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc