public void TestParse() { var margin1Arg = new MarginStyle("20px"); Assert.IsTrue(margin1Arg.IsValid); Assert.AreEqual(20, margin1Arg.Top.Value); Assert.AreEqual(SizeUnit.Pixels, margin1Arg.Top.Unit); Assert.AreEqual(20, margin1Arg.Right.Value); Assert.AreEqual(SizeUnit.Pixels, margin1Arg.Right.Unit); Assert.AreEqual(20, margin1Arg.Bottom.Value); Assert.AreEqual(SizeUnit.Pixels, margin1Arg.Bottom.Unit); Assert.AreEqual(20, margin1Arg.Left.Value); Assert.AreEqual(SizeUnit.Pixels, margin1Arg.Left.Unit); var margin2Arg = new MarginStyle("40px 0"); Assert.IsTrue(margin2Arg.IsValid); Assert.AreEqual(40, margin2Arg.Top.Value); Assert.AreEqual(SizeUnit.Pixels, margin2Arg.Top.Unit); Assert.AreEqual(0, margin2Arg.Right.Value); Assert.AreEqual(SizeUnit.Pixels, margin2Arg.Right.Unit); Assert.AreEqual(40, margin2Arg.Bottom.Value); Assert.AreEqual(SizeUnit.Pixels, margin2Arg.Bottom.Unit); Assert.AreEqual(0, margin2Arg.Left.Value); Assert.AreEqual(SizeUnit.Pixels, margin2Arg.Left.Unit); var margin3Arg = new MarginStyle("50px 10% 0"); Assert.IsTrue(margin3Arg.IsValid); Assert.AreEqual(50, margin3Arg.Top.Value); Assert.AreEqual(SizeUnit.Pixels, margin3Arg.Top.Unit); Assert.AreEqual(10, margin3Arg.Right.Value); Assert.AreEqual(SizeUnit.Percents, margin3Arg.Right.Unit); Assert.AreEqual(0, margin3Arg.Bottom.Value); Assert.AreEqual(SizeUnit.Pixels, margin3Arg.Bottom.Unit); Assert.AreEqual(10, margin3Arg.Left.Value); Assert.AreEqual(SizeUnit.Percents, margin3Arg.Left.Unit); var margin4Args = new MarginStyle("10px 0 5px 50%"); Assert.IsTrue(margin4Args.IsValid); Assert.AreEqual(10, margin4Args.Top.Value); Assert.AreEqual(SizeUnit.Pixels, margin4Args.Top.Unit); Assert.AreEqual(0, margin4Args.Right.Value); Assert.AreEqual(SizeUnit.Pixels, margin4Args.Right.Unit); Assert.AreEqual(5, margin4Args.Bottom.Value); Assert.AreEqual(SizeUnit.Pixels, margin4Args.Bottom.Unit); Assert.AreEqual(50, margin4Args.Left.Value); Assert.AreEqual(SizeUnit.Percents, margin4Args.Left.Unit); }
private static List <NewImageInfo> ScanImages(IBlogPostHtmlEditor currentEditor, IEditorAccount editorAccount, ContentEditor editor, bool useDefaultTargetSettings) { List <NewImageInfo> newImages = new List <NewImageInfo>(); ApplicationPerformance.ClearEvent("InsertImage"); ApplicationPerformance.StartEvent("InsertImage"); using (new WaitCursor()) { IHTMLElement2 postBodyElement = (IHTMLElement2)((BlogPostHtmlEditorControl)currentEditor).PostBodyElement; if (postBodyElement != null) { foreach (IHTMLElement imgElement in postBodyElement.getElementsByTagName("img")) { string imageSrc = imgElement.getAttribute("srcDelay", 2) as string; if (string.IsNullOrEmpty(imageSrc)) { imageSrc = imgElement.getAttribute("src", 2) as string; } // WinLive 96840 - Copying and pasting images within shared canvas should persist source // decorator settings. "wlCopySrcUrl" is inserted while copy/pasting within canvas. bool copyDecoratorSettings = false; string attributeCopySrcUrl = imgElement.getAttribute("wlCopySrcUrl", 2) as string; if (!string.IsNullOrEmpty(attributeCopySrcUrl)) { copyDecoratorSettings = true; imgElement.removeAttribute("wlCopySrcUrl", 0); } // Check if we need to apply default values for image decorators bool applyDefaultDecorator = true; string attributeNoDefaultDecorator = imgElement.getAttribute("wlNoDefaultDecorator", 2) as string; if (!string.IsNullOrEmpty(attributeNoDefaultDecorator) && string.Compare(attributeNoDefaultDecorator, "TRUE", StringComparison.OrdinalIgnoreCase) == 0) { applyDefaultDecorator = false; imgElement.removeAttribute("wlNoDefaultDecorator", 0); } string applyDefaultMargins = imgElement.getAttribute("wlApplyDefaultMargins", 2) as string; if (!String.IsNullOrEmpty(applyDefaultMargins)) { DefaultImageSettings defaultImageSettings = new DefaultImageSettings(editorAccount.Id, editor.DecoratorsManager); MarginStyle defaultMargin = defaultImageSettings.GetDefaultImageMargin(); // Now apply it to the image imgElement.style.marginTop = String.Format(CultureInfo.InvariantCulture, "{0} px", defaultMargin.Top); imgElement.style.marginLeft = String.Format(CultureInfo.InvariantCulture, "{0} px", defaultMargin.Left); imgElement.style.marginBottom = String.Format(CultureInfo.InvariantCulture, "{0} px", defaultMargin.Bottom); imgElement.style.marginRight = String.Format(CultureInfo.InvariantCulture, "{0} px", defaultMargin.Right); imgElement.removeAttribute("wlApplyDefaultMargins", 0); } if ((UrlHelper.IsFileUrl(imageSrc) || IsFullPath(imageSrc)) && !ContentSourceManager.IsSmartContent(imgElement)) { Uri imageSrcUri = new Uri(imageSrc); try { BlogPostImageData imageData = BlogPostImageDataList.LookupImageDataByInlineUri(editor.ImageList, imageSrcUri); Emoticon emoticon = EmoticonsManager.GetEmoticon(imgElement); if (imageData == null && emoticon != null) { // This is usually an emoticon copy/paste and needs to be cleaned up. Uri inlineImageUri = editor.EmoticonsManager.GetInlineImageUri(emoticon); imgElement.setAttribute("src", UrlHelper.SafeToAbsoluteUri(inlineImageUri), 0); } else if (imageData == null) { if (!File.Exists(imageSrcUri.LocalPath)) { throw new FileNotFoundException(imageSrcUri.LocalPath); } // WinLive 188841: Manually attach the behavior so that the image cannot be selected or resized while its loading. DisabledImageElementBehavior disabledImageBehavior = new DisabledImageElementBehavior(editor.IHtmlEditorComponentContext); disabledImageBehavior.AttachToElement(imgElement); Size sourceImageSize = ImageUtils.GetImageSize(imageSrcUri.LocalPath); ImagePropertiesInfo imageInfo = new ImagePropertiesInfo(imageSrcUri, sourceImageSize, new ImageDecoratorsList(editor.DecoratorsManager, new BlogPostSettingsBag())); DefaultImageSettings defaultImageSettings = new DefaultImageSettings(editorAccount.Id, editor.DecoratorsManager); // Make sure this is set because some imageInfo properties depend on it. imageInfo.ImgElement = imgElement; bool isMetafile = ImageHelper2.IsMetafile(imageSrcUri.LocalPath); ImageClassification imgClass = ImageHelper2.Classify(imageSrcUri.LocalPath); if (!isMetafile && ((imgClass & ImageClassification.AnimatedGif) != ImageClassification.AnimatedGif)) { // WinLive 96840 - Copying and pasting images within shared canvas should persist source // decorator settings. if (copyDecoratorSettings) { // Try to look up the original copied source image. BlogPostImageData imageDataOriginal = BlogPostImageDataList.LookupImageDataByInlineUri(editor.ImageList, new Uri(attributeCopySrcUrl)); if (imageDataOriginal != null && imageDataOriginal.GetImageSourceFile() != null) { // We have the original image reference, so lets make a clone of it. BlogPostSettingsBag originalBag = (BlogPostSettingsBag)imageDataOriginal.ImageDecoratorSettings.Clone(); ImageDecoratorsList originalDecoratorsList = new ImageDecoratorsList(editor.DecoratorsManager, originalBag); ImageFileData originalImageFileData = imageDataOriginal.GetImageSourceFile(); Size originalImageSize = new Size(originalImageFileData.Width, originalImageFileData.Height); imageInfo = new ImagePropertiesInfo(originalImageFileData.Uri, originalImageSize, originalDecoratorsList); } else { // There are probably decorators applied to the image, but in a different editor so we can't access them. // We probably don't want to apply any decorators to this image, so apply blank decorators and load the // image as full size so it looks like it did before. imageInfo.ImageDecorators = defaultImageSettings.LoadBlankLocalImageDecoratorsList(); imageInfo.InlineImageSizeName = ImageSizeName.Full; } } else if (applyDefaultDecorator) { imageInfo.ImageDecorators = defaultImageSettings.LoadDefaultImageDecoratorsList(); if ((imgClass & ImageClassification.TransparentGif) == ImageClassification.TransparentGif) { imageInfo.ImageDecorators.AddDecorator(NoBorderDecorator.Id); } } else { // Don't use default values for decorators imageInfo.ImageDecorators = defaultImageSettings.LoadBlankLocalImageDecoratorsList(); imageInfo.InlineImageSizeName = ImageSizeName.Full; } } else { ImageDecoratorsList decorators = new ImageDecoratorsList(editor.DecoratorsManager, new BlogPostSettingsBag()); decorators.AddDecorator(editor.DecoratorsManager.GetDefaultRemoteImageDecorators()); imageInfo.ImageDecorators = decorators; } imageInfo.ImgElement = imgElement; imageInfo.DhtmlImageViewer = editorAccount.EditorOptions.DhtmlImageViewer; //discover the "natural" target settings from the DOM string linkTargetUrl = imageInfo.LinkTargetUrl; if (linkTargetUrl == imageSrc) { imageInfo.LinkTarget = LinkTargetType.IMAGE; } else if (!String.IsNullOrEmpty(linkTargetUrl) && !UrlHelper.IsFileUrl(linkTargetUrl)) { imageInfo.LinkTarget = LinkTargetType.URL; } else { imageInfo.LinkTarget = LinkTargetType.NONE; } if (useDefaultTargetSettings) { if (!GlobalEditorOptions.SupportsFeature(ContentEditorFeature.SupportsImageClickThroughs) && imageInfo.DefaultLinkTarget == LinkTargetType.IMAGE) { imageInfo.DefaultLinkTarget = LinkTargetType.NONE; } if (imageInfo.LinkTarget == LinkTargetType.NONE) { imageInfo.LinkTarget = imageInfo.DefaultLinkTarget; } if (imageInfo.DefaultLinkOptions.ShowInNewWindow) { imageInfo.LinkOptions.ShowInNewWindow = true; } imageInfo.LinkOptions.UseImageViewer = imageInfo.DefaultLinkOptions.UseImageViewer; imageInfo.LinkOptions.ImageViewerGroupName = imageInfo.DefaultLinkOptions.ImageViewerGroupName; } Size defaultImageSize = defaultImageSettings.GetDefaultInlineImageSize(); Size initialSize = ImageUtils.GetScaledImageSize(defaultImageSize.Width, defaultImageSize.Height, sourceImageSize); // add to list of new images newImages.Add(new NewImageInfo(imageInfo, imgElement, initialSize, disabledImageBehavior)); } else { // When switching blogs, try to adapt image viewer settings according to the blog settings. ImagePropertiesInfo imageInfo = new ImagePropertiesInfo(imageSrcUri, ImageUtils.GetImageSize(imageSrcUri.LocalPath), new ImageDecoratorsList(editor.DecoratorsManager, imageData.ImageDecoratorSettings)); imageInfo.ImgElement = imgElement; // Make sure the new crop and tilt decorators get loaded imageInfo.ImageDecorators.MergeDecorators(DefaultImageSettings.GetImplicitLocalImageDecorators()); string viewer = imageInfo.DhtmlImageViewer; if (viewer != editorAccount.EditorOptions.DhtmlImageViewer) { imageInfo.DhtmlImageViewer = editorAccount.EditorOptions.DhtmlImageViewer; imageInfo.LinkOptions = imageInfo.DefaultLinkOptions; } // If the image is an emoticon, update the EmoticonsManager with the image's uri so that duplicate emoticons can point to the same file. if (emoticon != null) { editor.EmoticonsManager.SetInlineImageUri(emoticon, imageData.InlineImageFile.Uri); } } } catch (ArgumentException e) { Trace.WriteLine("Could not initialize image: " + imageSrc); Trace.WriteLine(e.ToString()); } catch (DirectoryNotFoundException) { Debug.WriteLine("Image file does not exist: " + imageSrc); } catch (FileNotFoundException) { Debug.WriteLine("Image file does not exist: " + imageSrc); } catch (IOException e) { Debug.WriteLine("Image file cannot be read: " + imageSrc + " " + e); DisplayMessage.Show(MessageId.FileInUse, imageSrc); } } } } } return(newImages); }